iwlwifi: fix initialisation while RF-kill is asserted
[deliverable/linux.git] / drivers / net / wireless / iwlwifi / mvm / nvm.c
CommitLineData
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 *
8 * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
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 *
33 * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
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 */
71static 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
EH
79#define IWL_NVM_DEFAULT_CHUNK_SIZE (2*1024)
80#define IWL_MAX_NVM_SECTION_SIZE 6000
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 */
89static 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
110static 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
168exit:
169 iwl_free_resp(&cmd);
170 return ret;
171}
172
1214755c
EH
173static 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 */
207static 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
235static struct iwl_nvm_data *
236iwl_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;
254 return iwl_parse_nvm_data(mvm->trans->dev, mvm->cfg, hw, sw, calib);
255}
256
1214755c
EH
257#define MAX_NVM_FILE_LEN 16384
258
259/*
260 * HOW TO CREATE THE NVM FILE FORMAT:
261 * ------------------------------
262 * 1. create hex file, format:
263 * 3800 -> header
264 * 0000 -> header
265 * 5a40 -> data
266 *
267 * rev - 6 bit (word1)
268 * len - 10 bit (word1)
269 * id - 4 bit (word2)
270 * rsv - 12 bit (word2)
271 *
272 * 2. flip 8bits with 8 bits per line to get the right NVM file format
273 *
274 * 3. create binary file from the hex file
275 *
276 * 4. save as "iNVM_xxx.bin" under /lib/firmware
277 */
278static int iwl_mvm_load_external_nvm(struct iwl_mvm *mvm)
279{
280 int ret, section_id, section_size;
281 const struct firmware *fw_entry;
282 const struct {
283 __le16 word1;
284 __le16 word2;
285 u8 data[];
286 } *file_sec;
287 const u8 *eof;
288
289#define NVM_WORD1_LEN(x) (8 * (x & 0x03FF))
290#define NVM_WORD2_ID(x) (x >> 12)
291
292 /*
293 * Obtain NVM image via request_firmware. Since we already used
294 * request_firmware_nowait() for the firmware binary load and only
295 * get here after that we assume the NVM request can be satisfied
296 * synchronously.
297 */
298 ret = request_firmware(&fw_entry, iwlwifi_mod_params.nvm_file,
299 mvm->trans->dev);
300 if (ret) {
301 IWL_ERR(mvm, "ERROR: %s isn't available %d\n",
302 iwlwifi_mod_params.nvm_file, ret);
303 return ret;
304 }
305
306 IWL_INFO(mvm, "Loaded NVM file %s (%zu bytes)\n",
307 iwlwifi_mod_params.nvm_file, fw_entry->size);
308
309 if (fw_entry->size < sizeof(*file_sec)) {
310 IWL_ERR(mvm, "NVM file too small\n");
311 ret = -EINVAL;
312 goto out;
313 }
314
315 if (fw_entry->size > MAX_NVM_FILE_LEN) {
316 IWL_ERR(mvm, "NVM file too large\n");
317 ret = -EINVAL;
318 goto out;
319 }
320
321 eof = fw_entry->data + fw_entry->size;
322
323 file_sec = (void *)fw_entry->data;
324
325 while (true) {
326 if (file_sec->data > eof) {
327 IWL_ERR(mvm,
328 "ERROR - NVM file too short for section header\n");
329 ret = -EINVAL;
330 break;
331 }
332
333 /* check for EOF marker */
334 if (!file_sec->word1 && !file_sec->word2) {
335 ret = 0;
336 break;
337 }
338
339 section_size = 2 * NVM_WORD1_LEN(le16_to_cpu(file_sec->word1));
340 section_id = NVM_WORD2_ID(le16_to_cpu(file_sec->word2));
341
342 if (section_size > IWL_MAX_NVM_SECTION_SIZE) {
343 IWL_ERR(mvm, "ERROR - section too large (%d)\n",
344 section_size);
345 ret = -EINVAL;
346 break;
347 }
348
349 if (!section_size) {
350 IWL_ERR(mvm, "ERROR - section empty\n");
351 ret = -EINVAL;
352 break;
353 }
354
355 if (file_sec->data + section_size > eof) {
356 IWL_ERR(mvm,
357 "ERROR - NVM file too short for section (%d bytes)\n",
358 section_size);
359 ret = -EINVAL;
360 break;
361 }
362
363 ret = iwl_nvm_write_section(mvm, section_id, file_sec->data,
364 section_size);
365 if (ret < 0) {
366 IWL_ERR(mvm, "iwl_mvm_send_cmd failed: %d\n", ret);
367 break;
368 }
369
370 /* advance to the next section */
371 file_sec = (void *)(file_sec->data + section_size);
372 }
373out:
374 release_firmware(fw_entry);
375 return ret;
376}
377
8ca151b5
JB
378int iwl_nvm_init(struct iwl_mvm *mvm)
379{
380 int ret, i, section;
381 u8 *nvm_buffer, *temp;
382
1214755c
EH
383 /* load external NVM if configured */
384 if (iwlwifi_mod_params.nvm_file) {
385 /* move to External NVM flow */
386 ret = iwl_mvm_load_external_nvm(mvm);
387 if (ret)
388 return ret;
389 }
390
391 /* Read From FW NVM */
392 IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from NVM\n");
393
b9545b48
EG
394 /* TODO: find correct NVM max size for a section */
395 nvm_buffer = kmalloc(mvm->cfg->base_params->eeprom_size,
396 GFP_KERNEL);
397 if (!nvm_buffer)
398 return -ENOMEM;
399 for (i = 0; i < ARRAY_SIZE(nvm_to_read); i++) {
400 section = nvm_to_read[i];
401 /* we override the constness for initial read */
402 ret = iwl_nvm_read_section(mvm, section, nvm_buffer);
8ca151b5 403 if (ret < 0)
b9545b48
EG
404 break;
405 temp = kmemdup(nvm_buffer, ret, GFP_KERNEL);
406 if (!temp) {
407 ret = -ENOMEM;
408 break;
8ca151b5 409 }
b9545b48
EG
410 mvm->nvm_sections[section].data = temp;
411 mvm->nvm_sections[section].length = ret;
8ca151b5 412 }
b9545b48
EG
413 kfree(nvm_buffer);
414 if (ret < 0)
415 return ret;
8ca151b5 416
b9545b48 417 mvm->nvm_data = iwl_parse_nvm_sections(mvm);
82598b4f
JB
418 if (!mvm->nvm_data)
419 return -ENODATA;
8ca151b5 420
82598b4f 421 return 0;
8ca151b5 422}
This page took 0.072189 seconds and 5 git commands to generate.