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