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