Home | History | Annotate | Line # | Download | only in ata
wd.c revision 1.205.2.2
      1 /*	$NetBSD: wd.c,v 1.205.2.2 2001/05/06 20:48:42 he 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 <vm/vm.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 wd_softc *));
    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 = wd_cd.cd_devs[WDUNIT(bp->b_dev)];
    439 	struct disklabel *lp = wd->sc_dk.dk_label;
    440 	daddr_t blkno;
    441 	int s;
    442 
    443 	WDCDEBUG_PRINT(("wdstrategy (%s)\n", wd->sc_dev.dv_xname),
    444 	    DEBUG_XFERS);
    445 
    446 	/* Valid request?  */
    447 	if (bp->b_blkno < 0 ||
    448 	    (bp->b_bcount % lp->d_secsize) != 0 ||
    449 	    (bp->b_bcount / lp->d_secsize) >= (1 << NBBY)) {
    450 		bp->b_error = EINVAL;
    451 		goto bad;
    452 	}
    453 
    454 	/* If device invalidated (e.g. media change, door open), error. */
    455 	if ((wd->sc_flags & WDF_LOADED) == 0) {
    456 		bp->b_error = EIO;
    457 		goto bad;
    458 	}
    459 
    460 	/* If it's a null transfer, return immediately. */
    461 	if (bp->b_bcount == 0)
    462 		goto done;
    463 
    464 	/*
    465 	 * Do bounds checking, adjust transfer. if error, process.
    466 	 * If end of partition, just return.
    467 	 */
    468 	if (WDPART(bp->b_dev) != RAW_PART &&
    469 	    bounds_check_with_label(bp, wd->sc_dk.dk_label,
    470 	    (wd->sc_flags & (WDF_WLABEL|WDF_LABELLING)) != 0) <= 0)
    471 		goto done;
    472 
    473 	/*
    474 	 * Now convert the block number to absolute and put it in
    475 	 * terms of the device's logical block size.
    476 	 */
    477 	if (lp->d_secsize >= DEV_BSIZE)
    478 		blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
    479 	else
    480 		blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
    481 
    482 	if (WDPART(bp->b_dev) != RAW_PART)
    483 		blkno += lp->d_partitions[WDPART(bp->b_dev)].p_offset;
    484 
    485 	bp->b_rawblkno = blkno;
    486 
    487 	/* Queue transfer on drive, activate drive and controller if idle. */
    488 	s = splbio();
    489 	disksort_blkno(&wd->sc_q, bp);
    490 	wdstart(wd);
    491 	splx(s);
    492 	return;
    493 bad:
    494 	bp->b_flags |= B_ERROR;
    495 done:
    496 	/* Toss transfer; we're done early. */
    497 	bp->b_resid = bp->b_bcount;
    498 	biodone(bp);
    499 }
    500 
    501 /*
    502  * Queue a drive for I/O.
    503  */
    504 void
    505 wdstart(arg)
    506 	void *arg;
    507 {
    508 	struct wd_softc *wd = arg;
    509 	struct buf *bp = NULL;
    510 
    511 	WDCDEBUG_PRINT(("wdstart %s\n", wd->sc_dev.dv_xname),
    512 	    DEBUG_XFERS);
    513 	while (wd->openings > 0) {
    514 
    515 		/* Is there a buf for us ? */
    516 		if ((bp = BUFQ_FIRST(&wd->sc_q)) == NULL)
    517 			return;
    518 		BUFQ_REMOVE(&wd->sc_q, bp);
    519 
    520 		/*
    521 		 * Make the command. First lock the device
    522 		 */
    523 		wd->openings--;
    524 
    525 		wd->retries = 0;
    526 		__wdstart(wd, bp);
    527 	}
    528 }
    529 
    530 void
    531 __wdstart(wd, bp)
    532 	struct wd_softc *wd;
    533 	struct buf *bp;
    534 {
    535 
    536 	wd->sc_wdc_bio.blkno = bp->b_rawblkno;
    537 	wd->sc_wdc_bio.blkdone =0;
    538 	wd->sc_bp = bp;
    539 	/*
    540 	 * If we're retrying, retry in single-sector mode. This will give us
    541 	 * the sector number of the problem, and will eventually allow the
    542 	 * transfer to succeed.
    543 	 */
    544 	if (wd->sc_multi == 1 || wd->retries >= WDIORETRIES_SINGLE)
    545 		wd->sc_wdc_bio.flags = ATA_SINGLE;
    546 	else
    547 		wd->sc_wdc_bio.flags = 0;
    548 	if (wd->sc_flags & WDF_LBA)
    549 		wd->sc_wdc_bio.flags |= ATA_LBA;
    550 	if (bp->b_flags & B_READ)
    551 		wd->sc_wdc_bio.flags |= ATA_READ;
    552 	wd->sc_wdc_bio.bcount = bp->b_bcount;
    553 	wd->sc_wdc_bio.databuf = bp->b_data;
    554 	/* Instrumentation. */
    555 	disk_busy(&wd->sc_dk);
    556 	switch (wdc_ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
    557 	case WDC_TRY_AGAIN:
    558 		callout_reset(&wd->sc_restart_ch, hz, wdrestart, wd);
    559 		break;
    560 	case WDC_QUEUED:
    561 	case WDC_COMPLETE:
    562 		break;
    563 	default:
    564 		panic("__wdstart: bad return code from wdc_ata_bio()");
    565 	}
    566 }
    567 
    568 void
    569 wddone(v)
    570 	void *v;
    571 {
    572 	struct wd_softc *wd = v;
    573 	struct buf *bp = wd->sc_bp;
    574 	char buf[256], *errbuf = buf;
    575 	WDCDEBUG_PRINT(("wddone %s\n", wd->sc_dev.dv_xname),
    576 	    DEBUG_XFERS);
    577 
    578 	bp->b_resid = wd->sc_wdc_bio.bcount;
    579 	errbuf[0] = '\0';
    580 	switch (wd->sc_wdc_bio.error) {
    581 	case ERR_DMA:
    582 		errbuf = "DMA error";
    583 		goto retry;
    584 	case ERR_DF:
    585 		errbuf = "device fault";
    586 		goto retry;
    587 	case TIMEOUT:
    588 		errbuf = "device timeout";
    589 		goto retry;
    590 	case ERROR:
    591 		/* Don't care about media change bits */
    592 		if (wd->sc_wdc_bio.r_error != 0 &&
    593 		    (wd->sc_wdc_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0)
    594 			goto noerror;
    595 		ata_perror(wd->drvp, wd->sc_wdc_bio.r_error, errbuf);
    596 retry:		/* Just reset and retry. Can we do more ? */
    597 		wdc_reset_channel(wd->drvp);
    598 		diskerr(bp, "wd", errbuf, LOG_PRINTF,
    599 		    wd->sc_wdc_bio.blkdone, wd->sc_dk.dk_label);
    600 		if (wd->retries++ < WDIORETRIES) {
    601 			printf(", retrying\n");
    602 			callout_reset(&wd->sc_restart_ch, RECOVERYTIME,
    603 			    wdrestart, wd);
    604 			return;
    605 		}
    606 		printf("\n");
    607 		bp->b_flags |= B_ERROR;
    608 		bp->b_error = EIO;
    609 		break;
    610 	case NOERROR:
    611 noerror:	if ((wd->sc_wdc_bio.flags & ATA_CORR) || wd->retries > 0)
    612 			printf("%s: soft error (corrected)\n",
    613 			    wd->sc_dev.dv_xname);
    614 		break;
    615 	case ERR_NODEV:
    616 		bp->b_flags |= B_ERROR;
    617 		bp->b_error = EIO;
    618 		break;
    619 	}
    620 	disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid));
    621 #if NRND > 0
    622 	rnd_add_uint32(&wd->rnd_source, bp->b_blkno);
    623 #endif
    624 	biodone(bp);
    625 	wd->openings++;
    626 	wdstart(wd);
    627 }
    628 
    629 void
    630 wdrestart(v)
    631 	void *v;
    632 {
    633 	struct wd_softc *wd = v;
    634 	struct buf *bp = wd->sc_bp;
    635 	int s;
    636 	WDCDEBUG_PRINT(("wdrestart %s\n", wd->sc_dev.dv_xname),
    637 	    DEBUG_XFERS);
    638 
    639 	s = splbio();
    640 	__wdstart(v, bp);
    641 	splx(s);
    642 }
    643 
    644 int
    645 wdread(dev, uio, flags)
    646 	dev_t dev;
    647 	struct uio *uio;
    648 	int flags;
    649 {
    650 
    651 	WDCDEBUG_PRINT(("wdread\n"), DEBUG_XFERS);
    652 	return (physio(wdstrategy, NULL, dev, B_READ, minphys, uio));
    653 }
    654 
    655 int
    656 wdwrite(dev, uio, flags)
    657 	dev_t dev;
    658 	struct uio *uio;
    659 	int flags;
    660 {
    661 
    662 	WDCDEBUG_PRINT(("wdwrite\n"), DEBUG_XFERS);
    663 	return (physio(wdstrategy, NULL, dev, B_WRITE, minphys, uio));
    664 }
    665 
    666 /*
    667  * Wait interruptibly for an exclusive lock.
    668  *
    669  * XXX
    670  * Several drivers do this; it should be abstracted and made MP-safe.
    671  */
    672 int
    673 wdlock(wd)
    674 	struct wd_softc *wd;
    675 {
    676 	int error;
    677 	int s;
    678 
    679 	WDCDEBUG_PRINT(("wdlock\n"), DEBUG_FUNCS);
    680 
    681 	s = splbio();
    682 
    683 	while ((wd->sc_flags & WDF_LOCKED) != 0) {
    684 		wd->sc_flags |= WDF_WANTED;
    685 		if ((error = tsleep(wd, PRIBIO | PCATCH,
    686 		    "wdlck", 0)) != 0) {
    687 			splx(s);
    688 			return error;
    689 		}
    690 	}
    691 	wd->sc_flags |= WDF_LOCKED;
    692 	splx(s);
    693 	return 0;
    694 }
    695 
    696 /*
    697  * Unlock and wake up any waiters.
    698  */
    699 void
    700 wdunlock(wd)
    701 	struct wd_softc *wd;
    702 {
    703 
    704 	WDCDEBUG_PRINT(("wdunlock\n"), DEBUG_FUNCS);
    705 
    706 	wd->sc_flags &= ~WDF_LOCKED;
    707 	if ((wd->sc_flags & WDF_WANTED) != 0) {
    708 		wd->sc_flags &= ~WDF_WANTED;
    709 		wakeup(wd);
    710 	}
    711 }
    712 
    713 int
    714 wdopen(dev, flag, fmt, p)
    715 	dev_t dev;
    716 	int flag, fmt;
    717 	struct proc *p;
    718 {
    719 	struct wd_softc *wd;
    720 	int unit, part;
    721 	int error;
    722 
    723 	WDCDEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS);
    724 	unit = WDUNIT(dev);
    725 	if (unit >= wd_cd.cd_ndevs)
    726 		return ENXIO;
    727 	wd = wd_cd.cd_devs[unit];
    728 	if (wd == NULL)
    729 		return ENXIO;
    730 
    731 	/*
    732 	 * If this is the first open of this device, add a reference
    733 	 * to the adapter.
    734 	 */
    735 	if (wd->sc_dk.dk_openmask == 0 &&
    736 	    (error = wdc_ata_addref(wd->drvp)) != 0)
    737 		return (error);
    738 
    739 	if ((error = wdlock(wd)) != 0)
    740 		goto bad4;
    741 
    742 	if (wd->sc_dk.dk_openmask != 0) {
    743 		/*
    744 		 * If any partition is open, but the disk has been invalidated,
    745 		 * disallow further opens.
    746 		 */
    747 		if ((wd->sc_flags & WDF_LOADED) == 0) {
    748 			error = EIO;
    749 			goto bad3;
    750 		}
    751 	} else {
    752 		if ((wd->sc_flags & WDF_LOADED) == 0) {
    753 			wd->sc_flags |= WDF_LOADED;
    754 
    755 			/* Load the physical device parameters. */
    756 			wd_get_params(wd, AT_WAIT, &wd->sc_params);
    757 
    758 			/* Load the partition info if not already loaded. */
    759 			wdgetdisklabel(wd);
    760 		}
    761 	}
    762 
    763 	part = WDPART(dev);
    764 
    765 	/* Check that the partition exists. */
    766 	if (part != RAW_PART &&
    767 	    (part >= wd->sc_dk.dk_label->d_npartitions ||
    768 	     wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
    769 		error = ENXIO;
    770 		goto bad;
    771 	}
    772 
    773 	/* Insure only one open at a time. */
    774 	switch (fmt) {
    775 	case S_IFCHR:
    776 		wd->sc_dk.dk_copenmask |= (1 << part);
    777 		break;
    778 	case S_IFBLK:
    779 		wd->sc_dk.dk_bopenmask |= (1 << part);
    780 		break;
    781 	}
    782 	wd->sc_dk.dk_openmask =
    783 	    wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
    784 
    785 	wdunlock(wd);
    786 	return 0;
    787 
    788 bad:
    789 	if (wd->sc_dk.dk_openmask == 0) {
    790 	}
    791 
    792 bad3:
    793 	wdunlock(wd);
    794 bad4:
    795 	if (wd->sc_dk.dk_openmask == 0)
    796 		wdc_ata_delref(wd->drvp);
    797 	return error;
    798 }
    799 
    800 int
    801 wdclose(dev, flag, fmt, p)
    802 	dev_t dev;
    803 	int flag, fmt;
    804 	struct proc *p;
    805 {
    806 	struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(dev)];
    807 	int part = WDPART(dev);
    808 	int error;
    809 
    810 	WDCDEBUG_PRINT(("wdclose\n"), DEBUG_FUNCS);
    811 	if ((error = wdlock(wd)) != 0)
    812 		return error;
    813 
    814 	switch (fmt) {
    815 	case S_IFCHR:
    816 		wd->sc_dk.dk_copenmask &= ~(1 << part);
    817 		break;
    818 	case S_IFBLK:
    819 		wd->sc_dk.dk_bopenmask &= ~(1 << part);
    820 		break;
    821 	}
    822 	wd->sc_dk.dk_openmask =
    823 	    wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
    824 
    825 	if (wd->sc_dk.dk_openmask == 0) {
    826 		wd_flushcache(wd, AT_WAIT);
    827 		/* XXXX Must wait for I/O to complete! */
    828 
    829 		if (! (wd->sc_flags & WDF_KLABEL))
    830 			wd->sc_flags &= ~WDF_LOADED;
    831 
    832 		wdc_ata_delref(wd->drvp);
    833 	}
    834 
    835 	wdunlock(wd);
    836 	return 0;
    837 }
    838 
    839 void
    840 wdgetdefaultlabel(wd, lp)
    841 	struct wd_softc *wd;
    842 	struct disklabel *lp;
    843 {
    844 
    845 	WDCDEBUG_PRINT(("wdgetdefaultlabel\n"), DEBUG_FUNCS);
    846 	memset(lp, 0, sizeof(struct disklabel));
    847 
    848 	lp->d_secsize = DEV_BSIZE;
    849 	lp->d_ntracks = wd->sc_params.atap_heads;
    850 	lp->d_nsectors = wd->sc_params.atap_sectors;
    851 	lp->d_ncylinders = wd->sc_params.atap_cylinders;
    852 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    853 
    854 	if (strcmp(wd->sc_params.atap_model, "ST506") == 0)
    855 		lp->d_type = DTYPE_ST506;
    856 	else
    857 		lp->d_type = DTYPE_ESDI;
    858 
    859 	strncpy(lp->d_typename, wd->sc_params.atap_model, 16);
    860 	strncpy(lp->d_packname, "fictitious", 16);
    861 	lp->d_secperunit = wd->sc_capacity;
    862 	lp->d_rpm = 3600;
    863 	lp->d_interleave = 1;
    864 	lp->d_flags = 0;
    865 
    866 	lp->d_partitions[RAW_PART].p_offset = 0;
    867 	lp->d_partitions[RAW_PART].p_size =
    868 	lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
    869 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
    870 	lp->d_npartitions = RAW_PART + 1;
    871 
    872 	lp->d_magic = DISKMAGIC;
    873 	lp->d_magic2 = DISKMAGIC;
    874 	lp->d_checksum = dkcksum(lp);
    875 }
    876 
    877 /*
    878  * Fabricate a default disk label, and try to read the correct one.
    879  */
    880 void
    881 wdgetdisklabel(wd)
    882 	struct wd_softc *wd;
    883 {
    884 	struct disklabel *lp = wd->sc_dk.dk_label;
    885 	char *errstring;
    886 
    887 	WDCDEBUG_PRINT(("wdgetdisklabel\n"), DEBUG_FUNCS);
    888 
    889 	memset(wd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
    890 
    891 	wdgetdefaultlabel(wd, lp);
    892 
    893 	wd->sc_badsect[0] = -1;
    894 
    895 	if (wd->drvp->state > RECAL)
    896 		wd->drvp->drive_flags |= DRIVE_RESET;
    897 	errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
    898 	    wdstrategy, lp, wd->sc_dk.dk_cpulabel);
    899 	if (errstring) {
    900 		/*
    901 		 * This probably happened because the drive's default
    902 		 * geometry doesn't match the DOS geometry.  We
    903 		 * assume the DOS geometry is now in the label and try
    904 		 * again.  XXX This is a kluge.
    905 		 */
    906 		if (wd->drvp->state > RECAL)
    907 			wd->drvp->drive_flags |= DRIVE_RESET;
    908 		errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit,
    909 		    RAW_PART), wdstrategy, lp, wd->sc_dk.dk_cpulabel);
    910 	}
    911 	if (errstring) {
    912 		printf("%s: %s\n", wd->sc_dev.dv_xname, errstring);
    913 		return;
    914 	}
    915 
    916 	if (wd->drvp->state > RECAL)
    917 		wd->drvp->drive_flags |= DRIVE_RESET;
    918 #ifdef HAS_BAD144_HANDLING
    919 	if ((lp->d_flags & D_BADSECT) != 0)
    920 		bad144intern(wd);
    921 #endif
    922 }
    923 
    924 int
    925 wdioctl(dev, xfer, addr, flag, p)
    926 	dev_t dev;
    927 	u_long xfer;
    928 	caddr_t addr;
    929 	int flag;
    930 	struct proc *p;
    931 {
    932 	struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(dev)];
    933 	int error;
    934 #ifdef __HAVE_OLD_DISKLABEL
    935 	struct disklabel newlabel;
    936 #endif
    937 
    938 	WDCDEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS);
    939 
    940 	if ((wd->sc_flags & WDF_LOADED) == 0)
    941 		return EIO;
    942 
    943 	switch (xfer) {
    944 #ifdef HAS_BAD144_HANDLING
    945 	case DIOCSBAD:
    946 		if ((flag & FWRITE) == 0)
    947 			return EBADF;
    948 		wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
    949 		wd->sc_dk.dk_label->d_flags |= D_BADSECT;
    950 		bad144intern(wd);
    951 		return 0;
    952 #endif
    953 
    954 	case DIOCGDINFO:
    955 		*(struct disklabel *)addr = *(wd->sc_dk.dk_label);
    956 		return 0;
    957 #ifdef __HAVE_OLD_DISKLABEL
    958 	case ODIOCGDINFO:
    959 		newlabel = *(wd->sc_dk.dk_label);
    960 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    961 			return ENOTTY;
    962 		memcpy(addr, &newlabel, sizeof (struct olddisklabel));
    963 		return 0;
    964 #endif
    965 
    966 	case DIOCGPART:
    967 		((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
    968 		((struct partinfo *)addr)->part =
    969 		    &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
    970 		return 0;
    971 
    972 	case DIOCWDINFO:
    973 	case DIOCSDINFO:
    974 #ifdef __HAVE_OLD_DISKLABEL
    975 	case ODIOCWDINFO:
    976 	case ODIOCSDINFO:
    977 #endif
    978 	{
    979 		struct disklabel *lp;
    980 
    981 #ifdef __HAVE_OLD_DISKLABEL
    982 		if (xfer == ODIOCSDINFO || xfer == ODIOCWDINFO) {
    983 			memset(&newlabel, 0, sizeof newlabel);
    984 			memcpy(&newlabel, addr, sizeof (struct olddisklabel));
    985 			lp = &newlabel;
    986 		} else
    987 #endif
    988 		lp = (struct disklabel *)addr;
    989 
    990 		if ((flag & FWRITE) == 0)
    991 			return EBADF;
    992 
    993 		if ((error = wdlock(wd)) != 0)
    994 			return error;
    995 		wd->sc_flags |= WDF_LABELLING;
    996 
    997 		error = setdisklabel(wd->sc_dk.dk_label,
    998 		    lp, /*wd->sc_dk.dk_openmask : */0,
    999 		    wd->sc_dk.dk_cpulabel);
   1000 		if (error == 0) {
   1001 			if (wd->drvp->state > RECAL)
   1002 				wd->drvp->drive_flags |= DRIVE_RESET;
   1003 			if (xfer == DIOCWDINFO
   1004 #ifdef __HAVE_OLD_DISKLABEL
   1005 			    || xfer == ODIOCWDINFO
   1006 #endif
   1007 			    )
   1008 				error = writedisklabel(WDLABELDEV(dev),
   1009 				    wdstrategy, wd->sc_dk.dk_label,
   1010 				    wd->sc_dk.dk_cpulabel);
   1011 		}
   1012 
   1013 		wd->sc_flags &= ~WDF_LABELLING;
   1014 		wdunlock(wd);
   1015 		return error;
   1016 	}
   1017 
   1018 	case DIOCKLABEL:
   1019 		if (*(int *)addr)
   1020 			wd->sc_flags |= WDF_KLABEL;
   1021 		else
   1022 			wd->sc_flags &= ~WDF_KLABEL;
   1023 		return 0;
   1024 
   1025 	case DIOCWLABEL:
   1026 		if ((flag & FWRITE) == 0)
   1027 			return EBADF;
   1028 		if (*(int *)addr)
   1029 			wd->sc_flags |= WDF_WLABEL;
   1030 		else
   1031 			wd->sc_flags &= ~WDF_WLABEL;
   1032 		return 0;
   1033 
   1034 	case DIOCGDEFLABEL:
   1035 		wdgetdefaultlabel(wd, (struct disklabel *)addr);
   1036 		return 0;
   1037 #ifdef __HAVE_OLD_DISKLABEL
   1038 	case ODIOCGDEFLABEL:
   1039 		wdgetdefaultlabel(wd, &newlabel);
   1040 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
   1041 			return ENOTTY;
   1042 		memcpy(addr, &newlabel, sizeof (struct olddisklabel));
   1043 		return 0;
   1044 #endif
   1045 
   1046 #ifdef notyet
   1047 	case DIOCWFORMAT:
   1048 		if ((flag & FWRITE) == 0)
   1049 			return EBADF;
   1050 		{
   1051 		register struct format_op *fop;
   1052 		struct iovec aiov;
   1053 		struct uio auio;
   1054 
   1055 		fop = (struct format_op *)addr;
   1056 		aiov.iov_base = fop->df_buf;
   1057 		aiov.iov_len = fop->df_count;
   1058 		auio.uio_iov = &aiov;
   1059 		auio.uio_iovcnt = 1;
   1060 		auio.uio_resid = fop->df_count;
   1061 		auio.uio_segflg = 0;
   1062 		auio.uio_offset =
   1063 			fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
   1064 		auio.uio_procp = p;
   1065 		error = physio(wdformat, NULL, dev, B_WRITE, minphys,
   1066 		    &auio);
   1067 		fop->df_count -= auio.uio_resid;
   1068 		fop->df_reg[0] = wdc->sc_status;
   1069 		fop->df_reg[1] = wdc->sc_error;
   1070 		return error;
   1071 		}
   1072 #endif
   1073 
   1074 	case ATAIOCCOMMAND:
   1075 		/*
   1076 		 * Make sure this command is (relatively) safe first
   1077 		 */
   1078 		if ((((atareq_t *) addr)->flags & ATACMD_READ) == 0 &&
   1079 		    (flag & FWRITE) == 0)
   1080 			return (EBADF);
   1081 		{
   1082 		struct wd_ioctl *wi;
   1083 		atareq_t *atareq = (atareq_t *) addr;
   1084 		int error;
   1085 
   1086 		wi = wi_get();
   1087 		wi->wi_softc = wd;
   1088 		wi->wi_atareq = *atareq;
   1089 
   1090 		if (atareq->datalen && atareq->flags &
   1091 		    (ATACMD_READ | ATACMD_WRITE)) {
   1092 			wi->wi_iov.iov_base = atareq->databuf;
   1093 			wi->wi_iov.iov_len = atareq->datalen;
   1094 			wi->wi_uio.uio_iov = &wi->wi_iov;
   1095 			wi->wi_uio.uio_iovcnt = 1;
   1096 			wi->wi_uio.uio_resid = atareq->datalen;
   1097 			wi->wi_uio.uio_offset = 0;
   1098 			wi->wi_uio.uio_segflg = UIO_USERSPACE;
   1099 			wi->wi_uio.uio_rw =
   1100 			    (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE;
   1101 			wi->wi_uio.uio_procp = p;
   1102 			error = physio(wdioctlstrategy, &wi->wi_bp, dev,
   1103 			    (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE,
   1104 			    minphys, &wi->wi_uio);
   1105 		} else {
   1106 			/* No need to call physio if we don't have any
   1107 			   user data */
   1108 			wi->wi_bp.b_flags = 0;
   1109 			wi->wi_bp.b_data = 0;
   1110 			wi->wi_bp.b_bcount = 0;
   1111 			wi->wi_bp.b_dev = 0;
   1112 			wi->wi_bp.b_proc = p;
   1113 			wdioctlstrategy(&wi->wi_bp);
   1114 			error = wi->wi_bp.b_error;
   1115 		}
   1116 		*atareq = wi->wi_atareq;
   1117 		wi_free(wi);
   1118 		return(error);
   1119 		}
   1120 
   1121 	default:
   1122 		return ENOTTY;
   1123 	}
   1124 
   1125 #ifdef DIAGNOSTIC
   1126 	panic("wdioctl: impossible");
   1127 #endif
   1128 }
   1129 
   1130 #ifdef B_FORMAT
   1131 int
   1132 wdformat(struct buf *bp)
   1133 {
   1134 
   1135 	bp->b_flags |= B_FORMAT;
   1136 	return wdstrategy(bp);
   1137 }
   1138 #endif
   1139 
   1140 int
   1141 wdsize(dev)
   1142 	dev_t dev;
   1143 {
   1144 	struct wd_softc *wd;
   1145 	int part, unit, omask;
   1146 	int size;
   1147 
   1148 	WDCDEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS);
   1149 
   1150 	unit = WDUNIT(dev);
   1151 	if (unit >= wd_cd.cd_ndevs)
   1152 		return (-1);
   1153 	wd = wd_cd.cd_devs[unit];
   1154 	if (wd == NULL)
   1155 		return (-1);
   1156 
   1157 	part = WDPART(dev);
   1158 	omask = wd->sc_dk.dk_openmask & (1 << part);
   1159 
   1160 	if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0)
   1161 		return (-1);
   1162 	if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
   1163 		size = -1;
   1164 	else
   1165 		size = wd->sc_dk.dk_label->d_partitions[part].p_size *
   1166 		    (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
   1167 	if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0)
   1168 		return (-1);
   1169 	return (size);
   1170 }
   1171 
   1172 #ifndef __BDEVSW_DUMP_OLD_TYPE
   1173 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
   1174 static int wddoingadump = 0;
   1175 static int wddumprecalibrated = 0;
   1176 static int wddumpmulti = 1;
   1177 
   1178 /*
   1179  * Dump core after a system crash.
   1180  */
   1181 int
   1182 wddump(dev, blkno, va, size)
   1183 	dev_t dev;
   1184 	daddr_t blkno;
   1185 	caddr_t va;
   1186 	size_t size;
   1187 {
   1188 	struct wd_softc *wd;	/* disk unit to do the I/O */
   1189 	struct disklabel *lp;   /* disk's disklabel */
   1190 	int unit, part;
   1191 	int nblks;	/* total number of sectors left to write */
   1192 	int err;
   1193 	char errbuf[256];
   1194 
   1195 	/* Check if recursive dump; if so, punt. */
   1196 	if (wddoingadump)
   1197 		return EFAULT;
   1198 	wddoingadump = 1;
   1199 
   1200 	unit = WDUNIT(dev);
   1201 	if (unit >= wd_cd.cd_ndevs)
   1202 		return ENXIO;
   1203 	wd = wd_cd.cd_devs[unit];
   1204 	if (wd == (struct wd_softc *)0)
   1205 		return ENXIO;
   1206 
   1207 	part = WDPART(dev);
   1208 
   1209 	/* Make sure it was initialized. */
   1210 	if (wd->drvp->state < READY)
   1211 		return ENXIO;
   1212 
   1213 	/* Convert to disk sectors.  Request must be a multiple of size. */
   1214 	lp = wd->sc_dk.dk_label;
   1215 	if ((size % lp->d_secsize) != 0)
   1216 		return EFAULT;
   1217 	nblks = size / lp->d_secsize;
   1218 	blkno = blkno / (lp->d_secsize / DEV_BSIZE);
   1219 
   1220 	/* Check transfer bounds against partition size. */
   1221 	if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
   1222 		return EINVAL;
   1223 
   1224 	/* Offset block number to start of partition. */
   1225 	blkno += lp->d_partitions[part].p_offset;
   1226 
   1227 	/* Recalibrate, if first dump transfer. */
   1228 	if (wddumprecalibrated == 0) {
   1229 		wddumpmulti = wd->sc_multi;
   1230 		wddumprecalibrated = 1;
   1231 		wd->drvp->state = RESET;
   1232 	}
   1233 
   1234 	while (nblks > 0) {
   1235 again:
   1236 		wd->sc_wdc_bio.blkno = blkno;
   1237 		wd->sc_wdc_bio.flags = ATA_POLL;
   1238 		if (wddumpmulti == 1)
   1239 			wd->sc_wdc_bio.flags |= ATA_SINGLE;
   1240 		if (wd->sc_flags & WDF_LBA)
   1241 			wd->sc_wdc_bio.flags |= ATA_LBA;
   1242 		wd->sc_wdc_bio.bcount =
   1243 			min(nblks, wddumpmulti) * lp->d_secsize;
   1244 		wd->sc_wdc_bio.databuf = va;
   1245 #ifndef WD_DUMP_NOT_TRUSTED
   1246 		switch (wdc_ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
   1247 		case WDC_TRY_AGAIN:
   1248 			panic("wddump: try again");
   1249 			break;
   1250 		case WDC_QUEUED:
   1251 			panic("wddump: polled command has been queued");
   1252 			break;
   1253 		case WDC_COMPLETE:
   1254 			break;
   1255 		}
   1256 		switch(wd->sc_wdc_bio.error) {
   1257 		case TIMEOUT:
   1258 			printf("wddump: device timed out");
   1259 			err = EIO;
   1260 			break;
   1261 		case ERR_DF:
   1262 			printf("wddump: drive fault");
   1263 			err = EIO;
   1264 			break;
   1265 		case ERR_DMA:
   1266 			printf("wddump: DMA error");
   1267 			err = EIO;
   1268 			break;
   1269 		case ERROR:
   1270 			errbuf[0] = '\0';
   1271 			ata_perror(wd->drvp, wd->sc_wdc_bio.r_error, errbuf);
   1272 			printf("wddump: %s", errbuf);
   1273 			err = EIO;
   1274 			break;
   1275 		case NOERROR:
   1276 			err = 0;
   1277 			break;
   1278 		default:
   1279 			panic("wddump: unknown error type");
   1280 		}
   1281 		if (err != 0) {
   1282 			if (wddumpmulti != 1) {
   1283 				wddumpmulti = 1; /* retry in single-sector */
   1284 				printf(", retrying\n");
   1285 				goto again;
   1286 			}
   1287 			printf("\n");
   1288 			return err;
   1289 		}
   1290 #else	/* WD_DUMP_NOT_TRUSTED */
   1291 		/* Let's just talk about this first... */
   1292 		printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
   1293 		    unit, va, cylin, head, sector);
   1294 		delay(500 * 1000);	/* half a second */
   1295 #endif
   1296 
   1297 		/* update block count */
   1298 		nblks -= min(nblks, wddumpmulti);
   1299 		blkno += min(nblks, wddumpmulti);
   1300 		va += min(nblks, wddumpmulti) * lp->d_secsize;
   1301 	}
   1302 
   1303 	wddoingadump = 0;
   1304 	return 0;
   1305 }
   1306 #else /* __BDEVSW_DUMP_NEW_TYPE */
   1307 
   1308 
   1309 int
   1310 wddump(dev, blkno, va, size)
   1311 	dev_t dev;
   1312 	daddr_t blkno;
   1313 	caddr_t va;
   1314 	size_t size;
   1315 {
   1316 
   1317 	/* Not implemented. */
   1318 	return ENXIO;
   1319 }
   1320 #endif /* __BDEVSW_DUMP_NEW_TYPE */
   1321 
   1322 #ifdef HAS_BAD144_HANDLING
   1323 /*
   1324  * Internalize the bad sector table.
   1325  */
   1326 void
   1327 bad144intern(wd)
   1328 	struct wd_softc *wd;
   1329 {
   1330 	struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
   1331 	struct disklabel *lp = wd->sc_dk.dk_label;
   1332 	int i = 0;
   1333 
   1334 	WDCDEBUG_PRINT(("bad144intern\n"), DEBUG_XFERS);
   1335 
   1336 	for (; i < NBT_BAD; i++) {
   1337 		if (bt->bt_bad[i].bt_cyl == 0xffff)
   1338 			break;
   1339 		wd->sc_badsect[i] =
   1340 		    bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
   1341 		    (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
   1342 		    (bt->bt_bad[i].bt_trksec & 0xff);
   1343 	}
   1344 	for (; i < NBT_BAD+1; i++)
   1345 		wd->sc_badsect[i] = -1;
   1346 }
   1347 #endif
   1348 
   1349 int
   1350 wd_get_params(wd, flags, params)
   1351 	struct wd_softc *wd;
   1352 	u_int8_t flags;
   1353 	struct ataparams *params;
   1354 {
   1355 	switch (ata_get_params(wd->drvp, flags, params)) {
   1356 	case CMD_AGAIN:
   1357 		return 1;
   1358 	case CMD_ERR:
   1359 		/*
   1360 		 * We `know' there's a drive here; just assume it's old.
   1361 		 * This geometry is only used to read the MBR and print a
   1362 		 * (false) attach message.
   1363 		 */
   1364 		strncpy(params->atap_model, "ST506",
   1365 		    sizeof params->atap_model);
   1366 		params->atap_config = ATA_CFG_FIXED;
   1367 		params->atap_cylinders = 1024;
   1368 		params->atap_heads = 8;
   1369 		params->atap_sectors = 17;
   1370 		params->atap_multi = 1;
   1371 		params->atap_capabilities1 = params->atap_capabilities2 = 0;
   1372 		wd->drvp->ata_vers = -1; /* Mark it as pre-ATA */
   1373 		return 0;
   1374 	case CMD_OK:
   1375 		return 0;
   1376 	default:
   1377 		panic("wd_get_params: bad return code from ata_get_params");
   1378 		/* NOTREACHED */
   1379 	}
   1380 }
   1381 
   1382 void
   1383 wd_flushcache(wd, flags)
   1384 	struct wd_softc *wd;
   1385 	int flags;
   1386 {
   1387 	struct wdc_command wdc_c;
   1388 
   1389 	if (wd->drvp->ata_vers < 4) /* WDCC_FLUSHCACHE is here since ATA-4 */
   1390 		return;
   1391 	memset(&wdc_c, 0, sizeof(struct wdc_command));
   1392 	wdc_c.r_command = WDCC_FLUSHCACHE;
   1393 	wdc_c.r_st_bmask = WDCS_DRDY;
   1394 	wdc_c.r_st_pmask = WDCS_DRDY;
   1395 	wdc_c.flags = flags;
   1396 	wdc_c.timeout = 30000; /* 30s timeout */
   1397 	if (wdc_exec_command(wd->drvp, &wdc_c) != WDC_COMPLETE) {
   1398 		printf("%s: flush cache command didn't complete\n",
   1399 		    wd->sc_dev.dv_xname);
   1400 	}
   1401 	if (wdc_c.flags & AT_TIMEOU) {
   1402 		printf("%s: flush cache command timeout\n",
   1403 		    wd->sc_dev.dv_xname);
   1404 	}
   1405 	if (wdc_c.flags & AT_DF) {
   1406 		printf("%s: flush cache command: drive fault\n",
   1407 		    wd->sc_dev.dv_xname);
   1408 	}
   1409 	/*
   1410 	 * Ignore error register, it shouldn't report anything else
   1411 	 * than COMMAND ABORTED, which means the device doesn't support
   1412 	 * flush cache
   1413 	 */
   1414 }
   1415 
   1416 void
   1417 wd_shutdown(arg)
   1418 	void *arg;
   1419 {
   1420 	struct wd_softc *wd = arg;
   1421 	wd_flushcache(wd, AT_POLL);
   1422 }
   1423 
   1424 /*
   1425  * Allocate space for a ioctl queue structure.  Mostly taken from
   1426  * scsipi_ioctl.c
   1427  */
   1428 struct wd_ioctl *
   1429 wi_get()
   1430 {
   1431 	struct wd_ioctl *wi;
   1432 	int s;
   1433 
   1434 	wi = malloc(sizeof(struct wd_ioctl), M_TEMP, M_WAITOK);
   1435 	memset(wi, 0, sizeof (struct wd_ioctl));
   1436 	s = splbio();
   1437 	LIST_INSERT_HEAD(&wi_head, wi, wi_list);
   1438 	splx(s);
   1439 	return (wi);
   1440 }
   1441 
   1442 /*
   1443  * Free an ioctl structure and remove it from our list
   1444  */
   1445 
   1446 void
   1447 wi_free(wi)
   1448 	struct wd_ioctl *wi;
   1449 {
   1450 	int s;
   1451 
   1452 	s = splbio();
   1453 	LIST_REMOVE(wi, wi_list);
   1454 	splx(s);
   1455 	free(wi, M_TEMP);
   1456 }
   1457 
   1458 /*
   1459  * Find a wd_ioctl structure based on the struct buf.
   1460  */
   1461 
   1462 struct wd_ioctl *
   1463 wi_find(bp)
   1464 	struct buf *bp;
   1465 {
   1466 	struct wd_ioctl *wi;
   1467 	int s;
   1468 
   1469 	s = splbio();
   1470 	for (wi = wi_head.lh_first; wi != 0; wi = wi->wi_list.le_next)
   1471 		if (bp == &wi->wi_bp)
   1472 			break;
   1473 	splx(s);
   1474 	return (wi);
   1475 }
   1476 
   1477 /*
   1478  * Ioctl pseudo strategy routine
   1479  *
   1480  * This is mostly stolen from scsipi_ioctl.c:scsistrategy().  What
   1481  * happens here is:
   1482  *
   1483  * - wdioctl() queues a wd_ioctl structure.
   1484  *
   1485  * - wdioctl() calls physio/wdioctlstrategy based on whether or not
   1486  *   user space I/O is required.  If physio() is called, physio() eventually
   1487  *   calls wdioctlstrategy().
   1488  *
   1489  * - In either case, wdioctlstrategy() calls wdc_exec_command()
   1490  *   to perform the actual command
   1491  *
   1492  * The reason for the use of the pseudo strategy routine is because
   1493  * when doing I/O to/from user space, physio _really_ wants to be in
   1494  * the loop.  We could put the entire buffer into the ioctl request
   1495  * structure, but that won't scale if we want to do things like download
   1496  * microcode.
   1497  */
   1498 
   1499 void
   1500 wdioctlstrategy(bp)
   1501 	struct buf *bp;
   1502 {
   1503 	struct wd_ioctl *wi;
   1504 	struct wdc_command wdc_c;
   1505 	int error = 0;
   1506 
   1507 	wi = wi_find(bp);
   1508 	if (wi == NULL) {
   1509 		printf("user_strat: No ioctl\n");
   1510 		error = EINVAL;
   1511 		goto bad;
   1512 	}
   1513 
   1514 	memset(&wdc_c, 0, sizeof(wdc_c));
   1515 
   1516 	/*
   1517 	 * Abort if physio broke up the transfer
   1518 	 */
   1519 
   1520 	if (bp->b_bcount != wi->wi_atareq.datalen) {
   1521 		printf("physio split wd ioctl request... cannot proceed\n");
   1522 		error = EIO;
   1523 		goto bad;
   1524 	}
   1525 
   1526 	/*
   1527 	 * Abort if we didn't get a buffer size that was a multiple of
   1528 	 * our sector size (or was larger than NBBY)
   1529 	 */
   1530 
   1531 	if ((bp->b_bcount % wi->wi_softc->sc_dk.dk_label->d_secsize) != 0 ||
   1532 	    (bp->b_bcount / wi->wi_softc->sc_dk.dk_label->d_secsize) >=
   1533 	     (1 << NBBY)) {
   1534 		error = EINVAL;
   1535 		goto bad;
   1536 	}
   1537 
   1538 	/*
   1539 	 * Make sure a timeout was supplied in the ioctl request
   1540 	 */
   1541 
   1542 	if (wi->wi_atareq.timeout == 0) {
   1543 		error = EINVAL;
   1544 		goto bad;
   1545 	}
   1546 
   1547 	if (wi->wi_atareq.flags & ATACMD_READ)
   1548 		wdc_c.flags |= AT_READ;
   1549 	else if (wi->wi_atareq.flags & ATACMD_WRITE)
   1550 		wdc_c.flags |= AT_WRITE;
   1551 
   1552 	if (wi->wi_atareq.flags & ATACMD_READREG)
   1553 		wdc_c.flags |= AT_READREG;
   1554 
   1555 	wdc_c.flags |= AT_WAIT;
   1556 
   1557 	wdc_c.timeout = wi->wi_atareq.timeout;
   1558 	wdc_c.r_command = wi->wi_atareq.command;
   1559 	wdc_c.r_head = wi->wi_atareq.head & 0x0f;
   1560 	wdc_c.r_cyl = wi->wi_atareq.cylinder;
   1561 	wdc_c.r_sector = wi->wi_atareq.sec_num;
   1562 	wdc_c.r_count = wi->wi_atareq.sec_count;
   1563 	wdc_c.r_precomp = wi->wi_atareq.features;
   1564 	wdc_c.r_st_bmask = WDCS_DRDY;
   1565 	wdc_c.r_st_pmask = WDCS_DRDY;
   1566 	wdc_c.data = wi->wi_bp.b_data;
   1567 	wdc_c.bcount = wi->wi_bp.b_bcount;
   1568 
   1569 	if (wdc_exec_command(wi->wi_softc->drvp, &wdc_c) != WDC_COMPLETE) {
   1570 		wi->wi_atareq.retsts = ATACMD_ERROR;
   1571 		goto bad;
   1572 	}
   1573 
   1574 	if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
   1575 		if (wdc_c.flags & AT_ERROR) {
   1576 			wi->wi_atareq.retsts = ATACMD_ERROR;
   1577 			wi->wi_atareq.error = wdc_c.r_error;
   1578 		} else if (wdc_c.flags & AT_DF)
   1579 			wi->wi_atareq.retsts = ATACMD_DF;
   1580 		else
   1581 			wi->wi_atareq.retsts = ATACMD_TIMEOUT;
   1582 	} else {
   1583 		wi->wi_atareq.retsts = ATACMD_OK;
   1584 		if (wi->wi_atareq.flags & ATACMD_READREG) {
   1585 			wi->wi_atareq.head = wdc_c.r_head ;
   1586 			wi->wi_atareq.cylinder = wdc_c.r_cyl;
   1587 			wi->wi_atareq.sec_num = wdc_c.r_sector;
   1588 			wi->wi_atareq.sec_count = wdc_c.r_count;
   1589 			wi->wi_atareq.features = wdc_c.r_precomp;
   1590 			wi->wi_atareq.error = wdc_c.r_error;
   1591 		}
   1592 	}
   1593 
   1594 	bp->b_error = 0;
   1595 	biodone(bp);
   1596 	return;
   1597 bad:
   1598 	bp->b_flags |= B_ERROR;
   1599 	bp->b_error = error;
   1600 	biodone(bp);
   1601 }
   1602