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