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