Home | History | Annotate | Line # | Download | only in isa
fd.c revision 1.80.6.1
      1  1.80.6.1       mjf /*	$NetBSD: fd.c,v 1.80.6.1 2008/04/03 12:42:44 mjf Exp $	*/
      2       1.1   thorpej 
      3       1.1   thorpej /*-
      4      1.48   mycroft  * Copyright (c) 1998, 2003 The NetBSD Foundation, Inc.
      5       1.1   thorpej  * All rights reserved.
      6       1.1   thorpej  *
      7       1.1   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1   thorpej  * by Charles M. Hannum.
      9       1.1   thorpej  *
     10       1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     11       1.1   thorpej  * modification, are permitted provided that the following conditions
     12       1.1   thorpej  * are met:
     13       1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     14       1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     15       1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     17       1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     18       1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     19       1.1   thorpej  *    must display the following acknowledgement:
     20       1.1   thorpej  *        This product includes software developed by the NetBSD
     21       1.1   thorpej  *        Foundation, Inc. and its contributors.
     22       1.1   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.1   thorpej  *    contributors may be used to endorse or promote products derived
     24       1.1   thorpej  *    from this software without specific prior written permission.
     25       1.1   thorpej  *
     26       1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.1   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37       1.1   thorpej  */
     38       1.1   thorpej 
     39       1.1   thorpej /*-
     40       1.1   thorpej  * Copyright (c) 1990 The Regents of the University of California.
     41       1.1   thorpej  * All rights reserved.
     42       1.1   thorpej  *
     43       1.1   thorpej  * This code is derived from software contributed to Berkeley by
     44       1.1   thorpej  * Don Ahn.
     45       1.1   thorpej  *
     46       1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     47       1.1   thorpej  * modification, are permitted provided that the following conditions
     48       1.1   thorpej  * are met:
     49       1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     50       1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     51       1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     52       1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     53       1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     54      1.46       agc  * 3. Neither the name of the University nor the names of its contributors
     55       1.1   thorpej  *    may be used to endorse or promote products derived from this software
     56       1.1   thorpej  *    without specific prior written permission.
     57       1.1   thorpej  *
     58       1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59       1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60       1.1   thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61       1.1   thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62       1.1   thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63       1.1   thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64       1.1   thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65       1.1   thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66       1.1   thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67       1.1   thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68       1.1   thorpej  * SUCH DAMAGE.
     69       1.1   thorpej  *
     70       1.1   thorpej  *	@(#)fd.c	7.4 (Berkeley) 5/25/91
     71       1.1   thorpej  */
     72       1.1   thorpej 
     73       1.1   thorpej /*
     74       1.1   thorpej  * Floppy formatting facilities merged from FreeBSD fd.c driver:
     75       1.1   thorpej  *	Id: fd.c,v 1.53 1995/03/12 22:40:56 joerg Exp
     76       1.1   thorpej  * which carries the same copyright/redistribution notice as shown above with
     77       1.1   thorpej  * the addition of the following statement before the "Redistribution and
     78       1.1   thorpej  * use ..." clause:
     79       1.1   thorpej  *
     80       1.1   thorpej  * Copyright (c) 1993, 1994 by
     81       1.1   thorpej  *  jc (at) irbs.UUCP (John Capo)
     82       1.1   thorpej  *  vak (at) zebub.msk.su (Serge Vakulenko)
     83       1.1   thorpej  *  ache (at) astral.msk.su (Andrew A. Chernov)
     84       1.1   thorpej  *
     85       1.1   thorpej  * Copyright (c) 1993, 1994, 1995 by
     86       1.1   thorpej  *  joerg_wunsch (at) uriah.sax.de (Joerg Wunsch)
     87       1.1   thorpej  *  dufault (at) hda.com (Peter Dufault)
     88       1.1   thorpej  */
     89      1.19     lukem 
     90      1.19     lukem #include <sys/cdefs.h>
     91  1.80.6.1       mjf __KERNEL_RCSID(0, "$NetBSD: fd.c,v 1.80.6.1 2008/04/03 12:42:44 mjf Exp $");
     92       1.1   thorpej 
     93       1.1   thorpej #include "rnd.h"
     94       1.1   thorpej #include "opt_ddb.h"
     95       1.1   thorpej 
     96       1.1   thorpej /*
     97       1.1   thorpej  * XXX This driver should be properly MI'd some day, but this allows us
     98       1.1   thorpej  * XXX to eliminate a lot of code duplication for now.
     99       1.1   thorpej  */
    100      1.16   thorpej #if !defined(alpha) && !defined(algor) && !defined(atari) && \
    101      1.23    simonb     !defined(bebox) && !defined(evbmips) && !defined(i386) && \
    102  1.80.6.1       mjf     !defined(prep) && !defined(sandpoint) && !defined(x86_64) && \
    103  1.80.6.1       mjf     !defined(mvmeppc)
    104       1.1   thorpej #error platform not supported by this driver, yet
    105       1.1   thorpej #endif
    106       1.1   thorpej 
    107       1.1   thorpej #include <sys/param.h>
    108       1.1   thorpej #include <sys/systm.h>
    109       1.1   thorpej #include <sys/callout.h>
    110       1.1   thorpej #include <sys/kernel.h>
    111       1.1   thorpej #include <sys/file.h>
    112       1.1   thorpej #include <sys/ioctl.h>
    113       1.1   thorpej #include <sys/device.h>
    114       1.1   thorpej #include <sys/disklabel.h>
    115       1.1   thorpej #include <sys/disk.h>
    116       1.1   thorpej #include <sys/buf.h>
    117      1.55      yamt #include <sys/bufq.h>
    118       1.1   thorpej #include <sys/malloc.h>
    119       1.1   thorpej #include <sys/uio.h>
    120       1.1   thorpej #include <sys/syslog.h>
    121       1.1   thorpej #include <sys/queue.h>
    122       1.1   thorpej #include <sys/proc.h>
    123       1.1   thorpej #include <sys/fdio.h>
    124      1.16   thorpej #include <sys/conf.h>
    125  1.80.6.1       mjf #include <sys/vnode.h>
    126       1.1   thorpej #if NRND > 0
    127       1.1   thorpej #include <sys/rnd.h>
    128       1.1   thorpej #endif
    129       1.1   thorpej 
    130      1.73   jnemeth #include <prop/proplib.h>
    131      1.73   jnemeth 
    132       1.1   thorpej #include <uvm/uvm_extern.h>
    133       1.1   thorpej 
    134       1.1   thorpej #include <dev/cons.h>
    135       1.1   thorpej 
    136      1.77        ad #include <sys/cpu.h>
    137      1.77        ad #include <sys/bus.h>
    138      1.16   thorpej 
    139      1.53  drochner #include "locators.h"
    140      1.53  drochner 
    141      1.14       leo #if defined(atari)
    142      1.14       leo /*
    143      1.14       leo  * On the atari, it is configured as fdcisa
    144      1.14       leo  */
    145      1.14       leo #define	FDCCF_DRIVE		FDCISACF_DRIVE
    146      1.14       leo #define	FDCCF_DRIVE_DEFAULT	FDCISACF_DRIVE_DEFAULT
    147      1.14       leo 
    148      1.14       leo #define	fd_cd	fdisa_cd
    149      1.16   thorpej #endif /* atari */
    150      1.14       leo 
    151      1.77        ad #include <sys/intr.h>
    152       1.1   thorpej 
    153       1.1   thorpej #include <dev/isa/isavar.h>
    154       1.1   thorpej #include <dev/isa/isadmavar.h>
    155       1.3   thorpej 
    156       1.1   thorpej #include <dev/isa/fdreg.h>
    157       1.3   thorpej #include <dev/isa/fdcvar.h>
    158       1.1   thorpej 
    159       1.9      matt #if defined(i386)
    160       1.5  jdolecek 
    161       1.1   thorpej #include <dev/ic/mc146818reg.h>			/* for NVRAM access */
    162       1.1   thorpej #include <i386/isa/nvram.h>
    163       1.5  jdolecek 
    164       1.5  jdolecek #include "mca.h"
    165       1.5  jdolecek #if NMCA > 0
    166       1.5  jdolecek #include <machine/mca_machdep.h>		/* for MCA_system */
    167       1.5  jdolecek #endif
    168       1.5  jdolecek 
    169       1.9      matt #endif /* i386 */
    170      1.16   thorpej 
    171      1.33  jmcneill #include <dev/isa/fdvar.h>
    172      1.33  jmcneill 
    173       1.1   thorpej #define FDUNIT(dev)	(minor(dev) / 8)
    174       1.1   thorpej #define FDTYPE(dev)	(minor(dev) % 8)
    175       1.1   thorpej 
    176      1.71   reinoud /* (mis)use device use flag to identify format operation */
    177      1.71   reinoud #define B_FORMAT B_DEVPRIVATE
    178       1.1   thorpej 
    179       1.1   thorpej /* controller driver configuration */
    180      1.57     perry int fdprint(void *, const char *);
    181       1.1   thorpej 
    182       1.5  jdolecek #if NMCA > 0
    183       1.5  jdolecek /* MCA - specific entries */
    184      1.13  jdolecek const struct fd_type mca_fd_types[] = {
    185       1.5  jdolecek 	{ 18,2,36,2,0xff,0x0f,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB"    }, /* 1.44MB diskette - XXX try 16ms step rate */
    186       1.5  jdolecek 	{  9,2,18,2,0xff,0x4f,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB"    }, /* 3.5 inch 720kB diskette - XXX try 24ms step rate */
    187       1.5  jdolecek };
    188       1.5  jdolecek #endif /* NMCA > 0 */
    189       1.5  jdolecek 
    190       1.1   thorpej /* The order of entries in the following table is important -- BEWARE! */
    191      1.14       leo 
    192      1.14       leo #if defined(atari)
    193      1.14       leo const struct fd_type fd_types[] = {
    194      1.14       leo 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS,0xf6,1, "360KB/PC" }, /* 360kB PC diskettes */
    195      1.14       leo 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB"    }, /* 3.5 inch 720kB diskette */
    196      1.14       leo 	{ 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB"   }, /* 1.44MB diskette */
    197      1.14       leo };
    198      1.14       leo #else
    199      1.13  jdolecek const struct fd_type fd_types[] = {
    200      1.14       leo 	{ 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB"   }, /* 1.44MB diskette */
    201       1.1   thorpej 	{ 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS,0xf6,1, "1.2MB"    }, /* 1.2 MB AT-diskettes */
    202       1.1   thorpej 	{  9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS,0xf6,1, "360KB/AT" }, /* 360kB in 1.2MB drive */
    203       1.1   thorpej 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS,0xf6,1, "360KB/PC" }, /* 360kB PC diskettes */
    204       1.1   thorpej 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB"    }, /* 3.5 inch 720kB diskette */
    205       1.1   thorpej 	{  9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS,0xf6,1, "720KB/x"  }, /* 720kB in 1.2MB drive */
    206       1.1   thorpej 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS,0xf6,1, "360KB/x"  }, /* 360kB in 720kB drive */
    207       1.1   thorpej };
    208      1.14       leo #endif /* defined(atari) */
    209       1.1   thorpej 
    210      1.79    dyoung void fdcfinishattach(device_t);
    211      1.79    dyoung int fdprobe(device_t, struct cfdata *, void *);
    212      1.79    dyoung void fdattach(device_t, device_t, void *);
    213  1.80.6.1       mjf static int fddetach(device_t, int);
    214  1.80.6.1       mjf static int fdcintr1(struct fdc_softc *);
    215  1.80.6.1       mjf static void fdcintrcb(void *);
    216  1.80.6.1       mjf static bool fdcsuspend(device_t PMF_FN_PROTO);
    217  1.80.6.1       mjf static bool fdcresume(device_t PMF_FN_PROTO);
    218       1.1   thorpej 
    219       1.3   thorpej extern struct cfdriver fd_cd;
    220       1.3   thorpej 
    221      1.41       leo #ifdef atari
    222  1.80.6.1       mjf CFATTACH_DECL_NEW(fdisa, sizeof(struct fd_softc),
    223  1.80.6.1       mjf     fdprobe, fdattach, fddetach, NULL);
    224      1.41       leo #else
    225  1.80.6.1       mjf CFATTACH_DECL_NEW(fd, sizeof(struct fd_softc),
    226  1.80.6.1       mjf     fdprobe, fdattach, fddetach, NULL);
    227      1.41       leo #endif
    228       1.1   thorpej 
    229      1.25   gehenna dev_type_open(fdopen);
    230      1.25   gehenna dev_type_close(fdclose);
    231      1.25   gehenna dev_type_read(fdread);
    232      1.25   gehenna dev_type_write(fdwrite);
    233      1.25   gehenna dev_type_ioctl(fdioctl);
    234      1.25   gehenna dev_type_strategy(fdstrategy);
    235      1.25   gehenna 
    236      1.25   gehenna const struct bdevsw fd_bdevsw = {
    237      1.25   gehenna 	fdopen, fdclose, fdstrategy, fdioctl, nodump, nosize, D_DISK
    238      1.25   gehenna };
    239      1.25   gehenna 
    240      1.25   gehenna const struct cdevsw fd_cdevsw = {
    241      1.25   gehenna 	fdopen, fdclose, fdread, fdwrite, fdioctl,
    242      1.30  jdolecek 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
    243      1.25   gehenna };
    244      1.25   gehenna 
    245      1.57     perry void fdgetdisklabel(struct fd_softc *);
    246      1.57     perry int fd_get_parms(struct fd_softc *);
    247      1.57     perry void fdstart(struct fd_softc *);
    248       1.1   thorpej 
    249      1.68  christos struct dkdriver fddkdriver = { fdstrategy, NULL };
    250       1.1   thorpej 
    251       1.9      matt #if defined(i386)
    252      1.79    dyoung const struct fd_type *fd_nvtotype(const char *, int, int);
    253       1.9      matt #endif /* i386 */
    254      1.57     perry void fd_set_motor(struct fdc_softc *fdc, int reset);
    255      1.57     perry void fd_motor_off(void *arg);
    256      1.57     perry void fd_motor_on(void *arg);
    257      1.57     perry int fdcresult(struct fdc_softc *fdc);
    258      1.57     perry void fdcstart(struct fdc_softc *fdc);
    259      1.79    dyoung void fdcstatus(device_t, int, const char *);
    260      1.57     perry void fdctimeout(void *arg);
    261      1.57     perry void fdcretry(struct fdc_softc *fdc);
    262      1.57     perry void fdfinish(struct fd_softc *fd, struct buf *bp);
    263      1.80    dyoung static const struct fd_type *fd_dev_to_type(struct fd_softc *, dev_t);
    264      1.61  christos int fdformat(dev_t, struct ne7_fd_formb *, struct lwp *);
    265      1.73   jnemeth static void fd_set_properties(struct fd_softc *fd);
    266       1.1   thorpej 
    267      1.79    dyoung void	fd_mountroot_hook(device_t);
    268       1.1   thorpej 
    269       1.1   thorpej /*
    270       1.1   thorpej  * Arguments passed between fdcattach and fdprobe.
    271       1.1   thorpej  */
    272       1.1   thorpej struct fdc_attach_args {
    273       1.1   thorpej 	int fa_drive;
    274      1.13  jdolecek 	const struct fd_type *fa_deftype;
    275       1.1   thorpej };
    276       1.1   thorpej 
    277       1.1   thorpej /*
    278       1.1   thorpej  * Print the location of a disk drive (called just before attaching the
    279       1.1   thorpej  * the drive).  If `fdc' is not NULL, the drive was found but was not
    280       1.1   thorpej  * in the system config file; print the drive name as well.
    281       1.1   thorpej  * Return QUIET (config_find ignores this if the device was configured) to
    282       1.1   thorpej  * avoid printing `fdN not configured' messages.
    283       1.1   thorpej  */
    284       1.1   thorpej int
    285      1.79    dyoung fdprint(void *aux, const char *fdc)
    286       1.1   thorpej {
    287      1.80    dyoung 	struct fdc_attach_args *fa = aux;
    288       1.1   thorpej 
    289       1.1   thorpej 	if (!fdc)
    290      1.32   thorpej 		aprint_normal(" drive %d", fa->fa_drive);
    291       1.1   thorpej 	return QUIET;
    292       1.1   thorpej }
    293       1.1   thorpej 
    294  1.80.6.1       mjf static bool
    295  1.80.6.1       mjf fdcresume(device_t self PMF_FN_ARGS)
    296  1.80.6.1       mjf {
    297  1.80.6.1       mjf 	struct fdc_softc *fdc = device_private(self);
    298  1.80.6.1       mjf 
    299  1.80.6.1       mjf 	(void)fdcintr1(fdc);
    300  1.80.6.1       mjf 	return true;
    301  1.80.6.1       mjf }
    302  1.80.6.1       mjf 
    303  1.80.6.1       mjf static bool
    304  1.80.6.1       mjf fdcsuspend(device_t self PMF_FN_ARGS)
    305  1.80.6.1       mjf {
    306  1.80.6.1       mjf 	struct fdc_softc *fdc = device_private(self);
    307  1.80.6.1       mjf 	int drive;
    308  1.80.6.1       mjf 	struct fd_softc *fd;
    309  1.80.6.1       mjf 
    310  1.80.6.1       mjf 	mutex_enter(&fdc->sc_mtx);
    311  1.80.6.1       mjf 	while (fdc->sc_state != DEVIDLE)
    312  1.80.6.1       mjf 		cv_wait(&fdc->sc_cv, &fdc->sc_mtx);
    313  1.80.6.1       mjf 	for (drive = 0; drive < 4; drive++) {
    314  1.80.6.1       mjf 		if ((fd = fdc->sc_fd[drive]) == NULL)
    315  1.80.6.1       mjf 			continue;
    316  1.80.6.1       mjf 		fd->sc_flags &= ~(FD_MOTOR|FD_MOTOR_WAIT);
    317  1.80.6.1       mjf 	}
    318  1.80.6.1       mjf 	fd_set_motor(fdc, 0);
    319  1.80.6.1       mjf 	mutex_exit(&fdc->sc_mtx);
    320  1.80.6.1       mjf 	return true;
    321  1.80.6.1       mjf }
    322  1.80.6.1       mjf 
    323  1.80.6.1       mjf void
    324  1.80.6.1       mjf fdc_childdet(device_t self, device_t child)
    325  1.80.6.1       mjf {
    326  1.80.6.1       mjf 	struct fdc_softc *fdc = device_private(self);
    327  1.80.6.1       mjf 	struct fd_softc *fd = device_private(child);
    328  1.80.6.1       mjf 	int drive = fd->sc_drive;
    329  1.80.6.1       mjf 
    330  1.80.6.1       mjf 	KASSERT(fdc->sc_fd[drive] == fd); /* but the kid is not my son */
    331  1.80.6.1       mjf 	fdc->sc_fd[drive] = NULL;
    332  1.80.6.1       mjf }
    333  1.80.6.1       mjf 
    334  1.80.6.1       mjf int
    335  1.80.6.1       mjf fdcdetach(device_t self, int flags)
    336  1.80.6.1       mjf {
    337  1.80.6.1       mjf 	int rc;
    338  1.80.6.1       mjf 	struct fdc_softc *fdc = device_private(self);
    339  1.80.6.1       mjf 
    340  1.80.6.1       mjf 	if ((rc = config_detach_children(self, flags)) != 0)
    341  1.80.6.1       mjf 		return rc;
    342  1.80.6.1       mjf 
    343  1.80.6.1       mjf 	pmf_device_deregister(self);
    344  1.80.6.1       mjf 
    345  1.80.6.1       mjf 	isa_dmamap_destroy(fdc->sc_ic, fdc->sc_drq);
    346  1.80.6.1       mjf 	isa_drq_free(fdc->sc_ic, fdc->sc_drq);
    347  1.80.6.1       mjf 
    348  1.80.6.1       mjf 	callout_destroy(&fdc->sc_intr_ch);
    349  1.80.6.1       mjf 	callout_destroy(&fdc->sc_timo_ch);
    350  1.80.6.1       mjf 
    351  1.80.6.1       mjf 	cv_destroy(&fdc->sc_cv);
    352  1.80.6.1       mjf 	mutex_destroy(&fdc->sc_mtx);
    353  1.80.6.1       mjf 
    354  1.80.6.1       mjf 	return 0;
    355  1.80.6.1       mjf }
    356  1.80.6.1       mjf 
    357       1.1   thorpej void
    358      1.79    dyoung fdcattach(struct fdc_softc *fdc)
    359       1.1   thorpej {
    360  1.80.6.1       mjf 	mutex_init(&fdc->sc_mtx, MUTEX_DEFAULT, IPL_BIO);
    361  1.80.6.1       mjf 	cv_init(&fdc->sc_cv, "fdcwakeup");
    362      1.74        ad 	callout_init(&fdc->sc_timo_ch, 0);
    363      1.74        ad 	callout_init(&fdc->sc_intr_ch, 0);
    364       1.1   thorpej 
    365       1.1   thorpej 	fdc->sc_state = DEVIDLE;
    366       1.1   thorpej 	TAILQ_INIT(&fdc->sc_drives);
    367       1.1   thorpej 
    368       1.1   thorpej 	fdc->sc_maxiosize = isa_dmamaxsize(fdc->sc_ic, fdc->sc_drq);
    369      1.42      fvdl 
    370      1.42      fvdl 	if (isa_drq_alloc(fdc->sc_ic, fdc->sc_drq) != 0) {
    371  1.80.6.1       mjf 		aprint_normal_dev(fdc->sc_dev, "can't reserve drq %d\n",
    372      1.79    dyoung 		    fdc->sc_drq);
    373      1.42      fvdl 		return;
    374      1.42      fvdl 	}
    375       1.1   thorpej 
    376       1.1   thorpej 	if (isa_dmamap_create(fdc->sc_ic, fdc->sc_drq, fdc->sc_maxiosize,
    377       1.1   thorpej 	    BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
    378  1.80.6.1       mjf 		aprint_normal_dev(fdc->sc_dev, "can't set up ISA DMA map\n");
    379       1.1   thorpej 		return;
    380       1.1   thorpej 	}
    381      1.49   mycroft 
    382  1.80.6.1       mjf 	config_interrupts(fdc->sc_dev, fdcfinishattach);
    383  1.80.6.1       mjf 
    384  1.80.6.1       mjf 	if (!pmf_device_register(fdc->sc_dev, fdcsuspend, fdcresume)) {
    385  1.80.6.1       mjf 		aprint_error_dev(fdc->sc_dev,
    386  1.80.6.1       mjf 		    "cannot set power mgmt handler\n");
    387  1.80.6.1       mjf 	}
    388      1.49   mycroft }
    389      1.56  drochner 
    390      1.49   mycroft void
    391      1.79    dyoung fdcfinishattach(device_t self)
    392      1.49   mycroft {
    393      1.79    dyoung 	struct fdc_softc *fdc = device_private(self);
    394      1.49   mycroft 	bus_space_tag_t iot = fdc->sc_iot;
    395      1.49   mycroft 	bus_space_handle_t ioh = fdc->sc_ioh;
    396      1.49   mycroft 	struct fdc_attach_args fa;
    397      1.49   mycroft #if defined(i386)
    398      1.49   mycroft 	int type;
    399      1.49   mycroft #endif
    400      1.49   mycroft 
    401      1.56  drochner 	/*
    402      1.15   tsutsui 	 * Reset the controller to get it into a known state. Not all
    403      1.15   tsutsui 	 * probes necessarily need do this to discover the controller up
    404      1.15   tsutsui 	 * front, so don't assume anything.
    405      1.15   tsutsui 	 */
    406      1.15   tsutsui 
    407      1.15   tsutsui 	bus_space_write_1(iot, ioh, fdout, 0);
    408      1.15   tsutsui 	delay(100);
    409      1.15   tsutsui 	bus_space_write_1(iot, ioh, fdout, FDO_FRST);
    410      1.56  drochner 
    411      1.15   tsutsui 	/* see if it can handle a command */
    412      1.15   tsutsui 	if (out_fdc(iot, ioh, NE7CMD_SPECIFY) < 0) {
    413  1.80.6.1       mjf 		aprint_normal_dev(fdc->sc_dev, "can't reset controller\n");
    414      1.15   tsutsui 		return;
    415      1.15   tsutsui 	}
    416      1.15   tsutsui 	out_fdc(iot, ioh, 0xdf);
    417      1.15   tsutsui 	out_fdc(iot, ioh, 2);
    418       1.1   thorpej 
    419       1.9      matt #if defined(i386)
    420       1.1   thorpej 	/*
    421       1.1   thorpej 	 * The NVRAM info only tells us about the first two disks on the
    422       1.1   thorpej 	 * `primary' floppy controller.
    423       1.1   thorpej 	 */
    424      1.66   thorpej 	/* XXX device_unit() abuse */
    425  1.80.6.1       mjf 	if (device_unit(fdc->sc_dev) == 0)
    426       1.1   thorpej 		type = mc146818_read(NULL, NVRAM_DISKETTE); /* XXX softc */
    427       1.1   thorpej 	else
    428       1.1   thorpej 		type = -1;
    429       1.9      matt #endif /* i386 */
    430       1.1   thorpej 
    431       1.1   thorpej 	/* physical limit: four drives per controller. */
    432      1.48   mycroft 	fdc->sc_state = PROBING;
    433       1.1   thorpej 	for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
    434      1.33  jmcneill 		if (fdc->sc_known) {
    435      1.33  jmcneill 			if (fdc->sc_present & (1 << fa.fa_drive)) {
    436      1.33  jmcneill 				fa.fa_deftype = fdc->sc_knownfds[fa.fa_drive];
    437  1.80.6.1       mjf 				config_found(fdc->sc_dev, (void *)&fa,
    438      1.33  jmcneill 				    fdprint);
    439      1.33  jmcneill 			}
    440      1.33  jmcneill 		} else {
    441       1.9      matt #if defined(i386)
    442      1.33  jmcneill 			if (type >= 0 && fa.fa_drive < 2)
    443      1.79    dyoung 				fa.fa_deftype =
    444  1.80.6.1       mjf 				    fd_nvtotype(device_xname(fdc->sc_dev),
    445      1.79    dyoung 				        type, fa.fa_drive);
    446      1.33  jmcneill 			else
    447      1.33  jmcneill 				fa.fa_deftype = NULL;		/* unknown */
    448      1.14       leo #elif defined(atari)
    449      1.33  jmcneill 			/*
    450      1.33  jmcneill 			 * Atari has a different ordening, defaults to 1.44
    451      1.33  jmcneill 			 */
    452      1.33  jmcneill 			fa.fa_deftype = &fd_types[2];
    453       1.1   thorpej #else
    454      1.33  jmcneill 			/*
    455      1.33  jmcneill 			 * Default to 1.44MB on Alpha and BeBox.  How do we tell
    456      1.33  jmcneill 			 * on these platforms?
    457      1.33  jmcneill 			 */
    458      1.33  jmcneill 			fa.fa_deftype = &fd_types[0];
    459       1.9      matt #endif /* i386 */
    460  1.80.6.1       mjf 			(void)config_found_ia(fdc->sc_dev, "fdc", (void *)&fa, fdprint);
    461      1.33  jmcneill 		}
    462       1.1   thorpej 	}
    463      1.48   mycroft 	fdc->sc_state = DEVIDLE;
    464       1.1   thorpej }
    465       1.1   thorpej 
    466       1.1   thorpej int
    467  1.80.6.1       mjf fdprobe(device_t parent, cfdata_t match, void *aux)
    468       1.1   thorpej {
    469      1.79    dyoung 	struct fdc_softc *fdc = device_private(parent);
    470       1.1   thorpej 	struct cfdata *cf = match;
    471       1.1   thorpej 	struct fdc_attach_args *fa = aux;
    472       1.1   thorpej 	int drive = fa->fa_drive;
    473       1.1   thorpej 	bus_space_tag_t iot = fdc->sc_iot;
    474       1.1   thorpej 	bus_space_handle_t ioh = fdc->sc_ioh;
    475       1.1   thorpej 	int n;
    476       1.1   thorpej 
    477       1.1   thorpej 	if (cf->cf_loc[FDCCF_DRIVE] != FDCCF_DRIVE_DEFAULT &&
    478       1.1   thorpej 	    cf->cf_loc[FDCCF_DRIVE] != drive)
    479       1.1   thorpej 		return 0;
    480       1.1   thorpej 	/*
    481       1.1   thorpej 	 * XXX
    482       1.1   thorpej 	 * This is to work around some odd interactions between this driver
    483       1.1   thorpej 	 * and SMC Ethernet cards.
    484       1.1   thorpej 	 */
    485       1.1   thorpej 	if (cf->cf_loc[FDCCF_DRIVE] == FDCCF_DRIVE_DEFAULT && drive >= 2)
    486       1.1   thorpej 		return 0;
    487      1.33  jmcneill 
    488      1.33  jmcneill 	/* Use PNP information if available */
    489      1.33  jmcneill 	if (fdc->sc_known)
    490      1.33  jmcneill 		return 1;
    491       1.1   thorpej 
    492  1.80.6.1       mjf 	mutex_enter(&fdc->sc_mtx);
    493      1.48   mycroft 	/* toss any interrupt status */
    494      1.48   mycroft 	for (n = 0; n < 4; n++) {
    495      1.48   mycroft 		out_fdc(iot, ioh, NE7CMD_SENSEI);
    496      1.48   mycroft 		(void) fdcresult(fdc);
    497      1.48   mycroft 	}
    498       1.1   thorpej 	/* select drive and turn on motor */
    499       1.1   thorpej 	bus_space_write_1(iot, ioh, fdout, drive | FDO_FRST | FDO_MOEN(drive));
    500       1.1   thorpej 	/* wait for motor to spin up */
    501  1.80.6.1       mjf 	/* XXX check sc_probe */
    502  1.80.6.1       mjf 	(void) cv_timedwait(&fdc->sc_cv, &fdc->sc_mtx, hz / 4);
    503       1.1   thorpej 	out_fdc(iot, ioh, NE7CMD_RECAL);
    504       1.1   thorpej 	out_fdc(iot, ioh, drive);
    505      1.47   mycroft 	/* wait for recalibrate, up to 2s */
    506  1.80.6.1       mjf 	/* XXX check sc_probe */
    507  1.80.6.1       mjf 	if (cv_timedwait(&fdc->sc_cv, &fdc->sc_mtx, 2 * hz) != EWOULDBLOCK){
    508      1.52   thorpej #ifdef FD_DEBUG
    509      1.52   thorpej 		/* XXX */
    510      1.52   thorpej 		printf("fdprobe: got intr\n");
    511      1.52   thorpej #endif
    512      1.52   thorpej 	}
    513       1.1   thorpej 	out_fdc(iot, ioh, NE7CMD_SENSEI);
    514       1.1   thorpej 	n = fdcresult(fdc);
    515       1.1   thorpej #ifdef FD_DEBUG
    516       1.1   thorpej 	{
    517       1.1   thorpej 		int i;
    518       1.1   thorpej 		printf("fdprobe: status");
    519       1.1   thorpej 		for (i = 0; i < n; i++)
    520       1.1   thorpej 			printf(" %x", fdc->sc_status[i]);
    521       1.1   thorpej 		printf("\n");
    522       1.1   thorpej 	}
    523       1.1   thorpej #endif
    524       1.1   thorpej 	/* turn off motor */
    525       1.1   thorpej 	bus_space_write_1(iot, ioh, fdout, FDO_FRST);
    526  1.80.6.1       mjf 	mutex_exit(&fdc->sc_mtx);
    527       1.1   thorpej 
    528      1.51    keihan #if defined(bebox)	/* XXX What is this about? --thorpej (at) NetBSD.org */
    529       1.1   thorpej 	if (n != 2 || (fdc->sc_status[1] != 0))
    530       1.1   thorpej 		return 0;
    531       1.1   thorpej #else
    532       1.1   thorpej 	if (n != 2 || (fdc->sc_status[0] & 0xf8) != 0x20)
    533       1.1   thorpej 		return 0;
    534       1.1   thorpej #endif /* bebox */
    535       1.1   thorpej 
    536       1.1   thorpej 	return 1;
    537       1.1   thorpej }
    538       1.1   thorpej 
    539       1.1   thorpej /*
    540       1.1   thorpej  * Controller is working, and drive responded.  Attach it.
    541       1.1   thorpej  */
    542       1.1   thorpej void
    543      1.79    dyoung fdattach(device_t parent, device_t self, void *aux)
    544       1.1   thorpej {
    545      1.79    dyoung 	struct fdc_softc *fdc = device_private(parent);
    546      1.79    dyoung 	struct fd_softc *fd = device_private(self);
    547       1.1   thorpej 	struct fdc_attach_args *fa = aux;
    548      1.13  jdolecek 	const struct fd_type *type = fa->fa_deftype;
    549       1.1   thorpej 	int drive = fa->fa_drive;
    550       1.1   thorpej 
    551  1.80.6.1       mjf 	fd->sc_dev = self;
    552  1.80.6.1       mjf 
    553      1.74        ad 	callout_init(&fd->sc_motoron_ch, 0);
    554      1.74        ad 	callout_init(&fd->sc_motoroff_ch, 0);
    555       1.1   thorpej 
    556       1.1   thorpej 	/* XXX Allow `flags' to override device type? */
    557       1.1   thorpej 
    558       1.1   thorpej 	if (type)
    559  1.80.6.1       mjf 		aprint_normal(": %s, %d cyl, %d head, %d sec\n", type->name,
    560       1.1   thorpej 		    type->cyls, type->heads, type->sectrac);
    561       1.1   thorpej 	else
    562  1.80.6.1       mjf 		aprint_normal(": density unknown\n");
    563       1.1   thorpej 
    564      1.60      yamt 	bufq_alloc(&fd->sc_q, "disksort", BUFQ_SORT_CYLINDER);
    565       1.1   thorpej 	fd->sc_cylin = -1;
    566       1.1   thorpej 	fd->sc_drive = drive;
    567       1.1   thorpej 	fd->sc_deftype = type;
    568       1.1   thorpej 	fdc->sc_fd[drive] = fd;
    569       1.1   thorpej 
    570       1.1   thorpej 	/*
    571       1.1   thorpej 	 * Initialize and attach the disk structure.
    572       1.1   thorpej 	 */
    573  1.80.6.1       mjf 	disk_init(&fd->sc_dk, device_xname(fd->sc_dev), &fddkdriver);
    574       1.1   thorpej 	disk_attach(&fd->sc_dk);
    575       1.1   thorpej 
    576       1.1   thorpej 	/*
    577       1.1   thorpej 	 * Establish a mountroot hook.
    578       1.1   thorpej 	 */
    579  1.80.6.1       mjf 	fd->sc_roothook =
    580  1.80.6.1       mjf 	    mountroothook_establish(fd_mountroot_hook, fd->sc_dev);
    581       1.1   thorpej 
    582       1.1   thorpej #if NRND > 0
    583  1.80.6.1       mjf 	rnd_attach_source(&fd->rnd_source, device_xname(fd->sc_dev),
    584       1.1   thorpej 			  RND_TYPE_DISK, 0);
    585       1.1   thorpej #endif
    586      1.73   jnemeth 
    587      1.73   jnemeth 	fd_set_properties(fd);
    588  1.80.6.1       mjf 
    589  1.80.6.1       mjf 	if (!pmf_device_register(self, NULL, NULL))
    590  1.80.6.1       mjf 		aprint_error_dev(self, "cannot set power mgmt handler\n");
    591  1.80.6.1       mjf }
    592  1.80.6.1       mjf 
    593  1.80.6.1       mjf static int
    594  1.80.6.1       mjf fddetach(device_t self, int flags)
    595  1.80.6.1       mjf {
    596  1.80.6.1       mjf 	struct fd_softc *fd = device_private(self);
    597  1.80.6.1       mjf 	int bmaj, cmaj, i, mn;
    598  1.80.6.1       mjf 
    599  1.80.6.1       mjf 	fd_motor_off(fd);
    600  1.80.6.1       mjf 
    601  1.80.6.1       mjf 	/* locate the major number */
    602  1.80.6.1       mjf 	bmaj = bdevsw_lookup_major(&fd_bdevsw);
    603  1.80.6.1       mjf 	cmaj = cdevsw_lookup_major(&fd_cdevsw);
    604  1.80.6.1       mjf 
    605  1.80.6.1       mjf 	/* Nuke the vnodes for any open instances. */
    606  1.80.6.1       mjf 	for (i = 0; i < MAXPARTITIONS; i++) {
    607  1.80.6.1       mjf 		mn = DISKMINOR(device_unit(self), i);
    608  1.80.6.1       mjf 		vdevgone(bmaj, mn, mn, VBLK);
    609  1.80.6.1       mjf 		vdevgone(cmaj, mn, mn, VCHR);
    610  1.80.6.1       mjf 	}
    611  1.80.6.1       mjf 
    612  1.80.6.1       mjf 	pmf_device_deregister(self);
    613  1.80.6.1       mjf 
    614  1.80.6.1       mjf #if 0 /* XXX need to undo at detach? */
    615  1.80.6.1       mjf 	fd_set_properties(fd);
    616  1.80.6.1       mjf #endif
    617  1.80.6.1       mjf #if NRND > 0
    618  1.80.6.1       mjf 	rnd_detach_source(&fd->rnd_source);
    619  1.80.6.1       mjf #endif
    620  1.80.6.1       mjf 
    621  1.80.6.1       mjf 	disk_detach(&fd->sc_dk);
    622  1.80.6.1       mjf 
    623  1.80.6.1       mjf 	/* Kill off any queued buffers. */
    624  1.80.6.1       mjf 	bufq_drain(fd->sc_q);
    625  1.80.6.1       mjf 
    626  1.80.6.1       mjf 	bufq_free(fd->sc_q);
    627  1.80.6.1       mjf 
    628  1.80.6.1       mjf 	callout_destroy(&fd->sc_motoroff_ch);
    629  1.80.6.1       mjf 	callout_destroy(&fd->sc_motoron_ch);
    630  1.80.6.1       mjf 
    631  1.80.6.1       mjf 	return 0;
    632       1.1   thorpej }
    633       1.1   thorpej 
    634       1.9      matt #if defined(i386)
    635       1.1   thorpej /*
    636       1.1   thorpej  * Translate nvram type into internal data structure.  Return NULL for
    637       1.1   thorpej  * none/unknown/unusable.
    638       1.1   thorpej  */
    639      1.13  jdolecek const struct fd_type *
    640      1.79    dyoung fd_nvtotype(const char *fdc, int nvraminfo, int drive)
    641       1.1   thorpej {
    642       1.1   thorpej 	int type;
    643       1.1   thorpej 
    644       1.1   thorpej 	type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0;
    645       1.1   thorpej 	switch (type) {
    646       1.1   thorpej 	case NVRAM_DISKETTE_NONE:
    647       1.1   thorpej 		return NULL;
    648       1.1   thorpej 	case NVRAM_DISKETTE_12M:
    649       1.1   thorpej 		return &fd_types[1];
    650       1.1   thorpej 	case NVRAM_DISKETTE_TYPE5:
    651       1.1   thorpej 	case NVRAM_DISKETTE_TYPE6:
    652       1.1   thorpej 		/* XXX We really ought to handle 2.88MB format. */
    653       1.1   thorpej 	case NVRAM_DISKETTE_144M:
    654       1.5  jdolecek #if NMCA > 0
    655       1.5  jdolecek 		if (MCA_system)
    656       1.5  jdolecek 			return &mca_fd_types[0];
    657       1.5  jdolecek 		else
    658       1.5  jdolecek #endif /* NMCA > 0 */
    659       1.5  jdolecek 			return &fd_types[0];
    660       1.1   thorpej 	case NVRAM_DISKETTE_360K:
    661       1.1   thorpej 		return &fd_types[3];
    662       1.1   thorpej 	case NVRAM_DISKETTE_720K:
    663       1.5  jdolecek #if NMCA > 0
    664       1.5  jdolecek 		if (MCA_system)
    665       1.5  jdolecek 			return &mca_fd_types[1];
    666       1.5  jdolecek 		else
    667       1.5  jdolecek #endif /* NMCA > 0 */
    668       1.5  jdolecek 			return &fd_types[4];
    669       1.1   thorpej 	default:
    670       1.1   thorpej 		printf("%s: drive %d: unknown device type 0x%x\n",
    671       1.1   thorpej 		    fdc, drive, type);
    672       1.1   thorpej 		return NULL;
    673       1.1   thorpej 	}
    674       1.1   thorpej }
    675       1.9      matt #endif /* i386 */
    676       1.1   thorpej 
    677      1.80    dyoung static const struct fd_type *
    678      1.79    dyoung fd_dev_to_type(struct fd_softc *fd, dev_t dev)
    679       1.1   thorpej {
    680      1.26   thorpej 	u_int type = FDTYPE(dev);
    681       1.1   thorpej 
    682      1.79    dyoung 	if (type > __arraycount(fd_types))
    683       1.1   thorpej 		return NULL;
    684       1.1   thorpej 	return type ? &fd_types[type - 1] : fd->sc_deftype;
    685       1.1   thorpej }
    686       1.1   thorpej 
    687       1.1   thorpej void
    688      1.79    dyoung fdstrategy(struct buf *bp)
    689       1.1   thorpej {
    690  1.80.6.1       mjf 	struct fd_softc *fd = device_lookup_private(&fd_cd, FDUNIT(bp->b_dev));
    691  1.80.6.1       mjf 	struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
    692       1.1   thorpej 	int sz;
    693       1.1   thorpej 
    694       1.1   thorpej 	/* Valid unit, controller, and request? */
    695       1.1   thorpej 	if (bp->b_blkno < 0 ||
    696       1.1   thorpej 	    ((bp->b_bcount % FDC_BSIZE) != 0 &&
    697       1.1   thorpej 	     (bp->b_flags & B_FORMAT) == 0)) {
    698       1.1   thorpej 		bp->b_error = EINVAL;
    699      1.75        ad 		goto done;
    700       1.1   thorpej 	}
    701       1.1   thorpej 
    702       1.1   thorpej 	/* If it's a null transfer, return immediately. */
    703       1.1   thorpej 	if (bp->b_bcount == 0)
    704       1.1   thorpej 		goto done;
    705       1.1   thorpej 
    706       1.1   thorpej 	sz = howmany(bp->b_bcount, FDC_BSIZE);
    707       1.1   thorpej 
    708       1.1   thorpej 	if (bp->b_blkno + sz > fd->sc_type->size) {
    709       1.1   thorpej 		sz = fd->sc_type->size - bp->b_blkno;
    710       1.1   thorpej 		if (sz == 0) {
    711       1.1   thorpej 			/* If exactly at end of disk, return EOF. */
    712       1.1   thorpej 			goto done;
    713       1.1   thorpej 		}
    714       1.1   thorpej 		if (sz < 0) {
    715       1.1   thorpej 			/* If past end of disk, return EINVAL. */
    716       1.1   thorpej 			bp->b_error = EINVAL;
    717      1.75        ad 			goto done;
    718       1.1   thorpej 		}
    719       1.1   thorpej 		/* Otherwise, truncate request. */
    720       1.1   thorpej 		bp->b_bcount = sz << DEV_BSHIFT;
    721       1.1   thorpej 	}
    722       1.1   thorpej 
    723       1.1   thorpej 	bp->b_rawblkno = bp->b_blkno;
    724      1.15   tsutsui  	bp->b_cylinder =
    725      1.15   tsutsui 	    bp->b_blkno / (FDC_BSIZE / DEV_BSIZE) / fd->sc_type->seccyl;
    726       1.1   thorpej 
    727       1.1   thorpej #ifdef FD_DEBUG
    728      1.59  christos 	printf("fdstrategy: b_blkno %llu b_bcount %d blkno %llu cylin %d "
    729      1.59  christos 	    "sz %d\n", (unsigned long long)bp->b_blkno, bp->b_bcount,
    730      1.59  christos 	    (unsigned long long)fd->sc_blkno, bp->b_cylinder, sz);
    731       1.1   thorpej #endif
    732       1.1   thorpej 
    733       1.1   thorpej 	/* Queue transfer on drive, activate drive and controller if idle. */
    734  1.80.6.1       mjf 	mutex_enter(&fdc->sc_mtx);
    735      1.60      yamt 	BUFQ_PUT(fd->sc_q, bp);
    736       1.2   thorpej 	callout_stop(&fd->sc_motoroff_ch);		/* a good idea */
    737       1.1   thorpej 	if (fd->sc_active == 0)
    738       1.1   thorpej 		fdstart(fd);
    739       1.1   thorpej #ifdef DIAGNOSTIC
    740       1.1   thorpej 	else {
    741       1.1   thorpej 		if (fdc->sc_state == DEVIDLE) {
    742       1.1   thorpej 			printf("fdstrategy: controller inactive\n");
    743       1.1   thorpej 			fdcstart(fdc);
    744       1.1   thorpej 		}
    745       1.1   thorpej 	}
    746       1.1   thorpej #endif
    747  1.80.6.1       mjf 	mutex_exit(&fdc->sc_mtx);
    748       1.1   thorpej 	return;
    749       1.1   thorpej 
    750       1.1   thorpej done:
    751       1.1   thorpej 	/* Toss transfer; we're done early. */
    752       1.1   thorpej 	bp->b_resid = bp->b_bcount;
    753       1.1   thorpej 	biodone(bp);
    754       1.1   thorpej }
    755       1.1   thorpej 
    756       1.1   thorpej void
    757      1.79    dyoung fdstart(struct fd_softc *fd)
    758       1.1   thorpej {
    759  1.80.6.1       mjf 	struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
    760      1.22  jdolecek 	int active = !TAILQ_EMPTY(&fdc->sc_drives);
    761       1.1   thorpej 
    762  1.80.6.1       mjf 	KASSERT(mutex_owned(&fdc->sc_mtx));
    763       1.1   thorpej 	/* Link into controller queue. */
    764       1.1   thorpej 	fd->sc_active = 1;
    765       1.1   thorpej 	TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    766       1.1   thorpej 
    767       1.1   thorpej 	/* If controller not already active, start it. */
    768       1.1   thorpej 	if (!active)
    769       1.1   thorpej 		fdcstart(fdc);
    770       1.1   thorpej }
    771       1.1   thorpej 
    772       1.1   thorpej void
    773      1.79    dyoung fdfinish(struct fd_softc *fd, struct buf *bp)
    774       1.1   thorpej {
    775  1.80.6.1       mjf 	struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
    776       1.1   thorpej 
    777       1.1   thorpej 	/*
    778       1.1   thorpej 	 * Move this drive to the end of the queue to give others a `fair'
    779       1.1   thorpej 	 * chance.  We only force a switch if N operations are completed while
    780       1.1   thorpej 	 * another drive is waiting to be serviced, since there is a long motor
    781       1.1   thorpej 	 * startup delay whenever we switch.
    782       1.1   thorpej 	 */
    783      1.60      yamt 	(void)BUFQ_GET(fd->sc_q);
    784      1.22  jdolecek 	if (TAILQ_NEXT(fd, sc_drivechain) && ++fd->sc_ops >= 8) {
    785       1.1   thorpej 		fd->sc_ops = 0;
    786       1.1   thorpej 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
    787      1.60      yamt 		if (BUFQ_PEEK(fd->sc_q) != NULL)
    788       1.1   thorpej 			TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    789       1.1   thorpej 		else
    790       1.1   thorpej 			fd->sc_active = 0;
    791       1.1   thorpej 	}
    792       1.1   thorpej 	bp->b_resid = fd->sc_bcount;
    793       1.1   thorpej 	fd->sc_skip = 0;
    794       1.1   thorpej 
    795       1.1   thorpej #if NRND > 0
    796       1.1   thorpej 	rnd_add_uint32(&fd->rnd_source, bp->b_blkno);
    797       1.1   thorpej #endif
    798       1.1   thorpej 
    799       1.1   thorpej 	biodone(bp);
    800       1.1   thorpej 	/* turn off motor 5s from now */
    801       1.2   thorpej 	callout_reset(&fd->sc_motoroff_ch, 5 * hz, fd_motor_off, fd);
    802       1.1   thorpej 	fdc->sc_state = DEVIDLE;
    803       1.1   thorpej }
    804       1.1   thorpej 
    805       1.1   thorpej int
    806      1.70  christos fdread(dev_t dev, struct uio *uio, int flags)
    807       1.1   thorpej {
    808       1.1   thorpej 
    809       1.1   thorpej 	return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio));
    810       1.1   thorpej }
    811       1.1   thorpej 
    812       1.1   thorpej int
    813      1.70  christos fdwrite(dev_t dev, struct uio *uio, int flags)
    814       1.1   thorpej {
    815       1.1   thorpej 
    816       1.1   thorpej 	return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio));
    817       1.1   thorpej }
    818       1.1   thorpej 
    819       1.1   thorpej void
    820      1.79    dyoung fd_set_motor(struct fdc_softc *fdc, int reset)
    821       1.1   thorpej {
    822       1.1   thorpej 	struct fd_softc *fd;
    823       1.1   thorpej 	u_char status;
    824       1.1   thorpej 	int n;
    825       1.1   thorpej 
    826      1.22  jdolecek 	if ((fd = TAILQ_FIRST(&fdc->sc_drives)) != NULL)
    827       1.1   thorpej 		status = fd->sc_drive;
    828       1.1   thorpej 	else
    829       1.1   thorpej 		status = 0;
    830       1.1   thorpej 	if (!reset)
    831       1.1   thorpej 		status |= FDO_FRST | FDO_FDMAEN;
    832       1.1   thorpej 	for (n = 0; n < 4; n++)
    833       1.1   thorpej 		if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
    834       1.1   thorpej 			status |= FDO_MOEN(n);
    835       1.1   thorpej 	bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, fdout, status);
    836       1.1   thorpej }
    837       1.1   thorpej 
    838       1.1   thorpej void
    839      1.79    dyoung fd_motor_off(void *arg)
    840       1.1   thorpej {
    841       1.1   thorpej 	struct fd_softc *fd = arg;
    842      1.79    dyoung 	struct fdc_softc *fdc;
    843       1.1   thorpej 
    844  1.80.6.1       mjf 	fdc = device_private(device_parent(fd->sc_dev));
    845      1.79    dyoung 
    846  1.80.6.1       mjf 	mutex_enter(&fdc->sc_mtx);
    847       1.1   thorpej 	fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
    848      1.79    dyoung 	fd_set_motor(fdc, 0);
    849  1.80.6.1       mjf 	mutex_exit(&fdc->sc_mtx);
    850       1.1   thorpej }
    851       1.1   thorpej 
    852       1.1   thorpej void
    853      1.79    dyoung fd_motor_on(void *arg)
    854       1.1   thorpej {
    855       1.1   thorpej 	struct fd_softc *fd = arg;
    856  1.80.6.1       mjf 	struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
    857       1.1   thorpej 
    858  1.80.6.1       mjf 	mutex_enter(&fdc->sc_mtx);
    859       1.1   thorpej 	fd->sc_flags &= ~FD_MOTOR_WAIT;
    860      1.79    dyoung 	if (TAILQ_FIRST(&fdc->sc_drives) == fd && fdc->sc_state == MOTORWAIT)
    861  1.80.6.1       mjf 		(void)fdcintr1(fdc);
    862  1.80.6.1       mjf 	mutex_exit(&fdc->sc_mtx);
    863       1.1   thorpej }
    864       1.1   thorpej 
    865       1.1   thorpej int
    866      1.79    dyoung fdcresult(struct fdc_softc *fdc)
    867       1.1   thorpej {
    868       1.1   thorpej 	bus_space_tag_t iot = fdc->sc_iot;
    869       1.1   thorpej 	bus_space_handle_t ioh = fdc->sc_ioh;
    870       1.1   thorpej 	u_char i;
    871      1.26   thorpej 	u_int j = 100000,
    872      1.26   thorpej 	      n = 0;
    873       1.1   thorpej 
    874       1.1   thorpej 	for (; j; j--) {
    875       1.1   thorpej 		i = bus_space_read_1(iot, ioh, fdsts) &
    876       1.1   thorpej 		    (NE7_DIO | NE7_RQM | NE7_CB);
    877       1.1   thorpej 		if (i == NE7_RQM)
    878       1.1   thorpej 			return n;
    879       1.1   thorpej 		if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
    880       1.1   thorpej 			if (n >= sizeof(fdc->sc_status)) {
    881       1.1   thorpej 				log(LOG_ERR, "fdcresult: overrun\n");
    882       1.1   thorpej 				return -1;
    883       1.1   thorpej 			}
    884       1.1   thorpej 			fdc->sc_status[n++] =
    885       1.1   thorpej 			    bus_space_read_1(iot, ioh, fddata);
    886       1.1   thorpej 		}
    887       1.1   thorpej 		delay(10);
    888       1.1   thorpej 	}
    889       1.1   thorpej 	log(LOG_ERR, "fdcresult: timeout\n");
    890       1.1   thorpej 	return -1;
    891       1.1   thorpej }
    892       1.1   thorpej 
    893       1.1   thorpej int
    894      1.79    dyoung out_fdc(bus_space_tag_t iot, bus_space_handle_t ioh, u_char x)
    895       1.1   thorpej {
    896      1.47   mycroft 	u_char i;
    897      1.47   mycroft 	u_int j = 100000;
    898       1.1   thorpej 
    899      1.47   mycroft 	for (; j; j--) {
    900      1.47   mycroft 		i = bus_space_read_1(iot, ioh, fdsts) &
    901      1.47   mycroft 		    (NE7_DIO | NE7_RQM);
    902      1.47   mycroft 		if (i == NE7_RQM) {
    903      1.47   mycroft 			bus_space_write_1(iot, ioh, fddata, x);
    904      1.47   mycroft 			return 0;
    905      1.47   mycroft 		}
    906      1.47   mycroft 		delay(10);
    907      1.47   mycroft 	}
    908      1.47   mycroft 	return -1;
    909       1.1   thorpej }
    910       1.1   thorpej 
    911       1.1   thorpej int
    912      1.70  christos fdopen(dev_t dev, int flags, int mode, struct lwp *l)
    913       1.1   thorpej {
    914       1.1   thorpej 	struct fd_softc *fd;
    915      1.13  jdolecek 	const struct fd_type *type;
    916       1.1   thorpej 
    917  1.80.6.1       mjf 	fd = device_lookup_private(&fd_cd, FDUNIT(dev));
    918       1.7   thorpej 	if (fd == NULL)
    919       1.7   thorpej 		return (ENXIO);
    920       1.7   thorpej 
    921       1.1   thorpej 	type = fd_dev_to_type(fd, dev);
    922       1.1   thorpej 	if (type == NULL)
    923       1.1   thorpej 		return ENXIO;
    924       1.1   thorpej 
    925       1.1   thorpej 	if ((fd->sc_flags & FD_OPEN) != 0 &&
    926       1.1   thorpej 	    memcmp(fd->sc_type, type, sizeof(*type)))
    927       1.1   thorpej 		return EBUSY;
    928       1.1   thorpej 
    929       1.1   thorpej 	fd->sc_type_copy = *type;
    930       1.1   thorpej 	fd->sc_type = &fd->sc_type_copy;
    931       1.1   thorpej 	fd->sc_cylin = -1;
    932       1.1   thorpej 	fd->sc_flags |= FD_OPEN;
    933       1.1   thorpej 
    934      1.73   jnemeth 	fd_set_properties(fd);
    935      1.73   jnemeth 
    936       1.1   thorpej 	return 0;
    937       1.1   thorpej }
    938       1.1   thorpej 
    939       1.1   thorpej int
    940      1.70  christos fdclose(dev_t dev, int flags, int mode, struct lwp *l)
    941       1.1   thorpej {
    942  1.80.6.1       mjf 	struct fd_softc *fd =
    943  1.80.6.1       mjf 	    device_lookup_private(&fd_cd, FDUNIT(dev));
    944       1.1   thorpej 
    945       1.1   thorpej 	fd->sc_flags &= ~FD_OPEN;
    946       1.1   thorpej 	fd->sc_opts &= ~(FDOPT_NORETRY|FDOPT_SILENT);
    947       1.1   thorpej 	return 0;
    948       1.1   thorpej }
    949       1.1   thorpej 
    950       1.1   thorpej void
    951      1.79    dyoung fdcstart(struct fdc_softc *fdc)
    952       1.1   thorpej {
    953       1.1   thorpej 
    954  1.80.6.1       mjf 	KASSERT(mutex_owned(&fdc->sc_mtx));
    955  1.80.6.1       mjf 
    956  1.80.6.1       mjf 	if (!device_is_active(fdc->sc_dev))
    957  1.80.6.1       mjf 		return;
    958  1.80.6.1       mjf 
    959       1.1   thorpej #ifdef DIAGNOSTIC
    960       1.1   thorpej 	/* only got here if controller's drive queue was inactive; should
    961       1.1   thorpej 	   be in idle state */
    962       1.1   thorpej 	if (fdc->sc_state != DEVIDLE) {
    963       1.1   thorpej 		printf("fdcstart: not idle\n");
    964       1.1   thorpej 		return;
    965       1.1   thorpej 	}
    966       1.1   thorpej #endif
    967  1.80.6.1       mjf 	(void)fdcintr1(fdc);
    968       1.1   thorpej }
    969       1.1   thorpej 
    970       1.1   thorpej void
    971      1.79    dyoung fdcstatus(device_t dv, int n, const char *s)
    972       1.1   thorpej {
    973      1.79    dyoung 	struct fdc_softc *fdc = device_private(device_parent(dv));
    974       1.1   thorpej 	char bits[64];
    975       1.1   thorpej 
    976       1.1   thorpej 	if (n == 0) {
    977       1.1   thorpej 		out_fdc(fdc->sc_iot, fdc->sc_ioh, NE7CMD_SENSEI);
    978       1.1   thorpej 		(void) fdcresult(fdc);
    979       1.1   thorpej 		n = 2;
    980       1.1   thorpej 	}
    981       1.1   thorpej 
    982      1.79    dyoung 	aprint_normal_dev(dv, "%s", s);
    983       1.1   thorpej 
    984       1.1   thorpej 	switch (n) {
    985       1.1   thorpej 	case 0:
    986       1.1   thorpej 		printf("\n");
    987       1.1   thorpej 		break;
    988       1.1   thorpej 	case 2:
    989       1.1   thorpej 		printf(" (st0 %s cyl %d)\n",
    990       1.1   thorpej 		    bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS,
    991       1.1   thorpej 		    bits, sizeof(bits)), fdc->sc_status[1]);
    992       1.1   thorpej 		break;
    993       1.1   thorpej 	case 7:
    994       1.1   thorpej 		printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
    995       1.1   thorpej 		    NE7_ST0BITS, bits, sizeof(bits)));
    996       1.1   thorpej 		printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
    997       1.1   thorpej 		    NE7_ST1BITS, bits, sizeof(bits)));
    998       1.1   thorpej 		printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
    999       1.1   thorpej 		    NE7_ST2BITS, bits, sizeof(bits)));
   1000       1.1   thorpej 		printf(" cyl %d head %d sec %d)\n",
   1001       1.1   thorpej 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
   1002       1.1   thorpej 		break;
   1003       1.1   thorpej #ifdef DIAGNOSTIC
   1004       1.1   thorpej 	default:
   1005       1.1   thorpej 		printf("\nfdcstatus: weird size");
   1006       1.1   thorpej 		break;
   1007       1.1   thorpej #endif
   1008       1.1   thorpej 	}
   1009       1.1   thorpej }
   1010       1.1   thorpej 
   1011       1.1   thorpej void
   1012      1.79    dyoung fdctimeout(void *arg)
   1013       1.1   thorpej {
   1014       1.1   thorpej 	struct fdc_softc *fdc = arg;
   1015      1.22  jdolecek 	struct fd_softc *fd = TAILQ_FIRST(&fdc->sc_drives);
   1016       1.1   thorpej 
   1017  1.80.6.1       mjf 	mutex_enter(&fdc->sc_mtx);
   1018       1.1   thorpej #ifdef DEBUG
   1019      1.15   tsutsui 	log(LOG_ERR, "fdctimeout: state %d\n", fdc->sc_state);
   1020       1.1   thorpej #endif
   1021  1.80.6.1       mjf 	fdcstatus(fd->sc_dev, 0, "timeout");
   1022       1.1   thorpej 
   1023      1.60      yamt 	if (BUFQ_PEEK(fd->sc_q) != NULL)
   1024       1.1   thorpej 		fdc->sc_state++;
   1025       1.1   thorpej 	else
   1026       1.1   thorpej 		fdc->sc_state = DEVIDLE;
   1027       1.1   thorpej 
   1028  1.80.6.1       mjf 	(void)fdcintr1(fdc);
   1029  1.80.6.1       mjf 	mutex_exit(&fdc->sc_mtx);
   1030       1.1   thorpej }
   1031       1.1   thorpej 
   1032  1.80.6.1       mjf static int
   1033  1.80.6.1       mjf fdcintr1(struct fdc_softc *fdc)
   1034       1.1   thorpej {
   1035       1.1   thorpej #define	st0	fdc->sc_status[0]
   1036       1.1   thorpej #define	cyl	fdc->sc_status[1]
   1037       1.1   thorpej 	struct fd_softc *fd;
   1038       1.1   thorpej 	struct buf *bp;
   1039       1.1   thorpej 	bus_space_tag_t iot = fdc->sc_iot;
   1040       1.1   thorpej 	bus_space_handle_t ioh = fdc->sc_ioh;
   1041       1.1   thorpej 	int read, head, sec, i, nblks;
   1042       1.1   thorpej 	struct fd_type *type;
   1043       1.1   thorpej 	struct ne7_fd_formb *finfo = NULL;
   1044      1.48   mycroft 
   1045  1.80.6.1       mjf 	KASSERT(mutex_owned(&fdc->sc_mtx));
   1046      1.48   mycroft 	if (fdc->sc_state == PROBING) {
   1047      1.52   thorpej #ifdef DEBUG
   1048      1.52   thorpej 		printf("fdcintr: got probe interrupt\n");
   1049      1.52   thorpej #endif
   1050  1.80.6.1       mjf 		fdc->sc_probe++;
   1051  1.80.6.1       mjf 		goto out;
   1052      1.48   mycroft 	}
   1053       1.1   thorpej 
   1054       1.1   thorpej loop:
   1055       1.1   thorpej 	/* Is there a drive for the controller to do a transfer with? */
   1056      1.22  jdolecek 	fd = TAILQ_FIRST(&fdc->sc_drives);
   1057       1.1   thorpej 	if (fd == NULL) {
   1058       1.1   thorpej 		fdc->sc_state = DEVIDLE;
   1059  1.80.6.1       mjf  		goto out;
   1060       1.1   thorpej 	}
   1061       1.1   thorpej 
   1062       1.1   thorpej 	/* Is there a transfer to this drive?  If not, deactivate drive. */
   1063      1.60      yamt 	bp = BUFQ_PEEK(fd->sc_q);
   1064       1.1   thorpej 	if (bp == NULL) {
   1065       1.1   thorpej 		fd->sc_ops = 0;
   1066       1.1   thorpej 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
   1067       1.1   thorpej 		fd->sc_active = 0;
   1068       1.1   thorpej 		goto loop;
   1069       1.1   thorpej 	}
   1070       1.1   thorpej 
   1071       1.1   thorpej 	if (bp->b_flags & B_FORMAT)
   1072       1.1   thorpej 		finfo = (struct ne7_fd_formb *)bp->b_data;
   1073       1.1   thorpej 
   1074       1.1   thorpej 	switch (fdc->sc_state) {
   1075       1.1   thorpej 	case DEVIDLE:
   1076       1.1   thorpej 		fdc->sc_errors = 0;
   1077       1.1   thorpej 		fd->sc_skip = 0;
   1078       1.1   thorpej 		fd->sc_bcount = bp->b_bcount;
   1079       1.1   thorpej 		fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
   1080       1.2   thorpej 		callout_stop(&fd->sc_motoroff_ch);
   1081       1.1   thorpej 		if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
   1082       1.1   thorpej 			fdc->sc_state = MOTORWAIT;
   1083       1.1   thorpej 			return 1;
   1084       1.1   thorpej 		}
   1085       1.1   thorpej 		if ((fd->sc_flags & FD_MOTOR) == 0) {
   1086       1.1   thorpej 			/* Turn on the motor, being careful about pairing. */
   1087       1.1   thorpej 			struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
   1088       1.1   thorpej 			if (ofd && ofd->sc_flags & FD_MOTOR) {
   1089       1.2   thorpej 				callout_stop(&ofd->sc_motoroff_ch);
   1090       1.1   thorpej 				ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
   1091       1.1   thorpej 			}
   1092       1.1   thorpej 			fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
   1093       1.1   thorpej 			fd_set_motor(fdc, 0);
   1094       1.1   thorpej 			fdc->sc_state = MOTORWAIT;
   1095       1.1   thorpej 			/* Allow .25s for motor to stabilize. */
   1096       1.2   thorpej 			callout_reset(&fd->sc_motoron_ch, hz / 4,
   1097       1.1   thorpej 			    fd_motor_on, fd);
   1098       1.1   thorpej 			return 1;
   1099       1.1   thorpej 		}
   1100       1.1   thorpej 		/* Make sure the right drive is selected. */
   1101       1.1   thorpej 		fd_set_motor(fdc, 0);
   1102       1.1   thorpej 
   1103       1.1   thorpej 		/* fall through */
   1104       1.1   thorpej 	case DOSEEK:
   1105       1.1   thorpej 	doseek:
   1106       1.1   thorpej 		if (fd->sc_cylin == bp->b_cylinder)
   1107       1.1   thorpej 			goto doio;
   1108       1.1   thorpej 
   1109       1.1   thorpej 		out_fdc(iot, ioh, NE7CMD_SPECIFY);/* specify command */
   1110       1.1   thorpej 		out_fdc(iot, ioh, fd->sc_type->steprate);
   1111       1.1   thorpej 		out_fdc(iot, ioh, 6);		/* XXX head load time == 6ms */
   1112       1.1   thorpej 
   1113       1.1   thorpej 		out_fdc(iot, ioh, NE7CMD_SEEK);	/* seek function */
   1114      1.15   tsutsui 		out_fdc(iot, ioh, fd->sc_drive); /* drive number */
   1115       1.1   thorpej 		out_fdc(iot, ioh, bp->b_cylinder * fd->sc_type->step);
   1116       1.1   thorpej 
   1117       1.1   thorpej 		fd->sc_cylin = -1;
   1118       1.1   thorpej 		fdc->sc_state = SEEKWAIT;
   1119       1.1   thorpej 
   1120      1.67     blymn 		iostat_seek(fd->sc_dk.dk_stats);
   1121       1.1   thorpej 		disk_busy(&fd->sc_dk);
   1122       1.1   thorpej 
   1123       1.1   thorpej 		callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
   1124       1.1   thorpej 		return 1;
   1125       1.1   thorpej 
   1126       1.1   thorpej 	case DOIO:
   1127       1.1   thorpej 	doio:
   1128       1.1   thorpej 		type = fd->sc_type;
   1129       1.1   thorpej 		if (finfo)
   1130       1.1   thorpej 			fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) -
   1131       1.1   thorpej 				      (char *)finfo;
   1132       1.1   thorpej 		sec = fd->sc_blkno % type->seccyl;
   1133       1.1   thorpej 		nblks = type->seccyl - sec;
   1134       1.1   thorpej 		nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
   1135       1.1   thorpej 		nblks = min(nblks, fdc->sc_maxiosize / FDC_BSIZE);
   1136       1.1   thorpej 		fd->sc_nblks = nblks;
   1137       1.1   thorpej 		fd->sc_nbytes = finfo ? bp->b_bcount : nblks * FDC_BSIZE;
   1138       1.1   thorpej 		head = sec / type->sectrac;
   1139       1.1   thorpej 		sec -= head * type->sectrac;
   1140       1.1   thorpej #ifdef DIAGNOSTIC
   1141      1.15   tsutsui 		{
   1142      1.15   tsutsui 			int block;
   1143      1.15   tsutsui 			block = (fd->sc_cylin * type->heads + head)
   1144      1.15   tsutsui 			    * type->sectrac + sec;
   1145      1.15   tsutsui 			if (block != fd->sc_blkno) {
   1146      1.35      tron 				printf("fdcintr: block %d != blkno "
   1147      1.35      tron 				    "%" PRId64 "\n", block, fd->sc_blkno);
   1148       1.1   thorpej #ifdef DDB
   1149      1.15   tsutsui 				 Debugger();
   1150       1.1   thorpej #endif
   1151      1.15   tsutsui 			}
   1152      1.15   tsutsui 		}
   1153       1.1   thorpej #endif
   1154       1.1   thorpej 		read = bp->b_flags & B_READ ? DMAMODE_READ : DMAMODE_WRITE;
   1155       1.1   thorpej 		isa_dmastart(fdc->sc_ic, fdc->sc_drq,
   1156      1.72  christos 		    (char *)bp->b_data + fd->sc_skip, fd->sc_nbytes,
   1157       1.1   thorpej 		    NULL, read | DMAMODE_DEMAND, BUS_DMA_NOWAIT);
   1158       1.1   thorpej 		bus_space_write_1(iot, fdc->sc_fdctlioh, 0, type->rate);
   1159       1.1   thorpej #ifdef FD_DEBUG
   1160       1.1   thorpej 		printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n",
   1161       1.1   thorpej 			read ? "read" : "write", fd->sc_drive, fd->sc_cylin,
   1162       1.1   thorpej 			head, sec, nblks);
   1163       1.1   thorpej #endif
   1164       1.1   thorpej 		if (finfo) {
   1165       1.1   thorpej 			/* formatting */
   1166       1.1   thorpej 			if (out_fdc(iot, ioh, NE7CMD_FORMAT) < 0) {
   1167       1.1   thorpej 				fdc->sc_errors = 4;
   1168       1.1   thorpej 				fdcretry(fdc);
   1169       1.1   thorpej 				goto loop;
   1170       1.1   thorpej 			}
   1171       1.1   thorpej 			out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
   1172       1.1   thorpej 			out_fdc(iot, ioh, finfo->fd_formb_secshift);
   1173       1.1   thorpej 			out_fdc(iot, ioh, finfo->fd_formb_nsecs);
   1174       1.1   thorpej 			out_fdc(iot, ioh, finfo->fd_formb_gaplen);
   1175       1.1   thorpej 			out_fdc(iot, ioh, finfo->fd_formb_fillbyte);
   1176       1.1   thorpej 		} else {
   1177       1.1   thorpej 			if (read)
   1178       1.1   thorpej 				out_fdc(iot, ioh, NE7CMD_READ);	/* READ */
   1179       1.1   thorpej 			else
   1180       1.1   thorpej 				out_fdc(iot, ioh, NE7CMD_WRITE); /* WRITE */
   1181       1.1   thorpej 			out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
   1182       1.1   thorpej 			out_fdc(iot, ioh, fd->sc_cylin); /* track */
   1183       1.1   thorpej 			out_fdc(iot, ioh, head);
   1184       1.1   thorpej 			out_fdc(iot, ioh, sec + 1);	 /* sector +1 */
   1185       1.1   thorpej 			out_fdc(iot, ioh, type->secsize);/* sector size */
   1186       1.1   thorpej 			out_fdc(iot, ioh, type->sectrac);/* sectors/track */
   1187       1.1   thorpej 			out_fdc(iot, ioh, type->gap1);	 /* gap1 size */
   1188       1.1   thorpej 			out_fdc(iot, ioh, type->datalen);/* data length */
   1189       1.1   thorpej 		}
   1190       1.1   thorpej 		fdc->sc_state = IOCOMPLETE;
   1191       1.1   thorpej 
   1192       1.1   thorpej 		disk_busy(&fd->sc_dk);
   1193       1.1   thorpej 
   1194       1.1   thorpej 		/* allow 2 seconds for operation */
   1195       1.1   thorpej 		callout_reset(&fdc->sc_timo_ch, 2 * hz, fdctimeout, fdc);
   1196       1.1   thorpej 		return 1;				/* will return later */
   1197       1.1   thorpej 
   1198       1.1   thorpej 	case SEEKWAIT:
   1199       1.1   thorpej 		callout_stop(&fdc->sc_timo_ch);
   1200       1.1   thorpej 		fdc->sc_state = SEEKCOMPLETE;
   1201       1.1   thorpej 		/* allow 1/50 second for heads to settle */
   1202  1.80.6.1       mjf 		callout_reset(&fdc->sc_intr_ch, hz / 50, fdcintrcb, fdc);
   1203       1.1   thorpej 		return 1;
   1204       1.1   thorpej 
   1205       1.1   thorpej 	case SEEKCOMPLETE:
   1206      1.31       mrg 		/* no data on seek */
   1207      1.31       mrg 		disk_unbusy(&fd->sc_dk, 0, 0);
   1208       1.1   thorpej 
   1209       1.1   thorpej 		/* Make sure seek really happened. */
   1210       1.1   thorpej 		out_fdc(iot, ioh, NE7CMD_SENSEI);
   1211       1.1   thorpej 		if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 ||
   1212       1.1   thorpej 		    cyl != bp->b_cylinder * fd->sc_type->step) {
   1213       1.1   thorpej #ifdef FD_DEBUG
   1214       1.1   thorpej 			fdcstatus(&fd->sc_dev, 2, "seek failed");
   1215       1.1   thorpej #endif
   1216       1.1   thorpej 			fdcretry(fdc);
   1217       1.1   thorpej 			goto loop;
   1218       1.1   thorpej 		}
   1219       1.1   thorpej 		fd->sc_cylin = bp->b_cylinder;
   1220       1.1   thorpej 		goto doio;
   1221       1.1   thorpej 
   1222       1.1   thorpej 	case IOTIMEDOUT:
   1223       1.1   thorpej 		isa_dmaabort(fdc->sc_ic, fdc->sc_drq);
   1224       1.1   thorpej 	case SEEKTIMEDOUT:
   1225       1.1   thorpej 	case RECALTIMEDOUT:
   1226       1.1   thorpej 	case RESETTIMEDOUT:
   1227       1.1   thorpej 		fdcretry(fdc);
   1228       1.1   thorpej 		goto loop;
   1229       1.1   thorpej 
   1230       1.1   thorpej 	case IOCOMPLETE: /* IO DONE, post-analyze */
   1231       1.1   thorpej 		callout_stop(&fdc->sc_timo_ch);
   1232       1.1   thorpej 
   1233      1.31       mrg 		disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
   1234      1.31       mrg 		    (bp->b_flags & B_READ));
   1235       1.1   thorpej 
   1236       1.1   thorpej 		if (fdcresult(fdc) != 7 || (st0 & 0xf8) != 0) {
   1237       1.1   thorpej 			isa_dmaabort(fdc->sc_ic, fdc->sc_drq);
   1238       1.1   thorpej #ifdef FD_DEBUG
   1239       1.1   thorpej 			fdcstatus(&fd->sc_dev, 7, bp->b_flags & B_READ ?
   1240       1.1   thorpej 			    "read failed" : "write failed");
   1241      1.59  christos 			printf("blkno %llu nblks %d\n",
   1242      1.59  christos 			    (unsigned long long)fd->sc_blkno, fd->sc_nblks);
   1243       1.1   thorpej #endif
   1244       1.1   thorpej 			fdcretry(fdc);
   1245       1.1   thorpej 			goto loop;
   1246       1.1   thorpej 		}
   1247       1.1   thorpej 		isa_dmadone(fdc->sc_ic, fdc->sc_drq);
   1248       1.1   thorpej 		if (fdc->sc_errors) {
   1249       1.1   thorpej 			diskerr(bp, "fd", "soft error (corrected)", LOG_PRINTF,
   1250      1.79    dyoung 			    fd->sc_skip / FDC_BSIZE, NULL);
   1251       1.1   thorpej 			printf("\n");
   1252       1.1   thorpej 			fdc->sc_errors = 0;
   1253       1.1   thorpej 		}
   1254       1.1   thorpej 		fd->sc_blkno += fd->sc_nblks;
   1255       1.1   thorpej 		fd->sc_skip += fd->sc_nbytes;
   1256       1.1   thorpej 		fd->sc_bcount -= fd->sc_nbytes;
   1257       1.1   thorpej 		if (!finfo && fd->sc_bcount > 0) {
   1258       1.1   thorpej 			bp->b_cylinder = fd->sc_blkno / fd->sc_type->seccyl;
   1259       1.1   thorpej 			goto doseek;
   1260       1.1   thorpej 		}
   1261       1.1   thorpej 		fdfinish(fd, bp);
   1262       1.1   thorpej 		goto loop;
   1263       1.1   thorpej 
   1264       1.1   thorpej 	case DORESET:
   1265       1.1   thorpej 		/* try a reset, keep motor on */
   1266       1.1   thorpej 		fd_set_motor(fdc, 1);
   1267       1.1   thorpej 		delay(100);
   1268       1.1   thorpej 		fd_set_motor(fdc, 0);
   1269       1.1   thorpej 		fdc->sc_state = RESETCOMPLETE;
   1270       1.1   thorpej 		callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc);
   1271       1.1   thorpej 		return 1;			/* will return later */
   1272       1.1   thorpej 
   1273       1.1   thorpej 	case RESETCOMPLETE:
   1274       1.1   thorpej 		callout_stop(&fdc->sc_timo_ch);
   1275       1.1   thorpej 		/* clear the controller output buffer */
   1276       1.1   thorpej 		for (i = 0; i < 4; i++) {
   1277       1.1   thorpej 			out_fdc(iot, ioh, NE7CMD_SENSEI);
   1278       1.1   thorpej 			(void) fdcresult(fdc);
   1279       1.1   thorpej 		}
   1280       1.1   thorpej 
   1281       1.1   thorpej 		/* fall through */
   1282       1.1   thorpej 	case DORECAL:
   1283      1.15   tsutsui 		out_fdc(iot, ioh, NE7CMD_RECAL); /* recalibrate function */
   1284       1.1   thorpej 		out_fdc(iot, ioh, fd->sc_drive);
   1285       1.1   thorpej 		fdc->sc_state = RECALWAIT;
   1286       1.1   thorpej 		callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc);
   1287       1.1   thorpej 		return 1;			/* will return later */
   1288       1.1   thorpej 
   1289       1.1   thorpej 	case RECALWAIT:
   1290       1.1   thorpej 		callout_stop(&fdc->sc_timo_ch);
   1291       1.1   thorpej 		fdc->sc_state = RECALCOMPLETE;
   1292       1.1   thorpej 		/* allow 1/30 second for heads to settle */
   1293  1.80.6.1       mjf 		callout_reset(&fdc->sc_intr_ch, hz / 30, fdcintrcb, fdc);
   1294       1.1   thorpej 		return 1;			/* will return later */
   1295       1.1   thorpej 
   1296       1.1   thorpej 	case RECALCOMPLETE:
   1297       1.1   thorpej 		out_fdc(iot, ioh, NE7CMD_SENSEI);
   1298       1.1   thorpej 		if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
   1299       1.1   thorpej #ifdef FD_DEBUG
   1300       1.1   thorpej 			fdcstatus(&fd->sc_dev, 2, "recalibrate failed");
   1301       1.1   thorpej #endif
   1302       1.1   thorpej 			fdcretry(fdc);
   1303       1.1   thorpej 			goto loop;
   1304       1.1   thorpej 		}
   1305       1.1   thorpej 		fd->sc_cylin = 0;
   1306       1.1   thorpej 		goto doseek;
   1307       1.1   thorpej 
   1308       1.1   thorpej 	case MOTORWAIT:
   1309       1.1   thorpej 		if (fd->sc_flags & FD_MOTOR_WAIT)
   1310       1.1   thorpej 			return 1;		/* time's not up yet */
   1311       1.1   thorpej 		goto doseek;
   1312       1.1   thorpej 
   1313       1.1   thorpej 	default:
   1314  1.80.6.1       mjf 		fdcstatus(fd->sc_dev, 0, "stray interrupt");
   1315       1.1   thorpej 		return 1;
   1316       1.1   thorpej 	}
   1317       1.1   thorpej #undef	st0
   1318       1.1   thorpej #undef	cyl
   1319  1.80.6.1       mjf 
   1320  1.80.6.1       mjf out:
   1321  1.80.6.1       mjf 	cv_signal(&fdc->sc_cv);
   1322  1.80.6.1       mjf 	return 1;
   1323  1.80.6.1       mjf }
   1324  1.80.6.1       mjf 
   1325  1.80.6.1       mjf static void
   1326  1.80.6.1       mjf fdcintrcb(void *arg)
   1327  1.80.6.1       mjf {
   1328  1.80.6.1       mjf 	(void)fdcintr(arg);
   1329  1.80.6.1       mjf }
   1330  1.80.6.1       mjf 
   1331  1.80.6.1       mjf int
   1332  1.80.6.1       mjf fdcintr(void *arg)
   1333  1.80.6.1       mjf {
   1334  1.80.6.1       mjf 	int rc;
   1335  1.80.6.1       mjf 	struct fdc_softc *fdc = arg;
   1336  1.80.6.1       mjf 
   1337  1.80.6.1       mjf 	mutex_enter(&fdc->sc_mtx);
   1338  1.80.6.1       mjf 	rc = fdcintr1(fdc);
   1339  1.80.6.1       mjf 	mutex_exit(&fdc->sc_mtx);
   1340  1.80.6.1       mjf 	return rc;
   1341       1.1   thorpej }
   1342       1.1   thorpej 
   1343       1.1   thorpej void
   1344      1.79    dyoung fdcretry(struct fdc_softc *fdc)
   1345       1.1   thorpej {
   1346       1.1   thorpej 	char bits[64];
   1347       1.1   thorpej 	struct fd_softc *fd;
   1348       1.1   thorpej 	struct buf *bp;
   1349       1.1   thorpej 
   1350      1.22  jdolecek 	fd = TAILQ_FIRST(&fdc->sc_drives);
   1351      1.60      yamt 	bp = BUFQ_PEEK(fd->sc_q);
   1352       1.1   thorpej 
   1353       1.1   thorpej 	if (fd->sc_opts & FDOPT_NORETRY)
   1354       1.1   thorpej 	    goto fail;
   1355       1.1   thorpej 	switch (fdc->sc_errors) {
   1356       1.1   thorpej 	case 0:
   1357       1.1   thorpej 		/* try again */
   1358       1.1   thorpej 		fdc->sc_state = DOSEEK;
   1359       1.1   thorpej 		break;
   1360       1.1   thorpej 
   1361       1.1   thorpej 	case 1: case 2: case 3:
   1362       1.1   thorpej 		/* didn't work; try recalibrating */
   1363       1.1   thorpej 		fdc->sc_state = DORECAL;
   1364       1.1   thorpej 		break;
   1365       1.1   thorpej 
   1366       1.1   thorpej 	case 4:
   1367       1.1   thorpej 		/* still no go; reset the bastard */
   1368       1.1   thorpej 		fdc->sc_state = DORESET;
   1369       1.1   thorpej 		break;
   1370       1.1   thorpej 
   1371       1.1   thorpej 	default:
   1372       1.1   thorpej 	fail:
   1373       1.1   thorpej 		if ((fd->sc_opts & FDOPT_SILENT) == 0) {
   1374       1.1   thorpej 			diskerr(bp, "fd", "hard error", LOG_PRINTF,
   1375      1.79    dyoung 				fd->sc_skip / FDC_BSIZE, NULL);
   1376       1.1   thorpej 
   1377       1.1   thorpej 			printf(" (st0 %s",
   1378       1.1   thorpej 			       bitmask_snprintf(fdc->sc_status[0],
   1379       1.1   thorpej 						NE7_ST0BITS, bits,
   1380       1.1   thorpej 						sizeof(bits)));
   1381       1.1   thorpej 			printf(" st1 %s",
   1382       1.1   thorpej 			       bitmask_snprintf(fdc->sc_status[1],
   1383       1.1   thorpej 						NE7_ST1BITS, bits,
   1384       1.1   thorpej 						sizeof(bits)));
   1385       1.1   thorpej 			printf(" st2 %s",
   1386       1.1   thorpej 			       bitmask_snprintf(fdc->sc_status[2],
   1387       1.1   thorpej 						NE7_ST2BITS, bits,
   1388       1.1   thorpej 						sizeof(bits)));
   1389       1.1   thorpej 			printf(" cyl %d head %d sec %d)\n",
   1390       1.1   thorpej 			       fdc->sc_status[3],
   1391       1.1   thorpej 			       fdc->sc_status[4],
   1392       1.1   thorpej 			       fdc->sc_status[5]);
   1393       1.1   thorpej 		}
   1394       1.1   thorpej 
   1395       1.1   thorpej 		bp->b_error = EIO;
   1396       1.1   thorpej 		fdfinish(fd, bp);
   1397       1.1   thorpej 	}
   1398       1.1   thorpej 	fdc->sc_errors++;
   1399       1.1   thorpej }
   1400       1.1   thorpej 
   1401       1.1   thorpej int
   1402      1.79    dyoung fdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
   1403       1.1   thorpej {
   1404  1.80.6.1       mjf 	struct fd_softc *fd =
   1405  1.80.6.1       mjf 	    device_lookup_private(&fd_cd, FDUNIT(dev));
   1406       1.1   thorpej 	struct fdformat_parms *form_parms;
   1407       1.1   thorpej 	struct fdformat_cmd *form_cmd;
   1408       1.8   nathanw 	struct ne7_fd_formb *fd_formb;
   1409       1.1   thorpej 	struct disklabel buffer;
   1410       1.1   thorpej 	int error;
   1411       1.1   thorpej 	unsigned int scratch;
   1412       1.1   thorpej 	int il[FD_MAX_NSEC + 1];
   1413      1.80    dyoung 	int i, j;
   1414      1.11      fvdl #ifdef __HAVE_OLD_DISKLABEL
   1415      1.11      fvdl 	struct disklabel newlabel;
   1416      1.11      fvdl #endif
   1417       1.1   thorpej 
   1418       1.1   thorpej 	switch (cmd) {
   1419       1.1   thorpej 	case DIOCGDINFO:
   1420      1.11      fvdl #ifdef __HAVE_OLD_DISKLABEL
   1421      1.11      fvdl 	case ODIOCGDINFO:
   1422      1.11      fvdl #endif
   1423       1.1   thorpej 		memset(&buffer, 0, sizeof(buffer));
   1424       1.1   thorpej 
   1425       1.1   thorpej 		buffer.d_secpercyl = fd->sc_type->seccyl;
   1426       1.1   thorpej 		buffer.d_type = DTYPE_FLOPPY;
   1427       1.1   thorpej 		buffer.d_secsize = FDC_BSIZE;
   1428       1.1   thorpej 
   1429       1.1   thorpej 		if (readdisklabel(dev, fdstrategy, &buffer, NULL) != NULL)
   1430       1.1   thorpej 			return EINVAL;
   1431       1.1   thorpej 
   1432      1.11      fvdl #ifdef __HAVE_OLD_DISKLABEL
   1433      1.11      fvdl 		if (cmd == ODIOCGDINFO) {
   1434      1.11      fvdl 			if (buffer.d_npartitions > OLDMAXPARTITIONS)
   1435      1.12      fvdl 				return ENOTTY;
   1436      1.11      fvdl 			memcpy(addr, &buffer, sizeof (struct olddisklabel));
   1437      1.11      fvdl 		} else
   1438      1.11      fvdl #endif
   1439       1.1   thorpej 		*(struct disklabel *)addr = buffer;
   1440       1.1   thorpej 		return 0;
   1441       1.1   thorpej 
   1442       1.1   thorpej 	case DIOCWLABEL:
   1443       1.1   thorpej 		if ((flag & FWRITE) == 0)
   1444       1.1   thorpej 			return EBADF;
   1445       1.1   thorpej 		/* XXX do something */
   1446       1.1   thorpej 		return 0;
   1447       1.1   thorpej 
   1448       1.1   thorpej 	case DIOCWDINFO:
   1449      1.11      fvdl #ifdef __HAVE_OLD_DISKLABEL
   1450      1.11      fvdl 	case ODIOCWDINFO:
   1451      1.11      fvdl #endif
   1452      1.11      fvdl 	{
   1453      1.11      fvdl 		struct disklabel *lp;
   1454      1.11      fvdl 
   1455       1.1   thorpej 		if ((flag & FWRITE) == 0)
   1456       1.1   thorpej 			return EBADF;
   1457      1.11      fvdl #ifdef __HAVE_OLD_DISKLABEL
   1458      1.11      fvdl 		if (cmd == ODIOCWDINFO) {
   1459      1.11      fvdl 			memset(&newlabel, 0, sizeof newlabel);
   1460      1.11      fvdl 			memcpy(&newlabel, addr, sizeof (struct olddisklabel));
   1461      1.11      fvdl 			lp = &newlabel;
   1462      1.11      fvdl 		} else
   1463      1.11      fvdl #endif
   1464      1.11      fvdl 		lp = (struct disklabel *)addr;
   1465       1.1   thorpej 
   1466      1.11      fvdl 		error = setdisklabel(&buffer, lp, 0, NULL);
   1467       1.1   thorpej 		if (error)
   1468       1.1   thorpej 			return error;
   1469       1.1   thorpej 
   1470       1.1   thorpej 		error = writedisklabel(dev, fdstrategy, &buffer, NULL);
   1471       1.1   thorpej 		return error;
   1472      1.11      fvdl 	}
   1473       1.1   thorpej 
   1474       1.1   thorpej 	case FDIOCGETFORMAT:
   1475       1.1   thorpej 		form_parms = (struct fdformat_parms *)addr;
   1476       1.1   thorpej 		form_parms->fdformat_version = FDFORMAT_VERSION;
   1477       1.1   thorpej 		form_parms->nbps = 128 * (1 << fd->sc_type->secsize);
   1478       1.1   thorpej 		form_parms->ncyl = fd->sc_type->cyls;
   1479       1.1   thorpej 		form_parms->nspt = fd->sc_type->sectrac;
   1480       1.1   thorpej 		form_parms->ntrk = fd->sc_type->heads;
   1481       1.1   thorpej 		form_parms->stepspercyl = fd->sc_type->step;
   1482       1.1   thorpej 		form_parms->gaplen = fd->sc_type->gap2;
   1483       1.1   thorpej 		form_parms->fillbyte = fd->sc_type->fillbyte;
   1484       1.1   thorpej 		form_parms->interleave = fd->sc_type->interleave;
   1485       1.1   thorpej 		switch (fd->sc_type->rate) {
   1486       1.1   thorpej 		case FDC_500KBPS:
   1487       1.1   thorpej 			form_parms->xfer_rate = 500 * 1024;
   1488       1.1   thorpej 			break;
   1489       1.1   thorpej 		case FDC_300KBPS:
   1490       1.1   thorpej 			form_parms->xfer_rate = 300 * 1024;
   1491       1.1   thorpej 			break;
   1492       1.1   thorpej 		case FDC_250KBPS:
   1493       1.1   thorpej 			form_parms->xfer_rate = 250 * 1024;
   1494       1.1   thorpej 			break;
   1495       1.1   thorpej 		default:
   1496       1.1   thorpej 			return EINVAL;
   1497       1.1   thorpej 		}
   1498       1.1   thorpej 		return 0;
   1499       1.1   thorpej 
   1500       1.1   thorpej 	case FDIOCSETFORMAT:
   1501       1.1   thorpej 		if((flag & FWRITE) == 0)
   1502       1.1   thorpej 			return EBADF;	/* must be opened for writing */
   1503       1.1   thorpej 		form_parms = (struct fdformat_parms *)addr;
   1504       1.1   thorpej 		if (form_parms->fdformat_version != FDFORMAT_VERSION)
   1505       1.1   thorpej 			return EINVAL;	/* wrong version of formatting prog */
   1506       1.1   thorpej 
   1507       1.1   thorpej 		scratch = form_parms->nbps >> 7;
   1508       1.1   thorpej 		if ((form_parms->nbps & 0x7f) || ffs(scratch) == 0 ||
   1509       1.1   thorpej 		    scratch & ~(1 << (ffs(scratch)-1)))
   1510       1.1   thorpej 			/* not a power-of-two multiple of 128 */
   1511       1.1   thorpej 			return EINVAL;
   1512       1.1   thorpej 
   1513       1.1   thorpej 		switch (form_parms->xfer_rate) {
   1514       1.1   thorpej 		case 500 * 1024:
   1515       1.1   thorpej 			fd->sc_type->rate = FDC_500KBPS;
   1516       1.1   thorpej 			break;
   1517       1.1   thorpej 		case 300 * 1024:
   1518       1.1   thorpej 			fd->sc_type->rate = FDC_300KBPS;
   1519       1.1   thorpej 			break;
   1520       1.1   thorpej 		case 250 * 1024:
   1521       1.1   thorpej 			fd->sc_type->rate = FDC_250KBPS;
   1522       1.1   thorpej 			break;
   1523       1.1   thorpej 		default:
   1524       1.1   thorpej 			return EINVAL;
   1525       1.1   thorpej 		}
   1526       1.1   thorpej 
   1527       1.1   thorpej 		if (form_parms->nspt > FD_MAX_NSEC ||
   1528       1.1   thorpej 		    form_parms->fillbyte > 0xff ||
   1529       1.1   thorpej 		    form_parms->interleave > 0xff)
   1530       1.1   thorpej 			return EINVAL;
   1531       1.1   thorpej 		fd->sc_type->sectrac = form_parms->nspt;
   1532       1.1   thorpej 		if (form_parms->ntrk != 2 && form_parms->ntrk != 1)
   1533       1.1   thorpej 			return EINVAL;
   1534       1.1   thorpej 		fd->sc_type->heads = form_parms->ntrk;
   1535       1.1   thorpej 		fd->sc_type->seccyl = form_parms->nspt * form_parms->ntrk;
   1536       1.1   thorpej 		fd->sc_type->secsize = ffs(scratch)-1;
   1537       1.1   thorpej 		fd->sc_type->gap2 = form_parms->gaplen;
   1538       1.1   thorpej 		fd->sc_type->cyls = form_parms->ncyl;
   1539       1.1   thorpej 		fd->sc_type->size = fd->sc_type->seccyl * form_parms->ncyl *
   1540      1.15   tsutsui 		    form_parms->nbps / DEV_BSIZE;
   1541       1.1   thorpej 		fd->sc_type->step = form_parms->stepspercyl;
   1542       1.1   thorpej 		fd->sc_type->fillbyte = form_parms->fillbyte;
   1543       1.1   thorpej 		fd->sc_type->interleave = form_parms->interleave;
   1544       1.1   thorpej 		return 0;
   1545       1.1   thorpej 
   1546       1.1   thorpej 	case FDIOCFORMAT_TRACK:
   1547       1.1   thorpej 		if((flag & FWRITE) == 0)
   1548       1.1   thorpej 			return EBADF;	/* must be opened for writing */
   1549       1.1   thorpej 		form_cmd = (struct fdformat_cmd *)addr;
   1550       1.1   thorpej 		if (form_cmd->formatcmd_version != FDFORMAT_VERSION)
   1551       1.1   thorpej 			return EINVAL;	/* wrong version of formatting prog */
   1552       1.1   thorpej 
   1553       1.1   thorpej 		if (form_cmd->head >= fd->sc_type->heads ||
   1554       1.1   thorpej 		    form_cmd->cylinder >= fd->sc_type->cyls) {
   1555       1.1   thorpej 			return EINVAL;
   1556       1.1   thorpej 		}
   1557       1.1   thorpej 
   1558      1.56  drochner 		fd_formb = malloc(sizeof(struct ne7_fd_formb),
   1559       1.8   nathanw 		    M_TEMP, M_NOWAIT);
   1560       1.8   nathanw 		if (fd_formb == 0)
   1561       1.8   nathanw 			return ENOMEM;
   1562       1.8   nathanw 
   1563       1.8   nathanw 		fd_formb->head = form_cmd->head;
   1564       1.8   nathanw 		fd_formb->cyl = form_cmd->cylinder;
   1565       1.8   nathanw 		fd_formb->transfer_rate = fd->sc_type->rate;
   1566       1.8   nathanw 		fd_formb->fd_formb_secshift = fd->sc_type->secsize;
   1567       1.8   nathanw 		fd_formb->fd_formb_nsecs = fd->sc_type->sectrac;
   1568       1.8   nathanw 		fd_formb->fd_formb_gaplen = fd->sc_type->gap2;
   1569       1.8   nathanw 		fd_formb->fd_formb_fillbyte = fd->sc_type->fillbyte;
   1570       1.1   thorpej 
   1571       1.1   thorpej 		memset(il, 0, sizeof il);
   1572       1.8   nathanw 		for (j = 0, i = 1; i <= fd_formb->fd_formb_nsecs; i++) {
   1573       1.8   nathanw 			while (il[(j%fd_formb->fd_formb_nsecs)+1])
   1574       1.1   thorpej 				j++;
   1575       1.8   nathanw 			il[(j%fd_formb->fd_formb_nsecs)+1] = i;
   1576       1.1   thorpej 			j += fd->sc_type->interleave;
   1577       1.1   thorpej 		}
   1578       1.8   nathanw 		for (i = 0; i < fd_formb->fd_formb_nsecs; i++) {
   1579       1.8   nathanw 			fd_formb->fd_formb_cylno(i) = form_cmd->cylinder;
   1580       1.8   nathanw 			fd_formb->fd_formb_headno(i) = form_cmd->head;
   1581       1.8   nathanw 			fd_formb->fd_formb_secno(i) = il[i+1];
   1582       1.8   nathanw 			fd_formb->fd_formb_secsize(i) = fd->sc_type->secsize;
   1583       1.1   thorpej 		}
   1584       1.1   thorpej 
   1585      1.61  christos 		error = fdformat(dev, fd_formb, l);
   1586       1.8   nathanw 		free(fd_formb, M_TEMP);
   1587       1.8   nathanw 		return error;
   1588       1.1   thorpej 
   1589       1.1   thorpej 	case FDIOCGETOPTS:		/* get drive options */
   1590       1.1   thorpej 		*(int *)addr = fd->sc_opts;
   1591       1.1   thorpej 		return 0;
   1592       1.1   thorpej 
   1593       1.1   thorpej 	case FDIOCSETOPTS:		/* set drive options */
   1594       1.1   thorpej 		fd->sc_opts = *(int *)addr;
   1595       1.1   thorpej 		return 0;
   1596       1.1   thorpej 
   1597       1.1   thorpej 	default:
   1598       1.1   thorpej 		return ENOTTY;
   1599       1.1   thorpej 	}
   1600       1.1   thorpej 
   1601       1.1   thorpej #ifdef DIAGNOSTIC
   1602       1.1   thorpej 	panic("fdioctl: impossible");
   1603       1.1   thorpej #endif
   1604       1.1   thorpej }
   1605       1.1   thorpej 
   1606       1.1   thorpej int
   1607      1.79    dyoung fdformat(dev_t dev, struct ne7_fd_formb *finfo, struct lwp *l)
   1608       1.1   thorpej {
   1609      1.63      yamt 	int rv = 0;
   1610  1.80.6.1       mjf 	struct fd_softc *fd =
   1611  1.80.6.1       mjf 	    device_lookup_private(&fd_cd, FDUNIT(dev));
   1612       1.1   thorpej 	struct fd_type *type = fd->sc_type;
   1613       1.1   thorpej 	struct buf *bp;
   1614       1.1   thorpej 
   1615       1.1   thorpej 	/* set up a buffer header for fdstrategy() */
   1616      1.78        ad 	bp = getiobuf(NULL, false);
   1617      1.37        pk 	if (bp == NULL)
   1618       1.1   thorpej 		return ENOBUFS;
   1619      1.37        pk 
   1620      1.78        ad 	bp->b_cflags = BC_BUSY;
   1621      1.78        ad 	bp->b_flags = B_PHYS | B_FORMAT;
   1622      1.61  christos 	bp->b_proc = l->l_proc;
   1623       1.1   thorpej 	bp->b_dev = dev;
   1624       1.1   thorpej 
   1625       1.1   thorpej 	/*
   1626       1.1   thorpej 	 * calculate a fake blkno, so fdstrategy() would initiate a
   1627       1.1   thorpej 	 * seek to the requested cylinder
   1628       1.1   thorpej 	 */
   1629       1.1   thorpej 	bp->b_blkno = (finfo->cyl * (type->sectrac * type->heads)
   1630       1.1   thorpej 		       + finfo->head * type->sectrac) * FDC_BSIZE / DEV_BSIZE;
   1631       1.1   thorpej 
   1632       1.1   thorpej 	bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
   1633      1.72  christos 	bp->b_data = (void *)finfo;
   1634       1.1   thorpej 
   1635       1.1   thorpej #ifdef DEBUG
   1636      1.54      yamt 	printf("fdformat: blkno %" PRIx64 " count %x\n",
   1637      1.36    kleink 	    bp->b_blkno, bp->b_bcount);
   1638       1.1   thorpej #endif
   1639       1.1   thorpej 
   1640       1.1   thorpej 	/* now do the format */
   1641       1.1   thorpej 	fdstrategy(bp);
   1642       1.1   thorpej 
   1643       1.1   thorpej 	/* ...and wait for it to complete */
   1644      1.37        pk 	rv = biowait(bp);
   1645      1.63      yamt 	putiobuf(bp);
   1646       1.1   thorpej 	return rv;
   1647       1.1   thorpej }
   1648       1.1   thorpej 
   1649       1.1   thorpej /*
   1650       1.1   thorpej  * Mountroot hook: prompt the user to enter the root file system
   1651       1.1   thorpej  * floppy.
   1652       1.1   thorpej  */
   1653       1.1   thorpej void
   1654      1.79    dyoung fd_mountroot_hook(device_t dev)
   1655       1.1   thorpej {
   1656       1.1   thorpej 	int c;
   1657       1.1   thorpej 
   1658       1.1   thorpej 	printf("Insert filesystem floppy and press return.");
   1659       1.1   thorpej 	cnpollc(1);
   1660       1.1   thorpej 	for (;;) {
   1661       1.1   thorpej 		c = cngetc();
   1662       1.1   thorpej 		if ((c == '\r') || (c == '\n')) {
   1663       1.1   thorpej 			printf("\n");
   1664       1.1   thorpej 			break;
   1665       1.1   thorpej 		}
   1666       1.1   thorpej 	}
   1667       1.1   thorpej 	cnpollc(0);
   1668       1.1   thorpej }
   1669      1.73   jnemeth 
   1670      1.73   jnemeth static void
   1671      1.73   jnemeth fd_set_properties(struct fd_softc *fd)
   1672      1.73   jnemeth {
   1673      1.73   jnemeth 	prop_dictionary_t disk_info, odisk_info, geom;
   1674      1.73   jnemeth 	const struct fd_type *fdt;
   1675      1.73   jnemeth 	int secsize;
   1676      1.73   jnemeth 
   1677      1.73   jnemeth 	fdt = fd->sc_type;
   1678      1.73   jnemeth 	if (fdt == NULL) {
   1679      1.73   jnemeth 		fdt = fd->sc_deftype;
   1680      1.73   jnemeth 		if (fdt == NULL)
   1681      1.73   jnemeth 			return;
   1682      1.73   jnemeth 	}
   1683      1.73   jnemeth 
   1684      1.73   jnemeth 	disk_info = prop_dictionary_create();
   1685      1.73   jnemeth 
   1686      1.73   jnemeth 	geom = prop_dictionary_create();
   1687      1.73   jnemeth 
   1688      1.73   jnemeth 	prop_dictionary_set_uint64(geom, "sectors-per-unit",
   1689      1.73   jnemeth 	    fdt->size);
   1690      1.73   jnemeth 
   1691      1.73   jnemeth 	switch (fdt->secsize) {
   1692      1.73   jnemeth 	case 2:
   1693      1.73   jnemeth 		secsize = 512;
   1694      1.73   jnemeth 		break;
   1695      1.73   jnemeth 	case 3:
   1696      1.73   jnemeth 		secsize = 1024;
   1697      1.73   jnemeth 		break;
   1698      1.73   jnemeth 	default:
   1699      1.73   jnemeth 		secsize = 0;
   1700      1.73   jnemeth 	}
   1701      1.73   jnemeth 
   1702      1.73   jnemeth 	prop_dictionary_set_uint32(geom, "sector-size",
   1703      1.73   jnemeth 	    secsize);
   1704      1.73   jnemeth 
   1705      1.73   jnemeth 	prop_dictionary_set_uint16(geom, "sectors-per-track",
   1706      1.73   jnemeth 	    fdt->sectrac);
   1707      1.73   jnemeth 
   1708      1.73   jnemeth 	prop_dictionary_set_uint16(geom, "tracks-per-cylinder",
   1709      1.73   jnemeth 	    fdt->heads);
   1710      1.73   jnemeth 
   1711      1.73   jnemeth 	prop_dictionary_set_uint64(geom, "cylinders-per-unit",
   1712      1.73   jnemeth 	    fdt->cyls);
   1713      1.73   jnemeth 
   1714      1.73   jnemeth 	prop_dictionary_set(disk_info, "geometry", geom);
   1715      1.73   jnemeth 	prop_object_release(geom);
   1716      1.73   jnemeth 
   1717  1.80.6.1       mjf 	prop_dictionary_set(device_properties(fd->sc_dev),
   1718      1.73   jnemeth 	    "disk-info", disk_info);
   1719      1.73   jnemeth 
   1720      1.73   jnemeth 	/*
   1721      1.73   jnemeth 	 * Don't release disk_info here; we keep a reference to it.
   1722      1.73   jnemeth 	 * disk_detach() will release it when we go away.
   1723      1.73   jnemeth 	 */
   1724      1.73   jnemeth 
   1725      1.73   jnemeth 	odisk_info = fd->sc_dk.dk_info;
   1726      1.73   jnemeth 	fd->sc_dk.dk_info = disk_info;
   1727      1.73   jnemeth 	if (odisk_info)
   1728      1.73   jnemeth 		prop_object_release(odisk_info);
   1729      1.73   jnemeth }
   1730