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 | ||
1fd4afe2 | 70 | /* Default NVM size to read */ |
1214755c | 71 | #define IWL_NVM_DEFAULT_CHUNK_SIZE (2*1024) |
8a87bddd | 72 | #define IWL_MAX_NVM_SECTION_SIZE 7000 |
1fd4afe2 | 73 | |
1214755c EH |
74 | #define NVM_WRITE_OPCODE 1 |
75 | #define NVM_READ_OPCODE 0 | |
76 | ||
77 | /* | |
78 | * prepare the NVM host command w/ the pointers to the nvm buffer | |
79 | * and send it to fw | |
80 | */ | |
81 | static int iwl_nvm_write_chunk(struct iwl_mvm *mvm, u16 section, | |
82 | u16 offset, u16 length, const u8 *data) | |
8ca151b5 | 83 | { |
1214755c EH |
84 | struct iwl_nvm_access_cmd nvm_access_cmd = { |
85 | .offset = cpu_to_le16(offset), | |
86 | .length = cpu_to_le16(length), | |
87 | .type = cpu_to_le16(section), | |
88 | .op_code = NVM_WRITE_OPCODE, | |
89 | }; | |
90 | struct iwl_host_cmd cmd = { | |
91 | .id = NVM_ACCESS_CMD, | |
92 | .len = { sizeof(struct iwl_nvm_access_cmd), length }, | |
4f59334b | 93 | .flags = CMD_SYNC | CMD_SEND_IN_RFKILL, |
1214755c EH |
94 | .data = { &nvm_access_cmd, data }, |
95 | /* data may come from vmalloc, so use _DUP */ | |
96 | .dataflags = { 0, IWL_HCMD_DFL_DUP }, | |
97 | }; | |
98 | ||
99 | return iwl_mvm_send_cmd(mvm, &cmd); | |
8ca151b5 JB |
100 | } |
101 | ||
102 | static int iwl_nvm_read_chunk(struct iwl_mvm *mvm, u16 section, | |
103 | u16 offset, u16 length, u8 *data) | |
104 | { | |
1214755c EH |
105 | struct iwl_nvm_access_cmd nvm_access_cmd = { |
106 | .offset = cpu_to_le16(offset), | |
107 | .length = cpu_to_le16(length), | |
108 | .type = cpu_to_le16(section), | |
109 | .op_code = NVM_READ_OPCODE, | |
110 | }; | |
b9545b48 | 111 | struct iwl_nvm_access_resp *nvm_resp; |
8ca151b5 JB |
112 | struct iwl_rx_packet *pkt; |
113 | struct iwl_host_cmd cmd = { | |
114 | .id = NVM_ACCESS_CMD, | |
4f59334b | 115 | .flags = CMD_SYNC | CMD_WANT_SKB | CMD_SEND_IN_RFKILL, |
8ca151b5 JB |
116 | .data = { &nvm_access_cmd, }, |
117 | }; | |
118 | int ret, bytes_read, offset_read; | |
119 | u8 *resp_data; | |
120 | ||
b9545b48 | 121 | cmd.len[0] = sizeof(struct iwl_nvm_access_cmd); |
8ca151b5 JB |
122 | |
123 | ret = iwl_mvm_send_cmd(mvm, &cmd); | |
124 | if (ret) | |
125 | return ret; | |
126 | ||
127 | pkt = cmd.resp_pkt; | |
128 | if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) { | |
129 | IWL_ERR(mvm, "Bad return from NVM_ACCES_COMMAND (0x%08X)\n", | |
130 | pkt->hdr.flags); | |
131 | ret = -EIO; | |
132 | goto exit; | |
133 | } | |
134 | ||
135 | /* Extract NVM response */ | |
136 | nvm_resp = (void *)pkt->data; | |
b9545b48 EG |
137 | ret = le16_to_cpu(nvm_resp->status); |
138 | bytes_read = le16_to_cpu(nvm_resp->length); | |
139 | offset_read = le16_to_cpu(nvm_resp->offset); | |
140 | resp_data = nvm_resp->data; | |
8ca151b5 JB |
141 | if (ret) { |
142 | IWL_ERR(mvm, | |
143 | "NVM access command failed with status %d (device: %s)\n", | |
144 | ret, mvm->cfg->name); | |
145 | ret = -EINVAL; | |
146 | goto exit; | |
147 | } | |
148 | ||
149 | if (offset_read != offset) { | |
150 | IWL_ERR(mvm, "NVM ACCESS response with invalid offset %d\n", | |
151 | offset_read); | |
152 | ret = -EINVAL; | |
153 | goto exit; | |
154 | } | |
155 | ||
156 | /* Write data to NVM */ | |
157 | memcpy(data + offset, resp_data, bytes_read); | |
158 | ret = bytes_read; | |
159 | ||
160 | exit: | |
161 | iwl_free_resp(&cmd); | |
162 | return ret; | |
163 | } | |
164 | ||
1214755c EH |
165 | static int iwl_nvm_write_section(struct iwl_mvm *mvm, u16 section, |
166 | const u8 *data, u16 length) | |
167 | { | |
168 | int offset = 0; | |
169 | ||
170 | /* copy data in chunks of 2k (and remainder if any) */ | |
171 | ||
172 | while (offset < length) { | |
173 | int chunk_size, ret; | |
174 | ||
175 | chunk_size = min(IWL_NVM_DEFAULT_CHUNK_SIZE, | |
176 | length - offset); | |
177 | ||
178 | ret = iwl_nvm_write_chunk(mvm, section, offset, | |
179 | chunk_size, data + offset); | |
180 | if (ret < 0) | |
181 | return ret; | |
182 | ||
183 | offset += chunk_size; | |
184 | } | |
185 | ||
186 | return 0; | |
187 | } | |
188 | ||
8ca151b5 JB |
189 | /* |
190 | * Reads an NVM section completely. | |
191 | * NICs prior to 7000 family doesn't have a real NVM, but just read | |
192 | * section 0 which is the EEPROM. Because the EEPROM reading is unlimited | |
193 | * by uCode, we need to manually check in this case that we don't | |
194 | * overflow and try to read more than the EEPROM size. | |
195 | * For 7000 family NICs, we supply the maximal size we can read, and | |
196 | * the uCode fills the response with as much data as we can, | |
197 | * without overflowing, so no check is needed. | |
198 | */ | |
199 | static int iwl_nvm_read_section(struct iwl_mvm *mvm, u16 section, | |
200 | u8 *data) | |
201 | { | |
202 | u16 length, offset = 0; | |
203 | int ret; | |
8ca151b5 | 204 | |
1fd4afe2 DS |
205 | /* Set nvm section read length */ |
206 | length = IWL_NVM_DEFAULT_CHUNK_SIZE; | |
207 | ||
8ca151b5 JB |
208 | ret = length; |
209 | ||
210 | /* Read the NVM until exhausted (reading less than requested) */ | |
211 | while (ret == length) { | |
212 | ret = iwl_nvm_read_chunk(mvm, section, offset, length, data); | |
213 | if (ret < 0) { | |
214 | IWL_ERR(mvm, | |
215 | "Cannot read NVM from section %d offset %d, length %d\n", | |
216 | section, offset, length); | |
217 | return ret; | |
218 | } | |
219 | offset += ret; | |
8ca151b5 JB |
220 | } |
221 | ||
07fd7d28 JB |
222 | IWL_DEBUG_EEPROM(mvm->trans->dev, |
223 | "NVM section %d read completed\n", section); | |
8ca151b5 JB |
224 | return offset; |
225 | } | |
226 | ||
227 | static struct iwl_nvm_data * | |
228 | iwl_parse_nvm_sections(struct iwl_mvm *mvm) | |
229 | { | |
230 | struct iwl_nvm_section *sections = mvm->nvm_sections; | |
231 | const __le16 *hw, *sw, *calib; | |
232 | ||
233 | /* Checking for required sections */ | |
234 | if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data || | |
ae2b21b0 | 235 | !mvm->nvm_sections[mvm->cfg->nvm_hw_section_num].data) { |
8ca151b5 JB |
236 | IWL_ERR(mvm, "Can't parse empty NVM sections\n"); |
237 | return NULL; | |
238 | } | |
239 | ||
240 | if (WARN_ON(!mvm->cfg)) | |
241 | return NULL; | |
242 | ||
ae2b21b0 | 243 | hw = (const __le16 *)sections[mvm->cfg->nvm_hw_section_num].data; |
8ca151b5 JB |
244 | sw = (const __le16 *)sections[NVM_SECTION_TYPE_SW].data; |
245 | calib = (const __le16 *)sections[NVM_SECTION_TYPE_CALIBRATION].data; | |
9ce4fa72 EG |
246 | return iwl_parse_nvm_data(mvm->trans->dev, mvm->cfg, hw, sw, calib, |
247 | iwl_fw_valid_tx_ant(mvm->fw), | |
248 | iwl_fw_valid_rx_ant(mvm->fw)); | |
8ca151b5 JB |
249 | } |
250 | ||
1214755c EH |
251 | #define MAX_NVM_FILE_LEN 16384 |
252 | ||
253 | /* | |
81a67e32 EL |
254 | * Reads external NVM from a file into mvm->nvm_sections |
255 | * | |
1214755c EH |
256 | * HOW TO CREATE THE NVM FILE FORMAT: |
257 | * ------------------------------ | |
258 | * 1. create hex file, format: | |
259 | * 3800 -> header | |
260 | * 0000 -> header | |
261 | * 5a40 -> data | |
262 | * | |
263 | * rev - 6 bit (word1) | |
264 | * len - 10 bit (word1) | |
265 | * id - 4 bit (word2) | |
266 | * rsv - 12 bit (word2) | |
267 | * | |
268 | * 2. flip 8bits with 8 bits per line to get the right NVM file format | |
269 | * | |
270 | * 3. create binary file from the hex file | |
271 | * | |
272 | * 4. save as "iNVM_xxx.bin" under /lib/firmware | |
273 | */ | |
81a67e32 | 274 | static int iwl_mvm_read_external_nvm(struct iwl_mvm *mvm) |
1214755c | 275 | { |
81a67e32 EL |
276 | int ret, section_size; |
277 | u16 section_id; | |
1214755c EH |
278 | const struct firmware *fw_entry; |
279 | const struct { | |
280 | __le16 word1; | |
281 | __le16 word2; | |
282 | u8 data[]; | |
283 | } *file_sec; | |
81a67e32 | 284 | const u8 *eof, *temp; |
1214755c EH |
285 | |
286 | #define NVM_WORD1_LEN(x) (8 * (x & 0x03FF)) | |
287 | #define NVM_WORD2_ID(x) (x >> 12) | |
288 | ||
81a67e32 EL |
289 | IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from external NVM\n"); |
290 | ||
1214755c EH |
291 | /* |
292 | * Obtain NVM image via request_firmware. Since we already used | |
293 | * request_firmware_nowait() for the firmware binary load and only | |
294 | * get here after that we assume the NVM request can be satisfied | |
295 | * synchronously. | |
296 | */ | |
297 | ret = request_firmware(&fw_entry, iwlwifi_mod_params.nvm_file, | |
298 | mvm->trans->dev); | |
299 | if (ret) { | |
300 | IWL_ERR(mvm, "ERROR: %s isn't available %d\n", | |
301 | iwlwifi_mod_params.nvm_file, ret); | |
302 | return ret; | |
303 | } | |
304 | ||
305 | IWL_INFO(mvm, "Loaded NVM file %s (%zu bytes)\n", | |
306 | iwlwifi_mod_params.nvm_file, fw_entry->size); | |
307 | ||
308 | if (fw_entry->size < sizeof(*file_sec)) { | |
309 | IWL_ERR(mvm, "NVM file too small\n"); | |
310 | ret = -EINVAL; | |
311 | goto out; | |
312 | } | |
313 | ||
314 | if (fw_entry->size > MAX_NVM_FILE_LEN) { | |
315 | IWL_ERR(mvm, "NVM file too large\n"); | |
316 | ret = -EINVAL; | |
317 | goto out; | |
318 | } | |
319 | ||
320 | eof = fw_entry->data + fw_entry->size; | |
321 | ||
322 | file_sec = (void *)fw_entry->data; | |
323 | ||
324 | while (true) { | |
325 | if (file_sec->data > eof) { | |
326 | IWL_ERR(mvm, | |
327 | "ERROR - NVM file too short for section header\n"); | |
328 | ret = -EINVAL; | |
329 | break; | |
330 | } | |
331 | ||
332 | /* check for EOF marker */ | |
333 | if (!file_sec->word1 && !file_sec->word2) { | |
334 | ret = 0; | |
335 | break; | |
336 | } | |
337 | ||
338 | section_size = 2 * NVM_WORD1_LEN(le16_to_cpu(file_sec->word1)); | |
339 | section_id = NVM_WORD2_ID(le16_to_cpu(file_sec->word2)); | |
340 | ||
341 | if (section_size > IWL_MAX_NVM_SECTION_SIZE) { | |
342 | IWL_ERR(mvm, "ERROR - section too large (%d)\n", | |
343 | section_size); | |
344 | ret = -EINVAL; | |
345 | break; | |
346 | } | |
347 | ||
348 | if (!section_size) { | |
349 | IWL_ERR(mvm, "ERROR - section empty\n"); | |
350 | ret = -EINVAL; | |
351 | break; | |
352 | } | |
353 | ||
354 | if (file_sec->data + section_size > eof) { | |
355 | IWL_ERR(mvm, | |
356 | "ERROR - NVM file too short for section (%d bytes)\n", | |
357 | section_size); | |
358 | ret = -EINVAL; | |
359 | break; | |
360 | } | |
361 | ||
ae2b21b0 | 362 | if (WARN(section_id >= NVM_MAX_NUM_SECTIONS, |
a4a12478 EL |
363 | "Invalid NVM section ID %d\n", section_id)) { |
364 | ret = -EINVAL; | |
365 | break; | |
366 | } | |
367 | ||
81a67e32 EL |
368 | temp = kmemdup(file_sec->data, section_size, GFP_KERNEL); |
369 | if (!temp) { | |
370 | ret = -ENOMEM; | |
371 | break; | |
372 | } | |
81a67e32 EL |
373 | mvm->nvm_sections[section_id].data = temp; |
374 | mvm->nvm_sections[section_id].length = section_size; | |
1214755c EH |
375 | |
376 | /* advance to the next section */ | |
377 | file_sec = (void *)(file_sec->data + section_size); | |
378 | } | |
379 | out: | |
380 | release_firmware(fw_entry); | |
381 | return ret; | |
382 | } | |
383 | ||
81a67e32 EL |
384 | /* Loads the NVM data stored in mvm->nvm_sections into the NIC */ |
385 | int iwl_mvm_load_nvm_to_nic(struct iwl_mvm *mvm) | |
386 | { | |
05159fcc | 387 | int i, ret = 0; |
81a67e32 EL |
388 | struct iwl_nvm_section *sections = mvm->nvm_sections; |
389 | ||
390 | IWL_DEBUG_EEPROM(mvm->trans->dev, "'Write to NVM\n"); | |
391 | ||
099d8f20 EG |
392 | for (i = 0; i < ARRAY_SIZE(mvm->nvm_sections); i++) { |
393 | if (!mvm->nvm_sections[i].data || !mvm->nvm_sections[i].length) | |
394 | continue; | |
395 | ret = iwl_nvm_write_section(mvm, i, sections[i].data, | |
396 | sections[i].length); | |
81a67e32 EL |
397 | if (ret < 0) { |
398 | IWL_ERR(mvm, "iwl_mvm_send_cmd failed: %d\n", ret); | |
399 | break; | |
400 | } | |
401 | } | |
402 | return ret; | |
403 | } | |
404 | ||
8ca151b5 JB |
405 | int iwl_nvm_init(struct iwl_mvm *mvm) |
406 | { | |
407 | int ret, i, section; | |
408 | u8 *nvm_buffer, *temp; | |
409 | ||
ae2b21b0 EH |
410 | if (WARN_ON_ONCE(mvm->cfg->nvm_hw_section_num >= NVM_MAX_NUM_SECTIONS)) |
411 | return -EINVAL; | |
412 | ||
1214755c EH |
413 | /* load external NVM if configured */ |
414 | if (iwlwifi_mod_params.nvm_file) { | |
415 | /* move to External NVM flow */ | |
81a67e32 | 416 | ret = iwl_mvm_read_external_nvm(mvm); |
1214755c EH |
417 | if (ret) |
418 | return ret; | |
81a67e32 | 419 | } else { |
ae2b21b0 EH |
420 | /* list of NVM sections we are allowed/need to read */ |
421 | int nvm_to_read[] = { | |
422 | mvm->cfg->nvm_hw_section_num, | |
423 | NVM_SECTION_TYPE_SW, | |
424 | NVM_SECTION_TYPE_CALIBRATION, | |
425 | NVM_SECTION_TYPE_PRODUCTION, | |
426 | }; | |
427 | ||
81a67e32 EL |
428 | /* Read From FW NVM */ |
429 | IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from NVM\n"); | |
430 | ||
431 | /* TODO: find correct NVM max size for a section */ | |
432 | nvm_buffer = kmalloc(mvm->cfg->base_params->eeprom_size, | |
433 | GFP_KERNEL); | |
434 | if (!nvm_buffer) | |
435 | return -ENOMEM; | |
436 | for (i = 0; i < ARRAY_SIZE(nvm_to_read); i++) { | |
437 | section = nvm_to_read[i]; | |
438 | /* we override the constness for initial read */ | |
439 | ret = iwl_nvm_read_section(mvm, section, nvm_buffer); | |
440 | if (ret < 0) | |
441 | break; | |
442 | temp = kmemdup(nvm_buffer, ret, GFP_KERNEL); | |
443 | if (!temp) { | |
444 | ret = -ENOMEM; | |
445 | break; | |
446 | } | |
447 | mvm->nvm_sections[section].data = temp; | |
448 | mvm->nvm_sections[section].length = ret; | |
086f7368 EG |
449 | |
450 | #ifdef CONFIG_IWLWIFI_DEBUGFS | |
451 | switch (section) { | |
086f7368 EG |
452 | case NVM_SECTION_TYPE_SW: |
453 | mvm->nvm_sw_blob.data = temp; | |
454 | mvm->nvm_sw_blob.size = ret; | |
455 | break; | |
456 | case NVM_SECTION_TYPE_CALIBRATION: | |
457 | mvm->nvm_calib_blob.data = temp; | |
458 | mvm->nvm_calib_blob.size = ret; | |
459 | break; | |
460 | case NVM_SECTION_TYPE_PRODUCTION: | |
461 | mvm->nvm_prod_blob.data = temp; | |
462 | mvm->nvm_prod_blob.size = ret; | |
463 | break; | |
464 | default: | |
ae2b21b0 EH |
465 | if (section == mvm->cfg->nvm_hw_section_num) { |
466 | mvm->nvm_hw_blob.data = temp; | |
467 | mvm->nvm_hw_blob.size = ret; | |
468 | break; | |
469 | } | |
086f7368 EG |
470 | WARN(1, "section: %d", section); |
471 | } | |
472 | #endif | |
8ca151b5 | 473 | } |
81a67e32 EL |
474 | kfree(nvm_buffer); |
475 | if (ret < 0) | |
476 | return ret; | |
8ca151b5 JB |
477 | } |
478 | ||
b9545b48 | 479 | mvm->nvm_data = iwl_parse_nvm_sections(mvm); |
82598b4f JB |
480 | if (!mvm->nvm_data) |
481 | return -ENODATA; | |
8ca151b5 | 482 | |
82598b4f | 483 | return 0; |
8ca151b5 | 484 | } |