fix a kmap leak in virtio_console
[linux/fpc-iii.git] / drivers / net / wireless / iwlwifi / mvm / nvm.c
blob35b71af78d029d90311b878f640f84c38b9d331d
1 /******************************************************************************
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.
6 * GPL LICENSE SUMMARY
8 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
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.
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.
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
24 * The full GNU General Public License is included in this distribution
25 * in the file called COPYING.
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
31 * BSD LICENSE
33 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
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.
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.
62 *****************************************************************************/
63 #include <linux/firmware.h>
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"
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,
78 /* Default NVM size to read */
79 #define IWL_NVM_DEFAULT_CHUNK_SIZE (2*1024)
80 #define IWL_MAX_NVM_SECTION_SIZE 7000
82 #define NVM_WRITE_OPCODE 1
83 #define NVM_READ_OPCODE 0
86 * prepare the NVM host command w/ the pointers to the nvm buffer
87 * and send it to fw
89 static int iwl_nvm_write_chunk(struct iwl_mvm *mvm, u16 section,
90 u16 offset, u16 length, const u8 *data)
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,
98 struct iwl_host_cmd cmd = {
99 .id = NVM_ACCESS_CMD,
100 .len = { sizeof(struct iwl_nvm_access_cmd), length },
101 .flags = CMD_SYNC | CMD_SEND_IN_RFKILL,
102 .data = { &nvm_access_cmd, data },
103 /* data may come from vmalloc, so use _DUP */
104 .dataflags = { 0, IWL_HCMD_DFL_DUP },
107 return iwl_mvm_send_cmd(mvm, &cmd);
110 static int iwl_nvm_read_chunk(struct iwl_mvm *mvm, u16 section,
111 u16 offset, u16 length, u8 *data)
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,
119 struct iwl_nvm_access_resp *nvm_resp;
120 struct iwl_rx_packet *pkt;
121 struct iwl_host_cmd cmd = {
122 .id = NVM_ACCESS_CMD,
123 .flags = CMD_SYNC | CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
124 .data = { &nvm_access_cmd, },
126 int ret, bytes_read, offset_read;
127 u8 *resp_data;
129 cmd.len[0] = sizeof(struct iwl_nvm_access_cmd);
131 ret = iwl_mvm_send_cmd(mvm, &cmd);
132 if (ret)
133 return ret;
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;
143 /* Extract NVM response */
144 nvm_resp = (void *)pkt->data;
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;
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;
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;
164 /* Write data to NVM */
165 memcpy(data + offset, resp_data, bytes_read);
166 ret = bytes_read;
168 exit:
169 iwl_free_resp(&cmd);
170 return ret;
173 static int iwl_nvm_write_section(struct iwl_mvm *mvm, u16 section,
174 const u8 *data, u16 length)
176 int offset = 0;
178 /* copy data in chunks of 2k (and remainder if any) */
180 while (offset < length) {
181 int chunk_size, ret;
183 chunk_size = min(IWL_NVM_DEFAULT_CHUNK_SIZE,
184 length - offset);
186 ret = iwl_nvm_write_chunk(mvm, section, offset,
187 chunk_size, data + offset);
188 if (ret < 0)
189 return ret;
191 offset += chunk_size;
194 return 0;
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.
207 static int iwl_nvm_read_section(struct iwl_mvm *mvm, u16 section,
208 u8 *data)
210 u16 length, offset = 0;
211 int ret;
213 /* Set nvm section read length */
214 length = IWL_NVM_DEFAULT_CHUNK_SIZE;
216 ret = length;
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;
227 offset += ret;
230 IWL_DEBUG_EEPROM(mvm->trans->dev,
231 "NVM section %d read completed\n", section);
232 return offset;
235 static struct iwl_nvm_data *
236 iwl_parse_nvm_sections(struct iwl_mvm *mvm)
238 struct iwl_nvm_section *sections = mvm->nvm_sections;
239 const __le16 *hw, *sw, *calib;
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;
248 if (WARN_ON(!mvm->cfg))
249 return NULL;
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;
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));
259 #define MAX_NVM_FILE_LEN 16384
262 * Reads external NVM from a file into mvm->nvm_sections
264 * HOW TO CREATE THE NVM FILE FORMAT:
265 * ------------------------------
266 * 1. create hex file, format:
267 * 3800 -> header
268 * 0000 -> header
269 * 5a40 -> data
271 * rev - 6 bit (word1)
272 * len - 10 bit (word1)
273 * id - 4 bit (word2)
274 * rsv - 12 bit (word2)
276 * 2. flip 8bits with 8 bits per line to get the right NVM file format
278 * 3. create binary file from the hex file
280 * 4. save as "iNVM_xxx.bin" under /lib/firmware
282 static int iwl_mvm_read_external_nvm(struct iwl_mvm *mvm)
284 int ret, section_size;
285 u16 section_id;
286 const struct firmware *fw_entry;
287 const struct {
288 __le16 word1;
289 __le16 word2;
290 u8 data[];
291 } *file_sec;
292 const u8 *eof, *temp;
294 #define NVM_WORD1_LEN(x) (8 * (x & 0x03FF))
295 #define NVM_WORD2_ID(x) (x >> 12)
297 IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from external NVM\n");
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.
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;
313 IWL_INFO(mvm, "Loaded NVM file %s (%zu bytes)\n",
314 iwlwifi_mod_params.nvm_file, fw_entry->size);
316 if (fw_entry->size < sizeof(*file_sec)) {
317 IWL_ERR(mvm, "NVM file too small\n");
318 ret = -EINVAL;
319 goto out;
322 if (fw_entry->size > MAX_NVM_FILE_LEN) {
323 IWL_ERR(mvm, "NVM file too large\n");
324 ret = -EINVAL;
325 goto out;
328 eof = fw_entry->data + fw_entry->size;
330 file_sec = (void *)fw_entry->data;
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;
340 /* check for EOF marker */
341 if (!file_sec->word1 && !file_sec->word2) {
342 ret = 0;
343 break;
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));
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;
356 if (!section_size) {
357 IWL_ERR(mvm, "ERROR - section empty\n");
358 ret = -EINVAL;
359 break;
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;
370 if (WARN(section_id >= NVM_NUM_OF_SECTIONS,
371 "Invalid NVM section ID %d\n", section_id)) {
372 ret = -EINVAL;
373 break;
376 temp = kmemdup(file_sec->data, section_size, GFP_KERNEL);
377 if (!temp) {
378 ret = -ENOMEM;
379 break;
381 mvm->nvm_sections[section_id].data = temp;
382 mvm->nvm_sections[section_id].length = section_size;
384 /* advance to the next section */
385 file_sec = (void *)(file_sec->data + section_size);
387 out:
388 release_firmware(fw_entry);
389 return ret;
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)
395 int i, ret = 0;
396 struct iwl_nvm_section *sections = mvm->nvm_sections;
398 IWL_DEBUG_EEPROM(mvm->trans->dev, "'Write to NVM\n");
400 for (i = 0; i < ARRAY_SIZE(mvm->nvm_sections); i++) {
401 if (!mvm->nvm_sections[i].data || !mvm->nvm_sections[i].length)
402 continue;
403 ret = iwl_nvm_write_section(mvm, i, sections[i].data,
404 sections[i].length);
405 if (ret < 0) {
406 IWL_ERR(mvm, "iwl_mvm_send_cmd failed: %d\n", ret);
407 break;
410 return ret;
413 int iwl_nvm_init(struct iwl_mvm *mvm)
415 int ret, i, section;
416 u8 *nvm_buffer, *temp;
418 /* load external NVM if configured */
419 if (iwlwifi_mod_params.nvm_file) {
420 /* move to External NVM flow */
421 ret = iwl_mvm_read_external_nvm(mvm);
422 if (ret)
423 return ret;
424 } else {
425 /* Read From FW NVM */
426 IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from NVM\n");
428 /* TODO: find correct NVM max size for a section */
429 nvm_buffer = kmalloc(mvm->cfg->base_params->eeprom_size,
430 GFP_KERNEL);
431 if (!nvm_buffer)
432 return -ENOMEM;
433 for (i = 0; i < ARRAY_SIZE(nvm_to_read); i++) {
434 section = nvm_to_read[i];
435 /* we override the constness for initial read */
436 ret = iwl_nvm_read_section(mvm, section, nvm_buffer);
437 if (ret < 0)
438 break;
439 temp = kmemdup(nvm_buffer, ret, GFP_KERNEL);
440 if (!temp) {
441 ret = -ENOMEM;
442 break;
444 mvm->nvm_sections[section].data = temp;
445 mvm->nvm_sections[section].length = ret;
447 #ifdef CONFIG_IWLWIFI_DEBUGFS
448 switch (section) {
449 case NVM_SECTION_TYPE_HW:
450 mvm->nvm_hw_blob.data = temp;
451 mvm->nvm_hw_blob.size = ret;
452 break;
453 case NVM_SECTION_TYPE_SW:
454 mvm->nvm_sw_blob.data = temp;
455 mvm->nvm_sw_blob.size = ret;
456 break;
457 case NVM_SECTION_TYPE_CALIBRATION:
458 mvm->nvm_calib_blob.data = temp;
459 mvm->nvm_calib_blob.size = ret;
460 break;
461 case NVM_SECTION_TYPE_PRODUCTION:
462 mvm->nvm_prod_blob.data = temp;
463 mvm->nvm_prod_blob.size = ret;
464 break;
465 default:
466 WARN(1, "section: %d", section);
468 #endif
470 kfree(nvm_buffer);
471 if (ret < 0)
472 return ret;
475 mvm->nvm_data = iwl_parse_nvm_sections(mvm);
476 if (!mvm->nvm_data)
477 return -ENODATA;
479 return 0;