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