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