Home | History | Annotate | Line # | Download | only in ata
wd.c revision 1.237
      1 /*	$NetBSD: wd.c,v 1.237 2003/01/27 18:21:29 thorpej Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1998, 2001 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 #include <sys/cdefs.h>
     69 __KERNEL_RCSID(0, "$NetBSD: wd.c,v 1.237 2003/01/27 18:21:29 thorpej Exp $");
     70 
     71 #ifndef WDCDEBUG
     72 #define WDCDEBUG
     73 #endif /* WDCDEBUG */
     74 
     75 #include "opt_bufq.h"
     76 #include "rnd.h"
     77 
     78 #include <sys/param.h>
     79 #include <sys/systm.h>
     80 #include <sys/kernel.h>
     81 #include <sys/conf.h>
     82 #include <sys/file.h>
     83 #include <sys/stat.h>
     84 #include <sys/ioctl.h>
     85 #include <sys/buf.h>
     86 #include <sys/uio.h>
     87 #include <sys/malloc.h>
     88 #include <sys/device.h>
     89 #include <sys/disklabel.h>
     90 #include <sys/disk.h>
     91 #include <sys/syslog.h>
     92 #include <sys/proc.h>
     93 #include <sys/vnode.h>
     94 #if NRND > 0
     95 #include <sys/rnd.h>
     96 #endif
     97 
     98 #include <machine/intr.h>
     99 #include <machine/bus.h>
    100 
    101 #define	__ATA_DISK_PRIVATE
    102 
    103 #include <dev/ata/atareg.h>
    104 #include <dev/ata/atavar.h>
    105 #include <dev/ata/wdvar.h>
    106 #include <dev/ic/wdcreg.h>
    107 #include <sys/ataio.h>
    108 #include "locators.h"
    109 
    110 #define	WAITTIME	(4 * hz)	/* time to wait for a completion */
    111 #define	WDIORETRIES_SINGLE 4	/* number of retries before single-sector */
    112 #define	WDIORETRIES	5	/* number of retries before giving up */
    113 #define	RECOVERYTIME hz/2	/* time to wait before retrying a cmd */
    114 
    115 #define	WDUNIT(dev)		DISKUNIT(dev)
    116 #define	WDPART(dev)		DISKPART(dev)
    117 #define	WDMINOR(unit, part)	DISKMINOR(unit, part)
    118 #define	MAKEWDDEV(maj, unit, part)	MAKEDISKDEV(maj, unit, part)
    119 
    120 #define	WDLABELDEV(dev)	(MAKEWDDEV(major(dev), WDUNIT(dev), RAW_PART))
    121 
    122 #define DEBUG_INTR   0x01
    123 #define DEBUG_XFERS  0x02
    124 #define DEBUG_STATUS 0x04
    125 #define DEBUG_FUNCS  0x08
    126 #define DEBUG_PROBE  0x10
    127 #ifdef WDCDEBUG
    128 extern int wdcdebug_wd_mask; /* init'ed in ata_wdc.c */
    129 #define WDCDEBUG_PRINT(args, level) \
    130 	if (wdcdebug_wd_mask & (level)) \
    131 		printf args
    132 #else
    133 #define WDCDEBUG_PRINT(args, level)
    134 #endif
    135 
    136 int	wdprobe		__P((struct device *, struct cfdata *, void *));
    137 void	wdattach	__P((struct device *, struct device *, void *));
    138 int	wddetach __P((struct device *, int));
    139 int	wdactivate __P((struct device *, enum devact));
    140 int	wdprint	__P((void *, char *));
    141 void	wdperror __P((const struct wd_softc *));
    142 
    143 CFATTACH_DECL(wd, sizeof(struct wd_softc),
    144     wdprobe, wdattach, wddetach, wdactivate);
    145 
    146 extern struct cfdriver wd_cd;
    147 
    148 dev_type_open(wdopen);
    149 dev_type_close(wdclose);
    150 dev_type_read(wdread);
    151 dev_type_write(wdwrite);
    152 dev_type_ioctl(wdioctl);
    153 dev_type_strategy(wdstrategy);
    154 dev_type_dump(wddump);
    155 dev_type_size(wdsize);
    156 
    157 const struct bdevsw wd_bdevsw = {
    158 	wdopen, wdclose, wdstrategy, wdioctl, wddump, wdsize, D_DISK
    159 };
    160 
    161 const struct cdevsw wd_cdevsw = {
    162 	wdopen, wdclose, wdread, wdwrite, wdioctl,
    163 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
    164 };
    165 
    166 /*
    167  * Glue necessary to hook WDCIOCCOMMAND into physio
    168  */
    169 
    170 struct wd_ioctl {
    171 	LIST_ENTRY(wd_ioctl) wi_list;
    172 	struct buf wi_bp;
    173 	struct uio wi_uio;
    174 	struct iovec wi_iov;
    175 	atareq_t wi_atareq;
    176 	struct wd_softc *wi_softc;
    177 };
    178 
    179 LIST_HEAD(, wd_ioctl) wi_head;
    180 
    181 struct	wd_ioctl *wi_find __P((struct buf *));
    182 void	wi_free __P((struct wd_ioctl *));
    183 struct	wd_ioctl *wi_get __P((void));
    184 void	wdioctlstrategy __P((struct buf *));
    185 
    186 void  wdgetdefaultlabel __P((struct wd_softc *, struct disklabel *));
    187 void  wdgetdisklabel	__P((struct wd_softc *));
    188 void  wdstart	__P((void *));
    189 void  __wdstart	__P((struct wd_softc*, struct buf *));
    190 void  wdrestart __P((void*));
    191 int   wd_get_params __P((struct wd_softc *, u_int8_t, struct ataparams *));
    192 void  wd_flushcache __P((struct wd_softc *, int));
    193 void  wd_shutdown __P((void*));
    194 
    195 struct dkdriver wddkdriver = { wdstrategy };
    196 
    197 #ifdef HAS_BAD144_HANDLING
    198 static void bad144intern __P((struct wd_softc *));
    199 #endif
    200 int	wdlock	__P((struct wd_softc *));
    201 void	wdunlock	__P((struct wd_softc *));
    202 
    203 int
    204 wdprobe(parent, match, aux)
    205 	struct device *parent;
    206 	struct cfdata *match;
    207 
    208 	void *aux;
    209 {
    210 	struct ata_device *adev = aux;
    211 
    212 	if (adev == NULL)
    213 		return 0;
    214 	if (adev->adev_bustype->bustype_type != SCSIPI_BUSTYPE_ATA)
    215 		return 0;
    216 
    217 	if (match->cf_loc[ATACF_CHANNEL] != ATACF_CHANNEL_DEFAULT &&
    218 	    match->cf_loc[ATACF_CHANNEL] != adev->adev_channel)
    219 		return 0;
    220 
    221 	if (match->cf_loc[ATACF_DRIVE] != ATACF_DRIVE_DEFAULT &&
    222 	    match->cf_loc[ATACF_DRIVE] != adev->adev_drv_data->drive)
    223 		return 0;
    224 	return 1;
    225 }
    226 
    227 void
    228 wdattach(parent, self, aux)
    229 	struct device *parent, *self;
    230 	void *aux;
    231 {
    232 	struct wd_softc *wd = (void *)self;
    233 	struct ata_device *adev= aux;
    234 	int i, blank;
    235 	char buf[41], pbuf[9], c, *p, *q;
    236 	WDCDEBUG_PRINT(("wdattach\n"), DEBUG_FUNCS | DEBUG_PROBE);
    237 
    238 	callout_init(&wd->sc_restart_ch);
    239 #ifdef NEW_BUFQ_STRATEGY
    240 	bufq_alloc(&wd->sc_q, BUFQ_READ_PRIO|BUFQ_SORT_RAWBLOCK);
    241 #else
    242 	bufq_alloc(&wd->sc_q, BUFQ_DISKSORT|BUFQ_SORT_RAWBLOCK);
    243 #endif
    244 
    245 	wd->atabus = adev->adev_bustype;
    246 	wd->openings = adev->adev_openings;
    247 	wd->drvp = adev->adev_drv_data;
    248 	wd->wdc_softc = parent;
    249 	/* give back our softc to our caller */
    250 	wd->drvp->drv_softc = &wd->sc_dev;
    251 
    252 	/* read our drive info */
    253 	if (wd_get_params(wd, AT_POLL, &wd->sc_params) != 0) {
    254 		printf("%s: IDENTIFY failed\n", wd->sc_dev.dv_xname);
    255 		return;
    256 	}
    257 
    258 	for (blank = 0, p = wd->sc_params.atap_model, q = buf, i = 0;
    259 	    i < sizeof(wd->sc_params.atap_model); i++) {
    260 		c = *p++;
    261 		if (c == '\0')
    262 			break;
    263 		if (c != ' ') {
    264 			if (blank) {
    265 				*q++ = ' ';
    266 				blank = 0;
    267 			}
    268 			*q++ = c;
    269 		} else
    270 			blank = 1;
    271 	}
    272 	*q++ = '\0';
    273 
    274 	printf(": <%s>\n", buf);
    275 
    276 	if ((wd->sc_params.atap_multi & 0xff) > 1) {
    277 		wd->sc_multi = wd->sc_params.atap_multi & 0xff;
    278 	} else {
    279 		wd->sc_multi = 1;
    280 	}
    281 
    282 	printf("%s: drive supports %d-sector PIO transfers,",
    283 	    wd->sc_dev.dv_xname, wd->sc_multi);
    284 
    285 	/* 48-bit LBA addressing */
    286 	if ((wd->sc_params.atap_cmd2_en & WDC_CAP_LBA48) != 0)
    287 		wd->sc_flags |= WDF_LBA48;
    288 
    289 	/* Prior to ATA-4, LBA was optional. */
    290 	if ((wd->sc_params.atap_capabilities1 & WDC_CAP_LBA) != 0)
    291 		wd->sc_flags |= WDF_LBA;
    292 #if 0
    293 	/* ATA-4 requires LBA. */
    294 	if (wd->sc_params.atap_ataversion != 0xffff &&
    295 	    wd->sc_params.atap_ataversion >= WDC_VER_ATA4)
    296 		wd->sc_flags |= WDF_LBA;
    297 #endif
    298 
    299 	if ((wd->sc_flags & WDF_LBA48) != 0) {
    300 		printf(" LBA48 addressing\n");
    301 		wd->sc_capacity =
    302 		    ((u_int64_t) wd->sc_params.__reserved6[11] << 48) |
    303 		    ((u_int64_t) wd->sc_params.__reserved6[10] << 32) |
    304 		    ((u_int64_t) wd->sc_params.__reserved6[9]  << 16) |
    305 		    ((u_int64_t) wd->sc_params.__reserved6[8]  << 0);
    306 	} else if ((wd->sc_flags & WDF_LBA) != 0) {
    307 		printf(" LBA addressing\n");
    308 		wd->sc_capacity =
    309 		    (wd->sc_params.atap_capacity[1] << 16) |
    310 		    wd->sc_params.atap_capacity[0];
    311 	} else {
    312 		printf(" chs addressing\n");
    313 		wd->sc_capacity =
    314 		    wd->sc_params.atap_cylinders *
    315 		    wd->sc_params.atap_heads *
    316 		    wd->sc_params.atap_sectors;
    317 	}
    318 	format_bytes(pbuf, sizeof(pbuf),
    319 	    (u_int64_t)wd->sc_capacity * DEV_BSIZE);
    320 	printf("%s: %s, %d cyl, %d head, %d sec, "
    321 	    "%d bytes/sect x %d sectors\n",
    322 	    self->dv_xname, pbuf, wd->sc_params.atap_cylinders,
    323 	    wd->sc_params.atap_heads, wd->sc_params.atap_sectors,
    324 	    DEV_BSIZE, wd->sc_capacity);
    325 
    326 	WDCDEBUG_PRINT(("%s: atap_dmatiming_mimi=%d, atap_dmatiming_recom=%d\n",
    327 	    self->dv_xname, wd->sc_params.atap_dmatiming_mimi,
    328 	    wd->sc_params.atap_dmatiming_recom), DEBUG_PROBE);
    329 	/*
    330 	 * Initialize and attach the disk structure.
    331 	 */
    332 	wd->sc_dk.dk_driver = &wddkdriver;
    333 	wd->sc_dk.dk_name = wd->sc_dev.dv_xname;
    334 	disk_attach(&wd->sc_dk);
    335 	wd->sc_wdc_bio.lp = wd->sc_dk.dk_label;
    336 	wd->sc_sdhook = shutdownhook_establish(wd_shutdown, wd);
    337 	if (wd->sc_sdhook == NULL)
    338 		printf("%s: WARNING: unable to establish shutdown hook\n",
    339 		    wd->sc_dev.dv_xname);
    340 #if NRND > 0
    341 	rnd_attach_source(&wd->rnd_source, wd->sc_dev.dv_xname,
    342 			  RND_TYPE_DISK, 0);
    343 #endif
    344 }
    345 
    346 int
    347 wdactivate(self, act)
    348 	struct device *self;
    349 	enum devact act;
    350 {
    351 	int rv = 0;
    352 
    353 	switch (act) {
    354 	case DVACT_ACTIVATE:
    355 		rv = EOPNOTSUPP;
    356 		break;
    357 
    358 	case DVACT_DEACTIVATE:
    359 		/*
    360 		 * Nothing to do; we key off the device's DVF_ACTIVATE.
    361 		 */
    362 		break;
    363 	}
    364 	return (rv);
    365 }
    366 
    367 int
    368 wddetach(self, flags)
    369 	struct device *self;
    370 	int flags;
    371 {
    372 	struct wd_softc *sc = (struct wd_softc *)self;
    373 	struct buf *bp;
    374 	int s, bmaj, cmaj, i, mn;
    375 
    376 	/* locate the major number */
    377 	bmaj = bdevsw_lookup_major(&wd_bdevsw);
    378 	cmaj = cdevsw_lookup_major(&wd_cdevsw);
    379 
    380 	s = splbio();
    381 
    382 	/* Kill off any queued buffers. */
    383 	while ((bp = BUFQ_GET(&sc->sc_q)) != NULL) {
    384 		bp->b_error = EIO;
    385 		bp->b_flags |= B_ERROR;
    386 		bp->b_resid = bp->b_bcount;
    387 		biodone(bp);
    388 	}
    389 
    390 	bufq_free(&sc->sc_q);
    391 
    392 	splx(s);
    393 
    394 	/* Nuke the vnodes for any open instances. */
    395 	for (i = 0; i < MAXPARTITIONS; i++) {
    396 		mn = WDMINOR(self->dv_unit, i);
    397 		vdevgone(bmaj, mn, mn, VBLK);
    398 		vdevgone(cmaj, mn, mn, VCHR);
    399 	}
    400 
    401 	/* Detach disk. */
    402 	disk_detach(&sc->sc_dk);
    403 
    404 	/* Get rid of the shutdown hook. */
    405 	if (sc->sc_sdhook != NULL)
    406 		shutdownhook_disestablish(sc->sc_sdhook);
    407 
    408 #if NRND > 0
    409 	/* Unhook the entropy source. */
    410 	rnd_detach_source(&sc->rnd_source);
    411 #endif
    412 
    413 	return (0);
    414 }
    415 
    416 /*
    417  * Read/write routine for a buffer.  Validates the arguments and schedules the
    418  * transfer.  Does not wait for the transfer to complete.
    419  */
    420 void
    421 wdstrategy(bp)
    422 	struct buf *bp;
    423 {
    424 	struct wd_softc *wd = device_lookup(&wd_cd, WDUNIT(bp->b_dev));
    425 	struct disklabel *lp = wd->sc_dk.dk_label;
    426 	daddr_t blkno;
    427 	int s;
    428 
    429 	WDCDEBUG_PRINT(("wdstrategy (%s)\n", wd->sc_dev.dv_xname),
    430 	    DEBUG_XFERS);
    431 
    432 	/* Valid request?  */
    433 	if (bp->b_blkno < 0 ||
    434 	    (bp->b_bcount % lp->d_secsize) != 0 ||
    435 	    (bp->b_bcount / lp->d_secsize) >= (1 << NBBY)) {
    436 		bp->b_error = EINVAL;
    437 		goto bad;
    438 	}
    439 
    440 	/* If device invalidated (e.g. media change, door open), error. */
    441 	if ((wd->sc_flags & WDF_LOADED) == 0) {
    442 		bp->b_error = EIO;
    443 		goto bad;
    444 	}
    445 
    446 	/* If it's a null transfer, return immediately. */
    447 	if (bp->b_bcount == 0)
    448 		goto done;
    449 
    450 	/*
    451 	 * Do bounds checking, adjust transfer. if error, process.
    452 	 * If end of partition, just return.
    453 	 */
    454 	if (bounds_check_with_label(bp, wd->sc_dk.dk_label,
    455 	    (wd->sc_flags & (WDF_WLABEL|WDF_LABELLING)) != 0) <= 0)
    456 		goto done;
    457 
    458 	/*
    459 	 * Now convert the block number to absolute and put it in
    460 	 * terms of the device's logical block size.
    461 	 */
    462 	if (lp->d_secsize >= DEV_BSIZE)
    463 		blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
    464 	else
    465 		blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
    466 
    467 	if (WDPART(bp->b_dev) != RAW_PART)
    468 		blkno += lp->d_partitions[WDPART(bp->b_dev)].p_offset;
    469 
    470 	bp->b_rawblkno = blkno;
    471 
    472 	/* Queue transfer on drive, activate drive and controller if idle. */
    473 	s = splbio();
    474 	BUFQ_PUT(&wd->sc_q, bp);
    475 	wdstart(wd);
    476 	splx(s);
    477 	return;
    478 bad:
    479 	bp->b_flags |= B_ERROR;
    480 done:
    481 	/* Toss transfer; we're done early. */
    482 	bp->b_resid = bp->b_bcount;
    483 	biodone(bp);
    484 }
    485 
    486 /*
    487  * Queue a drive for I/O.
    488  */
    489 void
    490 wdstart(arg)
    491 	void *arg;
    492 {
    493 	struct wd_softc *wd = arg;
    494 	struct buf *bp = NULL;
    495 
    496 	WDCDEBUG_PRINT(("wdstart %s\n", wd->sc_dev.dv_xname),
    497 	    DEBUG_XFERS);
    498 	while (wd->openings > 0) {
    499 
    500 		/* Is there a buf for us ? */
    501 		if ((bp = BUFQ_GET(&wd->sc_q)) == NULL)
    502 			return;
    503 
    504 		/*
    505 		 * Make the command. First lock the device
    506 		 */
    507 		wd->openings--;
    508 
    509 		wd->retries = 0;
    510 		__wdstart(wd, bp);
    511 	}
    512 }
    513 
    514 void
    515 __wdstart(wd, bp)
    516 	struct wd_softc *wd;
    517 	struct buf *bp;
    518 {
    519 
    520 	wd->sc_wdc_bio.blkno = bp->b_rawblkno;
    521 	wd->sc_wdc_bio.blkdone =0;
    522 	wd->sc_bp = bp;
    523 	/*
    524 	 * If we're retrying, retry in single-sector mode. This will give us
    525 	 * the sector number of the problem, and will eventually allow the
    526 	 * transfer to succeed.
    527 	 */
    528 	if (wd->sc_multi == 1 || wd->retries >= WDIORETRIES_SINGLE)
    529 		wd->sc_wdc_bio.flags = ATA_SINGLE;
    530 	else
    531 		wd->sc_wdc_bio.flags = 0;
    532 	if (wd->sc_flags & WDF_LBA48)
    533 		wd->sc_wdc_bio.flags |= ATA_LBA48;
    534 	if (wd->sc_flags & WDF_LBA)
    535 		wd->sc_wdc_bio.flags |= ATA_LBA;
    536 	if (bp->b_flags & B_READ)
    537 		wd->sc_wdc_bio.flags |= ATA_READ;
    538 	wd->sc_wdc_bio.bcount = bp->b_bcount;
    539 	wd->sc_wdc_bio.databuf = bp->b_data;
    540 	/* Instrumentation. */
    541 	disk_busy(&wd->sc_dk);
    542 	switch (wd->atabus->ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
    543 	case WDC_TRY_AGAIN:
    544 		callout_reset(&wd->sc_restart_ch, hz, wdrestart, wd);
    545 		break;
    546 	case WDC_QUEUED:
    547 	case WDC_COMPLETE:
    548 		break;
    549 	default:
    550 		panic("__wdstart: bad return code from ata_bio()");
    551 	}
    552 }
    553 
    554 void
    555 wddone(v)
    556 	void *v;
    557 {
    558 	struct wd_softc *wd = v;
    559 	struct buf *bp = wd->sc_bp;
    560 	const char *errmsg;
    561 	int do_perror = 0;
    562 	WDCDEBUG_PRINT(("wddone %s\n", wd->sc_dev.dv_xname),
    563 	    DEBUG_XFERS);
    564 
    565 	if (bp == NULL)
    566 		return;
    567 	bp->b_resid = wd->sc_wdc_bio.bcount;
    568 	switch (wd->sc_wdc_bio.error) {
    569 	case ERR_DMA:
    570 		errmsg = "DMA error";
    571 		goto retry;
    572 	case ERR_DF:
    573 		errmsg = "device fault";
    574 		goto retry;
    575 	case TIMEOUT:
    576 		errmsg = "device timeout";
    577 		goto retry;
    578 	case ERROR:
    579 		/* Don't care about media change bits */
    580 		if (wd->sc_wdc_bio.r_error != 0 &&
    581 		    (wd->sc_wdc_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0)
    582 			goto noerror;
    583 		errmsg = "error";
    584 		do_perror = 1;
    585 retry:		/* Just reset and retry. Can we do more ? */
    586 		wd->atabus->ata_reset_channel(wd->drvp);
    587 		diskerr(bp, "wd", errmsg, LOG_PRINTF,
    588 		    wd->sc_wdc_bio.blkdone, wd->sc_dk.dk_label);
    589 		if (wd->retries < WDIORETRIES)
    590 			printf(", retrying\n");
    591 		if (do_perror)
    592 			wdperror(wd);
    593 		if (wd->retries < WDIORETRIES) {
    594 			wd->retries++;
    595 			callout_reset(&wd->sc_restart_ch, RECOVERYTIME,
    596 			    wdrestart, wd);
    597 			return;
    598 		}
    599 		printf("\n");
    600 		bp->b_flags |= B_ERROR;
    601 		bp->b_error = EIO;
    602 		break;
    603 	case NOERROR:
    604 noerror:	if ((wd->sc_wdc_bio.flags & ATA_CORR) || wd->retries > 0)
    605 			printf("%s: soft error (corrected)\n",
    606 			    wd->sc_dev.dv_xname);
    607 		break;
    608 	case ERR_NODEV:
    609 		bp->b_flags |= B_ERROR;
    610 		bp->b_error = EIO;
    611 		break;
    612 	}
    613 	disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid),
    614 	    (bp->b_flags & B_READ));
    615 #if NRND > 0
    616 	rnd_add_uint32(&wd->rnd_source, bp->b_blkno);
    617 #endif
    618 	biodone(bp);
    619 	wd->openings++;
    620 	wdstart(wd);
    621 }
    622 
    623 void
    624 wdrestart(v)
    625 	void *v;
    626 {
    627 	struct wd_softc *wd = v;
    628 	struct buf *bp = wd->sc_bp;
    629 	int s;
    630 	WDCDEBUG_PRINT(("wdrestart %s\n", wd->sc_dev.dv_xname),
    631 	    DEBUG_XFERS);
    632 
    633 	s = splbio();
    634 	__wdstart(v, bp);
    635 	splx(s);
    636 }
    637 
    638 int
    639 wdread(dev, uio, flags)
    640 	dev_t dev;
    641 	struct uio *uio;
    642 	int flags;
    643 {
    644 
    645 	WDCDEBUG_PRINT(("wdread\n"), DEBUG_XFERS);
    646 	return (physio(wdstrategy, NULL, dev, B_READ, minphys, uio));
    647 }
    648 
    649 int
    650 wdwrite(dev, uio, flags)
    651 	dev_t dev;
    652 	struct uio *uio;
    653 	int flags;
    654 {
    655 
    656 	WDCDEBUG_PRINT(("wdwrite\n"), DEBUG_XFERS);
    657 	return (physio(wdstrategy, NULL, dev, B_WRITE, minphys, uio));
    658 }
    659 
    660 /*
    661  * Wait interruptibly for an exclusive lock.
    662  *
    663  * XXX
    664  * Several drivers do this; it should be abstracted and made MP-safe.
    665  */
    666 int
    667 wdlock(wd)
    668 	struct wd_softc *wd;
    669 {
    670 	int error;
    671 	int s;
    672 
    673 	WDCDEBUG_PRINT(("wdlock\n"), DEBUG_FUNCS);
    674 
    675 	s = splbio();
    676 
    677 	while ((wd->sc_flags & WDF_LOCKED) != 0) {
    678 		wd->sc_flags |= WDF_WANTED;
    679 		if ((error = tsleep(wd, PRIBIO | PCATCH,
    680 		    "wdlck", 0)) != 0) {
    681 			splx(s);
    682 			return error;
    683 		}
    684 	}
    685 	wd->sc_flags |= WDF_LOCKED;
    686 	splx(s);
    687 	return 0;
    688 }
    689 
    690 /*
    691  * Unlock and wake up any waiters.
    692  */
    693 void
    694 wdunlock(wd)
    695 	struct wd_softc *wd;
    696 {
    697 
    698 	WDCDEBUG_PRINT(("wdunlock\n"), DEBUG_FUNCS);
    699 
    700 	wd->sc_flags &= ~WDF_LOCKED;
    701 	if ((wd->sc_flags & WDF_WANTED) != 0) {
    702 		wd->sc_flags &= ~WDF_WANTED;
    703 		wakeup(wd);
    704 	}
    705 }
    706 
    707 int
    708 wdopen(dev, flag, fmt, p)
    709 	dev_t dev;
    710 	int flag, fmt;
    711 	struct proc *p;
    712 {
    713 	struct wd_softc *wd;
    714 	int part, error;
    715 
    716 	WDCDEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS);
    717 	wd = device_lookup(&wd_cd, WDUNIT(dev));
    718 	if (wd == NULL)
    719 		return (ENXIO);
    720 
    721 	/*
    722 	 * If this is the first open of this device, add a reference
    723 	 * to the adapter.
    724 	 */
    725 	if (wd->sc_dk.dk_openmask == 0 &&
    726 	    (error = wd->atabus->ata_addref(wd->drvp)) != 0)
    727 		return (error);
    728 
    729 	if ((error = wdlock(wd)) != 0)
    730 		goto bad4;
    731 
    732 	if (wd->sc_dk.dk_openmask != 0) {
    733 		/*
    734 		 * If any partition is open, but the disk has been invalidated,
    735 		 * disallow further opens.
    736 		 */
    737 		if ((wd->sc_flags & WDF_LOADED) == 0) {
    738 			error = EIO;
    739 			goto bad3;
    740 		}
    741 	} else {
    742 		if ((wd->sc_flags & WDF_LOADED) == 0) {
    743 			wd->sc_flags |= WDF_LOADED;
    744 
    745 			/* Load the physical device parameters. */
    746 			wd_get_params(wd, AT_WAIT, &wd->sc_params);
    747 
    748 			/* Load the partition info if not already loaded. */
    749 			wdgetdisklabel(wd);
    750 		}
    751 	}
    752 
    753 	part = WDPART(dev);
    754 
    755 	/* Check that the partition exists. */
    756 	if (part != RAW_PART &&
    757 	    (part >= wd->sc_dk.dk_label->d_npartitions ||
    758 	     wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
    759 		error = ENXIO;
    760 		goto bad;
    761 	}
    762 
    763 	/* Insure only one open at a time. */
    764 	switch (fmt) {
    765 	case S_IFCHR:
    766 		wd->sc_dk.dk_copenmask |= (1 << part);
    767 		break;
    768 	case S_IFBLK:
    769 		wd->sc_dk.dk_bopenmask |= (1 << part);
    770 		break;
    771 	}
    772 	wd->sc_dk.dk_openmask =
    773 	    wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
    774 
    775 	wdunlock(wd);
    776 	return 0;
    777 
    778 bad:
    779 	if (wd->sc_dk.dk_openmask == 0) {
    780 	}
    781 
    782 bad3:
    783 	wdunlock(wd);
    784 bad4:
    785 	if (wd->sc_dk.dk_openmask == 0)
    786 		wd->atabus->ata_delref(wd->drvp);
    787 	return error;
    788 }
    789 
    790 int
    791 wdclose(dev, flag, fmt, p)
    792 	dev_t dev;
    793 	int flag, fmt;
    794 	struct proc *p;
    795 {
    796 	struct wd_softc *wd = device_lookup(&wd_cd, WDUNIT(dev));
    797 	int part = WDPART(dev);
    798 	int error;
    799 
    800 	WDCDEBUG_PRINT(("wdclose\n"), DEBUG_FUNCS);
    801 	if ((error = wdlock(wd)) != 0)
    802 		return error;
    803 
    804 	switch (fmt) {
    805 	case S_IFCHR:
    806 		wd->sc_dk.dk_copenmask &= ~(1 << part);
    807 		break;
    808 	case S_IFBLK:
    809 		wd->sc_dk.dk_bopenmask &= ~(1 << part);
    810 		break;
    811 	}
    812 	wd->sc_dk.dk_openmask =
    813 	    wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
    814 
    815 	if (wd->sc_dk.dk_openmask == 0) {
    816 		wd_flushcache(wd, AT_WAIT);
    817 		/* XXXX Must wait for I/O to complete! */
    818 
    819 		if (! (wd->sc_flags & WDF_KLABEL))
    820 			wd->sc_flags &= ~WDF_LOADED;
    821 
    822 		wd->atabus->ata_delref(wd->drvp);
    823 	}
    824 
    825 	wdunlock(wd);
    826 	return 0;
    827 }
    828 
    829 void
    830 wdgetdefaultlabel(wd, lp)
    831 	struct wd_softc *wd;
    832 	struct disklabel *lp;
    833 {
    834 
    835 	WDCDEBUG_PRINT(("wdgetdefaultlabel\n"), DEBUG_FUNCS);
    836 	memset(lp, 0, sizeof(struct disklabel));
    837 
    838 	lp->d_secsize = DEV_BSIZE;
    839 	lp->d_ntracks = wd->sc_params.atap_heads;
    840 	lp->d_nsectors = wd->sc_params.atap_sectors;
    841 	lp->d_ncylinders = wd->sc_params.atap_cylinders;
    842 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    843 
    844 	if (strcmp(wd->sc_params.atap_model, "ST506") == 0)
    845 		lp->d_type = DTYPE_ST506;
    846 	else
    847 		lp->d_type = DTYPE_ESDI;
    848 
    849 	strncpy(lp->d_typename, wd->sc_params.atap_model, 16);
    850 	strncpy(lp->d_packname, "fictitious", 16);
    851 	lp->d_secperunit = wd->sc_capacity;
    852 	lp->d_rpm = 3600;
    853 	lp->d_interleave = 1;
    854 	lp->d_flags = 0;
    855 
    856 	lp->d_partitions[RAW_PART].p_offset = 0;
    857 	lp->d_partitions[RAW_PART].p_size =
    858 	lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
    859 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
    860 	lp->d_npartitions = RAW_PART + 1;
    861 
    862 	lp->d_magic = DISKMAGIC;
    863 	lp->d_magic2 = DISKMAGIC;
    864 	lp->d_checksum = dkcksum(lp);
    865 }
    866 
    867 /*
    868  * Fabricate a default disk label, and try to read the correct one.
    869  */
    870 void
    871 wdgetdisklabel(wd)
    872 	struct wd_softc *wd;
    873 {
    874 	struct disklabel *lp = wd->sc_dk.dk_label;
    875 	char *errstring;
    876 
    877 	WDCDEBUG_PRINT(("wdgetdisklabel\n"), DEBUG_FUNCS);
    878 
    879 	memset(wd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
    880 
    881 	wdgetdefaultlabel(wd, lp);
    882 
    883 	wd->sc_badsect[0] = -1;
    884 
    885 	if (wd->drvp->state > RECAL)
    886 		wd->drvp->drive_flags |= DRIVE_RESET;
    887 	errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
    888 	    wdstrategy, lp, wd->sc_dk.dk_cpulabel);
    889 	if (errstring) {
    890 		/*
    891 		 * This probably happened because the drive's default
    892 		 * geometry doesn't match the DOS geometry.  We
    893 		 * assume the DOS geometry is now in the label and try
    894 		 * again.  XXX This is a kluge.
    895 		 */
    896 		if (wd->drvp->state > RECAL)
    897 			wd->drvp->drive_flags |= DRIVE_RESET;
    898 		errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit,
    899 		    RAW_PART), wdstrategy, lp, wd->sc_dk.dk_cpulabel);
    900 	}
    901 	if (errstring) {
    902 		printf("%s: %s\n", wd->sc_dev.dv_xname, errstring);
    903 		return;
    904 	}
    905 
    906 	if (wd->drvp->state > RECAL)
    907 		wd->drvp->drive_flags |= DRIVE_RESET;
    908 #ifdef HAS_BAD144_HANDLING
    909 	if ((lp->d_flags & D_BADSECT) != 0)
    910 		bad144intern(wd);
    911 #endif
    912 }
    913 
    914 void
    915 wdperror(wd)
    916 	const struct wd_softc *wd;
    917 {
    918 	static const char *const errstr0_3[] = {"address mark not found",
    919 	    "track 0 not found", "aborted command", "media change requested",
    920 	    "id not found", "media changed", "uncorrectable data error",
    921 	    "bad block detected"};
    922 	static const char *const errstr4_5[] = {
    923 	    "obsolete (address mark not found)",
    924 	    "no media/write protected", "aborted command",
    925 	    "media change requested", "id not found", "media changed",
    926 	    "uncorrectable data error", "interface CRC error"};
    927 	const char *const *errstr;
    928 	int i;
    929 	char *sep = "";
    930 
    931 	const char *devname = wd->sc_dev.dv_xname;
    932 	struct ata_drive_datas *drvp = wd->drvp;
    933 	int errno = wd->sc_wdc_bio.r_error;
    934 
    935 	if (drvp->ata_vers >= 4)
    936 		errstr = errstr4_5;
    937 	else
    938 		errstr = errstr0_3;
    939 
    940 	printf("%s: (", devname);
    941 
    942 	if (errno == 0)
    943 		printf("error not notified");
    944 
    945 	for (i = 0; i < 8; i++) {
    946 		if (errno & (1 << i)) {
    947 			printf("%s%s", sep, errstr[i]);
    948 			sep = ", ";
    949 		}
    950 	}
    951 	printf(")\n");
    952 }
    953 
    954 int
    955 wdioctl(dev, xfer, addr, flag, p)
    956 	dev_t dev;
    957 	u_long xfer;
    958 	caddr_t addr;
    959 	int flag;
    960 	struct proc *p;
    961 {
    962 	struct wd_softc *wd = device_lookup(&wd_cd, WDUNIT(dev));
    963 	int error = 0;
    964 #ifdef __HAVE_OLD_DISKLABEL
    965 	struct disklabel *newlabel = NULL;
    966 #endif
    967 
    968 	WDCDEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS);
    969 
    970 	if ((wd->sc_flags & WDF_LOADED) == 0)
    971 		return EIO;
    972 
    973 	switch (xfer) {
    974 #ifdef HAS_BAD144_HANDLING
    975 	case DIOCSBAD:
    976 		if ((flag & FWRITE) == 0)
    977 			return EBADF;
    978 		wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
    979 		wd->sc_dk.dk_label->d_flags |= D_BADSECT;
    980 		bad144intern(wd);
    981 		return 0;
    982 #endif
    983 
    984 	case DIOCGDINFO:
    985 		*(struct disklabel *)addr = *(wd->sc_dk.dk_label);
    986 		return 0;
    987 #ifdef __HAVE_OLD_DISKLABEL
    988 	case ODIOCGDINFO:
    989 		newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
    990 		if (newlabel == NULL)
    991 			return EIO;
    992 		*newlabel = *(wd->sc_dk.dk_label);
    993 		if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
    994 			memcpy(addr, newlabel, sizeof (struct olddisklabel));
    995 		else
    996 			error = ENOTTY;
    997 		free(newlabel, M_TEMP);
    998 		return error;
    999 #endif
   1000 
   1001 	case DIOCGPART:
   1002 		((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
   1003 		((struct partinfo *)addr)->part =
   1004 		    &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
   1005 		return 0;
   1006 
   1007 	case DIOCWDINFO:
   1008 	case DIOCSDINFO:
   1009 #ifdef __HAVE_OLD_DISKLABEL
   1010 	case ODIOCWDINFO:
   1011 	case ODIOCSDINFO:
   1012 #endif
   1013 	{
   1014 		struct disklabel *lp;
   1015 
   1016 		if ((flag & FWRITE) == 0)
   1017 			return EBADF;
   1018 
   1019 #ifdef __HAVE_OLD_DISKLABEL
   1020 		if (xfer == ODIOCSDINFO || xfer == ODIOCWDINFO) {
   1021 			newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
   1022 			if (newlabel == NULL)
   1023 				return EIO;
   1024 			memset(newlabel, 0, sizeof newlabel);
   1025 			memcpy(newlabel, addr, sizeof (struct olddisklabel));
   1026 			lp = newlabel;
   1027 		} else
   1028 #endif
   1029 		lp = (struct disklabel *)addr;
   1030 
   1031 		if ((error = wdlock(wd)) != 0)
   1032 			goto bad;
   1033 		wd->sc_flags |= WDF_LABELLING;
   1034 
   1035 		error = setdisklabel(wd->sc_dk.dk_label,
   1036 		    lp, /*wd->sc_dk.dk_openmask : */0,
   1037 		    wd->sc_dk.dk_cpulabel);
   1038 		if (error == 0) {
   1039 			if (wd->drvp->state > RECAL)
   1040 				wd->drvp->drive_flags |= DRIVE_RESET;
   1041 			if (xfer == DIOCWDINFO
   1042 #ifdef __HAVE_OLD_DISKLABEL
   1043 			    || xfer == ODIOCWDINFO
   1044 #endif
   1045 			    )
   1046 				error = writedisklabel(WDLABELDEV(dev),
   1047 				    wdstrategy, wd->sc_dk.dk_label,
   1048 				    wd->sc_dk.dk_cpulabel);
   1049 		}
   1050 
   1051 		wd->sc_flags &= ~WDF_LABELLING;
   1052 		wdunlock(wd);
   1053 bad:
   1054 #ifdef __HAVE_OLD_DISKLABEL
   1055 		if (newlabel != NULL)
   1056 			free(newlabel, M_TEMP);
   1057 #endif
   1058 		return error;
   1059 	}
   1060 
   1061 	case DIOCKLABEL:
   1062 		if (*(int *)addr)
   1063 			wd->sc_flags |= WDF_KLABEL;
   1064 		else
   1065 			wd->sc_flags &= ~WDF_KLABEL;
   1066 		return 0;
   1067 
   1068 	case DIOCWLABEL:
   1069 		if ((flag & FWRITE) == 0)
   1070 			return EBADF;
   1071 		if (*(int *)addr)
   1072 			wd->sc_flags |= WDF_WLABEL;
   1073 		else
   1074 			wd->sc_flags &= ~WDF_WLABEL;
   1075 		return 0;
   1076 
   1077 	case DIOCGDEFLABEL:
   1078 		wdgetdefaultlabel(wd, (struct disklabel *)addr);
   1079 		return 0;
   1080 #ifdef __HAVE_OLD_DISKLABEL
   1081 	case ODIOCGDEFLABEL:
   1082 		newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
   1083 		if (newlabel == NULL)
   1084 			return EIO;
   1085 		wdgetdefaultlabel(wd, newlabel);
   1086 		if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
   1087 			memcpy(addr, &newlabel, sizeof (struct olddisklabel));
   1088 		else
   1089 			error = ENOTTY;
   1090 		free(newlabel, M_TEMP);
   1091 		return error;
   1092 #endif
   1093 
   1094 #ifdef notyet
   1095 	case DIOCWFORMAT:
   1096 		if ((flag & FWRITE) == 0)
   1097 			return EBADF;
   1098 		{
   1099 		register struct format_op *fop;
   1100 		struct iovec aiov;
   1101 		struct uio auio;
   1102 
   1103 		fop = (struct format_op *)addr;
   1104 		aiov.iov_base = fop->df_buf;
   1105 		aiov.iov_len = fop->df_count;
   1106 		auio.uio_iov = &aiov;
   1107 		auio.uio_iovcnt = 1;
   1108 		auio.uio_resid = fop->df_count;
   1109 		auio.uio_segflg = 0;
   1110 		auio.uio_offset =
   1111 			fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
   1112 		auio.uio_procp = p;
   1113 		error = physio(wdformat, NULL, dev, B_WRITE, minphys,
   1114 		    &auio);
   1115 		fop->df_count -= auio.uio_resid;
   1116 		fop->df_reg[0] = wdc->sc_status;
   1117 		fop->df_reg[1] = wdc->sc_error;
   1118 		return error;
   1119 		}
   1120 #endif
   1121 
   1122 	case ATAIOCCOMMAND:
   1123 		/*
   1124 		 * Make sure this command is (relatively) safe first
   1125 		 */
   1126 		if ((((atareq_t *) addr)->flags & ATACMD_READ) == 0 &&
   1127 		    (flag & FWRITE) == 0)
   1128 			return (EBADF);
   1129 		{
   1130 		struct wd_ioctl *wi;
   1131 		atareq_t *atareq = (atareq_t *) addr;
   1132 		int error;
   1133 
   1134 		wi = wi_get();
   1135 		wi->wi_softc = wd;
   1136 		wi->wi_atareq = *atareq;
   1137 
   1138 		if (atareq->datalen && atareq->flags &
   1139 		    (ATACMD_READ | ATACMD_WRITE)) {
   1140 			wi->wi_iov.iov_base = atareq->databuf;
   1141 			wi->wi_iov.iov_len = atareq->datalen;
   1142 			wi->wi_uio.uio_iov = &wi->wi_iov;
   1143 			wi->wi_uio.uio_iovcnt = 1;
   1144 			wi->wi_uio.uio_resid = atareq->datalen;
   1145 			wi->wi_uio.uio_offset = 0;
   1146 			wi->wi_uio.uio_segflg = UIO_USERSPACE;
   1147 			wi->wi_uio.uio_rw =
   1148 			    (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE;
   1149 			wi->wi_uio.uio_procp = p;
   1150 			error = physio(wdioctlstrategy, &wi->wi_bp, dev,
   1151 			    (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE,
   1152 			    minphys, &wi->wi_uio);
   1153 		} else {
   1154 			/* No need to call physio if we don't have any
   1155 			   user data */
   1156 			wi->wi_bp.b_flags = 0;
   1157 			wi->wi_bp.b_data = 0;
   1158 			wi->wi_bp.b_bcount = 0;
   1159 			wi->wi_bp.b_dev = 0;
   1160 			wi->wi_bp.b_proc = p;
   1161 			wdioctlstrategy(&wi->wi_bp);
   1162 			error = wi->wi_bp.b_error;
   1163 		}
   1164 		*atareq = wi->wi_atareq;
   1165 		wi_free(wi);
   1166 		return(error);
   1167 		}
   1168 
   1169 	default:
   1170 		return ENOTTY;
   1171 	}
   1172 
   1173 #ifdef DIAGNOSTIC
   1174 	panic("wdioctl: impossible");
   1175 #endif
   1176 }
   1177 
   1178 #ifdef B_FORMAT
   1179 int
   1180 wdformat(struct buf *bp)
   1181 {
   1182 
   1183 	bp->b_flags |= B_FORMAT;
   1184 	return wdstrategy(bp);
   1185 }
   1186 #endif
   1187 
   1188 int
   1189 wdsize(dev)
   1190 	dev_t dev;
   1191 {
   1192 	struct wd_softc *wd;
   1193 	int part, omask;
   1194 	int size;
   1195 
   1196 	WDCDEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS);
   1197 
   1198 	wd = device_lookup(&wd_cd, WDUNIT(dev));
   1199 	if (wd == NULL)
   1200 		return (-1);
   1201 
   1202 	part = WDPART(dev);
   1203 	omask = wd->sc_dk.dk_openmask & (1 << part);
   1204 
   1205 	if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0)
   1206 		return (-1);
   1207 	if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
   1208 		size = -1;
   1209 	else
   1210 		size = wd->sc_dk.dk_label->d_partitions[part].p_size *
   1211 		    (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
   1212 	if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0)
   1213 		return (-1);
   1214 	return (size);
   1215 }
   1216 
   1217 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
   1218 static int wddoingadump = 0;
   1219 static int wddumprecalibrated = 0;
   1220 static int wddumpmulti = 1;
   1221 
   1222 /*
   1223  * Dump core after a system crash.
   1224  */
   1225 int
   1226 wddump(dev, blkno, va, size)
   1227 	dev_t dev;
   1228 	daddr_t blkno;
   1229 	caddr_t va;
   1230 	size_t size;
   1231 {
   1232 	struct wd_softc *wd;	/* disk unit to do the I/O */
   1233 	struct disklabel *lp;   /* disk's disklabel */
   1234 	int part, err;
   1235 	int nblks;	/* total number of sectors left to write */
   1236 
   1237 	/* Check if recursive dump; if so, punt. */
   1238 	if (wddoingadump)
   1239 		return EFAULT;
   1240 	wddoingadump = 1;
   1241 
   1242 	wd = device_lookup(&wd_cd, WDUNIT(dev));
   1243 	if (wd == NULL)
   1244 		return (ENXIO);
   1245 
   1246 	part = WDPART(dev);
   1247 
   1248 	/* Convert to disk sectors.  Request must be a multiple of size. */
   1249 	lp = wd->sc_dk.dk_label;
   1250 	if ((size % lp->d_secsize) != 0)
   1251 		return EFAULT;
   1252 	nblks = size / lp->d_secsize;
   1253 	blkno = blkno / (lp->d_secsize / DEV_BSIZE);
   1254 
   1255 	/* Check transfer bounds against partition size. */
   1256 	if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
   1257 		return EINVAL;
   1258 
   1259 	/* Offset block number to start of partition. */
   1260 	blkno += lp->d_partitions[part].p_offset;
   1261 
   1262 	/* Recalibrate, if first dump transfer. */
   1263 	if (wddumprecalibrated == 0) {
   1264 		wddumpmulti = wd->sc_multi;
   1265 		wddumprecalibrated = 1;
   1266 		wd->drvp->state = RESET;
   1267 	}
   1268 
   1269 	while (nblks > 0) {
   1270 again:
   1271 		wd->sc_bp = NULL;
   1272 		wd->sc_wdc_bio.blkno = blkno;
   1273 		wd->sc_wdc_bio.flags = ATA_POLL;
   1274 		if (wddumpmulti == 1)
   1275 			wd->sc_wdc_bio.flags |= ATA_SINGLE;
   1276 		if (wd->sc_flags & WDF_LBA48)
   1277 			wd->sc_wdc_bio.flags |= ATA_LBA48;
   1278 		if (wd->sc_flags & WDF_LBA)
   1279 			wd->sc_wdc_bio.flags |= ATA_LBA;
   1280 		wd->sc_wdc_bio.bcount =
   1281 			min(nblks, wddumpmulti) * lp->d_secsize;
   1282 		wd->sc_wdc_bio.databuf = va;
   1283 #ifndef WD_DUMP_NOT_TRUSTED
   1284 		switch (wd->atabus->ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
   1285 		case WDC_TRY_AGAIN:
   1286 			panic("wddump: try again");
   1287 			break;
   1288 		case WDC_QUEUED:
   1289 			panic("wddump: polled command has been queued");
   1290 			break;
   1291 		case WDC_COMPLETE:
   1292 			break;
   1293 		}
   1294 		switch(wd->sc_wdc_bio.error) {
   1295 		case TIMEOUT:
   1296 			printf("wddump: device timed out");
   1297 			err = EIO;
   1298 			break;
   1299 		case ERR_DF:
   1300 			printf("wddump: drive fault");
   1301 			err = EIO;
   1302 			break;
   1303 		case ERR_DMA:
   1304 			printf("wddump: DMA error");
   1305 			err = EIO;
   1306 			break;
   1307 		case ERROR:
   1308 			printf("wddump: ");
   1309 			wdperror(wd);
   1310 			err = EIO;
   1311 			break;
   1312 		case NOERROR:
   1313 			err = 0;
   1314 			break;
   1315 		default:
   1316 			panic("wddump: unknown error type");
   1317 		}
   1318 		if (err != 0) {
   1319 			if (wddumpmulti != 1) {
   1320 				wddumpmulti = 1; /* retry in single-sector */
   1321 				printf(", retrying\n");
   1322 				goto again;
   1323 			}
   1324 			printf("\n");
   1325 			return err;
   1326 		}
   1327 #else	/* WD_DUMP_NOT_TRUSTED */
   1328 		/* Let's just talk about this first... */
   1329 		printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
   1330 		    unit, va, cylin, head, sector);
   1331 		delay(500 * 1000);	/* half a second */
   1332 #endif
   1333 
   1334 		/* update block count */
   1335 		nblks -= min(nblks, wddumpmulti);
   1336 		blkno += min(nblks, wddumpmulti);
   1337 		va += min(nblks, wddumpmulti) * lp->d_secsize;
   1338 	}
   1339 
   1340 	wddoingadump = 0;
   1341 	return 0;
   1342 }
   1343 
   1344 #ifdef HAS_BAD144_HANDLING
   1345 /*
   1346  * Internalize the bad sector table.
   1347  */
   1348 void
   1349 bad144intern(wd)
   1350 	struct wd_softc *wd;
   1351 {
   1352 	struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
   1353 	struct disklabel *lp = wd->sc_dk.dk_label;
   1354 	int i = 0;
   1355 
   1356 	WDCDEBUG_PRINT(("bad144intern\n"), DEBUG_XFERS);
   1357 
   1358 	for (; i < NBT_BAD; i++) {
   1359 		if (bt->bt_bad[i].bt_cyl == 0xffff)
   1360 			break;
   1361 		wd->sc_badsect[i] =
   1362 		    bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
   1363 		    (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
   1364 		    (bt->bt_bad[i].bt_trksec & 0xff);
   1365 	}
   1366 	for (; i < NBT_BAD+1; i++)
   1367 		wd->sc_badsect[i] = -1;
   1368 }
   1369 #endif
   1370 
   1371 int
   1372 wd_get_params(wd, flags, params)
   1373 	struct wd_softc *wd;
   1374 	u_int8_t flags;
   1375 	struct ataparams *params;
   1376 {
   1377 	switch (wd->atabus->ata_get_params(wd->drvp, flags, params)) {
   1378 	case CMD_AGAIN:
   1379 		return 1;
   1380 	case CMD_ERR:
   1381 		/*
   1382 		 * We `know' there's a drive here; just assume it's old.
   1383 		 * This geometry is only used to read the MBR and print a
   1384 		 * (false) attach message.
   1385 		 */
   1386 		strncpy(params->atap_model, "ST506",
   1387 		    sizeof params->atap_model);
   1388 		params->atap_config = ATA_CFG_FIXED;
   1389 		params->atap_cylinders = 1024;
   1390 		params->atap_heads = 8;
   1391 		params->atap_sectors = 17;
   1392 		params->atap_multi = 1;
   1393 		params->atap_capabilities1 = params->atap_capabilities2 = 0;
   1394 		wd->drvp->ata_vers = -1; /* Mark it as pre-ATA */
   1395 		return 0;
   1396 	case CMD_OK:
   1397 		return 0;
   1398 	default:
   1399 		panic("wd_get_params: bad return code from ata_get_params");
   1400 		/* NOTREACHED */
   1401 	}
   1402 }
   1403 
   1404 void
   1405 wd_flushcache(wd, flags)
   1406 	struct wd_softc *wd;
   1407 	int flags;
   1408 {
   1409 	struct wdc_command wdc_c;
   1410 
   1411 	if (wd->drvp->ata_vers < 4) /* WDCC_FLUSHCACHE is here since ATA-4 */
   1412 		return;
   1413 	memset(&wdc_c, 0, sizeof(struct wdc_command));
   1414 	wdc_c.r_command = WDCC_FLUSHCACHE;
   1415 	wdc_c.r_st_bmask = WDCS_DRDY;
   1416 	wdc_c.r_st_pmask = WDCS_DRDY;
   1417 	wdc_c.flags = flags;
   1418 	wdc_c.timeout = 30000; /* 30s timeout */
   1419 	if (wd->atabus->ata_exec_command(wd->drvp, &wdc_c) != WDC_COMPLETE) {
   1420 		printf("%s: flush cache command didn't complete\n",
   1421 		    wd->sc_dev.dv_xname);
   1422 	}
   1423 	if (wdc_c.flags & AT_TIMEOU) {
   1424 		printf("%s: flush cache command timeout\n",
   1425 		    wd->sc_dev.dv_xname);
   1426 	}
   1427 	if (wdc_c.flags & AT_DF) {
   1428 		printf("%s: flush cache command: drive fault\n",
   1429 		    wd->sc_dev.dv_xname);
   1430 	}
   1431 	/*
   1432 	 * Ignore error register, it shouldn't report anything else
   1433 	 * than COMMAND ABORTED, which means the device doesn't support
   1434 	 * flush cache
   1435 	 */
   1436 }
   1437 
   1438 void
   1439 wd_shutdown(arg)
   1440 	void *arg;
   1441 {
   1442 	struct wd_softc *wd = arg;
   1443 	wd_flushcache(wd, AT_POLL);
   1444 }
   1445 
   1446 /*
   1447  * Allocate space for a ioctl queue structure.  Mostly taken from
   1448  * scsipi_ioctl.c
   1449  */
   1450 struct wd_ioctl *
   1451 wi_get()
   1452 {
   1453 	struct wd_ioctl *wi;
   1454 	int s;
   1455 
   1456 	wi = malloc(sizeof(struct wd_ioctl), M_TEMP, M_WAITOK|M_ZERO);
   1457 	s = splbio();
   1458 	LIST_INSERT_HEAD(&wi_head, wi, wi_list);
   1459 	splx(s);
   1460 	return (wi);
   1461 }
   1462 
   1463 /*
   1464  * Free an ioctl structure and remove it from our list
   1465  */
   1466 
   1467 void
   1468 wi_free(wi)
   1469 	struct wd_ioctl *wi;
   1470 {
   1471 	int s;
   1472 
   1473 	s = splbio();
   1474 	LIST_REMOVE(wi, wi_list);
   1475 	splx(s);
   1476 	free(wi, M_TEMP);
   1477 }
   1478 
   1479 /*
   1480  * Find a wd_ioctl structure based on the struct buf.
   1481  */
   1482 
   1483 struct wd_ioctl *
   1484 wi_find(bp)
   1485 	struct buf *bp;
   1486 {
   1487 	struct wd_ioctl *wi;
   1488 	int s;
   1489 
   1490 	s = splbio();
   1491 	for (wi = wi_head.lh_first; wi != 0; wi = wi->wi_list.le_next)
   1492 		if (bp == &wi->wi_bp)
   1493 			break;
   1494 	splx(s);
   1495 	return (wi);
   1496 }
   1497 
   1498 /*
   1499  * Ioctl pseudo strategy routine
   1500  *
   1501  * This is mostly stolen from scsipi_ioctl.c:scsistrategy().  What
   1502  * happens here is:
   1503  *
   1504  * - wdioctl() queues a wd_ioctl structure.
   1505  *
   1506  * - wdioctl() calls physio/wdioctlstrategy based on whether or not
   1507  *   user space I/O is required.  If physio() is called, physio() eventually
   1508  *   calls wdioctlstrategy().
   1509  *
   1510  * - In either case, wdioctlstrategy() calls wd->atabus->ata_exec_command()
   1511  *   to perform the actual command
   1512  *
   1513  * The reason for the use of the pseudo strategy routine is because
   1514  * when doing I/O to/from user space, physio _really_ wants to be in
   1515  * the loop.  We could put the entire buffer into the ioctl request
   1516  * structure, but that won't scale if we want to do things like download
   1517  * microcode.
   1518  */
   1519 
   1520 void
   1521 wdioctlstrategy(bp)
   1522 	struct buf *bp;
   1523 {
   1524 	struct wd_ioctl *wi;
   1525 	struct wdc_command wdc_c;
   1526 	int error = 0;
   1527 
   1528 	wi = wi_find(bp);
   1529 	if (wi == NULL) {
   1530 		printf("user_strat: No ioctl\n");
   1531 		error = EINVAL;
   1532 		goto bad;
   1533 	}
   1534 
   1535 	memset(&wdc_c, 0, sizeof(wdc_c));
   1536 
   1537 	/*
   1538 	 * Abort if physio broke up the transfer
   1539 	 */
   1540 
   1541 	if (bp->b_bcount != wi->wi_atareq.datalen) {
   1542 		printf("physio split wd ioctl request... cannot proceed\n");
   1543 		error = EIO;
   1544 		goto bad;
   1545 	}
   1546 
   1547 	/*
   1548 	 * Abort if we didn't get a buffer size that was a multiple of
   1549 	 * our sector size (or was larger than NBBY)
   1550 	 */
   1551 
   1552 	if ((bp->b_bcount % wi->wi_softc->sc_dk.dk_label->d_secsize) != 0 ||
   1553 	    (bp->b_bcount / wi->wi_softc->sc_dk.dk_label->d_secsize) >=
   1554 	     (1 << NBBY)) {
   1555 		error = EINVAL;
   1556 		goto bad;
   1557 	}
   1558 
   1559 	/*
   1560 	 * Make sure a timeout was supplied in the ioctl request
   1561 	 */
   1562 
   1563 	if (wi->wi_atareq.timeout == 0) {
   1564 		error = EINVAL;
   1565 		goto bad;
   1566 	}
   1567 
   1568 	if (wi->wi_atareq.flags & ATACMD_READ)
   1569 		wdc_c.flags |= AT_READ;
   1570 	else if (wi->wi_atareq.flags & ATACMD_WRITE)
   1571 		wdc_c.flags |= AT_WRITE;
   1572 
   1573 	if (wi->wi_atareq.flags & ATACMD_READREG)
   1574 		wdc_c.flags |= AT_READREG;
   1575 
   1576 	wdc_c.flags |= AT_WAIT;
   1577 
   1578 	wdc_c.timeout = wi->wi_atareq.timeout;
   1579 	wdc_c.r_command = wi->wi_atareq.command;
   1580 	wdc_c.r_head = wi->wi_atareq.head & 0x0f;
   1581 	wdc_c.r_cyl = wi->wi_atareq.cylinder;
   1582 	wdc_c.r_sector = wi->wi_atareq.sec_num;
   1583 	wdc_c.r_count = wi->wi_atareq.sec_count;
   1584 	wdc_c.r_precomp = wi->wi_atareq.features;
   1585 	wdc_c.r_st_bmask = WDCS_DRDY;
   1586 	wdc_c.r_st_pmask = WDCS_DRDY;
   1587 	wdc_c.data = wi->wi_bp.b_data;
   1588 	wdc_c.bcount = wi->wi_bp.b_bcount;
   1589 
   1590 	if (wi->wi_softc->atabus->ata_exec_command(wi->wi_softc->drvp, &wdc_c)
   1591 	    != WDC_COMPLETE) {
   1592 		wi->wi_atareq.retsts = ATACMD_ERROR;
   1593 		goto bad;
   1594 	}
   1595 
   1596 	if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
   1597 		if (wdc_c.flags & AT_ERROR) {
   1598 			wi->wi_atareq.retsts = ATACMD_ERROR;
   1599 			wi->wi_atareq.error = wdc_c.r_error;
   1600 		} else if (wdc_c.flags & AT_DF)
   1601 			wi->wi_atareq.retsts = ATACMD_DF;
   1602 		else
   1603 			wi->wi_atareq.retsts = ATACMD_TIMEOUT;
   1604 	} else {
   1605 		wi->wi_atareq.retsts = ATACMD_OK;
   1606 		if (wi->wi_atareq.flags & ATACMD_READREG) {
   1607 			wi->wi_atareq.head = wdc_c.r_head ;
   1608 			wi->wi_atareq.cylinder = wdc_c.r_cyl;
   1609 			wi->wi_atareq.sec_num = wdc_c.r_sector;
   1610 			wi->wi_atareq.sec_count = wdc_c.r_count;
   1611 			wi->wi_atareq.features = wdc_c.r_precomp;
   1612 			wi->wi_atareq.error = wdc_c.r_error;
   1613 		}
   1614 	}
   1615 
   1616 	bp->b_error = 0;
   1617 	biodone(bp);
   1618 	return;
   1619 bad:
   1620 	bp->b_flags |= B_ERROR;
   1621 	bp->b_error = error;
   1622 	biodone(bp);
   1623 }
   1624