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