Home | History | Annotate | Line # | Download | only in ata
wd.c revision 1.175.2.10
      1 /*	$NetBSD: wd.c,v 1.175.2.10 1998/09/20 13:16:16 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 	return 1;
    218 }
    219 
    220 void
    221 wdattach(parent, self, aux)
    222 	struct device *parent, *self;
    223 	void *aux;
    224 {
    225 	struct wd_softc *wd = (void *)self;
    226 	struct ata_atapi_attach *aa_link= aux;
    227 	int i, blank;
    228 	char buf[41], c, *p, *q;
    229 	WDCDEBUG_PRINT(("wdattach\n"), DEBUG_FUNCS | DEBUG_PROBE);
    230 
    231 	wd->openings = aa_link->aa_openings;
    232 	wd->drvp = aa_link->aa_drv_data;;
    233 	wd->wdc_softc = parent;
    234 	/* give back our softc to our caller */
    235 	wd->drvp->drv_softc = wd;
    236 
    237 	/* read our drive info */
    238 	if (wd_get_params(wd, AT_POLL, &wd->sc_params) != 0) {
    239 		printf("%s: IDENTIFY failed\n", wd->sc_dev.dv_xname);
    240 		return;
    241 	}
    242 
    243 	for (blank = 0, p = wd->sc_params.atap_model, q = buf, i = 0;
    244 	    i < sizeof(wd->sc_params.atap_model); i++) {
    245 		c = *p++;
    246 		if (c == '\0')
    247 			break;
    248 		if (c != ' ') {
    249 			if (blank) {
    250 				*q++ = ' ';
    251 				blank = 0;
    252 			}
    253 			*q++ = c;
    254 		} else
    255 			blank = 1;
    256 		}
    257 	*q++ = '\0';
    258 
    259 	printf(": <%s>\n", buf);
    260 
    261 	if ((wd->sc_params.atap_multi & 0xff) > 1) {
    262 		wd->sc_multi = wd->sc_params.atap_multi & 0xff;
    263 	} else {
    264 		wd->sc_multi = 1;
    265 	}
    266 
    267 	printf("%s: using %d-sector pio transfers,", wd->sc_dev.dv_xname,
    268 	    wd->sc_multi);
    269 
    270 	/* Prior to ATA-4, LBA was optional. */
    271 	if ((wd->sc_params.atap_capabilities1 & WDC_CAP_LBA) != 0)
    272 		wd->sc_flags |= WDF_LBA;
    273 #if 0
    274 	/* ATA-4 requires LBA. */
    275 	if (wd->sc_params.atap_ataversion != 0xffff &&
    276 	    wd->sc_params.atap_ataversion >= WDC_VER_ATA4)
    277 		wd->sc_flags |= WDF_LBA;
    278 #endif
    279 
    280 	if ((wd->sc_flags & WDF_LBA) != 0) {
    281 		printf(" lba mode\n");
    282 		wd->sc_capacity =
    283 		    (wd->sc_params.atap_capacity[1] << 16) |
    284 		    wd->sc_params.atap_capacity[0];
    285 		printf("%s %dMB, %d cyl, %d head, %d sec, %d bytes/sect x %d sectors\n",
    286 		    self->dv_xname,
    287 		    wd->sc_capacity / (1048576 / DEV_BSIZE),
    288 		    wd->sc_params.atap_cylinders,
    289 		    wd->sc_params.atap_heads,
    290 		    wd->sc_params.atap_sectors,
    291 		    DEV_BSIZE,
    292 		    wd->sc_capacity);
    293 	} else {
    294 		printf(" chs mode\n");
    295 		wd->sc_capacity =
    296 		    wd->sc_params.atap_cylinders *
    297 		    wd->sc_params.atap_heads *
    298 		    wd->sc_params.atap_sectors;
    299 		printf("%s %dMB, %d cyl, %d head, %d sec, %d bytes/sect x %d "
    300 		    "sectors\n", self->dv_xname,
    301 		    wd->sc_capacity / (1048576 / DEV_BSIZE),
    302 		    wd->sc_params.atap_cylinders,
    303 		    wd->sc_params.atap_heads,
    304 		    wd->sc_params.atap_sectors,
    305 		    DEV_BSIZE,
    306 		    wd->sc_capacity);
    307 	}
    308 	WDCDEBUG_PRINT(("atap_dmatiming_mimi=%d, atap_dmatiming_recom=%d\n",
    309 	    wd->sc_params.atap_dmatiming_mimi,
    310 	    wd->sc_params.atap_dmatiming_recom), DEBUG_PROBE);
    311 	/*
    312 	 * Initialize and attach the disk structure.
    313 	 */
    314 	wd->sc_dk.dk_driver = &wddkdriver;
    315 	wd->sc_dk.dk_name = wd->sc_dev.dv_xname;
    316 	disk_attach(&wd->sc_dk);
    317 	wd->sc_wdc_bio.lp = wd->sc_dk.dk_label;
    318 
    319 #if NRND > 0
    320 	rnd_attach_source(&wd->rnd_source, wd->sc_dev.dv_xname, RND_TYPE_DISK);
    321 #endif
    322 }
    323 
    324 /*
    325  * Read/write routine for a buffer.  Validates the arguments and schedules the
    326  * transfer.  Does not wait for the transfer to complete.
    327  */
    328 void
    329 wdstrategy(bp)
    330 	struct buf *bp;
    331 {
    332 	struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(bp->b_dev)];
    333 	int s;
    334 	WDCDEBUG_PRINT(("wdstrategy\n"), DEBUG_FUNCS | DEBUG_XFERS);
    335 
    336 	/* Valid request?  */
    337 	if (bp->b_blkno < 0 ||
    338 	    (bp->b_bcount % wd->sc_dk.dk_label->d_secsize) != 0 ||
    339 	    (bp->b_bcount / wd->sc_dk.dk_label->d_secsize) >= (1 << NBBY)) {
    340 		bp->b_error = EINVAL;
    341 		goto bad;
    342 	}
    343 
    344 	/* If device invalidated (e.g. media change, door open), error. */
    345 	if ((wd->sc_flags & WDF_LOADED) == 0) {
    346 		bp->b_error = EIO;
    347 		goto bad;
    348 	}
    349 
    350 	/* If it's a null transfer, return immediately. */
    351 	if (bp->b_bcount == 0)
    352 		goto done;
    353 
    354 	/*
    355 	 * Do bounds checking, adjust transfer. if error, process.
    356 	 * If end of partition, just return.
    357 	 */
    358 	if (WDPART(bp->b_dev) != RAW_PART &&
    359 	    bounds_check_with_label(bp, wd->sc_dk.dk_label,
    360 	    (wd->sc_flags & (WDF_WLABEL|WDF_LABELLING)) != 0) <= 0)
    361 		goto done;
    362 	/* Queue transfer on drive, activate drive and controller if idle. */
    363 	s = splbio();
    364 	disksort(&wd->sc_q, bp);
    365 	wdstart(wd);
    366 	splx(s);
    367 	return;
    368 bad:
    369 	bp->b_flags |= B_ERROR;
    370 done:
    371 	/* Toss transfer; we're done early. */
    372 	bp->b_resid = bp->b_bcount;
    373 	biodone(bp);
    374 }
    375 
    376 /*
    377  * Queue a drive for I/O.
    378  */
    379 void
    380 wdstart(arg)
    381 	void *arg;
    382 {
    383 	struct wd_softc *wd = arg;
    384 	struct buf *dp, *bp=0;
    385 
    386 	WDCDEBUG_PRINT(("wdstart\n"), DEBUG_FUNCS | DEBUG_XFERS);
    387 	while (wd->openings > 0) {
    388 
    389 		/* Is there a buf for us ? */
    390 		dp = &wd->sc_q;
    391 		if ((bp = dp->b_actf) == NULL)  /* yes, an assign */
    392 			 return;
    393 		dp->b_actf = bp->b_actf;
    394 
    395 		/*
    396 		 * Make the command. First lock the device
    397 		 */
    398 		wd->openings--;
    399 
    400 		wd->retries = 0;
    401 		__wdstart(wd, bp);
    402 	}
    403 }
    404 
    405 void
    406 __wdstart(wd, bp)
    407 	struct wd_softc *wd;
    408 	struct buf *bp;
    409 {
    410 	daddr_t p_offset;
    411 	if (WDPART(bp->b_dev) != RAW_PART)
    412 		p_offset =
    413 		    wd->sc_dk.dk_label->d_partitions[WDPART(bp->b_dev)].p_offset;
    414 	else
    415 		p_offset = 0;
    416 	wd->sc_wdc_bio.blkno = bp->b_blkno + p_offset;
    417 	wd->sc_wdc_bio.blkno /= (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
    418 	wd->sc_wdc_bio.blkdone =0;
    419 	wd->sc_bp = bp;
    420 	/*
    421 	 * If we're retrying, retry in single-sector mode. This will give us
    422 	 * the sector number of the problem, and will eventually allow the
    423 	 * transfert to succeed.
    424 	 */
    425 	if (wd->sc_multi == 1 || wd->retries > 0)
    426 		wd->sc_wdc_bio.flags = ATA_SINGLE;
    427 	else
    428 		wd->sc_wdc_bio.flags = 0;
    429 	if (wd->sc_flags & WDF_LBA)
    430 		wd->sc_wdc_bio.flags |= ATA_LBA;
    431 	if (bp->b_flags & B_READ)
    432 		wd->sc_wdc_bio.flags |= ATA_READ;
    433 	wd->sc_wdc_bio.bcount = bp->b_bcount;
    434 	wd->sc_wdc_bio.databuf = bp->b_data;
    435 	/* Instrumentation. */
    436 	disk_busy(&wd->sc_dk);
    437 	switch (wdc_ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
    438 	case WDC_TRY_AGAIN:
    439 		timeout(wdrestart, wd, hz);
    440 		break;
    441 	case WDC_QUEUED:
    442 		break;
    443 	case WDC_COMPLETE:
    444 		wddone(wd);
    445 		break;
    446 	default:
    447 		panic("__wdstart: bad return code from wdc_ata_bio()");
    448 	}
    449 }
    450 
    451 void
    452 wddone(v)
    453 	void *v;
    454 {
    455 	struct wd_softc *wd = v;
    456 	struct buf *bp = wd->sc_bp;
    457 	char buf[256], *errbuf = buf;
    458 	WDCDEBUG_PRINT(("wddone\n"), DEBUG_FUNCS | DEBUG_XFERS);
    459 
    460 	bp->b_resid = wd->sc_wdc_bio.bcount;
    461 	errbuf[0] = '\0';
    462 	switch (wd->sc_wdc_bio.error) {
    463 	case ERR_DMA:
    464 		errbuf = "DMA error";
    465 		goto retry;
    466 	case ERR_DF:
    467 		errbuf = "device fault";
    468 		goto retry;
    469 	case TIMEOUT:
    470 		errbuf = "device timeout";
    471 		goto retry;
    472 	case ERROR:
    473 		/* Don't care about media change bits */
    474 		if (wd->sc_wdc_bio.r_error != 0 &&
    475 		    (wd->sc_wdc_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0)
    476 			goto noerror;
    477 		print_wderror(wd->sc_wdc_bio.r_error, errbuf);
    478 retry:		/* Just reset and retry. Can we do more ? */
    479 		wdc_reset_channel(wd->drvp);
    480 		diskerr(bp, "wd", errbuf, LOG_PRINTF,
    481 		    wd->sc_wdc_bio.blkdone, wd->sc_dk.dk_label);
    482 		if (wd->retries++ < WDIORETRIES) {
    483 			printf(", retrying\n");
    484 			timeout(wdrestart, wd, RECOVERYTIME);
    485 			return;
    486 		}
    487 		printf("\n");
    488 		bp->b_flags |= B_ERROR;
    489 		bp->b_error = EIO;
    490 		break;
    491 	case NOERROR:
    492 noerror:	if ((wd->sc_wdc_bio.flags & ATA_CORR) || wd->retries > 0)
    493 			printf("%s: soft error (corrected)\n",
    494 			    wd->sc_dev.dv_xname);
    495 	}
    496 	disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid));
    497 #if NRND > 0
    498 	rnd_add_uint32(&wd->rnd_source, bp->b_blkno);
    499 #endif
    500 	biodone(bp);
    501 	wd->openings++;
    502 	wdstart(wd);
    503 }
    504 
    505 void
    506 wdrestart(v)
    507 	void *v;
    508 {
    509 	struct wd_softc *wd = v;
    510 	struct buf *bp = wd->sc_bp;
    511 	int s;
    512 	WDCDEBUG_PRINT(("wdrestart\n"), DEBUG_FUNCS | DEBUG_XFERS);
    513 
    514 	s = splbio();
    515 	__wdstart(v, bp);
    516 	splx(s);
    517 }
    518 
    519 int
    520 wdread(dev, uio, flags)
    521 	dev_t dev;
    522 	struct uio *uio;
    523 	int flags;
    524 {
    525 
    526 	WDCDEBUG_PRINT(("wdread\n"), DEBUG_FUNCS | DEBUG_XFERS);
    527 	return (physio(wdstrategy, NULL, dev, B_READ, minphys, uio));
    528 }
    529 
    530 int
    531 wdwrite(dev, uio, flags)
    532 	dev_t dev;
    533 	struct uio *uio;
    534 	int flags;
    535 {
    536 
    537 	WDCDEBUG_PRINT(("wdwrite\n"), DEBUG_FUNCS | DEBUG_XFERS);
    538 	return (physio(wdstrategy, NULL, dev, B_WRITE, minphys, uio));
    539 }
    540 
    541 /*
    542  * Wait interruptibly for an exclusive lock.
    543  *
    544  * XXX
    545  * Several drivers do this; it should be abstracted and made MP-safe.
    546  */
    547 int
    548 wdlock(wd)
    549 	struct wd_softc *wd;
    550 {
    551 	int error;
    552 	int s;
    553 
    554 	WDCDEBUG_PRINT(("wdlock\n"), DEBUG_FUNCS);
    555 
    556 	s = splbio();
    557 
    558 	while ((wd->sc_flags & WDF_LOCKED) != 0) {
    559 		wd->sc_flags |= WDF_WANTED;
    560 		if ((error = tsleep(wd, PRIBIO | PCATCH,
    561 		    "wdlck", 0)) != 0) {
    562 			splx(s);
    563 			return error;
    564 		}
    565 	}
    566 	wd->sc_flags |= WDF_LOCKED;
    567 	splx(s);
    568 	return 0;
    569 }
    570 
    571 /*
    572  * Unlock and wake up any waiters.
    573  */
    574 void
    575 wdunlock(wd)
    576 	struct wd_softc *wd;
    577 {
    578 
    579 	WDCDEBUG_PRINT(("wdunlock\n"), DEBUG_FUNCS);
    580 
    581 	wd->sc_flags &= ~WDF_LOCKED;
    582 	if ((wd->sc_flags & WDF_WANTED) != 0) {
    583 		wd->sc_flags &= ~WDF_WANTED;
    584 		wakeup(wd);
    585 	}
    586 }
    587 
    588 int
    589 wdopen(dev, flag, fmt, p)
    590 	dev_t dev;
    591 	int flag, fmt;
    592 	struct proc *p;
    593 {
    594 	struct wd_softc *wd;
    595 	int unit, part;
    596 	int error;
    597 
    598 	WDCDEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS);
    599 	unit = WDUNIT(dev);
    600 	if (unit >= wd_cd.cd_ndevs)
    601 		return ENXIO;
    602 	wd = wd_cd.cd_devs[unit];
    603 	if (wd == NULL)
    604 		return ENXIO;
    605 
    606 	if ((error = wdlock(wd)) != 0)
    607 		return error;
    608 
    609 	if (wd->sc_dk.dk_openmask != 0) {
    610 		/*
    611 		 * If any partition is open, but the disk has been invalidated,
    612 		 * disallow further opens.
    613 		 */
    614 		if ((wd->sc_flags & WDF_LOADED) == 0) {
    615 			error = EIO;
    616 			goto bad3;
    617 		}
    618 	} else {
    619 		if ((wd->sc_flags & WDF_LOADED) == 0) {
    620 			wd->sc_flags |= WDF_LOADED;
    621 
    622 			/* Load the physical device parameters. */
    623 			wd_get_params(wd, AT_POLL, &wd->sc_params);
    624 
    625 			/* Load the partition info if not already loaded. */
    626 			wdgetdisklabel(wd);
    627 		}
    628 	}
    629 
    630 	part = WDPART(dev);
    631 
    632 	/* Check that the partition exists. */
    633 	if (part != RAW_PART &&
    634 	    (part >= wd->sc_dk.dk_label->d_npartitions ||
    635 	     wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
    636 		error = ENXIO;
    637 		goto bad;
    638 	}
    639 
    640 	/* Insure only one open at a time. */
    641 	switch (fmt) {
    642 	case S_IFCHR:
    643 		wd->sc_dk.dk_copenmask |= (1 << part);
    644 		break;
    645 	case S_IFBLK:
    646 		wd->sc_dk.dk_bopenmask |= (1 << part);
    647 		break;
    648 	}
    649 	wd->sc_dk.dk_openmask =
    650 	    wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
    651 
    652 	wdunlock(wd);
    653 	return 0;
    654 
    655 bad:
    656 	if (wd->sc_dk.dk_openmask == 0) {
    657 	}
    658 
    659 bad3:
    660 	wdunlock(wd);
    661 	return error;
    662 }
    663 
    664 int
    665 wdclose(dev, flag, fmt, p)
    666 	dev_t dev;
    667 	int flag, fmt;
    668 	struct proc *p;
    669 {
    670 	struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(dev)];
    671 	int part = WDPART(dev);
    672 	int error;
    673 
    674 	WDCDEBUG_PRINT(("wdclose\n"), DEBUG_FUNCS);
    675 	if ((error = wdlock(wd)) != 0)
    676 		return error;
    677 
    678 	switch (fmt) {
    679 	case S_IFCHR:
    680 		wd->sc_dk.dk_copenmask &= ~(1 << part);
    681 		break;
    682 	case S_IFBLK:
    683 		wd->sc_dk.dk_bopenmask &= ~(1 << part);
    684 		break;
    685 	}
    686 	wd->sc_dk.dk_openmask =
    687 	    wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
    688 
    689 	if (wd->sc_dk.dk_openmask == 0) {
    690 		/* XXXX Must wait for I/O to complete! */
    691 	}
    692 
    693 	wdunlock(wd);
    694 	return 0;
    695 }
    696 
    697 void
    698 wdgetdefaultlabel(wd, lp)
    699 	struct wd_softc *wd;
    700 	struct disklabel *lp;
    701 {
    702 
    703 	WDCDEBUG_PRINT(("wdgetdefaultlabel\n"), DEBUG_FUNCS);
    704 	memset(lp, 0, sizeof(struct disklabel));
    705 
    706 	lp->d_secsize = DEV_BSIZE;
    707 	lp->d_ntracks = wd->sc_params.atap_heads;
    708 	lp->d_nsectors = wd->sc_params.atap_sectors;
    709 	lp->d_ncylinders = wd->sc_params.atap_cylinders;
    710 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    711 
    712 #if 0
    713 	if (strcmp(wd->sc_params.atap_model, "ST506") == 0) {
    714 		lp->d_type = DTYPE_ST506;
    715 		strncpy(lp->d_typename, "ST506 disk", 16);
    716 	} else {
    717 		lp->d_type = DTYPE_ESDI;
    718 		strncpy(lp->d_typename, "ESDI/IDE",
    719 		sizeof lp->d_typename);
    720 	}
    721 #endif
    722 	strncpy(lp->d_typename, wd->sc_params.atap_model, 16);
    723 	strncpy(lp->d_packname, "fictitious", 16);
    724 	lp->d_secperunit = wd->sc_capacity;
    725 	lp->d_rpm = 3600;
    726 	lp->d_interleave = 1;
    727 	lp->d_flags = 0;
    728 
    729 	lp->d_partitions[RAW_PART].p_offset = 0;
    730 	lp->d_partitions[RAW_PART].p_size =
    731 	lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
    732 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
    733 	lp->d_npartitions = RAW_PART + 1;
    734 
    735 	lp->d_magic = DISKMAGIC;
    736 	lp->d_magic2 = DISKMAGIC;
    737 	lp->d_checksum = dkcksum(lp);
    738 }
    739 
    740 /*
    741  * Fabricate a default disk label, and try to read the correct one.
    742  */
    743 void
    744 wdgetdisklabel(wd)
    745 	struct wd_softc *wd;
    746 {
    747 	struct disklabel *lp = wd->sc_dk.dk_label;
    748 	char *errstring;
    749 
    750 	WDCDEBUG_PRINT(("wdgetdisklabel\n"), DEBUG_FUNCS);
    751 
    752 	memset(wd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
    753 
    754 	wdgetdefaultlabel(wd, lp);
    755 
    756 	wd->sc_badsect[0] = -1;
    757 
    758 	if (wd->drvp->state > RECAL)
    759 		wd->drvp->state = RECAL;
    760 	errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
    761 	    wdstrategy, lp, wd->sc_dk.dk_cpulabel);
    762 	if (errstring) {
    763 		/*
    764 		 * This probably happened because the drive's default
    765 		 * geometry doesn't match the DOS geometry.  We
    766 		 * assume the DOS geometry is now in the label and try
    767 		 * again.  XXX This is a kluge.
    768 		 */
    769 		if (wd->drvp->state > RECAL)
    770 			wd->drvp->state = RECAL;
    771 		errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit,
    772 		    RAW_PART), wdstrategy, lp, wd->sc_dk.dk_cpulabel);
    773 	}
    774 	if (errstring) {
    775 		printf("%s: %s\n", wd->sc_dev.dv_xname, errstring);
    776 		return;
    777 	}
    778 
    779 	if (wd->drvp->state > RECAL)
    780 		wd->drvp->state = RECAL;
    781 #ifdef HAS_BAD144_HANDLING
    782 	if ((lp->d_flags & D_BADSECT) != 0)
    783 		bad144intern(wd);
    784 #endif
    785 }
    786 
    787 int
    788 wdioctl(dev, xfer, addr, flag, p)
    789 	dev_t dev;
    790 	u_long xfer;
    791 	caddr_t addr;
    792 	int flag;
    793 	struct proc *p;
    794 {
    795 	struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(dev)];
    796 	int error;
    797 
    798 	WDCDEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS);
    799 
    800 	if ((wd->sc_flags & WDF_LOADED) == 0)
    801 		return EIO;
    802 
    803 	switch (xfer) {
    804 #ifdef HAS_BAD144_HANDLING
    805 	case DIOCSBAD:
    806 		if ((flag & FWRITE) == 0)
    807 			return EBADF;
    808 		wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
    809 		wd->sc_dk.dk_label->d_flags |= D_BADSECT;
    810 		bad144intern(wd);
    811 		return 0;
    812 #endif
    813 
    814 	case DIOCGDINFO:
    815 		*(struct disklabel *)addr = *(wd->sc_dk.dk_label);
    816 		return 0;
    817 
    818 	case DIOCGPART:
    819 		((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
    820 		((struct partinfo *)addr)->part =
    821 		    &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
    822 		return 0;
    823 
    824 	case DIOCWDINFO:
    825 	case DIOCSDINFO:
    826 		if ((flag & FWRITE) == 0)
    827 			return EBADF;
    828 
    829 		if ((error = wdlock(wd)) != 0)
    830 			return error;
    831 		wd->sc_flags |= WDF_LABELLING;
    832 
    833 		error = setdisklabel(wd->sc_dk.dk_label,
    834 		    (struct disklabel *)addr, /*wd->sc_dk.dk_openmask : */0,
    835 		    wd->sc_dk.dk_cpulabel);
    836 		if (error == 0) {
    837 			if (wd->drvp->state > RECAL)
    838 				wd->drvp->state = RECAL;
    839 			if (xfer == DIOCWDINFO)
    840 				error = writedisklabel(WDLABELDEV(dev),
    841 				    wdstrategy, wd->sc_dk.dk_label,
    842 				    wd->sc_dk.dk_cpulabel);
    843 		}
    844 
    845 		wd->sc_flags &= ~WDF_LABELLING;
    846 		wdunlock(wd);
    847 		return error;
    848 
    849 	case DIOCWLABEL:
    850 		if ((flag & FWRITE) == 0)
    851 			return EBADF;
    852 		if (*(int *)addr)
    853 			wd->sc_flags |= WDF_WLABEL;
    854 		else
    855 			wd->sc_flags &= ~WDF_WLABEL;
    856 		return 0;
    857 
    858 	case DIOCGDEFLABEL:
    859 		wdgetdefaultlabel(wd, (struct disklabel *)addr);
    860 		return 0;
    861 
    862 #ifdef notyet
    863 	case DIOCWFORMAT:
    864 		if ((flag & FWRITE) == 0)
    865 			return EBADF;
    866 		{
    867 		register struct format_op *fop;
    868 		struct iovec aiov;
    869 		struct uio auio;
    870 
    871 		fop = (struct format_op *)addr;
    872 		aiov.iov_base = fop->df_buf;
    873 		aiov.iov_len = fop->df_count;
    874 		auio.uio_iov = &aiov;
    875 		auio.uio_iovcnt = 1;
    876 		auio.uio_resid = fop->df_count;
    877 		auio.uio_segflg = 0;
    878 		auio.uio_offset =
    879 			fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
    880 		auio.uio_procp = p;
    881 		error = physio(wdformat, NULL, dev, B_WRITE, minphys,
    882 		    &auio);
    883 		fop->df_count -= auio.uio_resid;
    884 		fop->df_reg[0] = wdc->sc_status;
    885 		fop->df_reg[1] = wdc->sc_error;
    886 		return error;
    887 		}
    888 #endif
    889 
    890 	default:
    891 		return ENOTTY;
    892 	}
    893 
    894 #ifdef DIAGNOSTIC
    895 	panic("wdioctl: impossible");
    896 #endif
    897 }
    898 
    899 #ifdef B_FORMAT
    900 int
    901 wdformat(struct buf *bp)
    902 {
    903 
    904 	bp->b_flags |= B_FORMAT;
    905 	return wdstrategy(bp);
    906 }
    907 #endif
    908 
    909 int
    910 wdsize(dev)
    911 	dev_t dev;
    912 {
    913 	struct wd_softc *wd;
    914 	int part, unit, omask;
    915 	int size;
    916 
    917 	WDCDEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS);
    918 
    919 	unit = WDUNIT(dev);
    920 	if (unit >= wd_cd.cd_ndevs)
    921 		return (-1);
    922 	wd = wd_cd.cd_devs[unit];
    923 	if (wd == NULL)
    924 		return (-1);
    925 
    926 	part = WDPART(dev);
    927 	omask = wd->sc_dk.dk_openmask & (1 << part);
    928 
    929 	if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0)
    930 		return (-1);
    931 	if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
    932 		size = -1;
    933 	else
    934 		size = wd->sc_dk.dk_label->d_partitions[part].p_size *
    935 		    (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
    936 	if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0)
    937 		return (-1);
    938 	return (size);
    939 }
    940 
    941 #ifndef __BDEVSW_DUMP_OLD_TYPE
    942 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
    943 static int wddoingadump = 0;
    944 static int wddumprecalibrated = 0;
    945 static int wddumpmulti = 1;
    946 
    947 /*
    948  * Dump core after a system crash.
    949  */
    950 int
    951 wddump(dev, blkno, va, size)
    952 	dev_t dev;
    953 	daddr_t blkno;
    954 	caddr_t va;
    955 	size_t size;
    956 {
    957 	struct wd_softc *wd;	/* disk unit to do the I/O */
    958 	struct disklabel *lp;   /* disk's disklabel */
    959 	int unit, part;
    960 	int nblks;	/* total number of sectors left to write */
    961 	int err;
    962 	char errbuf[256];
    963 
    964 	/* Check if recursive dump; if so, punt. */
    965 	if (wddoingadump)
    966 		return EFAULT;
    967 	wddoingadump = 1;
    968 
    969 	unit = WDUNIT(dev);
    970 	if (unit >= wd_cd.cd_ndevs)
    971 		return ENXIO;
    972 	wd = wd_cd.cd_devs[unit];
    973 	if (wd == (struct wd_softc *)0)
    974 		return ENXIO;
    975 
    976 	part = WDPART(dev);
    977 
    978 	/* Make sure it was initialized. */
    979 	if (wd->drvp->state < READY)
    980 		return ENXIO;
    981 
    982 	/* Convert to disk sectors.  Request must be a multiple of size. */
    983 	lp = wd->sc_dk.dk_label;
    984 	if ((size % lp->d_secsize) != 0)
    985 		return EFAULT;
    986 	nblks = size / lp->d_secsize;
    987 	blkno = blkno / (lp->d_secsize / DEV_BSIZE);
    988 
    989 	/* Check transfer bounds against partition size. */
    990 	if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
    991 		return EINVAL;
    992 
    993 	/* Offset block number to start of partition. */
    994 	blkno += lp->d_partitions[part].p_offset;
    995 
    996 	/* Recalibrate, if first dump transfer. */
    997 	if (wddumprecalibrated == 0) {
    998 		wddumpmulti = wd->sc_multi;
    999 		wddumprecalibrated = 1;
   1000 		wd->drvp->state = RECAL;
   1001 	}
   1002 
   1003 	while (nblks > 0) {
   1004 again:
   1005 		wd->sc_wdc_bio.blkno = blkno;
   1006 		wd->sc_wdc_bio.flags = ATA_POLL;
   1007 		if (wddumpmulti == 1)
   1008 			wd->sc_wdc_bio.flags |= ATA_SINGLE;
   1009 		if (wd->sc_flags & WDF_LBA)
   1010 			wd->sc_wdc_bio.flags |= ATA_LBA;
   1011 		wd->sc_wdc_bio.bcount =
   1012 			min(nblks, wddumpmulti) * lp->d_secsize;
   1013 		wd->sc_wdc_bio.databuf = va;
   1014 #ifndef WD_DUMP_NOT_TRUSTED
   1015 		switch (wdc_ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
   1016 		case WDC_TRY_AGAIN:
   1017 			panic("wddump: try again");
   1018 			break;
   1019 		case WDC_QUEUED:
   1020 			panic("wddump: polled command has been queued");
   1021 			break;
   1022 		case WDC_COMPLETE:
   1023 			break;
   1024 		}
   1025 		switch(wd->sc_wdc_bio.error) {
   1026 		case TIMEOUT:
   1027 			printf("wddump: device timed out");
   1028 			err = EIO;
   1029 			break;
   1030 		case ERR_DF:
   1031 			printf("wddump: drive fault");
   1032 			err = EIO;
   1033 			break;
   1034 		case ERR_DMA:
   1035 			printf("wddump: DMA error");
   1036 			err = EIO;
   1037 			break;
   1038 		case ERROR:
   1039 			errbuf[0] = '\0';
   1040 			print_wderror(wd->sc_wdc_bio.r_error, errbuf);
   1041 			printf("wddump: %s", errbuf);
   1042 			err = EIO;
   1043 			break;
   1044 		case NOERROR:
   1045 			err = 0;
   1046 			break;
   1047 		default:
   1048 			panic("wddump: unknown error type");
   1049 		}
   1050 		if (err != 0) {
   1051 			if (wddumpmulti != 1) {
   1052 				wddumpmulti = 1; /* retry in single-sector */
   1053 				printf(", retrying\n");
   1054 				goto again;
   1055 			}
   1056 			printf("\n");
   1057 			return err;
   1058 		}
   1059 #else	/* WD_DUMP_NOT_TRUSTED */
   1060 		/* Let's just talk about this first... */
   1061 		printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
   1062 		    unit, va, cylin, head, sector);
   1063 		delay(500 * 1000);	/* half a second */
   1064 #endif
   1065 
   1066 		/* update block count */
   1067 		nblks -= min(nblks, wddumpmulti);
   1068 		blkno += min(nblks, wddumpmulti);
   1069 		va += min(nblks, wddumpmulti) * lp->d_secsize;
   1070 	}
   1071 
   1072 	wddoingadump = 0;
   1073 	return 0;
   1074 }
   1075 #else /* __BDEVSW_DUMP_NEW_TYPE */
   1076 
   1077 
   1078 int
   1079 wddump(dev, blkno, va, size)
   1080 	dev_t dev;
   1081 	daddr_t blkno;
   1082 	caddr_t va;
   1083 	size_t size;
   1084 {
   1085 
   1086 	/* Not implemented. */
   1087 	return ENXIO;
   1088 }
   1089 #endif /* __BDEVSW_DUMP_NEW_TYPE */
   1090 
   1091 #ifdef HAS_BAD144_HANDLING
   1092 /*
   1093  * Internalize the bad sector table.
   1094  */
   1095 void
   1096 bad144intern(wd)
   1097 	struct wd_softc *wd;
   1098 {
   1099 	struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
   1100 	struct disklabel *lp = wd->sc_dk.dk_label;
   1101 	int i = 0;
   1102 
   1103 	WDCDEBUG_PRINT(("bad144intern\n"), DEBUG_FUNCS | DEBUG_XFERS);
   1104 
   1105 	for (; i < NBT_BAD; i++) {
   1106 		if (bt->bt_bad[i].bt_cyl == 0xffff)
   1107 			break;
   1108 		wd->sc_badsect[i] =
   1109 		    bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
   1110 		    (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
   1111 		    (bt->bt_bad[i].bt_trksec & 0xff);
   1112 	}
   1113 	for (; i < NBT_BAD+1; i++)
   1114 		wd->sc_badsect[i] = -1;
   1115 }
   1116 #endif
   1117 
   1118 void
   1119 print_wderror(errno, buf)
   1120 	int errno;
   1121 	char *buf;
   1122 {
   1123 	static char *errstr[] = {"address mark not found", "track 0 not found",
   1124 	"aborted command", "media change requested", "id not found",
   1125 	"media changed", "uncorrectable data error", "bad block detected"};
   1126 	int i;
   1127 	char *sep = "";
   1128 
   1129 	if (errno == 0) {
   1130 		sprintf(buf, "error not notified");
   1131 	}
   1132 
   1133 	for (i = 0; i < 8; i++) {
   1134 		if (errno & (1 << i)) {
   1135 			buf += sprintf(buf, "%s %s", sep, errstr[i]);
   1136 			sep = ",";
   1137 		}
   1138 	}
   1139 }
   1140 
   1141 int
   1142 wd_get_params(wd, flags, params)
   1143 	struct wd_softc *wd;
   1144 	u_int8_t flags;
   1145 	struct ataparams *params;
   1146 {
   1147 	switch (ata_get_params(wd->drvp, flags, params)) {
   1148 	case CMD_AGAIN:
   1149 		return 1;
   1150 	case CMD_ERR:
   1151 		if ((wd->drvp->drive_flags & DRIVE_OLD) == 0)
   1152 			return 1;
   1153 		/*
   1154 		 * We `know' there's a drive here; just assume it's old.
   1155 		 * This geometry is only used to read the MBR and print a
   1156 		 * (false) attach message.
   1157 		 */
   1158 		strncpy(params->atap_model, "ST506",
   1159 		    sizeof params->atap_model);
   1160 		params->atap_config = ATA_CFG_FIXED;
   1161 		params->atap_cylinders = 1024;
   1162 		params->atap_heads = 8;
   1163 		params->atap_sectors = 17;
   1164 		params->atap_multi = 1;
   1165 		params->atap_capabilities1 = params->atap_capabilities2 = 0;
   1166 		return 0;
   1167 	case CMD_OK:
   1168 		return 0;
   1169 	default:
   1170 		panic("wd_get_params: bad return code from ata_get_params");
   1171 		/* NOTREACHED */
   1172 	}
   1173 }
   1174