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