blob: cfdd314fdd5decd15b57bf02b3ad63eb1ab16cd0 [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 Berg8ca151b2013-01-24 14:25:36 +01009 *
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
Emmanuel Grumbach410dc5a2013-02-18 09:22:28 +020025 * in the file called COPYING.
Johannes Berg8ca151b2013-01-24 14:25:36 +010026 *
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 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +020033 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Johannes Berg8ca151b2013-01-24 14:25:36 +010034 * 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 *****************************************************************************/
Eran Harary12147552013-05-09 08:07:59 +030063#include <linux/firmware.h>
Johannes Berg8ca151b2013-01-24 14:25:36 +010064#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
Dor Shaish1fd4afe2013-02-27 10:18:07 +020070/* Default NVM size to read */
Eran Harary12147552013-05-09 08:07:59 +030071#define IWL_NVM_DEFAULT_CHUNK_SIZE (2*1024)
Liad Kaufmanf251c072014-04-28 14:42:04 +030072#define IWL_MAX_NVM_SECTION_SIZE 0x1b58
73#define IWL_MAX_NVM_8000A_SECTION_SIZE 0xffc
74#define IWL_MAX_NVM_8000B_SECTION_SIZE 0x1ffc
Dor Shaish1fd4afe2013-02-27 10:18:07 +020075
Eran Harary12147552013-05-09 08:07:59 +030076#define NVM_WRITE_OPCODE 1
77#define NVM_READ_OPCODE 0
78
Eran Hararyd6aeb352014-05-11 09:44:17 +030079/* load nvm chunk response */
80enum {
81 READ_NVM_CHUNK_SUCCEED = 0,
82 READ_NVM_CHUNK_NOT_VALID_ADDRESS = 1
83};
84
Eran Harary12147552013-05-09 08:07:59 +030085/*
86 * prepare the NVM host command w/ the pointers to the nvm buffer
87 * and send it to fw
88 */
89static int iwl_nvm_write_chunk(struct iwl_mvm *mvm, u16 section,
90 u16 offset, u16 length, const u8 *data)
Johannes Berg8ca151b2013-01-24 14:25:36 +010091{
Eran Harary12147552013-05-09 08:07:59 +030092 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 },
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300101 .flags = CMD_SEND_IN_RFKILL,
Eran Harary12147552013-05-09 08:07:59 +0300102 .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);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100108}
109
110static int iwl_nvm_read_chunk(struct iwl_mvm *mvm, u16 section,
111 u16 offset, u16 length, u8 *data)
112{
Eran Harary12147552013-05-09 08:07:59 +0300113 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 };
Emmanuel Grumbachb9545b42013-03-06 11:34:44 +0200119 struct iwl_nvm_access_resp *nvm_resp;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100120 struct iwl_rx_packet *pkt;
121 struct iwl_host_cmd cmd = {
122 .id = NVM_ACCESS_CMD,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300123 .flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100124 .data = { &nvm_access_cmd, },
125 };
126 int ret, bytes_read, offset_read;
127 u8 *resp_data;
128
Emmanuel Grumbachb9545b42013-03-06 11:34:44 +0200129 cmd.len[0] = sizeof(struct iwl_nvm_access_cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100130
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;
Emmanuel Grumbachb9545b42013-03-06 11:34:44 +0200145 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;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100149 if (ret) {
Eran Hararyd6aeb352014-05-11 09:44:17 +0300150 if ((offset != 0) &&
151 (ret == READ_NVM_CHUNK_NOT_VALID_ADDRESS)) {
152 /*
153 * meaning of NOT_VALID_ADDRESS:
154 * driver try to read chunk from address that is
155 * multiple of 2K and got an error since addr is empty.
156 * meaning of (offset != 0): driver already
157 * read valid data from another chunk so this case
158 * is not an error.
159 */
160 IWL_DEBUG_EEPROM(mvm->trans->dev,
161 "NVM access command failed on offset 0x%x since that section size is multiple 2K\n",
162 offset);
163 ret = 0;
164 } else {
165 IWL_DEBUG_EEPROM(mvm->trans->dev,
166 "NVM access command failed with status %d (device: %s)\n",
167 ret, mvm->cfg->name);
168 ret = -EIO;
169 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100170 goto exit;
171 }
172
173 if (offset_read != offset) {
174 IWL_ERR(mvm, "NVM ACCESS response with invalid offset %d\n",
175 offset_read);
176 ret = -EINVAL;
177 goto exit;
178 }
179
180 /* Write data to NVM */
181 memcpy(data + offset, resp_data, bytes_read);
182 ret = bytes_read;
183
184exit:
185 iwl_free_resp(&cmd);
186 return ret;
187}
188
Eran Harary12147552013-05-09 08:07:59 +0300189static int iwl_nvm_write_section(struct iwl_mvm *mvm, u16 section,
190 const u8 *data, u16 length)
191{
192 int offset = 0;
193
194 /* copy data in chunks of 2k (and remainder if any) */
195
196 while (offset < length) {
197 int chunk_size, ret;
198
199 chunk_size = min(IWL_NVM_DEFAULT_CHUNK_SIZE,
200 length - offset);
201
202 ret = iwl_nvm_write_chunk(mvm, section, offset,
203 chunk_size, data + offset);
204 if (ret < 0)
205 return ret;
206
207 offset += chunk_size;
208 }
209
210 return 0;
211}
212
Johannes Berg8ca151b2013-01-24 14:25:36 +0100213/*
214 * Reads an NVM section completely.
215 * NICs prior to 7000 family doesn't have a real NVM, but just read
216 * section 0 which is the EEPROM. Because the EEPROM reading is unlimited
217 * by uCode, we need to manually check in this case that we don't
218 * overflow and try to read more than the EEPROM size.
219 * For 7000 family NICs, we supply the maximal size we can read, and
220 * the uCode fills the response with as much data as we can,
221 * without overflowing, so no check is needed.
222 */
223static int iwl_nvm_read_section(struct iwl_mvm *mvm, u16 section,
Liad Kaufman5daddc92014-05-21 14:37:00 +0300224 u8 *data, u32 size_read)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100225{
226 u16 length, offset = 0;
227 int ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100228
Dor Shaish1fd4afe2013-02-27 10:18:07 +0200229 /* Set nvm section read length */
230 length = IWL_NVM_DEFAULT_CHUNK_SIZE;
231
Johannes Berg8ca151b2013-01-24 14:25:36 +0100232 ret = length;
233
234 /* Read the NVM until exhausted (reading less than requested) */
235 while (ret == length) {
Liad Kaufman5daddc92014-05-21 14:37:00 +0300236 /* Check no memory assumptions fail and cause an overflow */
237 if ((size_read + offset + length) >
238 mvm->cfg->base_params->eeprom_size) {
239 IWL_ERR(mvm, "EEPROM size is too small for NVM\n");
240 return -ENOBUFS;
241 }
242
Johannes Berg8ca151b2013-01-24 14:25:36 +0100243 ret = iwl_nvm_read_chunk(mvm, section, offset, length, data);
244 if (ret < 0) {
Eran Hararyd6aeb352014-05-11 09:44:17 +0300245 IWL_DEBUG_EEPROM(mvm->trans->dev,
246 "Cannot read NVM from section %d offset %d, length %d\n",
247 section, offset, length);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100248 return ret;
249 }
250 offset += ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100251 }
252
Johannes Berg07fd7d22013-05-15 14:38:25 +0200253 IWL_DEBUG_EEPROM(mvm->trans->dev,
254 "NVM section %d read completed\n", section);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100255 return offset;
256}
257
258static struct iwl_nvm_data *
259iwl_parse_nvm_sections(struct iwl_mvm *mvm)
260{
261 struct iwl_nvm_section *sections = mvm->nvm_sections;
Eran Harary77db0a32014-02-04 14:21:38 +0200262 const __le16 *hw, *sw, *calib, *regulatory, *mac_override;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100263
264 /* Checking for required sections */
Eran Harary77db0a32014-02-04 14:21:38 +0200265 if (mvm->trans->cfg->device_family != IWL_DEVICE_FAMILY_8000) {
266 if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data ||
267 !mvm->nvm_sections[mvm->cfg->nvm_hw_section_num].data) {
Eran Hararyabf09c52014-07-01 17:33:29 +0300268 IWL_ERR(mvm, "Can't parse empty OTP/NVM sections\n");
Eran Harary77db0a32014-02-04 14:21:38 +0200269 return NULL;
270 }
271 } else {
Eran Harary9f32e012014-04-28 15:22:40 +0300272 /* SW and REGULATORY sections are mandatory */
Eran Harary77db0a32014-02-04 14:21:38 +0200273 if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data ||
Eran Harary77db0a32014-02-04 14:21:38 +0200274 !mvm->nvm_sections[NVM_SECTION_TYPE_REGULATORY].data) {
275 IWL_ERR(mvm,
Eran Hararyabf09c52014-07-01 17:33:29 +0300276 "Can't parse empty family 8000 OTP/NVM sections\n");
Eran Harary77db0a32014-02-04 14:21:38 +0200277 return NULL;
278 }
Eran Harary9f32e012014-04-28 15:22:40 +0300279 /* MAC_OVERRIDE or at least HW section must exist */
Eran Hararybb926922014-04-30 15:31:59 +0300280 if (!mvm->nvm_sections[mvm->cfg->nvm_hw_section_num].data &&
Eran Harary9f32e012014-04-28 15:22:40 +0300281 !mvm->nvm_sections[NVM_SECTION_TYPE_MAC_OVERRIDE].data) {
282 IWL_ERR(mvm,
283 "Can't parse mac_address, empty sections\n");
284 return NULL;
285 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100286 }
287
288 if (WARN_ON(!mvm->cfg))
289 return NULL;
290
Eran Hararyae2b21b2014-01-09 08:08:24 +0200291 hw = (const __le16 *)sections[mvm->cfg->nvm_hw_section_num].data;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100292 sw = (const __le16 *)sections[NVM_SECTION_TYPE_SW].data;
293 calib = (const __le16 *)sections[NVM_SECTION_TYPE_CALIBRATION].data;
Eran Harary77db0a32014-02-04 14:21:38 +0200294 regulatory = (const __le16 *)sections[NVM_SECTION_TYPE_REGULATORY].data;
295 mac_override =
296 (const __le16 *)sections[NVM_SECTION_TYPE_MAC_OVERRIDE].data;
297
Emmanuel Grumbach9ce4fa72013-05-22 13:16:23 +0300298 return iwl_parse_nvm_data(mvm->trans->dev, mvm->cfg, hw, sw, calib,
Eran Harary77db0a32014-02-04 14:21:38 +0200299 regulatory, mac_override,
Johannes Berg4ed735e2014-02-12 21:47:44 +0100300 mvm->fw->valid_tx_ant,
301 mvm->fw->valid_rx_ant);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100302}
303
Eran Harary12147552013-05-09 08:07:59 +0300304#define MAX_NVM_FILE_LEN 16384
305
306/*
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200307 * Reads external NVM from a file into mvm->nvm_sections
308 *
Eran Harary12147552013-05-09 08:07:59 +0300309 * HOW TO CREATE THE NVM FILE FORMAT:
310 * ------------------------------
311 * 1. create hex file, format:
312 * 3800 -> header
313 * 0000 -> header
314 * 5a40 -> data
315 *
316 * rev - 6 bit (word1)
317 * len - 10 bit (word1)
318 * id - 4 bit (word2)
319 * rsv - 12 bit (word2)
320 *
321 * 2. flip 8bits with 8 bits per line to get the right NVM file format
322 *
323 * 3. create binary file from the hex file
324 *
325 * 4. save as "iNVM_xxx.bin" under /lib/firmware
326 */
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200327static int iwl_mvm_read_external_nvm(struct iwl_mvm *mvm)
Eran Harary12147552013-05-09 08:07:59 +0300328{
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200329 int ret, section_size;
330 u16 section_id;
Eran Harary12147552013-05-09 08:07:59 +0300331 const struct firmware *fw_entry;
332 const struct {
333 __le16 word1;
334 __le16 word2;
335 u8 data[];
336 } *file_sec;
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200337 const u8 *eof, *temp;
Liad Kaufmanf251c072014-04-28 14:42:04 +0300338 int max_section_size;
Eran Harary12147552013-05-09 08:07:59 +0300339
340#define NVM_WORD1_LEN(x) (8 * (x & 0x03FF))
341#define NVM_WORD2_ID(x) (x >> 12)
Eran Harary77db0a32014-02-04 14:21:38 +0200342#define NVM_WORD2_LEN_FAMILY_8000(x) (2 * ((x & 0xFF) << 8 | x >> 8))
343#define NVM_WORD1_ID_FAMILY_8000(x) (x >> 4)
Eran Harary12147552013-05-09 08:07:59 +0300344
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200345 IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from external NVM\n");
346
Liad Kaufmanf251c072014-04-28 14:42:04 +0300347 /* Maximal size depends on HW family and step */
348 if (mvm->trans->cfg->device_family != IWL_DEVICE_FAMILY_8000)
349 max_section_size = IWL_MAX_NVM_SECTION_SIZE;
350 else if ((mvm->trans->hw_rev & 0xc) == 0) /* Family 8000 A-step */
351 max_section_size = IWL_MAX_NVM_8000A_SECTION_SIZE;
352 else /* Family 8000 B-step */
353 max_section_size = IWL_MAX_NVM_8000B_SECTION_SIZE;
354
Eran Harary12147552013-05-09 08:07:59 +0300355 /*
356 * Obtain NVM image via request_firmware. Since we already used
357 * request_firmware_nowait() for the firmware binary load and only
358 * get here after that we assume the NVM request can be satisfied
359 * synchronously.
360 */
Eran Hararye02a9d62014-05-07 12:27:10 +0300361 ret = request_firmware(&fw_entry, mvm->nvm_file_name,
Eran Harary12147552013-05-09 08:07:59 +0300362 mvm->trans->dev);
363 if (ret) {
364 IWL_ERR(mvm, "ERROR: %s isn't available %d\n",
Eran Hararye02a9d62014-05-07 12:27:10 +0300365 mvm->nvm_file_name, ret);
Eran Harary12147552013-05-09 08:07:59 +0300366 return ret;
367 }
368
369 IWL_INFO(mvm, "Loaded NVM file %s (%zu bytes)\n",
Eran Hararye02a9d62014-05-07 12:27:10 +0300370 mvm->nvm_file_name, fw_entry->size);
Eran Harary12147552013-05-09 08:07:59 +0300371
372 if (fw_entry->size < sizeof(*file_sec)) {
373 IWL_ERR(mvm, "NVM file too small\n");
374 ret = -EINVAL;
375 goto out;
376 }
377
378 if (fw_entry->size > MAX_NVM_FILE_LEN) {
379 IWL_ERR(mvm, "NVM file too large\n");
380 ret = -EINVAL;
381 goto out;
382 }
383
384 eof = fw_entry->data + fw_entry->size;
385
386 file_sec = (void *)fw_entry->data;
387
388 while (true) {
389 if (file_sec->data > eof) {
390 IWL_ERR(mvm,
391 "ERROR - NVM file too short for section header\n");
392 ret = -EINVAL;
393 break;
394 }
395
396 /* check for EOF marker */
397 if (!file_sec->word1 && !file_sec->word2) {
398 ret = 0;
399 break;
400 }
401
Eran Harary77db0a32014-02-04 14:21:38 +0200402 if (mvm->trans->cfg->device_family != IWL_DEVICE_FAMILY_8000) {
403 section_size =
404 2 * NVM_WORD1_LEN(le16_to_cpu(file_sec->word1));
405 section_id = NVM_WORD2_ID(le16_to_cpu(file_sec->word2));
406 } else {
407 section_size = 2 * NVM_WORD2_LEN_FAMILY_8000(
408 le16_to_cpu(file_sec->word2));
409 section_id = NVM_WORD1_ID_FAMILY_8000(
410 le16_to_cpu(file_sec->word1));
411 }
Eran Harary12147552013-05-09 08:07:59 +0300412
Liad Kaufmanf251c072014-04-28 14:42:04 +0300413 if (section_size > max_section_size) {
Eran Harary12147552013-05-09 08:07:59 +0300414 IWL_ERR(mvm, "ERROR - section too large (%d)\n",
415 section_size);
416 ret = -EINVAL;
417 break;
418 }
419
420 if (!section_size) {
421 IWL_ERR(mvm, "ERROR - section empty\n");
422 ret = -EINVAL;
423 break;
424 }
425
426 if (file_sec->data + section_size > eof) {
427 IWL_ERR(mvm,
428 "ERROR - NVM file too short for section (%d bytes)\n",
429 section_size);
430 ret = -EINVAL;
431 break;
432 }
433
Eran Hararyae2b21b2014-01-09 08:08:24 +0200434 if (WARN(section_id >= NVM_MAX_NUM_SECTIONS,
Eytan Lifshitza4a12472013-12-18 23:05:06 +0200435 "Invalid NVM section ID %d\n", section_id)) {
436 ret = -EINVAL;
437 break;
438 }
439
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200440 temp = kmemdup(file_sec->data, section_size, GFP_KERNEL);
441 if (!temp) {
442 ret = -ENOMEM;
Eran Harary12147552013-05-09 08:07:59 +0300443 break;
444 }
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200445 mvm->nvm_sections[section_id].data = temp;
446 mvm->nvm_sections[section_id].length = section_size;
Eran Harary12147552013-05-09 08:07:59 +0300447
448 /* advance to the next section */
449 file_sec = (void *)(file_sec->data + section_size);
450 }
451out:
452 release_firmware(fw_entry);
453 return ret;
454}
455
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200456/* Loads the NVM data stored in mvm->nvm_sections into the NIC */
457int iwl_mvm_load_nvm_to_nic(struct iwl_mvm *mvm)
458{
Eytan Lifshitz05159fc2014-01-07 12:50:45 +0200459 int i, ret = 0;
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200460 struct iwl_nvm_section *sections = mvm->nvm_sections;
461
462 IWL_DEBUG_EEPROM(mvm->trans->dev, "'Write to NVM\n");
463
Emmanuel Grumbach099d8f22014-01-05 15:09:44 +0200464 for (i = 0; i < ARRAY_SIZE(mvm->nvm_sections); i++) {
465 if (!mvm->nvm_sections[i].data || !mvm->nvm_sections[i].length)
466 continue;
467 ret = iwl_nvm_write_section(mvm, i, sections[i].data,
468 sections[i].length);
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200469 if (ret < 0) {
470 IWL_ERR(mvm, "iwl_mvm_send_cmd failed: %d\n", ret);
471 break;
472 }
473 }
474 return ret;
475}
476
Eran Harary14b485f2014-04-23 10:46:09 +0300477int iwl_nvm_init(struct iwl_mvm *mvm, bool read_nvm_from_nic)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100478{
Eran Hararyd6aeb352014-05-11 09:44:17 +0300479 int ret, section;
Liad Kaufman5daddc92014-05-21 14:37:00 +0300480 u32 size_read = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100481 u8 *nvm_buffer, *temp;
482
Eran Hararyae2b21b2014-01-09 08:08:24 +0200483 if (WARN_ON_ONCE(mvm->cfg->nvm_hw_section_num >= NVM_MAX_NUM_SECTIONS))
484 return -EINVAL;
485
Eran Harary26481bf2014-03-25 14:14:44 +0200486 /* load NVM values from nic */
Eran Harary14b485f2014-04-23 10:46:09 +0300487 if (read_nvm_from_nic) {
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200488 /* Read From FW NVM */
489 IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from NVM\n");
Eran Harary12147552013-05-09 08:07:59 +0300490
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200491 nvm_buffer = kmalloc(mvm->cfg->base_params->eeprom_size,
492 GFP_KERNEL);
493 if (!nvm_buffer)
494 return -ENOMEM;
Eran Hararyd6aeb352014-05-11 09:44:17 +0300495 for (section = 0; section < NVM_MAX_NUM_SECTIONS; section++) {
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200496 /* we override the constness for initial read */
Liad Kaufman5daddc92014-05-21 14:37:00 +0300497 ret = iwl_nvm_read_section(mvm, section, nvm_buffer,
498 size_read);
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200499 if (ret < 0)
Eran Hararyd6aeb352014-05-11 09:44:17 +0300500 continue;
Liad Kaufman5daddc92014-05-21 14:37:00 +0300501 size_read += ret;
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200502 temp = kmemdup(nvm_buffer, ret, GFP_KERNEL);
503 if (!temp) {
504 ret = -ENOMEM;
505 break;
506 }
507 mvm->nvm_sections[section].data = temp;
508 mvm->nvm_sections[section].length = ret;
Emmanuel Grumbach086f7362013-11-18 17:00:03 +0200509
510#ifdef CONFIG_IWLWIFI_DEBUGFS
511 switch (section) {
Emmanuel Grumbach086f7362013-11-18 17:00:03 +0200512 case NVM_SECTION_TYPE_SW:
513 mvm->nvm_sw_blob.data = temp;
514 mvm->nvm_sw_blob.size = ret;
515 break;
516 case NVM_SECTION_TYPE_CALIBRATION:
517 mvm->nvm_calib_blob.data = temp;
518 mvm->nvm_calib_blob.size = ret;
519 break;
520 case NVM_SECTION_TYPE_PRODUCTION:
521 mvm->nvm_prod_blob.data = temp;
522 mvm->nvm_prod_blob.size = ret;
523 break;
524 default:
Eran Hararyae2b21b2014-01-09 08:08:24 +0200525 if (section == mvm->cfg->nvm_hw_section_num) {
526 mvm->nvm_hw_blob.data = temp;
527 mvm->nvm_hw_blob.size = ret;
528 break;
529 }
Emmanuel Grumbach086f7362013-11-18 17:00:03 +0200530 }
531#endif
Johannes Berg8ca151b2013-01-24 14:25:36 +0100532 }
Eran Hararybdce40f2014-07-01 17:31:38 +0300533 if (!size_read)
534 IWL_ERR(mvm, "OTP is blank\n");
Eytan Lifshitz81a67e32013-09-11 12:39:18 +0200535 kfree(nvm_buffer);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100536 }
537
Eran Harary26481bf2014-03-25 14:14:44 +0200538 /* load external NVM if configured */
Eran Hararye02a9d62014-05-07 12:27:10 +0300539 if (mvm->nvm_file_name) {
Eran Harary26481bf2014-03-25 14:14:44 +0200540 /* move to External NVM flow */
541 ret = iwl_mvm_read_external_nvm(mvm);
542 if (ret)
543 return ret;
544 }
545
546 /* parse the relevant nvm sections */
Emmanuel Grumbachb9545b42013-03-06 11:34:44 +0200547 mvm->nvm_data = iwl_parse_nvm_sections(mvm);
Johannes Berg82598b42013-05-13 21:44:42 +0200548 if (!mvm->nvm_data)
549 return -ENODATA;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100550
Johannes Berg82598b42013-05-13 21:44:42 +0200551 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100552}