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