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