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