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