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