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