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