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