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