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