Home | History | Annotate | Line # | Download | only in ata
wd.c revision 1.164
      1  1.164   thorpej /*	$NetBSD: wd.c,v 1.164 1997/10/08 23:10:10 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.149  christos #include <sys/proc.h>
     50   1.29   mycroft 
     51   1.29   mycroft #include <vm/vm.h>
     52   1.29   mycroft 
     53   1.29   mycroft #include <machine/cpu.h>
     54  1.150   mycroft #include <machine/intr.h>
     55   1.29   mycroft #include <machine/pio.h>
     56   1.29   mycroft 
     57  1.139       cgd #include <dev/isa/isavar.h>
     58  1.130       cgd #include <dev/isa/wdreg.h>
     59  1.163    bouyer #include <dev/isa/wdlink.h>
     60  1.163    bouyer #include "locators.h"
     61    1.1       cgd 
     62   1.98   mycroft #define	WAITTIME	(4 * hz)	/* time to wait for a completion */
     63   1.23   deraadt 
     64  1.135   mycroft #define	WDIORETRIES	5		/* number of retries before giving up */
     65    1.1       cgd 
     66   1.92       cgd #define	WDUNIT(dev)			DISKUNIT(dev)
     67   1.92       cgd #define	WDPART(dev)			DISKPART(dev)
     68   1.92       cgd #define	MAKEWDDEV(maj, unit, part)	MAKEDISKDEV(maj, unit, part)
     69   1.92       cgd 
     70   1.92       cgd #define	WDLABELDEV(dev)	(MAKEWDDEV(major(dev), WDUNIT(dev), RAW_PART))
     71    1.1       cgd 
     72  1.163    bouyer #ifdef WDDEBUG
     73  1.163    bouyer #define WDDEBUG_PRINT(args)		printf args
     74  1.163    bouyer #else
     75  1.163    bouyer #define WDDEBUG_PRINT(args)
     76  1.163    bouyer #endif
     77  1.163    bouyer 
     78   1.64   mycroft struct wd_softc {
     79   1.64   mycroft 	struct device sc_dev;
     80  1.144   thorpej 	struct disk sc_dk;
     81  1.163    bouyer 	struct wd_link *d_link;
     82   1.64   mycroft 	struct buf sc_q;
     83   1.74   mycroft };
     84   1.64   mycroft 
     85  1.163    bouyer int	wdprobe		__P((struct device *, void *, void *));
     86  1.163    bouyer void	wdattach	__P((struct device *, struct device *, void *));
     87  1.163    bouyer int	wdprint		__P((void *, char *));
     88  1.102   mycroft 
     89  1.147   thorpej struct cfattach wd_ca = {
     90  1.147   thorpej 	sizeof(struct wd_softc), wdprobe, wdattach
     91  1.147   thorpej };
     92  1.147   thorpej 
     93  1.147   thorpej struct cfdriver wd_cd = {
     94  1.147   thorpej 	NULL, "wd", DV_DISK
     95   1.65   mycroft };
     96  1.108   mycroft 
     97  1.164   thorpej void	wdgetdefaultlabel __P((struct wd_softc *, struct disklabel *));
     98  1.149  christos void	wdgetdisklabel	__P((struct wd_softc *));
     99  1.149  christos int	wd_get_parms	__P((struct wd_softc *));
    100  1.149  christos void	wdstrategy	__P((struct buf *));
    101  1.163    bouyer void	wdstart		__P((void *));
    102  1.108   mycroft 
    103  1.102   mycroft struct dkdriver wddkdriver = { wdstrategy };
    104   1.65   mycroft 
    105  1.149  christos /* XXX: these should go elsewhere */
    106  1.149  christos cdev_decl(wd);
    107  1.149  christos bdev_decl(wd);
    108  1.149  christos 
    109  1.149  christos void	wdfinish	__P((struct wd_softc *, struct buf *));
    110  1.163    bouyer int	wdsetctlr	__P((struct wd_link *));
    111   1.64   mycroft static void bad144intern __P((struct wd_softc *));
    112  1.163    bouyer int	wdlock		__P((struct wd_link *));
    113  1.163    bouyer void	wdunlock	__P((struct wd_link *));
    114    1.1       cgd 
    115    1.1       cgd int
    116  1.163    bouyer wdprobe(parent, match, aux)
    117  1.102   mycroft 	struct device *parent;
    118  1.102   mycroft 	void *match, *aux;
    119    1.1       cgd {
    120  1.163    bouyer 	struct cfdata *cf = match;
    121  1.163    bouyer 	struct wd_link *d_link = aux;
    122  1.163    bouyer 	int drive;
    123  1.163    bouyer 
    124  1.163    bouyer 	if (d_link == NULL)
    125   1.64   mycroft 		return 0;
    126  1.163    bouyer 	if (d_link->type != ATA)
    127  1.105   mycroft 		return 0;
    128  1.105   mycroft 
    129  1.163    bouyer 	drive = d_link->drive;
    130  1.163    bouyer 	if (cf->cf_loc[ATACF_DRIVE] != ATACF_DRIVE_DEFAULT &&
    131  1.163    bouyer 		cf->cf_loc[ATACF_DRIVE] != drive)
    132   1.74   mycroft 		return 0;
    133   1.64   mycroft 
    134   1.74   mycroft 	return 1;
    135   1.74   mycroft }
    136   1.64   mycroft 
    137   1.74   mycroft void
    138   1.74   mycroft wdattach(parent, self, aux)
    139   1.74   mycroft 	struct device *parent, *self;
    140   1.74   mycroft 	void *aux;
    141   1.74   mycroft {
    142   1.74   mycroft 	struct wd_softc *wd = (void *)self;
    143  1.163    bouyer 	struct wd_link *d_link= aux;
    144   1.74   mycroft 	int i, blank;
    145  1.135   mycroft 	char buf[41], c, *p, *q;
    146   1.56   mycroft 
    147  1.163    bouyer 	wd->d_link = d_link;
    148  1.163    bouyer 	d_link->openings = 1;
    149  1.163    bouyer 	d_link->wd_softc = (caddr_t)wd;
    150  1.106   mycroft 
    151  1.144   thorpej 	/*
    152  1.144   thorpej 	 * Initialize and attach the disk structure.
    153  1.144   thorpej 	 */
    154  1.144   thorpej 	wd->sc_dk.dk_driver = &wddkdriver;
    155  1.144   thorpej 	wd->sc_dk.dk_name = wd->sc_dev.dv_xname;
    156  1.144   thorpej 	disk_attach(&wd->sc_dk);
    157  1.144   thorpej 
    158  1.163    bouyer 	d_link->sc_lp = wd->sc_dk.dk_label;
    159  1.163    bouyer 
    160  1.163    bouyer 	wdc_get_parms((struct wdc_softc *)d_link->wdc_softc, d_link);
    161  1.163    bouyer 	for (blank = 0, p = d_link->sc_params.wdp_model, q = buf, i = 0;
    162  1.163    bouyer 	     i < sizeof(d_link->sc_params.wdp_model); i++) {
    163  1.135   mycroft 		c = *p++;
    164  1.135   mycroft 		if (c == '\0')
    165  1.135   mycroft 			break;
    166  1.135   mycroft 		if (c != ' ') {
    167  1.135   mycroft 			if (blank) {
    168  1.135   mycroft 				*q++ = ' ';
    169  1.135   mycroft 				blank = 0;
    170  1.135   mycroft 			}
    171  1.135   mycroft 			*q++ = c;
    172  1.135   mycroft 		} else
    173  1.135   mycroft 			blank = 1;
    174  1.135   mycroft 	}
    175  1.135   mycroft 	*q++ = '\0';
    176  1.135   mycroft 
    177  1.155   thorpej 	printf(": <%s>\n%s: %dMB, %d cyl, %d head, %d sec, %d bytes/sec\n",
    178  1.163    bouyer 	    buf, self->dv_xname,
    179  1.163    bouyer 	    d_link->sc_params.wdp_cylinders *
    180  1.163    bouyer 	      (d_link->sc_params.wdp_heads * d_link->sc_params.wdp_sectors) /
    181  1.163    bouyer 		  (1048576 / DEV_BSIZE),
    182  1.163    bouyer 	    d_link->sc_params.wdp_cylinders,
    183  1.163    bouyer 	    d_link->sc_params.wdp_heads,
    184  1.163    bouyer 	    d_link->sc_params.wdp_sectors,
    185  1.155   thorpej 	    DEV_BSIZE);
    186  1.113   mycroft 
    187  1.163    bouyer 	if ((d_link->sc_params.wdp_capabilities & WD_CAP_DMA) != 0 &&
    188  1.163    bouyer 	    d_link->sc_mode == WDM_DMA) {
    189  1.163    bouyer 		d_link->sc_mode = WDM_DMA;
    190  1.163    bouyer 	} else if (d_link->sc_params.wdp_maxmulti > 1) {
    191  1.163    bouyer 		d_link->sc_mode = WDM_PIOMULTI;
    192  1.163    bouyer 		d_link->sc_multiple = min(d_link->sc_params.wdp_maxmulti, 16);
    193  1.113   mycroft 	} else {
    194  1.163    bouyer 		d_link->sc_mode = WDM_PIOSINGLE;
    195  1.163    bouyer 		d_link->sc_multiple = 1;
    196  1.113   mycroft 	}
    197  1.113   mycroft 
    198  1.153  christos 	printf("%s: using", wd->sc_dev.dv_xname);
    199  1.163    bouyer 	if (d_link->sc_mode == WDM_DMA)
    200  1.153  christos 		printf(" dma transfers,");
    201  1.113   mycroft 	else
    202  1.153  christos 		printf(" %d-sector %d-bit pio transfers,",
    203  1.163    bouyer 		    d_link->sc_multiple,
    204  1.163    bouyer 		    (d_link->sc_flags & WDF_32BIT) == 0 ? 16 : 32);
    205  1.163    bouyer 	if ((d_link->sc_params.wdp_capabilities & WD_CAP_LBA) != 0)
    206  1.153  christos 		printf(" lba addressing\n");
    207  1.116   mycroft 	else
    208  1.153  christos 		printf(" chs addressing\n");
    209    1.1       cgd }
    210    1.1       cgd 
    211   1.64   mycroft /*
    212  1.135   mycroft  * Read/write routine for a buffer.  Validates the arguments and schedules the
    213  1.135   mycroft  * transfer.  Does not wait for the transfer to complete.
    214    1.1       cgd  */
    215   1.64   mycroft void
    216   1.55   mycroft wdstrategy(bp)
    217   1.55   mycroft 	struct buf *bp;
    218    1.1       cgd {
    219  1.147   thorpej 	struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(bp->b_dev)];
    220  1.163    bouyer 	struct wd_link *d_link= wd->d_link;
    221   1.37   mycroft 	int s;
    222    1.3   deraadt 
    223  1.135   mycroft 	/* Valid request?  */
    224  1.135   mycroft 	if (bp->b_blkno < 0 ||
    225  1.144   thorpej 	    (bp->b_bcount % wd->sc_dk.dk_label->d_secsize) != 0 ||
    226  1.144   thorpej 	    (bp->b_bcount / wd->sc_dk.dk_label->d_secsize) >= (1 << NBBY)) {
    227    1.1       cgd 		bp->b_error = EINVAL;
    228   1.93   mycroft 		goto bad;
    229    1.1       cgd 	}
    230    1.3   deraadt 
    231  1.135   mycroft 	/* If device invalidated (e.g. media change, door open), error. */
    232  1.163    bouyer 	if ((d_link->sc_flags & WDF_LOADED) == 0) {
    233  1.135   mycroft 		bp->b_error = EIO;
    234  1.135   mycroft 		goto bad;
    235  1.135   mycroft 	}
    236  1.135   mycroft 
    237   1.93   mycroft 	/* If it's a null transfer, return immediately. */
    238   1.93   mycroft 	if (bp->b_bcount == 0)
    239   1.93   mycroft 		goto done;
    240   1.93   mycroft 
    241  1.128   mycroft 	/*
    242  1.128   mycroft 	 * Do bounds checking, adjust transfer. if error, process.
    243  1.128   mycroft 	 * If end of partition, just return.
    244  1.128   mycroft 	 */
    245  1.138   mycroft 	if (WDPART(bp->b_dev) != RAW_PART &&
    246  1.144   thorpej 	    bounds_check_with_label(bp, wd->sc_dk.dk_label,
    247  1.163    bouyer 	    (d_link->sc_flags & (WDF_WLABEL|WDF_LABELLING)) != 0) <= 0)
    248  1.128   mycroft 		goto done;
    249    1.3   deraadt 
    250   1.64   mycroft 	/* Queue transfer on drive, activate drive and controller if idle. */
    251    1.1       cgd 	s = splbio();
    252  1.113   mycroft 	disksort(&wd->sc_q, bp);
    253  1.163    bouyer 	wdstart(wd);
    254    1.1       cgd 	splx(s);
    255   1.64   mycroft 	return;
    256    1.3   deraadt 
    257   1.93   mycroft bad:
    258   1.93   mycroft 	bp->b_flags |= B_ERROR;
    259    1.1       cgd done:
    260   1.64   mycroft 	/* Toss transfer; we're done early. */
    261  1.135   mycroft 	bp->b_resid = bp->b_bcount;
    262    1.1       cgd 	biodone(bp);
    263    1.1       cgd }
    264    1.1       cgd 
    265    1.1       cgd /*
    266  1.135   mycroft  * Queue a drive for I/O.
    267    1.1       cgd  */
    268  1.108   mycroft void
    269  1.163    bouyer wdstart(arg)
    270  1.163    bouyer 	void *arg;
    271    1.1       cgd {
    272  1.163    bouyer 	struct wd_softc *wd = arg;
    273  1.163    bouyer 	struct buf *dp, *bp=0;
    274  1.163    bouyer 	struct wd_link *d_link = wd->d_link;
    275  1.163    bouyer 	struct wdc_xfer *xfer;
    276  1.163    bouyer 	u_long p_offset;
    277  1.163    bouyer 
    278  1.163    bouyer 	while (d_link->openings > 0) {
    279  1.163    bouyer 
    280  1.163    bouyer 		/* Is there a buf for us ? */
    281  1.163    bouyer 		dp = &wd->sc_q;
    282  1.163    bouyer 		if ((bp = dp->b_actf) == NULL)  /* yes, an assign */
    283  1.163    bouyer                  	return;
    284  1.163    bouyer 		dp->b_actf = bp->b_actf;
    285  1.163    bouyer 
    286  1.163    bouyer 		/*
    287  1.163    bouyer 		 * Make the command. First lock the device
    288  1.163    bouyer 		 */
    289  1.163    bouyer 		d_link->openings--;
    290  1.163    bouyer 		if (WDPART(bp->b_dev) != RAW_PART)
    291  1.163    bouyer 			p_offset =
    292  1.163    bouyer 		  wd->sc_dk.dk_label->d_partitions[WDPART(bp->b_dev)].p_offset;
    293  1.163    bouyer 		else
    294  1.163    bouyer 			p_offset = 0;
    295   1.64   mycroft 
    296  1.163    bouyer 		xfer = wdc_get_xfer(0);
    297  1.163    bouyer 		if (xfer == NULL)
    298  1.163    bouyer 			panic("wdc_xfer");
    299  1.163    bouyer 
    300  1.163    bouyer 		xfer->d_link = d_link;
    301  1.163    bouyer 		xfer->c_bp = bp;
    302  1.163    bouyer 		xfer->c_p_offset = p_offset;
    303  1.163    bouyer 		xfer->databuf = bp->b_data;
    304  1.163    bouyer 		xfer->c_bcount = bp->b_bcount;
    305  1.163    bouyer 		xfer->c_flags |= bp->b_flags & (B_READ|B_WRITE);
    306  1.163    bouyer 		xfer->c_blkno = bp->b_blkno;
    307  1.144   thorpej 
    308  1.163    bouyer 		/* Instrumentation. */
    309  1.163    bouyer 		disk_busy(&wd->sc_dk);
    310  1.163    bouyer 		wdc_exec_xfer((struct wdc_softc *)wd->d_link->wdc_softc,
    311  1.163    bouyer 			wd->d_link, xfer);
    312   1.64   mycroft 	}
    313  1.141   mycroft }
    314  1.141   mycroft 
    315  1.141   mycroft int
    316  1.149  christos wdread(dev, uio, flags)
    317  1.141   mycroft 	dev_t dev;
    318  1.141   mycroft 	struct uio *uio;
    319  1.149  christos 	int flags;
    320  1.141   mycroft {
    321  1.141   mycroft 
    322  1.163    bouyer 	WDDEBUG_PRINT(("wdread\n"));
    323  1.141   mycroft 	return (physio(wdstrategy, NULL, dev, B_READ, minphys, uio));
    324  1.141   mycroft }
    325  1.141   mycroft 
    326  1.141   mycroft int
    327  1.149  christos wdwrite(dev, uio, flags)
    328  1.141   mycroft 	dev_t dev;
    329  1.141   mycroft 	struct uio *uio;
    330  1.149  christos 	int flags;
    331  1.141   mycroft {
    332  1.141   mycroft 
    333  1.163    bouyer 	WDDEBUG_PRINT(("wdwrite\n"));
    334  1.141   mycroft 	return (physio(wdstrategy, NULL, dev, B_WRITE, minphys, uio));
    335   1.64   mycroft }
    336   1.64   mycroft 
    337    1.1       cgd /*
    338  1.135   mycroft  * Wait interruptibly for an exclusive lock.
    339  1.135   mycroft  *
    340  1.135   mycroft  * XXX
    341  1.135   mycroft  * Several drivers do this; it should be abstracted and made MP-safe.
    342  1.135   mycroft  */
    343  1.132   mycroft int
    344  1.163    bouyer wdlock(d_link)
    345  1.163    bouyer 	struct wd_link *d_link;
    346  1.132   mycroft {
    347  1.132   mycroft 	int error;
    348  1.163    bouyer 	int s;
    349  1.163    bouyer 
    350  1.163    bouyer 	WDDEBUG_PRINT(("wdlock\n"));
    351  1.132   mycroft 
    352  1.163    bouyer 	s = splbio();
    353  1.163    bouyer 
    354  1.163    bouyer 	while ((d_link->sc_flags & WDF_LOCKED) != 0) {
    355  1.163    bouyer 		d_link->sc_flags |= WDF_WANTED;
    356  1.163    bouyer 		if ((error = tsleep(d_link, PRIBIO | PCATCH,
    357  1.163    bouyer 		    "wdlck", 0)) != 0) {
    358  1.163    bouyer 			splx(s);
    359  1.132   mycroft 			return error;
    360  1.163    bouyer 		}
    361  1.132   mycroft 	}
    362  1.163    bouyer 	d_link->sc_flags |= WDF_LOCKED;
    363  1.163    bouyer 	splx(s);
    364  1.132   mycroft 	return 0;
    365  1.132   mycroft }
    366  1.132   mycroft 
    367  1.135   mycroft /*
    368  1.135   mycroft  * Unlock and wake up any waiters.
    369  1.135   mycroft  */
    370  1.132   mycroft void
    371  1.163    bouyer wdunlock(d_link)
    372  1.163    bouyer 	struct wd_link *d_link;
    373  1.132   mycroft {
    374  1.132   mycroft 
    375  1.163    bouyer 	WDDEBUG_PRINT(("wdunlock"));
    376  1.163    bouyer 
    377  1.163    bouyer 	d_link->sc_flags &= ~WDF_LOCKED;
    378  1.163    bouyer 	if ((d_link->sc_flags & WDF_WANTED) != 0) {
    379  1.163    bouyer 		d_link->sc_flags &= ~WDF_WANTED;
    380  1.163    bouyer 		wakeup(d_link);
    381  1.132   mycroft 	}
    382  1.132   mycroft }
    383  1.132   mycroft 
    384    1.1       cgd int
    385  1.149  christos wdopen(dev, flag, fmt, p)
    386   1.55   mycroft 	dev_t dev;
    387   1.93   mycroft 	int flag, fmt;
    388  1.149  christos 	struct proc *p;
    389    1.1       cgd {
    390  1.135   mycroft 	struct wd_softc *wd;
    391  1.163    bouyer 	struct wd_link *d_link;
    392  1.135   mycroft 	int unit, part;
    393  1.109   mycroft 	int error;
    394  1.163    bouyer 
    395  1.163    bouyer 	WDDEBUG_PRINT(("wdopen\n"));
    396  1.163    bouyer 
    397  1.108   mycroft 	unit = WDUNIT(dev);
    398  1.147   thorpej 	if (unit >= wd_cd.cd_ndevs)
    399   1.37   mycroft 		return ENXIO;
    400  1.147   thorpej 	wd = wd_cd.cd_devs[unit];
    401  1.158        pk 	if (wd == NULL)
    402   1.37   mycroft 		return ENXIO;
    403    1.3   deraadt 
    404  1.163    bouyer 	d_link = wd->d_link;
    405  1.163    bouyer 	if ((error = wdlock(d_link)) != 0)
    406  1.132   mycroft 		return error;
    407    1.3   deraadt 
    408  1.109   mycroft 	if (wd->sc_dk.dk_openmask != 0) {
    409  1.109   mycroft 		/*
    410  1.109   mycroft 		 * If any partition is open, but the disk has been invalidated,
    411  1.109   mycroft 		 * disallow further opens.
    412  1.109   mycroft 		 */
    413  1.163    bouyer 		if ((d_link->sc_flags & WDF_LOADED) == 0) {
    414  1.136   mycroft 			error = EIO;
    415  1.136   mycroft 			goto bad3;
    416  1.136   mycroft 		}
    417  1.109   mycroft 	} else {
    418  1.163    bouyer 		if ((d_link->sc_flags & WDF_LOADED) == 0) {
    419  1.163    bouyer 			d_link->sc_flags |= WDF_LOADED;
    420  1.108   mycroft 
    421  1.108   mycroft 			/* Load the physical device parameters. */
    422  1.163    bouyer 			if (wdc_get_parms((struct wdc_softc *)d_link->wdc_softc,
    423  1.163    bouyer 				d_link) != 0) {
    424  1.108   mycroft 				error = ENXIO;
    425  1.108   mycroft 				goto bad2;
    426  1.108   mycroft 			}
    427  1.108   mycroft 
    428  1.108   mycroft 			/* Load the partition info if not already loaded. */
    429  1.108   mycroft 			wdgetdisklabel(wd);
    430  1.108   mycroft 		}
    431  1.135   mycroft 	}
    432  1.108   mycroft 
    433  1.135   mycroft 	part = WDPART(dev);
    434  1.108   mycroft 
    435  1.109   mycroft 	/* Check that the partition exists. */
    436   1.93   mycroft 	if (part != RAW_PART &&
    437  1.144   thorpej 	    (part >= wd->sc_dk.dk_label->d_npartitions ||
    438  1.144   thorpej 	     wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
    439   1.93   mycroft 		error = ENXIO;
    440   1.93   mycroft 		goto bad;
    441   1.93   mycroft 	}
    442    1.3   deraadt 
    443   1.64   mycroft 	/* Insure only one open at a time. */
    444    1.3   deraadt 	switch (fmt) {
    445    1.3   deraadt 	case S_IFCHR:
    446   1.94   mycroft 		wd->sc_dk.dk_copenmask |= (1 << part);
    447    1.3   deraadt 		break;
    448    1.3   deraadt 	case S_IFBLK:
    449   1.94   mycroft 		wd->sc_dk.dk_bopenmask |= (1 << part);
    450    1.3   deraadt 		break;
    451    1.3   deraadt 	}
    452   1.94   mycroft 	wd->sc_dk.dk_openmask = wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
    453   1.60   mycroft 
    454  1.163    bouyer 	wdunlock(d_link);
    455   1.37   mycroft 	return 0;
    456   1.93   mycroft 
    457  1.108   mycroft bad2:
    458  1.163    bouyer 	d_link->sc_flags &= ~WDF_LOADED;
    459  1.108   mycroft 
    460   1.93   mycroft bad:
    461  1.118   mycroft 	if (wd->sc_dk.dk_openmask == 0) {
    462  1.108   mycroft 	}
    463  1.108   mycroft 
    464  1.136   mycroft bad3:
    465  1.163    bouyer 	wdunlock(d_link);
    466   1.93   mycroft 	return error;
    467    1.1       cgd }
    468    1.1       cgd 
    469  1.135   mycroft int
    470  1.149  christos wdclose(dev, flag, fmt, p)
    471  1.135   mycroft 	dev_t dev;
    472  1.135   mycroft 	int flag, fmt;
    473  1.149  christos 	struct proc *p;
    474  1.135   mycroft {
    475  1.147   thorpej 	struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(dev)];
    476  1.135   mycroft 	int part = WDPART(dev);
    477  1.135   mycroft 	int error;
    478  1.135   mycroft 
    479  1.163    bouyer 	if ((error = wdlock(wd->d_link)) != 0)
    480  1.135   mycroft 		return error;
    481  1.135   mycroft 
    482  1.135   mycroft 	switch (fmt) {
    483  1.135   mycroft 	case S_IFCHR:
    484  1.135   mycroft 		wd->sc_dk.dk_copenmask &= ~(1 << part);
    485  1.135   mycroft 		break;
    486  1.135   mycroft 	case S_IFBLK:
    487  1.135   mycroft 		wd->sc_dk.dk_bopenmask &= ~(1 << part);
    488  1.135   mycroft 		break;
    489  1.135   mycroft 	}
    490  1.135   mycroft 	wd->sc_dk.dk_openmask = wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
    491  1.135   mycroft 
    492  1.135   mycroft 	if (wd->sc_dk.dk_openmask == 0) {
    493  1.135   mycroft 		/* XXXX Must wait for I/O to complete! */
    494  1.135   mycroft 	}
    495  1.135   mycroft 
    496  1.163    bouyer 	wdunlock(wd->d_link);
    497  1.135   mycroft 	return 0;
    498  1.135   mycroft }
    499  1.135   mycroft 
    500  1.108   mycroft void
    501  1.164   thorpej wdgetdefaultlabel(wd, lp)
    502  1.108   mycroft 	struct wd_softc *wd;
    503  1.164   thorpej 	struct disklabel *lp;
    504  1.108   mycroft {
    505  1.163    bouyer 	struct wd_link *d_link = wd->d_link;
    506  1.163    bouyer 
    507  1.142   mycroft 	bzero(lp, sizeof(struct disklabel));
    508  1.108   mycroft 
    509  1.142   mycroft 	lp->d_secsize = DEV_BSIZE;
    510  1.163    bouyer 	lp->d_ntracks = d_link->sc_params.wdp_heads;
    511  1.163    bouyer 	lp->d_nsectors = d_link->sc_params.wdp_sectors;
    512  1.163    bouyer 	lp->d_ncylinders = d_link->sc_params.wdp_cylinders;
    513  1.142   mycroft 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    514  1.108   mycroft 
    515  1.108   mycroft #if 0
    516  1.142   mycroft 	strncpy(lp->d_typename, "ST506 disk", 16);
    517  1.142   mycroft 	lp->d_type = DTYPE_ST506;
    518  1.108   mycroft #endif
    519  1.163    bouyer 	strncpy(lp->d_packname, d_link->sc_params.wdp_model, 16);
    520  1.142   mycroft 	lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
    521  1.142   mycroft 	lp->d_rpm = 3600;
    522  1.142   mycroft 	lp->d_interleave = 1;
    523  1.142   mycroft 	lp->d_flags = 0;
    524  1.142   mycroft 
    525  1.142   mycroft 	lp->d_partitions[RAW_PART].p_offset = 0;
    526  1.142   mycroft 	lp->d_partitions[RAW_PART].p_size =
    527  1.142   mycroft 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
    528  1.142   mycroft 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
    529  1.142   mycroft 	lp->d_npartitions = RAW_PART + 1;
    530  1.142   mycroft 
    531  1.142   mycroft 	lp->d_magic = DISKMAGIC;
    532  1.142   mycroft 	lp->d_magic2 = DISKMAGIC;
    533  1.142   mycroft 	lp->d_checksum = dkcksum(lp);
    534  1.164   thorpej }
    535  1.164   thorpej 
    536  1.164   thorpej /*
    537  1.164   thorpej  * Fabricate a default disk label, and try to read the correct one.
    538  1.164   thorpej  */
    539  1.164   thorpej void
    540  1.164   thorpej wdgetdisklabel(wd)
    541  1.164   thorpej 	struct wd_softc *wd;
    542  1.164   thorpej {
    543  1.164   thorpej 	struct disklabel *lp = wd->sc_dk.dk_label;
    544  1.164   thorpej 	struct wd_link *d_link = wd->d_link;
    545  1.164   thorpej 	char *errstring;
    546  1.164   thorpej 
    547  1.164   thorpej 	WDDEBUG_PRINT(("wdgetdisklabel\n"));
    548  1.164   thorpej 
    549  1.164   thorpej 	bzero(wd->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
    550  1.164   thorpej 
    551  1.164   thorpej 	wdgetdefaultlabel(wd, lp);
    552  1.108   mycroft 
    553  1.163    bouyer 	d_link->sc_badsect[0] = -1;
    554  1.113   mycroft 
    555  1.163    bouyer 	if (d_link->sc_state > RECAL)
    556  1.163    bouyer 		d_link->sc_state = RECAL;
    557  1.108   mycroft 	errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
    558  1.144   thorpej 	    wdstrategy, lp, wd->sc_dk.dk_cpulabel);
    559  1.108   mycroft 	if (errstring) {
    560  1.108   mycroft 		/*
    561  1.108   mycroft 		 * This probably happened because the drive's default
    562  1.108   mycroft 		 * geometry doesn't match the DOS geometry.  We
    563  1.108   mycroft 		 * assume the DOS geometry is now in the label and try
    564  1.108   mycroft 		 * again.  XXX This is a kluge.
    565  1.108   mycroft 		 */
    566  1.163    bouyer 		if (d_link->sc_state > GEOMETRY)
    567  1.163    bouyer 			d_link->sc_state = GEOMETRY;
    568  1.108   mycroft 		errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
    569  1.144   thorpej 		    wdstrategy, lp, wd->sc_dk.dk_cpulabel);
    570  1.108   mycroft 	}
    571  1.108   mycroft 	if (errstring) {
    572  1.153  christos 		printf("%s: %s\n", wd->sc_dev.dv_xname, errstring);
    573  1.108   mycroft 		return;
    574  1.108   mycroft 	}
    575  1.108   mycroft 
    576  1.163    bouyer 	if (d_link->sc_state > GEOMETRY)
    577  1.163    bouyer 		d_link->sc_state = GEOMETRY;
    578  1.142   mycroft 	if ((lp->d_flags & D_BADSECT) != 0)
    579  1.108   mycroft 		bad144intern(wd);
    580  1.108   mycroft }
    581  1.108   mycroft 
    582    1.1       cgd 
    583    1.1       cgd /*
    584  1.135   mycroft  * Tell the drive what geometry to use.
    585    1.1       cgd  */
    586  1.135   mycroft int
    587  1.163    bouyer wdsetctlr(d_link)
    588  1.163    bouyer 	struct wd_link *d_link;
    589    1.3   deraadt {
    590  1.163    bouyer 	struct wd_softc *wd=(struct wd_softc *)d_link->wd_softc;
    591   1.63   mycroft 
    592  1.163    bouyer 	WDDEBUG_PRINT(("wd(%d,%d) C%dH%dS%d\n", wd->sc_dev.dv_unit,
    593  1.163    bouyer 	    d_link->drive, wd->sc_dk.dk_label->d_ncylinders,
    594  1.163    bouyer 	    wd->sc_dk.dk_label->d_ntracks, wd->sc_dk.dk_label->d_nsectors));
    595  1.163    bouyer 
    596  1.163    bouyer 	if (wdccommand((struct wdc_softc *)d_link->wdc_softc,
    597  1.163    bouyer 		d_link, WDCC_IDP, d_link->drive,
    598  1.163    bouyer 	    wd->sc_dk.dk_label->d_ncylinders,
    599  1.144   thorpej 	    wd->sc_dk.dk_label->d_ntracks - 1, 0,
    600  1.144   thorpej 	    wd->sc_dk.dk_label->d_nsectors) != 0) {
    601  1.163    bouyer 		wderror(d_link, NULL, "wdsetctlr: geometry upload failed");
    602   1.51   mycroft 		return -1;
    603   1.57   mycroft 	}
    604   1.69   mycroft 
    605   1.60   mycroft 	return 0;
    606    1.1       cgd }
    607    1.1       cgd 
    608    1.1       cgd int
    609  1.163    bouyer wdioctl(dev, xfer, addr, flag, p)
    610   1.55   mycroft 	dev_t dev;
    611  1.163    bouyer 	u_long xfer;
    612   1.55   mycroft 	caddr_t addr;
    613   1.55   mycroft 	int flag;
    614   1.55   mycroft 	struct proc *p;
    615    1.1       cgd {
    616  1.147   thorpej 	struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(dev)];
    617  1.163    bouyer 	struct wd_link *d_link = wd->d_link;
    618   1.54   mycroft 	int error;
    619  1.163    bouyer 
    620  1.163    bouyer 	WDDEBUG_PRINT(("wdioctl\n"));
    621  1.163    bouyer 
    622  1.163    bouyer 	if ((d_link->sc_flags & WDF_LOADED) == 0)
    623  1.108   mycroft 		return EIO;
    624  1.108   mycroft 
    625  1.163    bouyer 	switch (xfer) {
    626    1.1       cgd 	case DIOCSBAD:
    627    1.3   deraadt 		if ((flag & FWRITE) == 0)
    628   1.54   mycroft 			return EBADF;
    629  1.144   thorpej 		wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
    630  1.144   thorpej 		wd->sc_dk.dk_label->d_flags |= D_BADSECT;
    631   1.64   mycroft 		bad144intern(wd);
    632   1.54   mycroft 		return 0;
    633    1.1       cgd 
    634    1.1       cgd 	case DIOCGDINFO:
    635  1.144   thorpej 		*(struct disklabel *)addr = *(wd->sc_dk.dk_label);
    636   1.54   mycroft 		return 0;
    637    1.3   deraadt 
    638    1.3   deraadt 	case DIOCGPART:
    639  1.144   thorpej 		((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
    640    1.3   deraadt 		((struct partinfo *)addr)->part =
    641  1.144   thorpej 		    &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
    642   1.54   mycroft 		return 0;
    643    1.3   deraadt 
    644  1.132   mycroft 	case DIOCWDINFO:
    645    1.3   deraadt 	case DIOCSDINFO:
    646    1.3   deraadt 		if ((flag & FWRITE) == 0)
    647   1.54   mycroft 			return EBADF;
    648  1.132   mycroft 
    649  1.163    bouyer 		if ((error = wdlock(wd->d_link)) != 0)
    650  1.132   mycroft 			return error;
    651  1.163    bouyer 		d_link->sc_flags |= WDF_LABELLING;
    652  1.132   mycroft 
    653  1.144   thorpej 		error = setdisklabel(wd->sc_dk.dk_label,
    654  1.128   mycroft 		    (struct disklabel *)addr, /*wd->sc_dk.dk_openmask : */0,
    655  1.144   thorpej 		    wd->sc_dk.dk_cpulabel);
    656    1.3   deraadt 		if (error == 0) {
    657  1.163    bouyer 			if (d_link->sc_state > GEOMETRY)
    658  1.163    bouyer 				d_link->sc_state = GEOMETRY;
    659  1.163    bouyer 			if (xfer == DIOCWDINFO)
    660  1.132   mycroft 				error = writedisklabel(WDLABELDEV(dev),
    661  1.144   thorpej 				    wdstrategy, wd->sc_dk.dk_label,
    662  1.144   thorpej 				    wd->sc_dk.dk_cpulabel);
    663    1.1       cgd 		}
    664  1.132   mycroft 
    665  1.163    bouyer 		d_link->sc_flags &= ~WDF_LABELLING;
    666  1.163    bouyer 		wdunlock(d_link);
    667   1.54   mycroft 		return error;
    668    1.3   deraadt 
    669   1.44   mycroft 	case DIOCWLABEL:
    670    1.3   deraadt 		if ((flag & FWRITE) == 0)
    671   1.54   mycroft 			return EBADF;
    672   1.93   mycroft 		if (*(int *)addr)
    673  1.163    bouyer 			d_link->sc_flags |= WDF_WLABEL;
    674   1.93   mycroft 		else
    675  1.163    bouyer 			d_link->sc_flags &= ~WDF_WLABEL;
    676   1.54   mycroft 		return 0;
    677  1.164   thorpej 
    678  1.164   thorpej 	case DIOCGDEFLABEL:
    679  1.164   thorpej 		wdgetdefaultlabel(wd, (struct disklabel *)addr);
    680  1.164   thorpej 		return 0;
    681  1.164   thorpej 
    682    1.1       cgd #ifdef notyet
    683    1.1       cgd 	case DIOCWFORMAT:
    684    1.1       cgd 		if ((flag & FWRITE) == 0)
    685   1.54   mycroft 			return EBADF;
    686   1.54   mycroft 	{
    687   1.54   mycroft 		register struct format_op *fop;
    688   1.54   mycroft 		struct iovec aiov;
    689   1.54   mycroft 		struct uio auio;
    690    1.3   deraadt 
    691   1.54   mycroft 		fop = (struct format_op *)addr;
    692   1.54   mycroft 		aiov.iov_base = fop->df_buf;
    693   1.54   mycroft 		aiov.iov_len = fop->df_count;
    694   1.54   mycroft 		auio.uio_iov = &aiov;
    695   1.54   mycroft 		auio.uio_iovcnt = 1;
    696   1.54   mycroft 		auio.uio_resid = fop->df_count;
    697   1.54   mycroft 		auio.uio_segflg = 0;
    698   1.54   mycroft 		auio.uio_offset =
    699  1.144   thorpej 		    fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
    700   1.83        pk 		auio.uio_procp = p;
    701   1.64   mycroft 		error = physio(wdformat, NULL, dev, B_WRITE, minphys,
    702   1.64   mycroft 		    &auio);
    703   1.54   mycroft 		fop->df_count -= auio.uio_resid;
    704   1.64   mycroft 		fop->df_reg[0] = wdc->sc_status;
    705   1.64   mycroft 		fop->df_reg[1] = wdc->sc_error;
    706   1.54   mycroft 		return error;
    707   1.54   mycroft 	}
    708    1.1       cgd #endif
    709  1.163    bouyer 
    710    1.1       cgd 	default:
    711   1.54   mycroft 		return ENOTTY;
    712    1.1       cgd 	}
    713   1.54   mycroft 
    714   1.54   mycroft #ifdef DIAGNOSTIC
    715   1.54   mycroft 	panic("wdioctl: impossible");
    716   1.54   mycroft #endif
    717    1.1       cgd }
    718    1.1       cgd 
    719   1.44   mycroft #ifdef B_FORMAT
    720    1.1       cgd int
    721    1.1       cgd wdformat(struct buf *bp)
    722    1.1       cgd {
    723   1.44   mycroft 
    724    1.1       cgd 	bp->b_flags |= B_FORMAT;
    725   1.37   mycroft 	return wdstrategy(bp);
    726    1.1       cgd }
    727    1.1       cgd #endif
    728    1.1       cgd 
    729    1.1       cgd int
    730   1.55   mycroft wdsize(dev)
    731   1.55   mycroft 	dev_t dev;
    732    1.1       cgd {
    733   1.64   mycroft 	struct wd_softc *wd;
    734  1.158        pk 	int part, unit, omask;
    735  1.108   mycroft 	int size;
    736  1.163    bouyer 
    737  1.163    bouyer 	WDDEBUG_PRINT(("wdsize\n"));
    738  1.163    bouyer 
    739  1.158        pk 	unit = WDUNIT(dev);
    740  1.158        pk 	if (unit >= wd_cd.cd_ndevs)
    741  1.158        pk 		return (-1);
    742  1.158        pk 	wd = wd_cd.cd_devs[unit];
    743  1.158        pk 	if (wd == NULL)
    744  1.158        pk 		return (-1);
    745  1.158        pk 
    746  1.158        pk 	part = WDPART(dev);
    747  1.158        pk 	omask = wd->sc_dk.dk_openmask & (1 << part);
    748  1.158        pk 
    749  1.158        pk 	if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0)
    750  1.158        pk 		return (-1);
    751  1.144   thorpej 	if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
    752  1.108   mycroft 		size = -1;
    753  1.108   mycroft 	else
    754  1.160   thorpej 		size = wd->sc_dk.dk_label->d_partitions[part].p_size *
    755  1.163    bouyer 			(wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
    756  1.158        pk 	if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0)
    757  1.158        pk 		return (-1);
    758  1.158        pk 	return (size);
    759    1.1       cgd }
    760    1.1       cgd 
    761  1.140       cgd 
    762  1.140       cgd #ifndef __BDEVSW_DUMP_OLD_TYPE
    763  1.140       cgd /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
    764  1.140       cgd static int wddoingadump;
    765  1.140       cgd static int wddumprecalibrated;
    766  1.140       cgd 
    767   1.64   mycroft /*
    768   1.64   mycroft  * Dump core after a system crash.
    769  1.163    bouyer  *
    770  1.163    bouyer  * XXX:  This needs work!  Currently, it's a major hack: the
    771  1.163    bouyer  * use of wdc_softc is very bad and should go away.
    772   1.64   mycroft  */
    773    1.1       cgd int
    774  1.140       cgd wddump(dev, blkno, va, size)
    775  1.140       cgd         dev_t dev;
    776  1.140       cgd         daddr_t blkno;
    777  1.140       cgd         caddr_t va;
    778  1.140       cgd         size_t size;
    779  1.140       cgd {
    780  1.140       cgd 	struct wd_softc *wd;	/* disk unit to do the I/O */
    781  1.140       cgd 	struct wdc_softc *wdc;	/* disk controller to do the I/O */
    782  1.140       cgd 	struct disklabel *lp;	/* disk's disklabel */
    783  1.163    bouyer 	struct wd_link *d_link;
    784  1.140       cgd 	int	unit, part;
    785  1.142   mycroft 	int	nblks;		/* total number of sectors left to write */
    786  1.140       cgd 
    787  1.140       cgd 	/* Check if recursive dump; if so, punt. */
    788  1.116   mycroft 	if (wddoingadump)
    789  1.116   mycroft 		return EFAULT;
    790  1.142   mycroft 	wddoingadump = 1;
    791  1.140       cgd 
    792  1.142   mycroft 	unit = WDUNIT(dev);
    793  1.147   thorpej 	if (unit >= wd_cd.cd_ndevs)
    794  1.142   mycroft 		return ENXIO;
    795  1.147   thorpej 	wd = wd_cd.cd_devs[unit];
    796  1.163    bouyer 	if (wd == (struct wd_softc *)0)
    797  1.142   mycroft 		return ENXIO;
    798  1.163    bouyer 	d_link = wd->d_link;
    799   1.60   mycroft 
    800  1.140       cgd 	part = WDPART(dev);
    801  1.140       cgd 
    802  1.140       cgd 	/* Make sure it was initialized. */
    803  1.163    bouyer 	if (d_link->sc_state < READY)
    804   1.37   mycroft 		return ENXIO;
    805  1.140       cgd 
    806  1.142   mycroft 	wdc = (void *)wd->sc_dev.dv_parent;
    807   1.46   mycroft 
    808  1.140       cgd         /* Convert to disk sectors.  Request must be a multiple of size. */
    809  1.144   thorpej 	lp = wd->sc_dk.dk_label;
    810  1.142   mycroft 	if ((size % lp->d_secsize) != 0)
    811  1.140       cgd 		return EFAULT;
    812  1.142   mycroft 	nblks = size / lp->d_secsize;
    813  1.142   mycroft 	blkno = blkno / (lp->d_secsize / DEV_BSIZE);
    814  1.116   mycroft 
    815   1.64   mycroft 	/* Check transfer bounds against partition size. */
    816  1.142   mycroft 	if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
    817  1.140       cgd 		return EINVAL;
    818  1.140       cgd 
    819  1.140       cgd 	/* Offset block number to start of partition. */
    820  1.142   mycroft 	blkno += lp->d_partitions[part].p_offset;
    821   1.60   mycroft 
    822  1.140       cgd 	/* Recalibrate, if first dump transfer. */
    823  1.140       cgd 	if (wddumprecalibrated == 0) {
    824  1.140       cgd 		wddumprecalibrated = 1;
    825  1.163    bouyer 		if (wdccommandshort(wdc, d_link->drive, WDCC_RECAL) != 0 ||
    826  1.163    bouyer 		    wait_for_ready(wdc) != 0 || wdsetctlr(d_link) != 0 ||
    827  1.140       cgd 		    wait_for_ready(wdc) != 0) {
    828  1.163    bouyer 			wderror(d_link, NULL, "wddump: recal failed");
    829  1.140       cgd 			return EIO;
    830  1.140       cgd 		}
    831   1.63   mycroft 	}
    832  1.140       cgd 
    833  1.142   mycroft 	while (nblks > 0) {
    834  1.142   mycroft 		daddr_t xlt_blkno = blkno;
    835  1.116   mycroft 		long cylin, head, sector;
    836  1.116   mycroft 
    837  1.116   mycroft 		if ((lp->d_flags & D_BADSECT) != 0) {
    838  1.116   mycroft 			long blkdiff;
    839    1.3   deraadt 			int i;
    840    1.3   deraadt 
    841  1.163    bouyer 			for (i = 0; (blkdiff = d_link->sc_badsect[i]) != -1; i++) {
    842  1.140       cgd 				blkdiff -= xlt_blkno;
    843  1.116   mycroft 				if (blkdiff < 0)
    844  1.116   mycroft 					continue;
    845  1.116   mycroft 				if (blkdiff == 0) {
    846  1.116   mycroft 					/* Replace current block of transfer. */
    847  1.140       cgd 					xlt_blkno = lp->d_secperunit -
    848  1.140       cgd 					    lp->d_nsectors - i - 1;
    849    1.3   deraadt 				}
    850  1.116   mycroft 				break;
    851    1.1       cgd 			}
    852  1.116   mycroft 			/* Tranfer is okay now. */
    853  1.116   mycroft 		}
    854  1.116   mycroft 
    855  1.163    bouyer 		if ((d_link->sc_params.wdp_capabilities & WD_CAP_LBA) != 0) {
    856  1.140       cgd 			sector = (xlt_blkno >> 0) & 0xff;
    857  1.140       cgd 			cylin = (xlt_blkno >> 8) & 0xffff;
    858  1.140       cgd 			head = (xlt_blkno >> 24) & 0xf;
    859  1.116   mycroft 			head |= WDSD_LBA;
    860  1.116   mycroft 		} else {
    861  1.140       cgd 			sector = xlt_blkno % lp->d_nsectors;
    862  1.116   mycroft 			sector++;	/* Sectors begin with 1, not 0. */
    863  1.140       cgd 			xlt_blkno /= lp->d_nsectors;
    864  1.140       cgd 			head = xlt_blkno % lp->d_ntracks;
    865  1.140       cgd 			xlt_blkno /= lp->d_ntracks;
    866  1.140       cgd 			cylin = xlt_blkno;
    867  1.116   mycroft 			head |= WDSD_CHS;
    868    1.3   deraadt 		}
    869  1.140       cgd 
    870  1.140       cgd #ifndef WD_DUMP_NOT_TRUSTED
    871  1.163    bouyer 		if (wdccommand((struct wdc_softc *)d_link->wdc_softc, d_link,
    872  1.163    bouyer 			WDCC_WRITE, d_link->drive, cylin, head, sector, 1) != 0 ||
    873  1.105   mycroft 		    wait_for_drq(wdc) != 0) {
    874  1.163    bouyer 			wderror(d_link, NULL, "wddump: write failed");
    875   1.51   mycroft 			return EIO;
    876   1.63   mycroft 		}
    877    1.3   deraadt 
    878  1.163    bouyer 		/* XXX XXX XXX */
    879  1.163    bouyer 		outsw(wdc->sc_iobase + wd_data, va, lp->d_secsize >> 1);
    880    1.3   deraadt 
    881   1.13   deraadt 		/* Check data request (should be done). */
    882   1.64   mycroft 		if (wait_for_ready(wdc) != 0) {
    883  1.163    bouyer 			wderror(d_link, NULL,
    884  1.163    bouyer 			    "wddump: timeout waiting for ready");
    885   1.63   mycroft 			return EIO;
    886   1.63   mycroft 		}
    887  1.140       cgd #else	/* WD_DUMP_NOT_TRUSTED */
    888  1.140       cgd 		/* Let's just talk about this first... */
    889  1.153  christos 		printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
    890  1.140       cgd 		    unit, va, cylin, head, sector);
    891  1.140       cgd 		delay(500 * 1000);	/* half a second */
    892  1.140       cgd #endif
    893    1.1       cgd 
    894  1.140       cgd 		/* update block count */
    895  1.142   mycroft 		nblks -= 1;
    896  1.142   mycroft 		blkno += 1;
    897  1.142   mycroft 		va += lp->d_secsize;
    898    1.1       cgd 	}
    899  1.142   mycroft 
    900  1.140       cgd 	wddoingadump = 0;
    901  1.140       cgd 	return 0;
    902  1.140       cgd }
    903  1.140       cgd #else /* __BDEVSW_DUMP_NEW_TYPE */
    904  1.163    bouyer 
    905  1.163    bouyer 
    906  1.140       cgd int
    907  1.140       cgd wddump(dev, blkno, va, size)
    908  1.140       cgd         dev_t dev;
    909  1.140       cgd         daddr_t blkno;
    910  1.140       cgd         caddr_t va;
    911  1.140       cgd         size_t size;
    912  1.140       cgd {
    913  1.116   mycroft 
    914  1.140       cgd 	/* Not implemented. */
    915  1.140       cgd 	return ENXIO;
    916    1.1       cgd }
    917  1.140       cgd #endif /* __BDEVSW_DUMP_NEW_TYPE */
    918    1.3   deraadt 
    919    1.3   deraadt /*
    920    1.3   deraadt  * Internalize the bad sector table.
    921    1.3   deraadt  */
    922    1.3   deraadt void
    923   1.64   mycroft bad144intern(wd)
    924   1.64   mycroft 	struct wd_softc *wd;
    925    1.3   deraadt {
    926  1.144   thorpej 	struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
    927  1.144   thorpej 	struct disklabel *lp = wd->sc_dk.dk_label;
    928  1.163    bouyer 	struct wd_link *d_link = wd->d_link;
    929   1.98   mycroft 	int i = 0;
    930   1.55   mycroft 
    931  1.163    bouyer 	WDDEBUG_PRINT(("bad144intern\n"));
    932  1.163    bouyer 
    933   1.98   mycroft 	for (; i < 126; i++) {
    934   1.98   mycroft 		if (bt->bt_bad[i].bt_cyl == 0xffff)
    935   1.55   mycroft 			break;
    936  1.163    bouyer 		d_link->sc_badsect[i] =
    937   1.98   mycroft 		    bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
    938   1.98   mycroft 		    (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
    939   1.98   mycroft 		    (bt->bt_bad[i].bt_trksec & 0xff);
    940    1.3   deraadt 	}
    941   1.98   mycroft 	for (; i < 127; i++)
    942  1.163    bouyer 		d_link->sc_badsect[i] = -1;
    943   1.64   mycroft }
    944   1.64   mycroft 
    945  1.135   mycroft void
    946  1.163    bouyer wderror(d_link, bp, msg)
    947  1.163    bouyer 	struct wd_link *d_link;
    948  1.163    bouyer 	struct buf *bp;
    949  1.163    bouyer 	char *msg;
    950   1.64   mycroft {
    951  1.163    bouyer 	struct wd_softc *wd = (struct wd_softc *)d_link->wd_softc;
    952   1.64   mycroft 
    953  1.163    bouyer 	if (bp) {
    954  1.163    bouyer 		diskerr(bp, "wd", msg, LOG_PRINTF, bp->b_bcount,
    955  1.163    bouyer 		    wd->sc_dk.dk_label);
    956  1.163    bouyer 		printf("\n");
    957  1.120   mycroft 	} else
    958  1.163    bouyer 		printf("%s: %s\n", wd->sc_dev.dv_xname, msg);
    959   1.63   mycroft }
    960   1.63   mycroft 
    961  1.135   mycroft void
    962  1.163    bouyer wddone(d_link, bp)
    963  1.163    bouyer 	struct wd_link *d_link;
    964   1.63   mycroft 	struct buf *bp;
    965   1.63   mycroft {
    966  1.163    bouyer 	struct wd_softc *wd = (void *)d_link->wd_softc;
    967   1.64   mycroft 
    968  1.163    bouyer 	disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid));
    969   1.21   deraadt }
    970