Home | History | Annotate | Line # | Download | only in ata
wd.c revision 1.454
      1 /*	$NetBSD: wd.c,v 1.454 2020/01/13 21:20:17 jdolecek 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  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 /*-
     28  * Copyright (c) 1998, 2003, 2004 The NetBSD Foundation, Inc.
     29  * All rights reserved.
     30  *
     31  * This code is derived from software contributed to The NetBSD Foundation
     32  * by Charles M. Hannum and by Onno van der Linden.
     33  *
     34  * Redistribution and use in source and binary forms, with or without
     35  * modification, are permitted provided that the following conditions
     36  * are met:
     37  * 1. Redistributions of source code must retain the above copyright
     38  *    notice, this list of conditions and the following disclaimer.
     39  * 2. Redistributions in binary form must reproduce the above copyright
     40  *    notice, this list of conditions and the following disclaimer in the
     41  *    documentation and/or other materials provided with the distribution.
     42  *
     43  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     44  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     45  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     46  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     47  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     48  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     49  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     50  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     51  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     52  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     53  * POSSIBILITY OF SUCH DAMAGE.
     54  */
     55 
     56 #include <sys/cdefs.h>
     57 __KERNEL_RCSID(0, "$NetBSD: wd.c,v 1.454 2020/01/13 21:20:17 jdolecek Exp $");
     58 
     59 #include "opt_ata.h"
     60 #include "opt_wd.h"
     61 
     62 #include <sys/param.h>
     63 #include <sys/systm.h>
     64 #include <sys/kernel.h>
     65 #include <sys/conf.h>
     66 #include <sys/file.h>
     67 #include <sys/stat.h>
     68 #include <sys/ioctl.h>
     69 #include <sys/buf.h>
     70 #include <sys/bufq.h>
     71 #include <sys/uio.h>
     72 #include <sys/device.h>
     73 #include <sys/disklabel.h>
     74 #include <sys/disk.h>
     75 #include <sys/syslog.h>
     76 #include <sys/proc.h>
     77 #include <sys/reboot.h>
     78 #include <sys/vnode.h>
     79 #include <sys/rndsource.h>
     80 
     81 #include <sys/intr.h>
     82 #include <sys/bus.h>
     83 
     84 #include <dev/ata/atareg.h>
     85 #include <dev/ata/atavar.h>
     86 #include <dev/ata/wdvar.h>
     87 #include <dev/ic/wdcreg.h>
     88 #include <sys/ataio.h>
     89 #include "locators.h"
     90 
     91 #include <prop/proplib.h>
     92 
     93 #define	WDIORETRIES_SINGLE 4	/* number of retries for single-sector */
     94 #define	WDIORETRIES	5	/* number of retries before giving up */
     95 #define	RECOVERYTIME hz/2	/* time to wait before retrying a cmd */
     96 
     97 #define	WDUNIT(dev)		DISKUNIT(dev)
     98 #define	WDPART(dev)		DISKPART(dev)
     99 #define	WDMINOR(unit, part)	DISKMINOR(unit, part)
    100 #define	MAKEWDDEV(maj, unit, part)	MAKEDISKDEV(maj, unit, part)
    101 
    102 #define	WDLABELDEV(dev)	(MAKEWDDEV(major(dev), WDUNIT(dev), RAW_PART))
    103 
    104 #define DEBUG_FUNCS  0x08
    105 #define DEBUG_PROBE  0x10
    106 #define DEBUG_DETACH 0x20
    107 #define	DEBUG_XFERS  0x40
    108 #ifdef ATADEBUG
    109 #ifndef ATADEBUG_WD_MASK
    110 #define ATADEBUG_WD_MASK 0x0
    111 #endif
    112 int wdcdebug_wd_mask = ATADEBUG_WD_MASK;
    113 #define ATADEBUG_PRINT(args, level) \
    114 	if (wdcdebug_wd_mask & (level)) \
    115 		printf args
    116 #else
    117 #define ATADEBUG_PRINT(args, level)
    118 #endif
    119 
    120 static int	wdprobe(device_t, cfdata_t, void *);
    121 static void	wdattach(device_t, device_t, void *);
    122 static int	wddetach(device_t, int);
    123 static void	wdperror(const struct wd_softc *, struct ata_xfer *);
    124 
    125 static void	wdminphys(struct buf *);
    126 
    127 static int	wd_firstopen(device_t, dev_t, int, int);
    128 static int	wd_lastclose(device_t);
    129 static bool	wd_suspend(device_t, const pmf_qual_t *);
    130 static int	wd_standby(struct wd_softc *, int);
    131 
    132 CFATTACH_DECL3_NEW(wd, sizeof(struct wd_softc),
    133     wdprobe, wdattach, wddetach, NULL, NULL, NULL, DVF_DETACH_SHUTDOWN);
    134 
    135 extern struct cfdriver wd_cd;
    136 
    137 static dev_type_open(wdopen);
    138 static dev_type_close(wdclose);
    139 static dev_type_read(wdread);
    140 static dev_type_write(wdwrite);
    141 static dev_type_ioctl(wdioctl);
    142 static dev_type_strategy(wdstrategy);
    143 static dev_type_dump(wddump);
    144 static dev_type_size(wdsize);
    145 static dev_type_discard(wddiscard);
    146 
    147 const struct bdevsw wd_bdevsw = {
    148 	.d_open = wdopen,
    149 	.d_close = wdclose,
    150 	.d_strategy = wdstrategy,
    151 	.d_ioctl = wdioctl,
    152 	.d_dump = wddump,
    153 	.d_psize = wdsize,
    154 	.d_discard = wddiscard,
    155 	.d_flag = D_DISK
    156 };
    157 
    158 const struct cdevsw wd_cdevsw = {
    159 	.d_open = wdopen,
    160 	.d_close = wdclose,
    161 	.d_read = wdread,
    162 	.d_write = wdwrite,
    163 	.d_ioctl = wdioctl,
    164 	.d_stop = nostop,
    165 	.d_tty = notty,
    166 	.d_poll = nopoll,
    167 	.d_mmap = nommap,
    168 	.d_kqfilter = nokqfilter,
    169 	.d_discard = wddiscard,
    170 	.d_flag = D_DISK
    171 };
    172 
    173 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
    174 static int wddoingadump = 0;
    175 static int wddumprecalibrated = 0;
    176 
    177 /*
    178  * Glue necessary to hook WDCIOCCOMMAND into physio
    179  */
    180 
    181 struct wd_ioctl {
    182 	LIST_ENTRY(wd_ioctl) wi_list;
    183 	struct buf wi_bp;
    184 	struct uio wi_uio;
    185 	struct iovec wi_iov;
    186 	atareq_t wi_atareq;
    187 	struct wd_softc *wi_softc;
    188 };
    189 
    190 static struct	wd_ioctl *wi_find(struct buf *);
    191 static void	wi_free(struct wd_ioctl *);
    192 static struct	wd_ioctl *wi_get(struct wd_softc *);
    193 static void	wdioctlstrategy(struct buf *);
    194 
    195 static void	wdrestart(void *);
    196 static void	wdstart1(struct wd_softc *, struct buf *, struct ata_xfer *);
    197 static int	wd_diskstart(device_t, struct buf *);
    198 static int	wd_dumpblocks(device_t, void *, daddr_t, int);
    199 static void	wd_iosize(device_t, int *);
    200 static int	wd_discard(device_t, off_t, off_t);
    201 static void	wdbioretry(void *);
    202 static void	wdbiorequeue(void *);
    203 static void	wddone(device_t, struct ata_xfer *);
    204 static int	wd_get_params(struct wd_softc *, uint8_t, struct ataparams *);
    205 static void	wd_set_geometry(struct wd_softc *);
    206 static int	wd_flushcache(struct wd_softc *, int, bool);
    207 static int	wd_trim(struct wd_softc *, daddr_t, long);
    208 static bool	wd_shutdown(device_t, int);
    209 
    210 static int wd_getcache(struct wd_softc *, int *);
    211 static int wd_setcache(struct wd_softc *, int);
    212 
    213 static void wd_sysctl_attach(struct wd_softc *);
    214 static void wd_sysctl_detach(struct wd_softc *);
    215 
    216 struct dkdriver wddkdriver = {
    217 	.d_open = wdopen,
    218 	.d_close = wdclose,
    219 	.d_strategy = wdstrategy,
    220 	.d_minphys = wdminphys,
    221 	.d_diskstart = wd_diskstart,
    222 	.d_dumpblocks = wd_dumpblocks,
    223 	.d_iosize = wd_iosize,
    224 	.d_firstopen = wd_firstopen,
    225 	.d_lastclose = wd_lastclose,
    226 	.d_discard = wd_discard
    227 };
    228 
    229 #ifdef HAS_BAD144_HANDLING
    230 static void bad144intern(struct wd_softc *);
    231 #endif
    232 
    233 #define	WD_QUIRK_SPLIT_MOD15_WRITE	0x0001	/* must split certain writes */
    234 #define	WD_QUIRK_BAD_NCQ		0x0002	/* drive NCQ support broken */
    235 
    236 #define	WD_QUIRK_FMT "\20\1SPLIT_MOD15_WRITE\2BAD_NCQ"
    237 
    238 /*
    239  * Quirk table for IDE drives.  Put more-specific matches first, since
    240  * a simple globing routine is used for matching.
    241  */
    242 static const struct wd_quirk {
    243 	const char *wdq_match;		/* inquiry pattern to match */
    244 	int wdq_quirks;			/* drive quirks */
    245 } wd_quirk_table[] = {
    246 	/*
    247 	 * Some Seagate S-ATA drives have a PHY which can get confused
    248 	 * with the way data is packetized by some S-ATA controllers.
    249 	 *
    250 	 * The work-around is to split in two any write transfer whose
    251 	 * sector count % 15 == 1 (assuming 512 byte sectors).
    252 	 *
    253 	 * XXX This is an incomplete list.  There are at least a couple
    254 	 * XXX more model numbers.  If you have trouble with such transfers
    255 	 * XXX (8K is the most common) on Seagate S-ATA drives, please
    256 	 * XXX notify thorpej (at) NetBSD.org.
    257 	 *
    258 	 * The ST360015AS has not yet been confirmed to have this
    259 	 * issue, however, it is the only other drive in the
    260 	 * Seagate Barracuda Serial ATA V family.
    261 	 *
    262 	 */
    263 	{ "ST3120023AS", WD_QUIRK_SPLIT_MOD15_WRITE },
    264 	{ "ST380023AS", WD_QUIRK_SPLIT_MOD15_WRITE },
    265 	{ "ST360015AS", WD_QUIRK_SPLIT_MOD15_WRITE },
    266 	{ "Samsung SSD 860 EVO 1TB", WD_QUIRK_BAD_NCQ },
    267 	{ "Samsung SSD 860 EVO 500GB", WD_QUIRK_BAD_NCQ },
    268 	{ NULL, 0 }
    269 };
    270 
    271 static const struct wd_quirk *
    272 wd_lookup_quirks(const char *name)
    273 {
    274 	const struct wd_quirk *wdq;
    275 	const char *estr;
    276 
    277 	for (wdq = wd_quirk_table; wdq->wdq_match != NULL; wdq++) {
    278 		/*
    279 		 * We only want exact matches (which include matches
    280 		 * against globbing characters).
    281 		 */
    282 		if (pmatch(name, wdq->wdq_match, &estr) == 2)
    283 			return (wdq);
    284 	}
    285 	return (NULL);
    286 }
    287 
    288 static int
    289 wdprobe(device_t parent, cfdata_t match, void *aux)
    290 {
    291 	struct ata_device *adev = aux;
    292 
    293 	if (adev == NULL)
    294 		return 0;
    295 	if (adev->adev_bustype->bustype_type != SCSIPI_BUSTYPE_ATA)
    296 		return 0;
    297 
    298 	if (match->cf_loc[ATA_HLCF_DRIVE] != ATA_HLCF_DRIVE_DEFAULT &&
    299 	    match->cf_loc[ATA_HLCF_DRIVE] != adev->adev_drv_data->drive)
    300 		return 0;
    301 	return 1;
    302 }
    303 
    304 static void
    305 wdattach(device_t parent, device_t self, void *aux)
    306 {
    307 	struct wd_softc *wd = device_private(self);
    308 	struct dk_softc *dksc = &wd->sc_dksc;
    309 	struct ata_device *adev= aux;
    310 	int i, blank;
    311 	char tbuf[41],pbuf[9], c, *p, *q;
    312 	const struct wd_quirk *wdq;
    313 	int dtype = DKTYPE_UNKNOWN;
    314 
    315 	dksc->sc_dev = self;
    316 
    317 	ATADEBUG_PRINT(("wdattach\n"), DEBUG_FUNCS | DEBUG_PROBE);
    318 	mutex_init(&wd->sc_lock, MUTEX_DEFAULT, IPL_BIO);
    319 #ifdef WD_SOFTBADSECT
    320 	SLIST_INIT(&wd->sc_bslist);
    321 #endif
    322 	wd->atabus = adev->adev_bustype;
    323 	wd->inflight = 0;
    324 	wd->drvp = adev->adev_drv_data;
    325 
    326 	wd->drvp->drv_openings = 1;
    327 	wd->drvp->drv_done = wddone;
    328 	wd->drvp->drv_softc = dksc->sc_dev; /* done in atabusconfig_thread()
    329 					     but too late */
    330 
    331 	SLIST_INIT(&wd->sc_retry_list);
    332 	SLIST_INIT(&wd->sc_requeue_list);
    333 	callout_init(&wd->sc_retry_callout, 0);		/* XXX MPSAFE */
    334 	callout_init(&wd->sc_requeue_callout, 0);	/* XXX MPSAFE */
    335 	callout_init(&wd->sc_restart_diskqueue, 0);	/* XXX MPSAFE */
    336 
    337 	aprint_naive("\n");
    338 	aprint_normal("\n");
    339 
    340 	/* read our drive info */
    341 	if (wd_get_params(wd, AT_WAIT, &wd->sc_params) != 0) {
    342 		aprint_error_dev(self, "IDENTIFY failed\n");
    343 		goto out;
    344 	}
    345 
    346 	for (blank = 0, p = wd->sc_params.atap_model, q = tbuf, i = 0;
    347 	    i < sizeof(wd->sc_params.atap_model); i++) {
    348 		c = *p++;
    349 		if (c == '\0')
    350 			break;
    351 		if (c != ' ') {
    352 			if (blank) {
    353 				*q++ = ' ';
    354 				blank = 0;
    355 			}
    356 			*q++ = c;
    357 		} else
    358 			blank = 1;
    359 	}
    360 	*q++ = '\0';
    361 
    362 	wd->sc_typename = kmem_asprintf("%s", tbuf);
    363 	aprint_normal_dev(self, "<%s>\n", wd->sc_typename);
    364 
    365 	wdq = wd_lookup_quirks(tbuf);
    366 	if (wdq != NULL)
    367 		wd->sc_quirks = wdq->wdq_quirks;
    368 
    369 	if (wd->sc_quirks != 0) {
    370 		char sbuf[sizeof(WD_QUIRK_FMT) + 64];
    371 		snprintb(sbuf, sizeof(sbuf), WD_QUIRK_FMT, wd->sc_quirks);
    372 		aprint_normal_dev(self, "quirks %s\n", sbuf);
    373 
    374 		if (wd->sc_quirks & WD_QUIRK_SPLIT_MOD15_WRITE) {
    375 			aprint_error_dev(self, "drive corrupts write transfers with certain controllers, consider replacing\n");
    376 		}
    377 
    378 		if (wd->sc_quirks & WD_QUIRK_BAD_NCQ) {
    379 			aprint_error_dev(self, "drive NCQ support broken, NCQ disabled, consider replacing\n");
    380 		}
    381 	}
    382 
    383 	if ((wd->sc_params.atap_multi & 0xff) > 1) {
    384 		wd->drvp->multi = wd->sc_params.atap_multi & 0xff;
    385 	} else {
    386 		wd->drvp->multi = 1;
    387 	}
    388 
    389 	aprint_verbose_dev(self, "drive supports %d-sector PIO transfers,",
    390 	    wd->drvp->multi);
    391 
    392 	/* 48-bit LBA addressing */
    393 	if ((wd->sc_params.atap_cmd2_en & ATA_CMD2_LBA48) != 0)
    394 		wd->sc_flags |= WDF_LBA48;
    395 
    396 	/* Prior to ATA-4, LBA was optional. */
    397 	if ((wd->sc_params.atap_capabilities1 & WDC_CAP_LBA) != 0)
    398 		wd->sc_flags |= WDF_LBA;
    399 #if 0
    400 	/* ATA-4 requires LBA. */
    401 	if (wd->sc_params.atap_ataversion != 0xffff &&
    402 	    wd->sc_params.atap_ataversion >= WDC_VER_ATA4)
    403 		wd->sc_flags |= WDF_LBA;
    404 #endif
    405 
    406 	if ((wd->sc_flags & WDF_LBA48) != 0) {
    407 		aprint_verbose(" LBA48 addressing\n");
    408 		wd->sc_capacity =
    409 		    ((uint64_t) wd->sc_params.atap_max_lba[3] << 48) |
    410 		    ((uint64_t) wd->sc_params.atap_max_lba[2] << 32) |
    411 		    ((uint64_t) wd->sc_params.atap_max_lba[1] << 16) |
    412 		    ((uint64_t) wd->sc_params.atap_max_lba[0] <<  0);
    413 		wd->sc_capacity28 =
    414 		    (wd->sc_params.atap_capacity[1] << 16) |
    415 		    wd->sc_params.atap_capacity[0];
    416 	} else if ((wd->sc_flags & WDF_LBA) != 0) {
    417 		aprint_verbose(" LBA addressing\n");
    418 		wd->sc_capacity28 = wd->sc_capacity =
    419 		    (wd->sc_params.atap_capacity[1] << 16) |
    420 		    wd->sc_params.atap_capacity[0];
    421 	} else {
    422 		aprint_verbose(" chs addressing\n");
    423 		wd->sc_capacity28 = wd->sc_capacity =
    424 		    wd->sc_params.atap_cylinders *
    425 		    wd->sc_params.atap_heads *
    426 		    wd->sc_params.atap_sectors;
    427 	}
    428 	if ((wd->sc_params.atap_secsz & ATA_SECSZ_VALID_MASK) == ATA_SECSZ_VALID
    429 	    && ((wd->sc_params.atap_secsz & ATA_SECSZ_LLS) != 0)) {
    430 		wd->sc_blksize = 2ULL *
    431 		    ((uint32_t)((wd->sc_params.atap_lls_secsz[1] << 16) |
    432 		    wd->sc_params.atap_lls_secsz[0]));
    433 	} else {
    434 		wd->sc_blksize = 512;
    435 	}
    436 	wd->sc_capacity512 = (wd->sc_capacity * wd->sc_blksize) / DEV_BSIZE;
    437 	format_bytes(pbuf, sizeof(pbuf), wd->sc_capacity * wd->sc_blksize);
    438 	aprint_normal_dev(self, "%s, %d cyl, %d head, %d sec, "
    439 	    "%d bytes/sect x %llu sectors\n",
    440 	    pbuf,
    441 	    (wd->sc_flags & WDF_LBA) ? (int)(wd->sc_capacity /
    442 		(wd->sc_params.atap_heads * wd->sc_params.atap_sectors)) :
    443 		wd->sc_params.atap_cylinders,
    444 	    wd->sc_params.atap_heads, wd->sc_params.atap_sectors,
    445 	    wd->sc_blksize, (unsigned long long)wd->sc_capacity);
    446 
    447 	ATADEBUG_PRINT(("%s: atap_dmatiming_mimi=%d, atap_dmatiming_recom=%d\n",
    448 	    device_xname(self), wd->sc_params.atap_dmatiming_mimi,
    449 	    wd->sc_params.atap_dmatiming_recom), DEBUG_PROBE);
    450 
    451 	if (wd->sc_blksize <= 0 || !powerof2(wd->sc_blksize) ||
    452 	    wd->sc_blksize < DEV_BSIZE || wd->sc_blksize > MAXPHYS) {
    453 		aprint_normal_dev(self, "WARNING: block size %u "
    454 		    "might not actually work\n", wd->sc_blksize);
    455 	}
    456 
    457 	if (strcmp(wd->sc_params.atap_model, "ST506") == 0)
    458 		dtype = DKTYPE_ST506;
    459 	else
    460 		dtype = DKTYPE_ESDI;
    461 
    462 out:
    463 	/*
    464 	 * Initialize and attach the disk structure.
    465 	 */
    466 	dk_init(dksc, self, dtype);
    467 	disk_init(&dksc->sc_dkdev, dksc->sc_xname, &wddkdriver);
    468 
    469 	/* Attach dk and disk subsystems */
    470 	dk_attach(dksc);
    471 	disk_attach(&dksc->sc_dkdev);
    472 	wd_set_geometry(wd);
    473 
    474 	bufq_alloc(&dksc->sc_bufq, BUFQ_DISK_DEFAULT_STRAT, BUFQ_SORT_RAWBLOCK);
    475 
    476 	/* reference to label structure, used by ata code */
    477 	wd->drvp->lp = dksc->sc_dkdev.dk_label;
    478 
    479 	/* Discover wedges on this disk. */
    480 	dkwedge_discover(&dksc->sc_dkdev);
    481 
    482 	if (!pmf_device_register1(self, wd_suspend, NULL, wd_shutdown))
    483 		aprint_error_dev(self, "couldn't establish power handler\n");
    484 
    485 	wd_sysctl_attach(wd);
    486 }
    487 
    488 static bool
    489 wd_suspend(device_t dv, const pmf_qual_t *qual)
    490 {
    491 	struct wd_softc *sc = device_private(dv);
    492 
    493 	/* the adapter needs to be enabled */
    494 	if (sc->atabus->ata_addref(sc->drvp))
    495 		return true; /* no need to complain */
    496 
    497 	wd_flushcache(sc, AT_WAIT, false);
    498 	wd_standby(sc, AT_WAIT);
    499 
    500 	sc->atabus->ata_delref(sc->drvp);
    501 	return true;
    502 }
    503 
    504 static int
    505 wddetach(device_t self, int flags)
    506 {
    507 	struct wd_softc *wd = device_private(self);
    508 	struct dk_softc *dksc = &wd->sc_dksc;
    509 	int bmaj, cmaj, i, mn, rc;
    510 
    511 	if ((rc = disk_begindetach(&dksc->sc_dkdev, wd_lastclose, self, flags)) != 0)
    512 		return rc;
    513 
    514 	/* locate the major number */
    515 	bmaj = bdevsw_lookup_major(&wd_bdevsw);
    516 	cmaj = cdevsw_lookup_major(&wd_cdevsw);
    517 
    518 	/* Nuke the vnodes for any open instances. */
    519 	for (i = 0; i < MAXPARTITIONS; i++) {
    520 		mn = WDMINOR(device_unit(self), i);
    521 		vdevgone(bmaj, mn, mn, VBLK);
    522 		vdevgone(cmaj, mn, mn, VCHR);
    523 	}
    524 
    525 	dk_drain(dksc);
    526 
    527 	/* Kill off any pending commands. */
    528 	mutex_enter(&wd->sc_lock);
    529 	wd->atabus->ata_killpending(wd->drvp);
    530 
    531 	callout_halt(&wd->sc_retry_callout, &wd->sc_lock);
    532 	callout_destroy(&wd->sc_retry_callout);
    533 	callout_halt(&wd->sc_requeue_callout, &wd->sc_lock);
    534 	callout_destroy(&wd->sc_requeue_callout);
    535 	callout_halt(&wd->sc_restart_diskqueue, &wd->sc_lock);
    536 	callout_destroy(&wd->sc_restart_diskqueue);
    537 
    538 	mutex_exit(&wd->sc_lock);
    539 
    540 	bufq_free(dksc->sc_bufq);
    541 
    542 	/* Delete all of our wedges. */
    543 	dkwedge_delall(&dksc->sc_dkdev);
    544 
    545 	if (flags & DETACH_POWEROFF)
    546 		wd_standby(wd, AT_POLL);
    547 
    548 	/* Detach from the disk list. */
    549 	disk_detach(&dksc->sc_dkdev);
    550 	disk_destroy(&dksc->sc_dkdev);
    551 
    552 	dk_detach(dksc);
    553 
    554 #ifdef WD_SOFTBADSECT
    555 	/* Clean out the bad sector list */
    556 	while (!SLIST_EMPTY(&wd->sc_bslist)) {
    557 		struct disk_badsectors *dbs = SLIST_FIRST(&wd->sc_bslist);
    558 		SLIST_REMOVE_HEAD(&wd->sc_bslist, dbs_next);
    559 		kmem_free(dbs, sizeof(*dbs));
    560 	}
    561 	wd->sc_bscount = 0;
    562 #endif
    563 	if (wd->sc_typename != NULL) {
    564 		kmem_free(wd->sc_typename, strlen(wd->sc_typename) + 1);
    565 		wd->sc_typename = NULL;
    566 	}
    567 
    568 	pmf_device_deregister(self);
    569 
    570 	wd_sysctl_detach(wd);
    571 
    572 	mutex_destroy(&wd->sc_lock);
    573 
    574 	wd->drvp->drive_type = ATA_DRIVET_NONE; /* no drive any more here */
    575 	wd->drvp->drive_flags = 0;
    576 
    577 	return (0);
    578 }
    579 
    580 /*
    581  * Read/write routine for a buffer.  Validates the arguments and schedules the
    582  * transfer.  Does not wait for the transfer to complete.
    583  */
    584 static void
    585 wdstrategy(struct buf *bp)
    586 {
    587 	struct wd_softc *wd =
    588 	    device_lookup_private(&wd_cd, WDUNIT(bp->b_dev));
    589 	struct dk_softc *dksc = &wd->sc_dksc;
    590 
    591 	ATADEBUG_PRINT(("wdstrategy (%s)\n", dksc->sc_xname),
    592 	    DEBUG_XFERS);
    593 
    594 	/* If device invalidated (e.g. media change, door open,
    595 	 * device detachment), then error.
    596 	 */
    597 	if ((wd->sc_flags & WDF_LOADED) == 0 ||
    598 	    !device_is_enabled(dksc->sc_dev))
    599 		goto err;
    600 
    601 #ifdef WD_SOFTBADSECT
    602 	/*
    603 	 * If the transfer about to be attempted contains only a block that
    604 	 * is known to be bad then return an error for the transfer without
    605 	 * even attempting to start a transfer up under the premis that we
    606 	 * will just end up doing more retries for a transfer that will end
    607 	 * up failing again.
    608 	 */
    609 	if (__predict_false(!SLIST_EMPTY(&wd->sc_bslist))) {
    610 		struct disklabel *lp = dksc->sc_dkdev.dk_label;
    611 		struct disk_badsectors *dbs;
    612 		daddr_t blkno, maxblk;
    613 
    614 		/* convert the block number to absolute */
    615 		if (lp->d_secsize >= DEV_BSIZE)
    616 			blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
    617 		else
    618 			blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
    619 		if (WDPART(bp->b_dev) != RAW_PART)
    620 			blkno += lp->d_partitions[WDPART(bp->b_dev)].p_offset;
    621 		maxblk = blkno + (bp->b_bcount / wd->sc_blksize) - 1;
    622 
    623 		mutex_enter(&wd->sc_lock);
    624 		SLIST_FOREACH(dbs, &wd->sc_bslist, dbs_next)
    625 			if ((dbs->dbs_min <= bp->b_rawblkno &&
    626 			     bp->b_rawblkno <= dbs->dbs_max) ||
    627 			    (dbs->dbs_min <= maxblk && maxblk <= dbs->dbs_max)){
    628 				mutex_exit(&wd->sc_lock);
    629 				goto err;
    630 			}
    631 		mutex_exit(&wd->sc_lock);
    632 	}
    633 #endif
    634 
    635 	dk_strategy(dksc, bp);
    636 	return;
    637 
    638 err:
    639 	bp->b_error = EIO;
    640 	bp->b_resid = bp->b_bcount;
    641 	biodone(bp);
    642 }
    643 
    644 static void
    645 wdstart1(struct wd_softc *wd, struct buf *bp, struct ata_xfer *xfer)
    646 {
    647 	struct dk_softc *dksc = &wd->sc_dksc;
    648 
    649 	KASSERT(bp == xfer->c_bio.bp || xfer->c_bio.bp == NULL);
    650 	KASSERT((xfer->c_flags & (C_WAITACT|C_FREE)) == 0);
    651 
    652 	/* Reset state, so that retries don't use stale info */
    653 	if (__predict_false(xfer->c_retries > 0)) {
    654 		xfer->c_flags = 0;
    655 		memset(&xfer->c_bio, 0, sizeof(xfer->c_bio));
    656 	}
    657 
    658 	xfer->c_bio.blkno = bp->b_rawblkno;
    659 	xfer->c_bio.bcount = bp->b_bcount;
    660 	xfer->c_bio.databuf = bp->b_data;
    661 	xfer->c_bio.blkdone = 0;
    662 	xfer->c_bio.bp = bp;
    663 
    664 #ifdef WD_CHAOS_MONKEY
    665 	/*
    666 	 * Override blkno to be over device capacity to trigger error,
    667 	 * but only if it's read, to avoid trashing disk contents should
    668 	 * the command be clipped, or otherwise misinterpreted, by the
    669 	 * driver or controller.
    670 	 */
    671 	if (BUF_ISREAD(bp) && xfer->c_retries == 0 && wd->drv_chaos_freq > 0 &&
    672 	    (++wd->drv_chaos_cnt % wd->drv_chaos_freq) == 0) {
    673 		device_printf(dksc->sc_dev, "%s: chaos xfer %"PRIxPTR"\n",
    674 		    __func__, (intptr_t)xfer & PAGE_MASK);
    675 		xfer->c_bio.blkno = 7777777 + wd->sc_capacity;
    676 		xfer->c_flags |= C_CHAOS;
    677 	}
    678 #endif
    679 
    680 	/*
    681 	 * If we're retrying, retry in single-sector mode. This will give us
    682 	 * the sector number of the problem, and will eventually allow the
    683 	 * transfer to succeed. If FUA is requested, we can't actually
    684 	 * do this, as ATA_SINGLE is usually executed as PIO transfer by drivers
    685 	 * which support it, and that isn't compatible with NCQ/FUA.
    686 	 */
    687 	if (xfer->c_retries >= WDIORETRIES_SINGLE &&
    688 	    (bp->b_flags & B_MEDIA_FUA) == 0)
    689 		xfer->c_bio.flags = ATA_SINGLE;
    690 	else
    691 		xfer->c_bio.flags = 0;
    692 
    693 	/*
    694 	 * request LBA48 transfers when supported by the controller
    695 	 * and needed by transfer offset or size.
    696 	 */
    697 	if (wd->sc_flags & WDF_LBA48 &&
    698 	    (((xfer->c_bio.blkno +
    699 	     xfer->c_bio.bcount / dksc->sc_dkdev.dk_geom.dg_secsize) >
    700 	    wd->sc_capacity28) ||
    701 	    ((xfer->c_bio.bcount / dksc->sc_dkdev.dk_geom.dg_secsize) > 128)))
    702 		xfer->c_bio.flags |= ATA_LBA48;
    703 
    704 	/*
    705 	 * If NCQ was negotiated, always use it for the first several attempts.
    706 	 * Since device cancels all outstanding requests on error, downgrade
    707 	 * to non-NCQ on retry, so that the retried transfer would not cause
    708 	 * cascade failure for the other transfers if it fails again.
    709 	 * If FUA was requested, we can't downgrade, as that would violate
    710 	 * the semantics - FUA would not be honored. In that case, continue
    711 	 * retrying with NCQ.
    712 	 */
    713 	if (WD_USE_NCQ(wd) && (xfer->c_retries < WDIORETRIES_SINGLE ||
    714 	    (bp->b_flags & B_MEDIA_FUA) != 0)) {
    715 		xfer->c_bio.flags |= ATA_LBA48;
    716 		xfer->c_flags |= C_NCQ;
    717 
    718 		if (WD_USE_NCQ_PRIO(wd) &&
    719 		    BIO_GETPRIO(bp) == BPRIO_TIMECRITICAL)
    720 			xfer->c_bio.flags |= ATA_PRIO_HIGH;
    721 	}
    722 
    723 	if (wd->sc_flags & WDF_LBA)
    724 		xfer->c_bio.flags |= ATA_LBA;
    725 	if (bp->b_flags & B_READ) {
    726 		xfer->c_bio.flags |= ATA_READ;
    727 	} else {
    728 		/* it's a write */
    729 		wd->sc_flags |= WDF_DIRTY;
    730 	}
    731 	if (bp->b_flags & B_MEDIA_FUA) {
    732 		/* If not using NCQ, the command WRITE DMA FUA EXT is LBA48 */
    733 		KASSERT((wd->sc_flags & WDF_LBA48) != 0);
    734 		if ((xfer->c_flags & C_NCQ) == 0)
    735 			xfer->c_bio.flags |= ATA_LBA48;
    736 
    737 		xfer->c_bio.flags |= ATA_FUA;
    738 	}
    739 
    740 	if (xfer->c_retries == 0)
    741 		wd->inflight++;
    742 	switch (wd->atabus->ata_bio(wd->drvp, xfer)) {
    743 	case ATACMD_TRY_AGAIN:
    744 		panic("wdstart1: try again");
    745 		break;
    746 	case ATACMD_QUEUED:
    747 	case ATACMD_COMPLETE:
    748 		break;
    749 	default:
    750 		panic("wdstart1: bad return code from ata_bio()");
    751 	}
    752 }
    753 
    754 static int
    755 wd_diskstart(device_t dev, struct buf *bp)
    756 {
    757 	struct wd_softc *wd = device_private(dev);
    758 #ifdef ATADEBUG
    759 	struct dk_softc *dksc = &wd->sc_dksc;
    760 #endif
    761 	struct ata_xfer *xfer;
    762 	struct ata_channel *chp;
    763 	unsigned openings;
    764 	int ticks;
    765 
    766 	mutex_enter(&wd->sc_lock);
    767 
    768 	chp = wd->drvp->chnl_softc;
    769 
    770 	ata_channel_lock(chp);
    771 	openings = ata_queue_openings(chp);
    772 	ata_channel_unlock(chp);
    773 
    774 	openings = uimin(openings, wd->drvp->drv_openings);
    775 
    776 	if (wd->inflight >= openings) {
    777 		/*
    778 		 * pretend we run out of memory when the queue is full,
    779 		 * so that the operation is retried after a minimal
    780 		 * delay.
    781 		 */
    782 		xfer = NULL;
    783 		ticks = 1;
    784 	} else {
    785 		/*
    786 		 * If there is no available memory, retry later. This
    787 		 * happens very rarely and only under memory pressure,
    788 		 * so wait relatively long before retry.
    789 		 */
    790 		xfer = ata_get_xfer(chp, false);
    791 		ticks = hz/2;
    792 	}
    793 
    794 	if (xfer == NULL) {
    795 		ATADEBUG_PRINT(("wd_diskstart %s no xfer\n",
    796 		    dksc->sc_xname), DEBUG_XFERS);
    797 
    798 		/*
    799 		 * The disk queue is pushed automatically when an I/O
    800 		 * operation finishes or another one is queued. We
    801 		 * need this extra timeout because an ATA channel
    802 		 * might be shared by more than one disk queue and
    803 		 * all queues need to be restarted when another slot
    804 		 * becomes available.
    805 		 */
    806 		if (!callout_pending(&wd->sc_restart_diskqueue)) {
    807 			callout_reset(&wd->sc_restart_diskqueue, ticks,
    808 			    wdrestart, dev);
    809 		}
    810 
    811 		mutex_exit(&wd->sc_lock);
    812 		return EAGAIN;
    813 	}
    814 
    815 	wdstart1(wd, bp, xfer);
    816 
    817 	mutex_exit(&wd->sc_lock);
    818 
    819 	return 0;
    820 }
    821 
    822 /*
    823  * Queue a drive for I/O.
    824  */
    825 static void
    826 wdrestart(void *x)
    827 {
    828 	device_t self = x;
    829 	struct wd_softc *wd = device_private(self);
    830 	struct dk_softc *dksc = &wd->sc_dksc;
    831 
    832 	ATADEBUG_PRINT(("wdstart %s\n", dksc->sc_xname),
    833 	    DEBUG_XFERS);
    834 
    835 	if (!device_is_active(dksc->sc_dev))
    836 		return;
    837 
    838 	dk_start(dksc, NULL);
    839 }
    840 
    841 static void
    842 wddone(device_t self, struct ata_xfer *xfer)
    843 {
    844 	struct wd_softc *wd = device_private(self);
    845 	struct dk_softc *dksc = &wd->sc_dksc;
    846 	const char *errmsg;
    847 	int do_perror = 0;
    848 	struct buf *bp;
    849 
    850 	ATADEBUG_PRINT(("wddone %s\n", dksc->sc_xname),
    851 	    DEBUG_XFERS);
    852 
    853 	if (__predict_false(wddoingadump)) {
    854 		/* just drop it to the floor */
    855 		ata_free_xfer(wd->drvp->chnl_softc, xfer);
    856 		return;
    857 	}
    858 
    859 	bp = xfer->c_bio.bp;
    860 	KASSERT(bp != NULL);
    861 
    862 	bp->b_resid = xfer->c_bio.bcount;
    863 	switch (xfer->c_bio.error) {
    864 	case ERR_DMA:
    865 		errmsg = "DMA error";
    866 		goto retry;
    867 	case ERR_DF:
    868 		errmsg = "device fault";
    869 		goto retry;
    870 	case TIMEOUT:
    871 		errmsg = "device timeout";
    872 		goto retry;
    873 	case REQUEUE:
    874 		errmsg = "requeue";
    875 		goto retry2;
    876 	case ERR_RESET:
    877 		errmsg = "channel reset";
    878 		goto retry2;
    879 	case ERROR:
    880 		/* Don't care about media change bits */
    881 		if (xfer->c_bio.r_error != 0 &&
    882 		    (xfer->c_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0)
    883 			goto noerror;
    884 		errmsg = "error";
    885 		do_perror = 1;
    886 retry:		/* Just reset and retry. Can we do more ? */
    887 		if ((xfer->c_flags & C_RECOVERED) == 0) {
    888 			int wflags = (xfer->c_flags & C_POLL) ? AT_POLL : 0;
    889 			ata_channel_lock(wd->drvp->chnl_softc);
    890 			ata_thread_run(wd->drvp->chnl_softc, wflags,
    891 			    ATACH_TH_DRIVE_RESET, wd->drvp->drive);
    892 			ata_channel_unlock(wd->drvp->chnl_softc);
    893 		}
    894 retry2:
    895 		mutex_enter(&wd->sc_lock);
    896 
    897 		diskerr(bp, "wd", errmsg, LOG_PRINTF,
    898 		    xfer->c_bio.blkdone, dksc->sc_dkdev.dk_label);
    899 		if (xfer->c_retries < WDIORETRIES)
    900 			printf(", xfer %"PRIxPTR", retry %d",
    901 			    (intptr_t)xfer & PAGE_MASK,
    902 			    xfer->c_retries);
    903 		printf("\n");
    904 		if (do_perror)
    905 			wdperror(wd, xfer);
    906 
    907 		if (xfer->c_retries < WDIORETRIES) {
    908 			xfer->c_retries++;
    909 
    910 			/* Rerun ASAP if just requeued */
    911 			if (xfer->c_bio.error == REQUEUE) {
    912 				SLIST_INSERT_HEAD(&wd->sc_requeue_list, xfer,
    913 				    c_retrychain);
    914 				callout_reset(&wd->sc_requeue_callout,
    915 				    1, wdbiorequeue, wd);
    916 			} else {
    917 				SLIST_INSERT_HEAD(&wd->sc_retry_list, xfer,
    918 				    c_retrychain);
    919 				callout_reset(&wd->sc_retry_callout,
    920 				    RECOVERYTIME, wdbioretry, wd);
    921 			}
    922 
    923 			mutex_exit(&wd->sc_lock);
    924 			return;
    925 		}
    926 
    927 		mutex_exit(&wd->sc_lock);
    928 
    929 #ifdef WD_SOFTBADSECT
    930 		/*
    931 		 * Not all errors indicate a failed block but those that do,
    932 		 * put the block on the bad-block list for the device.  Only
    933 		 * do this for reads because the drive should do it for writes,
    934 		 * itself, according to Manuel.
    935 		 */
    936 		if ((bp->b_flags & B_READ) &&
    937 		    ((wd->drvp->ata_vers >= 4 && xfer->c_bio.r_error & 64) ||
    938 		     (wd->drvp->ata_vers < 4 && xfer->c_bio.r_error & 192))) {
    939 			struct disk_badsectors *dbs;
    940 
    941 			dbs = kmem_zalloc(sizeof *dbs, KM_NOSLEEP);
    942 			if (dbs == NULL) {
    943 				aprint_error_dev(dksc->sc_dev,
    944 				    "failed to add bad block to list\n");
    945 				goto out;
    946 			}
    947 
    948 			dbs->dbs_min = bp->b_rawblkno;
    949 			dbs->dbs_max = dbs->dbs_min +
    950 			    (bp->b_bcount /wd->sc_blksize) - 1;
    951 			microtime(&dbs->dbs_failedat);
    952 
    953 			mutex_enter(&wd->sc_lock);
    954 			SLIST_INSERT_HEAD(&wd->sc_bslist, dbs, dbs_next);
    955 			wd->sc_bscount++;
    956 			mutex_exit(&wd->sc_lock);
    957 		}
    958 out:
    959 #endif
    960 		bp->b_error = EIO;
    961 		break;
    962 	case NOERROR:
    963 #ifdef WD_CHAOS_MONKEY
    964 		/*
    965 		 * For example Parallels AHCI emulation doesn't actually
    966 		 * return error for the invalid I/O, so just re-run
    967 		 * the request and do not panic.
    968 		 */
    969 		if (__predict_false(xfer->c_flags & C_CHAOS)) {
    970 			xfer->c_bio.error = REQUEUE;
    971 			errmsg = "chaos noerror";
    972 			goto retry2;
    973 		}
    974 #endif
    975 
    976 noerror:	if ((xfer->c_bio.flags & ATA_CORR) || xfer->c_retries > 0)
    977 			device_printf(dksc->sc_dev,
    978 			    "soft error (corrected) xfer %"PRIxPTR"\n",
    979 			    (intptr_t)xfer & PAGE_MASK);
    980 		break;
    981 	case ERR_NODEV:
    982 		bp->b_error = EIO;
    983 		break;
    984 	}
    985 	if (__predict_false(bp->b_error != 0) && bp->b_resid == 0) {
    986 		/*
    987 		 * the disk or controller sometimes report a complete
    988 		 * xfer, when there has been an error. This is wrong,
    989 		 * assume nothing got transferred in this case
    990 		 */
    991 		bp->b_resid = bp->b_bcount;
    992 	}
    993 
    994 	ata_free_xfer(wd->drvp->chnl_softc, xfer);
    995 
    996 	mutex_enter(&wd->sc_lock);
    997 	wd->inflight--;
    998 	mutex_exit(&wd->sc_lock);
    999 	dk_done(dksc, bp);
   1000 	dk_start(dksc, NULL);
   1001 }
   1002 
   1003 static void
   1004 wdbioretry(void *v)
   1005 {
   1006 	struct wd_softc *wd = v;
   1007 	struct ata_xfer *xfer;
   1008 
   1009 	ATADEBUG_PRINT(("%s %s\n", __func__, wd->sc_dksc.sc_xname),
   1010 	    DEBUG_XFERS);
   1011 
   1012 	mutex_enter(&wd->sc_lock);
   1013 	while ((xfer = SLIST_FIRST(&wd->sc_retry_list))) {
   1014 		SLIST_REMOVE_HEAD(&wd->sc_retry_list, c_retrychain);
   1015 		wdstart1(wd, xfer->c_bio.bp, xfer);
   1016 	}
   1017 	mutex_exit(&wd->sc_lock);
   1018 }
   1019 
   1020 static void
   1021 wdbiorequeue(void *v)
   1022 {
   1023 	struct wd_softc *wd = v;
   1024 	struct ata_xfer *xfer;
   1025 
   1026 	ATADEBUG_PRINT(("%s %s\n", __func__, wd->sc_dksc.sc_xname),
   1027 	    DEBUG_XFERS);
   1028 
   1029 	mutex_enter(&wd->sc_lock);
   1030 	while ((xfer = SLIST_FIRST(&wd->sc_requeue_list))) {
   1031 		SLIST_REMOVE_HEAD(&wd->sc_requeue_list, c_retrychain);
   1032 		wdstart1(wd, xfer->c_bio.bp, xfer);
   1033 	}
   1034 	mutex_exit(&wd->sc_lock);
   1035 }
   1036 
   1037 static void
   1038 wdminphys(struct buf *bp)
   1039 {
   1040 	const struct wd_softc * const wd =
   1041 	    device_lookup_private(&wd_cd, WDUNIT(bp->b_dev));
   1042 	int maxsectors;
   1043 
   1044 	/*
   1045 	 * The limit is actually 65536 for LBA48 and 256 for non-LBA48,
   1046 	 * but that requires to set the count for the ATA command
   1047 	 * to 0, which is somewhat error prone, so better stay safe.
   1048 	 */
   1049 	if (wd->sc_flags & WDF_LBA48)
   1050 		maxsectors = 65535;
   1051 	else
   1052 		maxsectors = 128;
   1053 
   1054 	if (bp->b_bcount > (wd->sc_blksize * maxsectors))
   1055 		bp->b_bcount = (wd->sc_blksize * maxsectors);
   1056 
   1057 	minphys(bp);
   1058 }
   1059 
   1060 static void
   1061 wd_iosize(device_t dev, int *count)
   1062 {
   1063 	struct buf B;
   1064 	int bmaj;
   1065 
   1066 	bmaj       = bdevsw_lookup_major(&wd_bdevsw);
   1067 	B.b_dev    = MAKEWDDEV(bmaj,device_unit(dev),RAW_PART);
   1068 	B.b_bcount = *count;
   1069 
   1070 	wdminphys(&B);
   1071 
   1072 	*count = B.b_bcount;
   1073 }
   1074 
   1075 static int
   1076 wdread(dev_t dev, struct uio *uio, int flags)
   1077 {
   1078 
   1079 	ATADEBUG_PRINT(("wdread\n"), DEBUG_XFERS);
   1080 	return (physio(wdstrategy, NULL, dev, B_READ, wdminphys, uio));
   1081 }
   1082 
   1083 static int
   1084 wdwrite(dev_t dev, struct uio *uio, int flags)
   1085 {
   1086 
   1087 	ATADEBUG_PRINT(("wdwrite\n"), DEBUG_XFERS);
   1088 	return (physio(wdstrategy, NULL, dev, B_WRITE, wdminphys, uio));
   1089 }
   1090 
   1091 static int
   1092 wdopen(dev_t dev, int flag, int fmt, struct lwp *l)
   1093 {
   1094 	struct wd_softc *wd;
   1095 	struct dk_softc *dksc;
   1096 	int unit, part, error;
   1097 
   1098 	ATADEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS);
   1099 	unit = WDUNIT(dev);
   1100 	wd = device_lookup_private(&wd_cd, unit);
   1101 	if (wd == NULL)
   1102 		return (ENXIO);
   1103 	dksc = &wd->sc_dksc;
   1104 
   1105 	if (! device_is_active(dksc->sc_dev))
   1106 		return (ENODEV);
   1107 
   1108 	part = WDPART(dev);
   1109 
   1110 	if (wd->sc_capacity == 0)
   1111 		return (ENODEV);
   1112 
   1113 	/*
   1114 	 * If any partition is open, but the disk has been invalidated,
   1115 	 * disallow further opens.
   1116 	 */
   1117 	if ((wd->sc_flags & (WDF_OPEN | WDF_LOADED)) == WDF_OPEN) {
   1118 		if (part != RAW_PART || fmt != S_IFCHR)
   1119 			return EIO;
   1120 	}
   1121 
   1122 	error = dk_open(dksc, dev, flag, fmt, l);
   1123 
   1124 	return error;
   1125 }
   1126 
   1127 /*
   1128  * Serialized by caller
   1129  */
   1130 static int
   1131 wd_firstopen(device_t self, dev_t dev, int flag, int fmt)
   1132 {
   1133 	struct wd_softc *wd = device_private(self);
   1134 	struct dk_softc *dksc = &wd->sc_dksc;
   1135 	int error;
   1136 
   1137 	error = wd->atabus->ata_addref(wd->drvp);
   1138 	if (error)
   1139 		return error;
   1140 
   1141 	if ((wd->sc_flags & WDF_LOADED) == 0) {
   1142 		int param_error;
   1143 
   1144 		/* Load the physical device parameters. */
   1145 		param_error = wd_get_params(wd, AT_WAIT, &wd->sc_params);
   1146 		if (param_error != 0) {
   1147 			aprint_error_dev(dksc->sc_dev, "IDENTIFY failed\n");
   1148 			error = EIO;
   1149 			goto bad;
   1150 		}
   1151 		wd_set_geometry(wd);
   1152 		wd->sc_flags |= WDF_LOADED;
   1153 	}
   1154 
   1155 	wd->sc_flags |= WDF_OPEN;
   1156 	return 0;
   1157 
   1158 bad:
   1159 	wd->atabus->ata_delref(wd->drvp);
   1160 	return error;
   1161 }
   1162 
   1163 /*
   1164  * Caller must hold wd->sc_dk.dk_openlock.
   1165  */
   1166 static int
   1167 wd_lastclose(device_t self)
   1168 {
   1169 	struct wd_softc *wd = device_private(self);
   1170 
   1171 	KASSERTMSG(bufq_peek(wd->sc_dksc.sc_bufq) == NULL, "bufq not empty");
   1172 
   1173 	if (wd->sc_flags & WDF_DIRTY)
   1174 		wd_flushcache(wd, AT_WAIT, false);
   1175 
   1176 	wd->atabus->ata_delref(wd->drvp);
   1177 	wd->sc_flags &= ~WDF_OPEN;
   1178 
   1179 	return 0;
   1180 }
   1181 
   1182 static int
   1183 wdclose(dev_t dev, int flag, int fmt, struct lwp *l)
   1184 {
   1185 	struct wd_softc *wd;
   1186 	struct dk_softc *dksc;
   1187 	int unit;
   1188 
   1189 	unit = WDUNIT(dev);
   1190 	wd = device_lookup_private(&wd_cd, unit);
   1191 	dksc = &wd->sc_dksc;
   1192 
   1193 	return dk_close(dksc, dev, flag, fmt, l);
   1194 }
   1195 
   1196 void
   1197 wdperror(const struct wd_softc *wd, struct ata_xfer *xfer)
   1198 {
   1199 	static const char *const errstr0_3[] = {"address mark not found",
   1200 	    "track 0 not found", "aborted command", "media change requested",
   1201 	    "id not found", "media changed", "uncorrectable data error",
   1202 	    "bad block detected"};
   1203 	static const char *const errstr4_5[] = {
   1204 	    "obsolete (address mark not found)",
   1205 	    "no media/write protected", "aborted command",
   1206 	    "media change requested", "id not found", "media changed",
   1207 	    "uncorrectable data error", "interface CRC error"};
   1208 	const char *const *errstr;
   1209 	int i;
   1210 	const char *sep = "";
   1211 
   1212 	const struct dk_softc *dksc = &wd->sc_dksc;
   1213 	const char *devname = dksc->sc_xname;
   1214 	struct ata_drive_datas *drvp = wd->drvp;
   1215 	int errno = xfer->c_bio.r_error;
   1216 
   1217 	if (drvp->ata_vers >= 4)
   1218 		errstr = errstr4_5;
   1219 	else
   1220 		errstr = errstr0_3;
   1221 
   1222 	printf("%s: (", devname);
   1223 
   1224 	if (errno == 0)
   1225 		printf("error not notified");
   1226 
   1227 	for (i = 0; i < 8; i++) {
   1228 		if (errno & (1 << i)) {
   1229 			printf("%s%s", sep, errstr[i]);
   1230 			sep = ", ";
   1231 		}
   1232 	}
   1233 	printf(")\n");
   1234 }
   1235 
   1236 int
   1237 wdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
   1238 {
   1239 	struct wd_softc *wd =
   1240 	    device_lookup_private(&wd_cd, WDUNIT(dev));
   1241 	struct dk_softc *dksc = &wd->sc_dksc;
   1242 
   1243 	ATADEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS);
   1244 
   1245 	if ((wd->sc_flags & WDF_LOADED) == 0)
   1246 		return EIO;
   1247 
   1248 	switch (cmd) {
   1249 #ifdef HAS_BAD144_HANDLING
   1250 	case DIOCSBAD:
   1251 		if ((flag & FWRITE) == 0)
   1252 			return EBADF;
   1253 		dksc->sc_dkdev.dk_cpulabel->bad = *(struct dkbad *)addr;
   1254 		dksc->sc_dkdev.dk_label->d_flags |= D_BADSECT;
   1255 		bad144intern(wd);
   1256 		return 0;
   1257 #endif
   1258 #ifdef WD_SOFTBADSECT
   1259 	case DIOCBSLIST :
   1260 	{
   1261 		uint32_t count, missing, skip;
   1262 		struct disk_badsecinfo dbsi;
   1263 		struct disk_badsectors *dbs;
   1264 		size_t available;
   1265 		uint8_t *laddr;
   1266 
   1267 		dbsi = *(struct disk_badsecinfo *)addr;
   1268 		missing = wd->sc_bscount;
   1269 		count = 0;
   1270 		available = dbsi.dbsi_bufsize;
   1271 		skip = dbsi.dbsi_skip;
   1272 		laddr = (uint8_t *)dbsi.dbsi_buffer;
   1273 
   1274 		/*
   1275 		 * We start this loop with the expectation that all of the
   1276 		 * entries will be missed and decrement this counter each
   1277 		 * time we either skip over one (already copied out) or
   1278 		 * we actually copy it back to user space.  The structs
   1279 		 * holding the bad sector information are copied directly
   1280 		 * back to user space whilst the summary is returned via
   1281 		 * the struct passed in via the ioctl.
   1282 		 */
   1283 		mutex_enter(&wd->sc_lock);
   1284 		SLIST_FOREACH(dbs, &wd->sc_bslist, dbs_next) {
   1285 			if (skip > 0) {
   1286 				missing--;
   1287 				skip--;
   1288 				continue;
   1289 			}
   1290 			if (available < sizeof(*dbs))
   1291 				break;
   1292 			available -= sizeof(*dbs);
   1293 			copyout(dbs, laddr, sizeof(*dbs));
   1294 			laddr += sizeof(*dbs);
   1295 			missing--;
   1296 			count++;
   1297 		}
   1298 		mutex_exit(&wd->sc_lock);
   1299 		dbsi.dbsi_left = missing;
   1300 		dbsi.dbsi_copied = count;
   1301 		*(struct disk_badsecinfo *)addr = dbsi;
   1302 		return 0;
   1303 	}
   1304 
   1305 	case DIOCBSFLUSH :
   1306 		/* Clean out the bad sector list */
   1307 		mutex_enter(&wd->sc_lock);
   1308 		while (!SLIST_EMPTY(&wd->sc_bslist)) {
   1309 			struct disk_badsectors *dbs =
   1310 			    SLIST_FIRST(&wd->sc_bslist);
   1311 			SLIST_REMOVE_HEAD(&wd->sc_bslist, dbs_next);
   1312 			kmem_free(dbs, sizeof(*dbs));
   1313 		}
   1314 		mutex_exit(&wd->sc_lock);
   1315 		wd->sc_bscount = 0;
   1316 		return 0;
   1317 #endif
   1318 
   1319 #ifdef notyet
   1320 	case DIOCWFORMAT:
   1321 		if ((flag & FWRITE) == 0)
   1322 			return EBADF;
   1323 		{
   1324 		register struct format_op *fop;
   1325 		struct iovec aiov;
   1326 		struct uio auio;
   1327 		int error1;
   1328 
   1329 		fop = (struct format_op *)addr;
   1330 		aiov.iov_base = fop->df_buf;
   1331 		aiov.iov_len = fop->df_count;
   1332 		auio.uio_iov = &aiov;
   1333 		auio.uio_iovcnt = 1;
   1334 		auio.uio_resid = fop->df_count;
   1335 		auio.uio_offset =
   1336 			fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
   1337 		auio.uio_vmspace = l->l_proc->p_vmspace;
   1338 		error1 = physio(wdformat, NULL, dev, B_WRITE, wdminphys,
   1339 		    &auio);
   1340 		fop->df_count -= auio.uio_resid;
   1341 		fop->df_reg[0] = wdc->sc_status;
   1342 		fop->df_reg[1] = wdc->sc_error;
   1343 		return error1;
   1344 		}
   1345 #endif
   1346 	case DIOCGCACHE:
   1347 		return wd_getcache(wd, (int *)addr);
   1348 
   1349 	case DIOCSCACHE:
   1350 		return wd_setcache(wd, *(int *)addr);
   1351 
   1352 	case DIOCCACHESYNC:
   1353 		return wd_flushcache(wd, AT_WAIT, true);
   1354 
   1355 	case ATAIOCCOMMAND:
   1356 		/*
   1357 		 * Make sure this command is (relatively) safe first
   1358 		 */
   1359 		if ((((atareq_t *) addr)->flags & ATACMD_READ) == 0 &&
   1360 		    (flag & FWRITE) == 0)
   1361 			return (EBADF);
   1362 		{
   1363 		struct wd_ioctl *wi;
   1364 		atareq_t *atareq = (atareq_t *) addr;
   1365 		int error1;
   1366 
   1367 		wi = wi_get(wd);
   1368 		wi->wi_atareq = *atareq;
   1369 
   1370 		if (atareq->datalen && atareq->flags &
   1371 		    (ATACMD_READ | ATACMD_WRITE)) {
   1372 			void *tbuf;
   1373 			if (atareq->datalen < DEV_BSIZE
   1374 			    && atareq->command == WDCC_IDENTIFY) {
   1375 				tbuf = kmem_zalloc(DEV_BSIZE, KM_SLEEP);
   1376 				wi->wi_iov.iov_base = tbuf;
   1377 				wi->wi_iov.iov_len = DEV_BSIZE;
   1378 				UIO_SETUP_SYSSPACE(&wi->wi_uio);
   1379 			} else {
   1380 				tbuf = NULL;
   1381 				wi->wi_iov.iov_base = atareq->databuf;
   1382 				wi->wi_iov.iov_len = atareq->datalen;
   1383 				wi->wi_uio.uio_vmspace = l->l_proc->p_vmspace;
   1384 			}
   1385 			wi->wi_uio.uio_iov = &wi->wi_iov;
   1386 			wi->wi_uio.uio_iovcnt = 1;
   1387 			wi->wi_uio.uio_resid = atareq->datalen;
   1388 			wi->wi_uio.uio_offset = 0;
   1389 			wi->wi_uio.uio_rw =
   1390 			    (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE;
   1391 			error1 = physio(wdioctlstrategy, &wi->wi_bp, dev,
   1392 			    (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE,
   1393 			    wdminphys, &wi->wi_uio);
   1394 			if (tbuf != NULL && error1 == 0) {
   1395 				error1 = copyout(tbuf, atareq->databuf,
   1396 				    atareq->datalen);
   1397 				kmem_free(tbuf, DEV_BSIZE);
   1398 			}
   1399 		} else {
   1400 			/* No need to call physio if we don't have any
   1401 			   user data */
   1402 			wi->wi_bp.b_flags = 0;
   1403 			wi->wi_bp.b_data = 0;
   1404 			wi->wi_bp.b_bcount = 0;
   1405 			wi->wi_bp.b_dev = dev;
   1406 			wi->wi_bp.b_proc = l->l_proc;
   1407 			wdioctlstrategy(&wi->wi_bp);
   1408 			error1 = wi->wi_bp.b_error;
   1409 		}
   1410 		*atareq = wi->wi_atareq;
   1411 		wi_free(wi);
   1412 		return(error1);
   1413 		}
   1414 
   1415 	default:
   1416 		return dk_ioctl(dksc, dev, cmd, addr, flag, l);
   1417 	}
   1418 
   1419 #ifdef DIAGNOSTIC
   1420 	panic("wdioctl: impossible");
   1421 #endif
   1422 }
   1423 
   1424 static int
   1425 wd_discard(device_t dev, off_t pos, off_t len)
   1426 {
   1427 	struct wd_softc *wd = device_private(dev);
   1428 	daddr_t bno;
   1429 	long size, done;
   1430 	long maxatonce, amount;
   1431 	int result;
   1432 
   1433 	if (!(wd->sc_params.atap_ata_major & WDC_VER_ATA7)
   1434 	    || !(wd->sc_params.support_dsm & ATA_SUPPORT_DSM_TRIM)) {
   1435 		/* not supported; ignore request */
   1436 		ATADEBUG_PRINT(("wddiscard (unsupported)\n"), DEBUG_FUNCS);
   1437 		return 0;
   1438 	}
   1439 	maxatonce = 0xffff; /*wd->sc_params.max_dsm_blocks*/
   1440 
   1441 	ATADEBUG_PRINT(("wddiscard\n"), DEBUG_FUNCS);
   1442 
   1443 	if ((wd->sc_flags & WDF_LOADED) == 0)
   1444 		return EIO;
   1445 
   1446 	/* round the start up and the end down */
   1447 	bno = (pos + wd->sc_blksize - 1) / wd->sc_blksize;
   1448 	size = ((pos + len) / wd->sc_blksize) - bno;
   1449 
   1450 	done = 0;
   1451 	while (done < size) {
   1452 	     amount = size - done;
   1453 	     if (amount > maxatonce) {
   1454 		     amount = maxatonce;
   1455 	     }
   1456 	     result = wd_trim(wd, bno + done, amount);
   1457 	     if (result) {
   1458 		     return result;
   1459 	     }
   1460 	     done += amount;
   1461 	}
   1462 	return 0;
   1463 }
   1464 
   1465 static int
   1466 wddiscard(dev_t dev, off_t pos, off_t len)
   1467 {
   1468 	struct wd_softc *wd;
   1469 	struct dk_softc *dksc;
   1470 	int unit;
   1471 
   1472 	unit = WDUNIT(dev);
   1473 	wd = device_lookup_private(&wd_cd, unit);
   1474 	dksc = &wd->sc_dksc;
   1475 
   1476 	return dk_discard(dksc, dev, pos, len);
   1477 }
   1478 
   1479 #ifdef B_FORMAT
   1480 int
   1481 wdformat(struct buf *bp)
   1482 {
   1483 
   1484 	bp->b_flags |= B_FORMAT;
   1485 	return wdstrategy(bp);
   1486 }
   1487 #endif
   1488 
   1489 int
   1490 wdsize(dev_t dev)
   1491 {
   1492 	struct wd_softc *wd;
   1493 	struct dk_softc *dksc;
   1494 	int unit;
   1495 
   1496 	ATADEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS);
   1497 
   1498 	unit = WDUNIT(dev);
   1499 	wd = device_lookup_private(&wd_cd, unit);
   1500 	if (wd == NULL)
   1501 		return (-1);
   1502 	dksc = &wd->sc_dksc;
   1503 
   1504 	if (!device_is_active(dksc->sc_dev))
   1505 		return (-1);
   1506 
   1507 	return dk_size(dksc, dev);
   1508 }
   1509 
   1510 /*
   1511  * Dump core after a system crash.
   1512  */
   1513 static int
   1514 wddump(dev_t dev, daddr_t blkno, void *va, size_t size)
   1515 {
   1516 	struct wd_softc *wd;
   1517 	struct dk_softc *dksc;
   1518 	int unit;
   1519 
   1520 	/* Check if recursive dump; if so, punt. */
   1521 	if (wddoingadump)
   1522 		return EFAULT;
   1523 	wddoingadump = 1;
   1524 
   1525 	unit = WDUNIT(dev);
   1526 	wd = device_lookup_private(&wd_cd, unit);
   1527 	if (wd == NULL)
   1528 		return (ENXIO);
   1529 	dksc = &wd->sc_dksc;
   1530 
   1531 	return dk_dump(dksc, dev, blkno, va, size);
   1532 }
   1533 
   1534 static int
   1535 wd_dumpblocks(device_t dev, void *va, daddr_t blkno, int nblk)
   1536 {
   1537 	struct wd_softc *wd = device_private(dev);
   1538 	struct dk_softc *dksc = &wd->sc_dksc;
   1539 	struct disk_geom *dg = &dksc->sc_dkdev.dk_geom;
   1540 	struct ata_xfer *xfer = &wd->dump_xfer;
   1541 	int err;
   1542 
   1543 	/* Recalibrate, if first dump transfer. */
   1544 	if (wddumprecalibrated == 0) {
   1545 		wddumprecalibrated = 1;
   1546 		ata_channel_lock(wd->drvp->chnl_softc);
   1547 		/* This will directly execute the reset due to AT_POLL */
   1548 		ata_thread_run(wd->drvp->chnl_softc, AT_POLL,
   1549 		    ATACH_TH_DRIVE_RESET, wd->drvp->drive);
   1550 
   1551 		wd->drvp->state = RESET;
   1552 		ata_channel_unlock(wd->drvp->chnl_softc);
   1553 	}
   1554 
   1555 	memset(xfer, 0, sizeof(*xfer));
   1556 	xfer->c_flags |= C_PRIVATE_ALLOC | C_SKIP_QUEUE;
   1557 
   1558 	xfer->c_bio.blkno = blkno;
   1559 	xfer->c_bio.flags = ATA_POLL;
   1560 	if (wd->sc_flags & WDF_LBA48 &&
   1561 	    (xfer->c_bio.blkno + nblk) > wd->sc_capacity28)
   1562 		xfer->c_bio.flags |= ATA_LBA48;
   1563 	if (wd->sc_flags & WDF_LBA)
   1564 		xfer->c_bio.flags |= ATA_LBA;
   1565 	xfer->c_bio.bcount = nblk * dg->dg_secsize;
   1566 	xfer->c_bio.databuf = va;
   1567 #ifndef WD_DUMP_NOT_TRUSTED
   1568 	switch (err = wd->atabus->ata_bio(wd->drvp, xfer)) {
   1569 	case ATACMD_TRY_AGAIN:
   1570 		panic("wddump: try again");
   1571 		break;
   1572 	case ATACMD_QUEUED:
   1573 		panic("wddump: polled command has been queued");
   1574 		break;
   1575 	case ATACMD_COMPLETE:
   1576 		break;
   1577 	default:
   1578 		panic("wddump: unknown atacmd code %d", err);
   1579 	}
   1580 	switch(err = xfer->c_bio.error) {
   1581 	case TIMEOUT:
   1582 		printf("wddump: device timed out");
   1583 		err = EIO;
   1584 		break;
   1585 	case ERR_DF:
   1586 		printf("wddump: drive fault");
   1587 		err = EIO;
   1588 		break;
   1589 	case ERR_DMA:
   1590 		printf("wddump: DMA error");
   1591 		err = EIO;
   1592 		break;
   1593 	case ERROR:
   1594 		printf("wddump: ");
   1595 		wdperror(wd, xfer);
   1596 		err = EIO;
   1597 		break;
   1598 	case NOERROR:
   1599 		err = 0;
   1600 		break;
   1601 	default:
   1602 		panic("wddump: unknown error type %x", err);
   1603 	}
   1604 
   1605 	if (err != 0) {
   1606 		printf("\n");
   1607 		return err;
   1608 	}
   1609 #else	/* WD_DUMP_NOT_TRUSTED */
   1610 	/* Let's just talk about this first... */
   1611 	printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
   1612 	    unit, va, cylin, head, sector);
   1613 	delay(500 * 1000);	/* half a second */
   1614 #endif
   1615 
   1616 	wddoingadump = 0;
   1617 	return 0;
   1618 }
   1619 
   1620 #ifdef HAS_BAD144_HANDLING
   1621 /*
   1622  * Internalize the bad sector table.
   1623  */
   1624 void
   1625 bad144intern(struct wd_softc *wd)
   1626 {
   1627 	struct dk_softc *dksc = &wd->sc_dksc;
   1628 	struct dkbad *bt = &dksc->sc_dkdev.dk_cpulabel->bad;
   1629 	struct disklabel *lp = dksc->sc_dkdev.dk_label;
   1630 	int i = 0;
   1631 
   1632 	ATADEBUG_PRINT(("bad144intern\n"), DEBUG_XFERS);
   1633 
   1634 	for (; i < NBT_BAD; i++) {
   1635 		if (bt->bt_bad[i].bt_cyl == 0xffff)
   1636 			break;
   1637 		wd->drvp->badsect[i] =
   1638 		    bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
   1639 		    (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
   1640 		    (bt->bt_bad[i].bt_trksec & 0xff);
   1641 	}
   1642 	for (; i < NBT_BAD+1; i++)
   1643 		wd->drvp->badsect[i] = -1;
   1644 }
   1645 #endif
   1646 
   1647 static void
   1648 wd_set_geometry(struct wd_softc *wd)
   1649 {
   1650 	struct dk_softc *dksc = &wd->sc_dksc;
   1651 	struct disk_geom *dg = &dksc->sc_dkdev.dk_geom;
   1652 
   1653 	memset(dg, 0, sizeof(*dg));
   1654 
   1655 	dg->dg_secperunit = wd->sc_capacity;
   1656 	dg->dg_secsize = wd->sc_blksize;
   1657 	dg->dg_nsectors = wd->sc_params.atap_sectors;
   1658 	dg->dg_ntracks = wd->sc_params.atap_heads;
   1659 	if ((wd->sc_flags & WDF_LBA) == 0)
   1660 		dg->dg_ncylinders = wd->sc_params.atap_cylinders;
   1661 
   1662 	disk_set_info(dksc->sc_dev, &dksc->sc_dkdev, wd->sc_typename);
   1663 }
   1664 
   1665 int
   1666 wd_get_params(struct wd_softc *wd, uint8_t flags, struct ataparams *params)
   1667 {
   1668 	int retry = 0;
   1669 	struct ata_channel *chp = wd->drvp->chnl_softc;
   1670 
   1671 again:
   1672 	switch (wd->atabus->ata_get_params(wd->drvp, flags, params)) {
   1673 	case CMD_AGAIN:
   1674 		return 1;
   1675 	case CMD_ERR:
   1676 		if (retry == 0) {
   1677 			retry++;
   1678 			ata_channel_lock(chp);
   1679 			(*wd->atabus->ata_reset_drive)(wd->drvp, flags, NULL);
   1680 			ata_channel_unlock(chp);
   1681 			goto again;
   1682 		}
   1683 
   1684 		if (wd->drvp->drive_type != ATA_DRIVET_OLD)
   1685 			return 1;
   1686 		/*
   1687 		 * We `know' there's a drive here; just assume it's old.
   1688 		 * This geometry is only used to read the MBR and print a
   1689 		 * (false) attach message.
   1690 		 */
   1691 		strncpy(params->atap_model, "ST506",
   1692 		    sizeof params->atap_model);
   1693 		params->atap_config = ATA_CFG_FIXED;
   1694 		params->atap_cylinders = 1024;
   1695 		params->atap_heads = 8;
   1696 		params->atap_sectors = 17;
   1697 		params->atap_multi = 1;
   1698 		params->atap_capabilities1 = params->atap_capabilities2 = 0;
   1699 		wd->drvp->ata_vers = -1; /* Mark it as pre-ATA */
   1700 		/* FALLTHROUGH */
   1701 	case CMD_OK:
   1702 		return 0;
   1703 	default:
   1704 		panic("wd_get_params: bad return code from ata_get_params");
   1705 		/* NOTREACHED */
   1706 	}
   1707 }
   1708 
   1709 int
   1710 wd_getcache(struct wd_softc *wd, int *bitsp)
   1711 {
   1712 	struct ataparams params;
   1713 
   1714 	if (wd_get_params(wd, AT_WAIT, &params) != 0)
   1715 		return EIO;
   1716 	if (params.atap_cmd_set1 == 0x0000 ||
   1717 	    params.atap_cmd_set1 == 0xffff ||
   1718 	    (params.atap_cmd_set1 & WDC_CMD1_CACHE) == 0) {
   1719 		*bitsp = 0;
   1720 		return 0;
   1721 	}
   1722 	*bitsp = DKCACHE_WCHANGE | DKCACHE_READ;
   1723 	if (params.atap_cmd1_en & WDC_CMD1_CACHE)
   1724 		*bitsp |= DKCACHE_WRITE;
   1725 
   1726 	if (WD_USE_NCQ(wd) || (wd->drvp->drive_flags & ATA_DRIVE_WFUA))
   1727 		*bitsp |= DKCACHE_FUA;
   1728 
   1729 	return 0;
   1730 }
   1731 
   1732 const char at_errbits[] = "\20\10ERROR\11TIMEOU\12DF";
   1733 
   1734 int
   1735 wd_setcache(struct wd_softc *wd, int bits)
   1736 {
   1737 	struct dk_softc *dksc = &wd->sc_dksc;
   1738 	struct ataparams params;
   1739 	struct ata_xfer *xfer;
   1740 	int error;
   1741 
   1742 	if (wd_get_params(wd, AT_WAIT, &params) != 0)
   1743 		return EIO;
   1744 
   1745 	if (params.atap_cmd_set1 == 0x0000 ||
   1746 	    params.atap_cmd_set1 == 0xffff ||
   1747 	    (params.atap_cmd_set1 & WDC_CMD1_CACHE) == 0)
   1748 		return EOPNOTSUPP;
   1749 
   1750 	if ((bits & DKCACHE_READ) == 0 ||
   1751 	    (bits & DKCACHE_SAVE) != 0)
   1752 		return EOPNOTSUPP;
   1753 
   1754 	xfer = ata_get_xfer(wd->drvp->chnl_softc, true);
   1755 
   1756 	xfer->c_ata_c.r_command = SET_FEATURES;
   1757 	xfer->c_ata_c.r_st_bmask = 0;
   1758 	xfer->c_ata_c.r_st_pmask = 0;
   1759 	xfer->c_ata_c.timeout = 30000; /* 30s timeout */
   1760 	xfer->c_ata_c.flags = AT_WAIT;
   1761 	if (bits & DKCACHE_WRITE)
   1762 		xfer->c_ata_c.r_features = WDSF_WRITE_CACHE_EN;
   1763 	else
   1764 		xfer->c_ata_c.r_features = WDSF_WRITE_CACHE_DS;
   1765 	if (wd->atabus->ata_exec_command(wd->drvp, xfer) != ATACMD_COMPLETE) {
   1766 		aprint_error_dev(dksc->sc_dev,
   1767 		    "wd_setcache command not complete\n");
   1768 		error = EIO;
   1769 		goto out;
   1770 	}
   1771 
   1772 	if (xfer->c_ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
   1773 		char sbuf[sizeof(at_errbits) + 64];
   1774 		snprintb(sbuf, sizeof(sbuf), at_errbits, xfer->c_ata_c.flags);
   1775 		aprint_error_dev(dksc->sc_dev, "wd_setcache: status=%s\n", sbuf);
   1776 		error = EIO;
   1777 		goto out;
   1778 	}
   1779 
   1780 	error = 0;
   1781 
   1782 out:
   1783 	ata_free_xfer(wd->drvp->chnl_softc, xfer);
   1784 	return error;
   1785 }
   1786 
   1787 static int
   1788 wd_standby(struct wd_softc *wd, int flags)
   1789 {
   1790 	struct dk_softc *dksc = &wd->sc_dksc;
   1791 	struct ata_xfer *xfer;
   1792 	int error;
   1793 
   1794 	aprint_debug_dev(dksc->sc_dev, "standby immediate\n");
   1795 	xfer = ata_get_xfer(wd->drvp->chnl_softc, true);
   1796 
   1797 	xfer->c_ata_c.r_command = WDCC_STANDBY_IMMED;
   1798 	xfer->c_ata_c.r_st_bmask = WDCS_DRDY;
   1799 	xfer->c_ata_c.r_st_pmask = WDCS_DRDY;
   1800 	xfer->c_ata_c.flags = flags;
   1801 	xfer->c_ata_c.timeout = 30000; /* 30s timeout */
   1802 	if (wd->atabus->ata_exec_command(wd->drvp, xfer) != ATACMD_COMPLETE) {
   1803 		aprint_error_dev(dksc->sc_dev,
   1804 		    "standby immediate command didn't complete\n");
   1805 		error = EIO;
   1806 		goto out;
   1807 	}
   1808 	if (xfer->c_ata_c.flags & AT_ERROR) {
   1809 		if (xfer->c_ata_c.r_error == WDCE_ABRT) {
   1810 			/* command not supported */
   1811 			aprint_debug_dev(dksc->sc_dev,
   1812 				"standby immediate not supported\n");
   1813 			error = ENODEV;
   1814 			goto out;
   1815 		}
   1816 	}
   1817 	if (xfer->c_ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
   1818 		char sbuf[sizeof(at_errbits) + 64];
   1819 		snprintb(sbuf, sizeof(sbuf), at_errbits, xfer->c_ata_c.flags);
   1820 		aprint_error_dev(dksc->sc_dev, "wd_standby: status=%s\n", sbuf);
   1821 		error = EIO;
   1822 		goto out;
   1823 	}
   1824 	error = 0;
   1825 
   1826 out:
   1827 	ata_free_xfer(wd->drvp->chnl_softc, xfer);
   1828 	return error;
   1829 }
   1830 
   1831 int
   1832 wd_flushcache(struct wd_softc *wd, int flags, bool start_self)
   1833 {
   1834 	struct dk_softc *dksc = &wd->sc_dksc;
   1835 	struct ata_xfer *xfer;
   1836 	int error;
   1837 
   1838 	/*
   1839 	 * WDCC_FLUSHCACHE is here since ATA-4, but some drives report
   1840 	 * only ATA-2 and still support it.
   1841 	 */
   1842 	if (wd->drvp->ata_vers < 4 &&
   1843 	    ((wd->sc_params.atap_cmd_set2 & WDC_CMD2_FC) == 0 ||
   1844 	    wd->sc_params.atap_cmd_set2 == 0xffff))
   1845 		return ENODEV;
   1846 
   1847 	xfer = ata_get_xfer(wd->drvp->chnl_softc, true);
   1848 
   1849 	if ((wd->sc_params.atap_cmd2_en & ATA_CMD2_LBA48) != 0 &&
   1850 	    (wd->sc_params.atap_cmd2_en & ATA_CMD2_FCE) != 0) {
   1851 		xfer->c_ata_c.r_command = WDCC_FLUSHCACHE_EXT;
   1852 		flags |= AT_LBA48;
   1853 	} else
   1854 		xfer->c_ata_c.r_command = WDCC_FLUSHCACHE;
   1855 	xfer->c_ata_c.r_st_bmask = WDCS_DRDY;
   1856 	xfer->c_ata_c.r_st_pmask = WDCS_DRDY;
   1857 	xfer->c_ata_c.flags = flags | AT_READREG;
   1858 	xfer->c_ata_c.timeout = 300000; /* 5m timeout */
   1859 	if (wd->atabus->ata_exec_command(wd->drvp, xfer) != ATACMD_COMPLETE) {
   1860 		aprint_error_dev(dksc->sc_dev,
   1861 		    "flush cache command didn't complete\n");
   1862 		error = EIO;
   1863 		goto out_xfer;
   1864 	}
   1865 	if (xfer->c_ata_c.flags & AT_ERROR) {
   1866 		if (xfer->c_ata_c.r_error == WDCE_ABRT) {
   1867 			/* command not supported */
   1868 			error = ENODEV;
   1869 			goto out_xfer;
   1870 		}
   1871 	}
   1872 	if (xfer->c_ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
   1873 		char sbuf[sizeof(at_errbits) + 64];
   1874 		snprintb(sbuf, sizeof(sbuf), at_errbits, xfer->c_ata_c.flags);
   1875 		aprint_error_dev(dksc->sc_dev, "wd_flushcache: status=%s\n",
   1876 		    sbuf);
   1877 		error = EIO;
   1878 		goto out_xfer;
   1879 	}
   1880 	wd->sc_flags &= ~WDF_DIRTY;
   1881 	error = 0;
   1882 
   1883 out_xfer:
   1884 	ata_free_xfer(wd->drvp->chnl_softc, xfer);
   1885 	return error;
   1886 }
   1887 
   1888 /*
   1889  * Execute TRIM command, assumes sleep context.
   1890  */
   1891 static int
   1892 wd_trim(struct wd_softc *wd, daddr_t bno, long size)
   1893 {
   1894 	struct dk_softc *dksc = &wd->sc_dksc;
   1895 	struct ata_xfer *xfer;
   1896 	int error;
   1897 	unsigned char *req;
   1898 
   1899 	xfer = ata_get_xfer(wd->drvp->chnl_softc, true);
   1900 
   1901 	req = kmem_zalloc(512, KM_SLEEP);
   1902 	req[0] = bno & 0xff;
   1903 	req[1] = (bno >> 8) & 0xff;
   1904 	req[2] = (bno >> 16) & 0xff;
   1905 	req[3] = (bno >> 24) & 0xff;
   1906 	req[4] = (bno >> 32) & 0xff;
   1907 	req[5] = (bno >> 40) & 0xff;
   1908 	req[6] = size & 0xff;
   1909 	req[7] = (size >> 8) & 0xff;
   1910 
   1911 	/*
   1912  	 * XXX We could possibly use NCQ TRIM, which supports executing
   1913  	 * this command concurrently. It would need some investigation, some
   1914  	 * early or not so early disk firmware caused data loss with NCQ TRIM.
   1915 	 * atastart() et.al would need to be adjusted to allow and support
   1916 	 * running several non-I/O ATA commands in parallel.
   1917 	 */
   1918 
   1919 	xfer->c_ata_c.r_command = ATA_DATA_SET_MANAGEMENT;
   1920 	xfer->c_ata_c.r_count = 1;
   1921 	xfer->c_ata_c.r_features = ATA_SUPPORT_DSM_TRIM;
   1922 	xfer->c_ata_c.r_st_bmask = WDCS_DRDY;
   1923 	xfer->c_ata_c.r_st_pmask = WDCS_DRDY;
   1924 	xfer->c_ata_c.timeout = 30000; /* 30s timeout */
   1925 	xfer->c_ata_c.data = req;
   1926 	xfer->c_ata_c.bcount = 512;
   1927 	xfer->c_ata_c.flags |= AT_WRITE | AT_WAIT;
   1928 	if (wd->atabus->ata_exec_command(wd->drvp, xfer) != ATACMD_COMPLETE) {
   1929 		aprint_error_dev(dksc->sc_dev,
   1930 		    "trim command didn't complete\n");
   1931 		kmem_free(req, 512);
   1932 		error = EIO;
   1933 		goto out;
   1934 	}
   1935 	kmem_free(req, 512);
   1936 	if (xfer->c_ata_c.flags & AT_ERROR) {
   1937 		if (xfer->c_ata_c.r_error == WDCE_ABRT) {
   1938 			/* command not supported */
   1939 			error = ENODEV;
   1940 			goto out;
   1941 		}
   1942 	}
   1943 	if (xfer->c_ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
   1944 		char sbuf[sizeof(at_errbits) + 64];
   1945 		snprintb(sbuf, sizeof(sbuf), at_errbits, xfer->c_ata_c.flags);
   1946 		aprint_error_dev(dksc->sc_dev, "wd_trim: status=%s\n",
   1947 		    sbuf);
   1948 		error = EIO;
   1949 		goto out;
   1950 	}
   1951 	error = 0;
   1952 
   1953 out:
   1954 	ata_free_xfer(wd->drvp->chnl_softc, xfer);
   1955 	return error;
   1956 }
   1957 
   1958 bool
   1959 wd_shutdown(device_t dev, int how)
   1960 {
   1961 	struct wd_softc *wd = device_private(dev);
   1962 
   1963 	/* the adapter needs to be enabled */
   1964 	if (wd->atabus->ata_addref(wd->drvp))
   1965 		return true; /* no need to complain */
   1966 
   1967 	wd_flushcache(wd, AT_POLL, false);
   1968 	if ((how & RB_POWERDOWN) == RB_POWERDOWN)
   1969 		wd_standby(wd, AT_POLL);
   1970 	return true;
   1971 }
   1972 
   1973 /*
   1974  * Allocate space for a ioctl queue structure.  Mostly taken from
   1975  * scsipi_ioctl.c
   1976  */
   1977 struct wd_ioctl *
   1978 wi_get(struct wd_softc *wd)
   1979 {
   1980 	struct wd_ioctl *wi;
   1981 
   1982 	wi = kmem_zalloc(sizeof(struct wd_ioctl), KM_SLEEP);
   1983 	wi->wi_softc = wd;
   1984 	buf_init(&wi->wi_bp);
   1985 
   1986 	return (wi);
   1987 }
   1988 
   1989 /*
   1990  * Free an ioctl structure and remove it from our list
   1991  */
   1992 
   1993 void
   1994 wi_free(struct wd_ioctl *wi)
   1995 {
   1996 	buf_destroy(&wi->wi_bp);
   1997 	kmem_free(wi, sizeof(*wi));
   1998 }
   1999 
   2000 /*
   2001  * Find a wd_ioctl structure based on the struct buf.
   2002  */
   2003 
   2004 struct wd_ioctl *
   2005 wi_find(struct buf *bp)
   2006 {
   2007 	return container_of(bp, struct wd_ioctl, wi_bp);
   2008 }
   2009 
   2010 static uint
   2011 wi_sector_size(const struct wd_ioctl * const wi)
   2012 {
   2013 	switch (wi->wi_atareq.command) {
   2014 	case WDCC_READ:
   2015 	case WDCC_WRITE:
   2016 	case WDCC_READMULTI:
   2017 	case WDCC_WRITEMULTI:
   2018 	case WDCC_READDMA:
   2019 	case WDCC_WRITEDMA:
   2020 	case WDCC_READ_EXT:
   2021 	case WDCC_WRITE_EXT:
   2022 	case WDCC_READMULTI_EXT:
   2023 	case WDCC_WRITEMULTI_EXT:
   2024 	case WDCC_READDMA_EXT:
   2025 	case WDCC_WRITEDMA_EXT:
   2026 	case WDCC_READ_FPDMA_QUEUED:
   2027 	case WDCC_WRITE_FPDMA_QUEUED:
   2028 		return wi->wi_softc->sc_blksize;
   2029 	default:
   2030 		return 512;
   2031 	}
   2032 }
   2033 
   2034 /*
   2035  * Ioctl pseudo strategy routine
   2036  *
   2037  * This is mostly stolen from scsipi_ioctl.c:scsistrategy().  What
   2038  * happens here is:
   2039  *
   2040  * - wdioctl() queues a wd_ioctl structure.
   2041  *
   2042  * - wdioctl() calls physio/wdioctlstrategy based on whether or not
   2043  *   user space I/O is required.  If physio() is called, physio() eventually
   2044  *   calls wdioctlstrategy().
   2045  *
   2046  * - In either case, wdioctlstrategy() calls wd->atabus->ata_exec_command()
   2047  *   to perform the actual command
   2048  *
   2049  * The reason for the use of the pseudo strategy routine is because
   2050  * when doing I/O to/from user space, physio _really_ wants to be in
   2051  * the loop.  We could put the entire buffer into the ioctl request
   2052  * structure, but that won't scale if we want to do things like download
   2053  * microcode.
   2054  */
   2055 
   2056 void
   2057 wdioctlstrategy(struct buf *bp)
   2058 {
   2059 	struct wd_ioctl *wi;
   2060 	struct ata_xfer *xfer;
   2061 	int error = 0;
   2062 
   2063 	wi = wi_find(bp);
   2064 	if (wi == NULL) {
   2065 		printf("wdioctlstrategy: "
   2066 		    "No matching ioctl request found in queue\n");
   2067 		error = EINVAL;
   2068 		goto out2;
   2069 	}
   2070 
   2071 	xfer = ata_get_xfer(wi->wi_softc->drvp->chnl_softc, true);
   2072 
   2073 	/*
   2074 	 * Abort if physio broke up the transfer
   2075 	 */
   2076 
   2077 	if (bp->b_bcount != wi->wi_atareq.datalen) {
   2078 		printf("physio split wd ioctl request... cannot proceed\n");
   2079 		error = EIO;
   2080 		goto out;
   2081 	}
   2082 
   2083 	/*
   2084 	 * Abort if we didn't get a buffer size that was a multiple of
   2085 	 * our sector size (or overflows CHS/LBA28 sector count)
   2086 	 */
   2087 
   2088 	if ((bp->b_bcount % wi_sector_size(wi)) != 0 ||
   2089 	    (bp->b_bcount / wi_sector_size(wi)) >=
   2090 	     (1 << NBBY)) {
   2091 		error = EINVAL;
   2092 		goto out;
   2093 	}
   2094 
   2095 	/*
   2096 	 * Make sure a timeout was supplied in the ioctl request
   2097 	 */
   2098 
   2099 	if (wi->wi_atareq.timeout == 0) {
   2100 		error = EINVAL;
   2101 		goto out;
   2102 	}
   2103 
   2104 	if (wi->wi_atareq.flags & ATACMD_READ)
   2105 		xfer->c_ata_c.flags |= AT_READ;
   2106 	else if (wi->wi_atareq.flags & ATACMD_WRITE)
   2107 		xfer->c_ata_c.flags |= AT_WRITE;
   2108 
   2109 	if (wi->wi_atareq.flags & ATACMD_READREG)
   2110 		xfer->c_ata_c.flags |= AT_READREG;
   2111 
   2112 	if ((wi->wi_atareq.flags & ATACMD_LBA) != 0)
   2113 		xfer->c_ata_c.flags |= AT_LBA;
   2114 
   2115 	xfer->c_ata_c.flags |= AT_WAIT;
   2116 
   2117 	xfer->c_ata_c.timeout = wi->wi_atareq.timeout;
   2118 	xfer->c_ata_c.r_command = wi->wi_atareq.command;
   2119 	xfer->c_ata_c.r_lba = ((wi->wi_atareq.head & 0x0f) << 24) |
   2120 	    (wi->wi_atareq.cylinder << 8) |
   2121 	    wi->wi_atareq.sec_num;
   2122 	xfer->c_ata_c.r_count = wi->wi_atareq.sec_count;
   2123 	xfer->c_ata_c.r_features = wi->wi_atareq.features;
   2124 	xfer->c_ata_c.r_st_bmask = WDCS_DRDY;
   2125 	xfer->c_ata_c.r_st_pmask = WDCS_DRDY;
   2126 	xfer->c_ata_c.data = wi->wi_bp.b_data;
   2127 	xfer->c_ata_c.bcount = wi->wi_bp.b_bcount;
   2128 
   2129 	if (wi->wi_softc->atabus->ata_exec_command(wi->wi_softc->drvp, xfer)
   2130 	    != ATACMD_COMPLETE) {
   2131 		wi->wi_atareq.retsts = ATACMD_ERROR;
   2132 		error = EIO;
   2133 		goto out;
   2134 	}
   2135 
   2136 	if (xfer->c_ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
   2137 		if (xfer->c_ata_c.flags & AT_ERROR) {
   2138 			wi->wi_atareq.retsts = ATACMD_ERROR;
   2139 			wi->wi_atareq.error = xfer->c_ata_c.r_error;
   2140 		} else if (xfer->c_ata_c.flags & AT_DF)
   2141 			wi->wi_atareq.retsts = ATACMD_DF;
   2142 		else
   2143 			wi->wi_atareq.retsts = ATACMD_TIMEOUT;
   2144 	} else {
   2145 		wi->wi_atareq.retsts = ATACMD_OK;
   2146 		if (wi->wi_atareq.flags & ATACMD_READREG) {
   2147 			wi->wi_atareq.command = xfer->c_ata_c.r_status;
   2148 			wi->wi_atareq.features = xfer->c_ata_c.r_error;
   2149 			wi->wi_atareq.sec_count = xfer->c_ata_c.r_count;
   2150 			wi->wi_atareq.sec_num = xfer->c_ata_c.r_lba & 0xff;
   2151 			wi->wi_atareq.head = (xfer->c_ata_c.r_device & 0xf0) |
   2152 			    ((xfer->c_ata_c.r_lba >> 24) & 0x0f);
   2153 			wi->wi_atareq.cylinder =
   2154 			    (xfer->c_ata_c.r_lba >> 8) & 0xffff;
   2155 			wi->wi_atareq.error = xfer->c_ata_c.r_error;
   2156 		}
   2157 	}
   2158 
   2159 out:
   2160 	ata_free_xfer(wi->wi_softc->drvp->chnl_softc, xfer);
   2161 out2:
   2162 	bp->b_error = error;
   2163 	if (error)
   2164 		bp->b_resid = bp->b_bcount;
   2165 	biodone(bp);
   2166 }
   2167 
   2168 static void
   2169 wd_sysctl_attach(struct wd_softc *wd)
   2170 {
   2171 	struct dk_softc *dksc = &wd->sc_dksc;
   2172 	const struct sysctlnode *node;
   2173 	int error;
   2174 
   2175 	/* sysctl set-up */
   2176 	if (sysctl_createv(&wd->nodelog, 0, NULL, &node,
   2177 				0, CTLTYPE_NODE, dksc->sc_xname,
   2178 				SYSCTL_DESCR("wd driver settings"),
   2179 				NULL, 0, NULL, 0,
   2180 				CTL_HW, CTL_CREATE, CTL_EOL) != 0) {
   2181 		aprint_error_dev(dksc->sc_dev,
   2182 		    "could not create %s.%s sysctl node\n",
   2183 		    "hw", dksc->sc_xname);
   2184 		return;
   2185 	}
   2186 
   2187 	wd->drv_ncq = ((wd->sc_quirks & WD_QUIRK_BAD_NCQ) == 0) ? true : false;
   2188 	if ((error = sysctl_createv(&wd->nodelog, 0, NULL, NULL,
   2189 				CTLFLAG_READWRITE, CTLTYPE_BOOL, "use_ncq",
   2190 				SYSCTL_DESCR("use NCQ if supported"),
   2191 				NULL, 0, &wd->drv_ncq, 0,
   2192 				CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL))
   2193 				!= 0) {
   2194 		aprint_error_dev(dksc->sc_dev,
   2195 		    "could not create %s.%s.use_ncq sysctl - error %d\n",
   2196 		    "hw", dksc->sc_xname, error);
   2197 		return;
   2198 	}
   2199 
   2200 	wd->drv_ncq_prio = false;
   2201 	if ((error = sysctl_createv(&wd->nodelog, 0, NULL, NULL,
   2202 				CTLFLAG_READWRITE, CTLTYPE_BOOL, "use_ncq_prio",
   2203 				SYSCTL_DESCR("use NCQ PRIORITY if supported"),
   2204 				NULL, 0, &wd->drv_ncq_prio, 0,
   2205 				CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL))
   2206 				!= 0) {
   2207 		aprint_error_dev(dksc->sc_dev,
   2208 		    "could not create %s.%s.use_ncq_prio sysctl - error %d\n",
   2209 		    "hw", dksc->sc_xname, error);
   2210 		return;
   2211 	}
   2212 
   2213 #ifdef WD_CHAOS_MONKEY
   2214 	wd->drv_chaos_freq = 0;
   2215 	if ((error = sysctl_createv(&wd->nodelog, 0, NULL, NULL,
   2216 				CTLFLAG_READWRITE, CTLTYPE_INT, "chaos_freq",
   2217 				SYSCTL_DESCR("simulated bio read error rate"),
   2218 				NULL, 0, &wd->drv_chaos_freq, 0,
   2219 				CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL))
   2220 				!= 0) {
   2221 		aprint_error_dev(dksc->sc_dev,
   2222 		    "could not create %s.%s.chaos_freq sysctl - error %d\n",
   2223 		    "hw", dksc->sc_xname, error);
   2224 		return;
   2225 	}
   2226 
   2227 	wd->drv_chaos_cnt = 0;
   2228 	if ((error = sysctl_createv(&wd->nodelog, 0, NULL, NULL,
   2229 				CTLFLAG_READONLY, CTLTYPE_INT, "chaos_cnt",
   2230 				SYSCTL_DESCR("number of processed bio reads"),
   2231 				NULL, 0, &wd->drv_chaos_cnt, 0,
   2232 				CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL))
   2233 				!= 0) {
   2234 		aprint_error_dev(dksc->sc_dev,
   2235 		    "could not create %s.%s.chaos_cnt sysctl - error %d\n",
   2236 		    "hw", dksc->sc_xname, error);
   2237 		return;
   2238 	}
   2239 #endif
   2240 
   2241 }
   2242 
   2243 static void
   2244 wd_sysctl_detach(struct wd_softc *wd)
   2245 {
   2246 	sysctl_teardown(&wd->nodelog);
   2247 }
   2248 
   2249 #ifdef ATADEBUG
   2250 int wddebug(void);
   2251 
   2252 int
   2253 wddebug(void)
   2254 {
   2255 	struct wd_softc *wd;
   2256 	  struct dk_softc *dksc;
   2257 	  int unit;
   2258 
   2259 	  for (unit = 0; unit <= 3; unit++) {
   2260 		    wd = device_lookup_private(&wd_cd, unit);
   2261 		    if (wd == NULL)
   2262 				continue;
   2263 		    dksc = &wd->sc_dksc;
   2264 		printf("%s fl %x bufq %p:\n",
   2265 		    dksc->sc_xname, wd->sc_flags, bufq_peek(dksc->sc_bufq));
   2266 
   2267 		atachannel_debug(wd->drvp->chnl_softc);
   2268 	}
   2269 	return 0;
   2270 }
   2271 #endif /* ATADEBUG */
   2272