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