Home | History | Annotate | Line # | Download | only in ata
wd.c revision 1.64
      1  1.11  deraadt /*
      2  1.41  mycroft  * Copyright (c) 1994 Charles Hannum.
      3   1.1      cgd  * Copyright (c) 1990 The Regents of the University of California.
      4   1.1      cgd  * All rights reserved.
      5   1.1      cgd  *
      6   1.1      cgd  * This code is derived from software contributed to Berkeley by
      7   1.1      cgd  * William Jolitz.
      8   1.1      cgd  *
      9   1.1      cgd  * Redistribution and use in source and binary forms, with or without
     10   1.1      cgd  * modification, are permitted provided that the following conditions
     11   1.1      cgd  * are met:
     12   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     13   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     14   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     16   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     17   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     18   1.1      cgd  *    must display the following acknowledgement:
     19   1.1      cgd  *	This product includes software developed by the University of
     20   1.1      cgd  *	California, Berkeley and its contributors.
     21   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     22   1.1      cgd  *    may be used to endorse or promote products derived from this software
     23   1.1      cgd  *    without specific prior written permission.
     24   1.1      cgd  *
     25   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35   1.1      cgd  * SUCH DAMAGE.
     36   1.1      cgd  *
     37  1.15      cgd  *	from: @(#)wd.c	7.2 (Berkeley) 5/9/91
     38  1.64  mycroft  *	$Id: wd.c,v 1.64 1994/03/10 05:18:33 mycroft Exp $
     39   1.1      cgd  */
     40   1.1      cgd 
     41  1.46  mycroft #define	INSTRUMENT	/* instrumentation stuff by Brad Parker */
     42   1.1      cgd 
     43   1.1      cgd #include "wd.h"
     44  1.43  mycroft #if NWDC > 0
     45   1.1      cgd 
     46  1.29  mycroft #include <sys/param.h>
     47  1.29  mycroft #include <sys/dkbad.h>
     48  1.29  mycroft #include <sys/systm.h>
     49  1.43  mycroft #include <sys/kernel.h>
     50  1.29  mycroft #include <sys/conf.h>
     51  1.29  mycroft #include <sys/file.h>
     52  1.29  mycroft #include <sys/stat.h>
     53  1.29  mycroft #include <sys/ioctl.h>
     54  1.29  mycroft #include <sys/disklabel.h>
     55  1.29  mycroft #include <sys/buf.h>
     56  1.29  mycroft #include <sys/uio.h>
     57  1.29  mycroft #include <sys/malloc.h>
     58  1.64  mycroft #include <sys/device.h>
     59  1.29  mycroft #include <sys/syslog.h>
     60   1.3  deraadt #ifdef INSTRUMENT
     61  1.29  mycroft #include <sys/dkstat.h>
     62   1.3  deraadt #endif
     63  1.29  mycroft 
     64  1.29  mycroft #include <vm/vm.h>
     65  1.29  mycroft 
     66  1.29  mycroft #include <machine/cpu.h>
     67  1.32  mycroft #include <machine/cpufunc.h>
     68  1.29  mycroft #include <machine/pio.h>
     69  1.29  mycroft 
     70  1.29  mycroft #include <i386/isa/isa.h>
     71  1.29  mycroft #include <i386/isa/isa_device.h>
     72  1.29  mycroft #include <i386/isa/icu.h>
     73  1.29  mycroft #include <i386/isa/wdreg.h>
     74   1.1      cgd 
     75  1.41  mycroft #define WDCNDELAY	100000	/* delay = 100us; so 10s for a controller state change */
     76  1.41  mycroft #define WDCDELAY	100
     77  1.64  mycroft #define	RECOVERYTIME	hz/2	/* time to recover from an error */
     78  1.19  deraadt 
     79  1.51  mycroft #if 0
     80  1.64  mycroft /* If you enable this, it will report any delays more than 100us * N long. */
     81  1.64  mycroft #define WDCNDELAY_DEBUG	50
     82  1.51  mycroft #endif
     83  1.23  deraadt 
     84  1.21  deraadt #define	WDIORETRIES	5	/* number of retries before giving up */
     85   1.1      cgd 
     86  1.51  mycroft #define WDUNIT(dev)	((minor(dev) & 0xf8) >> 3)
     87  1.51  mycroft #define WDPART(dev)	((minor(dev) & 0x07)     )
     88  1.37  mycroft #define makewddev(maj, unit, part)	(makedev(maj, ((unit << 3) + part)))
     89   1.1      cgd #define WDRAW	3		/* 'd' partition isn't a partition! */
     90   1.1      cgd 
     91   1.1      cgd #define b_cylin	b_resid		/* cylinder number for doing IO to */
     92   1.1      cgd 				/* shares an entry in the buf struct */
     93   1.1      cgd 
     94   1.1      cgd /*
     95   1.1      cgd  * Drive states.  Used to initialize drive.
     96   1.1      cgd  */
     97  1.64  mycroft #define	CLOSED		0		/* disk is closed */
     98   1.1      cgd #define	WANTOPEN	1		/* open requested, not started */
     99   1.1      cgd #define	RECAL		2		/* doing restore */
    100   1.1      cgd #define	OPEN		3		/* done with open */
    101   1.1      cgd 
    102   1.1      cgd /*
    103  1.37  mycroft  * Drive status.
    104   1.1      cgd  */
    105  1.64  mycroft struct wd_softc {
    106  1.64  mycroft 	struct device sc_dev;
    107   1.9  deraadt 
    108  1.64  mycroft 	long	sc_bcount;	/* byte count left */
    109  1.64  mycroft 	long	sc_mbcount;	/* total byte count left */
    110  1.64  mycroft 	short	sc_skip;	/* blocks already transferred */
    111  1.64  mycroft 	short	sc_mskip;	/* blocks already transferred for multi */
    112  1.64  mycroft 	char	sc_unit;	/* physical unit number */
    113  1.64  mycroft 	char	sc_lunit;	/* logical unit number */
    114  1.64  mycroft 	char	sc_state;	/* control state */
    115  1.64  mycroft 
    116  1.64  mycroft 	u_long  sc_copenpart;   /* character units open on this drive */
    117  1.64  mycroft 	u_long  sc_bopenpart;   /* block units open on this drive */
    118  1.64  mycroft 	u_long  sc_openpart;    /* all units open on this drive */
    119  1.64  mycroft 	short	sc_wlabel;	/* label writable? */
    120  1.64  mycroft 	short	sc_flags;	/* drive characteistics found */
    121  1.64  mycroft #define	WDF_SINGLE	0x00004	/* sector at a time mode */
    122  1.64  mycroft #define	WDF_ERROR	0x00008	/* processing a disk error */
    123  1.64  mycroft #define	WDF_BSDLABEL	0x00010	/* has a BSD disk label */
    124  1.64  mycroft #define	WDF_BADSECT	0x00020	/* has a bad144 badsector table */
    125  1.64  mycroft #define	WDF_WRITEPROT	0x00040	/* manual unit write protect */
    126  1.64  mycroft 	struct buf sc_q;
    127  1.64  mycroft 	struct wdparams sc_params; /* ESDI/IDE drive/controller parameters */
    128  1.64  mycroft 	struct disklabel sc_label;	/* device configuration data */
    129  1.64  mycroft 	struct cpu_disklabel sc_cpulabel;
    130  1.64  mycroft 	long	sc_badsect[127];	/* 126 plus trailing -1 marker */
    131  1.64  mycroft } *wd_softc[NWD];
    132  1.64  mycroft 
    133  1.64  mycroft struct wdc_softc {
    134  1.64  mycroft 	struct device sc_dev;
    135  1.64  mycroft 
    136  1.64  mycroft 	struct buf sc_q;
    137  1.64  mycroft 	u_char	sc_status;	/* copy of status register */
    138  1.64  mycroft 	u_char	sc_error;	/* copy of error register */
    139  1.64  mycroft 	u_short	sc_iobase;	/* i/o port base */
    140  1.64  mycroft 	int	sc_timeout;	/* timeout counter */
    141  1.64  mycroft } wdc_softc[NWDC];
    142   1.3  deraadt 
    143  1.64  mycroft int wdprobe(), wdattach(), wdintr();
    144   1.3  deraadt 
    145   1.3  deraadt struct	isa_driver wdcdriver = {
    146   1.3  deraadt 	wdprobe, wdattach, "wdc",
    147   1.1      cgd };
    148   1.1      cgd 
    149  1.64  mycroft void wdfinish __P((struct wd_softc *, struct buf *));
    150  1.64  mycroft static void wdstart __P((struct wd_softc *));
    151  1.64  mycroft static void wdcstart __P((struct wdc_softc *));
    152  1.64  mycroft static int wdcommand __P((struct wd_softc *, int, int, int, int, int));
    153  1.55  mycroft static int wdcontrol __P((struct buf *));
    154  1.64  mycroft static int wdsetctlr __P((struct wd_softc *));
    155  1.64  mycroft static int wdgetctlr __P((struct wd_softc *));
    156  1.64  mycroft static void bad144intern __P((struct wd_softc *));
    157  1.64  mycroft static int wdcreset __P((struct wdc_softc *));
    158  1.64  mycroft static void wdcrestart __P((struct wdc_softc *));
    159  1.64  mycroft static void wdcunwedge __P((struct wdc_softc *));
    160  1.64  mycroft static void wdctimeout __P((struct wdc_softc *));
    161  1.63  mycroft void wddisksort __P((struct buf *, struct buf *));
    162  1.64  mycroft static void wderror __P((void *, struct buf *, char *));
    163  1.64  mycroft int wdcwait __P((struct wdc_softc *, int));
    164  1.64  mycroft #define	wait_for_drq(d)		wdcwait(d, WDCS_DRQ)
    165  1.64  mycroft #define	wait_for_ready(d)	wdcwait(d, WDCS_READY | WDCS_SEEKCMPLT)
    166  1.64  mycroft #define	wait_for_unbusy(d)	wdcwait(d, 0)
    167   1.1      cgd 
    168   1.1      cgd /*
    169   1.1      cgd  * Probe for controller.
    170   1.1      cgd  */
    171   1.1      cgd int
    172  1.55  mycroft wdprobe(isa_dev)
    173  1.55  mycroft 	struct isa_device *isa_dev;
    174   1.1      cgd {
    175  1.64  mycroft 	struct wd_softc *wd;
    176  1.64  mycroft 	struct wdc_softc *wdc;
    177  1.64  mycroft 	u_short iobase;
    178   1.1      cgd 
    179  1.55  mycroft 	if (isa_dev->id_unit >= NWDC)
    180  1.37  mycroft 		return 0;
    181  1.64  mycroft 	wdc = &wdc_softc[isa_dev->id_unit];
    182   1.1      cgd 
    183  1.64  mycroft 	/* XXX HACK */
    184  1.64  mycroft 	sprintf(wdc->sc_dev.dv_xname, "%s%d", "wdc", isa_dev->id_unit);
    185  1.64  mycroft 	wdc->sc_dev.dv_unit = isa_dev->id_unit;
    186  1.64  mycroft 
    187  1.64  mycroft 	wdc->sc_iobase = iobase = isa_dev->id_iobase;
    188  1.64  mycroft 
    189  1.64  mycroft 	/* Check if we have registers that work. */
    190  1.64  mycroft 	outb(iobase+wd_error, 0x5a);	/* Error register not writable. */
    191  1.64  mycroft 	outb(iobase+wd_cyl_lo, 0xa5);	/* But all of cyllo are implemented. */
    192  1.64  mycroft 	if (inb(iobase+wd_error) == 0x5a || inb(iobase+wd_cyl_lo) != 0xa5)
    193  1.64  mycroft 		return 0;
    194   1.3  deraadt 
    195  1.64  mycroft 	if (wdcreset(wdc) != 0) {
    196  1.64  mycroft 		delay(500000);
    197  1.64  mycroft 		if (wdcreset(wdc) != 0)
    198  1.64  mycroft 			return 0;
    199  1.64  mycroft 	}
    200   1.1      cgd 
    201  1.64  mycroft 	/*
    202  1.64  mycroft 	 * XXX wdcommand() accepts a wd_softc, so we have to make it one.
    203  1.64  mycroft 	 */
    204  1.64  mycroft 	wd = (void *)malloc(sizeof(struct wd_softc), M_TEMP, M_NOWAIT);
    205  1.64  mycroft 	bzero(wd, sizeof(struct wd_softc));
    206  1.64  mycroft 	wd->sc_unit = 0;
    207  1.64  mycroft 	wd->sc_lunit = 0;
    208  1.64  mycroft 	wd->sc_dev.dv_parent = (void *)wdc;
    209  1.64  mycroft 
    210  1.64  mycroft 	/* Execute a controller only command. */
    211  1.64  mycroft 	if (wdcommand(wd, 0, 0, 0, 0, WDCC_DIAGNOSE) != 0 ||
    212  1.64  mycroft 	    wait_for_unbusy(wdc) != 0)
    213  1.64  mycroft 		goto lose;
    214   1.3  deraadt 
    215  1.64  mycroft 	wdctimeout(wdc);
    216   1.1      cgd 
    217  1.64  mycroft 	free(wd, M_TEMP);
    218  1.37  mycroft 	return 8;
    219   1.1      cgd 
    220  1.64  mycroft lose:
    221  1.64  mycroft 	free(wd, M_TEMP);
    222  1.37  mycroft 	return 0;
    223   1.1      cgd }
    224   1.1      cgd 
    225   1.1      cgd /*
    226  1.64  mycroft  * Called for the controller too.  Attach each drive if possible.
    227   1.1      cgd  */
    228   1.1      cgd int
    229  1.55  mycroft wdattach(isa_dev)
    230  1.55  mycroft 	struct isa_device *isa_dev;
    231   1.1      cgd {
    232  1.64  mycroft 	int lunit;
    233  1.64  mycroft 	struct wd_softc *wd;
    234  1.64  mycroft 	struct wdc_softc *wdc;
    235  1.56  mycroft 	int i, blank;
    236   1.3  deraadt 
    237  1.55  mycroft 	if (isa_dev->id_masunit == -1)
    238  1.37  mycroft 		return 0;
    239  1.55  mycroft 	if (isa_dev->id_masunit >= NWDC)
    240  1.37  mycroft 		return 0;
    241   1.3  deraadt 
    242  1.55  mycroft 	lunit = isa_dev->id_unit;
    243  1.11  deraadt 	if (lunit == -1) {
    244  1.64  mycroft 		printf("%s: cannot support unit ?\n", wdc->sc_dev.dv_xname);
    245  1.11  deraadt 		return 0;
    246  1.11  deraadt 	}
    247   1.9  deraadt 	if (lunit >= NWD)
    248  1.37  mycroft 		return 0;
    249   1.3  deraadt 
    250  1.64  mycroft 	wdc = &wdc_softc[isa_dev->id_masunit];
    251  1.64  mycroft 
    252  1.64  mycroft 	wd_softc[lunit] = wd =
    253  1.64  mycroft 	    (void *)malloc(sizeof(struct wd_softc), M_TEMP, M_NOWAIT);
    254  1.64  mycroft 	bzero(wd, sizeof(struct wd_softc));
    255  1.64  mycroft 	wd->sc_unit = isa_dev->id_physid;
    256  1.64  mycroft 	wd->sc_lunit = lunit;
    257  1.64  mycroft 
    258  1.64  mycroft 	/* XXX HACK */
    259  1.64  mycroft 	sprintf(wd->sc_dev.dv_xname, "%s%d", "wd", isa_dev->id_unit);
    260  1.64  mycroft 	wd->sc_dev.dv_unit = isa_dev->id_unit;
    261  1.64  mycroft 	wd->sc_dev.dv_parent = (void *)wdc;
    262  1.64  mycroft 
    263  1.64  mycroft 	if (wdgetctlr(wd) != 0)
    264   1.9  deraadt 		return 0;
    265  1.56  mycroft 
    266  1.64  mycroft 	printf("%s at %s targ %d: ", wd->sc_dev.dv_xname, wdc->sc_dev.dv_xname,
    267  1.64  mycroft 	    isa_dev->id_physid);
    268  1.64  mycroft 	if (wd->sc_params.wdp_heads == 0)
    269  1.56  mycroft 		printf("(unknown size) <");
    270  1.56  mycroft 	else
    271  1.56  mycroft 		printf("%dMB %d cyl, %d head, %d sec <",
    272  1.64  mycroft 		    wd->sc_label.d_ncylinders * wd->sc_label.d_secpercyl /
    273  1.56  mycroft 		    (1048576 / DEV_BSIZE),
    274  1.64  mycroft 		    wd->sc_label.d_ncylinders, wd->sc_label.d_ntracks,
    275  1.64  mycroft 		    wd->sc_label.d_nsectors);
    276  1.64  mycroft 	for (i = blank = 0; i < sizeof(wd->sc_params.wdp_model); i++) {
    277  1.64  mycroft 		char c = wd->sc_params.wdp_model[i];
    278  1.56  mycroft 		if (c == '\0')
    279  1.56  mycroft 			break;
    280  1.56  mycroft 		if (c != ' ') {
    281  1.56  mycroft 			if (blank)
    282  1.56  mycroft 				printf(" %c", c);
    283  1.56  mycroft 			else
    284  1.56  mycroft 				printf("%c", c);
    285  1.56  mycroft 			blank = 0;
    286  1.56  mycroft 		} else
    287  1.56  mycroft 			blank = 1;
    288  1.56  mycroft 	}
    289  1.56  mycroft 	printf(">\n");
    290  1.56  mycroft 
    291   1.9  deraadt 	return 1;
    292   1.1      cgd }
    293   1.1      cgd 
    294  1.64  mycroft /*
    295  1.64  mycroft  * Read/write routine for a buffer.  Finds the proper unit, range checks
    296  1.64  mycroft  * arguments, and schedules the transfer.  Does not wait for the transfer to
    297  1.64  mycroft  * complete.  Multi-page transfers are supported.  All I/O requests must be a
    298  1.64  mycroft  * multiple of a sector in length.
    299   1.1      cgd  */
    300  1.64  mycroft void
    301  1.55  mycroft wdstrategy(bp)
    302  1.55  mycroft 	struct buf *bp;
    303   1.1      cgd {
    304  1.55  mycroft 	struct buf *dp;
    305  1.64  mycroft 	struct wd_softc *wd;	/* disk unit to do the IO */
    306  1.64  mycroft 	struct wdc_softc *wdc;
    307  1.51  mycroft 	int lunit = WDUNIT(bp->b_dev);
    308  1.37  mycroft 	int s;
    309   1.3  deraadt 
    310  1.64  mycroft 	/* Valid unit, controller, and request?  */
    311   1.3  deraadt 	if (lunit >= NWD || bp->b_blkno < 0 ||
    312  1.37  mycroft 	    howmany(bp->b_bcount, DEV_BSIZE) >= (1 << NBBY) ||
    313  1.64  mycroft 	    (wd = wd_softc[lunit]) == 0) {
    314   1.1      cgd 		bp->b_error = EINVAL;
    315   1.1      cgd 		bp->b_flags |= B_ERROR;
    316   1.1      cgd 		goto done;
    317   1.1      cgd 	}
    318   1.3  deraadt 
    319  1.64  mycroft 	/* "Soft" write protect check. */
    320  1.64  mycroft 	if ((wd->sc_flags & WDF_WRITEPROT) && (bp->b_flags & B_READ) == 0) {
    321   1.1      cgd 		bp->b_error = EROFS;
    322   1.1      cgd 		bp->b_flags |= B_ERROR;
    323   1.1      cgd 		goto done;
    324   1.1      cgd 	}
    325   1.3  deraadt 
    326  1.64  mycroft 	/* Have partitions and want to use them? */
    327  1.64  mycroft 	if ((wd->sc_flags & WDF_BSDLABEL) != 0 && WDPART(bp->b_dev) != WDRAW) {
    328   1.1      cgd 		/*
    329  1.64  mycroft 		 * Do bounds checking, adjust transfer. if error, process.
    330  1.64  mycroft 		 * If end of partition, just return.
    331   1.1      cgd 		 */
    332  1.64  mycroft 		if (bounds_check_with_label(bp, &wd->sc_label,
    333  1.64  mycroft 		    wd->sc_wlabel) <= 0)
    334   1.1      cgd 			goto done;
    335  1.64  mycroft 		/* Otherwise, process transfer request. */
    336   1.1      cgd 	}
    337   1.3  deraadt 
    338  1.64  mycroft 	/* Don't bother doing rotational optimization. */
    339  1.38  mycroft 	bp->b_cylin = 0;
    340  1.38  mycroft 
    341  1.64  mycroft 	/* Queue transfer on drive, activate drive and controller if idle. */
    342  1.64  mycroft 	dp = &wd->sc_q;
    343   1.1      cgd 	s = splbio();
    344  1.49  mycroft 	wddisksort(dp, bp);
    345   1.1      cgd 	if (dp->b_active == 0)
    346  1.64  mycroft 		wdstart(wd);		/* Start drive. */
    347  1.64  mycroft 	wdc = (void *)wd->sc_dev.dv_parent;
    348  1.64  mycroft 	if (wdc->sc_q.b_active == 0)
    349  1.64  mycroft 		wdcstart(wdc);		/* Start controller. */
    350   1.1      cgd 	splx(s);
    351  1.64  mycroft 	return;
    352   1.3  deraadt 
    353   1.1      cgd done:
    354  1.64  mycroft 	/* Toss transfer; we're done early. */
    355   1.1      cgd 	biodone(bp);
    356   1.1      cgd }
    357   1.1      cgd 
    358   1.1      cgd /*
    359  1.49  mycroft  * Need to skip over multitransfer bufs.
    360  1.49  mycroft  */
    361  1.49  mycroft void
    362  1.55  mycroft wddisksort(dp, bp)
    363  1.55  mycroft 	struct buf *dp, *bp;
    364  1.49  mycroft {
    365  1.55  mycroft 	struct buf *ap;
    366  1.49  mycroft 
    367  1.49  mycroft 	while ((ap = dp->b_actf) && ap->b_flags & B_XXX)
    368  1.49  mycroft 		dp = ap;
    369  1.49  mycroft 	disksort(dp, bp);
    370  1.49  mycroft }
    371  1.49  mycroft 
    372  1.49  mycroft /*
    373  1.64  mycroft  * Routine to queue a command to the controller.  The unit's request is linked
    374  1.64  mycroft  * into the active list for the controller.  If the controller is idle, the
    375  1.64  mycroft  * transfer is started.
    376   1.1      cgd  */
    377   1.1      cgd static void
    378  1.64  mycroft wdstart(wd)
    379  1.64  mycroft 	struct wd_softc *wd;
    380   1.1      cgd {
    381  1.64  mycroft 	struct buf *dp;
    382  1.64  mycroft 	struct wdc_softc *wdc;
    383  1.64  mycroft 
    384  1.64  mycroft 	dp = &wd->sc_q;
    385   1.3  deraadt 
    386  1.64  mycroft 	/* Unit already active? */
    387   1.1      cgd 	if (dp->b_active)
    388   1.1      cgd 		return;
    389  1.64  mycroft 	/* Anything to start? */
    390  1.53  mycroft 	if (dp->b_actf == NULL)
    391   1.1      cgd 		return;
    392   1.3  deraadt 
    393  1.64  mycroft 	/* Link onto controller queue. */
    394   1.1      cgd 	dp->b_forw = NULL;
    395  1.64  mycroft 	wdc = (void *)wd->sc_dev.dv_parent;
    396  1.64  mycroft 	if (wdc->sc_q.b_forw == NULL)
    397  1.64  mycroft 		wdc->sc_q.b_forw = dp;
    398   1.1      cgd 	else
    399  1.64  mycroft 		wdc->sc_q.b_actl->b_forw = dp;
    400  1.64  mycroft 	wdc->sc_q.b_actl = dp;
    401   1.3  deraadt 
    402  1.64  mycroft 	/* Mark the drive unit as busy. */
    403   1.1      cgd 	dp->b_active = 1;
    404   1.1      cgd }
    405   1.1      cgd 
    406  1.64  mycroft void
    407  1.64  mycroft wdfinish(wd, bp)
    408  1.64  mycroft 	struct wd_softc *wd;
    409  1.64  mycroft 	struct buf *bp;
    410  1.64  mycroft {
    411  1.64  mycroft 	struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
    412  1.64  mycroft 
    413  1.64  mycroft #ifdef INSTRUMENT
    414  1.64  mycroft 	dk_busy &= ~(1 << wd->sc_unit);
    415  1.64  mycroft #endif
    416  1.64  mycroft 	wd->sc_flags &= ~(WDF_SINGLE | WDF_ERROR);
    417  1.64  mycroft 	wdc->sc_q.b_errcnt = 0;
    418  1.64  mycroft 	/*
    419  1.64  mycroft 	 * If this is the only buf or the last buf in the transfer (taking into
    420  1.64  mycroft 	 * account any residual, in case we erred)...
    421  1.64  mycroft 	 */
    422  1.64  mycroft 	wd->sc_mbcount -= wd->sc_bcount;
    423  1.64  mycroft 	if (wd->sc_mbcount == 0) {
    424  1.64  mycroft 		/*
    425  1.64  mycroft 		 * ...then move this drive to the end of the queue to give
    426  1.64  mycroft 		 * others a `fair' chance.
    427  1.64  mycroft 		 */
    428  1.64  mycroft 		wdc->sc_q.b_forw = wd->sc_q.b_forw;
    429  1.64  mycroft 		wd->sc_mskip = 0;
    430  1.64  mycroft 		wd->sc_q.b_active = 0;
    431  1.64  mycroft 		if (wd->sc_q.b_actf)
    432  1.64  mycroft 			wdstart(wd);
    433  1.64  mycroft 	}
    434  1.64  mycroft 	bp->b_resid = wd->sc_bcount;
    435  1.64  mycroft 	bp->b_flags &= ~B_XXX;
    436  1.64  mycroft 	wd->sc_skip = 0;
    437  1.64  mycroft 	wd->sc_q.b_actf = bp->b_actf;
    438  1.64  mycroft 	biodone(bp);
    439  1.64  mycroft }
    440  1.64  mycroft 
    441   1.1      cgd /*
    442  1.64  mycroft  * Controller startup routine.  This does the calculation, and starts a
    443  1.64  mycroft  * single-sector read or write operation.  Called to start a transfer, or from
    444  1.64  mycroft  * the interrupt routine to continue a multi-sector transfer.
    445   1.1      cgd  * RESTRICTIONS:
    446   1.1      cgd  * 1.	The transfer length must be an exact multiple of the sector size.
    447   1.1      cgd  */
    448   1.1      cgd static void
    449  1.64  mycroft wdcstart(wdc)
    450  1.64  mycroft 	struct wdc_softc *wdc;
    451   1.1      cgd {
    452  1.64  mycroft 	struct wd_softc *wd;	/* disk unit for IO */
    453  1.64  mycroft 	struct buf *bp;
    454   1.1      cgd 	struct disklabel *lp;
    455   1.1      cgd 	struct buf *dp;
    456  1.54  mycroft 	long blknum, cylin, head, sector;
    457  1.60  mycroft 	long secpertrk, secpercyl;
    458   1.3  deraadt 	int xfrblknum;
    459  1.60  mycroft 	int lunit;
    460  1.64  mycroft 
    461   1.1      cgd loop:
    462  1.64  mycroft 	/* Is there a drive for the controller to do a transfer with? */
    463  1.64  mycroft 	dp = wdc->sc_q.b_forw;
    464  1.53  mycroft 	if (dp == NULL) {
    465  1.64  mycroft 		wdc->sc_q.b_active = 0;
    466   1.1      cgd 		return;
    467  1.53  mycroft 	}
    468   1.3  deraadt 
    469  1.64  mycroft 	/* Is there a transfer to this drive?  If not, deactivate drive. */
    470   1.1      cgd 	bp = dp->b_actf;
    471   1.1      cgd 	if (bp == NULL) {
    472  1.53  mycroft 		dp->b_active = 0;
    473  1.64  mycroft 		wdc->sc_q.b_forw = dp->b_forw;
    474   1.1      cgd 		goto loop;
    475   1.1      cgd 	}
    476   1.3  deraadt 
    477  1.64  mycroft 	/* Obtain controller and drive information */
    478  1.51  mycroft 	lunit = WDUNIT(bp->b_dev);
    479  1.64  mycroft 	wd = wd_softc[lunit];
    480  1.51  mycroft 
    481  1.64  mycroft 	if (wdc->sc_q.b_errcnt >= WDIORETRIES) {
    482  1.64  mycroft 		wderror(wd, bp, "hard error");
    483  1.64  mycroft 		bp->b_error = EIO;
    484  1.64  mycroft 		bp->b_flags |= B_ERROR;
    485  1.64  mycroft 		wdfinish(wd, bp);
    486  1.64  mycroft 		goto loop;
    487  1.64  mycroft 	}
    488   1.3  deraadt 
    489  1.64  mycroft 	/* Clear any pending timeout, just in case. */
    490  1.64  mycroft 	wdc->sc_timeout = 0;
    491  1.64  mycroft 
    492  1.64  mycroft 	/* If not really a transfer, do control operations specially. */
    493  1.64  mycroft 	if (wd->sc_state < OPEN) {
    494   1.1      cgd 		(void) wdcontrol(bp);
    495   1.1      cgd 		return;
    496   1.1      cgd 	}
    497  1.64  mycroft 
    498  1.64  mycroft 	/*
    499  1.64  mycroft 	 * WDF_ERROR is set by wdcunwedge() and wdintr() when an error is
    500  1.64  mycroft 	 * encountered *only* in multi-sector mode.  We switch to single-sector
    501  1.64  mycroft 	 * mode and retry the operation from the start.
    502  1.64  mycroft 	 */
    503  1.64  mycroft 	if (wd->sc_flags & WDF_ERROR) {
    504  1.64  mycroft 		/* Switch to single-sector mode. */
    505  1.64  mycroft 		wd->sc_flags &= ~WDF_ERROR;
    506  1.64  mycroft 		wd->sc_flags |= WDF_SINGLE;
    507  1.64  mycroft 		wd->sc_skip = 0;
    508  1.64  mycroft 		wd->sc_mskip = 0;
    509  1.64  mycroft 	}
    510  1.64  mycroft 
    511  1.64  mycroft 	/* Calculate transfer details. */
    512  1.64  mycroft 	blknum = bp->b_blkno + wd->sc_skip;
    513  1.44  mycroft #ifdef WDDEBUG
    514  1.64  mycroft 	if (wd->sc_skip == 0)
    515  1.64  mycroft 		printf("\nwdcstart %d: %s %d@%d; map ", lunit,
    516  1.37  mycroft 		    (bp->b_flags & B_READ) ? "read" : "write", bp->b_bcount,
    517  1.37  mycroft 		    blknum);
    518   1.1      cgd 	else
    519  1.64  mycroft 		printf(" %d)%x", wd->sc_skip, inb(wd->sc_iobase+wd_altsts));
    520   1.1      cgd #endif
    521  1.64  mycroft 	if (wd->sc_skip == 0)
    522  1.64  mycroft 		wd->sc_bcount = bp->b_bcount;
    523  1.64  mycroft 	if (wd->sc_mskip == 0) {
    524   1.3  deraadt 		struct buf *oldbp, *nextbp;
    525   1.3  deraadt 		oldbp = bp;
    526  1.33  mycroft 		nextbp = bp->b_actf;
    527  1.64  mycroft 		wd->sc_mbcount = wd->sc_bcount;
    528   1.3  deraadt 		oldbp->b_flags |= B_XXX;
    529  1.64  mycroft 		while (nextbp && (oldbp->b_flags & WDF_SINGLE) == 0 &&
    530  1.44  mycroft 		    oldbp->b_dev == nextbp->b_dev && nextbp->b_blkno ==
    531  1.44  mycroft 		    (oldbp->b_blkno + (oldbp->b_bcount / DEV_BSIZE)) &&
    532  1.37  mycroft 		    (oldbp->b_flags & B_READ) == (nextbp->b_flags & B_READ)) {
    533  1.64  mycroft 			if ((wd->sc_mbcount + nextbp->b_bcount) / DEV_BSIZE >= 240)
    534   1.3  deraadt 				break;
    535  1.64  mycroft 			wd->sc_mbcount += nextbp->b_bcount;
    536  1.49  mycroft 			nextbp->b_flags |= B_XXX;
    537   1.3  deraadt 			oldbp = nextbp;
    538  1.33  mycroft 			nextbp = nextbp->b_actf;
    539   1.3  deraadt 		}
    540   1.3  deraadt 	}
    541   1.3  deraadt 
    542  1.64  mycroft 	lp = &wd->sc_label;
    543   1.1      cgd 	secpertrk = lp->d_nsectors;
    544   1.1      cgd 	secpercyl = lp->d_secpercyl;
    545  1.64  mycroft 	if ((wd->sc_flags & WDF_BSDLABEL) != 0 && WDPART(bp->b_dev) != WDRAW)
    546  1.51  mycroft 		blknum += lp->d_partitions[WDPART(bp->b_dev)].p_offset;
    547   1.1      cgd 	cylin = blknum / secpercyl;
    548   1.1      cgd 	head = (blknum % secpercyl) / secpertrk;
    549   1.1      cgd 	sector = blknum % secpertrk;
    550   1.3  deraadt 
    551  1.64  mycroft 	/*
    552  1.64  mycroft 	 * Check for bad sectors if we have them, and not formatting.  Only do
    553  1.64  mycroft 	 * this in single-sector mode, or when starting a multiple-sector
    554  1.64  mycroft 	 * transfer.
    555  1.64  mycroft 	 */
    556  1.64  mycroft 	if ((wd->sc_flags & WDF_BADSECT) &&
    557  1.44  mycroft #ifdef B_FORMAT
    558  1.37  mycroft 	    (bp->b_flags & B_FORMAT) == 0 &&
    559   1.3  deraadt #endif
    560  1.64  mycroft 	    (wd->sc_mskip == 0 || (wd->sc_flags & WDF_SINGLE))) {
    561   1.1      cgd 
    562   1.3  deraadt 		long blkchk, blkend, blknew;
    563   1.3  deraadt 		int i;
    564   1.3  deraadt 
    565  1.64  mycroft 		blkend = blknum + howmany(wd->sc_mbcount, DEV_BSIZE) - 1;
    566  1.64  mycroft 		for (i = 0; (blkchk = wd->sc_badsect[i]) != -1; i++) {
    567  1.37  mycroft 			if (blkchk > blkend)
    568  1.64  mycroft 				break;	/* Transfer is completely OK; done. */
    569  1.37  mycroft 			if (blkchk == blknum) {
    570  1.37  mycroft 				blknew =
    571  1.37  mycroft 				    lp->d_secperunit - lp->d_nsectors - i - 1;
    572   1.3  deraadt 				cylin = blknew / secpercyl;
    573   1.3  deraadt 				head = (blknew % secpercyl) / secpertrk;
    574   1.3  deraadt 				sector = blknew % secpertrk;
    575  1.64  mycroft 				wd->sc_flags |= WDF_SINGLE;
    576  1.64  mycroft 				/* Found and replaced first blk of transfer; done. */
    577   1.3  deraadt 				break;
    578   1.3  deraadt 			} else if (blkchk > blknum) {
    579  1.64  mycroft 				wd->sc_flags |= WDF_SINGLE;
    580  1.64  mycroft 				break;	/* Bad block inside transfer; done. */
    581   1.3  deraadt 			}
    582   1.3  deraadt 		}
    583   1.3  deraadt 	}
    584  1.64  mycroft 	if (wd->sc_flags & WDF_SINGLE) {
    585  1.64  mycroft 		wd->sc_mbcount = wd->sc_bcount;
    586  1.64  mycroft 		wd->sc_mskip = wd->sc_skip;
    587   1.3  deraadt 	}
    588   1.3  deraadt 
    589   1.3  deraadt #ifdef WDDEBUG
    590  1.51  mycroft 	printf("c%d h%d s%d ", cylin, head, sector);
    591   1.1      cgd #endif
    592   1.3  deraadt 
    593  1.64  mycroft 	sector++;	/* Sectors begin with 1, not 0. */
    594   1.3  deraadt 
    595  1.64  mycroft 	wdc->sc_q.b_active = 1;		/* Mark controller active. */
    596   1.3  deraadt 
    597   1.3  deraadt #ifdef INSTRUMENT
    598  1.64  mycroft 	if (wd->sc_skip == 0) {
    599  1.64  mycroft 		dk_busy |= 1 << wd->sc_lunit;
    600  1.64  mycroft 		dk_wds[wd->sc_lunit] += bp->b_bcount >> 6;
    601   1.3  deraadt 	}
    602   1.3  deraadt #endif
    603   1.1      cgd 
    604  1.64  mycroft 	/* If starting a multisector transfer, or doing single transfers. */
    605  1.64  mycroft 	if (wd->sc_mskip == 0 || (wd->sc_flags & WDF_SINGLE)) {
    606  1.60  mycroft 		int command, count;
    607  1.51  mycroft 
    608  1.64  mycroft #ifdef INSTRUMENT
    609  1.64  mycroft 		++dk_seek[wd->sc_lunit];
    610  1.64  mycroft 		++dk_xfer[wd->sc_lunit];
    611  1.64  mycroft #endif
    612   1.1      cgd 
    613  1.37  mycroft #ifdef B_FORMAT
    614   1.1      cgd 		if (bp->b_flags & B_FORMAT) {
    615  1.60  mycroft 			sector = lp->d_gap3;
    616  1.60  mycroft 			count = lp->d_nsectors;
    617  1.60  mycroft 			command = WDCC_FORMAT;
    618   1.1      cgd 		} else {
    619  1.64  mycroft 			if (wd->sc_flags & WDF_SINGLE)
    620  1.60  mycroft 				count = 1;
    621   1.3  deraadt 			else
    622  1.64  mycroft 				count = howmany(wd->sc_mbcount, DEV_BSIZE);
    623  1.60  mycroft 			command =
    624  1.60  mycroft 			    (bp->b_flags & B_READ) ? WDCC_READ : WDCC_WRITE;
    625   1.3  deraadt 		}
    626   1.3  deraadt #else
    627  1.64  mycroft 		if (wd->sc_flags & WDF_SINGLE)
    628  1.60  mycroft 			count = 1;
    629   1.1      cgd 		else
    630  1.64  mycroft 			count = howmany(wd->sc_mbcount, DEV_BSIZE);
    631  1.60  mycroft 		command = (bp->b_flags & B_READ) ? WDCC_READ : WDCC_WRITE;
    632   1.1      cgd #endif
    633   1.3  deraadt 
    634  1.64  mycroft 		/* Initiate command! */
    635  1.64  mycroft 		if (wdcommand(wd, cylin, head, sector, count, command) != 0) {
    636  1.64  mycroft 			wderror(wd, NULL,
    637  1.64  mycroft 			    "wdcstart: timeout waiting for unbusy");
    638  1.64  mycroft 			wdcunwedge(wdc);
    639  1.64  mycroft 			return;
    640  1.51  mycroft 		}
    641  1.44  mycroft #ifdef WDDEBUG
    642  1.37  mycroft 		printf("sector %d cylin %d head %d addr %x sts %x\n", sector,
    643  1.64  mycroft 		    cylin, head, bp->b_un.b_addr, inb(wd->sc_iobase+wd_altsts));
    644   1.1      cgd #endif
    645   1.1      cgd 	}
    646   1.3  deraadt 
    647  1.64  mycroft 	wdc->sc_timeout = 4;
    648  1.60  mycroft 
    649  1.64  mycroft 	/* If this is a read operation, just go away until it's done. */
    650  1.60  mycroft 	if (bp->b_flags & B_READ)
    651   1.3  deraadt 		return;
    652   1.3  deraadt 
    653  1.64  mycroft 	if (wait_for_drq(wdc) < 0) {
    654  1.64  mycroft 		wderror(wd, NULL, "wdcstart: timeout waiting for drq");
    655  1.64  mycroft 		wdcunwedge(wdc);
    656  1.64  mycroft 		return;
    657  1.19  deraadt 	}
    658  1.60  mycroft 
    659  1.64  mycroft 	/* Then send it! */
    660  1.64  mycroft 	outsw(wdc->sc_iobase+wd_data, bp->b_un.b_addr + wd->sc_skip * DEV_BSIZE,
    661  1.44  mycroft 	    DEV_BSIZE / sizeof(short));
    662   1.1      cgd }
    663   1.1      cgd 
    664  1.64  mycroft /*
    665  1.64  mycroft  * Interrupt routine for the controller.  Acknowledge the interrupt, check for
    666  1.64  mycroft  * errors on the current operation, mark it done if necessary, and start the
    667  1.64  mycroft  * next request.  Also check for a partially done transfer, and continue with
    668  1.64  mycroft  * the next chunk if so.
    669   1.1      cgd  */
    670  1.64  mycroft int
    671  1.55  mycroft wdintr(ctrlr)
    672  1.55  mycroft 	int ctrlr;
    673   1.1      cgd {
    674  1.64  mycroft 	struct wdc_softc *wdc = &wdc_softc[ctrlr];
    675  1.64  mycroft 	struct wd_softc *wd;
    676  1.64  mycroft 	struct buf *bp;
    677  1.59  mycroft 
    678  1.64  mycroft 	/* Clear the pending interrupt. */
    679  1.64  mycroft 	(void) inb(wdc->sc_iobase+wd_status);
    680  1.51  mycroft 
    681  1.64  mycroft 	if (!wdc->sc_q.b_active) {
    682  1.64  mycroft 		printf("%s: extra interrupt\n", wdc->sc_dev.dv_xname);
    683  1.64  mycroft 		return 0;
    684   1.1      cgd 	}
    685   1.3  deraadt 
    686  1.64  mycroft 	bp = wdc->sc_q.b_forw->b_actf;
    687  1.64  mycroft 	wd = wd_softc[WDUNIT(bp->b_dev)];
    688  1.64  mycroft 	wdc->sc_timeout = 0;
    689   1.3  deraadt 
    690  1.44  mycroft #ifdef WDDEBUG
    691   1.3  deraadt 	printf("I%d ", ctrlr);
    692   1.1      cgd #endif
    693  1.13  deraadt 
    694  1.64  mycroft 	if (wait_for_unbusy(wdc) < 0) {
    695  1.64  mycroft 		wderror(wd, NULL, "wdintr: timeout waiting for unbusy");
    696  1.64  mycroft 		wdc->sc_status |= WDCS_ERR;	/* XXX */
    697  1.27      cgd 	}
    698   1.3  deraadt 
    699  1.64  mycroft 	/* Is it not a transfer, but a control operation? */
    700  1.64  mycroft 	if (wd->sc_state < OPEN) {
    701  1.64  mycroft 		wdc->sc_q.b_active = 0;
    702  1.64  mycroft 		if (wdcontrol(bp))
    703  1.64  mycroft 			wdcstart(wdc);
    704  1.64  mycroft 		return 1;
    705   1.1      cgd 	}
    706   1.3  deraadt 
    707  1.64  mycroft 	/* Have we an error? */
    708  1.64  mycroft 	if (wdc->sc_status & (WDCS_ERR | WDCS_ECCCOR)) {
    709  1.51  mycroft 	lose:
    710  1.44  mycroft #ifdef WDDEBUG
    711  1.64  mycroft 		wderror(wd, NULL, "wdintr");
    712   1.1      cgd #endif
    713  1.64  mycroft 		if ((wd->sc_flags & WDF_SINGLE) == 0) {
    714  1.64  mycroft 			wd->sc_flags |= WDF_ERROR;
    715  1.64  mycroft 			goto restart;
    716   1.1      cgd 		}
    717   1.1      cgd #ifdef B_FORMAT
    718   1.1      cgd 		if (bp->b_flags & B_FORMAT) {
    719  1.46  mycroft 			bp->b_error = EIO;
    720   1.1      cgd 			bp->b_flags |= B_ERROR;
    721   1.1      cgd 			goto done;
    722   1.1      cgd 		}
    723   1.1      cgd #endif
    724   1.3  deraadt 
    725  1.64  mycroft 		/* Error or error correction? */
    726  1.64  mycroft 		if (wdc->sc_status & WDCS_ERR) {
    727  1.64  mycroft 			if (++wdc->sc_q.b_errcnt >= WDIORETRIES) {
    728  1.64  mycroft 				wderror(wd, bp, "hard error");
    729  1.49  mycroft 				bp->b_error = EIO;
    730  1.64  mycroft 				bp->b_flags |= B_ERROR;	/* Flag the error. */
    731  1.64  mycroft 				goto done;
    732  1.64  mycroft 			} else
    733  1.64  mycroft 				goto restart;
    734  1.62  mycroft 		} else
    735  1.64  mycroft 			wderror(wd, bp, "soft ecc");
    736   1.1      cgd 	}
    737   1.3  deraadt 
    738  1.64  mycroft 	/* If this was a read, fetch the data. */
    739  1.64  mycroft 	if (bp->b_flags & B_READ) {
    740  1.64  mycroft 		/* Ready to receive data? */
    741  1.64  mycroft 		if (wait_for_drq(wdc) != 0) {
    742  1.64  mycroft 			wderror(wd, NULL, "wdintr: read error detected late");
    743  1.64  mycroft 			wdcunwedge(wdc);
    744  1.64  mycroft 			return 1;
    745  1.27      cgd 		}
    746  1.27      cgd 
    747  1.64  mycroft 		/* Suck in data. */
    748  1.64  mycroft 		insw(wdc->sc_iobase+wd_data,
    749  1.64  mycroft 		    bp->b_un.b_addr + wd->sc_skip * DEV_BSIZE,
    750  1.64  mycroft 		    DEV_BSIZE / sizeof(short));
    751  1.64  mycroft 	}
    752  1.64  mycroft 
    753  1.64  mycroft 	/* If we encountered any abnormalities, flag it as a soft error. */
    754  1.64  mycroft 	if (wdc->sc_q.b_errcnt) {
    755  1.64  mycroft 		wderror(wd, bp, "soft error");
    756  1.64  mycroft 		wdc->sc_q.b_errcnt = 0;
    757   1.1      cgd 	}
    758   1.3  deraadt 
    759  1.64  mycroft 	/* Ready for the next block, if any. */
    760  1.64  mycroft 	wd->sc_skip++;
    761  1.64  mycroft 	wd->sc_mskip++;
    762  1.64  mycroft 	wd->sc_bcount -= DEV_BSIZE;
    763  1.64  mycroft 	wd->sc_mbcount -= DEV_BSIZE;
    764  1.64  mycroft 
    765  1.64  mycroft 	/* See if more to transfer. */
    766  1.64  mycroft 	if (wd->sc_bcount > 0)
    767  1.64  mycroft 		goto restart;
    768  1.64  mycroft 
    769  1.64  mycroft done:
    770  1.64  mycroft 	/* Done with this transfer, with or without error. */
    771  1.64  mycroft 	wdfinish(wd, bp);
    772  1.64  mycroft 
    773  1.64  mycroft restart:
    774  1.64  mycroft 	/* Start the next transfer, if any. */
    775  1.64  mycroft 	wdc->sc_q.b_active = 0;
    776  1.64  mycroft 	wdcstart(wdc);
    777   1.1      cgd 
    778  1.64  mycroft 	return 1;
    779   1.1      cgd }
    780   1.1      cgd 
    781   1.1      cgd /*
    782   1.1      cgd  * Initialize a drive.
    783   1.1      cgd  */
    784   1.1      cgd int
    785  1.55  mycroft wdopen(dev, flag, fmt, p)
    786  1.55  mycroft 	dev_t dev;
    787  1.55  mycroft 	int flag;
    788  1.55  mycroft 	int fmt;
    789  1.55  mycroft 	struct proc *p;
    790   1.1      cgd {
    791  1.54  mycroft 	int lunit;
    792  1.64  mycroft 	struct wd_softc *wd;
    793  1.51  mycroft 	int part = WDPART(dev), mask = 1 << part;
    794   1.3  deraadt 	struct partition *pp;
    795   1.1      cgd 	char *msg;
    796   1.3  deraadt 
    797  1.51  mycroft 	lunit = WDUNIT(dev);
    798   1.3  deraadt 	if (lunit >= NWD)
    799  1.37  mycroft 		return ENXIO;
    800  1.64  mycroft 	wd = wd_softc[lunit];
    801  1.64  mycroft 	if (wd == 0)
    802  1.37  mycroft 		return ENXIO;
    803   1.3  deraadt 
    804  1.64  mycroft 	if ((wd->sc_flags & WDF_BSDLABEL) == 0) {
    805  1.64  mycroft 		wd->sc_flags |= WDF_WRITEPROT;
    806  1.64  mycroft 		wd->sc_q.b_actf = NULL;
    807   1.1      cgd 
    808   1.1      cgd 		/*
    809  1.64  mycroft 		 * Use the default sizes until we've read the label, or longer
    810  1.64  mycroft 		 * if there isn't one there.
    811   1.1      cgd 		 */
    812  1.64  mycroft 		bzero(&wd->sc_label, sizeof(wd->sc_label));
    813  1.64  mycroft 		wd->sc_label.d_type = DTYPE_ST506;
    814  1.64  mycroft 		wd->sc_label.d_ncylinders = 1024;
    815  1.64  mycroft 		wd->sc_label.d_secsize = DEV_BSIZE;
    816  1.64  mycroft 		wd->sc_label.d_ntracks = 8;
    817  1.64  mycroft 		wd->sc_label.d_nsectors = 17;
    818  1.64  mycroft 		wd->sc_label.d_secpercyl = 17*8;
    819  1.64  mycroft 		wd->sc_label.d_secperunit = 17*8*1024;
    820  1.64  mycroft 		wd->sc_state = WANTOPEN;
    821   1.1      cgd 
    822  1.64  mycroft 		/* Read label using "raw" partition. */
    823  1.55  mycroft #ifdef notdef
    824  1.64  mycroft 		/* wdsetctlr(wd); */	/* Maybe do this TIH */
    825  1.51  mycroft 		msg = readdisklabel(makewddev(major(dev), WDUNIT(dev), WDRAW),
    826  1.64  mycroft 		    wdstrategy, &wd->sc_label, &wd->sc_cpulabel);
    827  1.64  mycroft 		wdsetctlr(wd);
    828   1.3  deraadt #endif
    829  1.51  mycroft 		if (msg = readdisklabel(makewddev(major(dev), WDUNIT(dev),
    830  1.64  mycroft 		    WDRAW), wdstrategy, &wd->sc_label, &wd->sc_cpulabel)) {
    831  1.64  mycroft 			log(LOG_WARNING, "%s: cannot find label (%s)\n",
    832  1.64  mycroft 			    wd->sc_dev.dv_xname, msg);
    833  1.62  mycroft 			if (part != WDRAW)
    834  1.46  mycroft 				return EINVAL;	/* XXX needs translation */
    835   1.1      cgd 		} else {
    836  1.64  mycroft 			wdsetctlr(wd);
    837  1.64  mycroft 			wd->sc_flags |= WDF_BSDLABEL;
    838  1.64  mycroft 			wd->sc_flags &= ~WDF_WRITEPROT;
    839  1.64  mycroft 			if (wd->sc_label.d_flags & D_BADSECT)
    840  1.64  mycroft 				wd->sc_flags |= WDF_BADSECT;
    841   1.1      cgd 		}
    842   1.3  deraadt 	}
    843   1.3  deraadt 
    844  1.64  mycroft 	if (wd->sc_flags & WDF_BADSECT)
    845  1.64  mycroft 		bad144intern(wd);
    846   1.1      cgd 
    847   1.3  deraadt 	/*
    848  1.60  mycroft 	 * Warn if a partition is opened that overlaps another partition which
    849  1.60  mycroft 	 * is open unless one is the "raw" partition (whole disk).
    850   1.3  deraadt 	 */
    851  1.64  mycroft 	if ((wd->sc_openpart & mask) == 0 && part != WDRAW) {
    852  1.46  mycroft 		int start, end;
    853   1.3  deraadt 
    854  1.64  mycroft 		pp = &wd->sc_label.d_partitions[part];
    855   1.3  deraadt 		start = pp->p_offset;
    856   1.3  deraadt 		end = pp->p_offset + pp->p_size;
    857  1.64  mycroft 		for (pp = wd->sc_label.d_partitions;
    858  1.64  mycroft 		    pp < &wd->sc_label.d_partitions[wd->sc_label.d_npartitions];
    859  1.44  mycroft 		    pp++) {
    860  1.44  mycroft 			if (pp->p_offset + pp->p_size <= start ||
    861  1.44  mycroft 			    pp->p_offset >= end)
    862   1.3  deraadt 				continue;
    863  1.64  mycroft 			if (pp - wd->sc_label.d_partitions == WDRAW)
    864   1.3  deraadt 				continue;
    865  1.64  mycroft 			if (wd->sc_openpart & (1 << (pp - wd->sc_label.d_partitions)))
    866   1.3  deraadt 				log(LOG_WARNING,
    867  1.64  mycroft 				    "%s%c: overlaps open partition (%c)\n",
    868  1.64  mycroft 				    wd->sc_dev.dv_xname, part + 'a',
    869  1.64  mycroft 				    pp - wd->sc_label.d_partitions + 'a');
    870   1.3  deraadt 		}
    871   1.1      cgd 	}
    872  1.64  mycroft 	if (part >= wd->sc_label.d_npartitions && part != WDRAW)
    873  1.37  mycroft 		return ENXIO;
    874   1.3  deraadt 
    875  1.64  mycroft 	/* Insure only one open at a time. */
    876   1.3  deraadt 	switch (fmt) {
    877   1.3  deraadt 	case S_IFCHR:
    878  1.64  mycroft 		wd->sc_copenpart |= mask;
    879   1.3  deraadt 		break;
    880   1.3  deraadt 	case S_IFBLK:
    881  1.64  mycroft 		wd->sc_bopenpart |= mask;
    882   1.3  deraadt 		break;
    883   1.3  deraadt 	}
    884  1.64  mycroft 	wd->sc_openpart = wd->sc_copenpart | wd->sc_bopenpart;
    885  1.60  mycroft 
    886  1.37  mycroft 	return 0;
    887   1.1      cgd }
    888   1.1      cgd 
    889   1.1      cgd /*
    890   1.1      cgd  * Implement operations other than read/write.
    891  1.64  mycroft  * Called from wdcstart or wdintr during opens and formats.
    892   1.1      cgd  * Uses finite-state-machine to track progress of operation in progress.
    893   1.1      cgd  * Returns 0 if operation still in progress, 1 if completed.
    894   1.1      cgd  */
    895   1.1      cgd static int
    896  1.55  mycroft wdcontrol(bp)
    897  1.55  mycroft 	struct buf *bp;
    898   1.1      cgd {
    899  1.64  mycroft 	struct wd_softc *wd = wd_softc[WDUNIT(bp->b_dev)];
    900  1.64  mycroft 	struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
    901   1.3  deraadt 
    902  1.64  mycroft 	switch (wd->sc_state) {
    903  1.64  mycroft 	case WANTOPEN:			/* Set SDH, step rate, do restore. */
    904  1.46  mycroft 	tryagainrecal:
    905  1.64  mycroft 		wdgetctlr(wd);		/* XXX Is this necessary? */
    906  1.64  mycroft 		wdc->sc_q.b_active = 1;
    907  1.64  mycroft 		if (wdcommand(wd, 0, 0, 0, 0, WDCC_RESTORE | WD_STEP) != 0) {
    908  1.64  mycroft 			wderror(wd, NULL, "wdcontrol: wdcommand failed");
    909  1.64  mycroft 			wdcunwedge(wdc);
    910  1.64  mycroft 			return 0;
    911  1.21  deraadt 		}
    912  1.64  mycroft 		wd->sc_state = RECAL;
    913  1.37  mycroft 		return 0;
    914  1.55  mycroft 
    915   1.1      cgd 	case RECAL:
    916  1.64  mycroft 		if (wdc->sc_status & WDCS_ERR || wdsetctlr(wd) != 0) {
    917  1.64  mycroft 			wderror(wd, NULL, "wdcontrol: recal failed");
    918  1.64  mycroft 			wdcunwedge(wdc);
    919  1.64  mycroft 			return 0;
    920   1.1      cgd 		}
    921  1.64  mycroft 		wdc->sc_q.b_errcnt = 0;
    922  1.64  mycroft 		wd->sc_state = OPEN;
    923   1.1      cgd 		/*
    924  1.63  mycroft 		 * The rest of the initialization can be done by normal means.
    925   1.1      cgd 		 */
    926  1.37  mycroft 		return 1;
    927   1.1      cgd 	}
    928   1.1      cgd 
    929  1.60  mycroft #ifdef DIAGNOSTIC
    930  1.60  mycroft 	panic("wdcontrol: impossible");
    931  1.60  mycroft #endif
    932   1.1      cgd }
    933   1.1      cgd 
    934   1.1      cgd /*
    935  1.64  mycroft  * Send a command and wait uninterruptibly until controller is finished.
    936  1.64  mycroft  * Return -1 if controller busy for too long, otherwise return non-zero if
    937  1.64  mycroft  * error.  Intended for brief controller commands at critical points.
    938  1.64  mycroft  * Assumes interrupts are blocked.
    939   1.1      cgd  */
    940   1.1      cgd static int
    941  1.64  mycroft wdcommand(wd, cylin, head, sector, count, cmd)
    942  1.64  mycroft 	struct wd_softc *wd;
    943  1.60  mycroft 	int cylin, head, sector, count;
    944  1.55  mycroft 	int cmd;
    945   1.3  deraadt {
    946  1.64  mycroft 	struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
    947  1.60  mycroft 	int stat;
    948  1.64  mycroft 	u_short iobase;
    949   1.3  deraadt 
    950  1.64  mycroft 	/* Controller ready for command? */
    951  1.64  mycroft 	if (wait_for_unbusy(wdc) < 0)
    952  1.40  mycroft 		return -1;
    953  1.60  mycroft 
    954  1.64  mycroft 	/* Select drive. */
    955  1.64  mycroft 	iobase = wdc->sc_iobase;
    956  1.64  mycroft 	outb(iobase+wd_sdh, WDSD_IBM | (wd->sc_unit << 4) | head);
    957  1.60  mycroft 	if (cmd == WDCC_DIAGNOSE || cmd == WDCC_IDC)
    958  1.64  mycroft 		stat = wait_for_unbusy(wdc);
    959  1.60  mycroft 	else
    960  1.64  mycroft 		stat = wdcwait(wdc, WDCS_READY);
    961  1.60  mycroft 	if (stat < 0)
    962  1.60  mycroft 		return -1;
    963   1.3  deraadt 
    964  1.64  mycroft 	/* Load parameters. */
    965  1.64  mycroft 	outb(iobase+wd_precomp, wd->sc_label.d_precompcyl / 4);
    966  1.64  mycroft 	outb(iobase+wd_cyl_lo, cylin);
    967  1.64  mycroft 	outb(iobase+wd_cyl_hi, cylin >> 8);
    968  1.64  mycroft 	outb(iobase+wd_sector, sector);
    969  1.64  mycroft 	outb(iobase+wd_seccnt, count);
    970  1.60  mycroft 
    971  1.64  mycroft 	/* Send command, await results. */
    972  1.64  mycroft 	outb(iobase+wd_command, cmd);
    973  1.40  mycroft 
    974  1.64  mycroft 	return 0;
    975   1.1      cgd }
    976   1.1      cgd 
    977   1.1      cgd /*
    978  1.64  mycroft  * Issue IDC to drive to tell it just what geometry it is to be.
    979   1.1      cgd  */
    980   1.1      cgd static int
    981  1.64  mycroft wdsetctlr(wd)
    982  1.64  mycroft 	struct wd_softc *wd;
    983   1.3  deraadt {
    984  1.64  mycroft 	struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
    985  1.63  mycroft 
    986  1.44  mycroft #ifdef WDDEBUG
    987  1.64  mycroft 	printf("wd(%d,%d) C%dH%dS%d\n", wd->sc_ctrlr, wd->sc_unit,
    988  1.64  mycroft 	    wd->sc_label.d_ncylinders, wd->sc_label.d_ntracks,
    989  1.64  mycroft 	    wd->sc_label.d_nsectors);
    990  1.44  mycroft #endif
    991   1.3  deraadt 
    992  1.64  mycroft 	if (wdcommand(wd, wd->sc_label.d_ncylinders, wd->sc_label.d_ntracks - 1,
    993  1.64  mycroft 	    0, wd->sc_label.d_nsectors, WDCC_IDC) != 0 ||
    994  1.64  mycroft 	    wdcwait(wdc, WDCS_READY) != 0) {
    995  1.64  mycroft 		wderror(wd, NULL, "wdsetctlr: failed");
    996  1.51  mycroft 		return -1;
    997  1.57  mycroft 	}
    998  1.60  mycroft 	return 0;
    999   1.1      cgd }
   1000   1.1      cgd 
   1001   1.1      cgd /*
   1002  1.64  mycroft  * Issue READP to drive to ask it what it is.
   1003   1.1      cgd  */
   1004   1.1      cgd static int
   1005  1.64  mycroft wdgetctlr(wd)
   1006  1.64  mycroft 	struct wd_softc *wd;
   1007   1.3  deraadt {
   1008  1.64  mycroft 	struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
   1009  1.63  mycroft 	int i;
   1010   1.1      cgd 	char tb[DEV_BSIZE];
   1011   1.1      cgd 	struct wdparams *wp;
   1012   1.3  deraadt 
   1013  1.64  mycroft 	if (wdcommand(wd, 0, 0, 0, 0, WDCC_READP) != 0 ||
   1014  1.64  mycroft 	    wait_for_drq(wdc) != 0) {
   1015  1.60  mycroft 		/*
   1016  1.64  mycroft 		 * If WDCC_READP fails then we might have an old drive so we
   1017  1.64  mycroft 		 * try a seek to 0; if that passes then the drive is there but
   1018  1.64  mycroft 		 * it's OLD AND KRUSTY.
   1019  1.60  mycroft 		 */
   1020  1.64  mycroft 		if (wdcommand(wd, 0, 0, 0, 0, WDCC_RESTORE | WD_STEP) != 0 ||
   1021  1.64  mycroft 		    wait_for_ready(wdc) != 0)
   1022  1.60  mycroft 			return -1;
   1023  1.60  mycroft 
   1024  1.64  mycroft 		strncpy(wd->sc_label.d_typename, "ST506",
   1025  1.64  mycroft 		    sizeof wd->sc_label.d_typename);
   1026  1.64  mycroft 		strncpy(wd->sc_params.wdp_model, "Unknown Type",
   1027  1.64  mycroft 		    sizeof wd->sc_params.wdp_model);
   1028  1.64  mycroft 		wd->sc_label.d_type = DTYPE_ST506;
   1029  1.60  mycroft 	} else {
   1030  1.64  mycroft 		/* Obtain parameters. */
   1031  1.64  mycroft 		wp = &wd->sc_params;
   1032  1.64  mycroft 		insw(wdc->sc_iobase+wd_data, tb, sizeof(tb) / sizeof(short));
   1033  1.13  deraadt 		bcopy(tb, wp, sizeof(struct wdparams));
   1034  1.13  deraadt 
   1035  1.64  mycroft 		/* Shuffle string byte order. */
   1036  1.37  mycroft 		for (i = 0; i < sizeof(wp->wdp_model); i += 2) {
   1037  1.13  deraadt 			u_short *p;
   1038  1.37  mycroft 			p = (u_short *)(wp->wdp_model + i);
   1039  1.13  deraadt 			*p = ntohs(*p);
   1040  1.13  deraadt 		}
   1041  1.13  deraadt 
   1042  1.64  mycroft 		strncpy(wd->sc_label.d_typename, "ESDI/IDE",
   1043  1.64  mycroft 		    sizeof wd->sc_label.d_typename);
   1044  1.64  mycroft 		wd->sc_label.d_type = DTYPE_ESDI;
   1045  1.64  mycroft 		bcopy(wp->wdp_model+20, wd->sc_label.d_packname, 14-1);
   1046  1.13  deraadt 
   1047  1.64  mycroft 		/* Update disklabel given drive information. */
   1048  1.64  mycroft 		wd->sc_label.d_ncylinders =
   1049  1.44  mycroft 		    wp->wdp_fixedcyl + wp->wdp_removcyl /*+- 1*/;
   1050  1.64  mycroft 		wd->sc_label.d_ntracks = wp->wdp_heads;
   1051  1.64  mycroft 		wd->sc_label.d_nsectors = wp->wdp_sectors;
   1052  1.64  mycroft 		wd->sc_label.d_secpercyl =
   1053  1.64  mycroft 		    wd->sc_label.d_ntracks * wd->sc_label.d_nsectors;
   1054  1.64  mycroft 		wd->sc_label.d_partitions[1].p_size =
   1055  1.64  mycroft 		    wd->sc_label.d_secpercyl * wp->wdp_sectors;
   1056  1.64  mycroft 		wd->sc_label.d_partitions[1].p_offset = 0;
   1057   1.8  deraadt 	}
   1058  1.13  deraadt 
   1059  1.13  deraadt #if 0
   1060  1.37  mycroft 	printf("gc %x cyl %d trk %d sec %d type %d sz %d model %s\n",
   1061  1.37  mycroft 	    wp->wdp_config, wp->wdp_fixedcyl + wp->wdp_removcyl, wp->wdp_heads,
   1062  1.37  mycroft 	    wp->wdp_sectors, wp->wdp_cntype, wp->wdp_cnsbsz, wp->wdp_model);
   1063  1.13  deraadt #endif
   1064  1.13  deraadt 
   1065  1.64  mycroft 	wd->sc_label.d_subtype |= DSTYPE_GEOMETRY;
   1066   1.3  deraadt 
   1067   1.1      cgd 	/* XXX sometimes possibly needed */
   1068  1.64  mycroft 	(void) inb(wdc->sc_iobase+wd_status);
   1069  1.63  mycroft 
   1070  1.37  mycroft 	return 0;
   1071   1.1      cgd }
   1072   1.1      cgd 
   1073   1.1      cgd int
   1074  1.55  mycroft wdclose(dev, flag, fmt)
   1075  1.55  mycroft 	dev_t dev;
   1076  1.55  mycroft 	int flag;
   1077  1.55  mycroft 	int fmt;
   1078   1.1      cgd {
   1079  1.64  mycroft 	struct wd_softc *wd = wd_softc[WDUNIT(dev)];
   1080  1.51  mycroft 	int part = WDPART(dev), mask = 1 << part;
   1081   1.3  deraadt 
   1082   1.3  deraadt 	switch (fmt) {
   1083   1.3  deraadt 	case S_IFCHR:
   1084  1.64  mycroft 		wd->sc_copenpart &= ~mask;
   1085   1.3  deraadt 		break;
   1086   1.3  deraadt 	case S_IFBLK:
   1087  1.64  mycroft 		wd->sc_bopenpart &= ~mask;
   1088   1.3  deraadt 		break;
   1089   1.3  deraadt 	}
   1090  1.64  mycroft 	wd->sc_openpart = wd->sc_copenpart | wd->sc_bopenpart;
   1091  1.60  mycroft 
   1092  1.37  mycroft 	return 0;
   1093   1.1      cgd }
   1094   1.1      cgd 
   1095   1.1      cgd int
   1096  1.55  mycroft wdioctl(dev, cmd, addr, flag, p)
   1097  1.55  mycroft 	dev_t dev;
   1098  1.55  mycroft 	int cmd;
   1099  1.55  mycroft 	caddr_t addr;
   1100  1.55  mycroft 	int flag;
   1101  1.55  mycroft 	struct proc *p;
   1102   1.1      cgd {
   1103  1.51  mycroft 	int lunit = WDUNIT(dev);
   1104  1.64  mycroft 	struct wd_softc *wd = wd_softc[lunit];
   1105  1.54  mycroft 	int error;
   1106   1.3  deraadt 
   1107   1.1      cgd 	switch (cmd) {
   1108   1.1      cgd 	case DIOCSBAD:
   1109   1.3  deraadt 		if ((flag & FWRITE) == 0)
   1110  1.54  mycroft 			return EBADF;
   1111  1.64  mycroft 		wd->sc_cpulabel.bad = *(struct dkbad *)addr;
   1112  1.64  mycroft 		bad144intern(wd);
   1113  1.54  mycroft 		return 0;
   1114   1.1      cgd 
   1115   1.1      cgd 	case DIOCGDINFO:
   1116  1.64  mycroft 		*(struct disklabel *)addr = wd->sc_label;
   1117  1.54  mycroft 		return 0;
   1118   1.3  deraadt 
   1119   1.3  deraadt 	case DIOCGPART:
   1120  1.64  mycroft 		((struct partinfo *)addr)->disklab = &wd->sc_label;
   1121   1.3  deraadt 		((struct partinfo *)addr)->part =
   1122  1.64  mycroft 		    &wd->sc_label.d_partitions[WDPART(dev)];
   1123  1.54  mycroft 		return 0;
   1124   1.3  deraadt 
   1125   1.3  deraadt 	case DIOCSDINFO:
   1126   1.3  deraadt 		if ((flag & FWRITE) == 0)
   1127  1.54  mycroft 			return EBADF;
   1128  1.64  mycroft 		error = setdisklabel(&wd->sc_label,
   1129  1.54  mycroft 		    (struct disklabel *)addr,
   1130  1.64  mycroft 		    /*(wd->sc_flags & WDF_BSDLABEL) ? wd->sc_openpart : */0,
   1131  1.64  mycroft 		    &wd->sc_cpulabel);
   1132   1.3  deraadt 		if (error == 0) {
   1133  1.64  mycroft 			wd->sc_flags |= WDF_BSDLABEL;
   1134  1.64  mycroft 			wdsetctlr(wd);
   1135   1.1      cgd 		}
   1136  1.54  mycroft 		return error;
   1137   1.3  deraadt 
   1138  1.44  mycroft 	case DIOCWLABEL:
   1139  1.64  mycroft 		wd->sc_flags &= ~WDF_WRITEPROT;
   1140   1.3  deraadt 		if ((flag & FWRITE) == 0)
   1141  1.54  mycroft 			return EBADF;
   1142  1.64  mycroft 		wd->sc_wlabel = *(int *)addr;
   1143  1.54  mycroft 		return 0;
   1144   1.3  deraadt 
   1145  1.44  mycroft 	case DIOCWDINFO:
   1146  1.64  mycroft 		wd->sc_flags &= ~WDF_WRITEPROT;
   1147   1.3  deraadt 		if ((flag & FWRITE) == 0)
   1148  1.54  mycroft 			return EBADF;
   1149  1.64  mycroft 		error = setdisklabel(&wd->sc_label,
   1150  1.37  mycroft 		    (struct disklabel *)addr,
   1151  1.64  mycroft 		    /*(wd->sc_flags & WDF_BSDLABEL) ? wd->sc_openpart :*/0,
   1152  1.64  mycroft 		    &wd->sc_cpulabel);
   1153  1.54  mycroft 		if (error == 0) {
   1154   1.3  deraadt 			int wlab;
   1155   1.3  deraadt 
   1156  1.64  mycroft 			wd->sc_flags |= WDF_BSDLABEL;
   1157  1.64  mycroft 			wdsetctlr(wd);
   1158   1.3  deraadt 
   1159  1.64  mycroft 			/* Simulate opening partition 0 so write succeeds. */
   1160  1.64  mycroft 			wd->sc_openpart |= (1 << 0);	    /* XXX */
   1161  1.64  mycroft 			wlab = wd->sc_wlabel;
   1162  1.64  mycroft 			wd->sc_wlabel = 1;
   1163  1.64  mycroft 			error = writedisklabel(dev, wdstrategy, &wd->sc_label,
   1164  1.64  mycroft 			    &wd->sc_cpulabel);
   1165  1.64  mycroft 			wd->sc_openpart = wd->sc_copenpart | wd->sc_bopenpart;
   1166  1.64  mycroft 			wd->sc_wlabel = wlab;
   1167   1.3  deraadt 		}
   1168  1.54  mycroft 		return error;
   1169   1.3  deraadt 
   1170   1.1      cgd #ifdef notyet
   1171   1.1      cgd 	case DIOCGDINFOP:
   1172  1.64  mycroft 		*(struct disklabel **)addr = &wd->sc_label;
   1173  1.54  mycroft 		return 0;
   1174   1.3  deraadt 
   1175   1.1      cgd 	case DIOCWFORMAT:
   1176   1.1      cgd 		if ((flag & FWRITE) == 0)
   1177  1.54  mycroft 			return EBADF;
   1178  1.54  mycroft 	{
   1179  1.54  mycroft 		register struct format_op *fop;
   1180  1.54  mycroft 		struct iovec aiov;
   1181  1.54  mycroft 		struct uio auio;
   1182   1.3  deraadt 
   1183  1.54  mycroft 		fop = (struct format_op *)addr;
   1184  1.54  mycroft 		aiov.iov_base = fop->df_buf;
   1185  1.54  mycroft 		aiov.iov_len = fop->df_count;
   1186  1.54  mycroft 		auio.uio_iov = &aiov;
   1187  1.54  mycroft 		auio.uio_iovcnt = 1;
   1188  1.54  mycroft 		auio.uio_resid = fop->df_count;
   1189  1.54  mycroft 		auio.uio_segflg = 0;
   1190  1.54  mycroft 		auio.uio_offset =
   1191  1.64  mycroft 		    fop->df_startblk * wd->sc_label.d_secsize;
   1192  1.64  mycroft 		error = physio(wdformat, NULL, dev, B_WRITE, minphys,
   1193  1.64  mycroft 		    &auio);
   1194  1.54  mycroft 		fop->df_count -= auio.uio_resid;
   1195  1.64  mycroft 		fop->df_reg[0] = wdc->sc_status;
   1196  1.64  mycroft 		fop->df_reg[1] = wdc->sc_error;
   1197  1.54  mycroft 		return error;
   1198  1.54  mycroft 	}
   1199   1.1      cgd #endif
   1200   1.3  deraadt 
   1201   1.1      cgd 	default:
   1202  1.54  mycroft 		return ENOTTY;
   1203   1.1      cgd 	}
   1204  1.54  mycroft 
   1205  1.54  mycroft #ifdef DIAGNOSTIC
   1206  1.54  mycroft 	panic("wdioctl: impossible");
   1207  1.54  mycroft #endif
   1208   1.1      cgd }
   1209   1.1      cgd 
   1210  1.44  mycroft #ifdef B_FORMAT
   1211   1.1      cgd int
   1212   1.1      cgd wdformat(struct buf *bp)
   1213   1.1      cgd {
   1214  1.44  mycroft 
   1215   1.1      cgd 	bp->b_flags |= B_FORMAT;
   1216  1.37  mycroft 	return wdstrategy(bp);
   1217   1.1      cgd }
   1218   1.1      cgd #endif
   1219   1.1      cgd 
   1220   1.1      cgd int
   1221  1.55  mycroft wdsize(dev)
   1222  1.55  mycroft 	dev_t dev;
   1223   1.1      cgd {
   1224  1.51  mycroft 	int lunit = WDUNIT(dev), part = WDPART(dev);
   1225  1.64  mycroft 	struct wd_softc *wd;
   1226   1.3  deraadt 
   1227   1.3  deraadt 	if (lunit >= NWD)
   1228  1.37  mycroft 		return -1;
   1229  1.64  mycroft 	wd = wd_softc[lunit];
   1230  1.64  mycroft 	if (wd == 0)
   1231  1.37  mycroft 		return -1;
   1232   1.3  deraadt 
   1233  1.64  mycroft 	if (wd->sc_state < OPEN || (wd->sc_flags & WDF_BSDLABEL) == 0) {
   1234   1.3  deraadt 		int val;
   1235  1.44  mycroft 		val = wdopen(makewddev(major(dev), lunit, WDRAW), FREAD,
   1236  1.44  mycroft 		    S_IFBLK, 0);
   1237  1.55  mycroft 		/* XXX Clear the open flag? */
   1238   1.3  deraadt 		if (val != 0)
   1239  1.37  mycroft 			return -1;
   1240   1.3  deraadt 	}
   1241   1.3  deraadt 
   1242  1.64  mycroft 	if ((wd->sc_flags & (WDF_WRITEPROT | WDF_BSDLABEL)) != WDF_BSDLABEL)
   1243  1.37  mycroft 		return -1;
   1244  1.46  mycroft 
   1245  1.64  mycroft 	return (int)wd->sc_label.d_partitions[part].p_size;
   1246   1.1      cgd }
   1247   1.1      cgd 
   1248  1.64  mycroft /*
   1249  1.64  mycroft  * Dump core after a system crash.
   1250  1.64  mycroft  */
   1251   1.1      cgd int
   1252  1.55  mycroft wddump(dev)
   1253  1.55  mycroft 	dev_t dev;
   1254   1.1      cgd {
   1255  1.64  mycroft 	struct wd_softc *wd;	/* disk unit to do the IO */
   1256  1.64  mycroft 	struct wdc_softc *wdc;
   1257  1.64  mycroft 	long num;		/* number of sectors to write */
   1258  1.64  mycroft 	int lunit, part;
   1259  1.46  mycroft 	long blkoff, blknum;
   1260  1.63  mycroft 	long cylin, head, sector;
   1261  1.60  mycroft 	long secpertrk, secpercyl, nblocks;
   1262   1.1      cgd 	char *addr;
   1263  1.46  mycroft 	static wddoingadump = 0;
   1264   1.1      cgd 	extern caddr_t CADDR1;
   1265  1.30       ws 	extern struct pte *CMAP1;
   1266  1.30       ws 
   1267  1.64  mycroft 	addr = (char *)0;	/* starting address */
   1268   1.3  deraadt 
   1269  1.16  deraadt #if DO_NOT_KNOW_HOW
   1270  1.64  mycroft 	/* Toss any characters present prior to dump, ie. non-blocking getc. */
   1271  1.16  deraadt 	while (cngetc())
   1272   1.1      cgd 		;
   1273  1.16  deraadt #endif
   1274  1.60  mycroft 
   1275  1.51  mycroft 	lunit = WDUNIT(dev);
   1276  1.64  mycroft 	/* Check for acceptable drive number. */
   1277   1.3  deraadt 	if (lunit >= NWD)
   1278  1.37  mycroft 		return ENXIO;
   1279  1.64  mycroft 	wd = wd_softc[lunit];
   1280  1.64  mycroft 	/* Was it ever initialized? */
   1281  1.64  mycroft 	if (wd == 0 || wd->sc_state < OPEN || wd->sc_flags & WDF_WRITEPROT)
   1282  1.37  mycroft 		return ENXIO;
   1283  1.46  mycroft 
   1284  1.64  mycroft 	wdc = (void *)wd->sc_dev.dv_parent;
   1285  1.64  mycroft 
   1286  1.64  mycroft 	/* Convert to disk sectors. */
   1287  1.64  mycroft 	num = ctob(physmem) / wd->sc_label.d_secsize;
   1288   1.3  deraadt 
   1289  1.64  mycroft 	secpertrk = wd->sc_label.d_nsectors;
   1290  1.64  mycroft 	secpercyl = wd->sc_label.d_secpercyl;
   1291  1.64  mycroft 	part = WDPART(dev);
   1292  1.64  mycroft 	nblocks = wd->sc_label.d_partitions[part].p_size;
   1293  1.64  mycroft 	blkoff = wd->sc_label.d_partitions[part].p_offset;
   1294   1.3  deraadt 
   1295  1.60  mycroft 	/*printf("part %x, nblocks %d, dumplo %d, num %d\n", part, nblocks,
   1296  1.51  mycroft 	    dumplo, num);*/
   1297  1.60  mycroft 
   1298  1.64  mycroft 	/* Check transfer bounds against partition size. */
   1299  1.46  mycroft 	if (dumplo < 0 || dumplo + num > nblocks)
   1300  1.37  mycroft 		return EINVAL;
   1301  1.60  mycroft 
   1302  1.60  mycroft 	if (wddoingadump)
   1303  1.60  mycroft 		return EFAULT;
   1304   1.3  deraadt 	wddoingadump = 1;
   1305  1.60  mycroft 
   1306  1.60  mycroft 	/* Recalibrate. */
   1307  1.64  mycroft 	if (wdcommand(wd, 0, 0, 0, 0, WDCC_RESTORE | WD_STEP) != 0 ||
   1308  1.64  mycroft 	    wait_for_ready(wdc) != 0 || wdsetctlr(wd) != 0) {
   1309  1.64  mycroft 		wderror(wd, NULL, "wddump: recal failed");
   1310  1.51  mycroft 		return EIO;
   1311  1.63  mycroft 	}
   1312   1.3  deraadt 
   1313   1.1      cgd 	blknum = dumplo + blkoff;
   1314   1.1      cgd 	while (num > 0) {
   1315  1.64  mycroft 		/* Compute disk address. */
   1316   1.1      cgd 		cylin = blknum / secpercyl;
   1317   1.1      cgd 		head = (blknum % secpercyl) / secpertrk;
   1318   1.1      cgd 		sector = blknum % secpertrk;
   1319   1.3  deraadt 
   1320  1.64  mycroft 		if (wd->sc_flags & WDF_BADSECT) {
   1321   1.3  deraadt 			long newblk;
   1322   1.3  deraadt 			int i;
   1323   1.3  deraadt 
   1324  1.64  mycroft 			for (i = 0; wd->sc_badsect[i] != -1; i++) {
   1325  1.64  mycroft 				if (blknum < wd->sc_badsect[i]) {
   1326  1.64  mycroft 					/* Sorted list, passed our block by. */
   1327  1.44  mycroft 					break;
   1328  1.64  mycroft 				} else if (blknum == wd->sc_badsect[i]) {
   1329  1.64  mycroft 					newblk = wd->sc_label.d_secperunit -
   1330  1.64  mycroft 						wd->sc_label.d_nsectors - i - 1;
   1331   1.3  deraadt 					cylin = newblk / secpercyl;
   1332   1.3  deraadt 					head = (newblk % secpercyl) / secpertrk;
   1333   1.3  deraadt 					sector = newblk % secpertrk;
   1334  1.64  mycroft 					/* Found and replaced; done. */
   1335   1.3  deraadt 					break;
   1336   1.3  deraadt 				}
   1337   1.1      cgd 			}
   1338   1.3  deraadt 		}
   1339   1.1      cgd 		sector++;		/* origin 1 */
   1340   1.3  deraadt 
   1341   1.1      cgd #ifdef notdef
   1342  1.64  mycroft 		/* Let's just talk about this first. */
   1343  1.60  mycroft 		printf("cylin %d, head %d, sector %d, addr 0x%x", cylin, head,
   1344  1.60  mycroft 		    sector, addr);
   1345   1.1      cgd #endif
   1346  1.64  mycroft 		if (wdcommand(wd, cylin, head, sector, 1, WDCC_WRITE) != 0) {
   1347  1.64  mycroft 			wderror(wd, NULL, "wddump: wdcommand failed");
   1348  1.63  mycroft 			return EIO;
   1349  1.63  mycroft 		}
   1350  1.64  mycroft 		if (wait_for_drq(wdc) != 0) {
   1351  1.64  mycroft 			wderror(wd, NULL, "wddump: timeout waiting for drq");
   1352  1.51  mycroft 			return EIO;
   1353  1.63  mycroft 		}
   1354   1.3  deraadt 
   1355  1.64  mycroft #ifdef notdef	/* Cannot use this since this address was mapped differently. */
   1356  1.46  mycroft 		pmap_enter(kernel_pmap, CADDR1, trunc_page(addr), VM_PROT_READ, TRUE);
   1357  1.46  mycroft #else
   1358  1.46  mycroft 		*(int *)CMAP1 = PG_V | PG_KW | ctob((long)addr);
   1359  1.46  mycroft 		tlbflush();
   1360  1.46  mycroft #endif
   1361  1.46  mycroft 
   1362  1.64  mycroft 		outsw(wdc->sc_iobase+wd_data, CADDR1 + ((int)addr & PGOFSET),
   1363  1.51  mycroft 		    DEV_BSIZE / sizeof(short));
   1364   1.3  deraadt 
   1365  1.13  deraadt 		/* Check data request (should be done). */
   1366  1.64  mycroft 		if (wait_for_ready(wdc) != 0) {
   1367  1.64  mycroft 			wderror(wd, NULL, "wddump: timeout waiting for ready");
   1368  1.63  mycroft 			return EIO;
   1369  1.63  mycroft 		}
   1370  1.64  mycroft 		if (wdc->sc_status & WDCS_DRQ) {
   1371  1.64  mycroft 			wderror(wd, NULL, "wddump: extra drq");
   1372  1.37  mycroft 			return EIO;
   1373  1.63  mycroft 		}
   1374   1.3  deraadt 
   1375  1.46  mycroft 		if ((unsigned)addr % 1048576 == 0)
   1376  1.46  mycroft 			printf("%d ", num / (1048576 / DEV_BSIZE));
   1377   1.1      cgd 
   1378  1.64  mycroft 		/* Update block count. */
   1379   1.1      cgd 		num--;
   1380   1.3  deraadt 		blknum++;
   1381  1.46  mycroft 		(int)addr += DEV_BSIZE;
   1382   1.3  deraadt 
   1383  1.16  deraadt #if DO_NOT_KNOW_HOW
   1384  1.64  mycroft 		/* Operator aborting dump? non-blocking getc() */
   1385  1.16  deraadt 		if (cngetc())
   1386  1.37  mycroft 			return EINTR;
   1387  1.16  deraadt #endif
   1388   1.1      cgd 	}
   1389  1.37  mycroft 	return 0;
   1390   1.1      cgd }
   1391   1.3  deraadt 
   1392   1.3  deraadt /*
   1393   1.3  deraadt  * Internalize the bad sector table.
   1394   1.3  deraadt  */
   1395   1.3  deraadt void
   1396  1.64  mycroft bad144intern(wd)
   1397  1.64  mycroft 	struct wd_softc *wd;
   1398   1.3  deraadt {
   1399   1.3  deraadt 	int i;
   1400  1.55  mycroft 
   1401  1.64  mycroft 	if ((wd->sc_flags & WDF_BADSECT) == 0)
   1402  1.55  mycroft 		return;
   1403  1.55  mycroft 
   1404  1.55  mycroft 	for (i = 0; i < 127; i++)
   1405  1.64  mycroft 		wd->sc_badsect[i] = -1;
   1406  1.55  mycroft 	for (i = 0; i < 126; i++) {
   1407  1.64  mycroft 		if (wd->sc_cpulabel.bad.bt_bad[i].bt_cyl == 0xffff)
   1408  1.55  mycroft 			break;
   1409  1.64  mycroft 		wd->sc_badsect[i] =
   1410  1.64  mycroft 		    wd->sc_cpulabel.bad.bt_bad[i].bt_cyl *
   1411  1.64  mycroft 		    wd->sc_label.d_secpercyl +
   1412  1.64  mycroft 		    (wd->sc_cpulabel.bad.bt_bad[i].bt_trksec >> 8) *
   1413  1.64  mycroft 			wd->sc_label.d_nsectors +
   1414  1.64  mycroft 		    (wd->sc_cpulabel.bad.bt_bad[i].bt_trksec & 0x00ff);
   1415   1.3  deraadt 	}
   1416   1.3  deraadt }
   1417   1.3  deraadt 
   1418  1.27      cgd static int
   1419  1.64  mycroft wdcreset(wdc)
   1420  1.64  mycroft 	struct wdc_softc *wdc;
   1421  1.21  deraadt {
   1422  1.64  mycroft 	u_short iobase = wdc->sc_iobase;
   1423  1.21  deraadt 
   1424  1.64  mycroft 	/* Reset the device. */
   1425  1.64  mycroft 	outb(iobase+wd_ctlr, WDCTL_RST | WDCTL_IDS);
   1426  1.61  mycroft 	delay(1000);
   1427  1.64  mycroft 	outb(iobase+wd_ctlr, WDCTL_IDS);
   1428  1.61  mycroft 	delay(1000);
   1429  1.64  mycroft 	(void) inb(iobase+wd_error);
   1430  1.64  mycroft 	outb(iobase+wd_ctlr, WDCTL_4BIT);
   1431  1.64  mycroft 
   1432  1.64  mycroft 	if (wait_for_unbusy(wdc) < 0) {
   1433  1.64  mycroft 		printf("%s: reset failed\n", wdc->sc_dev.dv_xname);
   1434  1.64  mycroft 		return 1;
   1435  1.64  mycroft 	}
   1436  1.64  mycroft 
   1437  1.64  mycroft 	return 0;
   1438  1.64  mycroft }
   1439  1.64  mycroft 
   1440  1.64  mycroft static void
   1441  1.64  mycroft wdcrestart(wdc)
   1442  1.64  mycroft 	struct wdc_softc *wdc;
   1443  1.64  mycroft {
   1444  1.64  mycroft 	int s = splbio();
   1445  1.64  mycroft 
   1446  1.64  mycroft 	wdc->sc_q.b_active = 0;
   1447  1.64  mycroft 	wdcstart(wdc);
   1448  1.64  mycroft 	splx(s);
   1449  1.64  mycroft }
   1450  1.64  mycroft 
   1451  1.64  mycroft /*
   1452  1.64  mycroft  * Unwedge the controller after an unexpected error.  We do this by resetting
   1453  1.64  mycroft  * it, marking all drives for recalibration, and stalling the queue for a short
   1454  1.64  mycroft  * period to give the reset time to finish.
   1455  1.64  mycroft  * NOTE: We use a timeout here, so this routine must not be called during
   1456  1.64  mycroft  * autoconfig or dump.
   1457  1.64  mycroft  */
   1458  1.64  mycroft static void
   1459  1.64  mycroft wdcunwedge(wdc)
   1460  1.64  mycroft 	struct wdc_softc *wdc;
   1461  1.64  mycroft {
   1462  1.64  mycroft 	int lunit;
   1463  1.64  mycroft 
   1464  1.64  mycroft 	wdc->sc_timeout = 0;
   1465  1.64  mycroft 	(void) wdcreset(wdc);
   1466  1.21  deraadt 
   1467  1.64  mycroft 	/* Schedule recalibrate for all drives on this controller. */
   1468  1.64  mycroft 	for (lunit = 0; lunit < NWD; lunit++) {
   1469  1.64  mycroft 		struct wd_softc *wd = wd_softc[lunit];
   1470  1.64  mycroft 		if (!wd || (void *)wd->sc_dev.dv_parent != wdc)
   1471  1.64  mycroft 			continue;
   1472  1.64  mycroft 		if (wd->sc_state > WANTOPEN)
   1473  1.64  mycroft 			wd->sc_state = WANTOPEN;
   1474  1.64  mycroft 		if (wd->sc_q.b_active && (wd->sc_flags & WDF_SINGLE) == 0)
   1475  1.64  mycroft 			wd->sc_flags |= WDF_ERROR;
   1476  1.64  mycroft 	}
   1477  1.64  mycroft 
   1478  1.64  mycroft 	++wdc->sc_q.b_errcnt;
   1479  1.64  mycroft 
   1480  1.64  mycroft 	/* Wake up in a little bit and restart the operation. */
   1481  1.64  mycroft 	timeout((timeout_t)wdcrestart, (caddr_t)wdc, RECOVERYTIME);
   1482  1.40  mycroft }
   1483  1.40  mycroft 
   1484  1.40  mycroft int
   1485  1.64  mycroft wdcwait(wdc, mask)
   1486  1.64  mycroft 	struct wdc_softc *wdc;
   1487  1.49  mycroft 	int mask;
   1488  1.40  mycroft {
   1489  1.64  mycroft 	u_short iobase = wdc->sc_iobase;
   1490  1.40  mycroft 	int timeout = 0;
   1491  1.63  mycroft 	u_char status;
   1492  1.40  mycroft 
   1493  1.40  mycroft 	for (;;) {
   1494  1.64  mycroft 		wdc->sc_status = status = inb(iobase+wd_altsts);
   1495  1.63  mycroft 		if ((status & WDCS_BUSY) == 0 && (status & mask) == mask)
   1496  1.54  mycroft 			break;
   1497  1.40  mycroft 		if (++timeout > WDCNDELAY)
   1498  1.40  mycroft 			return -1;
   1499  1.61  mycroft 		delay(WDCDELAY);
   1500  1.21  deraadt 	}
   1501  1.63  mycroft 	if (status & WDCS_ERR)
   1502  1.64  mycroft 		wdc->sc_error = inb(iobase+wd_error);
   1503  1.23  deraadt #ifdef WDCNDELAY_DEBUG
   1504  1.37  mycroft 	if (timeout > WDCNDELAY_DEBUG)
   1505  1.64  mycroft 		printf("%s: busy-wait took %dus\n", wdc->sc_dev.dv_xname,
   1506  1.51  mycroft 		    WDCDELAY * timeout);
   1507  1.23  deraadt #endif
   1508  1.63  mycroft 	return status & WDCS_ERR;
   1509  1.27      cgd }
   1510  1.27      cgd 
   1511  1.60  mycroft static void
   1512  1.64  mycroft wdctimeout(wdc)
   1513  1.64  mycroft 	struct wdc_softc *wdc;
   1514  1.27      cgd {
   1515  1.55  mycroft 	int s = splbio();
   1516  1.27      cgd 
   1517  1.64  mycroft 	if (wdc->sc_timeout && --wdc->sc_timeout == 0) {
   1518  1.64  mycroft 		wderror(wdc, NULL, "lost interrupt");
   1519  1.64  mycroft 		wdcunwedge(wdc);
   1520  1.27      cgd 	}
   1521  1.64  mycroft 	timeout((timeout_t)wdctimeout, (caddr_t)wdc, hz);
   1522  1.55  mycroft 	splx(s);
   1523  1.63  mycroft }
   1524  1.63  mycroft 
   1525  1.63  mycroft static void
   1526  1.64  mycroft wderror(dev, bp, msg)
   1527  1.64  mycroft 	void *dev;
   1528  1.63  mycroft 	struct buf *bp;
   1529  1.63  mycroft 	char *msg;
   1530  1.63  mycroft {
   1531  1.64  mycroft 	struct wd_softc *wd = dev;
   1532  1.64  mycroft 	struct wdc_softc *wdc = dev;
   1533  1.64  mycroft 
   1534  1.63  mycroft 	if (bp)
   1535  1.64  mycroft 		diskerr(bp, "wd", msg, LOG_PRINTF, wd->sc_skip, &wd->sc_label);
   1536  1.63  mycroft 	else
   1537  1.64  mycroft 		printf("%s: %s: status %b error %b\n", wdc->sc_dev.dv_xname,
   1538  1.64  mycroft 		    msg, wdc->sc_status, WDCS_BITS, wdc->sc_error, WDERR_BITS);
   1539  1.21  deraadt }
   1540  1.43  mycroft 
   1541  1.43  mycroft #endif /* NWDC > 0 */
   1542