blob: af074563e77076fafb88ae6c3d1c21a6f4da53e3 [file] [log] [blame]
Johannes Berg8ca151b2013-01-24 14:25:36 +01001/******************************************************************************
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 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +02008 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Johannes Berg8b4139d2014-07-24 14:05:26 +02009 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
Johannes Berg8ca151b2013-01-24 14:25:36 +010010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
23 * USA
24 *
25 * The full GNU General Public License is included in this distribution
Emmanuel Grumbach410dc5a2013-02-18 09:22:28 +020026 * in the file called COPYING.
Johannes Berg8ca151b2013-01-24 14:25:36 +010027 *
28 * Contact Information:
29 * Intel Linux Wireless <ilw@linux.intel.com>
30 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
31 *
32 * BSD LICENSE
33 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +020034 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Johannes Berg8b4139d2014-07-24 14:05:26 +020035 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
Johannes Berg8ca151b2013-01-24 14:25:36 +010036 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 *
42 * * Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * * Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in
46 * the documentation and/or other materials provided with the
47 * distribution.
48 * * Neither the name Intel Corporation nor the names of its
49 * contributors may be used to endorse or promote products derived
50 * from this software without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 *
64 *****************************************************************************/
Eran Harary12147552013-05-09 08:07:59 +030065#include <linux/firmware.h>
Johannes Berg8ca151b2013-01-24 14:25:36 +010066#include "iwl-trans.h"
Liad Kaufmanc2a2b282014-09-07 11:41:05 +030067#include "iwl-csr.h"
Johannes Berg8ca151b2013-01-24 14:25:36 +010068#include "mvm.h"
69#include "iwl-eeprom-parse.h"
70#include "iwl-eeprom-read.h"
71#include "iwl-nvm-parse.h"
72
Dor Shaish1fd4afe2013-02-27 10:18:07 +020073/* Default NVM size to read */
Eran Harary12147552013-05-09 08:07:59 +030074#define IWL_NVM_DEFAULT_CHUNK_SIZE (2*1024)
Liad Kaufmanf251c072014-04-28 14:42:04 +030075#define IWL_MAX_NVM_SECTION_SIZE 0x1b58
76#define IWL_MAX_NVM_8000A_SECTION_SIZE 0xffc
77#define IWL_MAX_NVM_8000B_SECTION_SIZE 0x1ffc
Dor Shaish1fd4afe2013-02-27 10:18:07 +020078
Eran Harary12147552013-05-09 08:07:59 +030079#define NVM_WRITE_OPCODE 1
80#define NVM_READ_OPCODE 0
81
Eran Hararyd6aeb352014-05-11 09:44:17 +030082/* load nvm chunk response */
83enum {
84 READ_NVM_CHUNK_SUCCEED = 0,
85 READ_NVM_CHUNK_NOT_VALID_ADDRESS = 1
86};
87
Eran Harary12147552013-05-09 08:07:59 +030088/*
89 * prepare the NVM host command w/ the pointers to the nvm buffer
90 * and send it to fw
91 */
92static int iwl_nvm_write_chunk(struct iwl_mvm *mvm, u16 section,
93 u16 offset, u16 length, const u8 *data)
Johannes Berg8ca151b2013-01-24 14:25:36 +010094{
Eran Harary12147552013-05-09 08:07:59 +030095 struct iwl_nvm_access_cmd nvm_access_cmd = {
96 .offset = cpu_to_le16(offset),
97 .length = cpu_to_le16(length),
98 .type = cpu_to_le16(section),
99 .op_code = NVM_WRITE_OPCODE,
100 };
101 struct iwl_host_cmd cmd = {
102 .id = NVM_ACCESS_CMD,
103 .len = { sizeof(struct iwl_nvm_access_cmd), length },
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300104 .flags = CMD_SEND_IN_RFKILL,
Eran Harary12147552013-05-09 08:07:59 +0300105 .data = { &nvm_access_cmd, data },
106 /* data may come from vmalloc, so use _DUP */
107 .dataflags = { 0, IWL_HCMD_DFL_DUP },
108 };
109
110 return iwl_mvm_send_cmd(mvm, &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100111}
112
113static int iwl_nvm_read_chunk(struct iwl_mvm *mvm, u16 section,
114 u16 offset, u16 length, u8 *data)
115{
Eran Harary12147552013-05-09 08:07:59 +0300116 struct iwl_nvm_access_cmd nvm_access_cmd = {
117 .offset = cpu_to_le16(offset),
118 .length = cpu_to_le16(length),
119 .type = cpu_to_le16(section),
120 .op_code = NVM_READ_OPCODE,
121 };
Emmanuel Grumbachb9545b42013-03-06 11:34:44 +0200122 struct iwl_nvm_access_resp *nvm_resp;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100123 struct iwl_rx_packet *pkt;
124 struct iwl_host_cmd cmd = {
125 .id = NVM_ACCESS_CMD,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300126 .flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100127 .data = { &nvm_access_cmd, },
128 };
129 int ret, bytes_read, offset_read;
130 u8 *resp_data;
131
Emmanuel Grumbachb9545b42013-03-06 11:34:44 +0200132 cmd.len[0] = sizeof(struct iwl_nvm_access_cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100133
134 ret = iwl_mvm_send_cmd(mvm, &cmd);
135 if (ret)
136 return ret;
137
138 pkt = cmd.resp_pkt;
139 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
140 IWL_ERR(mvm, "Bad return from NVM_ACCES_COMMAND (0x%08X)\n",
141 pkt->hdr.flags);
142 ret = -EIO;
143 goto exit;
144 }
145
146 /* Extract NVM response */
147 nvm_resp = (void *)pkt->data;
Emmanuel Grumbachb9545b42013-03-06 11:34:44 +0200148 ret = le16_to_cpu(nvm_resp->status);
149 bytes_read = le16_to_cpu(nvm_resp->length);
150 offset_read = le16_to_cpu(nvm_resp->offset);
151 resp_data = nvm_resp->data;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100152 if (ret) {
Eran Hararyd6aeb352014-05-11 09:44:17 +0300153 if ((offset != 0) &&
154 (ret == READ_NVM_CHUNK_NOT_VALID_ADDRESS)) {
155 /*
156 * meaning of NOT_VALID_ADDRESS:
157 * driver try to read chunk from address that is
158 * multiple of 2K and got an error since addr is empty.
159 * meaning of (offset != 0): driver already
160 * read valid data from another chunk so this case
161 * is not an error.
162 */
163 IWL_DEBUG_EEPROM(mvm->trans->dev,
164 "NVM access command failed on offset 0x%x since that section size is multiple 2K\n",
165 offset);
166 ret = 0;
167 } else {
168 IWL_DEBUG_EEPROM(mvm->trans->dev,
169 "NVM access command failed with status %d (device: %s)\n",
170 ret, mvm->cfg->name);
171 ret = -EIO;
172 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100173 goto exit;
174 }
175
176 if (offset_read != offset) {
177 IWL_ERR(mvm, "NVM ACCESS response with invalid offset %d\n",
178 offset_read);
179 ret = -EINVAL;
180 goto exit;
181 }
182
183 /* Write data to NVM */
184 memcpy(data + offset, resp_data, bytes_read);
185 ret = bytes_read;
186
187exit:
188 iwl_free_resp(&cmd);
189 return ret;
190}
191
Eran Harary12147552013-05-09 08:07:59 +0300192static int iwl_nvm_write_section(struct iwl_mvm *mvm, u16 section,
193 const u8 *data, u16 length)
194{
195 int offset = 0;
196
197 /* copy data in chunks of 2k (and remainder if any) */
198
199 while (offset < length) {
200 int chunk_size, ret;
201
202 chunk_size = min(IWL_NVM_DEFAULT_CHUNK_SIZE,
203 length - offset);
204
205 ret = iwl_nvm_write_chunk(mvm, section, offset,
206 chunk_size, data + offset);
207 if (ret < 0)
208 return ret;
209
210 offset += chunk_size;
211 }
212
213 return 0;
214}
215
Johannes Berg8ca151b2013-01-24 14:25:36 +0100216/*
217 * Reads an NVM section completely.
218 * NICs prior to 7000 family doesn't have a real NVM, but just read
219 * section 0 which is the EEPROM. Because the EEPROM reading is unlimited
220 * by uCode, we need to manually check in this case that we don't
221 * overflow and try to read more than the EEPROM size.
222 * For 7000 family NICs, we supply the maximal size we can read, and
223 * the uCode fills the response with as much data as we can,
224 * without overflowing, so no check is needed.
225 */
226static int iwl_nvm_read_section(struct iwl_mvm *mvm, u16 section,
Liad Kaufman5daddc92014-05-21 14:37:00 +0300227 u8 *data, u32 size_read)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100228{
229 u16 length, offset = 0;
230 int ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100231
Dor Shaish1fd4afe2013-02-27 10:18:07 +0200232 /* Set nvm section read length */
233 length = IWL_NVM_DEFAULT_CHUNK_SIZE;
234
Johannes Berg8ca151b2013-01-24 14:25:36 +0100235 ret = length;
236
237 /* Read the NVM until exhausted (reading less than requested) */
238 while (ret == length) {
Liad Kaufman5daddc92014-05-21 14:37:00 +0300239 /* Check no memory assumptions fail and cause an overflow */
240 if ((size_read + offset + length) >
241 mvm->cfg->base_params->eeprom_size) {
242 IWL_ERR(mvm, "EEPROM size is too small for NVM\n");
243 return -ENOBUFS;
244 }
245
Johannes Berg8ca151b2013-01-24 14:25:36 +0100246 ret = iwl_nvm_read_chunk(mvm, section, offset, length, data);
247 if (ret < 0) {
Eran Hararyd6aeb352014-05-11 09:44:17 +0300248 IWL_DEBUG_EEPROM(mvm->trans->dev,
249 "Cannot read NVM from section %d offset %d, length %d\n",
250 section, offset, length);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100251 return ret;
252 }
253 offset += ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100254 }
255
Johannes Berg07fd7d22013-05-15 14:38:25 +0200256 IWL_DEBUG_EEPROM(mvm->trans->dev,
257 "NVM section %d read completed\n", section);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100258 return offset;
259}
260
261static struct iwl_nvm_data *
262iwl_parse_nvm_sections(struct iwl_mvm *mvm)
263{
264 struct iwl_nvm_section *sections = mvm->nvm_sections;
Eran Harary77db0a32014-02-04 14:21:38 +0200265 const __le16 *hw, *sw, *calib, *regulatory, *mac_override;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100266
267 /* Checking for required sections */
Eran Harary77db0a32014-02-04 14:21:38 +0200268 if (mvm->trans->cfg->device_family != IWL_DEVICE_FAMILY_8000) {
269 if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data ||
270 !mvm->nvm_sections[mvm->cfg->nvm_hw_section_num].data) {
Eran Hararyabf09c52014-07-01 17:33:29 +0300271 IWL_ERR(mvm, "Can't parse empty OTP/NVM sections\n");
Eran Harary77db0a32014-02-04 14:21:38 +0200272 return NULL;
273 }
274 } else {
Eran Harary9f32e012014-04-28 15:22:40 +0300275 /* SW and REGULATORY sections are mandatory */
Eran Harary77db0a32014-02-04 14:21:38 +0200276 if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data ||
Eran Harary77db0a32014-02-04 14:21:38 +0200277 !mvm->nvm_sections[NVM_SECTION_TYPE_REGULATORY].data) {
278 IWL_ERR(mvm,
Eran Hararyabf09c52014-07-01 17:33:29 +0300279 "Can't parse empty family 8000 OTP/NVM sections\n");
Eran Harary77db0a32014-02-04 14:21:38 +0200280 return NULL;
281 }
Eran Harary9f32e012014-04-28 15:22:40 +0300282 /* MAC_OVERRIDE or at least HW section must exist */
Eran Hararybb926922014-04-30 15:31:59 +0300283 if (!mvm->nvm_sections[mvm->cfg->nvm_hw_section_num].data &&
Eran Harary9f32e012014-04-28 15:22:40 +0300284 !mvm->nvm_sections[NVM_SECTION_TYPE_MAC_OVERRIDE].data) {
285 IWL_ERR(mvm,
286 "Can't parse mac_address, empty sections\n");
287 return NULL;
288 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100289 }
290
291 if (WARN_ON(!mvm->cfg))
292 return NULL;
293
Eran Hararyae2b21b2014-01-09 08:08:24 +0200294 hw = (const __le16 *)sections[mvm->cfg->nvm_hw_section_num].data;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100295 sw = (const __le16 *)sections[NVM_SECTION_TYPE_SW].data;
296 calib = (const __le16 *)sections[NVM_SECTION_TYPE_CALIBRATION].data;
Eran Harary77db0a32014-02-04 14:21:38 +0200297 regulatory = (const __le16 *)sections[NVM_SECTION_TYPE_REGULATORY].data;
298 mac_override =
299 (const __le16 *)sections[NVM_SECTION_TYPE_MAC_OVERRIDE].data;
300
Emmanuel Grumbach9ce4fa72013-05-22 13:16:23 +0300301 return iwl_parse_nvm_data(mvm->trans->dev, mvm->cfg, hw, sw, calib,
Eran Harary77db0a32014-02-04 14:21:38 +0200302 regulatory, mac_override,
Johannes Berg4ed735e2014-02-12 21:47:44 +0100303 mvm->fw->valid_tx_ant,
304 mvm->fw->valid_rx_ant);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100305}
306
Eran Harary12147552013-05-09 08:07:59 +0300307#define MAX_NVM_FILE_LEN 16384
308
309/*
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200310 * Reads external NVM from a file into mvm->nvm_sections
311 *
Eran Harary12147552013-05-09 08:07:59 +0300312 * HOW TO CREATE THE NVM FILE FORMAT:
313 * ------------------------------
314 * 1. create hex file, format:
315 * 3800 -> header
316 * 0000 -> header
317 * 5a40 -> data
318 *
319 * rev - 6 bit (word1)
320 * len - 10 bit (word1)
321 * id - 4 bit (word2)
322 * rsv - 12 bit (word2)
323 *
324 * 2. flip 8bits with 8 bits per line to get the right NVM file format
325 *
326 * 3. create binary file from the hex file
327 *
328 * 4. save as "iNVM_xxx.bin" under /lib/firmware
329 */
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200330static int iwl_mvm_read_external_nvm(struct iwl_mvm *mvm)
Eran Harary12147552013-05-09 08:07:59 +0300331{
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200332 int ret, section_size;
333 u16 section_id;
Eran Harary12147552013-05-09 08:07:59 +0300334 const struct firmware *fw_entry;
335 const struct {
336 __le16 word1;
337 __le16 word2;
338 u8 data[];
339 } *file_sec;
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200340 const u8 *eof, *temp;
Liad Kaufmanf251c072014-04-28 14:42:04 +0300341 int max_section_size;
Eran Harary12147552013-05-09 08:07:59 +0300342
343#define NVM_WORD1_LEN(x) (8 * (x & 0x03FF))
344#define NVM_WORD2_ID(x) (x >> 12)
Eran Harary77db0a32014-02-04 14:21:38 +0200345#define NVM_WORD2_LEN_FAMILY_8000(x) (2 * ((x & 0xFF) << 8 | x >> 8))
346#define NVM_WORD1_ID_FAMILY_8000(x) (x >> 4)
Eran Harary12147552013-05-09 08:07:59 +0300347
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200348 IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from external NVM\n");
349
Liad Kaufmanf251c072014-04-28 14:42:04 +0300350 /* Maximal size depends on HW family and step */
351 if (mvm->trans->cfg->device_family != IWL_DEVICE_FAMILY_8000)
352 max_section_size = IWL_MAX_NVM_SECTION_SIZE;
Liad Kaufmanc2a2b282014-09-07 11:41:05 +0300353 else if (CSR_HW_REV_STEP(mvm->trans->hw_rev) == SILICON_A_STEP)
Liad Kaufmanf251c072014-04-28 14:42:04 +0300354 max_section_size = IWL_MAX_NVM_8000A_SECTION_SIZE;
355 else /* Family 8000 B-step */
356 max_section_size = IWL_MAX_NVM_8000B_SECTION_SIZE;
357
Eran Harary12147552013-05-09 08:07:59 +0300358 /*
359 * Obtain NVM image via request_firmware. Since we already used
360 * request_firmware_nowait() for the firmware binary load and only
361 * get here after that we assume the NVM request can be satisfied
362 * synchronously.
363 */
Eran Hararye02a9d62014-05-07 12:27:10 +0300364 ret = request_firmware(&fw_entry, mvm->nvm_file_name,
Eran Harary12147552013-05-09 08:07:59 +0300365 mvm->trans->dev);
366 if (ret) {
367 IWL_ERR(mvm, "ERROR: %s isn't available %d\n",
Eran Hararye02a9d62014-05-07 12:27:10 +0300368 mvm->nvm_file_name, ret);
Eran Harary12147552013-05-09 08:07:59 +0300369 return ret;
370 }
371
372 IWL_INFO(mvm, "Loaded NVM file %s (%zu bytes)\n",
Eran Hararye02a9d62014-05-07 12:27:10 +0300373 mvm->nvm_file_name, fw_entry->size);
Eran Harary12147552013-05-09 08:07:59 +0300374
375 if (fw_entry->size < sizeof(*file_sec)) {
376 IWL_ERR(mvm, "NVM file too small\n");
377 ret = -EINVAL;
378 goto out;
379 }
380
381 if (fw_entry->size > MAX_NVM_FILE_LEN) {
382 IWL_ERR(mvm, "NVM file too large\n");
383 ret = -EINVAL;
384 goto out;
385 }
386
387 eof = fw_entry->data + fw_entry->size;
388
389 file_sec = (void *)fw_entry->data;
390
391 while (true) {
392 if (file_sec->data > eof) {
393 IWL_ERR(mvm,
394 "ERROR - NVM file too short for section header\n");
395 ret = -EINVAL;
396 break;
397 }
398
399 /* check for EOF marker */
400 if (!file_sec->word1 && !file_sec->word2) {
401 ret = 0;
402 break;
403 }
404
Eran Harary77db0a32014-02-04 14:21:38 +0200405 if (mvm->trans->cfg->device_family != IWL_DEVICE_FAMILY_8000) {
406 section_size =
407 2 * NVM_WORD1_LEN(le16_to_cpu(file_sec->word1));
408 section_id = NVM_WORD2_ID(le16_to_cpu(file_sec->word2));
409 } else {
410 section_size = 2 * NVM_WORD2_LEN_FAMILY_8000(
411 le16_to_cpu(file_sec->word2));
412 section_id = NVM_WORD1_ID_FAMILY_8000(
413 le16_to_cpu(file_sec->word1));
414 }
Eran Harary12147552013-05-09 08:07:59 +0300415
Liad Kaufmanf251c072014-04-28 14:42:04 +0300416 if (section_size > max_section_size) {
Eran Harary12147552013-05-09 08:07:59 +0300417 IWL_ERR(mvm, "ERROR - section too large (%d)\n",
418 section_size);
419 ret = -EINVAL;
420 break;
421 }
422
423 if (!section_size) {
424 IWL_ERR(mvm, "ERROR - section empty\n");
425 ret = -EINVAL;
426 break;
427 }
428
429 if (file_sec->data + section_size > eof) {
430 IWL_ERR(mvm,
431 "ERROR - NVM file too short for section (%d bytes)\n",
432 section_size);
433 ret = -EINVAL;
434 break;
435 }
436
Eran Hararyae2b21b2014-01-09 08:08:24 +0200437 if (WARN(section_id >= NVM_MAX_NUM_SECTIONS,
Eytan Lifshitza4a12472013-12-18 23:05:06 +0200438 "Invalid NVM section ID %d\n", section_id)) {
439 ret = -EINVAL;
440 break;
441 }
442
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200443 temp = kmemdup(file_sec->data, section_size, GFP_KERNEL);
444 if (!temp) {
445 ret = -ENOMEM;
Eran Harary12147552013-05-09 08:07:59 +0300446 break;
447 }
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200448 mvm->nvm_sections[section_id].data = temp;
449 mvm->nvm_sections[section_id].length = section_size;
Eran Harary12147552013-05-09 08:07:59 +0300450
451 /* advance to the next section */
452 file_sec = (void *)(file_sec->data + section_size);
453 }
454out:
455 release_firmware(fw_entry);
456 return ret;
457}
458
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200459/* Loads the NVM data stored in mvm->nvm_sections into the NIC */
460int iwl_mvm_load_nvm_to_nic(struct iwl_mvm *mvm)
461{
Eytan Lifshitz05159fc2014-01-07 12:50:45 +0200462 int i, ret = 0;
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200463 struct iwl_nvm_section *sections = mvm->nvm_sections;
464
465 IWL_DEBUG_EEPROM(mvm->trans->dev, "'Write to NVM\n");
466
Emmanuel Grumbach099d8f22014-01-05 15:09:44 +0200467 for (i = 0; i < ARRAY_SIZE(mvm->nvm_sections); i++) {
468 if (!mvm->nvm_sections[i].data || !mvm->nvm_sections[i].length)
469 continue;
470 ret = iwl_nvm_write_section(mvm, i, sections[i].data,
471 sections[i].length);
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200472 if (ret < 0) {
473 IWL_ERR(mvm, "iwl_mvm_send_cmd failed: %d\n", ret);
474 break;
475 }
476 }
477 return ret;
478}
479
Eran Harary14b485f2014-04-23 10:46:09 +0300480int iwl_nvm_init(struct iwl_mvm *mvm, bool read_nvm_from_nic)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100481{
Eran Hararyd6aeb352014-05-11 09:44:17 +0300482 int ret, section;
Liad Kaufman5daddc92014-05-21 14:37:00 +0300483 u32 size_read = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100484 u8 *nvm_buffer, *temp;
485
Eran Hararyae2b21b2014-01-09 08:08:24 +0200486 if (WARN_ON_ONCE(mvm->cfg->nvm_hw_section_num >= NVM_MAX_NUM_SECTIONS))
487 return -EINVAL;
488
Eran Harary26481bf2014-03-25 14:14:44 +0200489 /* load NVM values from nic */
Eran Harary14b485f2014-04-23 10:46:09 +0300490 if (read_nvm_from_nic) {
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200491 /* Read From FW NVM */
492 IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from NVM\n");
Eran Harary12147552013-05-09 08:07:59 +0300493
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200494 nvm_buffer = kmalloc(mvm->cfg->base_params->eeprom_size,
495 GFP_KERNEL);
496 if (!nvm_buffer)
497 return -ENOMEM;
Eran Hararyd6aeb352014-05-11 09:44:17 +0300498 for (section = 0; section < NVM_MAX_NUM_SECTIONS; section++) {
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200499 /* we override the constness for initial read */
Liad Kaufman5daddc92014-05-21 14:37:00 +0300500 ret = iwl_nvm_read_section(mvm, section, nvm_buffer,
501 size_read);
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200502 if (ret < 0)
Eran Hararyd6aeb352014-05-11 09:44:17 +0300503 continue;
Liad Kaufman5daddc92014-05-21 14:37:00 +0300504 size_read += ret;
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200505 temp = kmemdup(nvm_buffer, ret, GFP_KERNEL);
506 if (!temp) {
507 ret = -ENOMEM;
508 break;
509 }
510 mvm->nvm_sections[section].data = temp;
511 mvm->nvm_sections[section].length = ret;
Emmanuel Grumbach086f7362013-11-18 17:00:03 +0200512
513#ifdef CONFIG_IWLWIFI_DEBUGFS
514 switch (section) {
Emmanuel Grumbach086f7362013-11-18 17:00:03 +0200515 case NVM_SECTION_TYPE_SW:
516 mvm->nvm_sw_blob.data = temp;
517 mvm->nvm_sw_blob.size = ret;
518 break;
519 case NVM_SECTION_TYPE_CALIBRATION:
520 mvm->nvm_calib_blob.data = temp;
521 mvm->nvm_calib_blob.size = ret;
522 break;
523 case NVM_SECTION_TYPE_PRODUCTION:
524 mvm->nvm_prod_blob.data = temp;
525 mvm->nvm_prod_blob.size = ret;
526 break;
527 default:
Eran Hararyae2b21b2014-01-09 08:08:24 +0200528 if (section == mvm->cfg->nvm_hw_section_num) {
529 mvm->nvm_hw_blob.data = temp;
530 mvm->nvm_hw_blob.size = ret;
531 break;
532 }
Emmanuel Grumbach086f7362013-11-18 17:00:03 +0200533 }
534#endif
Johannes Berg8ca151b2013-01-24 14:25:36 +0100535 }
Eran Hararybdce40f2014-07-01 17:31:38 +0300536 if (!size_read)
537 IWL_ERR(mvm, "OTP is blank\n");
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200538 kfree(nvm_buffer);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100539 }
540
Eran Harary26481bf2014-03-25 14:14:44 +0200541 /* load external NVM if configured */
Eran Hararye02a9d62014-05-07 12:27:10 +0300542 if (mvm->nvm_file_name) {
Eran Harary26481bf2014-03-25 14:14:44 +0200543 /* move to External NVM flow */
544 ret = iwl_mvm_read_external_nvm(mvm);
545 if (ret)
546 return ret;
547 }
548
549 /* parse the relevant nvm sections */
Emmanuel Grumbachb9545b42013-03-06 11:34:44 +0200550 mvm->nvm_data = iwl_parse_nvm_sections(mvm);
Johannes Berg82598b42013-05-13 21:44:42 +0200551 if (!mvm->nvm_data)
552 return -ENODATA;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100553
Johannes Berg82598b42013-05-13 21:44:42 +0200554 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100555}