Home | History | Annotate | Line # | Download | only in ata
wd.c revision 1.303
      1 /*	$NetBSD: wd.c,v 1.303 2005/06/16 20:03:35 bouyer 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  * 3. All advertising materials mentioning features or use of this software
     15  *	must display the following acknowledgement:
     16  *  This product includes software developed by Manuel Bouyer.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *	derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 1998, 2003, 2004 The NetBSD Foundation, Inc.
     34  * All rights reserved.
     35  *
     36  * This code is derived from software contributed to The NetBSD Foundation
     37  * by Charles M. Hannum and by Onno van der Linden.
     38  *
     39  * Redistribution and use in source and binary forms, with or without
     40  * modification, are permitted provided that the following conditions
     41  * are met:
     42  * 1. Redistributions of source code must retain the above copyright
     43  *    notice, this list of conditions and the following disclaimer.
     44  * 2. Redistributions in binary form must reproduce the above copyright
     45  *    notice, this list of conditions and the following disclaimer in the
     46  *    documentation and/or other materials provided with the distribution.
     47  * 3. All advertising materials mentioning features or use of this software
     48  *    must display the following acknowledgement:
     49  *        This product includes software developed by the NetBSD
     50  *        Foundation, Inc. and its contributors.
     51  * 4. Neither the name of The NetBSD Foundation nor the names of its
     52  *    contributors may be used to endorse or promote products derived
     53  *    from this software without specific prior written permission.
     54  *
     55  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     56  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     57  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     58  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     59  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     60  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     61  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     62  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     63  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     64  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     65  * POSSIBILITY OF SUCH DAMAGE.
     66  */
     67 
     68 #include <sys/cdefs.h>
     69 __KERNEL_RCSID(0, "$NetBSD: wd.c,v 1.303 2005/06/16 20:03:35 bouyer Exp $");
     70 
     71 #ifndef ATADEBUG
     72 #define ATADEBUG
     73 #endif /* ATADEBUG */
     74 
     75 #include "rnd.h"
     76 
     77 #include <sys/param.h>
     78 #include <sys/systm.h>
     79 #include <sys/kernel.h>
     80 #include <sys/conf.h>
     81 #include <sys/file.h>
     82 #include <sys/stat.h>
     83 #include <sys/ioctl.h>
     84 #include <sys/buf.h>
     85 #include <sys/bufq.h>
     86 #include <sys/uio.h>
     87 #include <sys/malloc.h>
     88 #include <sys/device.h>
     89 #include <sys/disklabel.h>
     90 #include <sys/disk.h>
     91 #include <sys/syslog.h>
     92 #include <sys/proc.h>
     93 #include <sys/vnode.h>
     94 #if NRND > 0
     95 #include <sys/rnd.h>
     96 #endif
     97 
     98 #include <machine/intr.h>
     99 #include <machine/bus.h>
    100 
    101 #include <dev/ata/atareg.h>
    102 #include <dev/ata/atavar.h>
    103 #include <dev/ata/wdvar.h>
    104 #include <dev/ic/wdcreg.h>
    105 #include <sys/ataio.h>
    106 #include "locators.h"
    107 
    108 #define	LBA48_THRESHOLD		(0xfffffff)	/* 128GB / DEV_BSIZE */
    109 
    110 #define	WDIORETRIES_SINGLE 4	/* number of retries before single-sector */
    111 #define	WDIORETRIES	5	/* number of retries before giving up */
    112 #define	RECOVERYTIME hz/2	/* time to wait before retrying a cmd */
    113 
    114 #define	WDUNIT(dev)		DISKUNIT(dev)
    115 #define	WDPART(dev)		DISKPART(dev)
    116 #define	WDMINOR(unit, part)	DISKMINOR(unit, part)
    117 #define	MAKEWDDEV(maj, unit, part)	MAKEDISKDEV(maj, unit, part)
    118 
    119 #define	WDLABELDEV(dev)	(MAKEWDDEV(major(dev), WDUNIT(dev), RAW_PART))
    120 
    121 #define DEBUG_INTR   0x01
    122 #define DEBUG_XFERS  0x02
    123 #define DEBUG_STATUS 0x04
    124 #define DEBUG_FUNCS  0x08
    125 #define DEBUG_PROBE  0x10
    126 #ifdef ATADEBUG
    127 int wdcdebug_wd_mask = 0x0;
    128 #define ATADEBUG_PRINT(args, level) \
    129 	if (wdcdebug_wd_mask & (level)) \
    130 		printf args
    131 #else
    132 #define ATADEBUG_PRINT(args, level)
    133 #endif
    134 
    135 int	wdprobe(struct device *, struct cfdata *, void *);
    136 void	wdattach(struct device *, struct device *, void *);
    137 int	wddetach(struct device *, int);
    138 int	wdactivate(struct device *, enum devact);
    139 int	wdprint(void *, char *);
    140 void	wdperror(const struct wd_softc *);
    141 
    142 CFATTACH_DECL(wd, sizeof(struct wd_softc),
    143     wdprobe, wdattach, wddetach, wdactivate);
    144 
    145 extern struct cfdriver wd_cd;
    146 
    147 dev_type_open(wdopen);
    148 dev_type_close(wdclose);
    149 dev_type_read(wdread);
    150 dev_type_write(wdwrite);
    151 dev_type_ioctl(wdioctl);
    152 dev_type_strategy(wdstrategy);
    153 dev_type_dump(wddump);
    154 dev_type_size(wdsize);
    155 
    156 const struct bdevsw wd_bdevsw = {
    157 	wdopen, wdclose, wdstrategy, wdioctl, wddump, wdsize, D_DISK
    158 };
    159 
    160 const struct cdevsw wd_cdevsw = {
    161 	wdopen, wdclose, wdread, wdwrite, wdioctl,
    162 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
    163 };
    164 
    165 /*
    166  * Glue necessary to hook WDCIOCCOMMAND into physio
    167  */
    168 
    169 struct wd_ioctl {
    170 	LIST_ENTRY(wd_ioctl) wi_list;
    171 	struct buf wi_bp;
    172 	struct uio wi_uio;
    173 	struct iovec wi_iov;
    174 	atareq_t wi_atareq;
    175 	struct wd_softc *wi_softc;
    176 };
    177 
    178 LIST_HEAD(, wd_ioctl) wi_head;
    179 
    180 struct	wd_ioctl *wi_find(struct buf *);
    181 void	wi_free(struct wd_ioctl *);
    182 struct	wd_ioctl *wi_get(void);
    183 void	wdioctlstrategy(struct buf *);
    184 
    185 void  wdgetdefaultlabel(struct wd_softc *, struct disklabel *);
    186 void  wdgetdisklabel(struct wd_softc *);
    187 void  wdstart(void *);
    188 void  __wdstart(struct wd_softc*, struct buf *);
    189 void  wdrestart(void *);
    190 void  wddone(void *);
    191 int   wd_get_params(struct wd_softc *, u_int8_t, struct ataparams *);
    192 int   wd_standby(struct wd_softc *, int);
    193 int   wd_flushcache(struct wd_softc *, int);
    194 void  wd_shutdown(void *);
    195 
    196 int   wd_getcache(struct wd_softc *, int *);
    197 int   wd_setcache(struct wd_softc *, int);
    198 
    199 struct dkdriver wddkdriver = { wdstrategy, minphys };
    200 
    201 #ifdef HAS_BAD144_HANDLING
    202 static void bad144intern(struct wd_softc *);
    203 #endif
    204 
    205 #define	WD_QUIRK_SPLIT_MOD15_WRITE	0x0001	/* must split certain writes */
    206 #define	WD_QUIRK_FORCE_LBA48		0x0002	/* must use LBA48 commands */
    207 
    208 /*
    209  * Quirk table for IDE drives.  Put more-specific matches first, since
    210  * a simple globbing routine is used for matching.
    211  */
    212 static const struct wd_quirk {
    213 	const char *wdq_match;		/* inquiry pattern to match */
    214 	int wdq_quirks;			/* drive quirks */
    215 } wd_quirk_table[] = {
    216 	/*
    217 	 * Some Seagate S-ATA drives have a PHY which can get confused
    218 	 * with the way data is packetized by some S-ATA controllers.
    219 	 *
    220 	 * The work-around is to split in two any write transfer whose
    221 	 * sector count % 15 == 1 (assuming 512 byte sectors).
    222 	 *
    223 	 * XXX This is an incomplete list.  There are at least a couple
    224 	 * XXX more model numbers.  If you have trouble with such transfers
    225 	 * XXX (8K is the most common) on Seagate S-ATA drives, please
    226 	 * XXX notify thorpej (at) NetBSD.org.
    227 	 */
    228 	{ "ST3120023AS",
    229 	  WD_QUIRK_SPLIT_MOD15_WRITE },
    230 	{ "ST380023AS",
    231 	  WD_QUIRK_SPLIT_MOD15_WRITE },
    232 
    233 	/*
    234 	 * This seagate drive seems to have issue addressing sector 0xfffffff
    235 	 * (aka LBA48_THRESHOLD) in LBA mode. The workaround is to force
    236 	 * LBA48
    237 	 */
    238 	{ "ST3200822A*",
    239 	  WD_QUIRK_FORCE_LBA48 },
    240 	{ "ST3250823A*",
    241 	  WD_QUIRK_FORCE_LBA48 },
    242 
    243 	{ NULL,
    244 	  0 }
    245 };
    246 
    247 static const struct wd_quirk *
    248 wd_lookup_quirks(const char *name)
    249 {
    250 	const struct wd_quirk *wdq;
    251 	const char *estr;
    252 
    253 	for (wdq = wd_quirk_table; wdq->wdq_match != NULL; wdq++) {
    254 		/*
    255 		 * We only want exact matches (which include matches
    256 		 * against globbing characters).
    257 		 */
    258 		if (pmatch(name, wdq->wdq_match, &estr) == 2)
    259 			return (wdq);
    260 	}
    261 	return (NULL);
    262 }
    263 
    264 int
    265 wdprobe(struct device *parent, struct cfdata *match, void *aux)
    266 {
    267 	struct ata_device *adev = aux;
    268 
    269 	if (adev == NULL)
    270 		return 0;
    271 	if (adev->adev_bustype->bustype_type != SCSIPI_BUSTYPE_ATA)
    272 		return 0;
    273 
    274 	if (match->cf_loc[ATA_HLCF_DRIVE] != ATA_HLCF_DRIVE_DEFAULT &&
    275 	    match->cf_loc[ATA_HLCF_DRIVE] != adev->adev_drv_data->drive)
    276 		return 0;
    277 	return 1;
    278 }
    279 
    280 void
    281 wdattach(struct device *parent, struct device *self, void *aux)
    282 {
    283 	struct wd_softc *wd = (void *)self;
    284 	struct ata_device *adev= aux;
    285 	int i, blank;
    286 	char tbuf[41], pbuf[9], c, *p, *q;
    287 	const struct wd_quirk *wdq;
    288 	ATADEBUG_PRINT(("wdattach\n"), DEBUG_FUNCS | DEBUG_PROBE);
    289 
    290 	callout_init(&wd->sc_restart_ch);
    291 	bufq_alloc(&wd->sc_q, BUFQ_DISK_DEFAULT_STRAT()|BUFQ_SORT_RAWBLOCK);
    292 #ifdef WD_SOFTBADSECT
    293 	SLIST_INIT(&wd->sc_bslist);
    294 #endif
    295 	wd->atabus = adev->adev_bustype;
    296 	wd->openings = adev->adev_openings;
    297 	wd->drvp = adev->adev_drv_data;
    298 
    299 	wd->drvp->drv_done = wddone;
    300 	wd->drvp->drv_softc = &wd->sc_dev;
    301 
    302 	aprint_naive("\n");
    303 
    304 	/* read our drive info */
    305 	if (wd_get_params(wd, AT_WAIT, &wd->sc_params) != 0) {
    306 		aprint_error("\n%s: IDENTIFY failed\n", wd->sc_dev.dv_xname);
    307 		return;
    308 	}
    309 
    310 	for (blank = 0, p = wd->sc_params.atap_model, q = tbuf, i = 0;
    311 	    i < sizeof(wd->sc_params.atap_model); i++) {
    312 		c = *p++;
    313 		if (c == '\0')
    314 			break;
    315 		if (c != ' ') {
    316 			if (blank) {
    317 				*q++ = ' ';
    318 				blank = 0;
    319 			}
    320 			*q++ = c;
    321 		} else
    322 			blank = 1;
    323 	}
    324 	*q++ = '\0';
    325 
    326 	aprint_normal(": <%s>\n", tbuf);
    327 
    328 	wdq = wd_lookup_quirks(tbuf);
    329 	if (wdq != NULL)
    330 		wd->sc_quirks = wdq->wdq_quirks;
    331 
    332 	if ((wd->sc_params.atap_multi & 0xff) > 1) {
    333 		wd->sc_multi = wd->sc_params.atap_multi & 0xff;
    334 	} else {
    335 		wd->sc_multi = 1;
    336 	}
    337 
    338 	aprint_normal("%s: drive supports %d-sector PIO transfers,",
    339 	    wd->sc_dev.dv_xname, wd->sc_multi);
    340 
    341 	/* 48-bit LBA addressing */
    342 	if ((wd->sc_params.atap_cmd2_en & ATA_CMD2_LBA48) != 0)
    343 		wd->sc_flags |= WDF_LBA48;
    344 
    345 	/* Prior to ATA-4, LBA was optional. */
    346 	if ((wd->sc_params.atap_capabilities1 & WDC_CAP_LBA) != 0)
    347 		wd->sc_flags |= WDF_LBA;
    348 #if 0
    349 	/* ATA-4 requires LBA. */
    350 	if (wd->sc_params.atap_ataversion != 0xffff &&
    351 	    wd->sc_params.atap_ataversion >= WDC_VER_ATA4)
    352 		wd->sc_flags |= WDF_LBA;
    353 #endif
    354 
    355 	if ((wd->sc_flags & WDF_LBA48) != 0) {
    356 		aprint_normal(" LBA48 addressing\n");
    357 		wd->sc_capacity =
    358 		    ((u_int64_t) wd->sc_params.__reserved6[11] << 48) |
    359 		    ((u_int64_t) wd->sc_params.__reserved6[10] << 32) |
    360 		    ((u_int64_t) wd->sc_params.__reserved6[9]  << 16) |
    361 		    ((u_int64_t) wd->sc_params.__reserved6[8]  << 0);
    362 	} else if ((wd->sc_flags & WDF_LBA) != 0) {
    363 		aprint_normal(" LBA addressing\n");
    364 		wd->sc_capacity =
    365 		    (wd->sc_params.atap_capacity[1] << 16) |
    366 		    wd->sc_params.atap_capacity[0];
    367 	} else {
    368 		aprint_normal(" chs addressing\n");
    369 		wd->sc_capacity =
    370 		    wd->sc_params.atap_cylinders *
    371 		    wd->sc_params.atap_heads *
    372 		    wd->sc_params.atap_sectors;
    373 	}
    374 	format_bytes(pbuf, sizeof(pbuf), wd->sc_capacity * DEV_BSIZE);
    375 	aprint_normal("%s: %s, %d cyl, %d head, %d sec, "
    376 	    "%d bytes/sect x %llu sectors\n",
    377 	    self->dv_xname, pbuf,
    378 	    (wd->sc_flags & WDF_LBA) ? (int)(wd->sc_capacity /
    379 		(wd->sc_params.atap_heads * wd->sc_params.atap_sectors)) :
    380 		wd->sc_params.atap_cylinders,
    381 	    wd->sc_params.atap_heads, wd->sc_params.atap_sectors,
    382 	    DEV_BSIZE, (unsigned long long)wd->sc_capacity);
    383 
    384 	ATADEBUG_PRINT(("%s: atap_dmatiming_mimi=%d, atap_dmatiming_recom=%d\n",
    385 	    self->dv_xname, wd->sc_params.atap_dmatiming_mimi,
    386 	    wd->sc_params.atap_dmatiming_recom), DEBUG_PROBE);
    387 	/*
    388 	 * Initialize and attach the disk structure.
    389 	 */
    390 	wd->sc_dk.dk_driver = &wddkdriver;
    391 	wd->sc_dk.dk_name = wd->sc_dev.dv_xname;
    392 	disk_attach(&wd->sc_dk);
    393 	wd->sc_wdc_bio.lp = wd->sc_dk.dk_label;
    394 	wd->sc_sdhook = shutdownhook_establish(wd_shutdown, wd);
    395 	if (wd->sc_sdhook == NULL)
    396 		aprint_error("%s: WARNING: unable to establish shutdown hook\n",
    397 		    wd->sc_dev.dv_xname);
    398 #if NRND > 0
    399 	rnd_attach_source(&wd->rnd_source, wd->sc_dev.dv_xname,
    400 			  RND_TYPE_DISK, 0);
    401 #endif
    402 
    403 	/* Discover wedges on this disk. */
    404 	dkwedge_discover(&wd->sc_dk);
    405 }
    406 
    407 int
    408 wdactivate(struct device *self, enum devact act)
    409 {
    410 	int rv = 0;
    411 
    412 	switch (act) {
    413 	case DVACT_ACTIVATE:
    414 		rv = EOPNOTSUPP;
    415 		break;
    416 
    417 	case DVACT_DEACTIVATE:
    418 		/*
    419 		 * Nothing to do; we key off the device's DVF_ACTIVATE.
    420 		 */
    421 		break;
    422 	}
    423 	return (rv);
    424 }
    425 
    426 int
    427 wddetach(struct device *self, int flags)
    428 {
    429 	struct wd_softc *sc = (struct wd_softc *)self;
    430 	int s, bmaj, cmaj, i, mn;
    431 
    432 	/* locate the major number */
    433 	bmaj = bdevsw_lookup_major(&wd_bdevsw);
    434 	cmaj = cdevsw_lookup_major(&wd_cdevsw);
    435 
    436 	/* Nuke the vnodes for any open instances. */
    437 	for (i = 0; i < MAXPARTITIONS; i++) {
    438 		mn = WDMINOR(self->dv_unit, i);
    439 		vdevgone(bmaj, mn, mn, VBLK);
    440 		vdevgone(cmaj, mn, mn, VCHR);
    441 	}
    442 
    443 	/* Delete all of our wedges. */
    444 	dkwedge_delall(&sc->sc_dk);
    445 
    446 	s = splbio();
    447 
    448 	/* Kill off any queued buffers. */
    449 	bufq_drain(&sc->sc_q);
    450 
    451 	bufq_free(&sc->sc_q);
    452 	sc->atabus->ata_killpending(sc->drvp);
    453 
    454 	splx(s);
    455 
    456 	/* Detach disk. */
    457 	disk_detach(&sc->sc_dk);
    458 
    459 #ifdef WD_SOFTBADSECT
    460 	/* Clean out the bad sector list */
    461 	while (!SLIST_EMPTY(&sc->sc_bslist)) {
    462 		void *head = SLIST_FIRST(&sc->sc_bslist);
    463 		SLIST_REMOVE_HEAD(&sc->sc_bslist, dbs_next);
    464 		free(head, M_TEMP);
    465 	}
    466 	sc->sc_bscount = 0;
    467 #endif
    468 
    469 	/* Get rid of the shutdown hook. */
    470 	if (sc->sc_sdhook != NULL)
    471 		shutdownhook_disestablish(sc->sc_sdhook);
    472 
    473 #if NRND > 0
    474 	/* Unhook the entropy source. */
    475 	rnd_detach_source(&sc->rnd_source);
    476 #endif
    477 
    478 	sc->drvp->drive_flags = 0; /* no drive any more here */
    479 
    480 	return (0);
    481 }
    482 
    483 /*
    484  * Read/write routine for a buffer.  Validates the arguments and schedules the
    485  * transfer.  Does not wait for the transfer to complete.
    486  */
    487 void
    488 wdstrategy(struct buf *bp)
    489 {
    490 	struct wd_softc *wd = device_lookup(&wd_cd, WDUNIT(bp->b_dev));
    491 	struct disklabel *lp = wd->sc_dk.dk_label;
    492 	daddr_t blkno;
    493 	int s;
    494 
    495 	ATADEBUG_PRINT(("wdstrategy (%s)\n", wd->sc_dev.dv_xname),
    496 	    DEBUG_XFERS);
    497 
    498 	/* Valid request?  */
    499 	if (bp->b_blkno < 0 ||
    500 	    (bp->b_bcount % lp->d_secsize) != 0 ||
    501 	    (bp->b_bcount / lp->d_secsize) >= (1 << NBBY)) {
    502 		bp->b_error = EINVAL;
    503 		goto bad;
    504 	}
    505 
    506 	/* If device invalidated (e.g. media change, door open), error. */
    507 	if ((wd->sc_flags & WDF_LOADED) == 0) {
    508 		bp->b_error = EIO;
    509 		goto bad;
    510 	}
    511 
    512 	/* If it's a null transfer, return immediately. */
    513 	if (bp->b_bcount == 0)
    514 		goto done;
    515 
    516 	/*
    517 	 * Do bounds checking, adjust transfer. if error, process.
    518 	 * If end of partition, just return.
    519 	 */
    520 	if (WDPART(bp->b_dev) == RAW_PART) {
    521 		if (bounds_check_with_mediasize(bp, DEV_BSIZE,
    522 		    wd->sc_capacity) <= 0)
    523 			goto done;
    524 	} else {
    525 		if (bounds_check_with_label(&wd->sc_dk, bp,
    526 		    (wd->sc_flags & (WDF_WLABEL|WDF_LABELLING)) != 0) <= 0)
    527 			goto done;
    528 	}
    529 
    530 	/*
    531 	 * Now convert the block number to absolute and put it in
    532 	 * terms of the device's logical block size.
    533 	 */
    534 	if (lp->d_secsize >= DEV_BSIZE)
    535 		blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
    536 	else
    537 		blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
    538 
    539 	if (WDPART(bp->b_dev) != RAW_PART)
    540 		blkno += lp->d_partitions[WDPART(bp->b_dev)].p_offset;
    541 
    542 	bp->b_rawblkno = blkno;
    543 
    544 #ifdef WD_SOFTBADSECT
    545 	/*
    546 	 * If the transfer about to be attempted contains only a block that
    547 	 * is known to be bad then return an error for the transfer without
    548 	 * even attempting to start a transfer up under the premis that we
    549 	 * will just end up doing more retries for a transfer that will end
    550 	 * up failing again.
    551 	 * XXX:SMP - mutex required to protect with DIOCBSFLUSH
    552 	 */
    553 	if (__predict_false(!SLIST_EMPTY(&wd->sc_bslist))) {
    554 		struct disk_badsectors *dbs;
    555 		daddr_t maxblk = blkno + (bp->b_bcount >> DEV_BSHIFT) - 1;
    556 
    557 		SLIST_FOREACH(dbs, &wd->sc_bslist, dbs_next)
    558 			if ((dbs->dbs_min <= blkno && blkno <= dbs->dbs_max) ||
    559 			    (dbs->dbs_min <= maxblk && maxblk <= dbs->dbs_max)){
    560 				bp->b_error = EIO;
    561 				goto bad;
    562 			}
    563 	}
    564 #endif
    565 
    566 	/* Queue transfer on drive, activate drive and controller if idle. */
    567 	s = splbio();
    568 	BUFQ_PUT(&wd->sc_q, bp);
    569 	wdstart(wd);
    570 	splx(s);
    571 	return;
    572 bad:
    573 	bp->b_flags |= B_ERROR;
    574 done:
    575 	/* Toss transfer; we're done early. */
    576 	bp->b_resid = bp->b_bcount;
    577 	biodone(bp);
    578 }
    579 
    580 /*
    581  * Queue a drive for I/O.
    582  */
    583 void
    584 wdstart(void *arg)
    585 {
    586 	struct wd_softc *wd = arg;
    587 	struct buf *bp = NULL;
    588 
    589 	ATADEBUG_PRINT(("wdstart %s\n", wd->sc_dev.dv_xname),
    590 	    DEBUG_XFERS);
    591 	while (wd->openings > 0) {
    592 
    593 		/* Is there a buf for us ? */
    594 		if ((bp = BUFQ_GET(&wd->sc_q)) == NULL)
    595 			return;
    596 
    597 		/*
    598 		 * Make the command. First lock the device
    599 		 */
    600 		wd->openings--;
    601 
    602 		wd->retries = 0;
    603 		__wdstart(wd, bp);
    604 	}
    605 }
    606 
    607 static void
    608 wd_split_mod15_write(struct buf *bp)
    609 {
    610 	struct buf *obp = bp->b_private;
    611 	struct wd_softc *sc = wd_cd.cd_devs[DISKUNIT(obp->b_dev)];
    612 
    613 	if (__predict_false(bp->b_flags & B_ERROR) != 0) {
    614 		/*
    615 		 * Propagate the error.  If this was the first half of
    616 		 * the original transfer, make sure to account for that
    617 		 * in the residual.
    618 		 */
    619 		if (bp->b_data == obp->b_data)
    620 			bp->b_resid += bp->b_bcount;
    621 		goto done;
    622 	}
    623 
    624 	/*
    625 	 * If this was the second half of the transfer, we're all done!
    626 	 */
    627 	if (bp->b_data != obp->b_data)
    628 		goto done;
    629 
    630 	/*
    631 	 * Advance the pointer to the second half and issue that command
    632 	 * using the same opening.
    633 	 */
    634 	bp->b_flags = obp->b_flags | B_CALL;
    635 	bp->b_data += bp->b_bcount;
    636 	bp->b_blkno += (bp->b_bcount / 512);
    637 	bp->b_rawblkno += (bp->b_bcount / 512);
    638 	__wdstart(sc, bp);
    639 	return;
    640 
    641  done:
    642 	obp->b_flags |= (bp->b_flags & (B_EINTR|B_ERROR));
    643 	obp->b_error = bp->b_error;
    644 	obp->b_resid = bp->b_resid;
    645 	pool_put(&bufpool, bp);
    646 	biodone(obp);
    647 	sc->openings++;
    648 	/* wddone() will call wdstart() */
    649 }
    650 
    651 void
    652 __wdstart(struct wd_softc *wd, struct buf *bp)
    653 {
    654 
    655 	/*
    656 	 * Deal with the "split mod15 write" quirk.  We just divide the
    657 	 * transfer in two, doing the first half and then then second half
    658 	 * with the same command opening.
    659 	 *
    660 	 * Note we MUST do this here, because we can't let insertion
    661 	 * into the bufq cause the transfers to be re-merged.
    662 	 */
    663 	if (__predict_false((wd->sc_quirks & WD_QUIRK_SPLIT_MOD15_WRITE) != 0 &&
    664 			    (bp->b_flags & B_READ) == 0 &&
    665 			    bp->b_bcount > 512 &&
    666 			    ((bp->b_bcount / 512) % 15) == 1)) {
    667 		struct buf *nbp;
    668 
    669 		/* already at splbio */
    670 		nbp = pool_get(&bufpool, PR_NOWAIT);
    671 		if (__predict_false(nbp == NULL)) {
    672 			/* No memory -- fail the iop. */
    673 			bp->b_error = ENOMEM;
    674 			bp->b_flags |= B_ERROR;
    675 			bp->b_resid = bp->b_bcount;
    676 			biodone(bp);
    677 			wd->openings++;
    678 			return;
    679 		}
    680 
    681 		BUF_INIT(nbp);
    682 		nbp->b_error = 0;
    683 		nbp->b_proc = bp->b_proc;
    684 		nbp->b_vp = NULLVP;
    685 		nbp->b_dev = bp->b_dev;
    686 
    687 		nbp->b_bcount = bp->b_bcount / 2;
    688 		nbp->b_bufsize = bp->b_bcount / 2;
    689 		nbp->b_data = bp->b_data;
    690 
    691 		nbp->b_blkno = bp->b_blkno;
    692 		nbp->b_rawblkno = bp->b_rawblkno;
    693 
    694 		nbp->b_flags = bp->b_flags | B_CALL;
    695 		nbp->b_iodone = wd_split_mod15_write;
    696 
    697 		/* Put ptr to orig buf in b_private and use new buf */
    698 		nbp->b_private = bp;
    699 
    700 		BIO_COPYPRIO(nbp, bp);
    701 
    702 		bp = nbp;
    703 	}
    704 
    705 	wd->sc_wdc_bio.blkno = bp->b_rawblkno;
    706 	wd->sc_wdc_bio.blkdone =0;
    707 	wd->sc_bp = bp;
    708 	/*
    709 	 * If we're retrying, retry in single-sector mode. This will give us
    710 	 * the sector number of the problem, and will eventually allow the
    711 	 * transfer to succeed.
    712 	 */
    713 	if (wd->retries >= WDIORETRIES_SINGLE)
    714 		wd->sc_wdc_bio.flags = ATA_SINGLE;
    715 	else
    716 		wd->sc_wdc_bio.flags = 0;
    717 	if (wd->sc_flags & WDF_LBA48 &&
    718 	    (wd->sc_wdc_bio.blkno > LBA48_THRESHOLD ||
    719 	    (wd->sc_quirks & WD_QUIRK_FORCE_LBA48) != 0))
    720 		wd->sc_wdc_bio.flags |= ATA_LBA48;
    721 	if (wd->sc_flags & WDF_LBA)
    722 		wd->sc_wdc_bio.flags |= ATA_LBA;
    723 	if (bp->b_flags & B_READ)
    724 		wd->sc_wdc_bio.flags |= ATA_READ;
    725 	wd->sc_wdc_bio.bcount = bp->b_bcount;
    726 	wd->sc_wdc_bio.databuf = bp->b_data;
    727 	/* Instrumentation. */
    728 	disk_busy(&wd->sc_dk);
    729 	switch (wd->atabus->ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
    730 	case ATACMD_TRY_AGAIN:
    731 		callout_reset(&wd->sc_restart_ch, hz, wdrestart, wd);
    732 		break;
    733 	case ATACMD_QUEUED:
    734 	case ATACMD_COMPLETE:
    735 		break;
    736 	default:
    737 		panic("__wdstart: bad return code from ata_bio()");
    738 	}
    739 }
    740 
    741 void
    742 wddone(void *v)
    743 {
    744 	struct wd_softc *wd = v;
    745 	struct buf *bp = wd->sc_bp;
    746 	const char *errmsg;
    747 	int do_perror = 0;
    748 	ATADEBUG_PRINT(("wddone %s\n", wd->sc_dev.dv_xname),
    749 	    DEBUG_XFERS);
    750 
    751 	if (bp == NULL)
    752 		return;
    753 	bp->b_resid = wd->sc_wdc_bio.bcount;
    754 	switch (wd->sc_wdc_bio.error) {
    755 	case ERR_DMA:
    756 		errmsg = "DMA error";
    757 		goto retry;
    758 	case ERR_DF:
    759 		errmsg = "device fault";
    760 		goto retry;
    761 	case TIMEOUT:
    762 		errmsg = "device timeout";
    763 		goto retry;
    764 	case ERR_RESET:
    765 		errmsg = "channel reset";
    766 		goto retry2;
    767 	case ERROR:
    768 		/* Don't care about media change bits */
    769 		if (wd->sc_wdc_bio.r_error != 0 &&
    770 		    (wd->sc_wdc_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0)
    771 			goto noerror;
    772 		errmsg = "error";
    773 		do_perror = 1;
    774 retry:		/* Just reset and retry. Can we do more ? */
    775 		(*wd->atabus->ata_reset_drive)(wd->drvp, AT_RST_NOCMD);
    776 retry2:
    777 		diskerr(bp, "wd", errmsg, LOG_PRINTF,
    778 		    wd->sc_wdc_bio.blkdone, wd->sc_dk.dk_label);
    779 		if (wd->retries < WDIORETRIES)
    780 			printf(", retrying\n");
    781 		if (do_perror)
    782 			wdperror(wd);
    783 		if (wd->retries < WDIORETRIES) {
    784 			wd->retries++;
    785 			callout_reset(&wd->sc_restart_ch, RECOVERYTIME,
    786 			    wdrestart, wd);
    787 			return;
    788 		}
    789 		printf("\n");
    790 
    791 #ifdef WD_SOFTBADSECT
    792 		/*
    793 		 * Not all errors indicate a failed block but those that do,
    794 		 * put the block on the bad-block list for the device.  Only
    795 		 * do this for reads because the drive should do it for writes,
    796 		 * itself, according to Manuel.
    797 		 */
    798 		if ((bp->b_flags & B_READ) &&
    799 		    ((wd->drvp->ata_vers >= 4 && wd->sc_wdc_bio.r_error & 64) ||
    800 	     	     (wd->drvp->ata_vers < 4 && wd->sc_wdc_bio.r_error & 192))) {
    801 			struct disk_badsectors *dbs;
    802 
    803 			dbs = malloc(sizeof *dbs, M_TEMP, M_WAITOK);
    804 			dbs->dbs_min = bp->b_rawblkno;
    805 			dbs->dbs_max = dbs->dbs_min + (bp->b_bcount >> DEV_BSHIFT) - 1;
    806 			microtime(&dbs->dbs_failedat);
    807 			SLIST_INSERT_HEAD(&wd->sc_bslist, dbs, dbs_next);
    808 			wd->sc_bscount++;
    809 		}
    810 #endif
    811 		bp->b_flags |= B_ERROR;
    812 		bp->b_error = EIO;
    813 		break;
    814 	case NOERROR:
    815 noerror:	if ((wd->sc_wdc_bio.flags & ATA_CORR) || wd->retries > 0)
    816 			printf("%s: soft error (corrected)\n",
    817 			    wd->sc_dev.dv_xname);
    818 		break;
    819 	case ERR_NODEV:
    820 		bp->b_flags |= B_ERROR;
    821 		bp->b_error = EIO;
    822 		break;
    823 	}
    824 	disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid),
    825 	    (bp->b_flags & B_READ));
    826 #if NRND > 0
    827 	rnd_add_uint32(&wd->rnd_source, bp->b_blkno);
    828 #endif
    829 	/* XXX Yuck, but we don't want to increment openings in this case */
    830 	if (__predict_false((bp->b_flags & B_CALL) != 0 &&
    831 			    bp->b_iodone == wd_split_mod15_write))
    832 		biodone(bp);
    833 	else {
    834 		biodone(bp);
    835 		wd->openings++;
    836 	}
    837 	wdstart(wd);
    838 }
    839 
    840 void
    841 wdrestart(void *v)
    842 {
    843 	struct wd_softc *wd = v;
    844 	struct buf *bp = wd->sc_bp;
    845 	int s;
    846 	ATADEBUG_PRINT(("wdrestart %s\n", wd->sc_dev.dv_xname),
    847 	    DEBUG_XFERS);
    848 
    849 	s = splbio();
    850 	__wdstart(v, bp);
    851 	splx(s);
    852 }
    853 
    854 int
    855 wdread(dev_t dev, struct uio *uio, int flags)
    856 {
    857 
    858 	ATADEBUG_PRINT(("wdread\n"), DEBUG_XFERS);
    859 	return (physio(wdstrategy, NULL, dev, B_READ, minphys, uio));
    860 }
    861 
    862 int
    863 wdwrite(dev_t dev, struct uio *uio, int flags)
    864 {
    865 
    866 	ATADEBUG_PRINT(("wdwrite\n"), DEBUG_XFERS);
    867 	return (physio(wdstrategy, NULL, dev, B_WRITE, minphys, uio));
    868 }
    869 
    870 int
    871 wdopen(dev_t dev, int flag, int fmt, struct proc *p)
    872 {
    873 	struct wd_softc *wd;
    874 	int part, error;
    875 
    876 	ATADEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS);
    877 	wd = device_lookup(&wd_cd, WDUNIT(dev));
    878 	if (wd == NULL)
    879 		return (ENXIO);
    880 
    881 	if ((wd->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    882 		return (ENODEV);
    883 
    884 	part = WDPART(dev);
    885 
    886 	if ((error = lockmgr(&wd->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL)) != 0)
    887 		return (error);
    888 
    889 	/*
    890 	 * If there are wedges, and this is not RAW_PART, then we
    891 	 * need to fail.
    892 	 */
    893 	if (wd->sc_dk.dk_nwedges != 0 && part != RAW_PART) {
    894 		error = EBUSY;
    895 		goto bad1;
    896 	}
    897 
    898 	/*
    899 	 * If this is the first open of this device, add a reference
    900 	 * to the adapter.
    901 	 */
    902 	if (wd->sc_dk.dk_openmask == 0 &&
    903 	    (error = wd->atabus->ata_addref(wd->drvp)) != 0)
    904 		goto bad1;
    905 
    906 	if (wd->sc_dk.dk_openmask != 0) {
    907 		/*
    908 		 * If any partition is open, but the disk has been invalidated,
    909 		 * disallow further opens.
    910 		 */
    911 		if ((wd->sc_flags & WDF_LOADED) == 0) {
    912 			error = EIO;
    913 			goto bad2;
    914 		}
    915 	} else {
    916 		if ((wd->sc_flags & WDF_LOADED) == 0) {
    917 			wd->sc_flags |= WDF_LOADED;
    918 
    919 			/* Load the physical device parameters. */
    920 			wd_get_params(wd, AT_WAIT, &wd->sc_params);
    921 
    922 			/* Load the partition info if not already loaded. */
    923 			wdgetdisklabel(wd);
    924 		}
    925 	}
    926 
    927 	/* Check that the partition exists. */
    928 	if (part != RAW_PART &&
    929 	    (part >= wd->sc_dk.dk_label->d_npartitions ||
    930 	     wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
    931 		error = ENXIO;
    932 		goto bad2;
    933 	}
    934 
    935 	/* Insure only one open at a time. */
    936 	switch (fmt) {
    937 	case S_IFCHR:
    938 		wd->sc_dk.dk_copenmask |= (1 << part);
    939 		break;
    940 	case S_IFBLK:
    941 		wd->sc_dk.dk_bopenmask |= (1 << part);
    942 		break;
    943 	}
    944 	wd->sc_dk.dk_openmask =
    945 	    wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
    946 
    947 	(void) lockmgr(&wd->sc_dk.dk_openlock, LK_RELEASE, NULL);
    948 	return 0;
    949 
    950  bad2:
    951 	if (wd->sc_dk.dk_openmask == 0)
    952 		wd->atabus->ata_delref(wd->drvp);
    953  bad1:
    954 	(void) lockmgr(&wd->sc_dk.dk_openlock, LK_RELEASE, NULL);
    955 	return error;
    956 }
    957 
    958 int
    959 wdclose(dev_t dev, int flag, int fmt, struct proc *p)
    960 {
    961 	struct wd_softc *wd = device_lookup(&wd_cd, WDUNIT(dev));
    962 	int part = WDPART(dev);
    963 	int error;
    964 
    965 	ATADEBUG_PRINT(("wdclose\n"), DEBUG_FUNCS);
    966 
    967 	if ((error = lockmgr(&wd->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL)) != 0)
    968 		return error;
    969 
    970 	switch (fmt) {
    971 	case S_IFCHR:
    972 		wd->sc_dk.dk_copenmask &= ~(1 << part);
    973 		break;
    974 	case S_IFBLK:
    975 		wd->sc_dk.dk_bopenmask &= ~(1 << part);
    976 		break;
    977 	}
    978 	wd->sc_dk.dk_openmask =
    979 	    wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
    980 
    981 	if (wd->sc_dk.dk_openmask == 0) {
    982 		wd_flushcache(wd, AT_WAIT);
    983 
    984 		if (! (wd->sc_flags & WDF_KLABEL))
    985 			wd->sc_flags &= ~WDF_LOADED;
    986 
    987 		wd->atabus->ata_delref(wd->drvp);
    988 	}
    989 
    990 	(void) lockmgr(&wd->sc_dk.dk_openlock, LK_RELEASE, NULL);
    991 	return 0;
    992 }
    993 
    994 void
    995 wdgetdefaultlabel(struct wd_softc *wd, struct disklabel *lp)
    996 {
    997 
    998 	ATADEBUG_PRINT(("wdgetdefaultlabel\n"), DEBUG_FUNCS);
    999 	memset(lp, 0, sizeof(struct disklabel));
   1000 
   1001 	lp->d_secsize = DEV_BSIZE;
   1002 	lp->d_ntracks = wd->sc_params.atap_heads;
   1003 	lp->d_nsectors = wd->sc_params.atap_sectors;
   1004 	lp->d_ncylinders = (wd->sc_flags & WDF_LBA) ? wd->sc_capacity /
   1005 		(wd->sc_params.atap_heads * wd->sc_params.atap_sectors) :
   1006 		wd->sc_params.atap_cylinders;
   1007 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
   1008 
   1009 	if (strcmp(wd->sc_params.atap_model, "ST506") == 0)
   1010 		lp->d_type = DTYPE_ST506;
   1011 	else
   1012 		lp->d_type = DTYPE_ESDI;
   1013 
   1014 	strncpy(lp->d_typename, wd->sc_params.atap_model, 16);
   1015 	strncpy(lp->d_packname, "fictitious", 16);
   1016 	if (wd->sc_capacity > UINT32_MAX)
   1017 		lp->d_secperunit = UINT32_MAX;
   1018 	else
   1019 		lp->d_secperunit = wd->sc_capacity;
   1020 	lp->d_rpm = 3600;
   1021 	lp->d_interleave = 1;
   1022 	lp->d_flags = 0;
   1023 
   1024 	lp->d_partitions[RAW_PART].p_offset = 0;
   1025 	lp->d_partitions[RAW_PART].p_size =
   1026 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
   1027 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
   1028 	lp->d_npartitions = RAW_PART + 1;
   1029 
   1030 	lp->d_magic = DISKMAGIC;
   1031 	lp->d_magic2 = DISKMAGIC;
   1032 	lp->d_checksum = dkcksum(lp);
   1033 }
   1034 
   1035 /*
   1036  * Fabricate a default disk label, and try to read the correct one.
   1037  */
   1038 void
   1039 wdgetdisklabel(struct wd_softc *wd)
   1040 {
   1041 	struct disklabel *lp = wd->sc_dk.dk_label;
   1042 	const char *errstring;
   1043 	int s;
   1044 
   1045 	ATADEBUG_PRINT(("wdgetdisklabel\n"), DEBUG_FUNCS);
   1046 
   1047 	memset(wd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
   1048 
   1049 	wdgetdefaultlabel(wd, lp);
   1050 
   1051 	wd->sc_badsect[0] = -1;
   1052 
   1053 	if (wd->drvp->state > RESET) {
   1054 		s = splbio();
   1055 		wd->drvp->drive_flags |= DRIVE_RESET;
   1056 		splx(s);
   1057 	}
   1058 	errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
   1059 	    wdstrategy, lp, wd->sc_dk.dk_cpulabel);
   1060 	if (errstring) {
   1061 		/*
   1062 		 * This probably happened because the drive's default
   1063 		 * geometry doesn't match the DOS geometry.  We
   1064 		 * assume the DOS geometry is now in the label and try
   1065 		 * again.  XXX This is a kluge.
   1066 		 */
   1067 		if (wd->drvp->state > RESET) {
   1068 			s = splbio();
   1069 			wd->drvp->drive_flags |= DRIVE_RESET;
   1070 			splx(s);
   1071 		}
   1072 		errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit,
   1073 		    RAW_PART), wdstrategy, lp, wd->sc_dk.dk_cpulabel);
   1074 	}
   1075 	if (errstring) {
   1076 		printf("%s: %s\n", wd->sc_dev.dv_xname, errstring);
   1077 		return;
   1078 	}
   1079 
   1080 	if (wd->drvp->state > RESET) {
   1081 		s = splbio();
   1082 		wd->drvp->drive_flags |= DRIVE_RESET;
   1083 		splx(s);
   1084 	}
   1085 #ifdef HAS_BAD144_HANDLING
   1086 	if ((lp->d_flags & D_BADSECT) != 0)
   1087 		bad144intern(wd);
   1088 #endif
   1089 }
   1090 
   1091 void
   1092 wdperror(const struct wd_softc *wd)
   1093 {
   1094 	static const char *const errstr0_3[] = {"address mark not found",
   1095 	    "track 0 not found", "aborted command", "media change requested",
   1096 	    "id not found", "media changed", "uncorrectable data error",
   1097 	    "bad block detected"};
   1098 	static const char *const errstr4_5[] = {
   1099 	    "obsolete (address mark not found)",
   1100 	    "no media/write protected", "aborted command",
   1101 	    "media change requested", "id not found", "media changed",
   1102 	    "uncorrectable data error", "interface CRC error"};
   1103 	const char *const *errstr;
   1104 	int i;
   1105 	const char *sep = "";
   1106 
   1107 	const char *devname = wd->sc_dev.dv_xname;
   1108 	struct ata_drive_datas *drvp = wd->drvp;
   1109 	int errno = wd->sc_wdc_bio.r_error;
   1110 
   1111 	if (drvp->ata_vers >= 4)
   1112 		errstr = errstr4_5;
   1113 	else
   1114 		errstr = errstr0_3;
   1115 
   1116 	printf("%s: (", devname);
   1117 
   1118 	if (errno == 0)
   1119 		printf("error not notified");
   1120 
   1121 	for (i = 0; i < 8; i++) {
   1122 		if (errno & (1 << i)) {
   1123 			printf("%s%s", sep, errstr[i]);
   1124 			sep = ", ";
   1125 		}
   1126 	}
   1127 	printf(")\n");
   1128 }
   1129 
   1130 int
   1131 wdioctl(dev_t dev, u_long xfer, caddr_t addr, int flag, struct proc *p)
   1132 {
   1133 	struct wd_softc *wd = device_lookup(&wd_cd, WDUNIT(dev));
   1134 	int error = 0, s;
   1135 #ifdef __HAVE_OLD_DISKLABEL
   1136 	struct disklabel *newlabel = NULL;
   1137 #endif
   1138 
   1139 	ATADEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS);
   1140 
   1141 	if ((wd->sc_flags & WDF_LOADED) == 0)
   1142 		return EIO;
   1143 
   1144 	switch (xfer) {
   1145 #ifdef HAS_BAD144_HANDLING
   1146 	case DIOCSBAD:
   1147 		if ((flag & FWRITE) == 0)
   1148 			return EBADF;
   1149 		wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
   1150 		wd->sc_dk.dk_label->d_flags |= D_BADSECT;
   1151 		bad144intern(wd);
   1152 		return 0;
   1153 #endif
   1154 #ifdef WD_SOFTBADSECT
   1155 	case DIOCBSLIST :
   1156 	{
   1157 		u_int32_t count, missing, skip;
   1158 		struct disk_badsecinfo dbsi;
   1159 		struct disk_badsectors *dbs;
   1160 		size_t available;
   1161 		caddr_t laddr;
   1162 
   1163 		dbsi = *(struct disk_badsecinfo *)addr;
   1164 		missing = wd->sc_bscount;
   1165 		count = 0;
   1166 		available = dbsi.dbsi_bufsize;
   1167 		skip = dbsi.dbsi_skip;
   1168 		laddr = dbsi.dbsi_buffer;
   1169 
   1170 		/*
   1171 		 * We start this loop with the expectation that all of the
   1172 		 * entries will be missed and decrement this counter each
   1173 		 * time we either skip over one (already copied out) or
   1174 		 * we actually copy it back to user space.  The structs
   1175 		 * holding the bad sector information are copied directly
   1176 		 * back to user space whilst the summary is returned via
   1177 		 * the struct passed in via the ioctl.
   1178 		 */
   1179 		SLIST_FOREACH(dbs, &wd->sc_bslist, dbs_next) {
   1180 			if (skip > 0) {
   1181 				missing--;
   1182 				skip--;
   1183 				continue;
   1184 			}
   1185 			if (available < sizeof(*dbs))
   1186 				break;
   1187 			available -= sizeof(*dbs);
   1188 			copyout(dbs, laddr, sizeof(*dbs));
   1189 			laddr += sizeof(*dbs);
   1190 			missing--;
   1191 			count++;
   1192 		}
   1193 		dbsi.dbsi_left = missing;
   1194 		dbsi.dbsi_copied = count;
   1195 		*(struct disk_badsecinfo *)addr = dbsi;
   1196 		return 0;
   1197 	}
   1198 
   1199 	case DIOCBSFLUSH :
   1200 		/* Clean out the bad sector list */
   1201 		while (!SLIST_EMPTY(&wd->sc_bslist)) {
   1202 			void *head = SLIST_FIRST(&wd->sc_bslist);
   1203 			SLIST_REMOVE_HEAD(&wd->sc_bslist, dbs_next);
   1204 			free(head, M_TEMP);
   1205 		}
   1206 		wd->sc_bscount = 0;
   1207 		return 0;
   1208 #endif
   1209 	case DIOCGDINFO:
   1210 		*(struct disklabel *)addr = *(wd->sc_dk.dk_label);
   1211 		return 0;
   1212 #ifdef __HAVE_OLD_DISKLABEL
   1213 	case ODIOCGDINFO:
   1214 		newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
   1215 		if (newlabel == NULL)
   1216 			return EIO;
   1217 		*newlabel = *(wd->sc_dk.dk_label);
   1218 		if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
   1219 			memcpy(addr, newlabel, sizeof (struct olddisklabel));
   1220 		else
   1221 			error = ENOTTY;
   1222 		free(newlabel, M_TEMP);
   1223 		return error;
   1224 #endif
   1225 
   1226 	case DIOCGPART:
   1227 		((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
   1228 		((struct partinfo *)addr)->part =
   1229 		    &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
   1230 		return 0;
   1231 
   1232 	case DIOCWDINFO:
   1233 	case DIOCSDINFO:
   1234 #ifdef __HAVE_OLD_DISKLABEL
   1235 	case ODIOCWDINFO:
   1236 	case ODIOCSDINFO:
   1237 #endif
   1238 	{
   1239 		struct disklabel *lp;
   1240 
   1241 		if ((flag & FWRITE) == 0)
   1242 			return EBADF;
   1243 
   1244 #ifdef __HAVE_OLD_DISKLABEL
   1245 		if (xfer == ODIOCSDINFO || xfer == ODIOCWDINFO) {
   1246 			newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
   1247 			if (newlabel == NULL)
   1248 				return EIO;
   1249 			memset(newlabel, 0, sizeof newlabel);
   1250 			memcpy(newlabel, addr, sizeof (struct olddisklabel));
   1251 			lp = newlabel;
   1252 		} else
   1253 #endif
   1254 		lp = (struct disklabel *)addr;
   1255 
   1256 		if ((error = lockmgr(&wd->sc_dk.dk_openlock, LK_EXCLUSIVE,
   1257 				     NULL)) != 0)
   1258 			goto bad;
   1259 		wd->sc_flags |= WDF_LABELLING;
   1260 
   1261 		error = setdisklabel(wd->sc_dk.dk_label,
   1262 		    lp, /*wd->sc_dk.dk_openmask : */0,
   1263 		    wd->sc_dk.dk_cpulabel);
   1264 		if (error == 0) {
   1265 			if (wd->drvp->state > RESET) {
   1266 				s = splbio();
   1267 				wd->drvp->drive_flags |= DRIVE_RESET;
   1268 				splx(s);
   1269 			}
   1270 			if (xfer == DIOCWDINFO
   1271 #ifdef __HAVE_OLD_DISKLABEL
   1272 			    || xfer == ODIOCWDINFO
   1273 #endif
   1274 			    )
   1275 				error = writedisklabel(WDLABELDEV(dev),
   1276 				    wdstrategy, wd->sc_dk.dk_label,
   1277 				    wd->sc_dk.dk_cpulabel);
   1278 		}
   1279 
   1280 		wd->sc_flags &= ~WDF_LABELLING;
   1281 		(void) lockmgr(&wd->sc_dk.dk_openlock, LK_RELEASE, NULL);
   1282 bad:
   1283 #ifdef __HAVE_OLD_DISKLABEL
   1284 		if (newlabel != NULL)
   1285 			free(newlabel, M_TEMP);
   1286 #endif
   1287 		return error;
   1288 	}
   1289 
   1290 	case DIOCKLABEL:
   1291 		if (*(int *)addr)
   1292 			wd->sc_flags |= WDF_KLABEL;
   1293 		else
   1294 			wd->sc_flags &= ~WDF_KLABEL;
   1295 		return 0;
   1296 
   1297 	case DIOCWLABEL:
   1298 		if ((flag & FWRITE) == 0)
   1299 			return EBADF;
   1300 		if (*(int *)addr)
   1301 			wd->sc_flags |= WDF_WLABEL;
   1302 		else
   1303 			wd->sc_flags &= ~WDF_WLABEL;
   1304 		return 0;
   1305 
   1306 	case DIOCGDEFLABEL:
   1307 		wdgetdefaultlabel(wd, (struct disklabel *)addr);
   1308 		return 0;
   1309 #ifdef __HAVE_OLD_DISKLABEL
   1310 	case ODIOCGDEFLABEL:
   1311 		newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
   1312 		if (newlabel == NULL)
   1313 			return EIO;
   1314 		wdgetdefaultlabel(wd, newlabel);
   1315 		if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
   1316 			memcpy(addr, &newlabel, sizeof (struct olddisklabel));
   1317 		else
   1318 			error = ENOTTY;
   1319 		free(newlabel, M_TEMP);
   1320 		return error;
   1321 #endif
   1322 
   1323 #ifdef notyet
   1324 	case DIOCWFORMAT:
   1325 		if ((flag & FWRITE) == 0)
   1326 			return EBADF;
   1327 		{
   1328 		register struct format_op *fop;
   1329 		struct iovec aiov;
   1330 		struct uio auio;
   1331 
   1332 		fop = (struct format_op *)addr;
   1333 		aiov.iov_base = fop->df_buf;
   1334 		aiov.iov_len = fop->df_count;
   1335 		auio.uio_iov = &aiov;
   1336 		auio.uio_iovcnt = 1;
   1337 		auio.uio_resid = fop->df_count;
   1338 		auio.uio_segflg = 0;
   1339 		auio.uio_offset =
   1340 			fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
   1341 		auio.uio_procp = p;
   1342 		error = physio(wdformat, NULL, dev, B_WRITE, minphys,
   1343 		    &auio);
   1344 		fop->df_count -= auio.uio_resid;
   1345 		fop->df_reg[0] = wdc->sc_status;
   1346 		fop->df_reg[1] = wdc->sc_error;
   1347 		return error;
   1348 		}
   1349 #endif
   1350 	case DIOCGCACHE:
   1351 		return wd_getcache(wd, (int *)addr);
   1352 
   1353 	case DIOCSCACHE:
   1354 		return wd_setcache(wd, *(int *)addr);
   1355 
   1356 	case DIOCCACHESYNC:
   1357 		return wd_flushcache(wd, AT_WAIT);
   1358 
   1359 	case ATAIOCCOMMAND:
   1360 		/*
   1361 		 * Make sure this command is (relatively) safe first
   1362 		 */
   1363 		if ((((atareq_t *) addr)->flags & ATACMD_READ) == 0 &&
   1364 		    (flag & FWRITE) == 0)
   1365 			return (EBADF);
   1366 		{
   1367 		struct wd_ioctl *wi;
   1368 		atareq_t *atareq = (atareq_t *) addr;
   1369 		int error1;
   1370 
   1371 		wi = wi_get();
   1372 		wi->wi_softc = wd;
   1373 		wi->wi_atareq = *atareq;
   1374 
   1375 		if (atareq->datalen && atareq->flags &
   1376 		    (ATACMD_READ | ATACMD_WRITE)) {
   1377 			wi->wi_iov.iov_base = atareq->databuf;
   1378 			wi->wi_iov.iov_len = atareq->datalen;
   1379 			wi->wi_uio.uio_iov = &wi->wi_iov;
   1380 			wi->wi_uio.uio_iovcnt = 1;
   1381 			wi->wi_uio.uio_resid = atareq->datalen;
   1382 			wi->wi_uio.uio_offset = 0;
   1383 			wi->wi_uio.uio_segflg = UIO_USERSPACE;
   1384 			wi->wi_uio.uio_rw =
   1385 			    (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE;
   1386 			wi->wi_uio.uio_procp = p;
   1387 			error1 = physio(wdioctlstrategy, &wi->wi_bp, dev,
   1388 			    (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE,
   1389 			    minphys, &wi->wi_uio);
   1390 		} else {
   1391 			/* No need to call physio if we don't have any
   1392 			   user data */
   1393 			wi->wi_bp.b_flags = 0;
   1394 			wi->wi_bp.b_data = 0;
   1395 			wi->wi_bp.b_bcount = 0;
   1396 			wi->wi_bp.b_dev = 0;
   1397 			wi->wi_bp.b_proc = p;
   1398 			wdioctlstrategy(&wi->wi_bp);
   1399 			error1 = wi->wi_bp.b_error;
   1400 		}
   1401 		*atareq = wi->wi_atareq;
   1402 		wi_free(wi);
   1403 		return(error1);
   1404 		}
   1405 
   1406 	case DIOCAWEDGE:
   1407 	    {
   1408 	    	struct dkwedge_info *dkw = (void *) addr;
   1409 
   1410 		if ((flag & FWRITE) == 0)
   1411 			return (EBADF);
   1412 
   1413 		/* If the ioctl happens here, the parent is us. */
   1414 		strcpy(dkw->dkw_parent, wd->sc_dev.dv_xname);
   1415 		return (dkwedge_add(dkw));
   1416 	    }
   1417 
   1418 	case DIOCDWEDGE:
   1419 	    {
   1420 	    	struct dkwedge_info *dkw = (void *) addr;
   1421 
   1422 		if ((flag & FWRITE) == 0)
   1423 			return (EBADF);
   1424 
   1425 		/* If the ioctl happens here, the parent is us. */
   1426 		strcpy(dkw->dkw_parent, wd->sc_dev.dv_xname);
   1427 		return (dkwedge_del(dkw));
   1428 	    }
   1429 
   1430 	case DIOCLWEDGES:
   1431 	    {
   1432 	    	struct dkwedge_list *dkwl = (void *) addr;
   1433 
   1434 		return (dkwedge_list(&wd->sc_dk, dkwl, p));
   1435 	    }
   1436 
   1437 	default:
   1438 		return ENOTTY;
   1439 	}
   1440 
   1441 #ifdef DIAGNOSTIC
   1442 	panic("wdioctl: impossible");
   1443 #endif
   1444 }
   1445 
   1446 #ifdef B_FORMAT
   1447 int
   1448 wdformat(struct buf *bp)
   1449 {
   1450 
   1451 	bp->b_flags |= B_FORMAT;
   1452 	return wdstrategy(bp);
   1453 }
   1454 #endif
   1455 
   1456 int
   1457 wdsize(dev_t dev)
   1458 {
   1459 	struct wd_softc *wd;
   1460 	int part, omask;
   1461 	int size;
   1462 
   1463 	ATADEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS);
   1464 
   1465 	wd = device_lookup(&wd_cd, WDUNIT(dev));
   1466 	if (wd == NULL)
   1467 		return (-1);
   1468 
   1469 	part = WDPART(dev);
   1470 	omask = wd->sc_dk.dk_openmask & (1 << part);
   1471 
   1472 	if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0)
   1473 		return (-1);
   1474 	if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
   1475 		size = -1;
   1476 	else
   1477 		size = wd->sc_dk.dk_label->d_partitions[part].p_size *
   1478 		    (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
   1479 	if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0)
   1480 		return (-1);
   1481 	return (size);
   1482 }
   1483 
   1484 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
   1485 static int wddoingadump = 0;
   1486 static int wddumprecalibrated = 0;
   1487 static int wddumpmulti = 1;
   1488 
   1489 /*
   1490  * Dump core after a system crash.
   1491  */
   1492 int
   1493 wddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
   1494 {
   1495 	struct wd_softc *wd;	/* disk unit to do the I/O */
   1496 	struct disklabel *lp;   /* disk's disklabel */
   1497 	int part, err;
   1498 	int nblks;	/* total number of sectors left to write */
   1499 
   1500 	/* Check if recursive dump; if so, punt. */
   1501 	if (wddoingadump)
   1502 		return EFAULT;
   1503 	wddoingadump = 1;
   1504 
   1505 	wd = device_lookup(&wd_cd, WDUNIT(dev));
   1506 	if (wd == NULL)
   1507 		return (ENXIO);
   1508 
   1509 	part = WDPART(dev);
   1510 
   1511 	/* Convert to disk sectors.  Request must be a multiple of size. */
   1512 	lp = wd->sc_dk.dk_label;
   1513 	if ((size % lp->d_secsize) != 0)
   1514 		return EFAULT;
   1515 	nblks = size / lp->d_secsize;
   1516 	blkno = blkno / (lp->d_secsize / DEV_BSIZE);
   1517 
   1518 	/* Check transfer bounds against partition size. */
   1519 	if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
   1520 		return EINVAL;
   1521 
   1522 	/* Offset block number to start of partition. */
   1523 	blkno += lp->d_partitions[part].p_offset;
   1524 
   1525 	/* Recalibrate, if first dump transfer. */
   1526 	if (wddumprecalibrated == 0) {
   1527 		wddumpmulti = wd->sc_multi;
   1528 		wddumprecalibrated = 1;
   1529 		(*wd->atabus->ata_reset_drive)(wd->drvp,
   1530 					       AT_POLL | AT_RST_EMERG);
   1531 		wd->drvp->state = RESET;
   1532 	}
   1533 
   1534 	while (nblks > 0) {
   1535 		wd->sc_bp = NULL;
   1536 		wd->sc_wdc_bio.blkno = blkno;
   1537 		wd->sc_wdc_bio.flags = ATA_POLL;
   1538 		if (wd->sc_flags & WDF_LBA48 &&
   1539 		    (blkno > LBA48_THRESHOLD ||
   1540 	    	    (wd->sc_quirks & WD_QUIRK_FORCE_LBA48) != 0))
   1541 			wd->sc_wdc_bio.flags |= ATA_LBA48;
   1542 		if (wd->sc_flags & WDF_LBA)
   1543 			wd->sc_wdc_bio.flags |= ATA_LBA;
   1544 		wd->sc_wdc_bio.bcount =
   1545 			min(nblks, wddumpmulti) * lp->d_secsize;
   1546 		wd->sc_wdc_bio.databuf = va;
   1547 #ifndef WD_DUMP_NOT_TRUSTED
   1548 		switch (wd->atabus->ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
   1549 		case ATACMD_TRY_AGAIN:
   1550 			panic("wddump: try again");
   1551 			break;
   1552 		case ATACMD_QUEUED:
   1553 			panic("wddump: polled command has been queued");
   1554 			break;
   1555 		case ATACMD_COMPLETE:
   1556 			break;
   1557 		}
   1558 		switch(wd->sc_wdc_bio.error) {
   1559 		case TIMEOUT:
   1560 			printf("wddump: device timed out");
   1561 			err = EIO;
   1562 			break;
   1563 		case ERR_DF:
   1564 			printf("wddump: drive fault");
   1565 			err = EIO;
   1566 			break;
   1567 		case ERR_DMA:
   1568 			printf("wddump: DMA error");
   1569 			err = EIO;
   1570 			break;
   1571 		case ERROR:
   1572 			printf("wddump: ");
   1573 			wdperror(wd);
   1574 			err = EIO;
   1575 			break;
   1576 		case NOERROR:
   1577 			err = 0;
   1578 			break;
   1579 		default:
   1580 			panic("wddump: unknown error type");
   1581 		}
   1582 		if (err != 0) {
   1583 			printf("\n");
   1584 			return err;
   1585 		}
   1586 #else	/* WD_DUMP_NOT_TRUSTED */
   1587 		/* Let's just talk about this first... */
   1588 		printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
   1589 		    unit, va, cylin, head, sector);
   1590 		delay(500 * 1000);	/* half a second */
   1591 #endif
   1592 
   1593 		/* update block count */
   1594 		nblks -= min(nblks, wddumpmulti);
   1595 		blkno += min(nblks, wddumpmulti);
   1596 		va += min(nblks, wddumpmulti) * lp->d_secsize;
   1597 	}
   1598 
   1599 	wddoingadump = 0;
   1600 	return 0;
   1601 }
   1602 
   1603 #ifdef HAS_BAD144_HANDLING
   1604 /*
   1605  * Internalize the bad sector table.
   1606  */
   1607 void
   1608 bad144intern(struct wd_softc *wd)
   1609 {
   1610 	struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
   1611 	struct disklabel *lp = wd->sc_dk.dk_label;
   1612 	int i = 0;
   1613 
   1614 	ATADEBUG_PRINT(("bad144intern\n"), DEBUG_XFERS);
   1615 
   1616 	for (; i < NBT_BAD; i++) {
   1617 		if (bt->bt_bad[i].bt_cyl == 0xffff)
   1618 			break;
   1619 		wd->sc_badsect[i] =
   1620 		    bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
   1621 		    (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
   1622 		    (bt->bt_bad[i].bt_trksec & 0xff);
   1623 	}
   1624 	for (; i < NBT_BAD+1; i++)
   1625 		wd->sc_badsect[i] = -1;
   1626 }
   1627 #endif
   1628 
   1629 int
   1630 wd_get_params(struct wd_softc *wd, u_int8_t flags, struct ataparams *params)
   1631 {
   1632 	switch (wd->atabus->ata_get_params(wd->drvp, flags, params)) {
   1633 	case CMD_AGAIN:
   1634 		return 1;
   1635 	case CMD_ERR:
   1636 		/*
   1637 		 * We `know' there's a drive here; just assume it's old.
   1638 		 * This geometry is only used to read the MBR and print a
   1639 		 * (false) attach message.
   1640 		 */
   1641 		strncpy(params->atap_model, "ST506",
   1642 		    sizeof params->atap_model);
   1643 		params->atap_config = ATA_CFG_FIXED;
   1644 		params->atap_cylinders = 1024;
   1645 		params->atap_heads = 8;
   1646 		params->atap_sectors = 17;
   1647 		params->atap_multi = 1;
   1648 		params->atap_capabilities1 = params->atap_capabilities2 = 0;
   1649 		wd->drvp->ata_vers = -1; /* Mark it as pre-ATA */
   1650 		return 0;
   1651 	case CMD_OK:
   1652 		return 0;
   1653 	default:
   1654 		panic("wd_get_params: bad return code from ata_get_params");
   1655 		/* NOTREACHED */
   1656 	}
   1657 }
   1658 
   1659 int
   1660 wd_getcache(struct wd_softc *wd, int *bitsp)
   1661 {
   1662 	struct ataparams params;
   1663 
   1664 	if (wd_get_params(wd, AT_WAIT, &params) != 0)
   1665 		return EIO;
   1666 	if (params.atap_cmd_set1 == 0x0000 ||
   1667 	    params.atap_cmd_set1 == 0xffff ||
   1668 	    (params.atap_cmd_set1 & WDC_CMD1_CACHE) == 0) {
   1669 		*bitsp = 0;
   1670 		return 0;
   1671 	}
   1672 	*bitsp = DKCACHE_WCHANGE | DKCACHE_READ;
   1673 	if (params.atap_cmd1_en & WDC_CMD1_CACHE)
   1674 		*bitsp |= DKCACHE_WRITE;
   1675 
   1676 	return 0;
   1677 }
   1678 
   1679 const char at_errbits[] = "\20\10ERROR\11TIMEOU\12DF";
   1680 
   1681 int
   1682 wd_setcache(struct wd_softc *wd, int bits)
   1683 {
   1684 	struct ataparams params;
   1685 	struct ata_command ata_c;
   1686 
   1687 	if (wd_get_params(wd, AT_WAIT, &params) != 0)
   1688 		return EIO;
   1689 
   1690 	if (params.atap_cmd_set1 == 0x0000 ||
   1691 	    params.atap_cmd_set1 == 0xffff ||
   1692 	    (params.atap_cmd_set1 & WDC_CMD1_CACHE) == 0)
   1693 		return EOPNOTSUPP;
   1694 
   1695 	if ((bits & DKCACHE_READ) == 0 ||
   1696 	    (bits & DKCACHE_SAVE) != 0)
   1697 		return EOPNOTSUPP;
   1698 
   1699 	memset(&ata_c, 0, sizeof(struct ata_command));
   1700 	ata_c.r_command = SET_FEATURES;
   1701 	ata_c.r_st_bmask = 0;
   1702 	ata_c.r_st_pmask = 0;
   1703 	ata_c.timeout = 30000; /* 30s timeout */
   1704 	ata_c.flags = AT_WAIT;
   1705 	if (bits & DKCACHE_WRITE)
   1706 		ata_c.r_features = WDSF_WRITE_CACHE_EN;
   1707 	else
   1708 		ata_c.r_features = WDSF_WRITE_CACHE_DS;
   1709 	if (wd->atabus->ata_exec_command(wd->drvp, &ata_c) != ATACMD_COMPLETE) {
   1710 		printf("%s: wd_setcache command not complete\n",
   1711 		    wd->sc_dev.dv_xname);
   1712 		return EIO;
   1713 	}
   1714 	if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
   1715 		char sbuf[sizeof(at_errbits) + 64];
   1716 		bitmask_snprintf(ata_c.flags, at_errbits, sbuf, sizeof(sbuf));
   1717 		printf("%s: wd_setcache: status=%s\n", wd->sc_dev.dv_xname,
   1718 		    sbuf);
   1719 		return EIO;
   1720 	}
   1721 	return 0;
   1722 }
   1723 
   1724 int
   1725 wd_standby(struct wd_softc *wd, int flags)
   1726 {
   1727 	struct ata_command ata_c;
   1728 
   1729 	memset(&ata_c, 0, sizeof(struct ata_command));
   1730 	ata_c.r_command = WDCC_STANDBY_IMMED;
   1731 	ata_c.r_st_bmask = WDCS_DRDY;
   1732 	ata_c.r_st_pmask = WDCS_DRDY;
   1733 	ata_c.flags = flags;
   1734 	ata_c.timeout = 30000; /* 30s timeout */
   1735 	if (wd->atabus->ata_exec_command(wd->drvp, &ata_c) != ATACMD_COMPLETE) {
   1736 		printf("%s: standby immediate command didn't complete\n",
   1737 		    wd->sc_dev.dv_xname);
   1738 		return EIO;
   1739 	}
   1740 	if (ata_c.flags & AT_ERROR) {
   1741 		if (ata_c.r_error == WDCE_ABRT) /* command not supported */
   1742 			return ENODEV;
   1743 	}
   1744 	if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
   1745 		char sbuf[sizeof(at_errbits) + 64];
   1746 		bitmask_snprintf(ata_c.flags, at_errbits, sbuf, sizeof(sbuf));
   1747 		printf("%s: wd_standby: status=%s\n", wd->sc_dev.dv_xname,
   1748 		    sbuf);
   1749 		return EIO;
   1750 	}
   1751 	return 0;
   1752 }
   1753 
   1754 int
   1755 wd_flushcache(struct wd_softc *wd, int flags)
   1756 {
   1757 	struct ata_command ata_c;
   1758 
   1759 	/*
   1760 	 * WDCC_FLUSHCACHE is here since ATA-4, but some drives report
   1761 	 * only ATA-2 and still support it.
   1762 	 */
   1763 	if (wd->drvp->ata_vers < 4 &&
   1764 	    ((wd->sc_params.atap_cmd_set2 & WDC_CMD2_FC) == 0 ||
   1765 	    wd->sc_params.atap_cmd_set2 == 0xffff))
   1766 		return ENODEV;
   1767 	memset(&ata_c, 0, sizeof(struct ata_command));
   1768 	if ((wd->sc_params.atap_cmd2_en & ATA_CMD2_LBA48) != 0 &&
   1769 	    (wd->sc_params.atap_cmd2_en & ATA_CMD2_FCE) != 0)
   1770 		ata_c.r_command = WDCC_FLUSHCACHE_EXT;
   1771 	else
   1772 		ata_c.r_command = WDCC_FLUSHCACHE;
   1773 	ata_c.r_st_bmask = WDCS_DRDY;
   1774 	ata_c.r_st_pmask = WDCS_DRDY;
   1775 	ata_c.flags = flags;
   1776 	ata_c.timeout = 30000; /* 30s timeout */
   1777 	if (wd->atabus->ata_exec_command(wd->drvp, &ata_c) != ATACMD_COMPLETE) {
   1778 		printf("%s: flush cache command didn't complete\n",
   1779 		    wd->sc_dev.dv_xname);
   1780 		return EIO;
   1781 	}
   1782 	if (ata_c.flags & AT_ERROR) {
   1783 		if (ata_c.r_error == WDCE_ABRT) /* command not supported */
   1784 			return ENODEV;
   1785 	}
   1786 	if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
   1787 		char sbuf[sizeof(at_errbits) + 64];
   1788 		bitmask_snprintf(ata_c.flags, at_errbits, sbuf, sizeof(sbuf));
   1789 		printf("%s: wd_flushcache: status=%s\n", wd->sc_dev.dv_xname,
   1790 		    sbuf);
   1791 		return EIO;
   1792 	}
   1793 	return 0;
   1794 }
   1795 
   1796 void
   1797 wd_shutdown(void *arg)
   1798 {
   1799 	struct wd_softc *wd = arg;
   1800 	wd_flushcache(wd, AT_POLL);
   1801 }
   1802 
   1803 /*
   1804  * Allocate space for a ioctl queue structure.  Mostly taken from
   1805  * scsipi_ioctl.c
   1806  */
   1807 struct wd_ioctl *
   1808 wi_get(void)
   1809 {
   1810 	struct wd_ioctl *wi;
   1811 	int s;
   1812 
   1813 	wi = malloc(sizeof(struct wd_ioctl), M_TEMP, M_WAITOK|M_ZERO);
   1814 	simple_lock_init(&wi->wi_bp.b_interlock);
   1815 	s = splbio();
   1816 	LIST_INSERT_HEAD(&wi_head, wi, wi_list);
   1817 	splx(s);
   1818 	return (wi);
   1819 }
   1820 
   1821 /*
   1822  * Free an ioctl structure and remove it from our list
   1823  */
   1824 
   1825 void
   1826 wi_free(struct wd_ioctl *wi)
   1827 {
   1828 	int s;
   1829 
   1830 	s = splbio();
   1831 	LIST_REMOVE(wi, wi_list);
   1832 	splx(s);
   1833 	free(wi, M_TEMP);
   1834 }
   1835 
   1836 /*
   1837  * Find a wd_ioctl structure based on the struct buf.
   1838  */
   1839 
   1840 struct wd_ioctl *
   1841 wi_find(struct buf *bp)
   1842 {
   1843 	struct wd_ioctl *wi;
   1844 	int s;
   1845 
   1846 	s = splbio();
   1847 	for (wi = wi_head.lh_first; wi != 0; wi = wi->wi_list.le_next)
   1848 		if (bp == &wi->wi_bp)
   1849 			break;
   1850 	splx(s);
   1851 	return (wi);
   1852 }
   1853 
   1854 /*
   1855  * Ioctl pseudo strategy routine
   1856  *
   1857  * This is mostly stolen from scsipi_ioctl.c:scsistrategy().  What
   1858  * happens here is:
   1859  *
   1860  * - wdioctl() queues a wd_ioctl structure.
   1861  *
   1862  * - wdioctl() calls physio/wdioctlstrategy based on whether or not
   1863  *   user space I/O is required.  If physio() is called, physio() eventually
   1864  *   calls wdioctlstrategy().
   1865  *
   1866  * - In either case, wdioctlstrategy() calls wd->atabus->ata_exec_command()
   1867  *   to perform the actual command
   1868  *
   1869  * The reason for the use of the pseudo strategy routine is because
   1870  * when doing I/O to/from user space, physio _really_ wants to be in
   1871  * the loop.  We could put the entire buffer into the ioctl request
   1872  * structure, but that won't scale if we want to do things like download
   1873  * microcode.
   1874  */
   1875 
   1876 void
   1877 wdioctlstrategy(struct buf *bp)
   1878 {
   1879 	struct wd_ioctl *wi;
   1880 	struct ata_command ata_c;
   1881 	int error = 0;
   1882 
   1883 	wi = wi_find(bp);
   1884 	if (wi == NULL) {
   1885 		printf("user_strat: No ioctl\n");
   1886 		error = EINVAL;
   1887 		goto bad;
   1888 	}
   1889 
   1890 	memset(&ata_c, 0, sizeof(ata_c));
   1891 
   1892 	/*
   1893 	 * Abort if physio broke up the transfer
   1894 	 */
   1895 
   1896 	if (bp->b_bcount != wi->wi_atareq.datalen) {
   1897 		printf("physio split wd ioctl request... cannot proceed\n");
   1898 		error = EIO;
   1899 		goto bad;
   1900 	}
   1901 
   1902 	/*
   1903 	 * Abort if we didn't get a buffer size that was a multiple of
   1904 	 * our sector size (or was larger than NBBY)
   1905 	 */
   1906 
   1907 	if ((bp->b_bcount % wi->wi_softc->sc_dk.dk_label->d_secsize) != 0 ||
   1908 	    (bp->b_bcount / wi->wi_softc->sc_dk.dk_label->d_secsize) >=
   1909 	     (1 << NBBY)) {
   1910 		error = EINVAL;
   1911 		goto bad;
   1912 	}
   1913 
   1914 	/*
   1915 	 * Make sure a timeout was supplied in the ioctl request
   1916 	 */
   1917 
   1918 	if (wi->wi_atareq.timeout == 0) {
   1919 		error = EINVAL;
   1920 		goto bad;
   1921 	}
   1922 
   1923 	if (wi->wi_atareq.flags & ATACMD_READ)
   1924 		ata_c.flags |= AT_READ;
   1925 	else if (wi->wi_atareq.flags & ATACMD_WRITE)
   1926 		ata_c.flags |= AT_WRITE;
   1927 
   1928 	if (wi->wi_atareq.flags & ATACMD_READREG)
   1929 		ata_c.flags |= AT_READREG;
   1930 
   1931 	ata_c.flags |= AT_WAIT;
   1932 
   1933 	ata_c.timeout = wi->wi_atareq.timeout;
   1934 	ata_c.r_command = wi->wi_atareq.command;
   1935 	ata_c.r_head = wi->wi_atareq.head & 0x0f;
   1936 	ata_c.r_cyl = wi->wi_atareq.cylinder;
   1937 	ata_c.r_sector = wi->wi_atareq.sec_num;
   1938 	ata_c.r_count = wi->wi_atareq.sec_count;
   1939 	ata_c.r_features = wi->wi_atareq.features;
   1940 	ata_c.r_st_bmask = WDCS_DRDY;
   1941 	ata_c.r_st_pmask = WDCS_DRDY;
   1942 	ata_c.data = wi->wi_bp.b_data;
   1943 	ata_c.bcount = wi->wi_bp.b_bcount;
   1944 
   1945 	if (wi->wi_softc->atabus->ata_exec_command(wi->wi_softc->drvp, &ata_c)
   1946 	    != ATACMD_COMPLETE) {
   1947 		wi->wi_atareq.retsts = ATACMD_ERROR;
   1948 		goto bad;
   1949 	}
   1950 
   1951 	if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
   1952 		if (ata_c.flags & AT_ERROR) {
   1953 			wi->wi_atareq.retsts = ATACMD_ERROR;
   1954 			wi->wi_atareq.error = ata_c.r_error;
   1955 		} else if (ata_c.flags & AT_DF)
   1956 			wi->wi_atareq.retsts = ATACMD_DF;
   1957 		else
   1958 			wi->wi_atareq.retsts = ATACMD_TIMEOUT;
   1959 	} else {
   1960 		wi->wi_atareq.retsts = ATACMD_OK;
   1961 		if (wi->wi_atareq.flags & ATACMD_READREG) {
   1962 			wi->wi_atareq.head = ata_c.r_head ;
   1963 			wi->wi_atareq.cylinder = ata_c.r_cyl;
   1964 			wi->wi_atareq.sec_num = ata_c.r_sector;
   1965 			wi->wi_atareq.sec_count = ata_c.r_count;
   1966 			wi->wi_atareq.features = ata_c.r_features;
   1967 			wi->wi_atareq.error = ata_c.r_error;
   1968 		}
   1969 	}
   1970 
   1971 	bp->b_error = 0;
   1972 	biodone(bp);
   1973 	return;
   1974 bad:
   1975 	bp->b_flags |= B_ERROR;
   1976 	bp->b_error = error;
   1977 	biodone(bp);
   1978 }
   1979