Home | History | Annotate | Line # | Download | only in ata
wd.c revision 1.298
      1 /*	$NetBSD: wd.c,v 1.298 2005/02/27 00:26:59 perry 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, 2003, 2004 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.298 2005/02/27 00:26:59 perry Exp $");
     70 
     71 #ifndef ATADEBUG
     72 #define ATADEBUG
     73 #endif /* ATADEBUG */
     74 
     75 #include "rnd.h"
     76 
     77 #include <sys/param.h>
     78 #include <sys/systm.h>
     79 #include <sys/kernel.h>
     80 #include <sys/conf.h>
     81 #include <sys/file.h>
     82 #include <sys/stat.h>
     83 #include <sys/ioctl.h>
     84 #include <sys/buf.h>
     85 #include <sys/bufq.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 #include <dev/ata/atareg.h>
    102 #include <dev/ata/atavar.h>
    103 #include <dev/ata/wdvar.h>
    104 #include <dev/ic/wdcreg.h>
    105 #include <sys/ataio.h>
    106 #include "locators.h"
    107 
    108 #define	LBA48_THRESHOLD		(0xfffffff)	/* 128GB / DEV_BSIZE */
    109 
    110 #define	WDIORETRIES_SINGLE 4	/* number of retries before single-sector */
    111 #define	WDIORETRIES	5	/* number of retries before giving up */
    112 #define	RECOVERYTIME hz/2	/* time to wait before retrying a cmd */
    113 
    114 #define	WDUNIT(dev)		DISKUNIT(dev)
    115 #define	WDPART(dev)		DISKPART(dev)
    116 #define	WDMINOR(unit, part)	DISKMINOR(unit, part)
    117 #define	MAKEWDDEV(maj, unit, part)	MAKEDISKDEV(maj, unit, part)
    118 
    119 #define	WDLABELDEV(dev)	(MAKEWDDEV(major(dev), WDUNIT(dev), RAW_PART))
    120 
    121 #define DEBUG_INTR   0x01
    122 #define DEBUG_XFERS  0x02
    123 #define DEBUG_STATUS 0x04
    124 #define DEBUG_FUNCS  0x08
    125 #define DEBUG_PROBE  0x10
    126 #ifdef ATADEBUG
    127 int wdcdebug_wd_mask = 0x0;
    128 #define ATADEBUG_PRINT(args, level) \
    129 	if (wdcdebug_wd_mask & (level)) \
    130 		printf args
    131 #else
    132 #define ATADEBUG_PRINT(args, level)
    133 #endif
    134 
    135 int	wdprobe(struct device *, struct cfdata *, void *);
    136 void	wdattach(struct device *, struct device *, void *);
    137 int	wddetach(struct device *, int);
    138 int	wdactivate(struct device *, enum devact);
    139 int	wdprint(void *, char *);
    140 void	wdperror(const struct wd_softc *);
    141 
    142 CFATTACH_DECL(wd, sizeof(struct wd_softc),
    143     wdprobe, wdattach, wddetach, wdactivate);
    144 
    145 extern struct cfdriver wd_cd;
    146 
    147 dev_type_open(wdopen);
    148 dev_type_close(wdclose);
    149 dev_type_read(wdread);
    150 dev_type_write(wdwrite);
    151 dev_type_ioctl(wdioctl);
    152 dev_type_strategy(wdstrategy);
    153 dev_type_dump(wddump);
    154 dev_type_size(wdsize);
    155 
    156 const struct bdevsw wd_bdevsw = {
    157 	wdopen, wdclose, wdstrategy, wdioctl, wddump, wdsize, D_DISK
    158 };
    159 
    160 const struct cdevsw wd_cdevsw = {
    161 	wdopen, wdclose, wdread, wdwrite, wdioctl,
    162 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
    163 };
    164 
    165 /*
    166  * Glue necessary to hook WDCIOCCOMMAND into physio
    167  */
    168 
    169 struct wd_ioctl {
    170 	LIST_ENTRY(wd_ioctl) wi_list;
    171 	struct buf wi_bp;
    172 	struct uio wi_uio;
    173 	struct iovec wi_iov;
    174 	atareq_t wi_atareq;
    175 	struct wd_softc *wi_softc;
    176 };
    177 
    178 LIST_HEAD(, wd_ioctl) wi_head;
    179 
    180 struct	wd_ioctl *wi_find(struct buf *);
    181 void	wi_free(struct wd_ioctl *);
    182 struct	wd_ioctl *wi_get(void);
    183 void	wdioctlstrategy(struct buf *);
    184 
    185 void  wdgetdefaultlabel(struct wd_softc *, struct disklabel *);
    186 void  wdgetdisklabel(struct wd_softc *);
    187 void  wdstart(void *);
    188 void  __wdstart(struct wd_softc*, struct buf *);
    189 void  wdrestart(void *);
    190 void  wddone(void *);
    191 int   wd_get_params(struct wd_softc *, u_int8_t, struct ataparams *);
    192 int   wd_standby(struct wd_softc *, int);
    193 int   wd_flushcache(struct wd_softc *, int);
    194 void  wd_shutdown(void *);
    195 
    196 int   wd_getcache(struct wd_softc *, int *);
    197 int   wd_setcache(struct wd_softc *, int);
    198 
    199 struct dkdriver wddkdriver = { wdstrategy, minphys };
    200 
    201 #ifdef HAS_BAD144_HANDLING
    202 static void bad144intern(struct wd_softc *);
    203 #endif
    204 
    205 #define	WD_QUIRK_SPLIT_MOD15_WRITE	0x0001	/* must split certain writes */
    206 #define	WD_QUIRK_FORCE_LBA48		0x0002	/* must use LBA48 commands */
    207 
    208 /*
    209  * Quirk table for IDE drives.  Put more-specific matches first, since
    210  * a simple globbing routine is used for matching.
    211  */
    212 static const struct wd_quirk {
    213 	const char *wdq_match;		/* inquiry pattern to match */
    214 	int wdq_quirks;			/* drive quirks */
    215 } wd_quirk_table[] = {
    216 	/*
    217 	 * Some Seagate S-ATA drives have a PHY which can get confused
    218 	 * with the way data is packetized by some S-ATA controllers.
    219 	 *
    220 	 * The work-around is to split in two any write transfer whose
    221 	 * sector count % 15 == 1 (assuming 512 byte sectors).
    222 	 *
    223 	 * XXX This is an incomplete list.  There are at least a couple
    224 	 * XXX more model numbers.  If you have trouble with such transfers
    225 	 * XXX (8K is the most common) on Seagate S-ATA drives, please
    226 	 * XXX notify thorpej (at) NetBSD.org.
    227 	 */
    228 	{ "ST3120023AS",
    229 	  WD_QUIRK_SPLIT_MOD15_WRITE },
    230 	{ "ST380023AS",
    231 	  WD_QUIRK_SPLIT_MOD15_WRITE },
    232 
    233 	/*
    234 	 * This seagate drive seems to have issue addressing sector 0xfffffff
    235 	 * (aka LBA48_THRESHOLD) in LBA mode. The workaround is to force
    236 	 * LBA48
    237 	 */
    238 	{ "ST3200822A",
    239 	  WD_QUIRK_FORCE_LBA48 },
    240 
    241 	{ NULL,
    242 	  0 }
    243 };
    244 
    245 static const struct wd_quirk *
    246 wd_lookup_quirks(const char *name)
    247 {
    248 	const struct wd_quirk *wdq;
    249 	const char *estr;
    250 
    251 	for (wdq = wd_quirk_table; wdq->wdq_match != NULL; wdq++) {
    252 		/*
    253 		 * We only want exact matches (which include matches
    254 		 * against globbing characters).
    255 		 */
    256 		if (pmatch(name, wdq->wdq_match, &estr) == 2)
    257 			return (wdq);
    258 	}
    259 	return (NULL);
    260 }
    261 
    262 int
    263 wdprobe(struct device *parent, struct cfdata *match, void *aux)
    264 {
    265 	struct ata_device *adev = aux;
    266 
    267 	if (adev == NULL)
    268 		return 0;
    269 	if (adev->adev_bustype->bustype_type != SCSIPI_BUSTYPE_ATA)
    270 		return 0;
    271 
    272 	if (match->cf_loc[ATA_HLCF_DRIVE] != ATA_HLCF_DRIVE_DEFAULT &&
    273 	    match->cf_loc[ATA_HLCF_DRIVE] != adev->adev_drv_data->drive)
    274 		return 0;
    275 	return 1;
    276 }
    277 
    278 void
    279 wdattach(struct device *parent, struct device *self, void *aux)
    280 {
    281 	struct wd_softc *wd = (void *)self;
    282 	struct ata_device *adev= aux;
    283 	int i, blank;
    284 	char buf[41], pbuf[9], c, *p, *q;
    285 	const struct wd_quirk *wdq;
    286 	ATADEBUG_PRINT(("wdattach\n"), DEBUG_FUNCS | DEBUG_PROBE);
    287 
    288 	callout_init(&wd->sc_restart_ch);
    289 	bufq_alloc(&wd->sc_q, BUFQ_DISK_DEFAULT_STRAT()|BUFQ_SORT_RAWBLOCK);
    290 #ifdef WD_SOFTBADSECT
    291 	SLIST_INIT(&wd->sc_bslist);
    292 #endif
    293 	wd->atabus = adev->adev_bustype;
    294 	wd->openings = adev->adev_openings;
    295 	wd->drvp = adev->adev_drv_data;
    296 
    297 	wd->drvp->drv_done = wddone;
    298 	wd->drvp->drv_softc = &wd->sc_dev;
    299 
    300 	aprint_naive("\n");
    301 
    302 	/* read our drive info */
    303 	if (wd_get_params(wd, AT_WAIT, &wd->sc_params) != 0) {
    304 		aprint_error("\n%s: IDENTIFY failed\n", wd->sc_dev.dv_xname);
    305 		return;
    306 	}
    307 
    308 	for (blank = 0, p = wd->sc_params.atap_model, q = buf, i = 0;
    309 	    i < sizeof(wd->sc_params.atap_model); i++) {
    310 		c = *p++;
    311 		if (c == '\0')
    312 			break;
    313 		if (c != ' ') {
    314 			if (blank) {
    315 				*q++ = ' ';
    316 				blank = 0;
    317 			}
    318 			*q++ = c;
    319 		} else
    320 			blank = 1;
    321 	}
    322 	*q++ = '\0';
    323 
    324 	aprint_normal(": <%s>\n", buf);
    325 
    326 	wdq = wd_lookup_quirks(buf);
    327 	if (wdq != NULL)
    328 		wd->sc_quirks = wdq->wdq_quirks;
    329 
    330 	if ((wd->sc_params.atap_multi & 0xff) > 1) {
    331 		wd->sc_multi = wd->sc_params.atap_multi & 0xff;
    332 	} else {
    333 		wd->sc_multi = 1;
    334 	}
    335 
    336 	aprint_normal("%s: drive supports %d-sector PIO transfers,",
    337 	    wd->sc_dev.dv_xname, wd->sc_multi);
    338 
    339 	/* 48-bit LBA addressing */
    340 	if ((wd->sc_params.atap_cmd2_en & ATA_CMD2_LBA48) != 0)
    341 		wd->sc_flags |= WDF_LBA48;
    342 
    343 	/* Prior to ATA-4, LBA was optional. */
    344 	if ((wd->sc_params.atap_capabilities1 & WDC_CAP_LBA) != 0)
    345 		wd->sc_flags |= WDF_LBA;
    346 #if 0
    347 	/* ATA-4 requires LBA. */
    348 	if (wd->sc_params.atap_ataversion != 0xffff &&
    349 	    wd->sc_params.atap_ataversion >= WDC_VER_ATA4)
    350 		wd->sc_flags |= WDF_LBA;
    351 #endif
    352 
    353 	if ((wd->sc_flags & WDF_LBA48) != 0) {
    354 		aprint_normal(" LBA48 addressing\n");
    355 		wd->sc_capacity =
    356 		    ((u_int64_t) wd->sc_params.__reserved6[11] << 48) |
    357 		    ((u_int64_t) wd->sc_params.__reserved6[10] << 32) |
    358 		    ((u_int64_t) wd->sc_params.__reserved6[9]  << 16) |
    359 		    ((u_int64_t) wd->sc_params.__reserved6[8]  << 0);
    360 	} else if ((wd->sc_flags & WDF_LBA) != 0) {
    361 		aprint_normal(" LBA addressing\n");
    362 		wd->sc_capacity =
    363 		    (wd->sc_params.atap_capacity[1] << 16) |
    364 		    wd->sc_params.atap_capacity[0];
    365 	} else {
    366 		aprint_normal(" chs addressing\n");
    367 		wd->sc_capacity =
    368 		    wd->sc_params.atap_cylinders *
    369 		    wd->sc_params.atap_heads *
    370 		    wd->sc_params.atap_sectors;
    371 	}
    372 	format_bytes(pbuf, sizeof(pbuf), wd->sc_capacity * DEV_BSIZE);
    373 	aprint_normal("%s: %s, %d cyl, %d head, %d sec, "
    374 	    "%d bytes/sect x %llu sectors\n",
    375 	    self->dv_xname, pbuf,
    376 	    (wd->sc_flags & WDF_LBA) ? (int)(wd->sc_capacity /
    377 		(wd->sc_params.atap_heads * wd->sc_params.atap_sectors)) :
    378 		wd->sc_params.atap_cylinders,
    379 	    wd->sc_params.atap_heads, wd->sc_params.atap_sectors,
    380 	    DEV_BSIZE, (unsigned long long)wd->sc_capacity);
    381 
    382 	ATADEBUG_PRINT(("%s: atap_dmatiming_mimi=%d, atap_dmatiming_recom=%d\n",
    383 	    self->dv_xname, wd->sc_params.atap_dmatiming_mimi,
    384 	    wd->sc_params.atap_dmatiming_recom), DEBUG_PROBE);
    385 	/*
    386 	 * Initialize and attach the disk structure.
    387 	 */
    388 	wd->sc_dk.dk_driver = &wddkdriver;
    389 	wd->sc_dk.dk_name = wd->sc_dev.dv_xname;
    390 	disk_attach(&wd->sc_dk);
    391 	wd->sc_wdc_bio.lp = wd->sc_dk.dk_label;
    392 	wd->sc_sdhook = shutdownhook_establish(wd_shutdown, wd);
    393 	if (wd->sc_sdhook == NULL)
    394 		aprint_error("%s: WARNING: unable to establish shutdown hook\n",
    395 		    wd->sc_dev.dv_xname);
    396 #if NRND > 0
    397 	rnd_attach_source(&wd->rnd_source, wd->sc_dev.dv_xname,
    398 			  RND_TYPE_DISK, 0);
    399 #endif
    400 
    401 	/* Discover wedges on this disk. */
    402 	dkwedge_discover(&wd->sc_dk);
    403 }
    404 
    405 int
    406 wdactivate(struct device *self, enum devact act)
    407 {
    408 	int rv = 0;
    409 
    410 	switch (act) {
    411 	case DVACT_ACTIVATE:
    412 		rv = EOPNOTSUPP;
    413 		break;
    414 
    415 	case DVACT_DEACTIVATE:
    416 		/*
    417 		 * Nothing to do; we key off the device's DVF_ACTIVATE.
    418 		 */
    419 		break;
    420 	}
    421 	return (rv);
    422 }
    423 
    424 int
    425 wddetach(struct device *self, int flags)
    426 {
    427 	struct wd_softc *sc = (struct wd_softc *)self;
    428 	struct buf *bp;
    429 	int s, bmaj, cmaj, i, mn;
    430 
    431 	/* locate the major number */
    432 	bmaj = bdevsw_lookup_major(&wd_bdevsw);
    433 	cmaj = cdevsw_lookup_major(&wd_cdevsw);
    434 
    435 	/* Nuke the vnodes for any open instances. */
    436 	for (i = 0; i < MAXPARTITIONS; i++) {
    437 		mn = WDMINOR(self->dv_unit, i);
    438 		vdevgone(bmaj, mn, mn, VBLK);
    439 		vdevgone(cmaj, mn, mn, VCHR);
    440 	}
    441 
    442 	/* Delete all of our wedges. */
    443 	dkwedge_delall(&sc->sc_dk);
    444 
    445 	s = splbio();
    446 
    447 	/* Kill off any queued buffers. */
    448 	while ((bp = BUFQ_GET(&sc->sc_q)) != NULL) {
    449 		bp->b_error = EIO;
    450 		bp->b_flags |= B_ERROR;
    451 		bp->b_resid = bp->b_bcount;
    452 		biodone(bp);
    453 	}
    454 
    455 	bufq_free(&sc->sc_q);
    456 	sc->atabus->ata_killpending(sc->drvp);
    457 
    458 	splx(s);
    459 
    460 	/* Detach disk. */
    461 	disk_detach(&sc->sc_dk);
    462 
    463 #ifdef WD_SOFTBADSECT
    464 	/* Clean out the bad sector list */
    465 	while (!SLIST_EMPTY(&sc->sc_bslist)) {
    466 		void *head = SLIST_FIRST(&sc->sc_bslist);
    467 		SLIST_REMOVE_HEAD(&sc->sc_bslist, dbs_next);
    468 		free(head, M_TEMP);
    469 	}
    470 	sc->sc_bscount = 0;
    471 #endif
    472 
    473 	/* Get rid of the shutdown hook. */
    474 	if (sc->sc_sdhook != NULL)
    475 		shutdownhook_disestablish(sc->sc_sdhook);
    476 
    477 #if NRND > 0
    478 	/* Unhook the entropy source. */
    479 	rnd_detach_source(&sc->rnd_source);
    480 #endif
    481 
    482 	sc->drvp->drive_flags = 0; /* no drive any more here */
    483 
    484 	return (0);
    485 }
    486 
    487 /*
    488  * Read/write routine for a buffer.  Validates the arguments and schedules the
    489  * transfer.  Does not wait for the transfer to complete.
    490  */
    491 void
    492 wdstrategy(struct buf *bp)
    493 {
    494 	struct wd_softc *wd = device_lookup(&wd_cd, WDUNIT(bp->b_dev));
    495 	struct disklabel *lp = wd->sc_dk.dk_label;
    496 	daddr_t blkno;
    497 	int s;
    498 
    499 	ATADEBUG_PRINT(("wdstrategy (%s)\n", wd->sc_dev.dv_xname),
    500 	    DEBUG_XFERS);
    501 
    502 	/* Valid request?  */
    503 	if (bp->b_blkno < 0 ||
    504 	    (bp->b_bcount % lp->d_secsize) != 0 ||
    505 	    (bp->b_bcount / lp->d_secsize) >= (1 << NBBY)) {
    506 		bp->b_error = EINVAL;
    507 		goto bad;
    508 	}
    509 
    510 	/* If device invalidated (e.g. media change, door open), error. */
    511 	if ((wd->sc_flags & WDF_LOADED) == 0) {
    512 		bp->b_error = EIO;
    513 		goto bad;
    514 	}
    515 
    516 	/* If it's a null transfer, return immediately. */
    517 	if (bp->b_bcount == 0)
    518 		goto done;
    519 
    520 	/*
    521 	 * Do bounds checking, adjust transfer. if error, process.
    522 	 * If end of partition, just return.
    523 	 */
    524 	if (WDPART(bp->b_dev) == RAW_PART) {
    525 		if (bounds_check_with_mediasize(bp, DEV_BSIZE,
    526 		    wd->sc_capacity) <= 0)
    527 			goto done;
    528 	} else {
    529 		if (bounds_check_with_label(&wd->sc_dk, bp,
    530 		    (wd->sc_flags & (WDF_WLABEL|WDF_LABELLING)) != 0) <= 0)
    531 			goto done;
    532 	}
    533 
    534 	/*
    535 	 * Now convert the block number to absolute and put it in
    536 	 * terms of the device's logical block size.
    537 	 */
    538 	if (lp->d_secsize >= DEV_BSIZE)
    539 		blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
    540 	else
    541 		blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
    542 
    543 	if (WDPART(bp->b_dev) != RAW_PART)
    544 		blkno += lp->d_partitions[WDPART(bp->b_dev)].p_offset;
    545 
    546 	bp->b_rawblkno = blkno;
    547 
    548 #ifdef WD_SOFTBADSECT
    549 	/*
    550 	 * If the transfer about to be attempted contains only a block that
    551 	 * is known to be bad then return an error for the transfer without
    552 	 * even attempting to start a transfer up under the premis that we
    553 	 * will just end up doing more retries for a transfer that will end
    554 	 * up failing again.
    555 	 * XXX:SMP - mutex required to protect with DIOCBSFLUSH
    556 	 */
    557 	if (__predict_false(!SLIST_EMPTY(&wd->sc_bslist))) {
    558 		struct disk_badsectors *dbs;
    559 		daddr_t maxblk = blkno + (bp->b_bcount >> DEV_BSHIFT) - 1;
    560 
    561 		SLIST_FOREACH(dbs, &wd->sc_bslist, dbs_next)
    562 			if ((dbs->dbs_min <= blkno && blkno <= dbs->dbs_max) ||
    563 			    (dbs->dbs_min <= maxblk && maxblk <= dbs->dbs_max)){
    564 				bp->b_error = EIO;
    565 				goto bad;
    566 			}
    567 	}
    568 #endif
    569 
    570 	/* Queue transfer on drive, activate drive and controller if idle. */
    571 	s = splbio();
    572 	BUFQ_PUT(&wd->sc_q, bp);
    573 	wdstart(wd);
    574 	splx(s);
    575 	return;
    576 bad:
    577 	bp->b_flags |= B_ERROR;
    578 done:
    579 	/* Toss transfer; we're done early. */
    580 	bp->b_resid = bp->b_bcount;
    581 	biodone(bp);
    582 }
    583 
    584 /*
    585  * Queue a drive for I/O.
    586  */
    587 void
    588 wdstart(void *arg)
    589 {
    590 	struct wd_softc *wd = arg;
    591 	struct buf *bp = NULL;
    592 
    593 	ATADEBUG_PRINT(("wdstart %s\n", wd->sc_dev.dv_xname),
    594 	    DEBUG_XFERS);
    595 	while (wd->openings > 0) {
    596 
    597 		/* Is there a buf for us ? */
    598 		if ((bp = BUFQ_GET(&wd->sc_q)) == NULL)
    599 			return;
    600 
    601 		/*
    602 		 * Make the command. First lock the device
    603 		 */
    604 		wd->openings--;
    605 
    606 		wd->retries = 0;
    607 		__wdstart(wd, bp);
    608 	}
    609 }
    610 
    611 static void
    612 wd_split_mod15_write(struct buf *bp)
    613 {
    614 	struct buf *obp = bp->b_private;
    615 	struct wd_softc *sc = wd_cd.cd_devs[DISKUNIT(obp->b_dev)];
    616 
    617 	if (__predict_false(bp->b_flags & B_ERROR) != 0) {
    618 		/*
    619 		 * Propagate the error.  If this was the first half of
    620 		 * the original transfer, make sure to account for that
    621 		 * in the residual.
    622 		 */
    623 		if (bp->b_data == obp->b_data)
    624 			bp->b_resid += bp->b_bcount;
    625 		goto done;
    626 	}
    627 
    628 	/*
    629 	 * If this was the second half of the transfer, we're all done!
    630 	 */
    631 	if (bp->b_data != obp->b_data)
    632 		goto done;
    633 
    634 	/*
    635 	 * Advance the pointer to the second half and issue that command
    636 	 * using the same opening.
    637 	 */
    638 	bp->b_flags = obp->b_flags | B_CALL;
    639 	bp->b_data += bp->b_bcount;
    640 	bp->b_blkno += (bp->b_bcount / 512);
    641 	bp->b_rawblkno += (bp->b_bcount / 512);
    642 	__wdstart(sc, bp);
    643 	return;
    644 
    645  done:
    646 	obp->b_flags |= (bp->b_flags & (B_EINTR|B_ERROR));
    647 	obp->b_error = bp->b_error;
    648 	obp->b_resid = bp->b_resid;
    649 	pool_put(&bufpool, bp);
    650 	biodone(obp);
    651 	sc->openings++;
    652 	/* wddone() will call wdstart() */
    653 }
    654 
    655 void
    656 __wdstart(struct wd_softc *wd, struct buf *bp)
    657 {
    658 
    659 	/*
    660 	 * Deal with the "split mod15 write" quirk.  We just divide the
    661 	 * transfer in two, doing the first half and then then second half
    662 	 * with the same command opening.
    663 	 *
    664 	 * Note we MUST do this here, because we can't let insertion
    665 	 * into the bufq cause the transfers to be re-merged.
    666 	 */
    667 	if (__predict_false((wd->sc_quirks & WD_QUIRK_SPLIT_MOD15_WRITE) != 0 &&
    668 			    (bp->b_flags & B_READ) == 0 &&
    669 			    bp->b_bcount > 512 &&
    670 			    ((bp->b_bcount / 512) % 15) == 1)) {
    671 		struct buf *nbp;
    672 
    673 		/* already at splbio */
    674 		nbp = pool_get(&bufpool, PR_NOWAIT);
    675 		if (__predict_false(nbp == NULL)) {
    676 			/* No memory -- fail the iop. */
    677 			bp->b_error = ENOMEM;
    678 			bp->b_flags |= B_ERROR;
    679 			bp->b_resid = bp->b_bcount;
    680 			biodone(bp);
    681 			wd->openings++;
    682 			return;
    683 		}
    684 
    685 		BUF_INIT(nbp);
    686 		nbp->b_error = 0;
    687 		nbp->b_proc = bp->b_proc;
    688 		nbp->b_vp = NULLVP;
    689 		nbp->b_dev = bp->b_dev;
    690 
    691 		nbp->b_bcount = bp->b_bcount / 2;
    692 		nbp->b_bufsize = bp->b_bcount / 2;
    693 		nbp->b_data = bp->b_data;
    694 
    695 		nbp->b_blkno = bp->b_blkno;
    696 		nbp->b_rawblkno = bp->b_rawblkno;
    697 
    698 		nbp->b_flags = bp->b_flags | B_CALL;
    699 		nbp->b_iodone = wd_split_mod15_write;
    700 
    701 		/* Put ptr to orig buf in b_private and use new buf */
    702 		nbp->b_private = bp;
    703 
    704 		BIO_COPYPRIO(nbp, bp);
    705 
    706 		bp = nbp;
    707 	}
    708 
    709 	wd->sc_wdc_bio.blkno = bp->b_rawblkno;
    710 	wd->sc_wdc_bio.blkdone =0;
    711 	wd->sc_bp = bp;
    712 	/*
    713 	 * If we're retrying, retry in single-sector mode. This will give us
    714 	 * the sector number of the problem, and will eventually allow the
    715 	 * transfer to succeed.
    716 	 */
    717 	if (wd->retries >= WDIORETRIES_SINGLE)
    718 		wd->sc_wdc_bio.flags = ATA_SINGLE;
    719 	else
    720 		wd->sc_wdc_bio.flags = 0;
    721 	if (wd->sc_flags & WDF_LBA48 &&
    722 	    (wd->sc_wdc_bio.blkno > LBA48_THRESHOLD ||
    723 	    (wd->sc_quirks & WD_QUIRK_FORCE_LBA48) != 0))
    724 		wd->sc_wdc_bio.flags |= ATA_LBA48;
    725 	if (wd->sc_flags & WDF_LBA)
    726 		wd->sc_wdc_bio.flags |= ATA_LBA;
    727 	if (bp->b_flags & B_READ)
    728 		wd->sc_wdc_bio.flags |= ATA_READ;
    729 	wd->sc_wdc_bio.bcount = bp->b_bcount;
    730 	wd->sc_wdc_bio.databuf = bp->b_data;
    731 	/* Instrumentation. */
    732 	disk_busy(&wd->sc_dk);
    733 	switch (wd->atabus->ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
    734 	case ATACMD_TRY_AGAIN:
    735 		callout_reset(&wd->sc_restart_ch, hz, wdrestart, wd);
    736 		break;
    737 	case ATACMD_QUEUED:
    738 	case ATACMD_COMPLETE:
    739 		break;
    740 	default:
    741 		panic("__wdstart: bad return code from ata_bio()");
    742 	}
    743 }
    744 
    745 void
    746 wddone(void *v)
    747 {
    748 	struct wd_softc *wd = v;
    749 	struct buf *bp = wd->sc_bp;
    750 	const char *errmsg;
    751 	int do_perror = 0;
    752 	ATADEBUG_PRINT(("wddone %s\n", wd->sc_dev.dv_xname),
    753 	    DEBUG_XFERS);
    754 
    755 	if (bp == NULL)
    756 		return;
    757 	bp->b_resid = wd->sc_wdc_bio.bcount;
    758 	switch (wd->sc_wdc_bio.error) {
    759 	case ERR_DMA:
    760 		errmsg = "DMA error";
    761 		goto retry;
    762 	case ERR_DF:
    763 		errmsg = "device fault";
    764 		goto retry;
    765 	case TIMEOUT:
    766 		errmsg = "device timeout";
    767 		goto retry;
    768 	case ERR_RESET:
    769 		errmsg = "channel reset";
    770 		goto retry2;
    771 	case ERROR:
    772 		/* Don't care about media change bits */
    773 		if (wd->sc_wdc_bio.r_error != 0 &&
    774 		    (wd->sc_wdc_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0)
    775 			goto noerror;
    776 		errmsg = "error";
    777 		do_perror = 1;
    778 retry:		/* Just reset and retry. Can we do more ? */
    779 		(*wd->atabus->ata_reset_drive)(wd->drvp, AT_RST_NOCMD);
    780 retry2:
    781 		diskerr(bp, "wd", errmsg, LOG_PRINTF,
    782 		    wd->sc_wdc_bio.blkdone, wd->sc_dk.dk_label);
    783 		if (wd->retries < WDIORETRIES)
    784 			printf(", retrying\n");
    785 		if (do_perror)
    786 			wdperror(wd);
    787 		if (wd->retries < WDIORETRIES) {
    788 			wd->retries++;
    789 			callout_reset(&wd->sc_restart_ch, RECOVERYTIME,
    790 			    wdrestart, wd);
    791 			return;
    792 		}
    793 		printf("\n");
    794 
    795 #ifdef WD_SOFTBADSECT
    796 		/*
    797 		 * Not all errors indicate a failed block but those that do,
    798 		 * put the block on the bad-block list for the device.  Only
    799 		 * do this for reads because the drive should do it for writes,
    800 		 * itself, according to Manuel.
    801 		 */
    802 		if ((bp->b_flags & B_READ) &&
    803 		    ((wd->drvp->ata_vers >= 4 && wd->sc_wdc_bio.r_error & 64) ||
    804 	     	     (wd->drvp->ata_vers < 4 && wd->sc_wdc_bio.r_error & 192))) {
    805 			struct disk_badsectors *dbs;
    806 
    807 			dbs = malloc(sizeof *dbs, M_TEMP, M_WAITOK);
    808 			dbs->dbs_min = bp->b_rawblkno;
    809 			dbs->dbs_max = dbs->dbs_min + (bp->b_bcount >> DEV_BSHIFT) - 1;
    810 			microtime(&dbs->dbs_failedat);
    811 			SLIST_INSERT_HEAD(&wd->sc_bslist, dbs, dbs_next);
    812 			wd->sc_bscount++;
    813 		}
    814 #endif
    815 		bp->b_flags |= B_ERROR;
    816 		bp->b_error = EIO;
    817 		break;
    818 	case NOERROR:
    819 noerror:	if ((wd->sc_wdc_bio.flags & ATA_CORR) || wd->retries > 0)
    820 			printf("%s: soft error (corrected)\n",
    821 			    wd->sc_dev.dv_xname);
    822 		break;
    823 	case ERR_NODEV:
    824 		bp->b_flags |= B_ERROR;
    825 		bp->b_error = EIO;
    826 		break;
    827 	}
    828 	disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid),
    829 	    (bp->b_flags & B_READ));
    830 #if NRND > 0
    831 	rnd_add_uint32(&wd->rnd_source, bp->b_blkno);
    832 #endif
    833 	/* XXX Yuck, but we don't want to increment openings in this case */
    834 	if (__predict_false((bp->b_flags & B_CALL) != 0 &&
    835 			    bp->b_iodone == wd_split_mod15_write))
    836 		biodone(bp);
    837 	else {
    838 		biodone(bp);
    839 		wd->openings++;
    840 	}
    841 	wdstart(wd);
    842 }
    843 
    844 void
    845 wdrestart(void *v)
    846 {
    847 	struct wd_softc *wd = v;
    848 	struct buf *bp = wd->sc_bp;
    849 	int s;
    850 	ATADEBUG_PRINT(("wdrestart %s\n", wd->sc_dev.dv_xname),
    851 	    DEBUG_XFERS);
    852 
    853 	s = splbio();
    854 	__wdstart(v, bp);
    855 	splx(s);
    856 }
    857 
    858 int
    859 wdread(dev_t dev, struct uio *uio, int flags)
    860 {
    861 
    862 	ATADEBUG_PRINT(("wdread\n"), DEBUG_XFERS);
    863 	return (physio(wdstrategy, NULL, dev, B_READ, minphys, uio));
    864 }
    865 
    866 int
    867 wdwrite(dev_t dev, struct uio *uio, int flags)
    868 {
    869 
    870 	ATADEBUG_PRINT(("wdwrite\n"), DEBUG_XFERS);
    871 	return (physio(wdstrategy, NULL, dev, B_WRITE, minphys, uio));
    872 }
    873 
    874 int
    875 wdopen(dev_t dev, int flag, int fmt, struct proc *p)
    876 {
    877 	struct wd_softc *wd;
    878 	int part, error;
    879 
    880 	ATADEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS);
    881 	wd = device_lookup(&wd_cd, WDUNIT(dev));
    882 	if (wd == NULL)
    883 		return (ENXIO);
    884 
    885 	if ((wd->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    886 		return (ENODEV);
    887 
    888 	part = WDPART(dev);
    889 
    890 	if ((error = lockmgr(&wd->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL)) != 0)
    891 		return (error);
    892 
    893 	/*
    894 	 * If there are wedges, and this is not RAW_PART, then we
    895 	 * need to fail.
    896 	 */
    897 	if (wd->sc_dk.dk_nwedges != 0 && part != RAW_PART) {
    898 		error = EBUSY;
    899 		goto bad1;
    900 	}
    901 
    902 	/*
    903 	 * If this is the first open of this device, add a reference
    904 	 * to the adapter.
    905 	 */
    906 	if (wd->sc_dk.dk_openmask == 0 &&
    907 	    (error = wd->atabus->ata_addref(wd->drvp)) != 0)
    908 		goto bad1;
    909 
    910 	if (wd->sc_dk.dk_openmask != 0) {
    911 		/*
    912 		 * If any partition is open, but the disk has been invalidated,
    913 		 * disallow further opens.
    914 		 */
    915 		if ((wd->sc_flags & WDF_LOADED) == 0) {
    916 			error = EIO;
    917 			goto bad2;
    918 		}
    919 	} else {
    920 		if ((wd->sc_flags & WDF_LOADED) == 0) {
    921 			wd->sc_flags |= WDF_LOADED;
    922 
    923 			/* Load the physical device parameters. */
    924 			wd_get_params(wd, AT_WAIT, &wd->sc_params);
    925 
    926 			/* Load the partition info if not already loaded. */
    927 			wdgetdisklabel(wd);
    928 		}
    929 	}
    930 
    931 	/* Check that the partition exists. */
    932 	if (part != RAW_PART &&
    933 	    (part >= wd->sc_dk.dk_label->d_npartitions ||
    934 	     wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
    935 		error = ENXIO;
    936 		goto bad2;
    937 	}
    938 
    939 	/* Insure only one open at a time. */
    940 	switch (fmt) {
    941 	case S_IFCHR:
    942 		wd->sc_dk.dk_copenmask |= (1 << part);
    943 		break;
    944 	case S_IFBLK:
    945 		wd->sc_dk.dk_bopenmask |= (1 << part);
    946 		break;
    947 	}
    948 	wd->sc_dk.dk_openmask =
    949 	    wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
    950 
    951 	(void) lockmgr(&wd->sc_dk.dk_openlock, LK_RELEASE, NULL);
    952 	return 0;
    953 
    954  bad2:
    955 	if (wd->sc_dk.dk_openmask == 0)
    956 		wd->atabus->ata_delref(wd->drvp);
    957  bad1:
    958 	(void) lockmgr(&wd->sc_dk.dk_openlock, LK_RELEASE, NULL);
    959 	return error;
    960 }
    961 
    962 int
    963 wdclose(dev_t dev, int flag, int fmt, struct proc *p)
    964 {
    965 	struct wd_softc *wd = device_lookup(&wd_cd, WDUNIT(dev));
    966 	int part = WDPART(dev);
    967 	int error;
    968 
    969 	ATADEBUG_PRINT(("wdclose\n"), DEBUG_FUNCS);
    970 
    971 	if ((error = lockmgr(&wd->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL)) != 0)
    972 		return error;
    973 
    974 	switch (fmt) {
    975 	case S_IFCHR:
    976 		wd->sc_dk.dk_copenmask &= ~(1 << part);
    977 		break;
    978 	case S_IFBLK:
    979 		wd->sc_dk.dk_bopenmask &= ~(1 << part);
    980 		break;
    981 	}
    982 	wd->sc_dk.dk_openmask =
    983 	    wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
    984 
    985 	if (wd->sc_dk.dk_openmask == 0) {
    986 		wd_flushcache(wd, AT_WAIT);
    987 
    988 		if (! (wd->sc_flags & WDF_KLABEL))
    989 			wd->sc_flags &= ~WDF_LOADED;
    990 
    991 		wd->atabus->ata_delref(wd->drvp);
    992 	}
    993 
    994 	(void) lockmgr(&wd->sc_dk.dk_openlock, LK_RELEASE, NULL);
    995 	return 0;
    996 }
    997 
    998 void
    999 wdgetdefaultlabel(struct wd_softc *wd, struct disklabel *lp)
   1000 {
   1001 
   1002 	ATADEBUG_PRINT(("wdgetdefaultlabel\n"), DEBUG_FUNCS);
   1003 	memset(lp, 0, sizeof(struct disklabel));
   1004 
   1005 	lp->d_secsize = DEV_BSIZE;
   1006 	lp->d_ntracks = wd->sc_params.atap_heads;
   1007 	lp->d_nsectors = wd->sc_params.atap_sectors;
   1008 	lp->d_ncylinders = (wd->sc_flags & WDF_LBA) ? wd->sc_capacity /
   1009 		(wd->sc_params.atap_heads * wd->sc_params.atap_sectors) :
   1010 		wd->sc_params.atap_cylinders;
   1011 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
   1012 
   1013 	if (strcmp(wd->sc_params.atap_model, "ST506") == 0)
   1014 		lp->d_type = DTYPE_ST506;
   1015 	else
   1016 		lp->d_type = DTYPE_ESDI;
   1017 
   1018 	strncpy(lp->d_typename, wd->sc_params.atap_model, 16);
   1019 	strncpy(lp->d_packname, "fictitious", 16);
   1020 	if (wd->sc_capacity > UINT32_MAX)
   1021 		lp->d_secperunit = UINT32_MAX;
   1022 	else
   1023 		lp->d_secperunit = wd->sc_capacity;
   1024 	lp->d_rpm = 3600;
   1025 	lp->d_interleave = 1;
   1026 	lp->d_flags = 0;
   1027 
   1028 	lp->d_partitions[RAW_PART].p_offset = 0;
   1029 	lp->d_partitions[RAW_PART].p_size =
   1030 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
   1031 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
   1032 	lp->d_npartitions = RAW_PART + 1;
   1033 
   1034 	lp->d_magic = DISKMAGIC;
   1035 	lp->d_magic2 = DISKMAGIC;
   1036 	lp->d_checksum = dkcksum(lp);
   1037 }
   1038 
   1039 /*
   1040  * Fabricate a default disk label, and try to read the correct one.
   1041  */
   1042 void
   1043 wdgetdisklabel(struct wd_softc *wd)
   1044 {
   1045 	struct disklabel *lp = wd->sc_dk.dk_label;
   1046 	const char *errstring;
   1047 	int s;
   1048 
   1049 	ATADEBUG_PRINT(("wdgetdisklabel\n"), DEBUG_FUNCS);
   1050 
   1051 	memset(wd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
   1052 
   1053 	wdgetdefaultlabel(wd, lp);
   1054 
   1055 	wd->sc_badsect[0] = -1;
   1056 
   1057 	if (wd->drvp->state > RESET) {
   1058 		s = splbio();
   1059 		wd->drvp->drive_flags |= DRIVE_RESET;
   1060 		splx(s);
   1061 	}
   1062 	errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
   1063 	    wdstrategy, lp, wd->sc_dk.dk_cpulabel);
   1064 	if (errstring) {
   1065 		/*
   1066 		 * This probably happened because the drive's default
   1067 		 * geometry doesn't match the DOS geometry.  We
   1068 		 * assume the DOS geometry is now in the label and try
   1069 		 * again.  XXX This is a kluge.
   1070 		 */
   1071 		if (wd->drvp->state > RESET) {
   1072 			s = splbio();
   1073 			wd->drvp->drive_flags |= DRIVE_RESET;
   1074 			splx(s);
   1075 		}
   1076 		errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit,
   1077 		    RAW_PART), wdstrategy, lp, wd->sc_dk.dk_cpulabel);
   1078 	}
   1079 	if (errstring) {
   1080 		printf("%s: %s\n", wd->sc_dev.dv_xname, errstring);
   1081 		return;
   1082 	}
   1083 
   1084 	if (wd->drvp->state > RESET) {
   1085 		s = splbio();
   1086 		wd->drvp->drive_flags |= DRIVE_RESET;
   1087 		splx(s);
   1088 	}
   1089 #ifdef HAS_BAD144_HANDLING
   1090 	if ((lp->d_flags & D_BADSECT) != 0)
   1091 		bad144intern(wd);
   1092 #endif
   1093 }
   1094 
   1095 void
   1096 wdperror(const struct wd_softc *wd)
   1097 {
   1098 	static const char *const errstr0_3[] = {"address mark not found",
   1099 	    "track 0 not found", "aborted command", "media change requested",
   1100 	    "id not found", "media changed", "uncorrectable data error",
   1101 	    "bad block detected"};
   1102 	static const char *const errstr4_5[] = {
   1103 	    "obsolete (address mark not found)",
   1104 	    "no media/write protected", "aborted command",
   1105 	    "media change requested", "id not found", "media changed",
   1106 	    "uncorrectable data error", "interface CRC error"};
   1107 	const char *const *errstr;
   1108 	int i;
   1109 	char *sep = "";
   1110 
   1111 	const char *devname = wd->sc_dev.dv_xname;
   1112 	struct ata_drive_datas *drvp = wd->drvp;
   1113 	int errno = wd->sc_wdc_bio.r_error;
   1114 
   1115 	if (drvp->ata_vers >= 4)
   1116 		errstr = errstr4_5;
   1117 	else
   1118 		errstr = errstr0_3;
   1119 
   1120 	printf("%s: (", devname);
   1121 
   1122 	if (errno == 0)
   1123 		printf("error not notified");
   1124 
   1125 	for (i = 0; i < 8; i++) {
   1126 		if (errno & (1 << i)) {
   1127 			printf("%s%s", sep, errstr[i]);
   1128 			sep = ", ";
   1129 		}
   1130 	}
   1131 	printf(")\n");
   1132 }
   1133 
   1134 int
   1135 wdioctl(dev_t dev, u_long xfer, caddr_t addr, int flag, struct proc *p)
   1136 {
   1137 	struct wd_softc *wd = device_lookup(&wd_cd, WDUNIT(dev));
   1138 	int error = 0, s;
   1139 #ifdef __HAVE_OLD_DISKLABEL
   1140 	struct disklabel *newlabel = NULL;
   1141 #endif
   1142 
   1143 	ATADEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS);
   1144 
   1145 	if ((wd->sc_flags & WDF_LOADED) == 0)
   1146 		return EIO;
   1147 
   1148 	switch (xfer) {
   1149 #ifdef HAS_BAD144_HANDLING
   1150 	case DIOCSBAD:
   1151 		if ((flag & FWRITE) == 0)
   1152 			return EBADF;
   1153 		wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
   1154 		wd->sc_dk.dk_label->d_flags |= D_BADSECT;
   1155 		bad144intern(wd);
   1156 		return 0;
   1157 #endif
   1158 #ifdef WD_SOFTBADSECT
   1159 	case DIOCBSLIST :
   1160 	{
   1161 		u_int32_t count, missing, skip;
   1162 		struct disk_badsecinfo dbsi;
   1163 		struct disk_badsectors *dbs;
   1164 		size_t available;
   1165 		caddr_t laddr;
   1166 
   1167 		dbsi = *(struct disk_badsecinfo *)addr;
   1168 		missing = wd->sc_bscount;
   1169 		count = 0;
   1170 		available = dbsi.dbsi_bufsize;
   1171 		skip = dbsi.dbsi_skip;
   1172 		laddr = dbsi.dbsi_buffer;
   1173 
   1174 		/*
   1175 		 * We start this loop with the expectation that all of the
   1176 		 * entries will be missed and decrement this counter each
   1177 		 * time we either skip over one (already copied out) or
   1178 		 * we actually copy it back to user space.  The structs
   1179 		 * holding the bad sector information are copied directly
   1180 		 * back to user space whilst the summary is returned via
   1181 		 * the struct passed in via the ioctl.
   1182 		 */
   1183 		SLIST_FOREACH(dbs, &wd->sc_bslist, dbs_next) {
   1184 			if (skip > 0) {
   1185 				missing--;
   1186 				skip--;
   1187 				continue;
   1188 			}
   1189 			if (available < sizeof(*dbs))
   1190 				break;
   1191 			available -= sizeof(*dbs);
   1192 			copyout(dbs, laddr, sizeof(*dbs));
   1193 			laddr += sizeof(*dbs);
   1194 			missing--;
   1195 			count++;
   1196 		}
   1197 		dbsi.dbsi_left = missing;
   1198 		dbsi.dbsi_copied = count;
   1199 		*(struct disk_badsecinfo *)addr = dbsi;
   1200 		return 0;
   1201 	}
   1202 
   1203 	case DIOCBSFLUSH :
   1204 		/* Clean out the bad sector list */
   1205 		while (!SLIST_EMPTY(&wd->sc_bslist)) {
   1206 			void *head = SLIST_FIRST(&wd->sc_bslist);
   1207 			SLIST_REMOVE_HEAD(&wd->sc_bslist, dbs_next);
   1208 			free(head, M_TEMP);
   1209 		}
   1210 		wd->sc_bscount = 0;
   1211 		return 0;
   1212 #endif
   1213 	case DIOCGDINFO:
   1214 		*(struct disklabel *)addr = *(wd->sc_dk.dk_label);
   1215 		return 0;
   1216 #ifdef __HAVE_OLD_DISKLABEL
   1217 	case ODIOCGDINFO:
   1218 		newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
   1219 		if (newlabel == NULL)
   1220 			return EIO;
   1221 		*newlabel = *(wd->sc_dk.dk_label);
   1222 		if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
   1223 			memcpy(addr, newlabel, sizeof (struct olddisklabel));
   1224 		else
   1225 			error = ENOTTY;
   1226 		free(newlabel, M_TEMP);
   1227 		return error;
   1228 #endif
   1229 
   1230 	case DIOCGPART:
   1231 		((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
   1232 		((struct partinfo *)addr)->part =
   1233 		    &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
   1234 		return 0;
   1235 
   1236 	case DIOCWDINFO:
   1237 	case DIOCSDINFO:
   1238 #ifdef __HAVE_OLD_DISKLABEL
   1239 	case ODIOCWDINFO:
   1240 	case ODIOCSDINFO:
   1241 #endif
   1242 	{
   1243 		struct disklabel *lp;
   1244 
   1245 		if ((flag & FWRITE) == 0)
   1246 			return EBADF;
   1247 
   1248 #ifdef __HAVE_OLD_DISKLABEL
   1249 		if (xfer == ODIOCSDINFO || xfer == ODIOCWDINFO) {
   1250 			newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
   1251 			if (newlabel == NULL)
   1252 				return EIO;
   1253 			memset(newlabel, 0, sizeof newlabel);
   1254 			memcpy(newlabel, addr, sizeof (struct olddisklabel));
   1255 			lp = newlabel;
   1256 		} else
   1257 #endif
   1258 		lp = (struct disklabel *)addr;
   1259 
   1260 		if ((error = lockmgr(&wd->sc_dk.dk_openlock, LK_EXCLUSIVE,
   1261 				     NULL)) != 0)
   1262 			goto bad;
   1263 		wd->sc_flags |= WDF_LABELLING;
   1264 
   1265 		error = setdisklabel(wd->sc_dk.dk_label,
   1266 		    lp, /*wd->sc_dk.dk_openmask : */0,
   1267 		    wd->sc_dk.dk_cpulabel);
   1268 		if (error == 0) {
   1269 			if (wd->drvp->state > RESET) {
   1270 				s = splbio();
   1271 				wd->drvp->drive_flags |= DRIVE_RESET;
   1272 				splx(s);
   1273 			}
   1274 			if (xfer == DIOCWDINFO
   1275 #ifdef __HAVE_OLD_DISKLABEL
   1276 			    || xfer == ODIOCWDINFO
   1277 #endif
   1278 			    )
   1279 				error = writedisklabel(WDLABELDEV(dev),
   1280 				    wdstrategy, wd->sc_dk.dk_label,
   1281 				    wd->sc_dk.dk_cpulabel);
   1282 		}
   1283 
   1284 		wd->sc_flags &= ~WDF_LABELLING;
   1285 		(void) lockmgr(&wd->sc_dk.dk_openlock, LK_RELEASE, NULL);
   1286 bad:
   1287 #ifdef __HAVE_OLD_DISKLABEL
   1288 		if (newlabel != NULL)
   1289 			free(newlabel, M_TEMP);
   1290 #endif
   1291 		return error;
   1292 	}
   1293 
   1294 	case DIOCKLABEL:
   1295 		if (*(int *)addr)
   1296 			wd->sc_flags |= WDF_KLABEL;
   1297 		else
   1298 			wd->sc_flags &= ~WDF_KLABEL;
   1299 		return 0;
   1300 
   1301 	case DIOCWLABEL:
   1302 		if ((flag & FWRITE) == 0)
   1303 			return EBADF;
   1304 		if (*(int *)addr)
   1305 			wd->sc_flags |= WDF_WLABEL;
   1306 		else
   1307 			wd->sc_flags &= ~WDF_WLABEL;
   1308 		return 0;
   1309 
   1310 	case DIOCGDEFLABEL:
   1311 		wdgetdefaultlabel(wd, (struct disklabel *)addr);
   1312 		return 0;
   1313 #ifdef __HAVE_OLD_DISKLABEL
   1314 	case ODIOCGDEFLABEL:
   1315 		newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
   1316 		if (newlabel == NULL)
   1317 			return EIO;
   1318 		wdgetdefaultlabel(wd, newlabel);
   1319 		if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
   1320 			memcpy(addr, &newlabel, sizeof (struct olddisklabel));
   1321 		else
   1322 			error = ENOTTY;
   1323 		free(newlabel, M_TEMP);
   1324 		return error;
   1325 #endif
   1326 
   1327 #ifdef notyet
   1328 	case DIOCWFORMAT:
   1329 		if ((flag & FWRITE) == 0)
   1330 			return EBADF;
   1331 		{
   1332 		register struct format_op *fop;
   1333 		struct iovec aiov;
   1334 		struct uio auio;
   1335 
   1336 		fop = (struct format_op *)addr;
   1337 		aiov.iov_base = fop->df_buf;
   1338 		aiov.iov_len = fop->df_count;
   1339 		auio.uio_iov = &aiov;
   1340 		auio.uio_iovcnt = 1;
   1341 		auio.uio_resid = fop->df_count;
   1342 		auio.uio_segflg = 0;
   1343 		auio.uio_offset =
   1344 			fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
   1345 		auio.uio_procp = p;
   1346 		error = physio(wdformat, NULL, dev, B_WRITE, minphys,
   1347 		    &auio);
   1348 		fop->df_count -= auio.uio_resid;
   1349 		fop->df_reg[0] = wdc->sc_status;
   1350 		fop->df_reg[1] = wdc->sc_error;
   1351 		return error;
   1352 		}
   1353 #endif
   1354 	case DIOCGCACHE:
   1355 		return wd_getcache(wd, (int *)addr);
   1356 
   1357 	case DIOCSCACHE:
   1358 		return wd_setcache(wd, *(int *)addr);
   1359 
   1360 	case DIOCCACHESYNC:
   1361 		return wd_flushcache(wd, AT_WAIT);
   1362 
   1363 	case ATAIOCCOMMAND:
   1364 		/*
   1365 		 * Make sure this command is (relatively) safe first
   1366 		 */
   1367 		if ((((atareq_t *) addr)->flags & ATACMD_READ) == 0 &&
   1368 		    (flag & FWRITE) == 0)
   1369 			return (EBADF);
   1370 		{
   1371 		struct wd_ioctl *wi;
   1372 		atareq_t *atareq = (atareq_t *) addr;
   1373 		int error;
   1374 
   1375 		wi = wi_get();
   1376 		wi->wi_softc = wd;
   1377 		wi->wi_atareq = *atareq;
   1378 
   1379 		if (atareq->datalen && atareq->flags &
   1380 		    (ATACMD_READ | ATACMD_WRITE)) {
   1381 			wi->wi_iov.iov_base = atareq->databuf;
   1382 			wi->wi_iov.iov_len = atareq->datalen;
   1383 			wi->wi_uio.uio_iov = &wi->wi_iov;
   1384 			wi->wi_uio.uio_iovcnt = 1;
   1385 			wi->wi_uio.uio_resid = atareq->datalen;
   1386 			wi->wi_uio.uio_offset = 0;
   1387 			wi->wi_uio.uio_segflg = UIO_USERSPACE;
   1388 			wi->wi_uio.uio_rw =
   1389 			    (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE;
   1390 			wi->wi_uio.uio_procp = p;
   1391 			error = physio(wdioctlstrategy, &wi->wi_bp, dev,
   1392 			    (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE,
   1393 			    minphys, &wi->wi_uio);
   1394 		} else {
   1395 			/* No need to call physio if we don't have any
   1396 			   user data */
   1397 			wi->wi_bp.b_flags = 0;
   1398 			wi->wi_bp.b_data = 0;
   1399 			wi->wi_bp.b_bcount = 0;
   1400 			wi->wi_bp.b_dev = 0;
   1401 			wi->wi_bp.b_proc = p;
   1402 			wdioctlstrategy(&wi->wi_bp);
   1403 			error = wi->wi_bp.b_error;
   1404 		}
   1405 		*atareq = wi->wi_atareq;
   1406 		wi_free(wi);
   1407 		return(error);
   1408 		}
   1409 
   1410 	case DIOCAWEDGE:
   1411 	    {
   1412 	    	struct dkwedge_info *dkw = (void *) addr;
   1413 
   1414 		if ((flag & FWRITE) == 0)
   1415 			return (EBADF);
   1416 
   1417 		/* If the ioctl happens here, the parent is us. */
   1418 		strcpy(dkw->dkw_parent, wd->sc_dev.dv_xname);
   1419 		return (dkwedge_add(dkw));
   1420 	    }
   1421 
   1422 	case DIOCDWEDGE:
   1423 	    {
   1424 	    	struct dkwedge_info *dkw = (void *) addr;
   1425 
   1426 		if ((flag & FWRITE) == 0)
   1427 			return (EBADF);
   1428 
   1429 		/* If the ioctl happens here, the parent is us. */
   1430 		strcpy(dkw->dkw_parent, wd->sc_dev.dv_xname);
   1431 		return (dkwedge_del(dkw));
   1432 	    }
   1433 
   1434 	case DIOCLWEDGES:
   1435 	    {
   1436 	    	struct dkwedge_list *dkwl = (void *) addr;
   1437 
   1438 		return (dkwedge_list(&wd->sc_dk, dkwl, p));
   1439 	    }
   1440 
   1441 	default:
   1442 		return ENOTTY;
   1443 	}
   1444 
   1445 #ifdef DIAGNOSTIC
   1446 	panic("wdioctl: impossible");
   1447 #endif
   1448 }
   1449 
   1450 #ifdef B_FORMAT
   1451 int
   1452 wdformat(struct buf *bp)
   1453 {
   1454 
   1455 	bp->b_flags |= B_FORMAT;
   1456 	return wdstrategy(bp);
   1457 }
   1458 #endif
   1459 
   1460 int
   1461 wdsize(dev_t dev)
   1462 {
   1463 	struct wd_softc *wd;
   1464 	int part, omask;
   1465 	int size;
   1466 
   1467 	ATADEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS);
   1468 
   1469 	wd = device_lookup(&wd_cd, WDUNIT(dev));
   1470 	if (wd == NULL)
   1471 		return (-1);
   1472 
   1473 	part = WDPART(dev);
   1474 	omask = wd->sc_dk.dk_openmask & (1 << part);
   1475 
   1476 	if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0)
   1477 		return (-1);
   1478 	if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
   1479 		size = -1;
   1480 	else
   1481 		size = wd->sc_dk.dk_label->d_partitions[part].p_size *
   1482 		    (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
   1483 	if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0)
   1484 		return (-1);
   1485 	return (size);
   1486 }
   1487 
   1488 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
   1489 static int wddoingadump = 0;
   1490 static int wddumprecalibrated = 0;
   1491 static int wddumpmulti = 1;
   1492 
   1493 /*
   1494  * Dump core after a system crash.
   1495  */
   1496 int
   1497 wddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
   1498 {
   1499 	struct wd_softc *wd;	/* disk unit to do the I/O */
   1500 	struct disklabel *lp;   /* disk's disklabel */
   1501 	int part, err;
   1502 	int nblks;	/* total number of sectors left to write */
   1503 
   1504 	/* Check if recursive dump; if so, punt. */
   1505 	if (wddoingadump)
   1506 		return EFAULT;
   1507 	wddoingadump = 1;
   1508 
   1509 	wd = device_lookup(&wd_cd, WDUNIT(dev));
   1510 	if (wd == NULL)
   1511 		return (ENXIO);
   1512 
   1513 	part = WDPART(dev);
   1514 
   1515 	/* Convert to disk sectors.  Request must be a multiple of size. */
   1516 	lp = wd->sc_dk.dk_label;
   1517 	if ((size % lp->d_secsize) != 0)
   1518 		return EFAULT;
   1519 	nblks = size / lp->d_secsize;
   1520 	blkno = blkno / (lp->d_secsize / DEV_BSIZE);
   1521 
   1522 	/* Check transfer bounds against partition size. */
   1523 	if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
   1524 		return EINVAL;
   1525 
   1526 	/* Offset block number to start of partition. */
   1527 	blkno += lp->d_partitions[part].p_offset;
   1528 
   1529 	/* Recalibrate, if first dump transfer. */
   1530 	if (wddumprecalibrated == 0) {
   1531 		wddumpmulti = wd->sc_multi;
   1532 		wddumprecalibrated = 1;
   1533 		(*wd->atabus->ata_reset_drive)(wd->drvp,
   1534 					       AT_POLL | AT_RST_EMERG);
   1535 		wd->drvp->state = RESET;
   1536 	}
   1537 
   1538 	while (nblks > 0) {
   1539 		wd->sc_bp = NULL;
   1540 		wd->sc_wdc_bio.blkno = blkno;
   1541 		wd->sc_wdc_bio.flags = ATA_POLL;
   1542 		if (wd->sc_flags & WDF_LBA48 &&
   1543 		    (blkno > LBA48_THRESHOLD ||
   1544 	    	    (wd->sc_quirks & WD_QUIRK_FORCE_LBA48) != 0))
   1545 			wd->sc_wdc_bio.flags |= ATA_LBA48;
   1546 		if (wd->sc_flags & WDF_LBA)
   1547 			wd->sc_wdc_bio.flags |= ATA_LBA;
   1548 		wd->sc_wdc_bio.bcount =
   1549 			min(nblks, wddumpmulti) * lp->d_secsize;
   1550 		wd->sc_wdc_bio.databuf = va;
   1551 #ifndef WD_DUMP_NOT_TRUSTED
   1552 		switch (wd->atabus->ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
   1553 		case ATACMD_TRY_AGAIN:
   1554 			panic("wddump: try again");
   1555 			break;
   1556 		case ATACMD_QUEUED:
   1557 			panic("wddump: polled command has been queued");
   1558 			break;
   1559 		case ATACMD_COMPLETE:
   1560 			break;
   1561 		}
   1562 		switch(wd->sc_wdc_bio.error) {
   1563 		case TIMEOUT:
   1564 			printf("wddump: device timed out");
   1565 			err = EIO;
   1566 			break;
   1567 		case ERR_DF:
   1568 			printf("wddump: drive fault");
   1569 			err = EIO;
   1570 			break;
   1571 		case ERR_DMA:
   1572 			printf("wddump: DMA error");
   1573 			err = EIO;
   1574 			break;
   1575 		case ERROR:
   1576 			printf("wddump: ");
   1577 			wdperror(wd);
   1578 			err = EIO;
   1579 			break;
   1580 		case NOERROR:
   1581 			err = 0;
   1582 			break;
   1583 		default:
   1584 			panic("wddump: unknown error type");
   1585 		}
   1586 		if (err != 0) {
   1587 			printf("\n");
   1588 			return err;
   1589 		}
   1590 #else	/* WD_DUMP_NOT_TRUSTED */
   1591 		/* Let's just talk about this first... */
   1592 		printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
   1593 		    unit, va, cylin, head, sector);
   1594 		delay(500 * 1000);	/* half a second */
   1595 #endif
   1596 
   1597 		/* update block count */
   1598 		nblks -= min(nblks, wddumpmulti);
   1599 		blkno += min(nblks, wddumpmulti);
   1600 		va += min(nblks, wddumpmulti) * lp->d_secsize;
   1601 	}
   1602 
   1603 	wddoingadump = 0;
   1604 	return 0;
   1605 }
   1606 
   1607 #ifdef HAS_BAD144_HANDLING
   1608 /*
   1609  * Internalize the bad sector table.
   1610  */
   1611 void
   1612 bad144intern(struct wd_softc *wd)
   1613 {
   1614 	struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
   1615 	struct disklabel *lp = wd->sc_dk.dk_label;
   1616 	int i = 0;
   1617 
   1618 	ATADEBUG_PRINT(("bad144intern\n"), DEBUG_XFERS);
   1619 
   1620 	for (; i < NBT_BAD; i++) {
   1621 		if (bt->bt_bad[i].bt_cyl == 0xffff)
   1622 			break;
   1623 		wd->sc_badsect[i] =
   1624 		    bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
   1625 		    (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
   1626 		    (bt->bt_bad[i].bt_trksec & 0xff);
   1627 	}
   1628 	for (; i < NBT_BAD+1; i++)
   1629 		wd->sc_badsect[i] = -1;
   1630 }
   1631 #endif
   1632 
   1633 int
   1634 wd_get_params(struct wd_softc *wd, u_int8_t flags, struct ataparams *params)
   1635 {
   1636 	switch (wd->atabus->ata_get_params(wd->drvp, flags, params)) {
   1637 	case CMD_AGAIN:
   1638 		return 1;
   1639 	case CMD_ERR:
   1640 		/*
   1641 		 * We `know' there's a drive here; just assume it's old.
   1642 		 * This geometry is only used to read the MBR and print a
   1643 		 * (false) attach message.
   1644 		 */
   1645 		strncpy(params->atap_model, "ST506",
   1646 		    sizeof params->atap_model);
   1647 		params->atap_config = ATA_CFG_FIXED;
   1648 		params->atap_cylinders = 1024;
   1649 		params->atap_heads = 8;
   1650 		params->atap_sectors = 17;
   1651 		params->atap_multi = 1;
   1652 		params->atap_capabilities1 = params->atap_capabilities2 = 0;
   1653 		wd->drvp->ata_vers = -1; /* Mark it as pre-ATA */
   1654 		return 0;
   1655 	case CMD_OK:
   1656 		return 0;
   1657 	default:
   1658 		panic("wd_get_params: bad return code from ata_get_params");
   1659 		/* NOTREACHED */
   1660 	}
   1661 }
   1662 
   1663 int
   1664 wd_getcache(struct wd_softc *wd, int *bitsp)
   1665 {
   1666 	struct ataparams params;
   1667 
   1668 	if (wd_get_params(wd, AT_WAIT, &params) != 0)
   1669 		return EIO;
   1670 	if (params.atap_cmd_set1 == 0x0000 ||
   1671 	    params.atap_cmd_set1 == 0xffff ||
   1672 	    (params.atap_cmd_set1 & WDC_CMD1_CACHE) == 0) {
   1673 		*bitsp = 0;
   1674 		return 0;
   1675 	}
   1676 	*bitsp = DKCACHE_WCHANGE | DKCACHE_READ;
   1677 	if (params.atap_cmd1_en & WDC_CMD1_CACHE)
   1678 		*bitsp |= DKCACHE_WRITE;
   1679 
   1680 	return 0;
   1681 }
   1682 
   1683 const char at_errbits[] = "\20\10ERROR\11TIMEOU\12DF";
   1684 
   1685 int
   1686 wd_setcache(struct wd_softc *wd, int bits)
   1687 {
   1688 	struct ataparams params;
   1689 	struct ata_command ata_c;
   1690 
   1691 	if (wd_get_params(wd, AT_WAIT, &params) != 0)
   1692 		return EIO;
   1693 
   1694 	if (params.atap_cmd_set1 == 0x0000 ||
   1695 	    params.atap_cmd_set1 == 0xffff ||
   1696 	    (params.atap_cmd_set1 & WDC_CMD1_CACHE) == 0)
   1697 		return EOPNOTSUPP;
   1698 
   1699 	if ((bits & DKCACHE_READ) == 0 ||
   1700 	    (bits & DKCACHE_SAVE) != 0)
   1701 		return EOPNOTSUPP;
   1702 
   1703 	memset(&ata_c, 0, sizeof(struct ata_command));
   1704 	ata_c.r_command = SET_FEATURES;
   1705 	ata_c.r_st_bmask = 0;
   1706 	ata_c.r_st_pmask = 0;
   1707 	ata_c.timeout = 30000; /* 30s timeout */
   1708 	ata_c.flags = AT_WAIT;
   1709 	if (bits & DKCACHE_WRITE)
   1710 		ata_c.r_features = WDSF_WRITE_CACHE_EN;
   1711 	else
   1712 		ata_c.r_features = WDSF_WRITE_CACHE_DS;
   1713 	if (wd->atabus->ata_exec_command(wd->drvp, &ata_c) != ATACMD_COMPLETE) {
   1714 		printf("%s: wd_setcache command not complete\n",
   1715 		    wd->sc_dev.dv_xname);
   1716 		return EIO;
   1717 	}
   1718 	if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
   1719 		char sbuf[sizeof(at_errbits) + 64];
   1720 		bitmask_snprintf(ata_c.flags, at_errbits, sbuf, sizeof(sbuf));
   1721 		printf("%s: wd_setcache: status=%s\n", wd->sc_dev.dv_xname,
   1722 		    sbuf);
   1723 		return EIO;
   1724 	}
   1725 	return 0;
   1726 }
   1727 
   1728 int
   1729 wd_standby(struct wd_softc *wd, int flags)
   1730 {
   1731 	struct ata_command ata_c;
   1732 
   1733 	memset(&ata_c, 0, sizeof(struct ata_command));
   1734 	ata_c.r_command = WDCC_STANDBY_IMMED;
   1735 	ata_c.r_st_bmask = WDCS_DRDY;
   1736 	ata_c.r_st_pmask = WDCS_DRDY;
   1737 	ata_c.flags = flags;
   1738 	ata_c.timeout = 30000; /* 30s timeout */
   1739 	if (wd->atabus->ata_exec_command(wd->drvp, &ata_c) != ATACMD_COMPLETE) {
   1740 		printf("%s: standby immediate command didn't complete\n",
   1741 		    wd->sc_dev.dv_xname);
   1742 		return EIO;
   1743 	}
   1744 	if (ata_c.flags & AT_ERROR) {
   1745 		if (ata_c.r_error == WDCE_ABRT) /* command not supported */
   1746 			return ENODEV;
   1747 	}
   1748 	if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
   1749 		char sbuf[sizeof(at_errbits) + 64];
   1750 		bitmask_snprintf(ata_c.flags, at_errbits, sbuf, sizeof(sbuf));
   1751 		printf("%s: wd_standby: status=%s\n", wd->sc_dev.dv_xname,
   1752 		    sbuf);
   1753 		return EIO;
   1754 	}
   1755 	return 0;
   1756 }
   1757 
   1758 int
   1759 wd_flushcache(struct wd_softc *wd, int flags)
   1760 {
   1761 	struct ata_command ata_c;
   1762 
   1763 	/*
   1764 	 * WDCC_FLUSHCACHE is here since ATA-4, but some drives report
   1765 	 * only ATA-2 and still support it.
   1766 	 */
   1767 	if (wd->drvp->ata_vers < 4 &&
   1768 	    ((wd->sc_params.atap_cmd_set2 & WDC_CMD2_FC) == 0 ||
   1769 	    wd->sc_params.atap_cmd_set2 == 0xffff))
   1770 		return ENODEV;
   1771 	memset(&ata_c, 0, sizeof(struct ata_command));
   1772 	if ((wd->sc_params.atap_cmd2_en & ATA_CMD2_LBA48) != 0 &&
   1773 	    (wd->sc_params.atap_cmd2_en & ATA_CMD2_FCE) != 0)
   1774 		ata_c.r_command = WDCC_FLUSHCACHE_EXT;
   1775 	else
   1776 		ata_c.r_command = WDCC_FLUSHCACHE;
   1777 	ata_c.r_st_bmask = WDCS_DRDY;
   1778 	ata_c.r_st_pmask = WDCS_DRDY;
   1779 	ata_c.flags = flags;
   1780 	ata_c.timeout = 30000; /* 30s timeout */
   1781 	if (wd->atabus->ata_exec_command(wd->drvp, &ata_c) != ATACMD_COMPLETE) {
   1782 		printf("%s: flush cache command didn't complete\n",
   1783 		    wd->sc_dev.dv_xname);
   1784 		return EIO;
   1785 	}
   1786 	if (ata_c.flags & AT_ERROR) {
   1787 		if (ata_c.r_error == WDCE_ABRT) /* command not supported */
   1788 			return ENODEV;
   1789 	}
   1790 	if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
   1791 		char sbuf[sizeof(at_errbits) + 64];
   1792 		bitmask_snprintf(ata_c.flags, at_errbits, sbuf, sizeof(sbuf));
   1793 		printf("%s: wd_flushcache: status=%s\n", wd->sc_dev.dv_xname,
   1794 		    sbuf);
   1795 		return EIO;
   1796 	}
   1797 	return 0;
   1798 }
   1799 
   1800 void
   1801 wd_shutdown(void *arg)
   1802 {
   1803 	struct wd_softc *wd = arg;
   1804 	wd_flushcache(wd, AT_POLL);
   1805 }
   1806 
   1807 /*
   1808  * Allocate space for a ioctl queue structure.  Mostly taken from
   1809  * scsipi_ioctl.c
   1810  */
   1811 struct wd_ioctl *
   1812 wi_get(void)
   1813 {
   1814 	struct wd_ioctl *wi;
   1815 	int s;
   1816 
   1817 	wi = malloc(sizeof(struct wd_ioctl), M_TEMP, M_WAITOK|M_ZERO);
   1818 	simple_lock_init(&wi->wi_bp.b_interlock);
   1819 	s = splbio();
   1820 	LIST_INSERT_HEAD(&wi_head, wi, wi_list);
   1821 	splx(s);
   1822 	return (wi);
   1823 }
   1824 
   1825 /*
   1826  * Free an ioctl structure and remove it from our list
   1827  */
   1828 
   1829 void
   1830 wi_free(struct wd_ioctl *wi)
   1831 {
   1832 	int s;
   1833 
   1834 	s = splbio();
   1835 	LIST_REMOVE(wi, wi_list);
   1836 	splx(s);
   1837 	free(wi, M_TEMP);
   1838 }
   1839 
   1840 /*
   1841  * Find a wd_ioctl structure based on the struct buf.
   1842  */
   1843 
   1844 struct wd_ioctl *
   1845 wi_find(struct buf *bp)
   1846 {
   1847 	struct wd_ioctl *wi;
   1848 	int s;
   1849 
   1850 	s = splbio();
   1851 	for (wi = wi_head.lh_first; wi != 0; wi = wi->wi_list.le_next)
   1852 		if (bp == &wi->wi_bp)
   1853 			break;
   1854 	splx(s);
   1855 	return (wi);
   1856 }
   1857 
   1858 /*
   1859  * Ioctl pseudo strategy routine
   1860  *
   1861  * This is mostly stolen from scsipi_ioctl.c:scsistrategy().  What
   1862  * happens here is:
   1863  *
   1864  * - wdioctl() queues a wd_ioctl structure.
   1865  *
   1866  * - wdioctl() calls physio/wdioctlstrategy based on whether or not
   1867  *   user space I/O is required.  If physio() is called, physio() eventually
   1868  *   calls wdioctlstrategy().
   1869  *
   1870  * - In either case, wdioctlstrategy() calls wd->atabus->ata_exec_command()
   1871  *   to perform the actual command
   1872  *
   1873  * The reason for the use of the pseudo strategy routine is because
   1874  * when doing I/O to/from user space, physio _really_ wants to be in
   1875  * the loop.  We could put the entire buffer into the ioctl request
   1876  * structure, but that won't scale if we want to do things like download
   1877  * microcode.
   1878  */
   1879 
   1880 void
   1881 wdioctlstrategy(struct buf *bp)
   1882 {
   1883 	struct wd_ioctl *wi;
   1884 	struct ata_command ata_c;
   1885 	int error = 0;
   1886 
   1887 	wi = wi_find(bp);
   1888 	if (wi == NULL) {
   1889 		printf("user_strat: No ioctl\n");
   1890 		error = EINVAL;
   1891 		goto bad;
   1892 	}
   1893 
   1894 	memset(&ata_c, 0, sizeof(ata_c));
   1895 
   1896 	/*
   1897 	 * Abort if physio broke up the transfer
   1898 	 */
   1899 
   1900 	if (bp->b_bcount != wi->wi_atareq.datalen) {
   1901 		printf("physio split wd ioctl request... cannot proceed\n");
   1902 		error = EIO;
   1903 		goto bad;
   1904 	}
   1905 
   1906 	/*
   1907 	 * Abort if we didn't get a buffer size that was a multiple of
   1908 	 * our sector size (or was larger than NBBY)
   1909 	 */
   1910 
   1911 	if ((bp->b_bcount % wi->wi_softc->sc_dk.dk_label->d_secsize) != 0 ||
   1912 	    (bp->b_bcount / wi->wi_softc->sc_dk.dk_label->d_secsize) >=
   1913 	     (1 << NBBY)) {
   1914 		error = EINVAL;
   1915 		goto bad;
   1916 	}
   1917 
   1918 	/*
   1919 	 * Make sure a timeout was supplied in the ioctl request
   1920 	 */
   1921 
   1922 	if (wi->wi_atareq.timeout == 0) {
   1923 		error = EINVAL;
   1924 		goto bad;
   1925 	}
   1926 
   1927 	if (wi->wi_atareq.flags & ATACMD_READ)
   1928 		ata_c.flags |= AT_READ;
   1929 	else if (wi->wi_atareq.flags & ATACMD_WRITE)
   1930 		ata_c.flags |= AT_WRITE;
   1931 
   1932 	if (wi->wi_atareq.flags & ATACMD_READREG)
   1933 		ata_c.flags |= AT_READREG;
   1934 
   1935 	ata_c.flags |= AT_WAIT;
   1936 
   1937 	ata_c.timeout = wi->wi_atareq.timeout;
   1938 	ata_c.r_command = wi->wi_atareq.command;
   1939 	ata_c.r_head = wi->wi_atareq.head & 0x0f;
   1940 	ata_c.r_cyl = wi->wi_atareq.cylinder;
   1941 	ata_c.r_sector = wi->wi_atareq.sec_num;
   1942 	ata_c.r_count = wi->wi_atareq.sec_count;
   1943 	ata_c.r_features = wi->wi_atareq.features;
   1944 	ata_c.r_st_bmask = WDCS_DRDY;
   1945 	ata_c.r_st_pmask = WDCS_DRDY;
   1946 	ata_c.data = wi->wi_bp.b_data;
   1947 	ata_c.bcount = wi->wi_bp.b_bcount;
   1948 
   1949 	if (wi->wi_softc->atabus->ata_exec_command(wi->wi_softc->drvp, &ata_c)
   1950 	    != ATACMD_COMPLETE) {
   1951 		wi->wi_atareq.retsts = ATACMD_ERROR;
   1952 		goto bad;
   1953 	}
   1954 
   1955 	if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
   1956 		if (ata_c.flags & AT_ERROR) {
   1957 			wi->wi_atareq.retsts = ATACMD_ERROR;
   1958 			wi->wi_atareq.error = ata_c.r_error;
   1959 		} else if (ata_c.flags & AT_DF)
   1960 			wi->wi_atareq.retsts = ATACMD_DF;
   1961 		else
   1962 			wi->wi_atareq.retsts = ATACMD_TIMEOUT;
   1963 	} else {
   1964 		wi->wi_atareq.retsts = ATACMD_OK;
   1965 		if (wi->wi_atareq.flags & ATACMD_READREG) {
   1966 			wi->wi_atareq.head = ata_c.r_head ;
   1967 			wi->wi_atareq.cylinder = ata_c.r_cyl;
   1968 			wi->wi_atareq.sec_num = ata_c.r_sector;
   1969 			wi->wi_atareq.sec_count = ata_c.r_count;
   1970 			wi->wi_atareq.features = ata_c.r_features;
   1971 			wi->wi_atareq.error = ata_c.r_error;
   1972 		}
   1973 	}
   1974 
   1975 	bp->b_error = 0;
   1976 	biodone(bp);
   1977 	return;
   1978 bad:
   1979 	bp->b_flags |= B_ERROR;
   1980 	bp->b_error = error;
   1981 	biodone(bp);
   1982 }
   1983