1 /* $OpenBSD: ata.c,v 1.24 2004/01/15 21:37:57 grange 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 * 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 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/file.h>
36 #include <sys/stat.h>
37 #include <sys/malloc.h>
38 #include <sys/device.h>
39 #include <sys/syslog.h>
40
41 #include <machine/bus.h>
42
43 #include <dev/ata/atareg.h>
44 #include <dev/ata/atavar.h>
45 #include <dev/ic/wdcreg.h>
46 #include <dev/ic/wdcvar.h>
47
48 #define DEBUG_FUNCS 0x08
49 #define DEBUG_PROBE 0x10
50 #ifdef WDCDEBUG
51 extern int wdcdebug_mask; /* init'ed in wdc.c */
52 #define WDCDEBUG_PRINT(args, level) do { \
53 if ((wdcdebug_mask & (level)) != 0) \
54 printf args; \
55 } while (0)
56 #else
57 #define WDCDEBUG_PRINT(args, level)
58 #endif
59
60 #define ATAPARAMS_SIZE 512
61
62 /* Get the disk's parameters */
63 int
ata_get_params(struct ata_drive_datas * drvp,u_int8_t flags,struct ataparams * prms)64 ata_get_params(struct ata_drive_datas *drvp, u_int8_t flags,
65 struct ataparams *prms)
66 {
67 char tb[ATAPARAMS_SIZE];
68 struct wdc_command wdc_c;
69 int i;
70 u_int16_t *p;
71 int ret;
72
73 WDCDEBUG_PRINT(("ata_get_parms\n"), DEBUG_FUNCS);
74
75 bzero(tb, sizeof(tb));
76 bzero(prms, sizeof(struct ataparams));
77 bzero(&wdc_c, sizeof(struct wdc_command));
78
79 if (drvp->drive_flags & DRIVE_ATA) {
80 wdc_c.r_command = WDCC_IDENTIFY;
81 wdc_c.r_st_bmask = WDCS_DRDY;
82 wdc_c.r_st_pmask = 0;
83 wdc_c.timeout = 3000; /* 3s */
84 } else if (drvp->drive_flags & DRIVE_ATAPI) {
85 wdc_c.r_command = ATAPI_IDENTIFY_DEVICE;
86 wdc_c.r_st_bmask = 0;
87 wdc_c.r_st_pmask = 0;
88 wdc_c.timeout = 10000; /* 10s */
89 } else {
90 WDCDEBUG_PRINT(("wdc_ata_get_parms: no disks\n"),
91 DEBUG_FUNCS|DEBUG_PROBE);
92 return CMD_ERR;
93 }
94 wdc_c.flags = AT_READ | flags;
95 wdc_c.data = tb;
96 wdc_c.bcount = ATAPARAMS_SIZE;
97
98 if ((ret = wdc_exec_command(drvp, &wdc_c)) != WDC_COMPLETE) {
99 WDCDEBUG_PRINT(("%s: wdc_exec_command failed: %d\n",
100 __func__, ret), DEBUG_PROBE);
101 return CMD_AGAIN;
102 }
103
104 if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
105 WDCDEBUG_PRINT(("%s: IDENTIFY failed: 0x%x\n", __func__,
106 wdc_c.flags), DEBUG_PROBE);
107
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 return 0;
139 for (i = 0; i < sizeof(prms->atap_model); i += 2) {
140 p = (u_short *)(prms->atap_model + i);
141 *p = swap16(*p);
142 }
143 for (i = 0; i < sizeof(prms->atap_serial); i += 2) {
144 p = (u_short *)(prms->atap_serial + i);
145 *p = swap16(*p);
146 }
147 for (i = 0; i < sizeof(prms->atap_revision); i += 2) {
148 p = (u_short *)(prms->atap_revision + i);
149 *p = swap16(*p);
150 }
151
152 return CMD_OK;
153 }
154 }
155
156 int
ata_set_mode(struct ata_drive_datas * drvp,u_int8_t mode,u_int8_t flags)157 ata_set_mode(struct ata_drive_datas *drvp, u_int8_t mode, u_int8_t flags)
158 {
159 struct wdc_command wdc_c;
160
161 WDCDEBUG_PRINT(("%s: mode=0x%x, flags=0x%x\n", __func__,
162 mode, flags), DEBUG_PROBE);
163
164 bzero(&wdc_c, sizeof(struct wdc_command));
165
166 wdc_c.r_command = SET_FEATURES;
167 wdc_c.r_st_bmask = 0;
168 wdc_c.r_st_pmask = 0;
169 wdc_c.r_precomp = WDSF_SET_MODE;
170 wdc_c.r_count = mode;
171 wdc_c.flags = flags;
172 wdc_c.timeout = 1000; /* 1s */
173 if (wdc_exec_command(drvp, &wdc_c) != WDC_COMPLETE)
174 return CMD_AGAIN;
175
176 WDCDEBUG_PRINT(("%s: after wdc_exec_command() wdc_c.flags=0x%x\n",
177 __func__, wdc_c.flags), DEBUG_PROBE);
178
179 if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
180 return CMD_ERR;
181 }
182 return CMD_OK;
183 }
184
185 void
ata_dmaerr(struct ata_drive_datas * drvp)186 ata_dmaerr(struct ata_drive_datas *drvp)
187 {
188 /*
189 * Downgrade decision: if we get NERRS_MAX in NXFER.
190 * We start with n_dmaerrs set to NERRS_MAX-1 so that the
191 * first error within the first NXFER ops will immediatly trigger
192 * a downgrade.
193 * If we got an error and n_xfers is bigger than NXFER reset counters.
194 */
195 drvp->n_dmaerrs++;
196 if (drvp->n_dmaerrs >= NERRS_MAX && drvp->n_xfers <= NXFER) {
197 wdc_downgrade_mode(drvp);
198 drvp->n_dmaerrs = NERRS_MAX-1;
199 drvp->n_xfers = 0;
200 return;
201 }
202 if (drvp->n_xfers > NXFER) {
203 drvp->n_dmaerrs = 1; /* just got an error */
204 drvp->n_xfers = 1; /* restart counting from this error */
205 }
206 }
207
208 void
ata_perror(struct ata_drive_datas * drvp,int errno,char * buf,size_t buf_len)209 ata_perror(struct ata_drive_datas *drvp, int errno, char *buf, size_t buf_len)
210 {
211 static char *errstr0_3[] = {"address mark not found",
212 "track 0 not found", "aborted command", "media change requested",
213 "id not found", "media changed", "uncorrectable data error",
214 "bad block detected"};
215 static char *errstr4_5[] = {"",
216 "no media/write protected", "aborted command",
217 "media change requested", "id not found", "media changed",
218 "uncorrectable data error", "interface CRC error"};
219 char **errstr;
220 int i;
221 char *sep = "";
222 size_t len = 0;
223
224 if (drvp->ata_vers >= 4)
225 errstr = errstr4_5;
226 else
227 errstr = errstr0_3;
228
229 if (errno == 0) {
230 snprintf(buf, buf_len, "error not notified");
231 }
232
233 for (i = 0; i < 8; i++) {
234 if (errno & (1 << i)) {
235 snprintf(buf + len, buf_len - len, "%s %s", sep,
236 errstr[i]);
237 len = strlen(buf);
238 sep = ",";
239 }
240 }
241 }
242