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