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