xref: /openbsd-src/sys/dev/ata/ata.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*      $OpenBSD: ata.c,v 1.33 2011/06/22 14:17:01 krw Exp $      */
2 /*      $NetBSD: ata.c,v 1.9 1999/04/15 09:41:09 bouyer Exp $      */
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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/param.h>
28 #include <sys/systm.h>
29 #include <sys/kernel.h>
30 #include <sys/file.h>
31 #include <sys/stat.h>
32 #include <sys/malloc.h>
33 #include <sys/device.h>
34 #include <sys/syslog.h>
35 #include <sys/pool.h>
36 
37 #include <machine/bus.h>
38 
39 #include <dev/ata/atareg.h>
40 #include <dev/ata/atavar.h>
41 #include <dev/ic/wdcreg.h>
42 #include <dev/ic/wdcvar.h>
43 
44 #define DEBUG_FUNCS  0x08
45 #define DEBUG_PROBE  0x10
46 #ifdef WDCDEBUG
47 extern int wdcdebug_mask; /* init'ed in wdc.c */
48 #define WDCDEBUG_PRINT(args, level) do {	\
49 	if ((wdcdebug_mask & (level)) != 0)	\
50 		printf args;			\
51 } while (0)
52 #else
53 #define WDCDEBUG_PRINT(args, level)
54 #endif
55 
56 #define ATAPARAMS_SIZE 512
57 
58 /* Get the disk's parameters */
59 int
60 ata_get_params(struct ata_drive_datas *drvp, u_int8_t flags,
61     struct ataparams *prms)
62 {
63 	char *tb;
64 	struct wdc_command wdc_c;
65 	int i;
66 	u_int16_t *p;
67 	int ret;
68 
69 	WDCDEBUG_PRINT(("ata_get_params\n"), DEBUG_FUNCS);
70 
71 	bzero(&wdc_c, sizeof(struct wdc_command));
72 
73 	if (drvp->drive_flags & DRIVE_ATA) {
74 		wdc_c.r_command = WDCC_IDENTIFY;
75 		wdc_c.r_st_bmask = WDCS_DRDY;
76 		wdc_c.r_st_pmask = 0;
77 		wdc_c.timeout = 3000; /* 3s */
78 	} else if (drvp->drive_flags & DRIVE_ATAPI) {
79 		wdc_c.r_command = ATAPI_IDENTIFY_DEVICE;
80 		wdc_c.r_st_bmask = 0;
81 		wdc_c.r_st_pmask = 0;
82 		wdc_c.timeout = 10000; /* 10s */
83 	} else {
84 		WDCDEBUG_PRINT(("ata_get_params: no disks\n"),
85 		    DEBUG_FUNCS|DEBUG_PROBE);
86 		return CMD_ERR;
87 	}
88 
89 	tb = dma_alloc(ATAPARAMS_SIZE, PR_NOWAIT | PR_ZERO);
90 	if (tb == NULL)
91 		return CMD_AGAIN;
92 	wdc_c.flags = AT_READ | flags;
93 	wdc_c.data = tb;
94 	wdc_c.bcount = ATAPARAMS_SIZE;
95 
96 	if ((ret = wdc_exec_command(drvp, &wdc_c)) != WDC_COMPLETE) {
97 		WDCDEBUG_PRINT(("%s: wdc_exec_command failed: %d\n",
98 		    __func__, ret), DEBUG_PROBE);
99 		dma_free(tb, ATAPARAMS_SIZE);
100 		return CMD_AGAIN;
101 	}
102 
103 	if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
104 		WDCDEBUG_PRINT(("%s: IDENTIFY failed: 0x%x\n", __func__,
105 		    wdc_c.flags), DEBUG_PROBE);
106 
107 		dma_free(tb, ATAPARAMS_SIZE);
108 		return CMD_ERR;
109 	} else {
110 #if BYTE_ORDER == BIG_ENDIAN
111 		/* All the fields in the params structure are 16-bit
112 		   integers except for the ID strings which are char
113 		   strings.  The 16-bit integers are currently in
114 		   memory in little-endian, regardless of architecture.
115 		   So, they need to be swapped on big-endian architectures
116 		   before they are accessed through the ataparams structure.
117 
118 		   The swaps below avoid touching the char strings.
119 		*/
120 
121 		swap16_multi((u_int16_t *)tb, 10);
122 		swap16_multi((u_int16_t *)tb + 20, 3);
123 		swap16_multi((u_int16_t *)tb + 47, ATAPARAMS_SIZE / 2 - 47);
124 #endif
125 		/* Read in parameter block. */
126 		bcopy(tb, prms, sizeof(struct ataparams));
127 
128 		/*
129 		 * Shuffle string byte order.
130 		 * ATAPI Mitsumi and NEC drives don't need this.
131 		 */
132 		if ((prms->atap_config & WDC_CFG_ATAPI_MASK) ==
133 		    WDC_CFG_ATAPI &&
134 		    ((prms->atap_model[0] == 'N' &&
135 			prms->atap_model[1] == 'E') ||
136 		     (prms->atap_model[0] == 'F' &&
137 			 prms->atap_model[1] == 'X'))) {
138 			dma_free(tb, ATAPARAMS_SIZE);
139 			return CMD_OK;
140 		}
141 		for (i = 0; i < sizeof(prms->atap_model); i += 2) {
142 			p = (u_short *)(prms->atap_model + i);
143 			*p = swap16(*p);
144 		}
145 		for (i = 0; i < sizeof(prms->atap_serial); i += 2) {
146 			p = (u_short *)(prms->atap_serial + i);
147 			*p = swap16(*p);
148 		}
149 		for (i = 0; i < sizeof(prms->atap_revision); i += 2) {
150 			p = (u_short *)(prms->atap_revision + i);
151 			*p = swap16(*p);
152 		}
153 
154 		dma_free(tb, ATAPARAMS_SIZE);
155 		return CMD_OK;
156 	}
157 }
158 
159 int
160 ata_set_mode(struct ata_drive_datas *drvp, u_int8_t mode, u_int8_t flags)
161 {
162 	struct wdc_command wdc_c;
163 
164 	WDCDEBUG_PRINT(("%s: mode=0x%x, flags=0x%x\n", __func__,
165 	    mode, flags), DEBUG_PROBE);
166 
167 	bzero(&wdc_c, sizeof(struct wdc_command));
168 
169 	wdc_c.r_command = SET_FEATURES;
170 	wdc_c.r_st_bmask = 0;
171 	wdc_c.r_st_pmask = 0;
172 	wdc_c.r_precomp = WDSF_SET_MODE;
173 	wdc_c.r_count = mode;
174 	wdc_c.flags = flags;
175 	wdc_c.timeout = 1000; /* 1s */
176 	if (wdc_exec_command(drvp, &wdc_c) != WDC_COMPLETE)
177 		return CMD_AGAIN;
178 
179 	WDCDEBUG_PRINT(("%s: after wdc_exec_command() wdc_c.flags=0x%x\n",
180 	    __func__, wdc_c.flags), DEBUG_PROBE);
181 
182 	if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
183 		return CMD_ERR;
184 	}
185 	return CMD_OK;
186 }
187 
188 void
189 ata_dmaerr(struct ata_drive_datas *drvp)
190 {
191 	/*
192 	 * Downgrade decision: if we get NERRS_MAX in NXFER.
193 	 * We start with n_dmaerrs set to NERRS_MAX-1 so that the
194 	 * first error within the first NXFER ops will immediately trigger
195 	 * a downgrade.
196 	 * If we got an error and n_xfers is bigger than NXFER reset counters.
197 	 */
198 	drvp->n_dmaerrs++;
199 	if (drvp->n_dmaerrs >= NERRS_MAX && drvp->n_xfers <= NXFER) {
200 		wdc_downgrade_mode(drvp);
201 		drvp->n_dmaerrs = NERRS_MAX-1;
202 		drvp->n_xfers = 0;
203 		return;
204 	}
205 	if (drvp->n_xfers > NXFER) {
206 		drvp->n_dmaerrs = 1; /* just got an error */
207 		drvp->n_xfers = 1; /* restart counting from this error */
208 	}
209 }
210 
211 void
212 ata_perror(struct ata_drive_datas *drvp, int errno, char *buf, size_t buf_len)
213 {
214 	static char *errstr0_3[] = {"address mark not found",
215 	    "track 0 not found", "aborted command", "media change requested",
216 	    "id not found", "media changed", "uncorrectable data error",
217 	    "bad block detected"};
218 	static char *errstr4_5[] = {"",
219 	    "no media/write protected", "aborted command",
220 	    "media change requested", "id not found", "media changed",
221 	    "uncorrectable data error", "interface CRC error"};
222 	char **errstr;
223 	int i;
224 	char *sep = "";
225 	size_t len = 0;
226 
227 	if (drvp->ata_vers >= 4)
228 		errstr = errstr4_5;
229 	else
230 		errstr = errstr0_3;
231 
232 	if (errno == 0) {
233 		snprintf(buf, buf_len, "error not notified");
234 	}
235 
236 	for (i = 0; i < 8; i++) {
237 		if (errno & (1 << i)) {
238 			snprintf(buf + len, buf_len - len, "%s%s", sep,
239 			    errstr[i]);
240 			len = strlen(buf);
241 			sep = ", ";
242 		}
243 	}
244 }
245