1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35#include <linux/delay.h>
36#include <linux/pci.h>
37#include <linux/vmalloc.h>
38
39#include "qib.h"
40
41
42
43
44
45
46
47
48
49
50
51
52
53int qib_eeprom_read(struct qib_devdata *dd, u8 eeprom_offset,
54 void *buff, int len)
55{
56 int ret;
57
58 ret = mutex_lock_interruptible(&dd->eep_lock);
59 if (!ret) {
60 ret = qib_twsi_reset(dd);
61 if (ret)
62 qib_dev_err(dd, "EEPROM Reset for read failed\n");
63 else
64 ret = qib_twsi_blk_rd(dd, dd->twsi_eeprom_dev,
65 eeprom_offset, buff, len);
66 mutex_unlock(&dd->eep_lock);
67 }
68
69 return ret;
70}
71
72
73
74
75
76
77static int eeprom_write_with_enable(struct qib_devdata *dd, u8 offset,
78 const void *buf, int len)
79{
80 int ret, pwen;
81
82 pwen = dd->f_eeprom_wen(dd, 1);
83 ret = qib_twsi_reset(dd);
84 if (ret)
85 qib_dev_err(dd, "EEPROM Reset for write failed\n");
86 else
87 ret = qib_twsi_blk_wr(dd, dd->twsi_eeprom_dev,
88 offset, buf, len);
89 dd->f_eeprom_wen(dd, pwen);
90 return ret;
91}
92
93
94
95
96
97
98
99
100int qib_eeprom_write(struct qib_devdata *dd, u8 eeprom_offset,
101 const void *buff, int len)
102{
103 int ret;
104
105 ret = mutex_lock_interruptible(&dd->eep_lock);
106 if (!ret) {
107 ret = eeprom_write_with_enable(dd, eeprom_offset, buff, len);
108 mutex_unlock(&dd->eep_lock);
109 }
110
111 return ret;
112}
113
114static u8 flash_csum(struct qib_flash *ifp, int adjust)
115{
116 u8 *ip = (u8 *) ifp;
117 u8 csum = 0, len;
118
119
120
121
122
123
124 len = ifp->if_length;
125 if (len > sizeof(struct qib_flash))
126 len = sizeof(struct qib_flash);
127 while (len--)
128 csum += *ip++;
129 csum -= ifp->if_csum;
130 csum = ~csum;
131 if (adjust)
132 ifp->if_csum = csum;
133
134 return csum;
135}
136
137
138
139
140
141
142
143
144void qib_get_eeprom_info(struct qib_devdata *dd)
145{
146 void *buf;
147 struct qib_flash *ifp;
148 __be64 guid;
149 int len, eep_stat;
150 u8 csum, *bguid;
151 int t = dd->unit;
152 struct qib_devdata *dd0 = qib_lookup(0);
153
154 if (t && dd0->nguid > 1 && t <= dd0->nguid) {
155 u8 oguid;
156
157 dd->base_guid = dd0->base_guid;
158 bguid = (u8 *) &dd->base_guid;
159
160 oguid = bguid[7];
161 bguid[7] += t;
162 if (oguid > bguid[7]) {
163 if (bguid[6] == 0xff) {
164 if (bguid[5] == 0xff) {
165 qib_dev_err(dd,
166 "Can't set GUID from base, wraps to OUI!\n");
167 dd->base_guid = 0;
168 goto bail;
169 }
170 bguid[5]++;
171 }
172 bguid[6]++;
173 }
174 dd->nguid = 1;
175 goto bail;
176 }
177
178
179
180
181
182 len = sizeof(struct qib_flash);
183 buf = vmalloc(len);
184 if (!buf)
185 goto bail;
186
187
188
189
190
191 eep_stat = qib_eeprom_read(dd, 0, buf, len);
192
193 if (eep_stat) {
194 qib_dev_err(dd, "Failed reading GUID from eeprom\n");
195 goto done;
196 }
197 ifp = (struct qib_flash *)buf;
198
199 csum = flash_csum(ifp, 0);
200 if (csum != ifp->if_csum) {
201 qib_devinfo(dd->pcidev,
202 "Bad I2C flash checksum: 0x%x, not 0x%x\n",
203 csum, ifp->if_csum);
204 goto done;
205 }
206 if (*(__be64 *) ifp->if_guid == cpu_to_be64(0) ||
207 *(__be64 *) ifp->if_guid == ~cpu_to_be64(0)) {
208 qib_dev_err(dd,
209 "Invalid GUID %llx from flash; ignoring\n",
210 *(unsigned long long *) ifp->if_guid);
211
212 goto done;
213 }
214
215
216 if (*(u64 *) ifp->if_guid == 0x100007511000000ULL)
217 qib_devinfo(dd->pcidev,
218 "Warning, GUID %llx is default, probably not correct!\n",
219 *(unsigned long long *) ifp->if_guid);
220
221 bguid = ifp->if_guid;
222 if (!bguid[0] && !bguid[1] && !bguid[2]) {
223
224
225
226
227
228 bguid[1] = bguid[3];
229 bguid[2] = bguid[4];
230 bguid[3] = 0;
231 bguid[4] = 0;
232 guid = *(__be64 *) ifp->if_guid;
233 } else
234 guid = *(__be64 *) ifp->if_guid;
235 dd->base_guid = guid;
236 dd->nguid = ifp->if_numguid;
237
238
239
240
241
242 if ((ifp->if_fversion > 1) && ifp->if_sprefix[0] &&
243 ((u8 *) ifp->if_sprefix)[0] != 0xFF) {
244 char *snp = dd->serial;
245
246
247
248
249
250 memcpy(snp, ifp->if_sprefix, sizeof(ifp->if_sprefix));
251 snp[sizeof(ifp->if_sprefix)] = '\0';
252 len = strlen(snp);
253 snp += len;
254 len = sizeof(dd->serial) - len;
255 if (len > sizeof(ifp->if_serial))
256 len = sizeof(ifp->if_serial);
257 memcpy(snp, ifp->if_serial, len);
258 } else {
259 memcpy(dd->serial, ifp->if_serial, sizeof(ifp->if_serial));
260 }
261 if (!strstr(ifp->if_comment, "Tested successfully"))
262 qib_dev_err(dd,
263 "Board SN %s did not pass functional test: %s\n",
264 dd->serial, ifp->if_comment);
265
266done:
267 vfree(buf);
268
269bail:;
270}
271
272