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