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