Commit | Line | Data |
---|---|---|
8ca151b5 JB |
1 | /****************************************************************************** |
2 | * | |
3 | * This file is provided under a dual BSD/GPLv2 license. When using or | |
4 | * redistributing this file, you may do so under either license. | |
5 | * | |
6 | * GPL LICENSE SUMMARY | |
7 | * | |
51368bf7 | 8 | * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved. |
8ca151b5 JB |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of version 2 of the GNU General Public License as | |
12 | * published by the Free Software Foundation. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, but | |
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | * General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, | |
22 | * USA | |
23 | * | |
24 | * The full GNU General Public License is included in this distribution | |
410dc5aa | 25 | * in the file called COPYING. |
8ca151b5 JB |
26 | * |
27 | * Contact Information: | |
28 | * Intel Linux Wireless <ilw@linux.intel.com> | |
29 | * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 | |
30 | * | |
31 | * BSD LICENSE | |
32 | * | |
51368bf7 | 33 | * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved. |
8ca151b5 JB |
34 | * All rights reserved. |
35 | * | |
36 | * Redistribution and use in source and binary forms, with or without | |
37 | * modification, are permitted provided that the following conditions | |
38 | * are met: | |
39 | * | |
40 | * * Redistributions of source code must retain the above copyright | |
41 | * notice, this list of conditions and the following disclaimer. | |
42 | * * Redistributions in binary form must reproduce the above copyright | |
43 | * notice, this list of conditions and the following disclaimer in | |
44 | * the documentation and/or other materials provided with the | |
45 | * distribution. | |
46 | * * Neither the name Intel Corporation nor the names of its | |
47 | * contributors may be used to endorse or promote products derived | |
48 | * from this software without specific prior written permission. | |
49 | * | |
50 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
51 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
52 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
53 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
54 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
55 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
56 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
57 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
58 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
59 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
60 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
61 | * | |
62 | *****************************************************************************/ | |
1214755c | 63 | #include <linux/firmware.h> |
8ca151b5 JB |
64 | #include "iwl-trans.h" |
65 | #include "mvm.h" | |
66 | #include "iwl-eeprom-parse.h" | |
67 | #include "iwl-eeprom-read.h" | |
68 | #include "iwl-nvm-parse.h" | |
69 | ||
70 | /* list of NVM sections we are allowed/need to read */ | |
71 | static const int nvm_to_read[] = { | |
72 | NVM_SECTION_TYPE_HW, | |
73 | NVM_SECTION_TYPE_SW, | |
74 | NVM_SECTION_TYPE_CALIBRATION, | |
75 | NVM_SECTION_TYPE_PRODUCTION, | |
76 | }; | |
77 | ||
1fd4afe2 | 78 | /* Default NVM size to read */ |
1214755c | 79 | #define IWL_NVM_DEFAULT_CHUNK_SIZE (2*1024) |
8a87bddd | 80 | #define IWL_MAX_NVM_SECTION_SIZE 7000 |
1fd4afe2 | 81 | |
1214755c EH |
82 | #define NVM_WRITE_OPCODE 1 |
83 | #define NVM_READ_OPCODE 0 | |
84 | ||
85 | /* | |
86 | * prepare the NVM host command w/ the pointers to the nvm buffer | |
87 | * and send it to fw | |
88 | */ | |
89 | static int iwl_nvm_write_chunk(struct iwl_mvm *mvm, u16 section, | |
90 | u16 offset, u16 length, const u8 *data) | |
8ca151b5 | 91 | { |
1214755c EH |
92 | struct iwl_nvm_access_cmd nvm_access_cmd = { |
93 | .offset = cpu_to_le16(offset), | |
94 | .length = cpu_to_le16(length), | |
95 | .type = cpu_to_le16(section), | |
96 | .op_code = NVM_WRITE_OPCODE, | |
97 | }; | |
98 | struct iwl_host_cmd cmd = { | |
99 | .id = NVM_ACCESS_CMD, | |
100 | .len = { sizeof(struct iwl_nvm_access_cmd), length }, | |
4f59334b | 101 | .flags = CMD_SYNC | CMD_SEND_IN_RFKILL, |
1214755c EH |
102 | .data = { &nvm_access_cmd, data }, |
103 | /* data may come from vmalloc, so use _DUP */ | |
104 | .dataflags = { 0, IWL_HCMD_DFL_DUP }, | |
105 | }; | |
106 | ||
107 | return iwl_mvm_send_cmd(mvm, &cmd); | |
8ca151b5 JB |
108 | } |
109 | ||
110 | static int iwl_nvm_read_chunk(struct iwl_mvm *mvm, u16 section, | |
111 | u16 offset, u16 length, u8 *data) | |
112 | { | |
1214755c EH |
113 | struct iwl_nvm_access_cmd nvm_access_cmd = { |
114 | .offset = cpu_to_le16(offset), | |
115 | .length = cpu_to_le16(length), | |
116 | .type = cpu_to_le16(section), | |
117 | .op_code = NVM_READ_OPCODE, | |
118 | }; | |
b9545b48 | 119 | struct iwl_nvm_access_resp *nvm_resp; |
8ca151b5 JB |
120 | struct iwl_rx_packet *pkt; |
121 | struct iwl_host_cmd cmd = { | |
122 | .id = NVM_ACCESS_CMD, | |
4f59334b | 123 | .flags = CMD_SYNC | CMD_WANT_SKB | CMD_SEND_IN_RFKILL, |
8ca151b5 JB |
124 | .data = { &nvm_access_cmd, }, |
125 | }; | |
126 | int ret, bytes_read, offset_read; | |
127 | u8 *resp_data; | |
128 | ||
b9545b48 | 129 | cmd.len[0] = sizeof(struct iwl_nvm_access_cmd); |
8ca151b5 JB |
130 | |
131 | ret = iwl_mvm_send_cmd(mvm, &cmd); | |
132 | if (ret) | |
133 | return ret; | |
134 | ||
135 | pkt = cmd.resp_pkt; | |
136 | if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) { | |
137 | IWL_ERR(mvm, "Bad return from NVM_ACCES_COMMAND (0x%08X)\n", | |
138 | pkt->hdr.flags); | |
139 | ret = -EIO; | |
140 | goto exit; | |
141 | } | |
142 | ||
143 | /* Extract NVM response */ | |
144 | nvm_resp = (void *)pkt->data; | |
b9545b48 EG |
145 | ret = le16_to_cpu(nvm_resp->status); |
146 | bytes_read = le16_to_cpu(nvm_resp->length); | |
147 | offset_read = le16_to_cpu(nvm_resp->offset); | |
148 | resp_data = nvm_resp->data; | |
8ca151b5 JB |
149 | if (ret) { |
150 | IWL_ERR(mvm, | |
151 | "NVM access command failed with status %d (device: %s)\n", | |
152 | ret, mvm->cfg->name); | |
153 | ret = -EINVAL; | |
154 | goto exit; | |
155 | } | |
156 | ||
157 | if (offset_read != offset) { | |
158 | IWL_ERR(mvm, "NVM ACCESS response with invalid offset %d\n", | |
159 | offset_read); | |
160 | ret = -EINVAL; | |
161 | goto exit; | |
162 | } | |
163 | ||
164 | /* Write data to NVM */ | |
165 | memcpy(data + offset, resp_data, bytes_read); | |
166 | ret = bytes_read; | |
167 | ||
168 | exit: | |
169 | iwl_free_resp(&cmd); | |
170 | return ret; | |
171 | } | |
172 | ||
1214755c EH |
173 | static int iwl_nvm_write_section(struct iwl_mvm *mvm, u16 section, |
174 | const u8 *data, u16 length) | |
175 | { | |
176 | int offset = 0; | |
177 | ||
178 | /* copy data in chunks of 2k (and remainder if any) */ | |
179 | ||
180 | while (offset < length) { | |
181 | int chunk_size, ret; | |
182 | ||
183 | chunk_size = min(IWL_NVM_DEFAULT_CHUNK_SIZE, | |
184 | length - offset); | |
185 | ||
186 | ret = iwl_nvm_write_chunk(mvm, section, offset, | |
187 | chunk_size, data + offset); | |
188 | if (ret < 0) | |
189 | return ret; | |
190 | ||
191 | offset += chunk_size; | |
192 | } | |
193 | ||
194 | return 0; | |
195 | } | |
196 | ||
8ca151b5 JB |
197 | /* |
198 | * Reads an NVM section completely. | |
199 | * NICs prior to 7000 family doesn't have a real NVM, but just read | |
200 | * section 0 which is the EEPROM. Because the EEPROM reading is unlimited | |
201 | * by uCode, we need to manually check in this case that we don't | |
202 | * overflow and try to read more than the EEPROM size. | |
203 | * For 7000 family NICs, we supply the maximal size we can read, and | |
204 | * the uCode fills the response with as much data as we can, | |
205 | * without overflowing, so no check is needed. | |
206 | */ | |
207 | static int iwl_nvm_read_section(struct iwl_mvm *mvm, u16 section, | |
208 | u8 *data) | |
209 | { | |
210 | u16 length, offset = 0; | |
211 | int ret; | |
8ca151b5 | 212 | |
1fd4afe2 DS |
213 | /* Set nvm section read length */ |
214 | length = IWL_NVM_DEFAULT_CHUNK_SIZE; | |
215 | ||
8ca151b5 JB |
216 | ret = length; |
217 | ||
218 | /* Read the NVM until exhausted (reading less than requested) */ | |
219 | while (ret == length) { | |
220 | ret = iwl_nvm_read_chunk(mvm, section, offset, length, data); | |
221 | if (ret < 0) { | |
222 | IWL_ERR(mvm, | |
223 | "Cannot read NVM from section %d offset %d, length %d\n", | |
224 | section, offset, length); | |
225 | return ret; | |
226 | } | |
227 | offset += ret; | |
8ca151b5 JB |
228 | } |
229 | ||
07fd7d28 JB |
230 | IWL_DEBUG_EEPROM(mvm->trans->dev, |
231 | "NVM section %d read completed\n", section); | |
8ca151b5 JB |
232 | return offset; |
233 | } | |
234 | ||
235 | static struct iwl_nvm_data * | |
236 | iwl_parse_nvm_sections(struct iwl_mvm *mvm) | |
237 | { | |
238 | struct iwl_nvm_section *sections = mvm->nvm_sections; | |
239 | const __le16 *hw, *sw, *calib; | |
240 | ||
241 | /* Checking for required sections */ | |
242 | if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data || | |
243 | !mvm->nvm_sections[NVM_SECTION_TYPE_HW].data) { | |
244 | IWL_ERR(mvm, "Can't parse empty NVM sections\n"); | |
245 | return NULL; | |
246 | } | |
247 | ||
248 | if (WARN_ON(!mvm->cfg)) | |
249 | return NULL; | |
250 | ||
251 | hw = (const __le16 *)sections[NVM_SECTION_TYPE_HW].data; | |
252 | sw = (const __le16 *)sections[NVM_SECTION_TYPE_SW].data; | |
253 | calib = (const __le16 *)sections[NVM_SECTION_TYPE_CALIBRATION].data; | |
9ce4fa72 EG |
254 | return iwl_parse_nvm_data(mvm->trans->dev, mvm->cfg, hw, sw, calib, |
255 | iwl_fw_valid_tx_ant(mvm->fw), | |
256 | iwl_fw_valid_rx_ant(mvm->fw)); | |
8ca151b5 JB |
257 | } |
258 | ||
1214755c EH |
259 | #define MAX_NVM_FILE_LEN 16384 |
260 | ||
261 | /* | |
81a67e32 EL |
262 | * Reads external NVM from a file into mvm->nvm_sections |
263 | * | |
1214755c EH |
264 | * HOW TO CREATE THE NVM FILE FORMAT: |
265 | * ------------------------------ | |
266 | * 1. create hex file, format: | |
267 | * 3800 -> header | |
268 | * 0000 -> header | |
269 | * 5a40 -> data | |
270 | * | |
271 | * rev - 6 bit (word1) | |
272 | * len - 10 bit (word1) | |
273 | * id - 4 bit (word2) | |
274 | * rsv - 12 bit (word2) | |
275 | * | |
276 | * 2. flip 8bits with 8 bits per line to get the right NVM file format | |
277 | * | |
278 | * 3. create binary file from the hex file | |
279 | * | |
280 | * 4. save as "iNVM_xxx.bin" under /lib/firmware | |
281 | */ | |
81a67e32 | 282 | static int iwl_mvm_read_external_nvm(struct iwl_mvm *mvm) |
1214755c | 283 | { |
81a67e32 EL |
284 | int ret, section_size; |
285 | u16 section_id; | |
1214755c EH |
286 | const struct firmware *fw_entry; |
287 | const struct { | |
288 | __le16 word1; | |
289 | __le16 word2; | |
290 | u8 data[]; | |
291 | } *file_sec; | |
81a67e32 | 292 | const u8 *eof, *temp; |
1214755c EH |
293 | |
294 | #define NVM_WORD1_LEN(x) (8 * (x & 0x03FF)) | |
295 | #define NVM_WORD2_ID(x) (x >> 12) | |
296 | ||
81a67e32 EL |
297 | IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from external NVM\n"); |
298 | ||
1214755c EH |
299 | /* |
300 | * Obtain NVM image via request_firmware. Since we already used | |
301 | * request_firmware_nowait() for the firmware binary load and only | |
302 | * get here after that we assume the NVM request can be satisfied | |
303 | * synchronously. | |
304 | */ | |
305 | ret = request_firmware(&fw_entry, iwlwifi_mod_params.nvm_file, | |
306 | mvm->trans->dev); | |
307 | if (ret) { | |
308 | IWL_ERR(mvm, "ERROR: %s isn't available %d\n", | |
309 | iwlwifi_mod_params.nvm_file, ret); | |
310 | return ret; | |
311 | } | |
312 | ||
313 | IWL_INFO(mvm, "Loaded NVM file %s (%zu bytes)\n", | |
314 | iwlwifi_mod_params.nvm_file, fw_entry->size); | |
315 | ||
316 | if (fw_entry->size < sizeof(*file_sec)) { | |
317 | IWL_ERR(mvm, "NVM file too small\n"); | |
318 | ret = -EINVAL; | |
319 | goto out; | |
320 | } | |
321 | ||
322 | if (fw_entry->size > MAX_NVM_FILE_LEN) { | |
323 | IWL_ERR(mvm, "NVM file too large\n"); | |
324 | ret = -EINVAL; | |
325 | goto out; | |
326 | } | |
327 | ||
328 | eof = fw_entry->data + fw_entry->size; | |
329 | ||
330 | file_sec = (void *)fw_entry->data; | |
331 | ||
332 | while (true) { | |
333 | if (file_sec->data > eof) { | |
334 | IWL_ERR(mvm, | |
335 | "ERROR - NVM file too short for section header\n"); | |
336 | ret = -EINVAL; | |
337 | break; | |
338 | } | |
339 | ||
340 | /* check for EOF marker */ | |
341 | if (!file_sec->word1 && !file_sec->word2) { | |
342 | ret = 0; | |
343 | break; | |
344 | } | |
345 | ||
346 | section_size = 2 * NVM_WORD1_LEN(le16_to_cpu(file_sec->word1)); | |
347 | section_id = NVM_WORD2_ID(le16_to_cpu(file_sec->word2)); | |
348 | ||
349 | if (section_size > IWL_MAX_NVM_SECTION_SIZE) { | |
350 | IWL_ERR(mvm, "ERROR - section too large (%d)\n", | |
351 | section_size); | |
352 | ret = -EINVAL; | |
353 | break; | |
354 | } | |
355 | ||
356 | if (!section_size) { | |
357 | IWL_ERR(mvm, "ERROR - section empty\n"); | |
358 | ret = -EINVAL; | |
359 | break; | |
360 | } | |
361 | ||
362 | if (file_sec->data + section_size > eof) { | |
363 | IWL_ERR(mvm, | |
364 | "ERROR - NVM file too short for section (%d bytes)\n", | |
365 | section_size); | |
366 | ret = -EINVAL; | |
367 | break; | |
368 | } | |
369 | ||
a4a12478 EL |
370 | if (WARN(section_id >= NVM_NUM_OF_SECTIONS, |
371 | "Invalid NVM section ID %d\n", section_id)) { | |
372 | ret = -EINVAL; | |
373 | break; | |
374 | } | |
375 | ||
81a67e32 EL |
376 | temp = kmemdup(file_sec->data, section_size, GFP_KERNEL); |
377 | if (!temp) { | |
378 | ret = -ENOMEM; | |
379 | break; | |
380 | } | |
81a67e32 EL |
381 | mvm->nvm_sections[section_id].data = temp; |
382 | mvm->nvm_sections[section_id].length = section_size; | |
1214755c EH |
383 | |
384 | /* advance to the next section */ | |
385 | file_sec = (void *)(file_sec->data + section_size); | |
386 | } | |
387 | out: | |
388 | release_firmware(fw_entry); | |
389 | return ret; | |
390 | } | |
391 | ||
81a67e32 EL |
392 | /* Loads the NVM data stored in mvm->nvm_sections into the NIC */ |
393 | int iwl_mvm_load_nvm_to_nic(struct iwl_mvm *mvm) | |
394 | { | |
395 | int i, ret; | |
396 | u16 section_id; | |
397 | struct iwl_nvm_section *sections = mvm->nvm_sections; | |
398 | ||
399 | IWL_DEBUG_EEPROM(mvm->trans->dev, "'Write to NVM\n"); | |
400 | ||
401 | for (i = 0; i < ARRAY_SIZE(nvm_to_read); i++) { | |
402 | section_id = nvm_to_read[i]; | |
403 | ret = iwl_nvm_write_section(mvm, section_id, | |
404 | sections[section_id].data, | |
405 | sections[section_id].length); | |
406 | if (ret < 0) { | |
407 | IWL_ERR(mvm, "iwl_mvm_send_cmd failed: %d\n", ret); | |
408 | break; | |
409 | } | |
410 | } | |
411 | return ret; | |
412 | } | |
413 | ||
8ca151b5 JB |
414 | int iwl_nvm_init(struct iwl_mvm *mvm) |
415 | { | |
416 | int ret, i, section; | |
417 | u8 *nvm_buffer, *temp; | |
418 | ||
1214755c EH |
419 | /* load external NVM if configured */ |
420 | if (iwlwifi_mod_params.nvm_file) { | |
421 | /* move to External NVM flow */ | |
81a67e32 | 422 | ret = iwl_mvm_read_external_nvm(mvm); |
1214755c EH |
423 | if (ret) |
424 | return ret; | |
81a67e32 EL |
425 | } else { |
426 | /* Read From FW NVM */ | |
427 | IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from NVM\n"); | |
428 | ||
429 | /* TODO: find correct NVM max size for a section */ | |
430 | nvm_buffer = kmalloc(mvm->cfg->base_params->eeprom_size, | |
431 | GFP_KERNEL); | |
432 | if (!nvm_buffer) | |
433 | return -ENOMEM; | |
434 | for (i = 0; i < ARRAY_SIZE(nvm_to_read); i++) { | |
435 | section = nvm_to_read[i]; | |
436 | /* we override the constness for initial read */ | |
437 | ret = iwl_nvm_read_section(mvm, section, nvm_buffer); | |
438 | if (ret < 0) | |
439 | break; | |
440 | temp = kmemdup(nvm_buffer, ret, GFP_KERNEL); | |
441 | if (!temp) { | |
442 | ret = -ENOMEM; | |
443 | break; | |
444 | } | |
445 | mvm->nvm_sections[section].data = temp; | |
446 | mvm->nvm_sections[section].length = ret; | |
086f7368 EG |
447 | |
448 | #ifdef CONFIG_IWLWIFI_DEBUGFS | |
449 | switch (section) { | |
450 | case NVM_SECTION_TYPE_HW: | |
451 | mvm->nvm_hw_blob.data = temp; | |
452 | mvm->nvm_hw_blob.size = ret; | |
453 | break; | |
454 | case NVM_SECTION_TYPE_SW: | |
455 | mvm->nvm_sw_blob.data = temp; | |
456 | mvm->nvm_sw_blob.size = ret; | |
457 | break; | |
458 | case NVM_SECTION_TYPE_CALIBRATION: | |
459 | mvm->nvm_calib_blob.data = temp; | |
460 | mvm->nvm_calib_blob.size = ret; | |
461 | break; | |
462 | case NVM_SECTION_TYPE_PRODUCTION: | |
463 | mvm->nvm_prod_blob.data = temp; | |
464 | mvm->nvm_prod_blob.size = ret; | |
465 | break; | |
466 | default: | |
467 | WARN(1, "section: %d", section); | |
468 | } | |
469 | #endif | |
8ca151b5 | 470 | } |
81a67e32 EL |
471 | kfree(nvm_buffer); |
472 | if (ret < 0) | |
473 | return ret; | |
8ca151b5 JB |
474 | } |
475 | ||
b9545b48 | 476 | mvm->nvm_data = iwl_parse_nvm_sections(mvm); |
82598b4f JB |
477 | if (!mvm->nvm_data) |
478 | return -ENODATA; | |
8ca151b5 | 479 | |
82598b4f | 480 | return 0; |
8ca151b5 | 481 | } |