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