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