xref: /openbsd-src/sys/dev/ata/ata.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*      $OpenBSD: ata.c,v 1.35 2015/08/17 15:36:29 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 
43 #define DEBUG_FUNCS  0x08
44 #define DEBUG_PROBE  0x10
45 #ifdef WDCDEBUG
46 extern int wdcdebug_mask; /* init'ed in wdc.c */
47 #define WDCDEBUG_PRINT(args, level) do {	\
48 	if ((wdcdebug_mask & (level)) != 0)	\
49 		printf args;			\
50 } while (0)
51 #else
52 #define WDCDEBUG_PRINT(args, level)
53 #endif
54 
55 #define ATAPARAMS_SIZE 512
56 
57 /* Get the disk's parameters */
58 int
59 ata_get_params(struct ata_drive_datas *drvp, u_int8_t flags,
60     struct ataparams *prms)
61 {
62 	char *tb;
63 	struct wdc_command wdc_c;
64 	int i;
65 	u_int16_t *p;
66 	int ret;
67 
68 	WDCDEBUG_PRINT(("ata_get_params\n"), DEBUG_FUNCS);
69 
70 	bzero(&wdc_c, sizeof(struct wdc_command));
71 
72 	if (drvp->drive_flags & DRIVE_ATA) {
73 		wdc_c.r_command = WDCC_IDENTIFY;
74 		wdc_c.r_st_bmask = WDCS_DRDY;
75 		wdc_c.r_st_pmask = 0;
76 		wdc_c.timeout = 3000; /* 3s */
77 	} else if (drvp->drive_flags & DRIVE_ATAPI) {
78 		wdc_c.r_command = ATAPI_IDENTIFY_DEVICE;
79 		wdc_c.r_st_bmask = 0;
80 		wdc_c.r_st_pmask = 0;
81 		wdc_c.timeout = 10000; /* 10s */
82 	} else {
83 		WDCDEBUG_PRINT(("ata_get_params: no disks\n"),
84 		    DEBUG_FUNCS|DEBUG_PROBE);
85 		return CMD_ERR;
86 	}
87 
88 	tb = dma_alloc(ATAPARAMS_SIZE, PR_NOWAIT | PR_ZERO);
89 	if (tb == NULL)
90 		return CMD_AGAIN;
91 	wdc_c.flags = AT_READ | flags;
92 	wdc_c.data = tb;
93 	wdc_c.bcount = ATAPARAMS_SIZE;
94 
95 	if ((ret = wdc_exec_command(drvp, &wdc_c)) != WDC_COMPLETE) {
96 		WDCDEBUG_PRINT(("%s: wdc_exec_command failed: %d\n",
97 		    __func__, ret), DEBUG_PROBE);
98 		dma_free(tb, ATAPARAMS_SIZE);
99 		return CMD_AGAIN;
100 	}
101 
102 	if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
103 		WDCDEBUG_PRINT(("%s: IDENTIFY failed: 0x%x\n", __func__,
104 		    wdc_c.flags), DEBUG_PROBE);
105 
106 		dma_free(tb, ATAPARAMS_SIZE);
107 		return CMD_ERR;
108 	} else {
109 #if BYTE_ORDER == BIG_ENDIAN
110 		/* All the fields in the params structure are 16-bit
111 		   integers except for the ID strings which are char
112 		   strings.  The 16-bit integers are currently in
113 		   memory in little-endian, regardless of architecture.
114 		   So, they need to be swapped on big-endian architectures
115 		   before they are accessed through the ataparams structure.
116 
117 		   The swaps below avoid touching the char strings.
118 		*/
119 
120 		swap16_multi((u_int16_t *)tb, 10);
121 		swap16_multi((u_int16_t *)tb + 20, 3);
122 		swap16_multi((u_int16_t *)tb + 47, ATAPARAMS_SIZE / 2 - 47);
123 #endif
124 		/* Read in parameter block. */
125 		bcopy(tb, prms, sizeof(struct ataparams));
126 
127 		/*
128 		 * Shuffle string byte order.
129 		 * ATAPI Mitsumi and NEC drives don't need this.
130 		 */
131 		if ((prms->atap_config & WDC_CFG_ATAPI_MASK) ==
132 		    WDC_CFG_ATAPI &&
133 		    ((prms->atap_model[0] == 'N' &&
134 			prms->atap_model[1] == 'E') ||
135 		     (prms->atap_model[0] == 'F' &&
136 			 prms->atap_model[1] == 'X'))) {
137 			dma_free(tb, ATAPARAMS_SIZE);
138 			return CMD_OK;
139 		}
140 		for (i = 0; i < sizeof(prms->atap_model); i += 2) {
141 			p = (u_short *)(prms->atap_model + i);
142 			*p = swap16(*p);
143 		}
144 		for (i = 0; i < sizeof(prms->atap_serial); i += 2) {
145 			p = (u_short *)(prms->atap_serial + i);
146 			*p = swap16(*p);
147 		}
148 		for (i = 0; i < sizeof(prms->atap_revision); i += 2) {
149 			p = (u_short *)(prms->atap_revision + i);
150 			*p = swap16(*p);
151 		}
152 
153 		dma_free(tb, ATAPARAMS_SIZE);
154 		return CMD_OK;
155 	}
156 }
157 
158 int
159 ata_set_mode(struct ata_drive_datas *drvp, u_int8_t mode, u_int8_t flags)
160 {
161 	struct wdc_command wdc_c;
162 
163 	WDCDEBUG_PRINT(("%s: mode=0x%x, flags=0x%x\n", __func__,
164 	    mode, flags), DEBUG_PROBE);
165 
166 	bzero(&wdc_c, sizeof(struct wdc_command));
167 
168 	wdc_c.r_command = SET_FEATURES;
169 	wdc_c.r_st_bmask = 0;
170 	wdc_c.r_st_pmask = 0;
171 	wdc_c.r_features = WDSF_SET_MODE;
172 	wdc_c.r_count = mode;
173 	wdc_c.flags = flags;
174 	wdc_c.timeout = 1000; /* 1s */
175 	if (wdc_exec_command(drvp, &wdc_c) != WDC_COMPLETE)
176 		return CMD_AGAIN;
177 
178 	WDCDEBUG_PRINT(("%s: after wdc_exec_command() wdc_c.flags=0x%x\n",
179 	    __func__, wdc_c.flags), DEBUG_PROBE);
180 
181 	if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
182 		return CMD_ERR;
183 	}
184 	return CMD_OK;
185 }
186 
187 void
188 ata_dmaerr(struct ata_drive_datas *drvp)
189 {
190 	/*
191 	 * Downgrade decision: if we get NERRS_MAX in NXFER.
192 	 * We start with n_dmaerrs set to NERRS_MAX-1 so that the
193 	 * first error within the first NXFER ops will immediately trigger
194 	 * a downgrade.
195 	 * If we got an error and n_xfers is bigger than NXFER reset counters.
196 	 */
197 	drvp->n_dmaerrs++;
198 	if (drvp->n_dmaerrs >= NERRS_MAX && drvp->n_xfers <= NXFER) {
199 		wdc_downgrade_mode(drvp);
200 		drvp->n_dmaerrs = NERRS_MAX-1;
201 		drvp->n_xfers = 0;
202 		return;
203 	}
204 	if (drvp->n_xfers > NXFER) {
205 		drvp->n_dmaerrs = 1; /* just got an error */
206 		drvp->n_xfers = 1; /* restart counting from this error */
207 	}
208 }
209 
210 void
211 ata_perror(struct ata_drive_datas *drvp, int errno, char *buf, size_t buf_len)
212 {
213 	static char *errstr0_3[] = {"address mark not found",
214 	    "track 0 not found", "aborted command", "media change requested",
215 	    "id not found", "media changed", "uncorrectable data error",
216 	    "bad block detected"};
217 	static char *errstr4_5[] = {"",
218 	    "no media/write protected", "aborted command",
219 	    "media change requested", "id not found", "media changed",
220 	    "uncorrectable data error", "interface CRC error"};
221 	char **errstr;
222 	int i;
223 	char *sep = "";
224 	size_t len = 0;
225 
226 	if (drvp->ata_vers >= 4)
227 		errstr = errstr4_5;
228 	else
229 		errstr = errstr0_3;
230 
231 	if (errno == 0) {
232 		snprintf(buf, buf_len, "error not notified");
233 	}
234 
235 	for (i = 0; i < 8; i++) {
236 		if (errno & (1 << i)) {
237 			snprintf(buf + len, buf_len - len, "%s%s", sep,
238 			    errstr[i]);
239 			len = strlen(buf);
240 			sep = ", ";
241 		}
242 	}
243 }
244