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