Home | History | Annotate | Line # | Download | only in ata
wd.c revision 1.214.4.3
      1 /*	$NetBSD: wd.c,v 1.214.4.3 2001/09/26 15:28:10 fvdl Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1998 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 #ifndef WDCDEBUG
     69 #define WDCDEBUG
     70 #endif /* WDCDEBUG */
     71 
     72 #include "rnd.h"
     73 
     74 #include <sys/param.h>
     75 #include <sys/systm.h>
     76 #include <sys/kernel.h>
     77 #include <sys/conf.h>
     78 #include <sys/file.h>
     79 #include <sys/stat.h>
     80 #include <sys/ioctl.h>
     81 #include <sys/buf.h>
     82 #include <sys/uio.h>
     83 #include <sys/malloc.h>
     84 #include <sys/device.h>
     85 #include <sys/disklabel.h>
     86 #include <sys/disk.h>
     87 #include <sys/syslog.h>
     88 #include <sys/proc.h>
     89 #include <sys/vnode.h>
     90 #if NRND > 0
     91 #include <sys/rnd.h>
     92 #endif
     93 
     94 #include <miscfs/specfs/specdev.h>
     95 
     96 #include <machine/intr.h>
     97 #include <machine/bus.h>
     98 
     99 #include <dev/ata/atareg.h>
    100 #include <dev/ata/atavar.h>
    101 #include <dev/ata/wdvar.h>
    102 #include <dev/ic/wdcreg.h>
    103 #include <sys/ataio.h>
    104 #include "locators.h"
    105 
    106 #define	WAITTIME	(4 * hz)	/* time to wait for a completion */
    107 #define	WDIORETRIES_SINGLE 4	/* number of retries before single-sector */
    108 #define	WDIORETRIES	5	/* number of retries before giving up */
    109 #define	RECOVERYTIME hz/2	/* time to wait before retrying a cmd */
    110 
    111 #define	WDUNIT(dev)		DISKUNIT(dev)
    112 #define	WDPART(dev)		DISKPART(dev)
    113 #define	WDMINOR(unit, part)	DISKMINOR(unit, part)
    114 #define	MAKEWDDEV(maj, unit, part)	MAKEDISKDEV(maj, unit, part)
    115 
    116 #define	WDLABELDEV(dev)	(MAKEWDDEV(major(dev), WDUNIT(dev), RAW_PART))
    117 
    118 #define DEBUG_INTR   0x01
    119 #define DEBUG_XFERS  0x02
    120 #define DEBUG_STATUS 0x04
    121 #define DEBUG_FUNCS  0x08
    122 #define DEBUG_PROBE  0x10
    123 #ifdef WDCDEBUG
    124 extern int wdcdebug_wd_mask; /* init'ed in ata_wdc.c */
    125 #define WDCDEBUG_PRINT(args, level) \
    126 	if (wdcdebug_wd_mask & (level)) \
    127 		printf args
    128 #else
    129 #define WDCDEBUG_PRINT(args, level)
    130 #endif
    131 
    132 struct wd_softc {
    133 	/* General disk infos */
    134 	struct device sc_dev;
    135 	struct disk sc_dk;
    136 	struct buf_queue sc_q;
    137 	struct callout sc_restart_ch;
    138 	/* IDE disk soft states */
    139 	struct ata_bio sc_wdc_bio; /* current transfer */
    140 	struct buf *sc_bp; /* buf being transfered */
    141 	void *wdc_softc;   /* pointer to our parent */
    142 	struct ata_drive_datas *drvp; /* Our controller's infos */
    143 	int openings;
    144 	struct ataparams sc_params;/* drive characteistics found */
    145 	int sc_flags;
    146 #define WDF_LOCKED	  0x01
    147 #define WDF_WANTED	  0x02
    148 #define WDF_WLABEL	  0x04 /* label is writable */
    149 #define WDF_LABELLING   0x08 /* writing label */
    150 /*
    151  * XXX Nothing resets this yet, but disk change sensing will when ATA-4 is
    152  * more fully implemented.
    153  */
    154 #define WDF_LOADED	  0x10 /* parameters loaded */
    155 #define WDF_WAIT	0x20 /* waiting for resources */
    156 #define WDF_LBA	 0x40 /* using LBA mode */
    157 #define WDF_KLABEL	 0x80 /* retain label after 'full' close */
    158 	int sc_capacity;
    159 	int cyl; /* actual drive parameters */
    160 	int heads;
    161 	int sectors;
    162 	int retries; /* number of xfer retry */
    163 
    164 	void *sc_sdhook;		/* our shutdown hook */
    165 
    166 #if NRND > 0
    167 	rndsource_element_t	rnd_source;
    168 #endif
    169 };
    170 
    171 #define sc_drive sc_wdc_bio.drive
    172 #define sc_mode sc_wdc_bio.mode
    173 #define sc_multi sc_wdc_bio.multi
    174 #define sc_badsect sc_wdc_bio.badsect
    175 
    176 int	wdprobe		__P((struct device *, struct cfdata *, void *));
    177 void	wdattach	__P((struct device *, struct device *, void *));
    178 int	wddetach __P((struct device *, int));
    179 int	wdactivate __P((struct device *, enum devact));
    180 int	wdprint	__P((void *, char *));
    181 
    182 struct cfattach wd_ca = {
    183 	sizeof(struct wd_softc), wdprobe, wdattach, wddetach, wdactivate
    184 };
    185 
    186 extern struct cfdriver wd_cd;
    187 
    188 /*
    189  * Glue necessary to hook WDCIOCCOMMAND into physio
    190  */
    191 
    192 struct wd_ioctl {
    193 	LIST_ENTRY(wd_ioctl) wi_list;
    194 	struct buf wi_bp;
    195 	struct uio wi_uio;
    196 	struct iovec wi_iov;
    197 	atareq_t wi_atareq;
    198 	struct wd_softc *wi_softc;
    199 };
    200 
    201 LIST_HEAD(, wd_ioctl) wi_head;
    202 
    203 struct	wd_ioctl *wi_find __P((struct buf *));
    204 void	wi_free __P((struct wd_ioctl *));
    205 struct	wd_ioctl *wi_get __P((void));
    206 void	wdioctlstrategy __P((struct buf *));
    207 
    208 void  wdgetdefaultlabel __P((struct wd_softc *, struct disklabel *));
    209 void  wdgetdisklabel	__P((struct vnode *));
    210 void  wdstrategy	__P((struct buf *));
    211 void  wdstart	__P((void *));
    212 void  __wdstart	__P((struct wd_softc*, struct buf *));
    213 void  wdrestart __P((void*));
    214 int   wd_get_params __P((struct wd_softc *, u_int8_t, struct ataparams *));
    215 void  wd_flushcache __P((struct wd_softc *, int));
    216 void  wd_shutdown __P((void*));
    217 
    218 struct dkdriver wddkdriver = { wdstrategy };
    219 
    220 /* XXX: these should go elsewhere */
    221 cdev_decl(wd);
    222 bdev_decl(wd);
    223 
    224 #ifdef HAS_BAD144_HANDLING
    225 static void bad144intern __P((struct wd_softc *));
    226 #endif
    227 int	wdlock	__P((struct wd_softc *));
    228 void	wdunlock	__P((struct wd_softc *));
    229 
    230 int
    231 wdprobe(parent, match, aux)
    232 	struct device *parent;
    233 	struct cfdata *match;
    234 
    235 	void *aux;
    236 {
    237 	struct ata_atapi_attach *aa_link = aux;
    238 
    239 	if (aa_link == NULL)
    240 		return 0;
    241 	if (aa_link->aa_type != T_ATA)
    242 		return 0;
    243 
    244 	if (match->cf_loc[ATACF_CHANNEL] != ATACF_CHANNEL_DEFAULT &&
    245 	    match->cf_loc[ATACF_CHANNEL] != aa_link->aa_channel)
    246 		return 0;
    247 
    248 	if (match->cf_loc[ATACF_DRIVE] != ATACF_DRIVE_DEFAULT &&
    249 	    match->cf_loc[ATACF_DRIVE] != aa_link->aa_drv_data->drive)
    250 		return 0;
    251 	return 1;
    252 }
    253 
    254 void
    255 wdattach(parent, self, aux)
    256 	struct device *parent, *self;
    257 	void *aux;
    258 {
    259 	struct wd_softc *wd = (void *)self;
    260 	struct ata_atapi_attach *aa_link= aux;
    261 	int i, blank;
    262 	char buf[41], pbuf[9], c, *p, *q;
    263 	WDCDEBUG_PRINT(("wdattach\n"), DEBUG_FUNCS | DEBUG_PROBE);
    264 
    265 	callout_init(&wd->sc_restart_ch);
    266 	BUFQ_INIT(&wd->sc_q);
    267 
    268 	wd->openings = aa_link->aa_openings;
    269 	wd->drvp = aa_link->aa_drv_data;;
    270 	wd->wdc_softc = parent;
    271 	/* give back our softc to our caller */
    272 	wd->drvp->drv_softc = &wd->sc_dev;
    273 
    274 	/* read our drive info */
    275 	if (wd_get_params(wd, AT_POLL, &wd->sc_params) != 0) {
    276 		printf("%s: IDENTIFY failed\n", wd->sc_dev.dv_xname);
    277 		return;
    278 	}
    279 
    280 	for (blank = 0, p = wd->sc_params.atap_model, q = buf, i = 0;
    281 	    i < sizeof(wd->sc_params.atap_model); i++) {
    282 		c = *p++;
    283 		if (c == '\0')
    284 			break;
    285 		if (c != ' ') {
    286 			if (blank) {
    287 				*q++ = ' ';
    288 				blank = 0;
    289 			}
    290 			*q++ = c;
    291 		} else
    292 			blank = 1;
    293 	}
    294 	*q++ = '\0';
    295 
    296 	printf(": <%s>\n", buf);
    297 
    298 	if ((wd->sc_params.atap_multi & 0xff) > 1) {
    299 		wd->sc_multi = wd->sc_params.atap_multi & 0xff;
    300 	} else {
    301 		wd->sc_multi = 1;
    302 	}
    303 
    304 	printf("%s: drive supports %d-sector PIO transfers,",
    305 	    wd->sc_dev.dv_xname, wd->sc_multi);
    306 
    307 	/* Prior to ATA-4, LBA was optional. */
    308 	if ((wd->sc_params.atap_capabilities1 & WDC_CAP_LBA) != 0)
    309 		wd->sc_flags |= WDF_LBA;
    310 #if 0
    311 	/* ATA-4 requires LBA. */
    312 	if (wd->sc_params.atap_ataversion != 0xffff &&
    313 	    wd->sc_params.atap_ataversion >= WDC_VER_ATA4)
    314 		wd->sc_flags |= WDF_LBA;
    315 #endif
    316 
    317 	if ((wd->sc_flags & WDF_LBA) != 0) {
    318 		printf(" LBA addressing\n");
    319 		wd->sc_capacity =
    320 		    (wd->sc_params.atap_capacity[1] << 16) |
    321 		    wd->sc_params.atap_capacity[0];
    322 	} else {
    323 		printf(" chs addressing\n");
    324 		wd->sc_capacity =
    325 		    wd->sc_params.atap_cylinders *
    326 		    wd->sc_params.atap_heads *
    327 		    wd->sc_params.atap_sectors;
    328 	}
    329 	format_bytes(pbuf, sizeof(pbuf),
    330 	    (u_int64_t)wd->sc_capacity * DEV_BSIZE);
    331 	printf("%s: %s, %d cyl, %d head, %d sec, "
    332 	    "%d bytes/sect x %d sectors\n",
    333 	    self->dv_xname, pbuf, wd->sc_params.atap_cylinders,
    334 	    wd->sc_params.atap_heads, wd->sc_params.atap_sectors,
    335 	    DEV_BSIZE, wd->sc_capacity);
    336 
    337 	WDCDEBUG_PRINT(("%s: atap_dmatiming_mimi=%d, atap_dmatiming_recom=%d\n",
    338 	    self->dv_xname, wd->sc_params.atap_dmatiming_mimi,
    339 	    wd->sc_params.atap_dmatiming_recom), DEBUG_PROBE);
    340 	/*
    341 	 * Initialize and attach the disk structure.
    342 	 */
    343 	wd->sc_dk.dk_driver = &wddkdriver;
    344 	wd->sc_dk.dk_name = wd->sc_dev.dv_xname;
    345 	disk_attach(&wd->sc_dk);
    346 	wd->sc_wdc_bio.lp = wd->sc_dk.dk_label;
    347 	wd->sc_sdhook = shutdownhook_establish(wd_shutdown, wd);
    348 	if (wd->sc_sdhook == NULL)
    349 		printf("%s: WARNING: unable to establish shutdown hook\n",
    350 		    wd->sc_dev.dv_xname);
    351 #if NRND > 0
    352 	rnd_attach_source(&wd->rnd_source, wd->sc_dev.dv_xname,
    353 			  RND_TYPE_DISK, 0);
    354 #endif
    355 }
    356 
    357 int
    358 wdactivate(self, act)
    359 	struct device *self;
    360 	enum devact act;
    361 {
    362 	int rv = 0;
    363 
    364 	switch (act) {
    365 	case DVACT_ACTIVATE:
    366 		rv = EOPNOTSUPP;
    367 		break;
    368 
    369 	case DVACT_DEACTIVATE:
    370 		/*
    371 		 * Nothing to do; we key off the device's DVF_ACTIVATE.
    372 		 */
    373 		break;
    374 	}
    375 	return (rv);
    376 }
    377 
    378 int
    379 wddetach(self, flags)
    380 	struct device *self;
    381 	int flags;
    382 {
    383 	struct wd_softc *sc = (struct wd_softc *)self;
    384 	struct buf *bp;
    385 	int s, bmaj, cmaj, i, mn;
    386 
    387 	/* locate the major number */
    388 	for (bmaj = 0; bmaj < nblkdev; bmaj++)
    389 		if (bdevsw[bmaj].d_open == wdopen)
    390 			break;
    391 	for (cmaj = 0; cmaj < nchrdev; cmaj++)
    392 		if (cdevsw[cmaj].d_open == wdopen)
    393 			break;
    394 
    395 	s = splbio();
    396 
    397 	/* Kill off any queued buffers. */
    398 	while ((bp = BUFQ_FIRST(&sc->sc_q)) != NULL) {
    399 		BUFQ_REMOVE(&sc->sc_q, bp);
    400 		bp->b_error = EIO;
    401 		bp->b_flags |= B_ERROR;
    402 		bp->b_resid = bp->b_bcount;
    403 		biodone(bp);
    404 	}
    405 
    406 	splx(s);
    407 
    408 	/* Nuke the vnodes for any open instances. */
    409 	for (i = 0; i < MAXPARTITIONS; i++) {
    410 		mn = WDMINOR(self->dv_unit, i);
    411 		vdevgone(bmaj, mn, mn, VBLK);
    412 		vdevgone(cmaj, mn, mn, VCHR);
    413 	}
    414 
    415 	/* Detach disk. */
    416 	disk_detach(&sc->sc_dk);
    417 
    418 	/* Get rid of the shutdown hook. */
    419 	if (sc->sc_sdhook != NULL)
    420 		shutdownhook_disestablish(sc->sc_sdhook);
    421 
    422 #if NRND > 0
    423 	/* Unhook the entropy source. */
    424 	rnd_detach_source(&sc->rnd_source);
    425 #endif
    426 
    427 	return (0);
    428 }
    429 
    430 /*
    431  * Read/write routine for a buffer.  Validates the arguments and schedules the
    432  * transfer.  Does not wait for the transfer to complete.
    433  */
    434 void
    435 wdstrategy(bp)
    436 	struct buf *bp;
    437 {
    438 	struct wd_softc *wd;
    439 	struct disklabel *lp;
    440 	daddr_t blkno;
    441 	int s;
    442 	dev_t rdev;
    443 
    444 	wd = vdev_privdata(bp->b_devvp);
    445 	rdev = vdev_rdev(bp->b_devvp);
    446 	lp = wd->sc_dk.dk_label;
    447 
    448 	WDCDEBUG_PRINT(("wdstrategy (%s)\n", wd->sc_dev.dv_xname),
    449 	    DEBUG_XFERS);
    450 
    451 	/* Valid request?  */
    452 	if (bp->b_blkno < 0 ||
    453 	    (bp->b_bcount % lp->d_secsize) != 0 ||
    454 	    (bp->b_bcount / lp->d_secsize) >= (1 << NBBY)) {
    455 		bp->b_error = EINVAL;
    456 		goto bad;
    457 	}
    458 
    459 	/* If device invalidated (e.g. media change, door open), error. */
    460 	if ((wd->sc_flags & WDF_LOADED) == 0) {
    461 		bp->b_error = EIO;
    462 		goto bad;
    463 	}
    464 
    465 	/* If it's a null transfer, return immediately. */
    466 	if (bp->b_bcount == 0)
    467 		goto done;
    468 
    469 	/*
    470 	 * Do bounds checking, adjust transfer. if error, process.
    471 	 * If end of partition, just return.
    472 	 */
    473 	if (WDPART(rdev) != RAW_PART &&
    474 	    (bp->b_flags & B_DKLABEL) == 0 &&
    475 	    bounds_check_with_label(bp, wd->sc_dk.dk_label,
    476 	    (wd->sc_flags & (WDF_WLABEL|WDF_LABELLING)) != 0) <= 0)
    477 		goto done;
    478 
    479 	/*
    480 	 * Now convert the block number to absolute and put it in
    481 	 * terms of the device's logical block size.
    482 	 */
    483 	if (lp->d_secsize >= DEV_BSIZE)
    484 		blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
    485 	else
    486 		blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
    487 
    488 	if (WDPART(rdev) != RAW_PART &&
    489 	    (bp->b_flags & B_DKLABEL) == 0)
    490 		blkno += lp->d_partitions[WDPART(rdev)].p_offset;
    491 
    492 	bp->b_rawblkno = blkno;
    493 
    494 	/* Queue transfer on drive, activate drive and controller if idle. */
    495 	s = splbio();
    496 	disksort_blkno(&wd->sc_q, bp);
    497 	wdstart(wd);
    498 	splx(s);
    499 	return;
    500 bad:
    501 	bp->b_flags |= B_ERROR;
    502 done:
    503 	/* Toss transfer; we're done early. */
    504 	bp->b_resid = bp->b_bcount;
    505 	biodone(bp);
    506 }
    507 
    508 /*
    509  * Queue a drive for I/O.
    510  */
    511 void
    512 wdstart(arg)
    513 	void *arg;
    514 {
    515 	struct wd_softc *wd = arg;
    516 	struct buf *bp = NULL;
    517 
    518 	WDCDEBUG_PRINT(("wdstart %s\n", wd->sc_dev.dv_xname),
    519 	    DEBUG_XFERS);
    520 	while (wd->openings > 0) {
    521 
    522 		/* Is there a buf for us ? */
    523 		if ((bp = BUFQ_FIRST(&wd->sc_q)) == NULL)
    524 			return;
    525 		BUFQ_REMOVE(&wd->sc_q, bp);
    526 
    527 		/*
    528 		 * Make the command. First lock the device
    529 		 */
    530 		wd->openings--;
    531 
    532 		wd->retries = 0;
    533 		__wdstart(wd, bp);
    534 	}
    535 }
    536 
    537 void
    538 __wdstart(wd, bp)
    539 	struct wd_softc *wd;
    540 	struct buf *bp;
    541 {
    542 
    543 	wd->sc_wdc_bio.blkno = bp->b_rawblkno;
    544 	wd->sc_wdc_bio.blkdone =0;
    545 	wd->sc_bp = bp;
    546 	/*
    547 	 * If we're retrying, retry in single-sector mode. This will give us
    548 	 * the sector number of the problem, and will eventually allow the
    549 	 * transfer to succeed.
    550 	 */
    551 	if (wd->sc_multi == 1 || wd->retries >= WDIORETRIES_SINGLE)
    552 		wd->sc_wdc_bio.flags = ATA_SINGLE;
    553 	else
    554 		wd->sc_wdc_bio.flags = 0;
    555 	if (wd->sc_flags & WDF_LBA)
    556 		wd->sc_wdc_bio.flags |= ATA_LBA;
    557 	if (bp->b_flags & B_READ)
    558 		wd->sc_wdc_bio.flags |= ATA_READ;
    559 	wd->sc_wdc_bio.bcount = bp->b_bcount;
    560 	wd->sc_wdc_bio.databuf = bp->b_data;
    561 	/* Instrumentation. */
    562 	disk_busy(&wd->sc_dk);
    563 	switch (wdc_ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
    564 	case WDC_TRY_AGAIN:
    565 		callout_reset(&wd->sc_restart_ch, hz, wdrestart, wd);
    566 		break;
    567 	case WDC_QUEUED:
    568 	case WDC_COMPLETE:
    569 		break;
    570 	default:
    571 		panic("__wdstart: bad return code from wdc_ata_bio()");
    572 	}
    573 }
    574 
    575 void
    576 wddone(v)
    577 	void *v;
    578 {
    579 	struct wd_softc *wd = v;
    580 	struct buf *bp = wd->sc_bp;
    581 	char buf[256], *errbuf = buf;
    582 	WDCDEBUG_PRINT(("wddone %s\n", wd->sc_dev.dv_xname),
    583 	    DEBUG_XFERS);
    584 
    585 	if (bp == NULL)
    586 		return;
    587 	bp->b_resid = wd->sc_wdc_bio.bcount;
    588 	errbuf[0] = '\0';
    589 	switch (wd->sc_wdc_bio.error) {
    590 	case ERR_DMA:
    591 		errbuf = "DMA error";
    592 		goto retry;
    593 	case ERR_DF:
    594 		errbuf = "device fault";
    595 		goto retry;
    596 	case TIMEOUT:
    597 		errbuf = "device timeout";
    598 		goto retry;
    599 	case ERROR:
    600 		/* Don't care about media change bits */
    601 		if (wd->sc_wdc_bio.r_error != 0 &&
    602 		    (wd->sc_wdc_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0)
    603 			goto noerror;
    604 		ata_perror(wd->drvp, wd->sc_wdc_bio.r_error, errbuf);
    605 retry:		/* Just reset and retry. Can we do more ? */
    606 		wdc_reset_channel(wd->drvp);
    607 		diskerr(bp, "wd", errbuf, LOG_PRINTF,
    608 		    wd->sc_wdc_bio.blkdone, wd->sc_dk.dk_label);
    609 		if (wd->retries++ < WDIORETRIES) {
    610 			printf(", retrying\n");
    611 			callout_reset(&wd->sc_restart_ch, RECOVERYTIME,
    612 			    wdrestart, wd);
    613 			return;
    614 		}
    615 		printf("\n");
    616 		bp->b_flags |= B_ERROR;
    617 		bp->b_error = EIO;
    618 		break;
    619 	case NOERROR:
    620 noerror:	if ((wd->sc_wdc_bio.flags & ATA_CORR) || wd->retries > 0)
    621 			printf("%s: soft error (corrected)\n",
    622 			    wd->sc_dev.dv_xname);
    623 		break;
    624 	case ERR_NODEV:
    625 		bp->b_flags |= B_ERROR;
    626 		bp->b_error = EIO;
    627 		break;
    628 	}
    629 	disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid));
    630 #if NRND > 0
    631 	rnd_add_uint32(&wd->rnd_source, bp->b_blkno);
    632 #endif
    633 	biodone(bp);
    634 	wd->openings++;
    635 	wdstart(wd);
    636 }
    637 
    638 void
    639 wdrestart(v)
    640 	void *v;
    641 {
    642 	struct wd_softc *wd = v;
    643 	struct buf *bp = wd->sc_bp;
    644 	int s;
    645 	WDCDEBUG_PRINT(("wdrestart %s\n", wd->sc_dev.dv_xname),
    646 	    DEBUG_XFERS);
    647 
    648 	s = splbio();
    649 	__wdstart(v, bp);
    650 	splx(s);
    651 }
    652 
    653 int
    654 wdread(devvp, uio, flags)
    655 	struct vnode *devvp;
    656 	struct uio *uio;
    657 	int flags;
    658 {
    659 
    660 	WDCDEBUG_PRINT(("wdread\n"), DEBUG_XFERS);
    661 	return (physio(wdstrategy, NULL, devvp, B_READ, minphys, uio));
    662 }
    663 
    664 int
    665 wdwrite(devvp, uio, flags)
    666 	struct vnode *devvp;
    667 	struct uio *uio;
    668 	int flags;
    669 {
    670 
    671 	WDCDEBUG_PRINT(("wdwrite\n"), DEBUG_XFERS);
    672 	return (physio(wdstrategy, NULL, devvp, B_WRITE, minphys, uio));
    673 }
    674 
    675 /*
    676  * Wait interruptibly for an exclusive lock.
    677  *
    678  * XXX
    679  * Several drivers do this; it should be abstracted and made MP-safe.
    680  */
    681 int
    682 wdlock(wd)
    683 	struct wd_softc *wd;
    684 {
    685 	int error;
    686 	int s;
    687 
    688 	WDCDEBUG_PRINT(("wdlock\n"), DEBUG_FUNCS);
    689 
    690 	s = splbio();
    691 
    692 	while ((wd->sc_flags & WDF_LOCKED) != 0) {
    693 		wd->sc_flags |= WDF_WANTED;
    694 		if ((error = tsleep(wd, PRIBIO | PCATCH,
    695 		    "wdlck", 0)) != 0) {
    696 			splx(s);
    697 			return error;
    698 		}
    699 	}
    700 	wd->sc_flags |= WDF_LOCKED;
    701 	splx(s);
    702 	return 0;
    703 }
    704 
    705 /*
    706  * Unlock and wake up any waiters.
    707  */
    708 void
    709 wdunlock(wd)
    710 	struct wd_softc *wd;
    711 {
    712 
    713 	WDCDEBUG_PRINT(("wdunlock\n"), DEBUG_FUNCS);
    714 
    715 	wd->sc_flags &= ~WDF_LOCKED;
    716 	if ((wd->sc_flags & WDF_WANTED) != 0) {
    717 		wd->sc_flags &= ~WDF_WANTED;
    718 		wakeup(wd);
    719 	}
    720 }
    721 
    722 int
    723 wdopen(devvp, flag, fmt, p)
    724 	struct vnode *devvp;
    725 	int flag, fmt;
    726 	struct proc *p;
    727 {
    728 	struct wd_softc *wd;
    729 	int part, error;
    730 
    731 	WDCDEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS);
    732 	wd = device_lookup(&wd_cd, WDUNIT(vdev_rdev(devvp)));
    733 	if (wd == NULL)
    734 		return (ENXIO);
    735 
    736 	vdev_setprivdata(devvp, wd);
    737 
    738 	/*
    739 	 * If this is the first open of this device, add a reference
    740 	 * to the adapter.
    741 	 */
    742 	if (wd->sc_dk.dk_openmask == 0 &&
    743 	    (error = wdc_ata_addref(wd->drvp)) != 0)
    744 		return (error);
    745 
    746 	if ((error = wdlock(wd)) != 0)
    747 		goto bad4;
    748 
    749 	if (wd->sc_dk.dk_openmask != 0) {
    750 		/*
    751 		 * If any partition is open, but the disk has been invalidated,
    752 		 * disallow further opens.
    753 		 */
    754 		if ((wd->sc_flags & WDF_LOADED) == 0) {
    755 			error = EIO;
    756 			goto bad3;
    757 		}
    758 	} else {
    759 		if ((wd->sc_flags & WDF_LOADED) == 0) {
    760 			wd->sc_flags |= WDF_LOADED;
    761 
    762 			/* Load the physical device parameters. */
    763 			wd_get_params(wd, AT_WAIT, &wd->sc_params);
    764 
    765 			/* Load the partition info if not already loaded. */
    766 			wdgetdisklabel(devvp);
    767 		}
    768 	}
    769 
    770 	part = WDPART(devvp->v_rdev);
    771 
    772 	/* Check that the partition exists. */
    773 	if (part != RAW_PART &&
    774 	    (part >= wd->sc_dk.dk_label->d_npartitions ||
    775 	     wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
    776 		error = ENXIO;
    777 		goto bad;
    778 	}
    779 
    780 	/* Insure only one open at a time. */
    781 	switch (fmt) {
    782 	case S_IFCHR:
    783 		wd->sc_dk.dk_copenmask |= (1 << part);
    784 		break;
    785 	case S_IFBLK:
    786 		wd->sc_dk.dk_bopenmask |= (1 << part);
    787 		break;
    788 	}
    789 	wd->sc_dk.dk_openmask =
    790 	    wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
    791 
    792 	wdunlock(wd);
    793 	return 0;
    794 
    795 bad:
    796 	if (wd->sc_dk.dk_openmask == 0) {
    797 	}
    798 
    799 bad3:
    800 	wdunlock(wd);
    801 bad4:
    802 	if (wd->sc_dk.dk_openmask == 0)
    803 		wdc_ata_delref(wd->drvp);
    804 	return error;
    805 }
    806 
    807 int
    808 wdclose(devvp, flag, fmt, p)
    809 	struct vnode *devvp;
    810 	int flag, fmt;
    811 	struct proc *p;
    812 {
    813 	struct wd_softc *wd;
    814 	int part, error;
    815 
    816 	WDCDEBUG_PRINT(("wdclose\n"), DEBUG_FUNCS);
    817 
    818 	wd = vdev_privdata(devvp);
    819 	part = WDPART(vdev_rdev(devvp));
    820 
    821 	if ((error = wdlock(wd)) != 0)
    822 		return error;
    823 
    824 	switch (fmt) {
    825 	case S_IFCHR:
    826 		wd->sc_dk.dk_copenmask &= ~(1 << part);
    827 		break;
    828 	case S_IFBLK:
    829 		wd->sc_dk.dk_bopenmask &= ~(1 << part);
    830 		break;
    831 	}
    832 	wd->sc_dk.dk_openmask =
    833 	    wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
    834 
    835 	if (wd->sc_dk.dk_openmask == 0) {
    836 		wd_flushcache(wd, AT_WAIT);
    837 		/* XXXX Must wait for I/O to complete! */
    838 
    839 		if (! (wd->sc_flags & WDF_KLABEL))
    840 			wd->sc_flags &= ~WDF_LOADED;
    841 
    842 		wdc_ata_delref(wd->drvp);
    843 	}
    844 
    845 	wdunlock(wd);
    846 	return 0;
    847 }
    848 
    849 void
    850 wdgetdefaultlabel(wd, lp)
    851 	struct wd_softc *wd;
    852 	struct disklabel *lp;
    853 {
    854 
    855 	WDCDEBUG_PRINT(("wdgetdefaultlabel\n"), DEBUG_FUNCS);
    856 	memset(lp, 0, sizeof(struct disklabel));
    857 
    858 	lp->d_secsize = DEV_BSIZE;
    859 	lp->d_ntracks = wd->sc_params.atap_heads;
    860 	lp->d_nsectors = wd->sc_params.atap_sectors;
    861 	lp->d_ncylinders = wd->sc_params.atap_cylinders;
    862 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    863 
    864 	if (strcmp(wd->sc_params.atap_model, "ST506") == 0)
    865 		lp->d_type = DTYPE_ST506;
    866 	else
    867 		lp->d_type = DTYPE_ESDI;
    868 
    869 	strncpy(lp->d_typename, wd->sc_params.atap_model, 16);
    870 	strncpy(lp->d_packname, "fictitious", 16);
    871 	lp->d_secperunit = wd->sc_capacity;
    872 	lp->d_rpm = 3600;
    873 	lp->d_interleave = 1;
    874 	lp->d_flags = 0;
    875 
    876 	lp->d_partitions[RAW_PART].p_offset = 0;
    877 	lp->d_partitions[RAW_PART].p_size =
    878 	lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
    879 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
    880 	lp->d_npartitions = RAW_PART + 1;
    881 
    882 	lp->d_magic = DISKMAGIC;
    883 	lp->d_magic2 = DISKMAGIC;
    884 	lp->d_checksum = dkcksum(lp);
    885 }
    886 
    887 /*
    888  * Fabricate a default disk label, and try to read the correct one.
    889  */
    890 void
    891 wdgetdisklabel(devvp)
    892 	struct vnode *devvp;
    893 {
    894 	struct wd_softc *wd;
    895 	struct disklabel *lp;
    896 	char *errstring;
    897 
    898 	WDCDEBUG_PRINT(("wdgetdisklabel\n"), DEBUG_FUNCS);
    899 
    900 	wd = vdev_privdata(devvp);
    901 
    902 	lp = wd->sc_dk.dk_label;
    903 
    904 	memset(wd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
    905 
    906 	wdgetdefaultlabel(wd, lp);
    907 
    908 	wd->sc_badsect[0] = -1;
    909 
    910 	if (wd->drvp->state > RECAL)
    911 		wd->drvp->drive_flags |= DRIVE_RESET;
    912 	errstring = readdisklabel(devvp,
    913 	    wdstrategy, lp, wd->sc_dk.dk_cpulabel);
    914 	if (errstring) {
    915 		/*
    916 		 * This probably happened because the drive's default
    917 		 * geometry doesn't match the DOS geometry.  We
    918 		 * assume the DOS geometry is now in the label and try
    919 		 * again.  XXX This is a kluge.
    920 		 */
    921 		if (wd->drvp->state > RECAL)
    922 			wd->drvp->drive_flags |= DRIVE_RESET;
    923 		errstring = readdisklabel(devvp,
    924 		    wdstrategy, lp, wd->sc_dk.dk_cpulabel);
    925 	}
    926 	if (errstring) {
    927 		printf("%s: %s\n", wd->sc_dev.dv_xname, errstring);
    928 		return;
    929 	}
    930 
    931 	if (wd->drvp->state > RECAL)
    932 		wd->drvp->drive_flags |= DRIVE_RESET;
    933 #ifdef HAS_BAD144_HANDLING
    934 	if ((lp->d_flags & D_BADSECT) != 0)
    935 		bad144intern(wd);
    936 #endif
    937 }
    938 
    939 int
    940 wdioctl(devvp, xfer, addr, flag, p)
    941 	struct vnode *devvp;
    942 	u_long xfer;
    943 	caddr_t addr;
    944 	int flag;
    945 	struct proc *p;
    946 {
    947 	struct wd_softc *wd;
    948 	int error;
    949 	dev_t rdev;
    950 #ifdef __HAVE_OLD_DISKLABEL
    951 	struct disklabel newlabel;
    952 #endif
    953 
    954 	WDCDEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS);
    955 
    956 	rdev = vdev_rdev(devvp);
    957 	wd = vdev_privdata(devvp);
    958 
    959 	if ((wd->sc_flags & WDF_LOADED) == 0)
    960 		return EIO;
    961 
    962 	switch (xfer) {
    963 #ifdef HAS_BAD144_HANDLING
    964 	case DIOCSBAD:
    965 		if ((flag & FWRITE) == 0)
    966 			return EBADF;
    967 		wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
    968 		wd->sc_dk.dk_label->d_flags |= D_BADSECT;
    969 		bad144intern(wd);
    970 		return 0;
    971 #endif
    972 
    973 	case DIOCGDINFO:
    974 		*(struct disklabel *)addr = *(wd->sc_dk.dk_label);
    975 		return 0;
    976 #ifdef __HAVE_OLD_DISKLABEL
    977 	case ODIOCGDINFO:
    978 		newlabel = *(wd->sc_dk.dk_label);
    979 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    980 			return ENOTTY;
    981 		memcpy(addr, &newlabel, sizeof (struct olddisklabel));
    982 		return 0;
    983 #endif
    984 
    985 	case DIOCGPART:
    986 		((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
    987 		((struct partinfo *)addr)->part =
    988 		    &wd->sc_dk.dk_label->d_partitions[WDPART(rdev)];
    989 		return 0;
    990 
    991 	case DIOCWDINFO:
    992 	case DIOCSDINFO:
    993 #ifdef __HAVE_OLD_DISKLABEL
    994 	case ODIOCWDINFO:
    995 	case ODIOCSDINFO:
    996 #endif
    997 	{
    998 		struct disklabel *lp;
    999 
   1000 #ifdef __HAVE_OLD_DISKLABEL
   1001 		if (xfer == ODIOCSDINFO || xfer == ODIOCWDINFO) {
   1002 			memset(&newlabel, 0, sizeof newlabel);
   1003 			memcpy(&newlabel, addr, sizeof (struct olddisklabel));
   1004 			lp = &newlabel;
   1005 		} else
   1006 #endif
   1007 		lp = (struct disklabel *)addr;
   1008 
   1009 		if ((flag & FWRITE) == 0)
   1010 			return EBADF;
   1011 
   1012 		if ((error = wdlock(wd)) != 0)
   1013 			return error;
   1014 		wd->sc_flags |= WDF_LABELLING;
   1015 
   1016 		error = setdisklabel(wd->sc_dk.dk_label,
   1017 		    lp, /*wd->sc_dk.dk_openmask : */0,
   1018 		    wd->sc_dk.dk_cpulabel);
   1019 		if (error == 0) {
   1020 			if (wd->drvp->state > RECAL)
   1021 				wd->drvp->drive_flags |= DRIVE_RESET;
   1022 			if (xfer == DIOCWDINFO
   1023 #ifdef __HAVE_OLD_DISKLABEL
   1024 			    || xfer == ODIOCWDINFO
   1025 #endif
   1026 			    )
   1027 				error = writedisklabel(devvp,
   1028 				    wdstrategy, wd->sc_dk.dk_label,
   1029 				    wd->sc_dk.dk_cpulabel);
   1030 		}
   1031 
   1032 		wd->sc_flags &= ~WDF_LABELLING;
   1033 		wdunlock(wd);
   1034 		return error;
   1035 	}
   1036 
   1037 	case DIOCKLABEL:
   1038 		if (*(int *)addr)
   1039 			wd->sc_flags |= WDF_KLABEL;
   1040 		else
   1041 			wd->sc_flags &= ~WDF_KLABEL;
   1042 		return 0;
   1043 
   1044 	case DIOCWLABEL:
   1045 		if ((flag & FWRITE) == 0)
   1046 			return EBADF;
   1047 		if (*(int *)addr)
   1048 			wd->sc_flags |= WDF_WLABEL;
   1049 		else
   1050 			wd->sc_flags &= ~WDF_WLABEL;
   1051 		return 0;
   1052 
   1053 	case DIOCGDEFLABEL:
   1054 		wdgetdefaultlabel(wd, (struct disklabel *)addr);
   1055 		return 0;
   1056 #ifdef __HAVE_OLD_DISKLABEL
   1057 	case ODIOCGDEFLABEL:
   1058 		wdgetdefaultlabel(wd, &newlabel);
   1059 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
   1060 			return ENOTTY;
   1061 		memcpy(addr, &newlabel, sizeof (struct olddisklabel));
   1062 		return 0;
   1063 #endif
   1064 
   1065 #ifdef notyet
   1066 	case DIOCWFORMAT:
   1067 		if ((flag & FWRITE) == 0)
   1068 			return EBADF;
   1069 		{
   1070 		register struct format_op *fop;
   1071 		struct iovec aiov;
   1072 		struct uio auio;
   1073 
   1074 		fop = (struct format_op *)addr;
   1075 		aiov.iov_base = fop->df_buf;
   1076 		aiov.iov_len = fop->df_count;
   1077 		auio.uio_iov = &aiov;
   1078 		auio.uio_iovcnt = 1;
   1079 		auio.uio_resid = fop->df_count;
   1080 		auio.uio_segflg = 0;
   1081 		auio.uio_offset =
   1082 			fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
   1083 		auio.uio_procp = p;
   1084 		error = physio(wdformat, NULL, dev, B_WRITE, minphys,
   1085 		    &auio);
   1086 		fop->df_count -= auio.uio_resid;
   1087 		fop->df_reg[0] = wdc->sc_status;
   1088 		fop->df_reg[1] = wdc->sc_error;
   1089 		return error;
   1090 		}
   1091 #endif
   1092 
   1093 	case ATAIOCCOMMAND:
   1094 		/*
   1095 		 * Make sure this command is (relatively) safe first
   1096 		 */
   1097 		if ((((atareq_t *) addr)->flags & ATACMD_READ) == 0 &&
   1098 		    (flag & FWRITE) == 0)
   1099 			return (EBADF);
   1100 		{
   1101 		struct wd_ioctl *wi;
   1102 		atareq_t *atareq = (atareq_t *) addr;
   1103 		int error;
   1104 
   1105 		wi = wi_get();
   1106 		wi->wi_softc = wd;
   1107 		wi->wi_atareq = *atareq;
   1108 
   1109 		if (atareq->datalen && atareq->flags &
   1110 		    (ATACMD_READ | ATACMD_WRITE)) {
   1111 			wi->wi_iov.iov_base = atareq->databuf;
   1112 			wi->wi_iov.iov_len = atareq->datalen;
   1113 			wi->wi_uio.uio_iov = &wi->wi_iov;
   1114 			wi->wi_uio.uio_iovcnt = 1;
   1115 			wi->wi_uio.uio_resid = atareq->datalen;
   1116 			wi->wi_uio.uio_offset = 0;
   1117 			wi->wi_uio.uio_segflg = UIO_USERSPACE;
   1118 			wi->wi_uio.uio_rw =
   1119 			    (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE;
   1120 			wi->wi_uio.uio_procp = p;
   1121 			error = physio(wdioctlstrategy, &wi->wi_bp, devvp,
   1122 			    (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE,
   1123 			    minphys, &wi->wi_uio);
   1124 		} else {
   1125 			/* No need to call physio if we don't have any
   1126 			   user data */
   1127 			wi->wi_bp.b_flags = 0;
   1128 			wi->wi_bp.b_data = 0;
   1129 			wi->wi_bp.b_bcount = 0;
   1130 			wi->wi_bp.b_devvp = 0;
   1131 			wi->wi_bp.b_proc = p;
   1132 			wdioctlstrategy(&wi->wi_bp);
   1133 			error = wi->wi_bp.b_error;
   1134 		}
   1135 		*atareq = wi->wi_atareq;
   1136 		wi_free(wi);
   1137 		return(error);
   1138 		}
   1139 
   1140 	default:
   1141 		return ENOTTY;
   1142 	}
   1143 
   1144 #ifdef DIAGNOSTIC
   1145 	panic("wdioctl: impossible");
   1146 #endif
   1147 }
   1148 
   1149 #ifdef B_FORMAT
   1150 int
   1151 wdformat(struct buf *bp)
   1152 {
   1153 
   1154 	bp->b_flags |= B_FORMAT;
   1155 	return wdstrategy(bp);
   1156 }
   1157 #endif
   1158 
   1159 int
   1160 wdsize(dev)
   1161 	dev_t dev;
   1162 {
   1163 	struct wd_softc *wd;
   1164 	struct vnode *vp;
   1165 	int part, omask;
   1166 	int size;
   1167 
   1168 	WDCDEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS);
   1169 
   1170 	wd = device_lookup(&wd_cd, WDUNIT(dev));
   1171 	if (wd == NULL)
   1172 		return (-1);
   1173 
   1174 	part = WDPART(dev);
   1175 	omask = wd->sc_dk.dk_openmask & (1 << part);
   1176 
   1177 	/* XXXDEVVP */
   1178 
   1179 	if (omask == 0) {
   1180 		if (bdevvp(dev, &vp) != 0)
   1181 			return (-1);
   1182 		if (wdopen(vp, 0, S_IFBLK, NULL) != 0) {
   1183 			vrele(vp);
   1184 			return (-1);
   1185 		}
   1186 	}
   1187 
   1188 	if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
   1189 		size = -1;
   1190 	else
   1191 		size = wd->sc_dk.dk_label->d_partitions[part].p_size *
   1192 		    (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
   1193 
   1194 	if (omask == 0) {
   1195 		if (wdclose(vp, 0, S_IFBLK, NULL) != 0)
   1196 			size = -1;
   1197 		vrele(vp);
   1198 	}
   1199 	return (size);
   1200 }
   1201 
   1202 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
   1203 static int wddoingadump = 0;
   1204 static int wddumprecalibrated = 0;
   1205 static int wddumpmulti = 1;
   1206 
   1207 /*
   1208  * Dump core after a system crash.
   1209  */
   1210 int
   1211 wddump(dev, blkno, va, size)
   1212 	dev_t dev;
   1213 	daddr_t blkno;
   1214 	caddr_t va;
   1215 	size_t size;
   1216 {
   1217 	struct wd_softc *wd;	/* disk unit to do the I/O */
   1218 	struct disklabel *lp;   /* disk's disklabel */
   1219 	int part, err;
   1220 	int nblks;	/* total number of sectors left to write */
   1221 	char errbuf[256];
   1222 
   1223 	/* Check if recursive dump; if so, punt. */
   1224 	if (wddoingadump)
   1225 		return EFAULT;
   1226 	wddoingadump = 1;
   1227 
   1228 	wd = device_lookup(&wd_cd, WDUNIT(dev));
   1229 	if (wd == NULL)
   1230 		return (ENXIO);
   1231 
   1232 	part = WDPART(dev);
   1233 
   1234 	/* Make sure it was initialized. */
   1235 	if (wd->drvp->state < READY)
   1236 		return ENXIO;
   1237 
   1238 	/* Convert to disk sectors.  Request must be a multiple of size. */
   1239 	lp = wd->sc_dk.dk_label;
   1240 	if ((size % lp->d_secsize) != 0)
   1241 		return EFAULT;
   1242 	nblks = size / lp->d_secsize;
   1243 	blkno = blkno / (lp->d_secsize / DEV_BSIZE);
   1244 
   1245 	/* Check transfer bounds against partition size. */
   1246 	if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
   1247 		return EINVAL;
   1248 
   1249 	/* Offset block number to start of partition. */
   1250 	blkno += lp->d_partitions[part].p_offset;
   1251 
   1252 	/* Recalibrate, if first dump transfer. */
   1253 	if (wddumprecalibrated == 0) {
   1254 		wddumpmulti = wd->sc_multi;
   1255 		wddumprecalibrated = 1;
   1256 		wd->drvp->state = RESET;
   1257 	}
   1258 
   1259 	while (nblks > 0) {
   1260 again:
   1261 		wd->sc_bp = NULL;
   1262 		wd->sc_wdc_bio.blkno = blkno;
   1263 		wd->sc_wdc_bio.flags = ATA_POLL;
   1264 		if (wddumpmulti == 1)
   1265 			wd->sc_wdc_bio.flags |= ATA_SINGLE;
   1266 		if (wd->sc_flags & WDF_LBA)
   1267 			wd->sc_wdc_bio.flags |= ATA_LBA;
   1268 		wd->sc_wdc_bio.bcount =
   1269 			min(nblks, wddumpmulti) * lp->d_secsize;
   1270 		wd->sc_wdc_bio.databuf = va;
   1271 #ifndef WD_DUMP_NOT_TRUSTED
   1272 		switch (wdc_ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
   1273 		case WDC_TRY_AGAIN:
   1274 			panic("wddump: try again");
   1275 			break;
   1276 		case WDC_QUEUED:
   1277 			panic("wddump: polled command has been queued");
   1278 			break;
   1279 		case WDC_COMPLETE:
   1280 			break;
   1281 		}
   1282 		switch(wd->sc_wdc_bio.error) {
   1283 		case TIMEOUT:
   1284 			printf("wddump: device timed out");
   1285 			err = EIO;
   1286 			break;
   1287 		case ERR_DF:
   1288 			printf("wddump: drive fault");
   1289 			err = EIO;
   1290 			break;
   1291 		case ERR_DMA:
   1292 			printf("wddump: DMA error");
   1293 			err = EIO;
   1294 			break;
   1295 		case ERROR:
   1296 			errbuf[0] = '\0';
   1297 			ata_perror(wd->drvp, wd->sc_wdc_bio.r_error, errbuf);
   1298 			printf("wddump: %s", errbuf);
   1299 			err = EIO;
   1300 			break;
   1301 		case NOERROR:
   1302 			err = 0;
   1303 			break;
   1304 		default:
   1305 			panic("wddump: unknown error type");
   1306 		}
   1307 		if (err != 0) {
   1308 			if (wddumpmulti != 1) {
   1309 				wddumpmulti = 1; /* retry in single-sector */
   1310 				printf(", retrying\n");
   1311 				goto again;
   1312 			}
   1313 			printf("\n");
   1314 			return err;
   1315 		}
   1316 #else	/* WD_DUMP_NOT_TRUSTED */
   1317 		/* Let's just talk about this first... */
   1318 		printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
   1319 		    unit, va, cylin, head, sector);
   1320 		delay(500 * 1000);	/* half a second */
   1321 #endif
   1322 
   1323 		/* update block count */
   1324 		nblks -= min(nblks, wddumpmulti);
   1325 		blkno += min(nblks, wddumpmulti);
   1326 		va += min(nblks, wddumpmulti) * lp->d_secsize;
   1327 	}
   1328 
   1329 	wddoingadump = 0;
   1330 	return 0;
   1331 }
   1332 
   1333 #ifdef HAS_BAD144_HANDLING
   1334 /*
   1335  * Internalize the bad sector table.
   1336  */
   1337 void
   1338 bad144intern(wd)
   1339 	struct wd_softc *wd;
   1340 {
   1341 	struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
   1342 	struct disklabel *lp = wd->sc_dk.dk_label;
   1343 	int i = 0;
   1344 
   1345 	WDCDEBUG_PRINT(("bad144intern\n"), DEBUG_XFERS);
   1346 
   1347 	for (; i < NBT_BAD; i++) {
   1348 		if (bt->bt_bad[i].bt_cyl == 0xffff)
   1349 			break;
   1350 		wd->sc_badsect[i] =
   1351 		    bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
   1352 		    (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
   1353 		    (bt->bt_bad[i].bt_trksec & 0xff);
   1354 	}
   1355 	for (; i < NBT_BAD+1; i++)
   1356 		wd->sc_badsect[i] = -1;
   1357 }
   1358 #endif
   1359 
   1360 int
   1361 wd_get_params(wd, flags, params)
   1362 	struct wd_softc *wd;
   1363 	u_int8_t flags;
   1364 	struct ataparams *params;
   1365 {
   1366 	switch (ata_get_params(wd->drvp, flags, params)) {
   1367 	case CMD_AGAIN:
   1368 		return 1;
   1369 	case CMD_ERR:
   1370 		/*
   1371 		 * We `know' there's a drive here; just assume it's old.
   1372 		 * This geometry is only used to read the MBR and print a
   1373 		 * (false) attach message.
   1374 		 */
   1375 		strncpy(params->atap_model, "ST506",
   1376 		    sizeof params->atap_model);
   1377 		params->atap_config = ATA_CFG_FIXED;
   1378 		params->atap_cylinders = 1024;
   1379 		params->atap_heads = 8;
   1380 		params->atap_sectors = 17;
   1381 		params->atap_multi = 1;
   1382 		params->atap_capabilities1 = params->atap_capabilities2 = 0;
   1383 		wd->drvp->ata_vers = -1; /* Mark it as pre-ATA */
   1384 		return 0;
   1385 	case CMD_OK:
   1386 		return 0;
   1387 	default:
   1388 		panic("wd_get_params: bad return code from ata_get_params");
   1389 		/* NOTREACHED */
   1390 	}
   1391 }
   1392 
   1393 void
   1394 wd_flushcache(wd, flags)
   1395 	struct wd_softc *wd;
   1396 	int flags;
   1397 {
   1398 	struct wdc_command wdc_c;
   1399 
   1400 	if (wd->drvp->ata_vers < 4) /* WDCC_FLUSHCACHE is here since ATA-4 */
   1401 		return;
   1402 	memset(&wdc_c, 0, sizeof(struct wdc_command));
   1403 	wdc_c.r_command = WDCC_FLUSHCACHE;
   1404 	wdc_c.r_st_bmask = WDCS_DRDY;
   1405 	wdc_c.r_st_pmask = WDCS_DRDY;
   1406 	wdc_c.flags = flags;
   1407 	wdc_c.timeout = 30000; /* 30s timeout */
   1408 	if (wdc_exec_command(wd->drvp, &wdc_c) != WDC_COMPLETE) {
   1409 		printf("%s: flush cache command didn't complete\n",
   1410 		    wd->sc_dev.dv_xname);
   1411 	}
   1412 	if (wdc_c.flags & AT_TIMEOU) {
   1413 		printf("%s: flush cache command timeout\n",
   1414 		    wd->sc_dev.dv_xname);
   1415 	}
   1416 	if (wdc_c.flags & AT_DF) {
   1417 		printf("%s: flush cache command: drive fault\n",
   1418 		    wd->sc_dev.dv_xname);
   1419 	}
   1420 	/*
   1421 	 * Ignore error register, it shouldn't report anything else
   1422 	 * than COMMAND ABORTED, which means the device doesn't support
   1423 	 * flush cache
   1424 	 */
   1425 }
   1426 
   1427 void
   1428 wd_shutdown(arg)
   1429 	void *arg;
   1430 {
   1431 	struct wd_softc *wd = arg;
   1432 	wd_flushcache(wd, AT_POLL);
   1433 }
   1434 
   1435 /*
   1436  * Allocate space for a ioctl queue structure.  Mostly taken from
   1437  * scsipi_ioctl.c
   1438  */
   1439 struct wd_ioctl *
   1440 wi_get()
   1441 {
   1442 	struct wd_ioctl *wi;
   1443 	int s;
   1444 
   1445 	wi = malloc(sizeof(struct wd_ioctl), M_TEMP, M_WAITOK);
   1446 	memset(wi, 0, sizeof (struct wd_ioctl));
   1447 	s = splbio();
   1448 	LIST_INSERT_HEAD(&wi_head, wi, wi_list);
   1449 	splx(s);
   1450 	return (wi);
   1451 }
   1452 
   1453 /*
   1454  * Free an ioctl structure and remove it from our list
   1455  */
   1456 
   1457 void
   1458 wi_free(wi)
   1459 	struct wd_ioctl *wi;
   1460 {
   1461 	int s;
   1462 
   1463 	s = splbio();
   1464 	LIST_REMOVE(wi, wi_list);
   1465 	splx(s);
   1466 	free(wi, M_TEMP);
   1467 }
   1468 
   1469 /*
   1470  * Find a wd_ioctl structure based on the struct buf.
   1471  */
   1472 
   1473 struct wd_ioctl *
   1474 wi_find(bp)
   1475 	struct buf *bp;
   1476 {
   1477 	struct wd_ioctl *wi;
   1478 	int s;
   1479 
   1480 	s = splbio();
   1481 	for (wi = wi_head.lh_first; wi != 0; wi = wi->wi_list.le_next)
   1482 		if (bp == &wi->wi_bp)
   1483 			break;
   1484 	splx(s);
   1485 	return (wi);
   1486 }
   1487 
   1488 /*
   1489  * Ioctl pseudo strategy routine
   1490  *
   1491  * This is mostly stolen from scsipi_ioctl.c:scsistrategy().  What
   1492  * happens here is:
   1493  *
   1494  * - wdioctl() queues a wd_ioctl structure.
   1495  *
   1496  * - wdioctl() calls physio/wdioctlstrategy based on whether or not
   1497  *   user space I/O is required.  If physio() is called, physio() eventually
   1498  *   calls wdioctlstrategy().
   1499  *
   1500  * - In either case, wdioctlstrategy() calls wdc_exec_command()
   1501  *   to perform the actual command
   1502  *
   1503  * The reason for the use of the pseudo strategy routine is because
   1504  * when doing I/O to/from user space, physio _really_ wants to be in
   1505  * the loop.  We could put the entire buffer into the ioctl request
   1506  * structure, but that won't scale if we want to do things like download
   1507  * microcode.
   1508  */
   1509 
   1510 void
   1511 wdioctlstrategy(bp)
   1512 	struct buf *bp;
   1513 {
   1514 	struct wd_ioctl *wi;
   1515 	struct wdc_command wdc_c;
   1516 	int error = 0;
   1517 
   1518 	wi = wi_find(bp);
   1519 	if (wi == NULL) {
   1520 		printf("user_strat: No ioctl\n");
   1521 		error = EINVAL;
   1522 		goto bad;
   1523 	}
   1524 
   1525 	memset(&wdc_c, 0, sizeof(wdc_c));
   1526 
   1527 	/*
   1528 	 * Abort if physio broke up the transfer
   1529 	 */
   1530 
   1531 	if (bp->b_bcount != wi->wi_atareq.datalen) {
   1532 		printf("physio split wd ioctl request... cannot proceed\n");
   1533 		error = EIO;
   1534 		goto bad;
   1535 	}
   1536 
   1537 	/*
   1538 	 * Abort if we didn't get a buffer size that was a multiple of
   1539 	 * our sector size (or was larger than NBBY)
   1540 	 */
   1541 
   1542 	if ((bp->b_bcount % wi->wi_softc->sc_dk.dk_label->d_secsize) != 0 ||
   1543 	    (bp->b_bcount / wi->wi_softc->sc_dk.dk_label->d_secsize) >=
   1544 	     (1 << NBBY)) {
   1545 		error = EINVAL;
   1546 		goto bad;
   1547 	}
   1548 
   1549 	/*
   1550 	 * Make sure a timeout was supplied in the ioctl request
   1551 	 */
   1552 
   1553 	if (wi->wi_atareq.timeout == 0) {
   1554 		error = EINVAL;
   1555 		goto bad;
   1556 	}
   1557 
   1558 	if (wi->wi_atareq.flags & ATACMD_READ)
   1559 		wdc_c.flags |= AT_READ;
   1560 	else if (wi->wi_atareq.flags & ATACMD_WRITE)
   1561 		wdc_c.flags |= AT_WRITE;
   1562 
   1563 	if (wi->wi_atareq.flags & ATACMD_READREG)
   1564 		wdc_c.flags |= AT_READREG;
   1565 
   1566 	wdc_c.flags |= AT_WAIT;
   1567 
   1568 	wdc_c.timeout = wi->wi_atareq.timeout;
   1569 	wdc_c.r_command = wi->wi_atareq.command;
   1570 	wdc_c.r_head = wi->wi_atareq.head & 0x0f;
   1571 	wdc_c.r_cyl = wi->wi_atareq.cylinder;
   1572 	wdc_c.r_sector = wi->wi_atareq.sec_num;
   1573 	wdc_c.r_count = wi->wi_atareq.sec_count;
   1574 	wdc_c.r_precomp = wi->wi_atareq.features;
   1575 	wdc_c.r_st_bmask = WDCS_DRDY;
   1576 	wdc_c.r_st_pmask = WDCS_DRDY;
   1577 	wdc_c.data = wi->wi_bp.b_data;
   1578 	wdc_c.bcount = wi->wi_bp.b_bcount;
   1579 
   1580 	if (wdc_exec_command(wi->wi_softc->drvp, &wdc_c) != WDC_COMPLETE) {
   1581 		wi->wi_atareq.retsts = ATACMD_ERROR;
   1582 		goto bad;
   1583 	}
   1584 
   1585 	if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
   1586 		if (wdc_c.flags & AT_ERROR) {
   1587 			wi->wi_atareq.retsts = ATACMD_ERROR;
   1588 			wi->wi_atareq.error = wdc_c.r_error;
   1589 		} else if (wdc_c.flags & AT_DF)
   1590 			wi->wi_atareq.retsts = ATACMD_DF;
   1591 		else
   1592 			wi->wi_atareq.retsts = ATACMD_TIMEOUT;
   1593 	} else {
   1594 		wi->wi_atareq.retsts = ATACMD_OK;
   1595 		if (wi->wi_atareq.flags & ATACMD_READREG) {
   1596 			wi->wi_atareq.head = wdc_c.r_head ;
   1597 			wi->wi_atareq.cylinder = wdc_c.r_cyl;
   1598 			wi->wi_atareq.sec_num = wdc_c.r_sector;
   1599 			wi->wi_atareq.sec_count = wdc_c.r_count;
   1600 			wi->wi_atareq.features = wdc_c.r_precomp;
   1601 			wi->wi_atareq.error = wdc_c.r_error;
   1602 		}
   1603 	}
   1604 
   1605 	bp->b_error = 0;
   1606 	biodone(bp);
   1607 	return;
   1608 bad:
   1609 	bp->b_flags |= B_ERROR;
   1610 	bp->b_error = error;
   1611 	biodone(bp);
   1612 }
   1613