xref: /openbsd/sys/dev/ata/wd.c (revision 5bf94a72)
1*5bf94a72Sjsg /*	$OpenBSD: wd.c,v 1.132 2024/07/22 14:03:22 jsg Exp $ */
27481efa2Scsapuntz /*	$NetBSD: wd.c,v 1.193 1999/02/28 17:15:27 explorer Exp $ */
37481efa2Scsapuntz 
47481efa2Scsapuntz /*
59a5cc0e7Sgrange  * Copyright (c) 1998, 2001 Manuel Bouyer.  All rights reserved.
67481efa2Scsapuntz  *
77481efa2Scsapuntz  * Redistribution and use in source and binary forms, with or without
87481efa2Scsapuntz  * modification, are permitted provided that the following conditions
97481efa2Scsapuntz  * are met:
107481efa2Scsapuntz  * 1. Redistributions of source code must retain the above copyright
117481efa2Scsapuntz  *	notice, this list of conditions and the following disclaimer.
127481efa2Scsapuntz  * 2. Redistributions in binary form must reproduce the above copyright
137481efa2Scsapuntz  *	notice, this list of conditions and the following disclaimer in the
147481efa2Scsapuntz  *	documentation and/or other materials provided with the distribution.
157481efa2Scsapuntz  *
167481efa2Scsapuntz  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
177481efa2Scsapuntz  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
187481efa2Scsapuntz  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
197481efa2Scsapuntz  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
207481efa2Scsapuntz  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
217481efa2Scsapuntz  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
227481efa2Scsapuntz  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
237481efa2Scsapuntz  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
247481efa2Scsapuntz  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
257481efa2Scsapuntz  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
267481efa2Scsapuntz  */
277481efa2Scsapuntz 
287481efa2Scsapuntz /*-
29805cc48eSsf  * Copyright (c) 1998, 2003, 2004 The NetBSD Foundation, Inc.
307481efa2Scsapuntz  * All rights reserved.
317481efa2Scsapuntz  *
327481efa2Scsapuntz  * This code is derived from software contributed to The NetBSD Foundation
337481efa2Scsapuntz  * by Charles M. Hannum and by Onno van der Linden.
347481efa2Scsapuntz  *
357481efa2Scsapuntz  * Redistribution and use in source and binary forms, with or without
367481efa2Scsapuntz  * modification, are permitted provided that the following conditions
377481efa2Scsapuntz  * are met:
387481efa2Scsapuntz  * 1. Redistributions of source code must retain the above copyright
397481efa2Scsapuntz  *    notice, this list of conditions and the following disclaimer.
407481efa2Scsapuntz  * 2. Redistributions in binary form must reproduce the above copyright
417481efa2Scsapuntz  *    notice, this list of conditions and the following disclaimer in the
427481efa2Scsapuntz  *    documentation and/or other materials provided with the distribution.
437481efa2Scsapuntz  *
447481efa2Scsapuntz  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
457481efa2Scsapuntz  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
467481efa2Scsapuntz  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
477481efa2Scsapuntz  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
487481efa2Scsapuntz  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
497481efa2Scsapuntz  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
507481efa2Scsapuntz  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
517481efa2Scsapuntz  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
527481efa2Scsapuntz  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
537481efa2Scsapuntz  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
547481efa2Scsapuntz  * POSSIBILITY OF SUCH DAMAGE.
557481efa2Scsapuntz  */
567481efa2Scsapuntz 
577481efa2Scsapuntz #if 0
587481efa2Scsapuntz #include "rnd.h"
597481efa2Scsapuntz #endif
607481efa2Scsapuntz 
617481efa2Scsapuntz #include <sys/param.h>
627481efa2Scsapuntz #include <sys/systm.h>
637481efa2Scsapuntz #include <sys/conf.h>
64c0cd3489Sguenther #include <sys/fcntl.h>
657481efa2Scsapuntz #include <sys/stat.h>
667481efa2Scsapuntz #include <sys/buf.h>
677481efa2Scsapuntz #include <sys/uio.h>
687481efa2Scsapuntz #include <sys/malloc.h>
697481efa2Scsapuntz #include <sys/device.h>
707481efa2Scsapuntz #include <sys/disklabel.h>
717481efa2Scsapuntz #include <sys/disk.h>
727481efa2Scsapuntz #include <sys/syslog.h>
737f58a11fSjsg #include <sys/timeout.h>
7491f4f7d8Sdlg #include <sys/dkio.h>
7514cbac95Skettenis #include <sys/reboot.h>
767481efa2Scsapuntz 
777481efa2Scsapuntz #include <machine/intr.h>
787481efa2Scsapuntz #include <machine/bus.h>
797481efa2Scsapuntz 
807481efa2Scsapuntz #include <dev/ata/atareg.h>
817481efa2Scsapuntz #include <dev/ata/atavar.h>
827481efa2Scsapuntz #include <dev/ata/wdvar.h>
837481efa2Scsapuntz #include <dev/ic/wdcreg.h>
84f5423f8cScsapuntz #include <dev/ic/wdcvar.h>
857481efa2Scsapuntz #if 0
867481efa2Scsapuntz #include "locators.h"
877481efa2Scsapuntz #endif
887481efa2Scsapuntz 
8981a81281Saaron #define	LBA48_THRESHOLD		(0xfffffff)	/* 128GB / DEV_BSIZE */
9081a81281Saaron 
917481efa2Scsapuntz #define	WDIORETRIES_SINGLE 4	/* number of retries before single-sector */
927481efa2Scsapuntz #define	WDIORETRIES	5	/* number of retries before giving up */
93c6a53756Skn #define	RECOVERYTIME_MSEC 500	/* time to wait before retrying a cmd */
947481efa2Scsapuntz 
957481efa2Scsapuntz #define DEBUG_INTR   0x01
967481efa2Scsapuntz #define DEBUG_XFERS  0x02
977481efa2Scsapuntz #define DEBUG_STATUS 0x04
987481efa2Scsapuntz #define DEBUG_FUNCS  0x08
997481efa2Scsapuntz #define DEBUG_PROBE  0x10
1007481efa2Scsapuntz #ifdef WDCDEBUG
1017481efa2Scsapuntz extern int wdcdebug_wd_mask; /* init'ed in ata_wdc.c */
102d6986e4fSgrange #define WDCDEBUG_PRINT(args, level) do {	\
103d6986e4fSgrange 	if ((wdcdebug_wd_mask & (level)) != 0)	\
104d6986e4fSgrange 		printf args;			\
105d6986e4fSgrange } while (0)
1067481efa2Scsapuntz #else
1077481efa2Scsapuntz #define WDCDEBUG_PRINT(args, level)
1087481efa2Scsapuntz #endif
1097481efa2Scsapuntz 
1107481efa2Scsapuntz 
1117481efa2Scsapuntz #define sc_drive sc_wdc_bio.drive
1127481efa2Scsapuntz #define sc_mode sc_wdc_bio.mode
1137481efa2Scsapuntz #define sc_multi sc_wdc_bio.multi
1147481efa2Scsapuntz 
115c4071fd1Smillert int	wdprobe(struct device *, void *, void *);
116c4071fd1Smillert void	wdattach(struct device *, struct device *, void *);
117c4071fd1Smillert int	wddetach(struct device *, int);
118e78728c7Spirofti int	wdactivate(struct device *, int);
1197481efa2Scsapuntz 
120471aeecfSnaddy const struct cfattach wd_ca = {
121f97ca3e4Scsapuntz 	sizeof(struct wd_softc), wdprobe, wdattach,
122a23d39c4Smiod 	wddetach, wdactivate
1237481efa2Scsapuntz };
1247481efa2Scsapuntz 
1257481efa2Scsapuntz struct cfdriver wd_cd = {
1267481efa2Scsapuntz 	NULL, "wd", DV_DISK
1277481efa2Scsapuntz };
1287481efa2Scsapuntz 
129c4071fd1Smillert void  wdgetdefaultlabel(struct wd_softc *, struct disklabel *);
130df591ed6Sderaadt int   wdgetdisklabel(dev_t dev, struct wd_softc *, struct disklabel *, int);
131c4071fd1Smillert void  wdstrategy(struct buf *);
132c4071fd1Smillert void  wdstart(void *);
133c4071fd1Smillert void  __wdstart(struct wd_softc*, struct buf *);
134c4071fd1Smillert void  wdrestart(void *);
135c4071fd1Smillert int   wd_get_params(struct wd_softc *, u_int8_t, struct ataparams *);
136805cc48eSsf int   wd_flushcache(struct wd_softc *, int);
137f8fc1e98Skettenis void  wd_standby(struct wd_softc *, int);
1387481efa2Scsapuntz 
1397481efa2Scsapuntz /* XXX: these should go elsewhere */
1407481efa2Scsapuntz cdev_decl(wd);
1417481efa2Scsapuntz bdev_decl(wd);
1427481efa2Scsapuntz 
14331efee77Sjsing #define wdlookup(unit) (struct wd_softc *)disk_lookup(&wd_cd, (unit))
144f97ca3e4Scsapuntz 
1457481efa2Scsapuntz 
1467481efa2Scsapuntz int
wdprobe(struct device * parent,void * match_,void * aux)14706ad70d2Sgrange wdprobe(struct device *parent, void *match_, void *aux)
1487481efa2Scsapuntz {
1497481efa2Scsapuntz 	struct ata_atapi_attach *aa_link = aux;
1507481efa2Scsapuntz 	struct cfdata *match = match_;
1517481efa2Scsapuntz 
1527481efa2Scsapuntz 	if (aa_link == NULL)
1537481efa2Scsapuntz 		return 0;
1547481efa2Scsapuntz 	if (aa_link->aa_type != T_ATA)
1557481efa2Scsapuntz 		return 0;
1567481efa2Scsapuntz 
1577481efa2Scsapuntz 	if (match->cf_loc[0] != -1 &&
1587481efa2Scsapuntz 	    match->cf_loc[0] != aa_link->aa_channel)
1597481efa2Scsapuntz 		return 0;
1607481efa2Scsapuntz 
1617481efa2Scsapuntz 	if (match->cf_loc[1] != -1 &&
1627481efa2Scsapuntz 	    match->cf_loc[1] != aa_link->aa_drv_data->drive)
1637481efa2Scsapuntz 		return 0;
1647481efa2Scsapuntz 
1657481efa2Scsapuntz 	return 1;
1667481efa2Scsapuntz }
1677481efa2Scsapuntz 
1687481efa2Scsapuntz void
wdattach(struct device * parent,struct device * self,void * aux)16906ad70d2Sgrange wdattach(struct device *parent, struct device *self, void *aux)
1707481efa2Scsapuntz {
1717481efa2Scsapuntz 	struct wd_softc *wd = (void *)self;
1727481efa2Scsapuntz 	struct ata_atapi_attach *aa_link= aux;
17342a17170Sjsg 	struct wdc_command wdc_c;
1747481efa2Scsapuntz 	int i, blank;
1757481efa2Scsapuntz 	char buf[41], c, *p, *q;
1767481efa2Scsapuntz 	WDCDEBUG_PRINT(("wdattach\n"), DEBUG_FUNCS | DEBUG_PROBE);
1777481efa2Scsapuntz 
1787481efa2Scsapuntz 	wd->openings = aa_link->aa_openings;
179f97ca3e4Scsapuntz 	wd->drvp = aa_link->aa_drv_data;
180f97ca3e4Scsapuntz 
18102acd7cbSray 	strlcpy(wd->drvp->drive_name, wd->sc_dev.dv_xname,
18202acd7cbSray 	    sizeof(wd->drvp->drive_name));
183f97ca3e4Scsapuntz 	wd->drvp->cf_flags = wd->sc_dev.dv_cfdata->cf_flags;
1847481efa2Scsapuntz 
185ec8802a4Scsapuntz 	if ((NERRS_MAX - 2) > 0)
186ec8802a4Scsapuntz 		wd->drvp->n_dmaerrs = NERRS_MAX - 2;
187ec8802a4Scsapuntz 	else
188ec8802a4Scsapuntz 		wd->drvp->n_dmaerrs = 0;
189ec8802a4Scsapuntz 
1907481efa2Scsapuntz 	/* read our drive info */
191f97ca3e4Scsapuntz 	if (wd_get_params(wd, at_poll, &wd->sc_params) != 0) {
1927481efa2Scsapuntz 		printf("%s: IDENTIFY failed\n", wd->sc_dev.dv_xname);
1937481efa2Scsapuntz 		return;
1947481efa2Scsapuntz 	}
1957481efa2Scsapuntz 
1967481efa2Scsapuntz 	for (blank = 0, p = wd->sc_params.atap_model, q = buf, i = 0;
1977481efa2Scsapuntz 	    i < sizeof(wd->sc_params.atap_model); i++) {
1987481efa2Scsapuntz 		c = *p++;
1997481efa2Scsapuntz 		if (c == '\0')
2007481efa2Scsapuntz 			break;
2017481efa2Scsapuntz 		if (c != ' ') {
2027481efa2Scsapuntz 			if (blank) {
2037481efa2Scsapuntz 				*q++ = ' ';
2047481efa2Scsapuntz 				blank = 0;
2057481efa2Scsapuntz 			}
2067481efa2Scsapuntz 			*q++ = c;
2077481efa2Scsapuntz 		} else
2087481efa2Scsapuntz 			blank = 1;
2097481efa2Scsapuntz 		}
2107481efa2Scsapuntz 	*q++ = '\0';
2117481efa2Scsapuntz 
2127481efa2Scsapuntz 	printf(": <%s>\n", buf);
2137481efa2Scsapuntz 
21412dc5567Scsapuntz 	wdc_probe_caps(wd->drvp, &wd->sc_params);
21512dc5567Scsapuntz 	wdc_print_caps(wd->drvp);
21612dc5567Scsapuntz 
2177481efa2Scsapuntz 	if ((wd->sc_params.atap_multi & 0xff) > 1) {
2187481efa2Scsapuntz 		wd->sc_multi = wd->sc_params.atap_multi & 0xff;
2197481efa2Scsapuntz 	} else {
2207481efa2Scsapuntz 		wd->sc_multi = 1;
2217481efa2Scsapuntz 	}
2227481efa2Scsapuntz 
223c3a9df52Sderaadt 	printf("%s: %d-sector PIO,", wd->sc_dev.dv_xname, wd->sc_multi);
2247481efa2Scsapuntz 
2254b4c6f4eSgluk 	/* use 48-bit LBA if enabled */
2264b4c6f4eSgluk 	if ((wd->sc_params.atap_cmd2_en & ATAPI_CMD2_48AD) != 0)
2274b4c6f4eSgluk 		wd->sc_flags |= WDF_LBA48;
2284b4c6f4eSgluk 
2297481efa2Scsapuntz 	/* Prior to ATA-4, LBA was optional. */
2307481efa2Scsapuntz 	if ((wd->sc_params.atap_capabilities1 & WDC_CAP_LBA) != 0)
2317481efa2Scsapuntz 		wd->sc_flags |= WDF_LBA;
2327481efa2Scsapuntz #if 0
2337481efa2Scsapuntz 	/* ATA-4 requires LBA. */
2347481efa2Scsapuntz 	if (wd->sc_params.atap_ataversion != 0xffff &&
2357481efa2Scsapuntz 	    wd->sc_params.atap_ataversion >= WDC_VER_ATA4)
2367481efa2Scsapuntz 		wd->sc_flags |= WDF_LBA;
2377481efa2Scsapuntz #endif
2387481efa2Scsapuntz 
2394b4c6f4eSgluk 	if ((wd->sc_flags & WDF_LBA48) != 0) {
2404b4c6f4eSgluk 		wd->sc_capacity =
2414b4c6f4eSgluk 		    (((u_int64_t)wd->sc_params.atap_max_lba[3] << 48) |
2424b4c6f4eSgluk 		     ((u_int64_t)wd->sc_params.atap_max_lba[2] << 32) |
2434b4c6f4eSgluk 		     ((u_int64_t)wd->sc_params.atap_max_lba[1] << 16) |
2444b4c6f4eSgluk 		      (u_int64_t)wd->sc_params.atap_max_lba[0]);
2452a13a35bSgrange 		printf(" LBA48, %lluMB, %llu sectors\n",
2464b4c6f4eSgluk 		    wd->sc_capacity / (1048576 / DEV_BSIZE),
2474b4c6f4eSgluk 		    wd->sc_capacity);
2484b4c6f4eSgluk 	} else if ((wd->sc_flags & WDF_LBA) != 0) {
2497481efa2Scsapuntz 		wd->sc_capacity =
2507481efa2Scsapuntz 		    (wd->sc_params.atap_capacity[1] << 16) |
2517481efa2Scsapuntz 		    wd->sc_params.atap_capacity[0];
2522a13a35bSgrange 		printf(" LBA, %lluMB, %llu sectors\n",
2537481efa2Scsapuntz 		    wd->sc_capacity / (1048576 / DEV_BSIZE),
2547481efa2Scsapuntz 		    wd->sc_capacity);
2557481efa2Scsapuntz 	} else {
2567481efa2Scsapuntz 		wd->sc_capacity =
2577481efa2Scsapuntz 		    wd->sc_params.atap_cylinders *
2587481efa2Scsapuntz 		    wd->sc_params.atap_heads *
2597481efa2Scsapuntz 		    wd->sc_params.atap_sectors;
2604b4c6f4eSgluk 		printf(" CHS, %lluMB, %d cyl, %d head, %d sec, %llu sectors\n",
2617481efa2Scsapuntz 		    wd->sc_capacity / (1048576 / DEV_BSIZE),
2627481efa2Scsapuntz 		    wd->sc_params.atap_cylinders,
2637481efa2Scsapuntz 		    wd->sc_params.atap_heads,
2647481efa2Scsapuntz 		    wd->sc_params.atap_sectors,
2657481efa2Scsapuntz 		    wd->sc_capacity);
2667481efa2Scsapuntz 	}
2677481efa2Scsapuntz 	WDCDEBUG_PRINT(("%s: atap_dmatiming_mimi=%d, atap_dmatiming_recom=%d\n",
2687481efa2Scsapuntz 	    self->dv_xname, wd->sc_params.atap_dmatiming_mimi,
2697481efa2Scsapuntz 	    wd->sc_params.atap_dmatiming_recom), DEBUG_PROBE);
27042a17170Sjsg 
27144323affSjsg 	/* use read look ahead if supported */
272119641fdSjsg 	if (wd->sc_params.atap_cmd_set1 & WDC_CMD1_AHEAD) {
273119641fdSjsg 		bzero(&wdc_c, sizeof(struct wdc_command));
274119641fdSjsg 		wdc_c.r_command = SET_FEATURES;
275ee15be18Skrw 		wdc_c.r_features = WDSF_READAHEAD_EN;
276119641fdSjsg 		wdc_c.timeout = 1000;
277119641fdSjsg 		wdc_c.flags = at_poll;
278119641fdSjsg 
279119641fdSjsg 		if (wdc_exec_command(wd->drvp, &wdc_c) != WDC_COMPLETE) {
280119641fdSjsg 			printf("%s: enable look ahead command didn't "
281119641fdSjsg 			    "complete\n", wd->sc_dev.dv_xname);
282119641fdSjsg 		}
283119641fdSjsg 	}
284119641fdSjsg 
28544323affSjsg 	/* use write cache if supported */
286119641fdSjsg 	if (wd->sc_params.atap_cmd_set1 & WDC_CMD1_CACHE) {
287119641fdSjsg 		bzero(&wdc_c, sizeof(struct wdc_command));
288119641fdSjsg 		wdc_c.r_command = SET_FEATURES;
289ee15be18Skrw 		wdc_c.r_features = WDSF_EN_WR_CACHE;
290119641fdSjsg 		wdc_c.timeout = 1000;
291119641fdSjsg 		wdc_c.flags = at_poll;
292119641fdSjsg 
293119641fdSjsg 		if (wdc_exec_command(wd->drvp, &wdc_c) != WDC_COMPLETE) {
294119641fdSjsg 			printf("%s: enable write cache command didn't "
295119641fdSjsg 			    "complete\n", wd->sc_dev.dv_xname);
296119641fdSjsg 		}
297119641fdSjsg 	}
29844323affSjsg 
29942a17170Sjsg 	/*
3004b1a56afSjsg 	 * FREEZE LOCK the drive so malicious users can't lock it on us.
30142a17170Sjsg 	 * As there is no harm in issuing this to drives that don't
30242a17170Sjsg 	 * support the security feature set we just send it, and don't
30342a17170Sjsg 	 * bother checking if the drive sends a command abort to tell us it
30442a17170Sjsg 	 * doesn't support it.
30542a17170Sjsg 	 */
30642a17170Sjsg 	bzero(&wdc_c, sizeof(struct wdc_command));
30742a17170Sjsg 
30842a17170Sjsg 	wdc_c.r_command = WDCC_SEC_FREEZE_LOCK;
30942a17170Sjsg 	wdc_c.timeout = 1000;
31042a17170Sjsg 	wdc_c.flags = at_poll;
31142a17170Sjsg 	if (wdc_exec_command(wd->drvp, &wdc_c) != WDC_COMPLETE) {
31242a17170Sjsg 		printf("%s: freeze lock command didn't complete\n",
31342a17170Sjsg 		    wd->sc_dev.dv_xname);
31442a17170Sjsg 	}
31542a17170Sjsg 
3167481efa2Scsapuntz 	/*
31773b308c6Sjsing 	 * Initialize disk structures.
3187481efa2Scsapuntz 	 */
3197481efa2Scsapuntz 	wd->sc_dk.dk_name = wd->sc_dev.dv_xname;
320c2235760Sdlg 	bufq_init(&wd->sc_bufq, BUFQ_DEFAULT);
3217c4d2af8Sart 	timeout_set(&wd->sc_restart_timeout, wdrestart, wd);
32273b308c6Sjsing 
32373b308c6Sjsing 	/* Attach disk. */
3242690bc4bSjsing 	disk_attach(&wd->sc_dev, &wd->sc_dk);
32573b308c6Sjsing 	wd->sc_wdc_bio.lp = wd->sc_dk.dk_label;
3267481efa2Scsapuntz }
3277481efa2Scsapuntz 
328f97ca3e4Scsapuntz int
wdactivate(struct device * self,int act)329e78728c7Spirofti wdactivate(struct device *self, int act)
330f97ca3e4Scsapuntz {
3316753d76aSkettenis 	struct wd_softc *wd = (void *)self;
332f97ca3e4Scsapuntz 	int rv = 0;
333f97ca3e4Scsapuntz 
334f97ca3e4Scsapuntz 	switch (act) {
3356753d76aSkettenis 	case DVACT_SUSPEND:
3361e4b376cSderaadt 		break;
3371e4b376cSderaadt 	case DVACT_POWERDOWN:
33845326f51Skettenis 		wd_flushcache(wd, AT_POLL);
3391e4b376cSderaadt 		if (boothowto & RB_POWERDOWN)
340f8fc1e98Skettenis 			wd_standby(wd, AT_POLL);
3416753d76aSkettenis 		break;
342dff40dcaSmlarkin 	case DVACT_RESUME:
343dff40dcaSmlarkin 		/*
344dff40dcaSmlarkin 		 * Do two resets separated by a small delay. The
345dff40dcaSmlarkin 		 * first wakes the controller, the second resets
346ba8cbb4aSmiod 		 * the channel.
347dff40dcaSmlarkin 		 */
348dff40dcaSmlarkin 		wdc_disable_intr(wd->drvp->chnl_softc);
349ba8cbb4aSmiod 		wdc_reset_channel(wd->drvp, 1);
350dff40dcaSmlarkin 		delay(10000);
351ba8cbb4aSmiod 		wdc_reset_channel(wd->drvp, 0);
352dff40dcaSmlarkin 		wdc_enable_intr(wd->drvp->chnl_softc);
353ba8cbb4aSmiod 		wd_get_params(wd, at_poll, &wd->sc_params);
354dff40dcaSmlarkin 		break;
355f97ca3e4Scsapuntz 	}
356f97ca3e4Scsapuntz 	return (rv);
357f97ca3e4Scsapuntz }
358f97ca3e4Scsapuntz 
359f97ca3e4Scsapuntz int
wddetach(struct device * self,int flags)36006ad70d2Sgrange wddetach(struct device *self, int flags)
361f97ca3e4Scsapuntz {
362f97ca3e4Scsapuntz 	struct wd_softc *sc = (struct wd_softc *)self;
363f97ca3e4Scsapuntz 
3644072b0baSderaadt 	timeout_del(&sc->sc_restart_timeout);
3654072b0baSderaadt 
36699b6117dSmatthew 	bufq_drain(&sc->sc_bufq);
367f97ca3e4Scsapuntz 
368e1d64023Smatthew 	disk_gone(wdopen, self->dv_unit);
369f97ca3e4Scsapuntz 
370a23d39c4Smiod 	/* Detach disk. */
371c2235760Sdlg 	bufq_destroy(&sc->sc_bufq);
372a23d39c4Smiod 	disk_detach(&sc->sc_dk);
373a23d39c4Smiod 
374f97ca3e4Scsapuntz 	return (0);
375f97ca3e4Scsapuntz }
376f97ca3e4Scsapuntz 
3777481efa2Scsapuntz /*
3787481efa2Scsapuntz  * Read/write routine for a buffer.  Validates the arguments and schedules the
3797481efa2Scsapuntz  * transfer.  Does not wait for the transfer to complete.
3807481efa2Scsapuntz  */
3817481efa2Scsapuntz void
wdstrategy(struct buf * bp)38206ad70d2Sgrange wdstrategy(struct buf *bp)
3837481efa2Scsapuntz {
384f97ca3e4Scsapuntz 	struct wd_softc *wd;
3857481efa2Scsapuntz 	int s;
386f97ca3e4Scsapuntz 
387a83b1495Skrw 	wd = wdlookup(DISKUNIT(bp->b_dev));
388f97ca3e4Scsapuntz 	if (wd == NULL) {
389f97ca3e4Scsapuntz 		bp->b_error = ENXIO;
390f97ca3e4Scsapuntz 		goto bad;
391f97ca3e4Scsapuntz 	}
392f97ca3e4Scsapuntz 
3937481efa2Scsapuntz 	WDCDEBUG_PRINT(("wdstrategy (%s)\n", wd->sc_dev.dv_xname),
3947481efa2Scsapuntz 	    DEBUG_XFERS);
3957481efa2Scsapuntz 
3967481efa2Scsapuntz 	/* If device invalidated (e.g. media change, door open), error. */
3977481efa2Scsapuntz 	if ((wd->sc_flags & WDF_LOADED) == 0) {
3987481efa2Scsapuntz 		bp->b_error = EIO;
3997481efa2Scsapuntz 		goto bad;
4007481efa2Scsapuntz 	}
4017481efa2Scsapuntz 
402d1e5b14cSmatthew 	/* Validate the request. */
403d1e5b14cSmatthew 	if (bounds_check_with_label(bp, wd->sc_dk.dk_label) == -1)
4047481efa2Scsapuntz 		goto done;
4057481efa2Scsapuntz 
406d1e5b14cSmatthew 	/* Check that the number of sectors can fit in a byte. */
407d1e5b14cSmatthew 	if ((bp->b_bcount / wd->sc_dk.dk_label->d_secsize) >= (1 << NBBY)) {
408d1e5b14cSmatthew 		bp->b_error = EINVAL;
409d1e5b14cSmatthew 		goto bad;
410d1e5b14cSmatthew 	}
411d1e5b14cSmatthew 
4127481efa2Scsapuntz 	/* Queue transfer on drive, activate drive and controller if idle. */
413c2235760Sdlg 	bufq_queue(&wd->sc_bufq, bp);
4147481efa2Scsapuntz 	s = splbio();
4157481efa2Scsapuntz 	wdstart(wd);
4167481efa2Scsapuntz 	splx(s);
417f97ca3e4Scsapuntz 	device_unref(&wd->sc_dev);
4187481efa2Scsapuntz 	return;
419d1e5b14cSmatthew 
4207481efa2Scsapuntz  bad:
4217481efa2Scsapuntz 	bp->b_flags |= B_ERROR;
4227481efa2Scsapuntz 	bp->b_resid = bp->b_bcount;
423d1e5b14cSmatthew  done:
424343aa71cSart 	s = splbio();
4257481efa2Scsapuntz 	biodone(bp);
426343aa71cSart 	splx(s);
427f97ca3e4Scsapuntz 	if (wd != NULL)
428f97ca3e4Scsapuntz 		device_unref(&wd->sc_dev);
4297481efa2Scsapuntz }
4307481efa2Scsapuntz 
4317481efa2Scsapuntz /*
4327481efa2Scsapuntz  * Queue a drive for I/O.
4337481efa2Scsapuntz  */
4347481efa2Scsapuntz void
wdstart(void * arg)43506ad70d2Sgrange wdstart(void *arg)
4367481efa2Scsapuntz {
4377481efa2Scsapuntz 	struct wd_softc *wd = arg;
438fd114b83Sthib 	struct buf *bp = NULL;
4397481efa2Scsapuntz 
4407481efa2Scsapuntz 	WDCDEBUG_PRINT(("wdstart %s\n", wd->sc_dev.dv_xname),
4417481efa2Scsapuntz 	    DEBUG_XFERS);
4427481efa2Scsapuntz 	while (wd->openings > 0) {
4437481efa2Scsapuntz 
4447481efa2Scsapuntz 		/* Is there a buf for us ? */
445c2235760Sdlg 		if ((bp = bufq_dequeue(&wd->sc_bufq)) == NULL)
4467481efa2Scsapuntz 			return;
4477481efa2Scsapuntz 		/*
4487481efa2Scsapuntz 		 * Make the command. First lock the device
4497481efa2Scsapuntz 		 */
4507481efa2Scsapuntz 		wd->openings--;
4517481efa2Scsapuntz 
4527481efa2Scsapuntz 		wd->retries = 0;
4537481efa2Scsapuntz 		__wdstart(wd, bp);
4547481efa2Scsapuntz 	}
4557481efa2Scsapuntz }
4567481efa2Scsapuntz 
4577481efa2Scsapuntz void
__wdstart(struct wd_softc * wd,struct buf * bp)45806ad70d2Sgrange __wdstart(struct wd_softc *wd, struct buf *bp)
4597481efa2Scsapuntz {
4602e7ffe50Skrw 	struct disklabel *lp;
4612e7ffe50Skrw 	u_int64_t nsecs;
462cec793cfSgrange 
4632e7ffe50Skrw 	lp = wd->sc_dk.dk_label;
4642e7ffe50Skrw 	wd->sc_wdc_bio.blkno = DL_BLKTOSEC(lp, bp->b_blkno + DL_SECTOBLK(lp,
4652e7ffe50Skrw 	    DL_GETPOFFSET(&lp->d_partitions[DISKPART(bp->b_dev)])));
4667481efa2Scsapuntz 	wd->sc_wdc_bio.blkdone =0;
4677481efa2Scsapuntz 	wd->sc_bp = bp;
4687481efa2Scsapuntz 	/*
4697481efa2Scsapuntz 	 * If we're retrying, retry in single-sector mode. This will give us
4707481efa2Scsapuntz 	 * the sector number of the problem, and will eventually allow the
4717481efa2Scsapuntz 	 * transfer to succeed.
4727481efa2Scsapuntz 	 */
473a9a2d616Ssthen 	if (wd->retries >= WDIORETRIES_SINGLE)
4747481efa2Scsapuntz 		wd->sc_wdc_bio.flags = ATA_SINGLE;
4757481efa2Scsapuntz 	else
4767481efa2Scsapuntz 		wd->sc_wdc_bio.flags = 0;
4772e7ffe50Skrw 	nsecs = howmany(bp->b_bcount, lp->d_secsize);
478cec793cfSgrange 	if ((wd->sc_flags & WDF_LBA48) &&
479cec793cfSgrange 	    /* use LBA48 only if really need */
4802e7ffe50Skrw 	    ((wd->sc_wdc_bio.blkno + nsecs - 1 >= LBA48_THRESHOLD) ||
4812e7ffe50Skrw 	     (nsecs > 0xff)))
4824b4c6f4eSgluk 		wd->sc_wdc_bio.flags |= ATA_LBA48;
4837481efa2Scsapuntz 	if (wd->sc_flags & WDF_LBA)
4847481efa2Scsapuntz 		wd->sc_wdc_bio.flags |= ATA_LBA;
4857481efa2Scsapuntz 	if (bp->b_flags & B_READ)
4867481efa2Scsapuntz 		wd->sc_wdc_bio.flags |= ATA_READ;
4877481efa2Scsapuntz 	wd->sc_wdc_bio.bcount = bp->b_bcount;
4887481efa2Scsapuntz 	wd->sc_wdc_bio.databuf = bp->b_data;
489f97ca3e4Scsapuntz 	wd->sc_wdc_bio.wd = wd;
4907481efa2Scsapuntz 	/* Instrumentation. */
4917481efa2Scsapuntz 	disk_busy(&wd->sc_dk);
4927481efa2Scsapuntz 	switch (wdc_ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
4937481efa2Scsapuntz 	case WDC_TRY_AGAIN:
494e15e1c8cSblambert 		timeout_add_sec(&wd->sc_restart_timeout, 1);
4957481efa2Scsapuntz 		break;
4967481efa2Scsapuntz 	case WDC_QUEUED:
4977481efa2Scsapuntz 		break;
4987481efa2Scsapuntz 	case WDC_COMPLETE:
49913456e59Scsapuntz 		/*
50013456e59Scsapuntz 		 * This code is never executed because we never set
50113456e59Scsapuntz 		 * the ATA_POLL flag above
50213456e59Scsapuntz 		 */
50313456e59Scsapuntz #if 0
50413456e59Scsapuntz 		if (wd->sc_wdc_bio.flags & ATA_POLL)
5057481efa2Scsapuntz 			wddone(wd);
50613456e59Scsapuntz #endif
5077481efa2Scsapuntz 		break;
5087481efa2Scsapuntz 	default:
5097481efa2Scsapuntz 		panic("__wdstart: bad return code from wdc_ata_bio()");
5107481efa2Scsapuntz 	}
5117481efa2Scsapuntz }
5127481efa2Scsapuntz 
5137481efa2Scsapuntz void
wddone(void * v)51406ad70d2Sgrange wddone(void *v)
5157481efa2Scsapuntz {
5167481efa2Scsapuntz 	struct wd_softc *wd = v;
5177481efa2Scsapuntz 	struct buf *bp = wd->sc_bp;
5187481efa2Scsapuntz 	char buf[256], *errbuf = buf;
5197481efa2Scsapuntz 	WDCDEBUG_PRINT(("wddone %s\n", wd->sc_dev.dv_xname),
5207481efa2Scsapuntz 	    DEBUG_XFERS);
5217481efa2Scsapuntz 
5227481efa2Scsapuntz 	bp->b_resid = wd->sc_wdc_bio.bcount;
5237481efa2Scsapuntz 	errbuf[0] = '\0';
5247481efa2Scsapuntz 	switch (wd->sc_wdc_bio.error) {
525f97ca3e4Scsapuntz 	case ERR_NODEV:
526f97ca3e4Scsapuntz 		bp->b_flags |= B_ERROR;
527f97ca3e4Scsapuntz 		bp->b_error = ENXIO;
528f97ca3e4Scsapuntz 		break;
5297481efa2Scsapuntz 	case ERR_DMA:
5307481efa2Scsapuntz 		errbuf = "DMA error";
5317481efa2Scsapuntz 		goto retry;
5327481efa2Scsapuntz 	case ERR_DF:
5337481efa2Scsapuntz 		errbuf = "device fault";
5347481efa2Scsapuntz 		goto retry;
5357481efa2Scsapuntz 	case TIMEOUT:
5367481efa2Scsapuntz 		errbuf = "device timeout";
5377481efa2Scsapuntz 		goto retry;
5387481efa2Scsapuntz 	case ERROR:
5397481efa2Scsapuntz 		/* Don't care about media change bits */
5407481efa2Scsapuntz 		if (wd->sc_wdc_bio.r_error != 0 &&
5417481efa2Scsapuntz 		    (wd->sc_wdc_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0)
5427481efa2Scsapuntz 			goto noerror;
543b6136c85Sho 		ata_perror(wd->drvp, wd->sc_wdc_bio.r_error, errbuf,
544b6136c85Sho 		    sizeof buf);
545f97ca3e4Scsapuntz retry:
546f97ca3e4Scsapuntz 		/* Just reset and retry. Can we do more ? */
547ba8cbb4aSmiod 		wdc_reset_channel(wd->drvp, 0);
5487481efa2Scsapuntz 		diskerr(bp, "wd", errbuf, LOG_PRINTF,
5497481efa2Scsapuntz 		    wd->sc_wdc_bio.blkdone, wd->sc_dk.dk_label);
5507481efa2Scsapuntz 		if (wd->retries++ < WDIORETRIES) {
5517481efa2Scsapuntz 			printf(", retrying\n");
552c6a53756Skn 			timeout_add_msec(&wd->sc_restart_timeout,
553c6a53756Skn 			    RECOVERYTIME_MSEC);
5547481efa2Scsapuntz 			return;
5557481efa2Scsapuntz 		}
5567481efa2Scsapuntz 		printf("\n");
5577481efa2Scsapuntz 		bp->b_flags |= B_ERROR;
5587481efa2Scsapuntz 		bp->b_error = EIO;
5597481efa2Scsapuntz 		break;
5607481efa2Scsapuntz 	case NOERROR:
5617481efa2Scsapuntz noerror:	if ((wd->sc_wdc_bio.flags & ATA_CORR) || wd->retries > 0)
5627481efa2Scsapuntz 			printf("%s: soft error (corrected)\n",
5637481efa2Scsapuntz 			    wd->sc_dev.dv_xname);
5647481efa2Scsapuntz 	}
56535fa4c39Stedu 	disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid),
566d40269afSderaadt 	    bp->b_blkno, (bp->b_flags & B_READ));
5677481efa2Scsapuntz 	biodone(bp);
5687481efa2Scsapuntz 	wd->openings++;
5697481efa2Scsapuntz 	wdstart(wd);
5707481efa2Scsapuntz }
5717481efa2Scsapuntz 
5727481efa2Scsapuntz void
wdrestart(void * v)57306ad70d2Sgrange wdrestart(void *v)
5747481efa2Scsapuntz {
5757481efa2Scsapuntz 	struct wd_softc *wd = v;
5767481efa2Scsapuntz 	struct buf *bp = wd->sc_bp;
5774072b0baSderaadt 	struct channel_softc *chnl;
5787481efa2Scsapuntz 	int s;
5797481efa2Scsapuntz 	WDCDEBUG_PRINT(("wdrestart %s\n", wd->sc_dev.dv_xname),
5807481efa2Scsapuntz 	    DEBUG_XFERS);
5817481efa2Scsapuntz 
5824072b0baSderaadt 	chnl = (struct channel_softc *)(wd->drvp->chnl_softc);
5834072b0baSderaadt 	if (chnl->dying)
5844072b0baSderaadt 		return;
5854072b0baSderaadt 
5867481efa2Scsapuntz 	s = splbio();
587d40269afSderaadt 	disk_unbusy(&wd->sc_dk, 0, 0, (bp->b_flags & B_READ));
5887481efa2Scsapuntz 	__wdstart(v, bp);
5897481efa2Scsapuntz 	splx(s);
5907481efa2Scsapuntz }
5917481efa2Scsapuntz 
5927481efa2Scsapuntz int
wdread(dev_t dev,struct uio * uio,int flags)59306ad70d2Sgrange wdread(dev_t dev, struct uio *uio, int flags)
5947481efa2Scsapuntz {
5957481efa2Scsapuntz 
5967481efa2Scsapuntz 	WDCDEBUG_PRINT(("wdread\n"), DEBUG_XFERS);
59790f6ca67Smatthew 	return (physio(wdstrategy, dev, B_READ, minphys, uio));
5987481efa2Scsapuntz }
5997481efa2Scsapuntz 
6007481efa2Scsapuntz int
wdwrite(dev_t dev,struct uio * uio,int flags)60106ad70d2Sgrange wdwrite(dev_t dev, struct uio *uio, int flags)
6027481efa2Scsapuntz {
6037481efa2Scsapuntz 
6047481efa2Scsapuntz 	WDCDEBUG_PRINT(("wdwrite\n"), DEBUG_XFERS);
60590f6ca67Smatthew 	return (physio(wdstrategy, dev, B_WRITE, minphys, uio));
6067481efa2Scsapuntz }
6077481efa2Scsapuntz 
6087481efa2Scsapuntz int
wdopen(dev_t dev,int flag,int fmt,struct proc * p)60906ad70d2Sgrange wdopen(dev_t dev, int flag, int fmt, struct proc *p)
6107481efa2Scsapuntz {
6117481efa2Scsapuntz 	struct wd_softc *wd;
6124072b0baSderaadt 	struct channel_softc *chnl;
6137481efa2Scsapuntz 	int unit, part;
6147481efa2Scsapuntz 	int error;
6157481efa2Scsapuntz 
6167481efa2Scsapuntz 	WDCDEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS);
617f97ca3e4Scsapuntz 
618a83b1495Skrw 	unit = DISKUNIT(dev);
619f97ca3e4Scsapuntz 	wd = wdlookup(unit);
6207481efa2Scsapuntz 	if (wd == NULL)
6217481efa2Scsapuntz 		return ENXIO;
6224072b0baSderaadt 	chnl = (struct channel_softc *)(wd->drvp->chnl_softc);
6234072b0baSderaadt 	if (chnl->dying)
6244072b0baSderaadt 		return (ENXIO);
6257481efa2Scsapuntz 
6267481efa2Scsapuntz 	/*
6277481efa2Scsapuntz 	 * If this is the first open of this device, add a reference
6287481efa2Scsapuntz 	 * to the adapter.
6297481efa2Scsapuntz 	 */
63085bbddc9Sderaadt 	if ((error = disk_lock(&wd->sc_dk)) != 0)
6317481efa2Scsapuntz 		goto bad4;
6327481efa2Scsapuntz 
6337481efa2Scsapuntz 	if (wd->sc_dk.dk_openmask != 0) {
6347481efa2Scsapuntz 		/*
6357481efa2Scsapuntz 		 * If any partition is open, but the disk has been invalidated,
6367481efa2Scsapuntz 		 * disallow further opens.
6377481efa2Scsapuntz 		 */
6387481efa2Scsapuntz 		if ((wd->sc_flags & WDF_LOADED) == 0) {
6397481efa2Scsapuntz 			error = EIO;
6407481efa2Scsapuntz 			goto bad3;
6417481efa2Scsapuntz 		}
6427481efa2Scsapuntz 	} else {
6437481efa2Scsapuntz 		if ((wd->sc_flags & WDF_LOADED) == 0) {
6447481efa2Scsapuntz 			wd->sc_flags |= WDF_LOADED;
6457481efa2Scsapuntz 
6467481efa2Scsapuntz 			/* Load the physical device parameters. */
647e1a5e72aScsapuntz 			wd_get_params(wd, AT_WAIT, &wd->sc_params);
6487481efa2Scsapuntz 
6497481efa2Scsapuntz 			/* Load the partition info if not already loaded. */
650df591ed6Sderaadt 			if (wdgetdisklabel(dev, wd,
651df591ed6Sderaadt 			    wd->sc_dk.dk_label, 0) == EIO) {
652df591ed6Sderaadt 				error = EIO;
653df591ed6Sderaadt 				goto bad;
654df591ed6Sderaadt 			}
6557481efa2Scsapuntz 		}
6567481efa2Scsapuntz 	}
6577481efa2Scsapuntz 
658a83b1495Skrw 	part = DISKPART(dev);
6597481efa2Scsapuntz 
660e1d64023Smatthew 	if ((error = disk_openpart(&wd->sc_dk, part, fmt, 1)) != 0)
6617481efa2Scsapuntz 		goto bad;
6627481efa2Scsapuntz 
66385bbddc9Sderaadt 	disk_unlock(&wd->sc_dk);
664f97ca3e4Scsapuntz 	device_unref(&wd->sc_dev);
6657481efa2Scsapuntz 	return 0;
6667481efa2Scsapuntz 
6677481efa2Scsapuntz bad:
6687481efa2Scsapuntz 	if (wd->sc_dk.dk_openmask == 0) {
6697481efa2Scsapuntz 	}
6707481efa2Scsapuntz 
6717481efa2Scsapuntz bad3:
67285bbddc9Sderaadt 	disk_unlock(&wd->sc_dk);
6737481efa2Scsapuntz bad4:
674f97ca3e4Scsapuntz 	device_unref(&wd->sc_dev);
6757481efa2Scsapuntz 	return error;
6767481efa2Scsapuntz }
6777481efa2Scsapuntz 
6787481efa2Scsapuntz int
wdclose(dev_t dev,int flag,int fmt,struct proc * p)67906ad70d2Sgrange wdclose(dev_t dev, int flag, int fmt, struct proc *p)
6807481efa2Scsapuntz {
681f97ca3e4Scsapuntz 	struct wd_softc *wd;
682a83b1495Skrw 	int part = DISKPART(dev);
683f97ca3e4Scsapuntz 
684a83b1495Skrw 	wd = wdlookup(DISKUNIT(dev));
685f97ca3e4Scsapuntz 	if (wd == NULL)
686f97ca3e4Scsapuntz 		return ENXIO;
6877481efa2Scsapuntz 
6887481efa2Scsapuntz 	WDCDEBUG_PRINT(("wdclose\n"), DEBUG_FUNCS);
6895f30bcfcSderaadt 
6905f30bcfcSderaadt 	disk_lock_nointr(&wd->sc_dk);
6917481efa2Scsapuntz 
692e1d64023Smatthew 	disk_closepart(&wd->sc_dk, part, fmt);
6937481efa2Scsapuntz 
6947481efa2Scsapuntz 	if (wd->sc_dk.dk_openmask == 0) {
695edc82994Sderaadt 		wd_flushcache(wd, AT_WAIT);
6967481efa2Scsapuntz 		/* XXXX Must wait for I/O to complete! */
6977481efa2Scsapuntz 	}
6987481efa2Scsapuntz 
69985bbddc9Sderaadt 	disk_unlock(&wd->sc_dk);
700f97ca3e4Scsapuntz 
701f97ca3e4Scsapuntz 	device_unref(&wd->sc_dev);
7025f30bcfcSderaadt 	return (0);
7037481efa2Scsapuntz }
7047481efa2Scsapuntz 
7057481efa2Scsapuntz void
wdgetdefaultlabel(struct wd_softc * wd,struct disklabel * lp)70606ad70d2Sgrange wdgetdefaultlabel(struct wd_softc *wd, struct disklabel *lp)
7077481efa2Scsapuntz {
7087481efa2Scsapuntz 	WDCDEBUG_PRINT(("wdgetdefaultlabel\n"), DEBUG_FUNCS);
709afaefa16Sniklas 	bzero(lp, sizeof(struct disklabel));
7107481efa2Scsapuntz 
7117481efa2Scsapuntz 	lp->d_secsize = DEV_BSIZE;
712e16633b4Sderaadt 	DL_SETDSIZE(lp, wd->sc_capacity);
7137481efa2Scsapuntz 	lp->d_ntracks = wd->sc_params.atap_heads;
7147481efa2Scsapuntz 	lp->d_nsectors = wd->sc_params.atap_sectors;
7157481efa2Scsapuntz 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
716e16633b4Sderaadt 	lp->d_ncylinders = DL_GETDSIZE(lp) / lp->d_secpercyl;
717a2c80ea8Smillert 	if (wd->drvp->ata_vers == -1) {
7187481efa2Scsapuntz 		lp->d_type = DTYPE_ST506;
719bf3a6870Skrw 		strncpy(lp->d_typename, "ST506/MFM/RLL", sizeof lp->d_typename);
7207481efa2Scsapuntz 	} else {
7217481efa2Scsapuntz 		lp->d_type = DTYPE_ESDI;
722bf3a6870Skrw 		strncpy(lp->d_typename, "ESDI/IDE disk", sizeof lp->d_typename);
7237481efa2Scsapuntz 	}
724a2c80ea8Smillert 	/* XXX - user viscopy() like sd.c */
725bf3a6870Skrw 	strncpy(lp->d_packname, wd->sc_params.atap_model, sizeof lp->d_packname);
72622230921Sderaadt 	lp->d_version = 1;
7277481efa2Scsapuntz 
7287481efa2Scsapuntz 	lp->d_magic = DISKMAGIC;
7297481efa2Scsapuntz 	lp->d_magic2 = DISKMAGIC;
7307481efa2Scsapuntz 	lp->d_checksum = dkcksum(lp);
7317481efa2Scsapuntz }
7327481efa2Scsapuntz 
7337481efa2Scsapuntz /*
7347481efa2Scsapuntz  * Fabricate a default disk label, and try to read the correct one.
7357481efa2Scsapuntz  */
736df591ed6Sderaadt int
wdgetdisklabel(dev_t dev,struct wd_softc * wd,struct disklabel * lp,int spoofonly)73706ad70d2Sgrange wdgetdisklabel(dev_t dev, struct wd_softc *wd, struct disklabel *lp,
738c326f117Sderaadt     int spoofonly)
7397481efa2Scsapuntz {
740df591ed6Sderaadt 	int error;
7417481efa2Scsapuntz 
7427481efa2Scsapuntz 	WDCDEBUG_PRINT(("wdgetdisklabel\n"), DEBUG_FUNCS);
7437481efa2Scsapuntz 
7447481efa2Scsapuntz 	wdgetdefaultlabel(wd, lp);
7457481efa2Scsapuntz 
7467481efa2Scsapuntz 	if (wd->drvp->state > RECAL)
7477481efa2Scsapuntz 		wd->drvp->drive_flags |= DRIVE_RESET;
748df591ed6Sderaadt 	error = readdisklabel(DISKLABELDEV(dev), wdstrategy, lp,
749657ac4baSkrw 	    spoofonly);
7507481efa2Scsapuntz 	if (wd->drvp->state > RECAL)
7517481efa2Scsapuntz 		wd->drvp->drive_flags |= DRIVE_RESET;
752df591ed6Sderaadt 	return (error);
7537481efa2Scsapuntz }
7547481efa2Scsapuntz 
7557481efa2Scsapuntz int
wdioctl(dev_t dev,u_long xfer,caddr_t addr,int flag,struct proc * p)75606ad70d2Sgrange wdioctl(dev_t dev, u_long xfer, caddr_t addr, int flag, struct proc *p)
7577481efa2Scsapuntz {
758f97ca3e4Scsapuntz 	struct wd_softc *wd;
759ce578d69Sderaadt 	struct disklabel *lp;
760f97ca3e4Scsapuntz 	int error = 0;
7617481efa2Scsapuntz 
7627481efa2Scsapuntz 	WDCDEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS);
7637481efa2Scsapuntz 
764a83b1495Skrw 	wd = wdlookup(DISKUNIT(dev));
765f97ca3e4Scsapuntz 	if (wd == NULL)
766f97ca3e4Scsapuntz 		return ENXIO;
767f97ca3e4Scsapuntz 
768f97ca3e4Scsapuntz 	if ((wd->sc_flags & WDF_LOADED) == 0) {
769f97ca3e4Scsapuntz 		error = EIO;
770f97ca3e4Scsapuntz 		goto exit;
771f97ca3e4Scsapuntz 	}
7727481efa2Scsapuntz 
7737481efa2Scsapuntz 	switch (xfer) {
7741dfe2d86Sderaadt 	case DIOCRLDINFO:
775ce578d69Sderaadt 		lp = malloc(sizeof(*lp), M_TEMP, M_WAITOK);
776c326f117Sderaadt 		wdgetdisklabel(dev, wd, lp, 0);
777ce578d69Sderaadt 		bcopy(lp, wd->sc_dk.dk_label, sizeof(*lp));
778688f9b10Sderaadt 		free(lp, M_TEMP, sizeof(*lp));
779f97ca3e4Scsapuntz 		goto exit;
780ce578d69Sderaadt 
781c326f117Sderaadt 	case DIOCGPDINFO:
782c326f117Sderaadt 		wdgetdisklabel(dev, wd, (struct disklabel *)addr, 1);
783f97ca3e4Scsapuntz 		goto exit;
784e1a5e72aScsapuntz 
7857481efa2Scsapuntz 	case DIOCGDINFO:
7867481efa2Scsapuntz 		*(struct disklabel *)addr = *(wd->sc_dk.dk_label);
787f97ca3e4Scsapuntz 		goto exit;
7887481efa2Scsapuntz 
7897481efa2Scsapuntz 	case DIOCGPART:
7907481efa2Scsapuntz 		((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
7917481efa2Scsapuntz 		((struct partinfo *)addr)->part =
792a83b1495Skrw 		    &wd->sc_dk.dk_label->d_partitions[DISKPART(dev)];
793f97ca3e4Scsapuntz 		goto exit;
7947481efa2Scsapuntz 
7957481efa2Scsapuntz 	case DIOCWDINFO:
7967481efa2Scsapuntz 	case DIOCSDINFO:
797f97ca3e4Scsapuntz 		if ((flag & FWRITE) == 0) {
798f97ca3e4Scsapuntz 			error = EBADF;
799f97ca3e4Scsapuntz 			goto exit;
800f97ca3e4Scsapuntz 		}
8017481efa2Scsapuntz 
80285bbddc9Sderaadt 		if ((error = disk_lock(&wd->sc_dk)) != 0)
803f97ca3e4Scsapuntz 			goto exit;
8047481efa2Scsapuntz 
8057481efa2Scsapuntz 		error = setdisklabel(wd->sc_dk.dk_label,
806a2f17f2eSmatthew 		    (struct disklabel *)addr, wd->sc_dk.dk_openmask);
8077481efa2Scsapuntz 		if (error == 0) {
8087481efa2Scsapuntz 			if (wd->drvp->state > RECAL)
8097481efa2Scsapuntz 				wd->drvp->drive_flags |= DRIVE_RESET;
8107481efa2Scsapuntz 			if (xfer == DIOCWDINFO)
811a83b1495Skrw 				error = writedisklabel(DISKLABELDEV(dev),
812c326f117Sderaadt 				    wdstrategy, wd->sc_dk.dk_label);
8137481efa2Scsapuntz 		}
8147481efa2Scsapuntz 
81585bbddc9Sderaadt 		disk_unlock(&wd->sc_dk);
816f97ca3e4Scsapuntz 		goto exit;
8177481efa2Scsapuntz 
818805cc48eSsf 	case DIOCCACHESYNC:
819805cc48eSsf 		if ((flag & FWRITE) == 0) {
820805cc48eSsf 			error = EBADF;
821805cc48eSsf 			goto exit;
822805cc48eSsf 		}
823805cc48eSsf 		error = wd_flushcache(wd, AT_WAIT);
824805cc48eSsf 		goto exit;
825805cc48eSsf 
8267481efa2Scsapuntz 	default:
82718885d83Smickey 		error = wdc_ioctl(wd->drvp, xfer, addr, flag, p);
828f97ca3e4Scsapuntz 		goto exit;
8297481efa2Scsapuntz 	}
8307481efa2Scsapuntz 
8317481efa2Scsapuntz #ifdef DIAGNOSTIC
8327481efa2Scsapuntz 	panic("wdioctl: impossible");
8337481efa2Scsapuntz #endif
834f97ca3e4Scsapuntz 
835f97ca3e4Scsapuntz  exit:
836f97ca3e4Scsapuntz 	device_unref(&wd->sc_dev);
837f97ca3e4Scsapuntz 	return (error);
8387481efa2Scsapuntz }
8397481efa2Scsapuntz 
8407481efa2Scsapuntz #ifdef B_FORMAT
8417481efa2Scsapuntz int
wdformat(struct buf * bp)8427481efa2Scsapuntz wdformat(struct buf *bp)
8437481efa2Scsapuntz {
8447481efa2Scsapuntz 
8457481efa2Scsapuntz 	bp->b_flags |= B_FORMAT;
8467481efa2Scsapuntz 	return wdstrategy(bp);
8477481efa2Scsapuntz }
8487481efa2Scsapuntz #endif
8497481efa2Scsapuntz 
8501abdbfdeSderaadt daddr_t
wdsize(dev_t dev)85106ad70d2Sgrange wdsize(dev_t dev)
8527481efa2Scsapuntz {
8537481efa2Scsapuntz 	struct wd_softc *wd;
8542d8e523bSkrw 	struct disklabel *lp;
855f97ca3e4Scsapuntz 	int part, omask;
856c1e37bc9Skrw 	daddr_t size;
8577481efa2Scsapuntz 
8587481efa2Scsapuntz 	WDCDEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS);
8597481efa2Scsapuntz 
860a83b1495Skrw 	wd = wdlookup(DISKUNIT(dev));
8617481efa2Scsapuntz 	if (wd == NULL)
8627481efa2Scsapuntz 		return (-1);
8637481efa2Scsapuntz 
864a83b1495Skrw 	part = DISKPART(dev);
8657481efa2Scsapuntz 	omask = wd->sc_dk.dk_openmask & (1 << part);
8667481efa2Scsapuntz 
867f97ca3e4Scsapuntz 	if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0) {
868f97ca3e4Scsapuntz 		size = -1;
869f97ca3e4Scsapuntz 		goto exit;
870f97ca3e4Scsapuntz 	}
871f97ca3e4Scsapuntz 
8722d8e523bSkrw 	lp = wd->sc_dk.dk_label;
8732d8e523bSkrw 	size = DL_SECTOBLK(lp, DL_GETPSIZE(&lp->d_partitions[part]));
8747481efa2Scsapuntz 	if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0)
875f97ca3e4Scsapuntz 		size = -1;
876f97ca3e4Scsapuntz 
877f97ca3e4Scsapuntz  exit:
878f97ca3e4Scsapuntz 	device_unref(&wd->sc_dev);
8797481efa2Scsapuntz 	return (size);
8807481efa2Scsapuntz }
8817481efa2Scsapuntz 
8827481efa2Scsapuntz /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
8837481efa2Scsapuntz static int wddoingadump = 0;
8847481efa2Scsapuntz static int wddumprecalibrated = 0;
8857481efa2Scsapuntz static int wddumpmulti = 1;
8867481efa2Scsapuntz 
8877481efa2Scsapuntz /*
8887481efa2Scsapuntz  * Dump core after a system crash.
8897481efa2Scsapuntz  */
8907481efa2Scsapuntz int
wddump(dev_t dev,daddr_t blkno,caddr_t va,size_t size)8911abdbfdeSderaadt wddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
8927481efa2Scsapuntz {
8937481efa2Scsapuntz 	struct wd_softc *wd;	/* disk unit to do the I/O */
8947481efa2Scsapuntz 	struct disklabel *lp;   /* disk's disklabel */
8957481efa2Scsapuntz 	int unit, part;
8967481efa2Scsapuntz 	int nblks;	/* total number of sectors left to write */
897b2c3bd7dSkrw 	int nwrt;	/* sectors to write with current i/o. */
8987481efa2Scsapuntz 	int err;
8997481efa2Scsapuntz 	char errbuf[256];
9007481efa2Scsapuntz 
9017481efa2Scsapuntz 	/* Check if recursive dump; if so, punt. */
9027481efa2Scsapuntz 	if (wddoingadump)
9037481efa2Scsapuntz 		return EFAULT;
9047481efa2Scsapuntz 	wddoingadump = 1;
9057481efa2Scsapuntz 
906a83b1495Skrw 	unit = DISKUNIT(dev);
907f97ca3e4Scsapuntz 	wd = wdlookup(unit);
908f97ca3e4Scsapuntz 	if (wd == NULL)
9097481efa2Scsapuntz 		return ENXIO;
9107481efa2Scsapuntz 
911a83b1495Skrw 	part = DISKPART(dev);
9127481efa2Scsapuntz 
9137481efa2Scsapuntz 	/* Make sure it was initialized. */
9147481efa2Scsapuntz 	if (wd->drvp->state < READY)
9157481efa2Scsapuntz 		return ENXIO;
9167481efa2Scsapuntz 
9177481efa2Scsapuntz 	/* Convert to disk sectors.  Request must be a multiple of size. */
9187481efa2Scsapuntz 	lp = wd->sc_dk.dk_label;
9197481efa2Scsapuntz 	if ((size % lp->d_secsize) != 0)
9207481efa2Scsapuntz 		return EFAULT;
9217481efa2Scsapuntz 	nblks = size / lp->d_secsize;
9227481efa2Scsapuntz 	blkno = blkno / (lp->d_secsize / DEV_BSIZE);
9237481efa2Scsapuntz 
9247481efa2Scsapuntz 	/* Check transfer bounds against partition size. */
925e16633b4Sderaadt 	if ((blkno < 0) || ((blkno + nblks) > DL_GETPSIZE(&lp->d_partitions[part])))
9267481efa2Scsapuntz 		return EINVAL;
9277481efa2Scsapuntz 
9287481efa2Scsapuntz 	/* Offset block number to start of partition. */
929e16633b4Sderaadt 	blkno += DL_GETPOFFSET(&lp->d_partitions[part]);
9307481efa2Scsapuntz 
9317481efa2Scsapuntz 	/* Recalibrate, if first dump transfer. */
9327481efa2Scsapuntz 	if (wddumprecalibrated == 0) {
9337481efa2Scsapuntz 		wddumpmulti = wd->sc_multi;
9347481efa2Scsapuntz 		wddumprecalibrated = 1;
9357481efa2Scsapuntz 		wd->drvp->state = RECAL;
9367481efa2Scsapuntz 	}
9377481efa2Scsapuntz 
9387481efa2Scsapuntz 	while (nblks > 0) {
939b2c3bd7dSkrw 		nwrt = min(nblks, wddumpmulti);
9407481efa2Scsapuntz 		wd->sc_wdc_bio.blkno = blkno;
9417481efa2Scsapuntz 		wd->sc_wdc_bio.flags = ATA_POLL;
9424b4c6f4eSgluk 		if (wd->sc_flags & WDF_LBA48)
9434b4c6f4eSgluk 			wd->sc_wdc_bio.flags |= ATA_LBA48;
9447481efa2Scsapuntz 		if (wd->sc_flags & WDF_LBA)
9457481efa2Scsapuntz 			wd->sc_wdc_bio.flags |= ATA_LBA;
946b2c3bd7dSkrw 		wd->sc_wdc_bio.bcount = nwrt * lp->d_secsize;
9477481efa2Scsapuntz 		wd->sc_wdc_bio.databuf = va;
948f97ca3e4Scsapuntz 		wd->sc_wdc_bio.wd = wd;
9497481efa2Scsapuntz #ifndef WD_DUMP_NOT_TRUSTED
9507481efa2Scsapuntz 		switch (wdc_ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
9517481efa2Scsapuntz 		case WDC_TRY_AGAIN:
9527481efa2Scsapuntz 			panic("wddump: try again");
9537481efa2Scsapuntz 			break;
9547481efa2Scsapuntz 		case WDC_QUEUED:
9557481efa2Scsapuntz 			panic("wddump: polled command has been queued");
9567481efa2Scsapuntz 			break;
9577481efa2Scsapuntz 		case WDC_COMPLETE:
9587481efa2Scsapuntz 			break;
9597481efa2Scsapuntz 		}
9607481efa2Scsapuntz 		switch(wd->sc_wdc_bio.error) {
9617481efa2Scsapuntz 		case TIMEOUT:
9627481efa2Scsapuntz 			printf("wddump: device timed out");
9637481efa2Scsapuntz 			err = EIO;
9647481efa2Scsapuntz 			break;
9657481efa2Scsapuntz 		case ERR_DF:
9667481efa2Scsapuntz 			printf("wddump: drive fault");
9677481efa2Scsapuntz 			err = EIO;
9687481efa2Scsapuntz 			break;
9697481efa2Scsapuntz 		case ERR_DMA:
9707481efa2Scsapuntz 			printf("wddump: DMA error");
9717481efa2Scsapuntz 			err = EIO;
9727481efa2Scsapuntz 			break;
9737481efa2Scsapuntz 		case ERROR:
9747481efa2Scsapuntz 			errbuf[0] = '\0';
975b6136c85Sho 			ata_perror(wd->drvp, wd->sc_wdc_bio.r_error, errbuf,
976b6136c85Sho 			    sizeof errbuf);
9777481efa2Scsapuntz 			printf("wddump: %s", errbuf);
9787481efa2Scsapuntz 			err = EIO;
9797481efa2Scsapuntz 			break;
9807481efa2Scsapuntz 		case NOERROR:
9817481efa2Scsapuntz 			err = 0;
9827481efa2Scsapuntz 			break;
9837481efa2Scsapuntz 		default:
9847481efa2Scsapuntz 			panic("wddump: unknown error type");
9857481efa2Scsapuntz 		}
9867481efa2Scsapuntz 		if (err != 0) {
9877481efa2Scsapuntz 			printf("\n");
9887481efa2Scsapuntz 			return err;
9897481efa2Scsapuntz 		}
9907481efa2Scsapuntz #else	/* WD_DUMP_NOT_TRUSTED */
9917481efa2Scsapuntz 		/* Let's just talk about this first... */
9927481efa2Scsapuntz 		printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
9937481efa2Scsapuntz 		    unit, va, cylin, head, sector);
9947481efa2Scsapuntz 		delay(500 * 1000);	/* half a second */
9957481efa2Scsapuntz #endif
9967481efa2Scsapuntz 
9977481efa2Scsapuntz 		/* update block count */
998b2c3bd7dSkrw 		nblks -= nwrt;
999b2c3bd7dSkrw 		blkno += nwrt;
1000b2c3bd7dSkrw 		va += nwrt * lp->d_secsize;
10017481efa2Scsapuntz 	}
10027481efa2Scsapuntz 
10037481efa2Scsapuntz 	wddoingadump = 0;
10047481efa2Scsapuntz 	return 0;
10057481efa2Scsapuntz }
10067481efa2Scsapuntz 
10077481efa2Scsapuntz int
wd_get_params(struct wd_softc * wd,u_int8_t flags,struct ataparams * params)100806ad70d2Sgrange wd_get_params(struct wd_softc *wd, u_int8_t flags, struct ataparams *params)
10097481efa2Scsapuntz {
10107481efa2Scsapuntz 	switch (ata_get_params(wd->drvp, flags, params)) {
10117481efa2Scsapuntz 	case CMD_AGAIN:
10127481efa2Scsapuntz 		return 1;
10137481efa2Scsapuntz 	case CMD_ERR:
1014e9207eccSuwe 		/* If we already have drive parameters, reuse them. */
1015e880da88Suwe 		if (wd->sc_params.atap_cylinders != 0) {
1016e9207eccSuwe 			if (params != &wd->sc_params)
1017e9207eccSuwe 				bcopy(&wd->sc_params, params,
1018e9207eccSuwe 				    sizeof(struct ataparams));
1019e880da88Suwe 			return 0;
1020e880da88Suwe 		}
10217481efa2Scsapuntz 		/*
10227481efa2Scsapuntz 		 * We `know' there's a drive here; just assume it's old.
10237481efa2Scsapuntz 		 * This geometry is only used to read the MBR and print a
10247481efa2Scsapuntz 		 * (false) attach message.
10257481efa2Scsapuntz 		 */
1026e9207eccSuwe 		bzero(params, sizeof(struct ataparams));
10277481efa2Scsapuntz 		strncpy(params->atap_model, "ST506",
10287481efa2Scsapuntz 		    sizeof params->atap_model);
10297481efa2Scsapuntz 		params->atap_config = ATA_CFG_FIXED;
10307481efa2Scsapuntz 		params->atap_cylinders = 1024;
10317481efa2Scsapuntz 		params->atap_heads = 8;
10327481efa2Scsapuntz 		params->atap_sectors = 17;
10337481efa2Scsapuntz 		params->atap_multi = 1;
10347481efa2Scsapuntz 		params->atap_capabilities1 = params->atap_capabilities2 = 0;
10357481efa2Scsapuntz 		wd->drvp->ata_vers = -1; /* Mark it as pre-ATA */
10367481efa2Scsapuntz 		return 0;
10377481efa2Scsapuntz 	case CMD_OK:
10387481efa2Scsapuntz 		return 0;
10397481efa2Scsapuntz 	default:
10407481efa2Scsapuntz 		panic("wd_get_params: bad return code from ata_get_params");
10417481efa2Scsapuntz 		/* NOTREACHED */
10427481efa2Scsapuntz 	}
10437481efa2Scsapuntz }
10447481efa2Scsapuntz 
1045805cc48eSsf int
wd_flushcache(struct wd_softc * wd,int flags)104606ad70d2Sgrange wd_flushcache(struct wd_softc *wd, int flags)
10477481efa2Scsapuntz {
10487481efa2Scsapuntz 	struct wdc_command wdc_c;
10497481efa2Scsapuntz 
10507481efa2Scsapuntz 	if (wd->drvp->ata_vers < 4) /* WDCC_FLUSHCACHE is here since ATA-4 */
1051805cc48eSsf 		return EIO;
1052afaefa16Sniklas 	bzero(&wdc_c, sizeof(struct wdc_command));
10535a1200c8Sgrange 	wdc_c.r_command = (wd->sc_flags & WDF_LBA48 ? WDCC_FLUSHCACHE_EXT :
10545a1200c8Sgrange 	    WDCC_FLUSHCACHE);
10557481efa2Scsapuntz 	wdc_c.r_st_bmask = WDCS_DRDY;
10567481efa2Scsapuntz 	wdc_c.r_st_pmask = WDCS_DRDY;
1057edc82994Sderaadt 	wdc_c.flags = flags;
10587481efa2Scsapuntz 	wdc_c.timeout = 30000; /* 30s timeout */
10597481efa2Scsapuntz 	if (wdc_exec_command(wd->drvp, &wdc_c) != WDC_COMPLETE) {
10607481efa2Scsapuntz 		printf("%s: flush cache command didn't complete\n",
10617481efa2Scsapuntz 		    wd->sc_dev.dv_xname);
1062805cc48eSsf 		return EIO;
10637481efa2Scsapuntz 	}
1064805cc48eSsf 	if (wdc_c.flags & ERR_NODEV)
1065805cc48eSsf 		return ENODEV;
10667481efa2Scsapuntz 	if (wdc_c.flags & AT_TIMEOU) {
10677481efa2Scsapuntz 		printf("%s: flush cache command timeout\n",
10687481efa2Scsapuntz 		    wd->sc_dev.dv_xname);
1069805cc48eSsf 		return EIO;
1070805cc48eSsf 	}
1071805cc48eSsf 	if (wdc_c.flags & AT_ERROR) {
1072805cc48eSsf 		if (wdc_c.r_error == WDCE_ABRT) /* command not supported */
1073805cc48eSsf 			return ENODEV;
1074805cc48eSsf 		printf("%s: flush cache command: error 0x%x\n",
1075805cc48eSsf 		    wd->sc_dev.dv_xname, wdc_c.r_error);
1076805cc48eSsf 		return EIO;
10777481efa2Scsapuntz 	}
10787481efa2Scsapuntz 	if (wdc_c.flags & AT_DF) {
10797481efa2Scsapuntz 		printf("%s: flush cache command: drive fault\n",
10807481efa2Scsapuntz 		    wd->sc_dev.dv_xname);
1081805cc48eSsf 		return EIO;
10827481efa2Scsapuntz 	}
1083805cc48eSsf 	return 0;
10847481efa2Scsapuntz }
10857481efa2Scsapuntz 
10867481efa2Scsapuntz void
wd_standby(struct wd_softc * wd,int flags)1087f8fc1e98Skettenis wd_standby(struct wd_softc *wd, int flags)
1088f8fc1e98Skettenis {
1089f8fc1e98Skettenis 	struct wdc_command wdc_c;
1090f8fc1e98Skettenis 
1091f8fc1e98Skettenis 	bzero(&wdc_c, sizeof(struct wdc_command));
1092f8fc1e98Skettenis 	wdc_c.r_command = WDCC_STANDBY_IMMED;
1093f8fc1e98Skettenis 	wdc_c.r_st_bmask = WDCS_DRDY;
1094f8fc1e98Skettenis 	wdc_c.r_st_pmask = WDCS_DRDY;
1095edc82994Sderaadt 	wdc_c.flags = flags;
10966693db94Skettenis 	wdc_c.timeout = 30000; /* 30s timeout */
1097f8fc1e98Skettenis 	if (wdc_exec_command(wd->drvp, &wdc_c) != WDC_COMPLETE) {
1098f8fc1e98Skettenis 		printf("%s: standby command didn't complete\n",
1099f8fc1e98Skettenis 		    wd->sc_dev.dv_xname);
1100f8fc1e98Skettenis 	}
1101f8fc1e98Skettenis 	if (wdc_c.flags & AT_TIMEOU) {
1102f8fc1e98Skettenis 		printf("%s: standby command timeout\n",
1103f8fc1e98Skettenis 		    wd->sc_dev.dv_xname);
1104f8fc1e98Skettenis 	}
1105f8fc1e98Skettenis 	if (wdc_c.flags & AT_DF) {
1106f8fc1e98Skettenis 		printf("%s: standby command: drive fault\n",
1107f8fc1e98Skettenis 		    wd->sc_dev.dv_xname);
1108f8fc1e98Skettenis 	}
1109f8fc1e98Skettenis 	/*
1110f8fc1e98Skettenis 	 * Ignore error register, it shouldn't report anything else
1111f8fc1e98Skettenis 	 * than COMMAND ABORTED, which means the device doesn't support
1112f8fc1e98Skettenis 	 * standby
1113f8fc1e98Skettenis 	 */
1114f8fc1e98Skettenis }
1115