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