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