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