Home | History | Annotate | Line # | Download | only in ata
wd.c revision 1.175.2.8
      1 /*	$NetBSD: wd.c,v 1.175.2.8 1998/08/21 16:34:46 bouyer Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1998 Manuel Bouyer.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *	notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *	notice, this list of conditions and the following disclaimer in the
     13  *	documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *	must display the following acknowledgement:
     16  *  This product includes software developed by Manuel Bouyer.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *	derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 1998 The NetBSD Foundation, Inc.
     34  * All rights reserved.
     35  *
     36  * This code is derived from software contributed to The NetBSD Foundation
     37  * by Charles M. Hannum and by Onno van der Linden.
     38  *
     39  * Redistribution and use in source and binary forms, with or without
     40  * modification, are permitted provided that the following conditions
     41  * are met:
     42  * 1. Redistributions of source code must retain the above copyright
     43  *    notice, this list of conditions and the following disclaimer.
     44  * 2. Redistributions in binary form must reproduce the above copyright
     45  *    notice, this list of conditions and the following disclaimer in the
     46  *    documentation and/or other materials provided with the distribution.
     47  * 3. All advertising materials mentioning features or use of this software
     48  *    must display the following acknowledgement:
     49  *        This product includes software developed by the NetBSD
     50  *        Foundation, Inc. and its contributors.
     51  * 4. Neither the name of The NetBSD Foundation nor the names of its
     52  *    contributors may be used to endorse or promote products derived
     53  *    from this software without specific prior written permission.
     54  *
     55  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     56  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     57  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     58  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     59  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     60  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     61  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     62  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     63  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     64  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     65  * POSSIBILITY OF SUCH DAMAGE.
     66  */
     67 
     68 #define WDCDEBUG
     69 
     70 #include "rnd.h"
     71 
     72 #include <sys/param.h>
     73 #include <sys/systm.h>
     74 #include <sys/kernel.h>
     75 #include <sys/conf.h>
     76 #include <sys/file.h>
     77 #include <sys/stat.h>
     78 #include <sys/ioctl.h>
     79 #include <sys/buf.h>
     80 #include <sys/uio.h>
     81 #include <sys/malloc.h>
     82 #include <sys/device.h>
     83 #include <sys/disklabel.h>
     84 #include <sys/disk.h>
     85 #include <sys/syslog.h>
     86 #include <sys/proc.h>
     87 #if NRND > 0
     88 #include <sys/rnd.h>
     89 #endif
     90 
     91 #include <vm/vm.h>
     92 
     93 #include <machine/intr.h>
     94 #include <machine/bus.h>
     95 
     96 #include <dev/ata/atareg.h>
     97 #include <dev/ata/atavar.h>
     98 #include <dev/ata/wdvar.h>
     99 #include <dev/ic/wdcreg.h>
    100 #include "locators.h"
    101 
    102 #define	WAITTIME	(4 * hz)	/* time to wait for a completion */
    103 #define	WDIORETRIES	5	/* number of retries before giving up */
    104 #define	RECOVERYTIME hz/2	/* time to wait before retrying a cmd */
    105 
    106 #define	WDUNIT(dev)		DISKUNIT(dev)
    107 #define	WDPART(dev)		DISKPART(dev)
    108 #define	MAKEWDDEV(maj, unit, part)	MAKEDISKDEV(maj, unit, part)
    109 
    110 #define	WDLABELDEV(dev)	(MAKEWDDEV(major(dev), WDUNIT(dev), RAW_PART))
    111 
    112 #define DEBUG_INTR   0x01
    113 #define DEBUG_XFERS  0x02
    114 #define DEBUG_STATUS 0x04
    115 #define DEBUG_FUNCS  0x08
    116 #define DEBUG_PROBE  0x10
    117 #ifdef WDCDEBUG
    118 extern int wdcdebug_wd_mask; /* init'ed in ata_wdc.c */
    119 #define WDCDEBUG_PRINT(args, level) \
    120 	if (wdcdebug_wd_mask & (level)) \
    121 		printf args
    122 #else
    123 #define WDCDEBUG_PRINT(args, level)
    124 #endif
    125 
    126 struct wd_softc {
    127 	/* General disk infos */
    128 	struct device sc_dev;
    129 	struct disk sc_dk;
    130 	struct buf sc_q;
    131 	/* IDE disk soft states */
    132 	struct ata_bio sc_wdc_bio; /* current transfert */
    133 	struct buf *sc_bp; /* buf being transfered */
    134 	void *wdc_softc;   /* pointer to our parent */
    135 	struct ata_drive_datas *drvp; /* Our controller's infos */
    136 	int openings;
    137 	struct ataparams sc_params;/* drive characteistics found */
    138 	int sc_flags;
    139 #define WDF_LOCKED	  0x01
    140 #define WDF_WANTED	  0x02
    141 #define WDF_WLABEL	  0x04 /* label is writable */
    142 #define WDF_LABELLING   0x08 /* writing label */
    143 /*
    144  * XXX Nothing resets this yet, but disk change sensing will when ATA-4 is
    145  * more fully implemented.
    146  */
    147 #define WDF_LOADED	  0x10 /* parameters loaded */
    148 #define WDF_WAIT	0x20 /* waiting for resources */
    149 #define WDF_LBA	 0x40 /* using LBA mode */
    150 	int sc_capacity;
    151 	int cyl; /* actual drive parameters */
    152 	int heads;
    153 	int sectors;
    154 	int retries; /* number of xfer retry */
    155 #if NRND > 0
    156 	rndsource_element_t	rnd_source;
    157 #endif
    158 };
    159 
    160 #define sc_drive sc_wdc_bio.drive
    161 #define sc_mode sc_wdc_bio.mode
    162 #define sc_multi sc_wdc_bio.multi
    163 #define sc_badsect sc_wdc_bio.badsect
    164 
    165 int	wdprobe		__P((struct device *, struct cfdata *, void *));
    166 void	wdattach	__P((struct device *, struct device *, void *));
    167 int	wdprint	__P((void *, char *));
    168 
    169 struct cfattach wd_ca = {
    170 	sizeof(struct wd_softc), wdprobe, wdattach
    171 };
    172 
    173 extern struct cfdriver wd_cd;
    174 
    175 void  wdgetdefaultlabel __P((struct wd_softc *, struct disklabel *));
    176 void  wdgetdisklabel	__P((struct wd_softc *));
    177 void  wdstrategy	__P((struct buf *));
    178 void  wdstart	__P((void *));
    179 void  __wdstart	__P((struct wd_softc*, struct buf *));
    180 void  wdrestart __P((void*));
    181 int   wd_get_params __P((struct wd_softc *, u_int8_t, struct ataparams *));
    182 
    183 struct dkdriver wddkdriver = { wdstrategy };
    184 
    185 /* XXX: these should go elsewhere */
    186 cdev_decl(wd);
    187 bdev_decl(wd);
    188 
    189 #ifdef HAS_BAD144_HANDLING
    190 static void bad144intern __P((struct wd_softc *));
    191 #endif
    192 int	wdlock	__P((struct wd_softc *));
    193 void	wdunlock	__P((struct wd_softc *));
    194 void print_wderror __P((int, char*));
    195 
    196 int
    197 wdprobe(parent, match, aux)
    198 	struct device *parent;
    199 	struct cfdata *match;
    200 
    201 	void *aux;
    202 {
    203 	struct ata_atapi_attach *aa_link = aux;
    204 
    205 	if (aa_link == NULL)
    206 		return 0;
    207 	if (aa_link->aa_type != T_ATA)
    208 		return 0;
    209 
    210 	if (match->cf_loc[ATACF_CHANNEL] != ATACF_CHANNEL_DEFAULT &&
    211 	    match->cf_loc[ATACF_CHANNEL] != aa_link->aa_channel)
    212 		return 0;
    213 
    214 	if (match->cf_loc[ATACF_DRIVE] != ATACF_DRIVE_DEFAULT &&
    215 	    match->cf_loc[ATACF_DRIVE] != aa_link->aa_drv_data->drive)
    216 		return 0;
    217 
    218 	return 1;
    219 }
    220 
    221 void
    222 wdattach(parent, self, aux)
    223 	struct device *parent, *self;
    224 	void *aux;
    225 {
    226 	struct wd_softc *wd = (void *)self;
    227 	struct ata_atapi_attach *aa_link= aux;
    228 	int i, blank;
    229 	char buf[41], c, *p, *q;
    230 	WDCDEBUG_PRINT(("wdattach\n"), DEBUG_FUNCS | DEBUG_PROBE);
    231 
    232 	wd->openings = aa_link->aa_openings;
    233 	wd->drvp = aa_link->aa_drv_data;;
    234 	wd->wdc_softc = parent;
    235 	/* give back our softc to our caller */
    236 	wd->drvp->drv_softc = wd;
    237 
    238 	/* read our drive info */
    239 	wd_get_params(wd, AT_POLL, &wd->sc_params);
    240 
    241 	for (blank = 0, p = wd->sc_params.atap_model, q = buf, i = 0;
    242 	    i < sizeof(wd->sc_params.atap_model); i++) {
    243 		c = *p++;
    244 		if (c == '\0')
    245 			break;
    246 		if (c != ' ') {
    247 			if (blank) {
    248 				*q++ = ' ';
    249 				blank = 0;
    250 			}
    251 			*q++ = c;
    252 		} else
    253 			blank = 1;
    254 		}
    255 	*q++ = '\0';
    256 
    257 	printf(": <%s>\n", buf);
    258 
    259 	if ((wd->sc_params.atap_multi & 0xff) > 1) {
    260 		wd->sc_multi = wd->sc_params.atap_multi & 0xff;
    261 	} else {
    262 		wd->sc_multi = 1;
    263 	}
    264 
    265 	printf("%s: using %d-sector pio transfers,", wd->sc_dev.dv_xname,
    266 	    wd->sc_multi);
    267 
    268 	/* Prior to ATA-4, LBA was optional. */
    269 	if ((wd->sc_params.atap_capabilities1 & WDC_CAP_LBA) != 0)
    270 		wd->sc_flags |= WDF_LBA;
    271 #if 0
    272 	/* ATA-4 requires LBA. */
    273 	if (wd->sc_params.atap_ataversion != 0xffff &&
    274 	    wd->sc_params.atap_ataversion >= WDC_VER_ATA4)
    275 		wd->sc_flags |= WDF_LBA;
    276 #endif
    277 
    278 	if ((wd->sc_flags & WDF_LBA) != 0) {
    279 		printf(" lba mode\n");
    280 		wd->sc_capacity =
    281 		    (wd->sc_params.atap_capacity[1] << 16) |
    282 		    wd->sc_params.atap_capacity[0];
    283 		printf("%s %dMB, %d cyl, %d head, %d sec, %d bytes/sect x %d sectors\n",
    284 		    self->dv_xname,
    285 		    wd->sc_capacity / (1048576 / DEV_BSIZE),
    286 		    wd->sc_params.atap_cylinders,
    287 		    wd->sc_params.atap_heads,
    288 		    wd->sc_params.atap_sectors,
    289 		    DEV_BSIZE,
    290 		    wd->sc_capacity);
    291 	} else {
    292 		printf(" chs mode\n");
    293 		wd->sc_capacity =
    294 		    wd->sc_params.atap_cylinders *
    295 		    wd->sc_params.atap_heads *
    296 		    wd->sc_params.atap_sectors;
    297 		printf("%s %dMB, %d cyl, %d head, %d sec, %d bytes/sect x %d "
    298 		    "sectors\n", self->dv_xname,
    299 		    wd->sc_capacity / (1048576 / DEV_BSIZE),
    300 		    wd->sc_params.atap_cylinders,
    301 		    wd->sc_params.atap_heads,
    302 		    wd->sc_params.atap_sectors,
    303 		    DEV_BSIZE,
    304 		    wd->sc_capacity);
    305 	}
    306 	WDCDEBUG_PRINT(("atap_dmatiming_mimi=%d, atap_dmatiming_recom=%d\n",
    307 	    wd->sc_params.atap_dmatiming_mimi,
    308 	    wd->sc_params.atap_dmatiming_recom), DEBUG_PROBE);
    309 	/*
    310 	 * Initialize and attach the disk structure.
    311 	 */
    312 	wd->sc_dk.dk_driver = &wddkdriver;
    313 	wd->sc_dk.dk_name = wd->sc_dev.dv_xname;
    314 	disk_attach(&wd->sc_dk);
    315 	wd->sc_wdc_bio.lp = wd->sc_dk.dk_label;
    316 
    317 #if NRND > 0
    318 	rnd_attach_source(&wd->rnd_source, wd->sc_dev.dv_xname, RND_TYPE_DISK);
    319 #endif
    320 }
    321 
    322 /*
    323  * Read/write routine for a buffer.  Validates the arguments and schedules the
    324  * transfer.  Does not wait for the transfer to complete.
    325  */
    326 void
    327 wdstrategy(bp)
    328 	struct buf *bp;
    329 {
    330 	struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(bp->b_dev)];
    331 	int s;
    332 	WDCDEBUG_PRINT(("wdstrategy\n"), DEBUG_FUNCS | DEBUG_XFERS);
    333 
    334 	/* Valid request?  */
    335 	if (bp->b_blkno < 0 ||
    336 	    (bp->b_bcount % wd->sc_dk.dk_label->d_secsize) != 0 ||
    337 	    (bp->b_bcount / wd->sc_dk.dk_label->d_secsize) >= (1 << NBBY)) {
    338 		bp->b_error = EINVAL;
    339 		goto bad;
    340 	}
    341 
    342 	/* If device invalidated (e.g. media change, door open), error. */
    343 	if ((wd->sc_flags & WDF_LOADED) == 0) {
    344 		bp->b_error = EIO;
    345 		goto bad;
    346 	}
    347 
    348 	/* If it's a null transfer, return immediately. */
    349 	if (bp->b_bcount == 0)
    350 		goto done;
    351 
    352 	/*
    353 	 * Do bounds checking, adjust transfer. if error, process.
    354 	 * If end of partition, just return.
    355 	 */
    356 	if (WDPART(bp->b_dev) != RAW_PART &&
    357 	    bounds_check_with_label(bp, wd->sc_dk.dk_label,
    358 	    (wd->sc_flags & (WDF_WLABEL|WDF_LABELLING)) != 0) <= 0)
    359 		goto done;
    360 	/* Queue transfer on drive, activate drive and controller if idle. */
    361 	s = splbio();
    362 	disksort(&wd->sc_q, bp);
    363 	wdstart(wd);
    364 	splx(s);
    365 	return;
    366 bad:
    367 	bp->b_flags |= B_ERROR;
    368 done:
    369 	/* Toss transfer; we're done early. */
    370 	bp->b_resid = bp->b_bcount;
    371 	biodone(bp);
    372 }
    373 
    374 /*
    375  * Queue a drive for I/O.
    376  */
    377 void
    378 wdstart(arg)
    379 	void *arg;
    380 {
    381 	struct wd_softc *wd = arg;
    382 	struct buf *dp, *bp=0;
    383 
    384 	WDCDEBUG_PRINT(("wdstart\n"), DEBUG_FUNCS | DEBUG_XFERS);
    385 	while (wd->openings > 0) {
    386 
    387 		/* Is there a buf for us ? */
    388 		dp = &wd->sc_q;
    389 		if ((bp = dp->b_actf) == NULL)  /* yes, an assign */
    390 			 return;
    391 		dp->b_actf = bp->b_actf;
    392 
    393 		/*
    394 		 * Make the command. First lock the device
    395 		 */
    396 		wd->openings--;
    397 
    398 		wd->retries = 0;
    399 		__wdstart(wd, bp);
    400 	}
    401 }
    402 
    403 void
    404 __wdstart(wd, bp)
    405 	struct wd_softc *wd;
    406 	struct buf *bp;
    407 {
    408 	daddr_t p_offset;
    409 	if (WDPART(bp->b_dev) != RAW_PART)
    410 		p_offset =
    411 		    wd->sc_dk.dk_label->d_partitions[WDPART(bp->b_dev)].p_offset;
    412 	else
    413 		p_offset = 0;
    414 	wd->sc_wdc_bio.blkno = bp->b_blkno + p_offset;
    415 	wd->sc_wdc_bio.blkno /= (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
    416 	wd->sc_wdc_bio.blkdone =0;
    417 	wd->sc_bp = bp;
    418 	/*
    419 	 * If we're retrying, retry in single-sector mode. This will give us
    420 	 * the sector number of the problem, and will eventually allow the
    421 	 * transfert to succeed.
    422 	 */
    423 	if (wd->sc_multi == 1 || wd->retries > 0)
    424 		wd->sc_wdc_bio.flags = ATA_SINGLE;
    425 	else
    426 		wd->sc_wdc_bio.flags = 0;
    427 	if (wd->sc_flags & WDF_LBA)
    428 		wd->sc_wdc_bio.flags |= ATA_LBA;
    429 	if (bp->b_flags & B_READ)
    430 		wd->sc_wdc_bio.flags |= ATA_READ;
    431 	wd->sc_wdc_bio.bcount = bp->b_bcount;
    432 	wd->sc_wdc_bio.databuf = bp->b_data;
    433 	/* Instrumentation. */
    434 	disk_busy(&wd->sc_dk);
    435 	switch (wdc_ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
    436 	case WDC_TRY_AGAIN:
    437 		timeout(wdrestart, wd, hz);
    438 		break;
    439 	case WDC_QUEUED:
    440 		break;
    441 	case WDC_COMPLETE:
    442 		wddone(wd);
    443 		break;
    444 	default:
    445 		panic("__wdstart: bad return code from wdc_ata_bio()");
    446 	}
    447 }
    448 
    449 void
    450 wddone(v)
    451 	void *v;
    452 {
    453 	struct wd_softc *wd = v;
    454 	struct buf *bp = wd->sc_bp;
    455 	char buf[256], *errbuf = buf;
    456 	WDCDEBUG_PRINT(("wddone\n"), DEBUG_FUNCS | DEBUG_XFERS);
    457 
    458 	bp->b_resid = wd->sc_wdc_bio.bcount;
    459 	errbuf[0] = '\0';
    460 	switch (wd->sc_wdc_bio.error) {
    461 	case ERR_DMA:
    462 		errbuf = "DMA error";
    463 		goto retry;
    464 	case ERR_DF:
    465 		errbuf = "device fault";
    466 		goto retry;
    467 	case TIMEOUT:
    468 		errbuf = "device timeout";
    469 		goto retry;
    470 	case ERROR:
    471 		/* Don't care about media change bits */
    472 		if (wd->sc_wdc_bio.r_error != 0 &&
    473 		    (wd->sc_wdc_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0)
    474 			goto noerror;
    475 		print_wderror(wd->sc_wdc_bio.r_error, errbuf);
    476 retry:		/* Just reset and retry. Can we do more ? */
    477 		wdc_reset_channel(wd->drvp);
    478 		diskerr(bp, "wd", errbuf, LOG_PRINTF,
    479 		    wd->sc_wdc_bio.blkdone, wd->sc_dk.dk_label);
    480 		if (wd->retries++ < WDIORETRIES) {
    481 			printf(", retrying\n");
    482 			timeout(wdrestart, wd, RECOVERYTIME);
    483 			return;
    484 		}
    485 		printf("\n");
    486 		bp->b_flags |= B_ERROR;
    487 		bp->b_error = EIO;
    488 		break;
    489 	case NOERROR:
    490 noerror:	if ((wd->sc_wdc_bio.flags & ATA_CORR) || wd->retries > 0)
    491 			printf("%s: soft error (corrected)\n",
    492 			    wd->sc_dev.dv_xname);
    493 	}
    494 	disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid));
    495 #if NRND > 0
    496 	rnd_add_uint32(&wd->rnd_source, bp->b_blkno);
    497 #endif
    498 	biodone(bp);
    499 	wd->openings++;
    500 	wdstart(wd);
    501 }
    502 
    503 void
    504 wdrestart(v)
    505 	void *v;
    506 {
    507 	struct wd_softc *wd = v;
    508 	struct buf *bp = wd->sc_bp;
    509 	int s;
    510 	WDCDEBUG_PRINT(("wdrestart\n"), DEBUG_FUNCS | DEBUG_XFERS);
    511 
    512 	s = splbio();
    513 	__wdstart(v, bp);
    514 	splx(s);
    515 }
    516 
    517 int
    518 wdread(dev, uio, flags)
    519 	dev_t dev;
    520 	struct uio *uio;
    521 	int flags;
    522 {
    523 
    524 	WDCDEBUG_PRINT(("wdread\n"), DEBUG_FUNCS | DEBUG_XFERS);
    525 	return (physio(wdstrategy, NULL, dev, B_READ, minphys, uio));
    526 }
    527 
    528 int
    529 wdwrite(dev, uio, flags)
    530 	dev_t dev;
    531 	struct uio *uio;
    532 	int flags;
    533 {
    534 
    535 	WDCDEBUG_PRINT(("wdwrite\n"), DEBUG_FUNCS | DEBUG_XFERS);
    536 	return (physio(wdstrategy, NULL, dev, B_WRITE, minphys, uio));
    537 }
    538 
    539 /*
    540  * Wait interruptibly for an exclusive lock.
    541  *
    542  * XXX
    543  * Several drivers do this; it should be abstracted and made MP-safe.
    544  */
    545 int
    546 wdlock(wd)
    547 	struct wd_softc *wd;
    548 {
    549 	int error;
    550 	int s;
    551 
    552 	WDCDEBUG_PRINT(("wdlock\n"), DEBUG_FUNCS);
    553 
    554 	s = splbio();
    555 
    556 	while ((wd->sc_flags & WDF_LOCKED) != 0) {
    557 		wd->sc_flags |= WDF_WANTED;
    558 		if ((error = tsleep(wd, PRIBIO | PCATCH,
    559 		    "wdlck", 0)) != 0) {
    560 			splx(s);
    561 			return error;
    562 		}
    563 	}
    564 	wd->sc_flags |= WDF_LOCKED;
    565 	splx(s);
    566 	return 0;
    567 }
    568 
    569 /*
    570  * Unlock and wake up any waiters.
    571  */
    572 void
    573 wdunlock(wd)
    574 	struct wd_softc *wd;
    575 {
    576 
    577 	WDCDEBUG_PRINT(("wdunlock\n"), DEBUG_FUNCS);
    578 
    579 	wd->sc_flags &= ~WDF_LOCKED;
    580 	if ((wd->sc_flags & WDF_WANTED) != 0) {
    581 		wd->sc_flags &= ~WDF_WANTED;
    582 		wakeup(wd);
    583 	}
    584 }
    585 
    586 int
    587 wdopen(dev, flag, fmt, p)
    588 	dev_t dev;
    589 	int flag, fmt;
    590 	struct proc *p;
    591 {
    592 	struct wd_softc *wd;
    593 	int unit, part;
    594 	int error;
    595 
    596 	WDCDEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS);
    597 	unit = WDUNIT(dev);
    598 	if (unit >= wd_cd.cd_ndevs)
    599 		return ENXIO;
    600 	wd = wd_cd.cd_devs[unit];
    601 	if (wd == NULL)
    602 		return ENXIO;
    603 
    604 	if ((error = wdlock(wd)) != 0)
    605 		return error;
    606 
    607 	if (wd->sc_dk.dk_openmask != 0) {
    608 		/*
    609 		 * If any partition is open, but the disk has been invalidated,
    610 		 * disallow further opens.
    611 		 */
    612 		if ((wd->sc_flags & WDF_LOADED) == 0) {
    613 			error = EIO;
    614 			goto bad3;
    615 		}
    616 	} else {
    617 		if ((wd->sc_flags & WDF_LOADED) == 0) {
    618 			wd->sc_flags |= WDF_LOADED;
    619 
    620 			/* Load the physical device parameters. */
    621 			wd_get_params(wd, AT_POLL, &wd->sc_params);
    622 
    623 			/* Load the partition info if not already loaded. */
    624 			wdgetdisklabel(wd);
    625 		}
    626 	}
    627 
    628 	part = WDPART(dev);
    629 
    630 	/* Check that the partition exists. */
    631 	if (part != RAW_PART &&
    632 	    (part >= wd->sc_dk.dk_label->d_npartitions ||
    633 	     wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
    634 		error = ENXIO;
    635 		goto bad;
    636 	}
    637 
    638 	/* Insure only one open at a time. */
    639 	switch (fmt) {
    640 	case S_IFCHR:
    641 		wd->sc_dk.dk_copenmask |= (1 << part);
    642 		break;
    643 	case S_IFBLK:
    644 		wd->sc_dk.dk_bopenmask |= (1 << part);
    645 		break;
    646 	}
    647 	wd->sc_dk.dk_openmask =
    648 	    wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
    649 
    650 	wdunlock(wd);
    651 	return 0;
    652 
    653 bad:
    654 	if (wd->sc_dk.dk_openmask == 0) {
    655 	}
    656 
    657 bad3:
    658 	wdunlock(wd);
    659 	return error;
    660 }
    661 
    662 int
    663 wdclose(dev, flag, fmt, p)
    664 	dev_t dev;
    665 	int flag, fmt;
    666 	struct proc *p;
    667 {
    668 	struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(dev)];
    669 	int part = WDPART(dev);
    670 	int error;
    671 
    672 	WDCDEBUG_PRINT(("wdclose\n"), DEBUG_FUNCS);
    673 	if ((error = wdlock(wd)) != 0)
    674 		return error;
    675 
    676 	switch (fmt) {
    677 	case S_IFCHR:
    678 		wd->sc_dk.dk_copenmask &= ~(1 << part);
    679 		break;
    680 	case S_IFBLK:
    681 		wd->sc_dk.dk_bopenmask &= ~(1 << part);
    682 		break;
    683 	}
    684 	wd->sc_dk.dk_openmask =
    685 	    wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
    686 
    687 	if (wd->sc_dk.dk_openmask == 0) {
    688 		/* XXXX Must wait for I/O to complete! */
    689 	}
    690 
    691 	wdunlock(wd);
    692 	return 0;
    693 }
    694 
    695 void
    696 wdgetdefaultlabel(wd, lp)
    697 	struct wd_softc *wd;
    698 	struct disklabel *lp;
    699 {
    700 
    701 	WDCDEBUG_PRINT(("wdgetdefaultlabel\n"), DEBUG_FUNCS);
    702 	memset(lp, 0, sizeof(struct disklabel));
    703 
    704 	lp->d_secsize = DEV_BSIZE;
    705 	lp->d_ntracks = wd->sc_params.atap_heads;
    706 	lp->d_nsectors = wd->sc_params.atap_sectors;
    707 	lp->d_ncylinders = wd->sc_params.atap_cylinders;
    708 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    709 
    710 	if (strcmp(wd->sc_params.atap_model, "ST506") == 0) {
    711 		lp->d_type = DTYPE_ST506;
    712 		strncpy(lp->d_typename, "ST506 disk", 16);
    713 	} else {
    714 		lp->d_type = DTYPE_ESDI;
    715 		strncpy(lp->d_typename, "ESDI/IDE",
    716 		sizeof lp->d_typename);
    717 	}
    718 	strncpy(lp->d_packname, wd->sc_params.atap_model, 16);
    719 	lp->d_secperunit = wd->sc_capacity;
    720 	lp->d_rpm = 3600;
    721 	lp->d_interleave = 1;
    722 	lp->d_flags = 0;
    723 
    724 	lp->d_partitions[RAW_PART].p_offset = 0;
    725 	lp->d_partitions[RAW_PART].p_size =
    726 	lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
    727 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
    728 	lp->d_npartitions = RAW_PART + 1;
    729 
    730 	lp->d_magic = DISKMAGIC;
    731 	lp->d_magic2 = DISKMAGIC;
    732 	lp->d_checksum = dkcksum(lp);
    733 }
    734 
    735 /*
    736  * Fabricate a default disk label, and try to read the correct one.
    737  */
    738 void
    739 wdgetdisklabel(wd)
    740 	struct wd_softc *wd;
    741 {
    742 	struct disklabel *lp = wd->sc_dk.dk_label;
    743 	char *errstring;
    744 
    745 	WDCDEBUG_PRINT(("wdgetdisklabel\n"), DEBUG_FUNCS);
    746 
    747 	memset(wd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
    748 
    749 	wdgetdefaultlabel(wd, lp);
    750 
    751 	wd->sc_badsect[0] = -1;
    752 
    753 	if (wd->drvp->state > RECAL)
    754 		wd->drvp->state = RECAL;
    755 	errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
    756 	    wdstrategy, lp, wd->sc_dk.dk_cpulabel);
    757 	if (errstring) {
    758 		/*
    759 		 * This probably happened because the drive's default
    760 		 * geometry doesn't match the DOS geometry.  We
    761 		 * assume the DOS geometry is now in the label and try
    762 		 * again.  XXX This is a kluge.
    763 		 */
    764 		if (wd->drvp->state > RECAL)
    765 			wd->drvp->state = RECAL;
    766 		errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit,
    767 		    RAW_PART), wdstrategy, lp, wd->sc_dk.dk_cpulabel);
    768 	}
    769 	if (errstring) {
    770 		printf("%s: %s\n", wd->sc_dev.dv_xname, errstring);
    771 		return;
    772 	}
    773 
    774 	if (wd->drvp->state > RECAL)
    775 		wd->drvp->state = RECAL;
    776 #ifdef HAS_BAD144_HANDLING
    777 	if ((lp->d_flags & D_BADSECT) != 0)
    778 		bad144intern(wd);
    779 #endif
    780 }
    781 
    782 int
    783 wdioctl(dev, xfer, addr, flag, p)
    784 	dev_t dev;
    785 	u_long xfer;
    786 	caddr_t addr;
    787 	int flag;
    788 	struct proc *p;
    789 {
    790 	struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(dev)];
    791 	int error;
    792 
    793 	WDCDEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS);
    794 
    795 	if ((wd->sc_flags & WDF_LOADED) == 0)
    796 		return EIO;
    797 
    798 	switch (xfer) {
    799 #ifdef HAS_BAD144_HANDLING
    800 	case DIOCSBAD:
    801 		if ((flag & FWRITE) == 0)
    802 			return EBADF;
    803 		wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
    804 		wd->sc_dk.dk_label->d_flags |= D_BADSECT;
    805 		bad144intern(wd);
    806 		return 0;
    807 #endif
    808 
    809 	case DIOCGDINFO:
    810 		*(struct disklabel *)addr = *(wd->sc_dk.dk_label);
    811 		return 0;
    812 
    813 	case DIOCGPART:
    814 		((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
    815 		((struct partinfo *)addr)->part =
    816 		    &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
    817 		return 0;
    818 
    819 	case DIOCWDINFO:
    820 	case DIOCSDINFO:
    821 		if ((flag & FWRITE) == 0)
    822 			return EBADF;
    823 
    824 		if ((error = wdlock(wd)) != 0)
    825 			return error;
    826 		wd->sc_flags |= WDF_LABELLING;
    827 
    828 		error = setdisklabel(wd->sc_dk.dk_label,
    829 		    (struct disklabel *)addr, /*wd->sc_dk.dk_openmask : */0,
    830 		    wd->sc_dk.dk_cpulabel);
    831 		if (error == 0) {
    832 			if (wd->drvp->state > RECAL)
    833 				wd->drvp->state = RECAL;
    834 			if (xfer == DIOCWDINFO)
    835 				error = writedisklabel(WDLABELDEV(dev),
    836 				    wdstrategy, wd->sc_dk.dk_label,
    837 				    wd->sc_dk.dk_cpulabel);
    838 		}
    839 
    840 		wd->sc_flags &= ~WDF_LABELLING;
    841 		wdunlock(wd);
    842 		return error;
    843 
    844 	case DIOCWLABEL:
    845 		if ((flag & FWRITE) == 0)
    846 			return EBADF;
    847 		if (*(int *)addr)
    848 			wd->sc_flags |= WDF_WLABEL;
    849 		else
    850 			wd->sc_flags &= ~WDF_WLABEL;
    851 		return 0;
    852 
    853 	case DIOCGDEFLABEL:
    854 		wdgetdefaultlabel(wd, (struct disklabel *)addr);
    855 		return 0;
    856 
    857 #ifdef notyet
    858 	case DIOCWFORMAT:
    859 		if ((flag & FWRITE) == 0)
    860 			return EBADF;
    861 		{
    862 		register struct format_op *fop;
    863 		struct iovec aiov;
    864 		struct uio auio;
    865 
    866 		fop = (struct format_op *)addr;
    867 		aiov.iov_base = fop->df_buf;
    868 		aiov.iov_len = fop->df_count;
    869 		auio.uio_iov = &aiov;
    870 		auio.uio_iovcnt = 1;
    871 		auio.uio_resid = fop->df_count;
    872 		auio.uio_segflg = 0;
    873 		auio.uio_offset =
    874 			fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
    875 		auio.uio_procp = p;
    876 		error = physio(wdformat, NULL, dev, B_WRITE, minphys,
    877 		    &auio);
    878 		fop->df_count -= auio.uio_resid;
    879 		fop->df_reg[0] = wdc->sc_status;
    880 		fop->df_reg[1] = wdc->sc_error;
    881 		return error;
    882 		}
    883 #endif
    884 
    885 	default:
    886 		return ENOTTY;
    887 	}
    888 
    889 #ifdef DIAGNOSTIC
    890 	panic("wdioctl: impossible");
    891 #endif
    892 }
    893 
    894 #ifdef B_FORMAT
    895 int
    896 wdformat(struct buf *bp)
    897 {
    898 
    899 	bp->b_flags |= B_FORMAT;
    900 	return wdstrategy(bp);
    901 }
    902 #endif
    903 
    904 int
    905 wdsize(dev)
    906 	dev_t dev;
    907 {
    908 	struct wd_softc *wd;
    909 	int part, unit, omask;
    910 	int size;
    911 
    912 	WDCDEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS);
    913 
    914 	unit = WDUNIT(dev);
    915 	if (unit >= wd_cd.cd_ndevs)
    916 		return (-1);
    917 	wd = wd_cd.cd_devs[unit];
    918 	if (wd == NULL)
    919 		return (-1);
    920 
    921 	part = WDPART(dev);
    922 	omask = wd->sc_dk.dk_openmask & (1 << part);
    923 
    924 	if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0)
    925 		return (-1);
    926 	if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
    927 		size = -1;
    928 	else
    929 		size = wd->sc_dk.dk_label->d_partitions[part].p_size *
    930 		    (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
    931 	if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0)
    932 		return (-1);
    933 	return (size);
    934 }
    935 
    936 #ifndef __BDEVSW_DUMP_OLD_TYPE
    937 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
    938 static int wddoingadump = 0;
    939 static int wddumprecalibrated = 0;
    940 static int wddumpmulti = 1;
    941 
    942 /*
    943  * Dump core after a system crash.
    944  */
    945 int
    946 wddump(dev, blkno, va, size)
    947 	dev_t dev;
    948 	daddr_t blkno;
    949 	caddr_t va;
    950 	size_t size;
    951 {
    952 	struct wd_softc *wd;	/* disk unit to do the I/O */
    953 	struct disklabel *lp;   /* disk's disklabel */
    954 	int unit, part;
    955 	int nblks;	/* total number of sectors left to write */
    956 	int err;
    957 	char errbuf[256];
    958 
    959 	/* Check if recursive dump; if so, punt. */
    960 	if (wddoingadump)
    961 		return EFAULT;
    962 	wddoingadump = 1;
    963 
    964 	unit = WDUNIT(dev);
    965 	if (unit >= wd_cd.cd_ndevs)
    966 		return ENXIO;
    967 	wd = wd_cd.cd_devs[unit];
    968 	if (wd == (struct wd_softc *)0)
    969 		return ENXIO;
    970 
    971 	part = WDPART(dev);
    972 
    973 	/* Make sure it was initialized. */
    974 	if (wd->drvp->state < READY)
    975 		return ENXIO;
    976 
    977 	/* Convert to disk sectors.  Request must be a multiple of size. */
    978 	lp = wd->sc_dk.dk_label;
    979 	if ((size % lp->d_secsize) != 0)
    980 		return EFAULT;
    981 	nblks = size / lp->d_secsize;
    982 	blkno = blkno / (lp->d_secsize / DEV_BSIZE);
    983 
    984 	/* Check transfer bounds against partition size. */
    985 	if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
    986 		return EINVAL;
    987 
    988 	/* Offset block number to start of partition. */
    989 	blkno += lp->d_partitions[part].p_offset;
    990 
    991 	/* Recalibrate, if first dump transfer. */
    992 	if (wddumprecalibrated == 0) {
    993 		wddumpmulti = wd->sc_multi;
    994 		wddumprecalibrated = 1;
    995 		wd->drvp->state = RECAL;
    996 	}
    997 
    998 	while (nblks > 0) {
    999 again:
   1000 		wd->sc_wdc_bio.blkno = blkno;
   1001 		wd->sc_wdc_bio.flags = ATA_POLL;
   1002 		if (wddumpmulti == 1)
   1003 			wd->sc_wdc_bio.flags |= ATA_SINGLE;
   1004 		if (wd->sc_flags & WDF_LBA)
   1005 			wd->sc_wdc_bio.flags |= ATA_LBA;
   1006 		wd->sc_wdc_bio.bcount =
   1007 			min(nblks, wddumpmulti) * lp->d_secsize;
   1008 		wd->sc_wdc_bio.databuf = va;
   1009 #ifndef WD_DUMP_NOT_TRUSTED
   1010 		switch (wdc_ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
   1011 		case WDC_TRY_AGAIN:
   1012 			panic("wddump: try again");
   1013 			break;
   1014 		case WDC_QUEUED:
   1015 			panic("wddump: polled command has been queued");
   1016 			break;
   1017 		case WDC_COMPLETE:
   1018 			break;
   1019 		}
   1020 		switch(wd->sc_wdc_bio.error) {
   1021 		case TIMEOUT:
   1022 			printf("wddump: device timed out");
   1023 			err = EIO;
   1024 			break;
   1025 		case ERR_DF:
   1026 			printf("wddump: drive fault");
   1027 			err = EIO;
   1028 			break;
   1029 		case ERR_DMA:
   1030 			printf("wddump: DMA error");
   1031 			err = EIO;
   1032 			break;
   1033 		case ERROR:
   1034 			errbuf[0] = '\0';
   1035 			print_wderror(wd->sc_wdc_bio.r_error, errbuf);
   1036 			printf("wddump: %s", errbuf);
   1037 			err = EIO;
   1038 			break;
   1039 		case NOERROR:
   1040 			err = 0;
   1041 			break;
   1042 		default:
   1043 			panic("wddump: unknown error type");
   1044 		}
   1045 		if (err != 0) {
   1046 			if (wddumpmulti != 1) {
   1047 				wddumpmulti = 1; /* retry in single-sector */
   1048 				printf(", retrying\n");
   1049 				goto again;
   1050 			}
   1051 			printf("\n");
   1052 			return err;
   1053 		}
   1054 #else	/* WD_DUMP_NOT_TRUSTED */
   1055 		/* Let's just talk about this first... */
   1056 		printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
   1057 		    unit, va, cylin, head, sector);
   1058 		delay(500 * 1000);	/* half a second */
   1059 #endif
   1060 
   1061 		/* update block count */
   1062 		nblks -= min(nblks, wddumpmulti);
   1063 		blkno += min(nblks, wddumpmulti);
   1064 		va += min(nblks, wddumpmulti) * lp->d_secsize;
   1065 	}
   1066 
   1067 	wddoingadump = 0;
   1068 	return 0;
   1069 }
   1070 #else /* __BDEVSW_DUMP_NEW_TYPE */
   1071 
   1072 
   1073 int
   1074 wddump(dev, blkno, va, size)
   1075 	dev_t dev;
   1076 	daddr_t blkno;
   1077 	caddr_t va;
   1078 	size_t size;
   1079 {
   1080 
   1081 	/* Not implemented. */
   1082 	return ENXIO;
   1083 }
   1084 #endif /* __BDEVSW_DUMP_NEW_TYPE */
   1085 
   1086 #ifdef HAS_BAD144_HANDLING
   1087 /*
   1088  * Internalize the bad sector table.
   1089  */
   1090 void
   1091 bad144intern(wd)
   1092 	struct wd_softc *wd;
   1093 {
   1094 	struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
   1095 	struct disklabel *lp = wd->sc_dk.dk_label;
   1096 	int i = 0;
   1097 
   1098 	WDCDEBUG_PRINT(("bad144intern\n"), DEBUG_FUNCS | DEBUG_XFERS);
   1099 
   1100 	for (; i < NBT_BAD; i++) {
   1101 		if (bt->bt_bad[i].bt_cyl == 0xffff)
   1102 			break;
   1103 		wd->sc_badsect[i] =
   1104 		    bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
   1105 		    (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
   1106 		    (bt->bt_bad[i].bt_trksec & 0xff);
   1107 	}
   1108 	for (; i < NBT_BAD+1; i++)
   1109 		wd->sc_badsect[i] = -1;
   1110 }
   1111 #endif
   1112 
   1113 void
   1114 print_wderror(errno, buf)
   1115 	int errno;
   1116 	char *buf;
   1117 {
   1118 	static char *errstr[] = {"address mark not found", "track 0 not found",
   1119 	"aborted command", "media change requested", "id not found",
   1120 	"media changed", "uncorrectable data error", "bad block detected"};
   1121 	int i;
   1122 	char *sep = "";
   1123 
   1124 	if (errno == 0) {
   1125 		sprintf(buf, "error not notified");
   1126 	}
   1127 
   1128 	for (i = 0; i < 8; i++) {
   1129 		if (errno & (1 << i)) {
   1130 			buf += sprintf(buf, "%s %s", sep, errstr[i]);
   1131 			sep = ",";
   1132 		}
   1133 	}
   1134 }
   1135 
   1136 int
   1137 wd_get_params(wd, flags, params)
   1138 	struct wd_softc *wd;
   1139 	u_int8_t flags;
   1140 	struct ataparams *params;
   1141 {
   1142 	switch (ata_get_params(wd->drvp, flags, params)) {
   1143 	case CMD_AGAIN:
   1144 		return 1;
   1145 	case CMD_ERR:
   1146 		/*
   1147 		 * We `know' there's a drive here; just assume it's old.
   1148 		 * This geometry is only used to read the MBR and print a
   1149 		 * (false) attach message.
   1150 		 */
   1151 		strncpy(params->atap_model, "ST506",
   1152 		    sizeof params->atap_model);
   1153 		params->atap_config = ATA_CFG_FIXED;
   1154 		params->atap_cylinders = 1024;
   1155 		params->atap_heads = 8;
   1156 		params->atap_sectors = 17;
   1157 		params->atap_multi = 1;
   1158 		params->atap_capabilities1 = params->atap_capabilities2 = 0;
   1159 		return 0;
   1160 	case CMD_OK:
   1161 		return 0;
   1162 	default:
   1163 		panic("wd_get_params: bad return code from ata_get_params");
   1164 		/* NOTREACHED */
   1165 	}
   1166 }
   1167