Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 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 | * |
Emmanuel Grumbach | 51368bf | 2013-12-30 13:15:54 +0200 | [diff] [blame] | 8 | * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved. |
Johannes Berg | 8b4139d | 2014-07-24 14:05:26 +0200 | [diff] [blame^] | 9 | * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 10 | * |
| 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 Grumbach | 410dc5a | 2013-02-18 09:22:28 +0200 | [diff] [blame] | 26 | * in the file called COPYING. |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 27 | * |
| 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 Grumbach | 51368bf | 2013-12-30 13:15:54 +0200 | [diff] [blame] | 34 | * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved. |
Johannes Berg | 8b4139d | 2014-07-24 14:05:26 +0200 | [diff] [blame^] | 35 | * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 36 | * 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 Harary | 1214755 | 2013-05-09 08:07:59 +0300 | [diff] [blame] | 65 | #include <linux/firmware.h> |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 66 | #include "iwl-trans.h" |
| 67 | #include "mvm.h" |
| 68 | #include "iwl-eeprom-parse.h" |
| 69 | #include "iwl-eeprom-read.h" |
| 70 | #include "iwl-nvm-parse.h" |
| 71 | |
Dor Shaish | 1fd4afe | 2013-02-27 10:18:07 +0200 | [diff] [blame] | 72 | /* Default NVM size to read */ |
Eran Harary | 1214755 | 2013-05-09 08:07:59 +0300 | [diff] [blame] | 73 | #define IWL_NVM_DEFAULT_CHUNK_SIZE (2*1024) |
Liad Kaufman | f251c07 | 2014-04-28 14:42:04 +0300 | [diff] [blame] | 74 | #define IWL_MAX_NVM_SECTION_SIZE 0x1b58 |
| 75 | #define IWL_MAX_NVM_8000A_SECTION_SIZE 0xffc |
| 76 | #define IWL_MAX_NVM_8000B_SECTION_SIZE 0x1ffc |
Dor Shaish | 1fd4afe | 2013-02-27 10:18:07 +0200 | [diff] [blame] | 77 | |
Eran Harary | 1214755 | 2013-05-09 08:07:59 +0300 | [diff] [blame] | 78 | #define NVM_WRITE_OPCODE 1 |
| 79 | #define NVM_READ_OPCODE 0 |
| 80 | |
Eran Harary | d6aeb35 | 2014-05-11 09:44:17 +0300 | [diff] [blame] | 81 | /* load nvm chunk response */ |
| 82 | enum { |
| 83 | READ_NVM_CHUNK_SUCCEED = 0, |
| 84 | READ_NVM_CHUNK_NOT_VALID_ADDRESS = 1 |
| 85 | }; |
| 86 | |
Eran Harary | 1214755 | 2013-05-09 08:07:59 +0300 | [diff] [blame] | 87 | /* |
| 88 | * prepare the NVM host command w/ the pointers to the nvm buffer |
| 89 | * and send it to fw |
| 90 | */ |
| 91 | static int iwl_nvm_write_chunk(struct iwl_mvm *mvm, u16 section, |
| 92 | u16 offset, u16 length, const u8 *data) |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 93 | { |
Eran Harary | 1214755 | 2013-05-09 08:07:59 +0300 | [diff] [blame] | 94 | struct iwl_nvm_access_cmd nvm_access_cmd = { |
| 95 | .offset = cpu_to_le16(offset), |
| 96 | .length = cpu_to_le16(length), |
| 97 | .type = cpu_to_le16(section), |
| 98 | .op_code = NVM_WRITE_OPCODE, |
| 99 | }; |
| 100 | struct iwl_host_cmd cmd = { |
| 101 | .id = NVM_ACCESS_CMD, |
| 102 | .len = { sizeof(struct iwl_nvm_access_cmd), length }, |
Emmanuel Grumbach | a102292 | 2014-05-12 11:36:41 +0300 | [diff] [blame] | 103 | .flags = CMD_SEND_IN_RFKILL, |
Eran Harary | 1214755 | 2013-05-09 08:07:59 +0300 | [diff] [blame] | 104 | .data = { &nvm_access_cmd, data }, |
| 105 | /* data may come from vmalloc, so use _DUP */ |
| 106 | .dataflags = { 0, IWL_HCMD_DFL_DUP }, |
| 107 | }; |
| 108 | |
| 109 | return iwl_mvm_send_cmd(mvm, &cmd); |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static int iwl_nvm_read_chunk(struct iwl_mvm *mvm, u16 section, |
| 113 | u16 offset, u16 length, u8 *data) |
| 114 | { |
Eran Harary | 1214755 | 2013-05-09 08:07:59 +0300 | [diff] [blame] | 115 | struct iwl_nvm_access_cmd nvm_access_cmd = { |
| 116 | .offset = cpu_to_le16(offset), |
| 117 | .length = cpu_to_le16(length), |
| 118 | .type = cpu_to_le16(section), |
| 119 | .op_code = NVM_READ_OPCODE, |
| 120 | }; |
Emmanuel Grumbach | b9545b4 | 2013-03-06 11:34:44 +0200 | [diff] [blame] | 121 | struct iwl_nvm_access_resp *nvm_resp; |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 122 | struct iwl_rx_packet *pkt; |
| 123 | struct iwl_host_cmd cmd = { |
| 124 | .id = NVM_ACCESS_CMD, |
Emmanuel Grumbach | a102292 | 2014-05-12 11:36:41 +0300 | [diff] [blame] | 125 | .flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL, |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 126 | .data = { &nvm_access_cmd, }, |
| 127 | }; |
| 128 | int ret, bytes_read, offset_read; |
| 129 | u8 *resp_data; |
| 130 | |
Emmanuel Grumbach | b9545b4 | 2013-03-06 11:34:44 +0200 | [diff] [blame] | 131 | cmd.len[0] = sizeof(struct iwl_nvm_access_cmd); |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 132 | |
| 133 | ret = iwl_mvm_send_cmd(mvm, &cmd); |
| 134 | if (ret) |
| 135 | return ret; |
| 136 | |
| 137 | pkt = cmd.resp_pkt; |
| 138 | if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) { |
| 139 | IWL_ERR(mvm, "Bad return from NVM_ACCES_COMMAND (0x%08X)\n", |
| 140 | pkt->hdr.flags); |
| 141 | ret = -EIO; |
| 142 | goto exit; |
| 143 | } |
| 144 | |
| 145 | /* Extract NVM response */ |
| 146 | nvm_resp = (void *)pkt->data; |
Emmanuel Grumbach | b9545b4 | 2013-03-06 11:34:44 +0200 | [diff] [blame] | 147 | ret = le16_to_cpu(nvm_resp->status); |
| 148 | bytes_read = le16_to_cpu(nvm_resp->length); |
| 149 | offset_read = le16_to_cpu(nvm_resp->offset); |
| 150 | resp_data = nvm_resp->data; |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 151 | if (ret) { |
Eran Harary | d6aeb35 | 2014-05-11 09:44:17 +0300 | [diff] [blame] | 152 | if ((offset != 0) && |
| 153 | (ret == READ_NVM_CHUNK_NOT_VALID_ADDRESS)) { |
| 154 | /* |
| 155 | * meaning of NOT_VALID_ADDRESS: |
| 156 | * driver try to read chunk from address that is |
| 157 | * multiple of 2K and got an error since addr is empty. |
| 158 | * meaning of (offset != 0): driver already |
| 159 | * read valid data from another chunk so this case |
| 160 | * is not an error. |
| 161 | */ |
| 162 | IWL_DEBUG_EEPROM(mvm->trans->dev, |
| 163 | "NVM access command failed on offset 0x%x since that section size is multiple 2K\n", |
| 164 | offset); |
| 165 | ret = 0; |
| 166 | } else { |
| 167 | IWL_DEBUG_EEPROM(mvm->trans->dev, |
| 168 | "NVM access command failed with status %d (device: %s)\n", |
| 169 | ret, mvm->cfg->name); |
| 170 | ret = -EIO; |
| 171 | } |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 172 | goto exit; |
| 173 | } |
| 174 | |
| 175 | if (offset_read != offset) { |
| 176 | IWL_ERR(mvm, "NVM ACCESS response with invalid offset %d\n", |
| 177 | offset_read); |
| 178 | ret = -EINVAL; |
| 179 | goto exit; |
| 180 | } |
| 181 | |
| 182 | /* Write data to NVM */ |
| 183 | memcpy(data + offset, resp_data, bytes_read); |
| 184 | ret = bytes_read; |
| 185 | |
| 186 | exit: |
| 187 | iwl_free_resp(&cmd); |
| 188 | return ret; |
| 189 | } |
| 190 | |
Eran Harary | 1214755 | 2013-05-09 08:07:59 +0300 | [diff] [blame] | 191 | static int iwl_nvm_write_section(struct iwl_mvm *mvm, u16 section, |
| 192 | const u8 *data, u16 length) |
| 193 | { |
| 194 | int offset = 0; |
| 195 | |
| 196 | /* copy data in chunks of 2k (and remainder if any) */ |
| 197 | |
| 198 | while (offset < length) { |
| 199 | int chunk_size, ret; |
| 200 | |
| 201 | chunk_size = min(IWL_NVM_DEFAULT_CHUNK_SIZE, |
| 202 | length - offset); |
| 203 | |
| 204 | ret = iwl_nvm_write_chunk(mvm, section, offset, |
| 205 | chunk_size, data + offset); |
| 206 | if (ret < 0) |
| 207 | return ret; |
| 208 | |
| 209 | offset += chunk_size; |
| 210 | } |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 215 | /* |
| 216 | * Reads an NVM section completely. |
| 217 | * NICs prior to 7000 family doesn't have a real NVM, but just read |
| 218 | * section 0 which is the EEPROM. Because the EEPROM reading is unlimited |
| 219 | * by uCode, we need to manually check in this case that we don't |
| 220 | * overflow and try to read more than the EEPROM size. |
| 221 | * For 7000 family NICs, we supply the maximal size we can read, and |
| 222 | * the uCode fills the response with as much data as we can, |
| 223 | * without overflowing, so no check is needed. |
| 224 | */ |
| 225 | static int iwl_nvm_read_section(struct iwl_mvm *mvm, u16 section, |
Liad Kaufman | 5daddc9 | 2014-05-21 14:37:00 +0300 | [diff] [blame] | 226 | u8 *data, u32 size_read) |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 227 | { |
| 228 | u16 length, offset = 0; |
| 229 | int ret; |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 230 | |
Dor Shaish | 1fd4afe | 2013-02-27 10:18:07 +0200 | [diff] [blame] | 231 | /* Set nvm section read length */ |
| 232 | length = IWL_NVM_DEFAULT_CHUNK_SIZE; |
| 233 | |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 234 | ret = length; |
| 235 | |
| 236 | /* Read the NVM until exhausted (reading less than requested) */ |
| 237 | while (ret == length) { |
Liad Kaufman | 5daddc9 | 2014-05-21 14:37:00 +0300 | [diff] [blame] | 238 | /* Check no memory assumptions fail and cause an overflow */ |
| 239 | if ((size_read + offset + length) > |
| 240 | mvm->cfg->base_params->eeprom_size) { |
| 241 | IWL_ERR(mvm, "EEPROM size is too small for NVM\n"); |
| 242 | return -ENOBUFS; |
| 243 | } |
| 244 | |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 245 | ret = iwl_nvm_read_chunk(mvm, section, offset, length, data); |
| 246 | if (ret < 0) { |
Eran Harary | d6aeb35 | 2014-05-11 09:44:17 +0300 | [diff] [blame] | 247 | IWL_DEBUG_EEPROM(mvm->trans->dev, |
| 248 | "Cannot read NVM from section %d offset %d, length %d\n", |
| 249 | section, offset, length); |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 250 | return ret; |
| 251 | } |
| 252 | offset += ret; |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 253 | } |
| 254 | |
Johannes Berg | 07fd7d2 | 2013-05-15 14:38:25 +0200 | [diff] [blame] | 255 | IWL_DEBUG_EEPROM(mvm->trans->dev, |
| 256 | "NVM section %d read completed\n", section); |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 257 | return offset; |
| 258 | } |
| 259 | |
| 260 | static struct iwl_nvm_data * |
| 261 | iwl_parse_nvm_sections(struct iwl_mvm *mvm) |
| 262 | { |
| 263 | struct iwl_nvm_section *sections = mvm->nvm_sections; |
Eran Harary | 77db0a3 | 2014-02-04 14:21:38 +0200 | [diff] [blame] | 264 | const __le16 *hw, *sw, *calib, *regulatory, *mac_override; |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 265 | |
| 266 | /* Checking for required sections */ |
Eran Harary | 77db0a3 | 2014-02-04 14:21:38 +0200 | [diff] [blame] | 267 | if (mvm->trans->cfg->device_family != IWL_DEVICE_FAMILY_8000) { |
| 268 | if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data || |
| 269 | !mvm->nvm_sections[mvm->cfg->nvm_hw_section_num].data) { |
Eran Harary | abf09c5 | 2014-07-01 17:33:29 +0300 | [diff] [blame] | 270 | IWL_ERR(mvm, "Can't parse empty OTP/NVM sections\n"); |
Eran Harary | 77db0a3 | 2014-02-04 14:21:38 +0200 | [diff] [blame] | 271 | return NULL; |
| 272 | } |
| 273 | } else { |
Eran Harary | 9f32e01 | 2014-04-28 15:22:40 +0300 | [diff] [blame] | 274 | /* SW and REGULATORY sections are mandatory */ |
Eran Harary | 77db0a3 | 2014-02-04 14:21:38 +0200 | [diff] [blame] | 275 | if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data || |
Eran Harary | 77db0a3 | 2014-02-04 14:21:38 +0200 | [diff] [blame] | 276 | !mvm->nvm_sections[NVM_SECTION_TYPE_REGULATORY].data) { |
| 277 | IWL_ERR(mvm, |
Eran Harary | abf09c5 | 2014-07-01 17:33:29 +0300 | [diff] [blame] | 278 | "Can't parse empty family 8000 OTP/NVM sections\n"); |
Eran Harary | 77db0a3 | 2014-02-04 14:21:38 +0200 | [diff] [blame] | 279 | return NULL; |
| 280 | } |
Eran Harary | 9f32e01 | 2014-04-28 15:22:40 +0300 | [diff] [blame] | 281 | /* MAC_OVERRIDE or at least HW section must exist */ |
Eran Harary | bb92692 | 2014-04-30 15:31:59 +0300 | [diff] [blame] | 282 | if (!mvm->nvm_sections[mvm->cfg->nvm_hw_section_num].data && |
Eran Harary | 9f32e01 | 2014-04-28 15:22:40 +0300 | [diff] [blame] | 283 | !mvm->nvm_sections[NVM_SECTION_TYPE_MAC_OVERRIDE].data) { |
| 284 | IWL_ERR(mvm, |
| 285 | "Can't parse mac_address, empty sections\n"); |
| 286 | return NULL; |
| 287 | } |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | if (WARN_ON(!mvm->cfg)) |
| 291 | return NULL; |
| 292 | |
Eran Harary | ae2b21b | 2014-01-09 08:08:24 +0200 | [diff] [blame] | 293 | hw = (const __le16 *)sections[mvm->cfg->nvm_hw_section_num].data; |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 294 | sw = (const __le16 *)sections[NVM_SECTION_TYPE_SW].data; |
| 295 | calib = (const __le16 *)sections[NVM_SECTION_TYPE_CALIBRATION].data; |
Eran Harary | 77db0a3 | 2014-02-04 14:21:38 +0200 | [diff] [blame] | 296 | regulatory = (const __le16 *)sections[NVM_SECTION_TYPE_REGULATORY].data; |
| 297 | mac_override = |
| 298 | (const __le16 *)sections[NVM_SECTION_TYPE_MAC_OVERRIDE].data; |
| 299 | |
Emmanuel Grumbach | 9ce4fa7 | 2013-05-22 13:16:23 +0300 | [diff] [blame] | 300 | return iwl_parse_nvm_data(mvm->trans->dev, mvm->cfg, hw, sw, calib, |
Eran Harary | 77db0a3 | 2014-02-04 14:21:38 +0200 | [diff] [blame] | 301 | regulatory, mac_override, |
Johannes Berg | 4ed735e | 2014-02-12 21:47:44 +0100 | [diff] [blame] | 302 | mvm->fw->valid_tx_ant, |
| 303 | mvm->fw->valid_rx_ant); |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 304 | } |
| 305 | |
Eran Harary | 1214755 | 2013-05-09 08:07:59 +0300 | [diff] [blame] | 306 | #define MAX_NVM_FILE_LEN 16384 |
| 307 | |
| 308 | /* |
Eytan Lifshitz | 81a67e3 | 2013-09-11 12:39:18 +0200 | [diff] [blame] | 309 | * Reads external NVM from a file into mvm->nvm_sections |
| 310 | * |
Eran Harary | 1214755 | 2013-05-09 08:07:59 +0300 | [diff] [blame] | 311 | * HOW TO CREATE THE NVM FILE FORMAT: |
| 312 | * ------------------------------ |
| 313 | * 1. create hex file, format: |
| 314 | * 3800 -> header |
| 315 | * 0000 -> header |
| 316 | * 5a40 -> data |
| 317 | * |
| 318 | * rev - 6 bit (word1) |
| 319 | * len - 10 bit (word1) |
| 320 | * id - 4 bit (word2) |
| 321 | * rsv - 12 bit (word2) |
| 322 | * |
| 323 | * 2. flip 8bits with 8 bits per line to get the right NVM file format |
| 324 | * |
| 325 | * 3. create binary file from the hex file |
| 326 | * |
| 327 | * 4. save as "iNVM_xxx.bin" under /lib/firmware |
| 328 | */ |
Eytan Lifshitz | 81a67e3 | 2013-09-11 12:39:18 +0200 | [diff] [blame] | 329 | static int iwl_mvm_read_external_nvm(struct iwl_mvm *mvm) |
Eran Harary | 1214755 | 2013-05-09 08:07:59 +0300 | [diff] [blame] | 330 | { |
Eytan Lifshitz | 81a67e3 | 2013-09-11 12:39:18 +0200 | [diff] [blame] | 331 | int ret, section_size; |
| 332 | u16 section_id; |
Eran Harary | 1214755 | 2013-05-09 08:07:59 +0300 | [diff] [blame] | 333 | const struct firmware *fw_entry; |
| 334 | const struct { |
| 335 | __le16 word1; |
| 336 | __le16 word2; |
| 337 | u8 data[]; |
| 338 | } *file_sec; |
Eytan Lifshitz | 81a67e3 | 2013-09-11 12:39:18 +0200 | [diff] [blame] | 339 | const u8 *eof, *temp; |
Liad Kaufman | f251c07 | 2014-04-28 14:42:04 +0300 | [diff] [blame] | 340 | int max_section_size; |
Eran Harary | 1214755 | 2013-05-09 08:07:59 +0300 | [diff] [blame] | 341 | |
| 342 | #define NVM_WORD1_LEN(x) (8 * (x & 0x03FF)) |
| 343 | #define NVM_WORD2_ID(x) (x >> 12) |
Eran Harary | 77db0a3 | 2014-02-04 14:21:38 +0200 | [diff] [blame] | 344 | #define NVM_WORD2_LEN_FAMILY_8000(x) (2 * ((x & 0xFF) << 8 | x >> 8)) |
| 345 | #define NVM_WORD1_ID_FAMILY_8000(x) (x >> 4) |
Eran Harary | 1214755 | 2013-05-09 08:07:59 +0300 | [diff] [blame] | 346 | |
Eytan Lifshitz | 81a67e3 | 2013-09-11 12:39:18 +0200 | [diff] [blame] | 347 | IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from external NVM\n"); |
| 348 | |
Liad Kaufman | f251c07 | 2014-04-28 14:42:04 +0300 | [diff] [blame] | 349 | /* Maximal size depends on HW family and step */ |
| 350 | if (mvm->trans->cfg->device_family != IWL_DEVICE_FAMILY_8000) |
| 351 | max_section_size = IWL_MAX_NVM_SECTION_SIZE; |
| 352 | else if ((mvm->trans->hw_rev & 0xc) == 0) /* Family 8000 A-step */ |
| 353 | max_section_size = IWL_MAX_NVM_8000A_SECTION_SIZE; |
| 354 | else /* Family 8000 B-step */ |
| 355 | max_section_size = IWL_MAX_NVM_8000B_SECTION_SIZE; |
| 356 | |
Eran Harary | 1214755 | 2013-05-09 08:07:59 +0300 | [diff] [blame] | 357 | /* |
| 358 | * Obtain NVM image via request_firmware. Since we already used |
| 359 | * request_firmware_nowait() for the firmware binary load and only |
| 360 | * get here after that we assume the NVM request can be satisfied |
| 361 | * synchronously. |
| 362 | */ |
Eran Harary | e02a9d6 | 2014-05-07 12:27:10 +0300 | [diff] [blame] | 363 | ret = request_firmware(&fw_entry, mvm->nvm_file_name, |
Eran Harary | 1214755 | 2013-05-09 08:07:59 +0300 | [diff] [blame] | 364 | mvm->trans->dev); |
| 365 | if (ret) { |
| 366 | IWL_ERR(mvm, "ERROR: %s isn't available %d\n", |
Eran Harary | e02a9d6 | 2014-05-07 12:27:10 +0300 | [diff] [blame] | 367 | mvm->nvm_file_name, ret); |
Eran Harary | 1214755 | 2013-05-09 08:07:59 +0300 | [diff] [blame] | 368 | return ret; |
| 369 | } |
| 370 | |
| 371 | IWL_INFO(mvm, "Loaded NVM file %s (%zu bytes)\n", |
Eran Harary | e02a9d6 | 2014-05-07 12:27:10 +0300 | [diff] [blame] | 372 | mvm->nvm_file_name, fw_entry->size); |
Eran Harary | 1214755 | 2013-05-09 08:07:59 +0300 | [diff] [blame] | 373 | |
| 374 | if (fw_entry->size < sizeof(*file_sec)) { |
| 375 | IWL_ERR(mvm, "NVM file too small\n"); |
| 376 | ret = -EINVAL; |
| 377 | goto out; |
| 378 | } |
| 379 | |
| 380 | if (fw_entry->size > MAX_NVM_FILE_LEN) { |
| 381 | IWL_ERR(mvm, "NVM file too large\n"); |
| 382 | ret = -EINVAL; |
| 383 | goto out; |
| 384 | } |
| 385 | |
| 386 | eof = fw_entry->data + fw_entry->size; |
| 387 | |
| 388 | file_sec = (void *)fw_entry->data; |
| 389 | |
| 390 | while (true) { |
| 391 | if (file_sec->data > eof) { |
| 392 | IWL_ERR(mvm, |
| 393 | "ERROR - NVM file too short for section header\n"); |
| 394 | ret = -EINVAL; |
| 395 | break; |
| 396 | } |
| 397 | |
| 398 | /* check for EOF marker */ |
| 399 | if (!file_sec->word1 && !file_sec->word2) { |
| 400 | ret = 0; |
| 401 | break; |
| 402 | } |
| 403 | |
Eran Harary | 77db0a3 | 2014-02-04 14:21:38 +0200 | [diff] [blame] | 404 | if (mvm->trans->cfg->device_family != IWL_DEVICE_FAMILY_8000) { |
| 405 | section_size = |
| 406 | 2 * NVM_WORD1_LEN(le16_to_cpu(file_sec->word1)); |
| 407 | section_id = NVM_WORD2_ID(le16_to_cpu(file_sec->word2)); |
| 408 | } else { |
| 409 | section_size = 2 * NVM_WORD2_LEN_FAMILY_8000( |
| 410 | le16_to_cpu(file_sec->word2)); |
| 411 | section_id = NVM_WORD1_ID_FAMILY_8000( |
| 412 | le16_to_cpu(file_sec->word1)); |
| 413 | } |
Eran Harary | 1214755 | 2013-05-09 08:07:59 +0300 | [diff] [blame] | 414 | |
Liad Kaufman | f251c07 | 2014-04-28 14:42:04 +0300 | [diff] [blame] | 415 | if (section_size > max_section_size) { |
Eran Harary | 1214755 | 2013-05-09 08:07:59 +0300 | [diff] [blame] | 416 | IWL_ERR(mvm, "ERROR - section too large (%d)\n", |
| 417 | section_size); |
| 418 | ret = -EINVAL; |
| 419 | break; |
| 420 | } |
| 421 | |
| 422 | if (!section_size) { |
| 423 | IWL_ERR(mvm, "ERROR - section empty\n"); |
| 424 | ret = -EINVAL; |
| 425 | break; |
| 426 | } |
| 427 | |
| 428 | if (file_sec->data + section_size > eof) { |
| 429 | IWL_ERR(mvm, |
| 430 | "ERROR - NVM file too short for section (%d bytes)\n", |
| 431 | section_size); |
| 432 | ret = -EINVAL; |
| 433 | break; |
| 434 | } |
| 435 | |
Eran Harary | ae2b21b | 2014-01-09 08:08:24 +0200 | [diff] [blame] | 436 | if (WARN(section_id >= NVM_MAX_NUM_SECTIONS, |
Eytan Lifshitz | a4a1247 | 2013-12-18 23:05:06 +0200 | [diff] [blame] | 437 | "Invalid NVM section ID %d\n", section_id)) { |
| 438 | ret = -EINVAL; |
| 439 | break; |
| 440 | } |
| 441 | |
Eytan Lifshitz | 81a67e3 | 2013-09-11 12:39:18 +0200 | [diff] [blame] | 442 | temp = kmemdup(file_sec->data, section_size, GFP_KERNEL); |
| 443 | if (!temp) { |
| 444 | ret = -ENOMEM; |
Eran Harary | 1214755 | 2013-05-09 08:07:59 +0300 | [diff] [blame] | 445 | break; |
| 446 | } |
Eytan Lifshitz | 81a67e3 | 2013-09-11 12:39:18 +0200 | [diff] [blame] | 447 | mvm->nvm_sections[section_id].data = temp; |
| 448 | mvm->nvm_sections[section_id].length = section_size; |
Eran Harary | 1214755 | 2013-05-09 08:07:59 +0300 | [diff] [blame] | 449 | |
| 450 | /* advance to the next section */ |
| 451 | file_sec = (void *)(file_sec->data + section_size); |
| 452 | } |
| 453 | out: |
| 454 | release_firmware(fw_entry); |
| 455 | return ret; |
| 456 | } |
| 457 | |
Eytan Lifshitz | 81a67e3 | 2013-09-11 12:39:18 +0200 | [diff] [blame] | 458 | /* Loads the NVM data stored in mvm->nvm_sections into the NIC */ |
| 459 | int iwl_mvm_load_nvm_to_nic(struct iwl_mvm *mvm) |
| 460 | { |
Eytan Lifshitz | 05159fc | 2014-01-07 12:50:45 +0200 | [diff] [blame] | 461 | int i, ret = 0; |
Eytan Lifshitz | 81a67e3 | 2013-09-11 12:39:18 +0200 | [diff] [blame] | 462 | struct iwl_nvm_section *sections = mvm->nvm_sections; |
| 463 | |
| 464 | IWL_DEBUG_EEPROM(mvm->trans->dev, "'Write to NVM\n"); |
| 465 | |
Emmanuel Grumbach | 099d8f2 | 2014-01-05 15:09:44 +0200 | [diff] [blame] | 466 | for (i = 0; i < ARRAY_SIZE(mvm->nvm_sections); i++) { |
| 467 | if (!mvm->nvm_sections[i].data || !mvm->nvm_sections[i].length) |
| 468 | continue; |
| 469 | ret = iwl_nvm_write_section(mvm, i, sections[i].data, |
| 470 | sections[i].length); |
Eytan Lifshitz | 81a67e3 | 2013-09-11 12:39:18 +0200 | [diff] [blame] | 471 | if (ret < 0) { |
| 472 | IWL_ERR(mvm, "iwl_mvm_send_cmd failed: %d\n", ret); |
| 473 | break; |
| 474 | } |
| 475 | } |
| 476 | return ret; |
| 477 | } |
| 478 | |
Eran Harary | 14b485f | 2014-04-23 10:46:09 +0300 | [diff] [blame] | 479 | int iwl_nvm_init(struct iwl_mvm *mvm, bool read_nvm_from_nic) |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 480 | { |
Eran Harary | d6aeb35 | 2014-05-11 09:44:17 +0300 | [diff] [blame] | 481 | int ret, section; |
Liad Kaufman | 5daddc9 | 2014-05-21 14:37:00 +0300 | [diff] [blame] | 482 | u32 size_read = 0; |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 483 | u8 *nvm_buffer, *temp; |
| 484 | |
Eran Harary | ae2b21b | 2014-01-09 08:08:24 +0200 | [diff] [blame] | 485 | if (WARN_ON_ONCE(mvm->cfg->nvm_hw_section_num >= NVM_MAX_NUM_SECTIONS)) |
| 486 | return -EINVAL; |
| 487 | |
Eran Harary | 26481bf | 2014-03-25 14:14:44 +0200 | [diff] [blame] | 488 | /* load NVM values from nic */ |
Eran Harary | 14b485f | 2014-04-23 10:46:09 +0300 | [diff] [blame] | 489 | if (read_nvm_from_nic) { |
Eytan Lifshitz | 81a67e3 | 2013-09-11 12:39:18 +0200 | [diff] [blame] | 490 | /* Read From FW NVM */ |
| 491 | IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from NVM\n"); |
Eran Harary | 1214755 | 2013-05-09 08:07:59 +0300 | [diff] [blame] | 492 | |
Eytan Lifshitz | 81a67e3 | 2013-09-11 12:39:18 +0200 | [diff] [blame] | 493 | nvm_buffer = kmalloc(mvm->cfg->base_params->eeprom_size, |
| 494 | GFP_KERNEL); |
| 495 | if (!nvm_buffer) |
| 496 | return -ENOMEM; |
Eran Harary | d6aeb35 | 2014-05-11 09:44:17 +0300 | [diff] [blame] | 497 | for (section = 0; section < NVM_MAX_NUM_SECTIONS; section++) { |
Eytan Lifshitz | 81a67e3 | 2013-09-11 12:39:18 +0200 | [diff] [blame] | 498 | /* we override the constness for initial read */ |
Liad Kaufman | 5daddc9 | 2014-05-21 14:37:00 +0300 | [diff] [blame] | 499 | ret = iwl_nvm_read_section(mvm, section, nvm_buffer, |
| 500 | size_read); |
Eytan Lifshitz | 81a67e3 | 2013-09-11 12:39:18 +0200 | [diff] [blame] | 501 | if (ret < 0) |
Eran Harary | d6aeb35 | 2014-05-11 09:44:17 +0300 | [diff] [blame] | 502 | continue; |
Liad Kaufman | 5daddc9 | 2014-05-21 14:37:00 +0300 | [diff] [blame] | 503 | size_read += ret; |
Eytan Lifshitz | 81a67e3 | 2013-09-11 12:39:18 +0200 | [diff] [blame] | 504 | temp = kmemdup(nvm_buffer, ret, GFP_KERNEL); |
| 505 | if (!temp) { |
| 506 | ret = -ENOMEM; |
| 507 | break; |
| 508 | } |
| 509 | mvm->nvm_sections[section].data = temp; |
| 510 | mvm->nvm_sections[section].length = ret; |
Emmanuel Grumbach | 086f736 | 2013-11-18 17:00:03 +0200 | [diff] [blame] | 511 | |
| 512 | #ifdef CONFIG_IWLWIFI_DEBUGFS |
| 513 | switch (section) { |
Emmanuel Grumbach | 086f736 | 2013-11-18 17:00:03 +0200 | [diff] [blame] | 514 | case NVM_SECTION_TYPE_SW: |
| 515 | mvm->nvm_sw_blob.data = temp; |
| 516 | mvm->nvm_sw_blob.size = ret; |
| 517 | break; |
| 518 | case NVM_SECTION_TYPE_CALIBRATION: |
| 519 | mvm->nvm_calib_blob.data = temp; |
| 520 | mvm->nvm_calib_blob.size = ret; |
| 521 | break; |
| 522 | case NVM_SECTION_TYPE_PRODUCTION: |
| 523 | mvm->nvm_prod_blob.data = temp; |
| 524 | mvm->nvm_prod_blob.size = ret; |
| 525 | break; |
| 526 | default: |
Eran Harary | ae2b21b | 2014-01-09 08:08:24 +0200 | [diff] [blame] | 527 | if (section == mvm->cfg->nvm_hw_section_num) { |
| 528 | mvm->nvm_hw_blob.data = temp; |
| 529 | mvm->nvm_hw_blob.size = ret; |
| 530 | break; |
| 531 | } |
Emmanuel Grumbach | 086f736 | 2013-11-18 17:00:03 +0200 | [diff] [blame] | 532 | } |
| 533 | #endif |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 534 | } |
Eran Harary | bdce40f | 2014-07-01 17:31:38 +0300 | [diff] [blame] | 535 | if (!size_read) |
| 536 | IWL_ERR(mvm, "OTP is blank\n"); |
Eytan Lifshitz | 81a67e3 | 2013-09-11 12:39:18 +0200 | [diff] [blame] | 537 | kfree(nvm_buffer); |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 538 | } |
| 539 | |
Eran Harary | 26481bf | 2014-03-25 14:14:44 +0200 | [diff] [blame] | 540 | /* load external NVM if configured */ |
Eran Harary | e02a9d6 | 2014-05-07 12:27:10 +0300 | [diff] [blame] | 541 | if (mvm->nvm_file_name) { |
Eran Harary | 26481bf | 2014-03-25 14:14:44 +0200 | [diff] [blame] | 542 | /* move to External NVM flow */ |
| 543 | ret = iwl_mvm_read_external_nvm(mvm); |
| 544 | if (ret) |
| 545 | return ret; |
| 546 | } |
| 547 | |
| 548 | /* parse the relevant nvm sections */ |
Emmanuel Grumbach | b9545b4 | 2013-03-06 11:34:44 +0200 | [diff] [blame] | 549 | mvm->nvm_data = iwl_parse_nvm_sections(mvm); |
Johannes Berg | 82598b4 | 2013-05-13 21:44:42 +0200 | [diff] [blame] | 550 | if (!mvm->nvm_data) |
| 551 | return -ENODATA; |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 552 | |
Johannes Berg | 82598b4 | 2013-05-13 21:44:42 +0200 | [diff] [blame] | 553 | return 0; |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 554 | } |