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