1e705c121SKalle Valo /****************************************************************************** 2e705c121SKalle Valo * 3e705c121SKalle Valo * This file is provided under a dual BSD/GPLv2 license. When using or 4e705c121SKalle Valo * redistributing this file, you may do so under either license. 5e705c121SKalle Valo * 6e705c121SKalle Valo * GPL LICENSE SUMMARY 7e705c121SKalle Valo * 8e705c121SKalle Valo * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved. 9e705c121SKalle Valo * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH 106fa52430SMatti Gottlieb * Copyright(c) 2016 Intel Deutschland GmbH 11e705c121SKalle Valo * 12e705c121SKalle Valo * This program is free software; you can redistribute it and/or modify 13e705c121SKalle Valo * it under the terms of version 2 of the GNU General Public License as 14e705c121SKalle Valo * published by the Free Software Foundation. 15e705c121SKalle Valo * 16e705c121SKalle Valo * This program is distributed in the hope that it will be useful, but 17e705c121SKalle Valo * WITHOUT ANY WARRANTY; without even the implied warranty of 18e705c121SKalle Valo * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19e705c121SKalle Valo * General Public License for more details. 20e705c121SKalle Valo * 21e705c121SKalle Valo * You should have received a copy of the GNU General Public License 22e705c121SKalle Valo * along with this program; if not, write to the Free Software 23e705c121SKalle Valo * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, 24e705c121SKalle Valo * USA 25e705c121SKalle Valo * 26e705c121SKalle Valo * The full GNU General Public License is included in this distribution 27e705c121SKalle Valo * in the file called COPYING. 28e705c121SKalle Valo * 29e705c121SKalle Valo * Contact Information: 30cb2f8277SEmmanuel Grumbach * Intel Linux Wireless <linuxwifi@intel.com> 31e705c121SKalle Valo * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 32e705c121SKalle Valo * 33e705c121SKalle Valo * BSD LICENSE 34e705c121SKalle Valo * 35e705c121SKalle Valo * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved. 36e705c121SKalle Valo * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH 376fa52430SMatti Gottlieb * Copyright(c) 2016 Intel Deutschland GmbH 38e705c121SKalle Valo * All rights reserved. 39e705c121SKalle Valo * 40e705c121SKalle Valo * Redistribution and use in source and binary forms, with or without 41e705c121SKalle Valo * modification, are permitted provided that the following conditions 42e705c121SKalle Valo * are met: 43e705c121SKalle Valo * 44e705c121SKalle Valo * * Redistributions of source code must retain the above copyright 45e705c121SKalle Valo * notice, this list of conditions and the following disclaimer. 46e705c121SKalle Valo * * Redistributions in binary form must reproduce the above copyright 47e705c121SKalle Valo * notice, this list of conditions and the following disclaimer in 48e705c121SKalle Valo * the documentation and/or other materials provided with the 49e705c121SKalle Valo * distribution. 50e705c121SKalle Valo * * Neither the name Intel Corporation nor the names of its 51e705c121SKalle Valo * contributors may be used to endorse or promote products derived 52e705c121SKalle Valo * from this software without specific prior written permission. 53e705c121SKalle Valo * 54e705c121SKalle Valo * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 55e705c121SKalle Valo * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 56e705c121SKalle Valo * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 57e705c121SKalle Valo * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 58e705c121SKalle Valo * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 59e705c121SKalle Valo * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 60e705c121SKalle Valo * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 61e705c121SKalle Valo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 62e705c121SKalle Valo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 63e705c121SKalle Valo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 64e705c121SKalle Valo * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 65e705c121SKalle Valo * 66e705c121SKalle Valo *****************************************************************************/ 67e705c121SKalle Valo #include <linux/firmware.h> 68e705c121SKalle Valo #include <linux/rtnetlink.h> 69e705c121SKalle Valo #include <linux/pci.h> 70e705c121SKalle Valo #include <linux/acpi.h> 71e705c121SKalle Valo #include "iwl-trans.h" 72e705c121SKalle Valo #include "iwl-csr.h" 73e705c121SKalle Valo #include "mvm.h" 74e705c121SKalle Valo #include "iwl-eeprom-parse.h" 75e705c121SKalle Valo #include "iwl-eeprom-read.h" 76e705c121SKalle Valo #include "iwl-nvm-parse.h" 77e705c121SKalle Valo #include "iwl-prph.h" 78e705c121SKalle Valo 79e705c121SKalle Valo /* Default NVM size to read */ 80e705c121SKalle Valo #define IWL_NVM_DEFAULT_CHUNK_SIZE (2*1024) 81e705c121SKalle Valo #define IWL_MAX_NVM_SECTION_SIZE 0x1b58 82e705c121SKalle Valo #define IWL_MAX_NVM_8000_SECTION_SIZE 0x1ffc 83e705c121SKalle Valo 84e705c121SKalle Valo #define NVM_WRITE_OPCODE 1 85e705c121SKalle Valo #define NVM_READ_OPCODE 0 86e705c121SKalle Valo 87e705c121SKalle Valo /* load nvm chunk response */ 88e705c121SKalle Valo enum { 89e705c121SKalle Valo READ_NVM_CHUNK_SUCCEED = 0, 90e705c121SKalle Valo READ_NVM_CHUNK_NOT_VALID_ADDRESS = 1 91e705c121SKalle Valo }; 92e705c121SKalle Valo 93e705c121SKalle Valo /* 94e705c121SKalle Valo * prepare the NVM host command w/ the pointers to the nvm buffer 95e705c121SKalle Valo * and send it to fw 96e705c121SKalle Valo */ 97e705c121SKalle Valo static int iwl_nvm_write_chunk(struct iwl_mvm *mvm, u16 section, 98e705c121SKalle Valo u16 offset, u16 length, const u8 *data) 99e705c121SKalle Valo { 100e705c121SKalle Valo struct iwl_nvm_access_cmd nvm_access_cmd = { 101e705c121SKalle Valo .offset = cpu_to_le16(offset), 102e705c121SKalle Valo .length = cpu_to_le16(length), 103e705c121SKalle Valo .type = cpu_to_le16(section), 104e705c121SKalle Valo .op_code = NVM_WRITE_OPCODE, 105e705c121SKalle Valo }; 106e705c121SKalle Valo struct iwl_host_cmd cmd = { 107e705c121SKalle Valo .id = NVM_ACCESS_CMD, 108e705c121SKalle Valo .len = { sizeof(struct iwl_nvm_access_cmd), length }, 1099a57f650SMatti Gottlieb .flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL, 110e705c121SKalle Valo .data = { &nvm_access_cmd, data }, 111e705c121SKalle Valo /* data may come from vmalloc, so use _DUP */ 112e705c121SKalle Valo .dataflags = { 0, IWL_HCMD_DFL_DUP }, 113e705c121SKalle Valo }; 1149a57f650SMatti Gottlieb struct iwl_rx_packet *pkt; 1159a57f650SMatti Gottlieb struct iwl_nvm_access_resp *nvm_resp; 1169a57f650SMatti Gottlieb int ret; 117e705c121SKalle Valo 1189a57f650SMatti Gottlieb ret = iwl_mvm_send_cmd(mvm, &cmd); 1199a57f650SMatti Gottlieb if (ret) 1209a57f650SMatti Gottlieb return ret; 1219a57f650SMatti Gottlieb 1229a57f650SMatti Gottlieb pkt = cmd.resp_pkt; 1239a57f650SMatti Gottlieb if (!pkt) { 1249a57f650SMatti Gottlieb IWL_ERR(mvm, "Error in NVM_ACCESS response\n"); 1259a57f650SMatti Gottlieb return -EINVAL; 1269a57f650SMatti Gottlieb } 1279a57f650SMatti Gottlieb /* Extract & check NVM write response */ 1289a57f650SMatti Gottlieb nvm_resp = (void *)pkt->data; 1299a57f650SMatti Gottlieb if (le16_to_cpu(nvm_resp->status) != READ_NVM_CHUNK_SUCCEED) { 1309a57f650SMatti Gottlieb IWL_ERR(mvm, 1319a57f650SMatti Gottlieb "NVM access write command failed for section %u (status = 0x%x)\n", 1329a57f650SMatti Gottlieb section, le16_to_cpu(nvm_resp->status)); 1339a57f650SMatti Gottlieb ret = -EIO; 1349a57f650SMatti Gottlieb } 1359a57f650SMatti Gottlieb 1369a57f650SMatti Gottlieb iwl_free_resp(&cmd); 1379a57f650SMatti Gottlieb return ret; 138e705c121SKalle Valo } 139e705c121SKalle Valo 140e705c121SKalle Valo static int iwl_nvm_read_chunk(struct iwl_mvm *mvm, u16 section, 141e705c121SKalle Valo u16 offset, u16 length, u8 *data) 142e705c121SKalle Valo { 143e705c121SKalle Valo struct iwl_nvm_access_cmd nvm_access_cmd = { 144e705c121SKalle Valo .offset = cpu_to_le16(offset), 145e705c121SKalle Valo .length = cpu_to_le16(length), 146e705c121SKalle Valo .type = cpu_to_le16(section), 147e705c121SKalle Valo .op_code = NVM_READ_OPCODE, 148e705c121SKalle Valo }; 149e705c121SKalle Valo struct iwl_nvm_access_resp *nvm_resp; 150e705c121SKalle Valo struct iwl_rx_packet *pkt; 151e705c121SKalle Valo struct iwl_host_cmd cmd = { 152e705c121SKalle Valo .id = NVM_ACCESS_CMD, 153e705c121SKalle Valo .flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL, 154e705c121SKalle Valo .data = { &nvm_access_cmd, }, 155e705c121SKalle Valo }; 156e705c121SKalle Valo int ret, bytes_read, offset_read; 157e705c121SKalle Valo u8 *resp_data; 158e705c121SKalle Valo 159e705c121SKalle Valo cmd.len[0] = sizeof(struct iwl_nvm_access_cmd); 160e705c121SKalle Valo 161e705c121SKalle Valo ret = iwl_mvm_send_cmd(mvm, &cmd); 162e705c121SKalle Valo if (ret) 163e705c121SKalle Valo return ret; 164e705c121SKalle Valo 165e705c121SKalle Valo pkt = cmd.resp_pkt; 166e705c121SKalle Valo 167e705c121SKalle Valo /* Extract NVM response */ 168e705c121SKalle Valo nvm_resp = (void *)pkt->data; 169e705c121SKalle Valo ret = le16_to_cpu(nvm_resp->status); 170e705c121SKalle Valo bytes_read = le16_to_cpu(nvm_resp->length); 171e705c121SKalle Valo offset_read = le16_to_cpu(nvm_resp->offset); 172e705c121SKalle Valo resp_data = nvm_resp->data; 173e705c121SKalle Valo if (ret) { 174e705c121SKalle Valo if ((offset != 0) && 175e705c121SKalle Valo (ret == READ_NVM_CHUNK_NOT_VALID_ADDRESS)) { 176e705c121SKalle Valo /* 177e705c121SKalle Valo * meaning of NOT_VALID_ADDRESS: 178e705c121SKalle Valo * driver try to read chunk from address that is 179e705c121SKalle Valo * multiple of 2K and got an error since addr is empty. 180e705c121SKalle Valo * meaning of (offset != 0): driver already 181e705c121SKalle Valo * read valid data from another chunk so this case 182e705c121SKalle Valo * is not an error. 183e705c121SKalle Valo */ 184e705c121SKalle Valo IWL_DEBUG_EEPROM(mvm->trans->dev, 185e705c121SKalle Valo "NVM access command failed on offset 0x%x since that section size is multiple 2K\n", 186e705c121SKalle Valo offset); 187e705c121SKalle Valo ret = 0; 188e705c121SKalle Valo } else { 189e705c121SKalle Valo IWL_DEBUG_EEPROM(mvm->trans->dev, 190e705c121SKalle Valo "NVM access command failed with status %d (device: %s)\n", 191e705c121SKalle Valo ret, mvm->cfg->name); 192e705c121SKalle Valo ret = -EIO; 193e705c121SKalle Valo } 194e705c121SKalle Valo goto exit; 195e705c121SKalle Valo } 196e705c121SKalle Valo 197e705c121SKalle Valo if (offset_read != offset) { 198e705c121SKalle Valo IWL_ERR(mvm, "NVM ACCESS response with invalid offset %d\n", 199e705c121SKalle Valo offset_read); 200e705c121SKalle Valo ret = -EINVAL; 201e705c121SKalle Valo goto exit; 202e705c121SKalle Valo } 203e705c121SKalle Valo 204e705c121SKalle Valo /* Write data to NVM */ 205e705c121SKalle Valo memcpy(data + offset, resp_data, bytes_read); 206e705c121SKalle Valo ret = bytes_read; 207e705c121SKalle Valo 208e705c121SKalle Valo exit: 209e705c121SKalle Valo iwl_free_resp(&cmd); 210e705c121SKalle Valo return ret; 211e705c121SKalle Valo } 212e705c121SKalle Valo 213e705c121SKalle Valo static int iwl_nvm_write_section(struct iwl_mvm *mvm, u16 section, 214e705c121SKalle Valo const u8 *data, u16 length) 215e705c121SKalle Valo { 216e705c121SKalle Valo int offset = 0; 217e705c121SKalle Valo 218e705c121SKalle Valo /* copy data in chunks of 2k (and remainder if any) */ 219e705c121SKalle Valo 220e705c121SKalle Valo while (offset < length) { 221e705c121SKalle Valo int chunk_size, ret; 222e705c121SKalle Valo 223e705c121SKalle Valo chunk_size = min(IWL_NVM_DEFAULT_CHUNK_SIZE, 224e705c121SKalle Valo length - offset); 225e705c121SKalle Valo 226e705c121SKalle Valo ret = iwl_nvm_write_chunk(mvm, section, offset, 227e705c121SKalle Valo chunk_size, data + offset); 228e705c121SKalle Valo if (ret < 0) 229e705c121SKalle Valo return ret; 230e705c121SKalle Valo 231e705c121SKalle Valo offset += chunk_size; 232e705c121SKalle Valo } 233e705c121SKalle Valo 234e705c121SKalle Valo return 0; 235e705c121SKalle Valo } 236e705c121SKalle Valo 2377d162045SJohannes Berg static void iwl_mvm_nvm_fixups(struct iwl_mvm *mvm, unsigned int section, 2387d162045SJohannes Berg u8 *data, unsigned int len) 2397d162045SJohannes Berg { 2407d162045SJohannes Berg #define IWL_4165_DEVICE_ID 0x5501 2417d162045SJohannes Berg #define NVM_SKU_CAP_MIMO_DISABLE BIT(5) 2427d162045SJohannes Berg 2437d162045SJohannes Berg if (section == NVM_SECTION_TYPE_PHY_SKU && 2447d162045SJohannes Berg mvm->trans->hw_id == IWL_4165_DEVICE_ID && data && len >= 5 && 2457d162045SJohannes Berg (data[4] & NVM_SKU_CAP_MIMO_DISABLE)) 2467d162045SJohannes Berg /* OTP 0x52 bug work around: it's a 1x1 device */ 2477d162045SJohannes Berg data[3] = ANT_B | (ANT_B << 4); 2487d162045SJohannes Berg } 2497d162045SJohannes Berg 250e705c121SKalle Valo /* 251e705c121SKalle Valo * Reads an NVM section completely. 252e705c121SKalle Valo * NICs prior to 7000 family doesn't have a real NVM, but just read 253e705c121SKalle Valo * section 0 which is the EEPROM. Because the EEPROM reading is unlimited 254e705c121SKalle Valo * by uCode, we need to manually check in this case that we don't 255e705c121SKalle Valo * overflow and try to read more than the EEPROM size. 256e705c121SKalle Valo * For 7000 family NICs, we supply the maximal size we can read, and 257e705c121SKalle Valo * the uCode fills the response with as much data as we can, 258e705c121SKalle Valo * without overflowing, so no check is needed. 259e705c121SKalle Valo */ 260e705c121SKalle Valo static int iwl_nvm_read_section(struct iwl_mvm *mvm, u16 section, 261e705c121SKalle Valo u8 *data, u32 size_read) 262e705c121SKalle Valo { 263e705c121SKalle Valo u16 length, offset = 0; 264e705c121SKalle Valo int ret; 265e705c121SKalle Valo 266e705c121SKalle Valo /* Set nvm section read length */ 267e705c121SKalle Valo length = IWL_NVM_DEFAULT_CHUNK_SIZE; 268e705c121SKalle Valo 269e705c121SKalle Valo ret = length; 270e705c121SKalle Valo 271e705c121SKalle Valo /* Read the NVM until exhausted (reading less than requested) */ 272e705c121SKalle Valo while (ret == length) { 273e705c121SKalle Valo /* Check no memory assumptions fail and cause an overflow */ 274e705c121SKalle Valo if ((size_read + offset + length) > 275e705c121SKalle Valo mvm->cfg->base_params->eeprom_size) { 276e705c121SKalle Valo IWL_ERR(mvm, "EEPROM size is too small for NVM\n"); 277e705c121SKalle Valo return -ENOBUFS; 278e705c121SKalle Valo } 279e705c121SKalle Valo 280e705c121SKalle Valo ret = iwl_nvm_read_chunk(mvm, section, offset, length, data); 281e705c121SKalle Valo if (ret < 0) { 282e705c121SKalle Valo IWL_DEBUG_EEPROM(mvm->trans->dev, 283e705c121SKalle Valo "Cannot read NVM from section %d offset %d, length %d\n", 284e705c121SKalle Valo section, offset, length); 285e705c121SKalle Valo return ret; 286e705c121SKalle Valo } 287e705c121SKalle Valo offset += ret; 288e705c121SKalle Valo } 289e705c121SKalle Valo 2907d162045SJohannes Berg iwl_mvm_nvm_fixups(mvm, section, data, offset); 2917d162045SJohannes Berg 292e705c121SKalle Valo IWL_DEBUG_EEPROM(mvm->trans->dev, 293e705c121SKalle Valo "NVM section %d read completed\n", section); 294e705c121SKalle Valo return offset; 295e705c121SKalle Valo } 296e705c121SKalle Valo 297e705c121SKalle Valo static struct iwl_nvm_data * 298e705c121SKalle Valo iwl_parse_nvm_sections(struct iwl_mvm *mvm) 299e705c121SKalle Valo { 300e705c121SKalle Valo struct iwl_nvm_section *sections = mvm->nvm_sections; 301e705c121SKalle Valo const __le16 *hw, *sw, *calib, *regulatory, *mac_override, *phy_sku; 302e705c121SKalle Valo bool lar_enabled; 303e705c121SKalle Valo 304e705c121SKalle Valo /* Checking for required sections */ 305e705c121SKalle Valo if (mvm->trans->cfg->device_family != IWL_DEVICE_FAMILY_8000) { 306e705c121SKalle Valo if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data || 307e705c121SKalle Valo !mvm->nvm_sections[mvm->cfg->nvm_hw_section_num].data) { 308e705c121SKalle Valo IWL_ERR(mvm, "Can't parse empty OTP/NVM sections\n"); 309e705c121SKalle Valo return NULL; 310e705c121SKalle Valo } 311e705c121SKalle Valo } else { 312e705c121SKalle Valo /* SW and REGULATORY sections are mandatory */ 313e705c121SKalle Valo if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data || 314e705c121SKalle Valo !mvm->nvm_sections[NVM_SECTION_TYPE_REGULATORY].data) { 315e705c121SKalle Valo IWL_ERR(mvm, 316e705c121SKalle Valo "Can't parse empty family 8000 OTP/NVM sections\n"); 317e705c121SKalle Valo return NULL; 318e705c121SKalle Valo } 319e705c121SKalle Valo /* MAC_OVERRIDE or at least HW section must exist */ 320e705c121SKalle Valo if (!mvm->nvm_sections[mvm->cfg->nvm_hw_section_num].data && 321e705c121SKalle Valo !mvm->nvm_sections[NVM_SECTION_TYPE_MAC_OVERRIDE].data) { 322e705c121SKalle Valo IWL_ERR(mvm, 323e705c121SKalle Valo "Can't parse mac_address, empty sections\n"); 324e705c121SKalle Valo return NULL; 325e705c121SKalle Valo } 326e705c121SKalle Valo 327e705c121SKalle Valo /* PHY_SKU section is mandatory in B0 */ 328e705c121SKalle Valo if (!mvm->nvm_sections[NVM_SECTION_TYPE_PHY_SKU].data) { 329e705c121SKalle Valo IWL_ERR(mvm, 330e705c121SKalle Valo "Can't parse phy_sku in B0, empty sections\n"); 331e705c121SKalle Valo return NULL; 332e705c121SKalle Valo } 333e705c121SKalle Valo } 334e705c121SKalle Valo 335e705c121SKalle Valo if (WARN_ON(!mvm->cfg)) 336e705c121SKalle Valo return NULL; 337e705c121SKalle Valo 338e705c121SKalle Valo hw = (const __le16 *)sections[mvm->cfg->nvm_hw_section_num].data; 339e705c121SKalle Valo sw = (const __le16 *)sections[NVM_SECTION_TYPE_SW].data; 340e705c121SKalle Valo calib = (const __le16 *)sections[NVM_SECTION_TYPE_CALIBRATION].data; 341e705c121SKalle Valo regulatory = (const __le16 *)sections[NVM_SECTION_TYPE_REGULATORY].data; 342e705c121SKalle Valo mac_override = 343e705c121SKalle Valo (const __le16 *)sections[NVM_SECTION_TYPE_MAC_OVERRIDE].data; 344e705c121SKalle Valo phy_sku = (const __le16 *)sections[NVM_SECTION_TYPE_PHY_SKU].data; 345e705c121SKalle Valo 346e705c121SKalle Valo lar_enabled = !iwlwifi_mod_params.lar_disable && 347e705c121SKalle Valo fw_has_capa(&mvm->fw->ucode_capa, 348e705c121SKalle Valo IWL_UCODE_TLV_CAPA_LAR_SUPPORT); 349e705c121SKalle Valo 350*afd5b170SSara Sharon return iwl_parse_nvm_data(mvm->trans, mvm->cfg, hw, sw, calib, 351e705c121SKalle Valo regulatory, mac_override, phy_sku, 352e705c121SKalle Valo mvm->fw->valid_tx_ant, mvm->fw->valid_rx_ant, 353*afd5b170SSara Sharon lar_enabled); 354e705c121SKalle Valo } 355e705c121SKalle Valo 356e705c121SKalle Valo #define MAX_NVM_FILE_LEN 16384 357e705c121SKalle Valo 358e705c121SKalle Valo /* 359e705c121SKalle Valo * Reads external NVM from a file into mvm->nvm_sections 360e705c121SKalle Valo * 361e705c121SKalle Valo * HOW TO CREATE THE NVM FILE FORMAT: 362e705c121SKalle Valo * ------------------------------ 363e705c121SKalle Valo * 1. create hex file, format: 364e705c121SKalle Valo * 3800 -> header 365e705c121SKalle Valo * 0000 -> header 366e705c121SKalle Valo * 5a40 -> data 367e705c121SKalle Valo * 368e705c121SKalle Valo * rev - 6 bit (word1) 369e705c121SKalle Valo * len - 10 bit (word1) 370e705c121SKalle Valo * id - 4 bit (word2) 371e705c121SKalle Valo * rsv - 12 bit (word2) 372e705c121SKalle Valo * 373e705c121SKalle Valo * 2. flip 8bits with 8 bits per line to get the right NVM file format 374e705c121SKalle Valo * 375e705c121SKalle Valo * 3. create binary file from the hex file 376e705c121SKalle Valo * 377e705c121SKalle Valo * 4. save as "iNVM_xxx.bin" under /lib/firmware 378e705c121SKalle Valo */ 379e705c121SKalle Valo static int iwl_mvm_read_external_nvm(struct iwl_mvm *mvm) 380e705c121SKalle Valo { 381e705c121SKalle Valo int ret, section_size; 382e705c121SKalle Valo u16 section_id; 383e705c121SKalle Valo const struct firmware *fw_entry; 384e705c121SKalle Valo const struct { 385e705c121SKalle Valo __le16 word1; 386e705c121SKalle Valo __le16 word2; 387e705c121SKalle Valo u8 data[]; 388e705c121SKalle Valo } *file_sec; 3897d162045SJohannes Berg const u8 *eof; 3907d162045SJohannes Berg u8 *temp; 391e705c121SKalle Valo int max_section_size; 392e705c121SKalle Valo const __le32 *dword_buff; 393e705c121SKalle Valo 394e705c121SKalle Valo #define NVM_WORD1_LEN(x) (8 * (x & 0x03FF)) 395e705c121SKalle Valo #define NVM_WORD2_ID(x) (x >> 12) 396e705c121SKalle Valo #define NVM_WORD2_LEN_FAMILY_8000(x) (2 * ((x & 0xFF) << 8 | x >> 8)) 397e705c121SKalle Valo #define NVM_WORD1_ID_FAMILY_8000(x) (x >> 4) 398e705c121SKalle Valo #define NVM_HEADER_0 (0x2A504C54) 399e705c121SKalle Valo #define NVM_HEADER_1 (0x4E564D2A) 400e705c121SKalle Valo #define NVM_HEADER_SIZE (4 * sizeof(u32)) 401e705c121SKalle Valo 402e705c121SKalle Valo IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from external NVM\n"); 403e705c121SKalle Valo 404e705c121SKalle Valo /* Maximal size depends on HW family and step */ 405e705c121SKalle Valo if (mvm->trans->cfg->device_family != IWL_DEVICE_FAMILY_8000) 406e705c121SKalle Valo max_section_size = IWL_MAX_NVM_SECTION_SIZE; 407e705c121SKalle Valo else 408e705c121SKalle Valo max_section_size = IWL_MAX_NVM_8000_SECTION_SIZE; 409e705c121SKalle Valo 410e705c121SKalle Valo /* 411e705c121SKalle Valo * Obtain NVM image via request_firmware. Since we already used 412e705c121SKalle Valo * request_firmware_nowait() for the firmware binary load and only 413e705c121SKalle Valo * get here after that we assume the NVM request can be satisfied 414e705c121SKalle Valo * synchronously. 415e705c121SKalle Valo */ 416e705c121SKalle Valo ret = request_firmware(&fw_entry, mvm->nvm_file_name, 417e705c121SKalle Valo mvm->trans->dev); 418e705c121SKalle Valo if (ret) { 419e705c121SKalle Valo IWL_ERR(mvm, "ERROR: %s isn't available %d\n", 420e705c121SKalle Valo mvm->nvm_file_name, ret); 421e705c121SKalle Valo return ret; 422e705c121SKalle Valo } 423e705c121SKalle Valo 424e705c121SKalle Valo IWL_INFO(mvm, "Loaded NVM file %s (%zu bytes)\n", 425e705c121SKalle Valo mvm->nvm_file_name, fw_entry->size); 426e705c121SKalle Valo 427e705c121SKalle Valo if (fw_entry->size > MAX_NVM_FILE_LEN) { 428e705c121SKalle Valo IWL_ERR(mvm, "NVM file too large\n"); 429e705c121SKalle Valo ret = -EINVAL; 430e705c121SKalle Valo goto out; 431e705c121SKalle Valo } 432e705c121SKalle Valo 433e705c121SKalle Valo eof = fw_entry->data + fw_entry->size; 434e705c121SKalle Valo dword_buff = (__le32 *)fw_entry->data; 435e705c121SKalle Valo 436e705c121SKalle Valo /* some NVM file will contain a header. 437e705c121SKalle Valo * The header is identified by 2 dwords header as follow: 438e705c121SKalle Valo * dword[0] = 0x2A504C54 439e705c121SKalle Valo * dword[1] = 0x4E564D2A 440e705c121SKalle Valo * 441e705c121SKalle Valo * This header must be skipped when providing the NVM data to the FW. 442e705c121SKalle Valo */ 443e705c121SKalle Valo if (fw_entry->size > NVM_HEADER_SIZE && 444e705c121SKalle Valo dword_buff[0] == cpu_to_le32(NVM_HEADER_0) && 445e705c121SKalle Valo dword_buff[1] == cpu_to_le32(NVM_HEADER_1)) { 446e705c121SKalle Valo file_sec = (void *)(fw_entry->data + NVM_HEADER_SIZE); 447e705c121SKalle Valo IWL_INFO(mvm, "NVM Version %08X\n", le32_to_cpu(dword_buff[2])); 448e705c121SKalle Valo IWL_INFO(mvm, "NVM Manufacturing date %08X\n", 449e705c121SKalle Valo le32_to_cpu(dword_buff[3])); 450e705c121SKalle Valo 451e705c121SKalle Valo /* nvm file validation, dword_buff[2] holds the file version */ 452e705c121SKalle Valo if ((CSR_HW_REV_STEP(mvm->trans->hw_rev) == SILICON_C_STEP && 453e705c121SKalle Valo le32_to_cpu(dword_buff[2]) < 0xE4A) || 454e705c121SKalle Valo (CSR_HW_REV_STEP(mvm->trans->hw_rev) == SILICON_B_STEP && 455e705c121SKalle Valo le32_to_cpu(dword_buff[2]) >= 0xE4A)) { 456e705c121SKalle Valo ret = -EFAULT; 457e705c121SKalle Valo goto out; 458e705c121SKalle Valo } 459e705c121SKalle Valo } else { 460e705c121SKalle Valo file_sec = (void *)fw_entry->data; 461e705c121SKalle Valo } 462e705c121SKalle Valo 463e705c121SKalle Valo while (true) { 464e705c121SKalle Valo if (file_sec->data > eof) { 465e705c121SKalle Valo IWL_ERR(mvm, 466e705c121SKalle Valo "ERROR - NVM file too short for section header\n"); 467e705c121SKalle Valo ret = -EINVAL; 468e705c121SKalle Valo break; 469e705c121SKalle Valo } 470e705c121SKalle Valo 471e705c121SKalle Valo /* check for EOF marker */ 472e705c121SKalle Valo if (!file_sec->word1 && !file_sec->word2) { 473e705c121SKalle Valo ret = 0; 474e705c121SKalle Valo break; 475e705c121SKalle Valo } 476e705c121SKalle Valo 477e705c121SKalle Valo if (mvm->trans->cfg->device_family != IWL_DEVICE_FAMILY_8000) { 478e705c121SKalle Valo section_size = 479e705c121SKalle Valo 2 * NVM_WORD1_LEN(le16_to_cpu(file_sec->word1)); 480e705c121SKalle Valo section_id = NVM_WORD2_ID(le16_to_cpu(file_sec->word2)); 481e705c121SKalle Valo } else { 482e705c121SKalle Valo section_size = 2 * NVM_WORD2_LEN_FAMILY_8000( 483e705c121SKalle Valo le16_to_cpu(file_sec->word2)); 484e705c121SKalle Valo section_id = NVM_WORD1_ID_FAMILY_8000( 485e705c121SKalle Valo le16_to_cpu(file_sec->word1)); 486e705c121SKalle Valo } 487e705c121SKalle Valo 488e705c121SKalle Valo if (section_size > max_section_size) { 489e705c121SKalle Valo IWL_ERR(mvm, "ERROR - section too large (%d)\n", 490e705c121SKalle Valo section_size); 491e705c121SKalle Valo ret = -EINVAL; 492e705c121SKalle Valo break; 493e705c121SKalle Valo } 494e705c121SKalle Valo 495e705c121SKalle Valo if (!section_size) { 496e705c121SKalle Valo IWL_ERR(mvm, "ERROR - section empty\n"); 497e705c121SKalle Valo ret = -EINVAL; 498e705c121SKalle Valo break; 499e705c121SKalle Valo } 500e705c121SKalle Valo 501e705c121SKalle Valo if (file_sec->data + section_size > eof) { 502e705c121SKalle Valo IWL_ERR(mvm, 503e705c121SKalle Valo "ERROR - NVM file too short for section (%d bytes)\n", 504e705c121SKalle Valo section_size); 505e705c121SKalle Valo ret = -EINVAL; 506e705c121SKalle Valo break; 507e705c121SKalle Valo } 508e705c121SKalle Valo 509e705c121SKalle Valo if (WARN(section_id >= NVM_MAX_NUM_SECTIONS, 510e705c121SKalle Valo "Invalid NVM section ID %d\n", section_id)) { 511e705c121SKalle Valo ret = -EINVAL; 512e705c121SKalle Valo break; 513e705c121SKalle Valo } 514e705c121SKalle Valo 515e705c121SKalle Valo temp = kmemdup(file_sec->data, section_size, GFP_KERNEL); 516e705c121SKalle Valo if (!temp) { 517e705c121SKalle Valo ret = -ENOMEM; 518e705c121SKalle Valo break; 519e705c121SKalle Valo } 5207d162045SJohannes Berg 5217d162045SJohannes Berg iwl_mvm_nvm_fixups(mvm, section_id, temp, section_size); 5227d162045SJohannes Berg 523e705c121SKalle Valo kfree(mvm->nvm_sections[section_id].data); 524e705c121SKalle Valo mvm->nvm_sections[section_id].data = temp; 525e705c121SKalle Valo mvm->nvm_sections[section_id].length = section_size; 526e705c121SKalle Valo 527e705c121SKalle Valo /* advance to the next section */ 528e705c121SKalle Valo file_sec = (void *)(file_sec->data + section_size); 529e705c121SKalle Valo } 530e705c121SKalle Valo out: 531e705c121SKalle Valo release_firmware(fw_entry); 532e705c121SKalle Valo return ret; 533e705c121SKalle Valo } 534e705c121SKalle Valo 535e705c121SKalle Valo /* Loads the NVM data stored in mvm->nvm_sections into the NIC */ 536e705c121SKalle Valo int iwl_mvm_load_nvm_to_nic(struct iwl_mvm *mvm) 537e705c121SKalle Valo { 538e705c121SKalle Valo int i, ret = 0; 539e705c121SKalle Valo struct iwl_nvm_section *sections = mvm->nvm_sections; 540e705c121SKalle Valo 541e705c121SKalle Valo IWL_DEBUG_EEPROM(mvm->trans->dev, "'Write to NVM\n"); 542e705c121SKalle Valo 543e705c121SKalle Valo for (i = 0; i < ARRAY_SIZE(mvm->nvm_sections); i++) { 544e705c121SKalle Valo if (!mvm->nvm_sections[i].data || !mvm->nvm_sections[i].length) 545e705c121SKalle Valo continue; 546e705c121SKalle Valo ret = iwl_nvm_write_section(mvm, i, sections[i].data, 547e705c121SKalle Valo sections[i].length); 548e705c121SKalle Valo if (ret < 0) { 549e705c121SKalle Valo IWL_ERR(mvm, "iwl_mvm_send_cmd failed: %d\n", ret); 550e705c121SKalle Valo break; 551e705c121SKalle Valo } 552e705c121SKalle Valo } 553e705c121SKalle Valo return ret; 554e705c121SKalle Valo } 555e705c121SKalle Valo 556e705c121SKalle Valo int iwl_nvm_init(struct iwl_mvm *mvm, bool read_nvm_from_nic) 557e705c121SKalle Valo { 558e705c121SKalle Valo int ret, section; 559e705c121SKalle Valo u32 size_read = 0; 560e705c121SKalle Valo u8 *nvm_buffer, *temp; 561e705c121SKalle Valo const char *nvm_file_B = mvm->cfg->default_nvm_file_B_step; 562e705c121SKalle Valo const char *nvm_file_C = mvm->cfg->default_nvm_file_C_step; 563e705c121SKalle Valo 564e705c121SKalle Valo if (WARN_ON_ONCE(mvm->cfg->nvm_hw_section_num >= NVM_MAX_NUM_SECTIONS)) 565e705c121SKalle Valo return -EINVAL; 566e705c121SKalle Valo 567e705c121SKalle Valo /* load NVM values from nic */ 568e705c121SKalle Valo if (read_nvm_from_nic) { 569e705c121SKalle Valo /* Read From FW NVM */ 570e705c121SKalle Valo IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from NVM\n"); 571e705c121SKalle Valo 572e705c121SKalle Valo nvm_buffer = kmalloc(mvm->cfg->base_params->eeprom_size, 573e705c121SKalle Valo GFP_KERNEL); 574e705c121SKalle Valo if (!nvm_buffer) 575e705c121SKalle Valo return -ENOMEM; 576e705c121SKalle Valo for (section = 0; section < NVM_MAX_NUM_SECTIONS; section++) { 577e705c121SKalle Valo /* we override the constness for initial read */ 578e705c121SKalle Valo ret = iwl_nvm_read_section(mvm, section, nvm_buffer, 579e705c121SKalle Valo size_read); 580e705c121SKalle Valo if (ret < 0) 581e705c121SKalle Valo continue; 582e705c121SKalle Valo size_read += ret; 583e705c121SKalle Valo temp = kmemdup(nvm_buffer, ret, GFP_KERNEL); 584e705c121SKalle Valo if (!temp) { 585e705c121SKalle Valo ret = -ENOMEM; 586e705c121SKalle Valo break; 587e705c121SKalle Valo } 5887d162045SJohannes Berg 5897d162045SJohannes Berg iwl_mvm_nvm_fixups(mvm, section, temp, ret); 5907d162045SJohannes Berg 591e705c121SKalle Valo mvm->nvm_sections[section].data = temp; 592e705c121SKalle Valo mvm->nvm_sections[section].length = ret; 593e705c121SKalle Valo 594e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_DEBUGFS 595e705c121SKalle Valo switch (section) { 596e705c121SKalle Valo case NVM_SECTION_TYPE_SW: 597e705c121SKalle Valo mvm->nvm_sw_blob.data = temp; 598e705c121SKalle Valo mvm->nvm_sw_blob.size = ret; 599e705c121SKalle Valo break; 600e705c121SKalle Valo case NVM_SECTION_TYPE_CALIBRATION: 601e705c121SKalle Valo mvm->nvm_calib_blob.data = temp; 602e705c121SKalle Valo mvm->nvm_calib_blob.size = ret; 603e705c121SKalle Valo break; 604e705c121SKalle Valo case NVM_SECTION_TYPE_PRODUCTION: 605e705c121SKalle Valo mvm->nvm_prod_blob.data = temp; 606e705c121SKalle Valo mvm->nvm_prod_blob.size = ret; 607e705c121SKalle Valo break; 608e705c121SKalle Valo case NVM_SECTION_TYPE_PHY_SKU: 609e705c121SKalle Valo mvm->nvm_phy_sku_blob.data = temp; 610e705c121SKalle Valo mvm->nvm_phy_sku_blob.size = ret; 611e705c121SKalle Valo break; 612e705c121SKalle Valo default: 613e705c121SKalle Valo if (section == mvm->cfg->nvm_hw_section_num) { 614e705c121SKalle Valo mvm->nvm_hw_blob.data = temp; 615e705c121SKalle Valo mvm->nvm_hw_blob.size = ret; 616e705c121SKalle Valo break; 617e705c121SKalle Valo } 618e705c121SKalle Valo } 619e705c121SKalle Valo #endif 620e705c121SKalle Valo } 621e705c121SKalle Valo if (!size_read) 622e705c121SKalle Valo IWL_ERR(mvm, "OTP is blank\n"); 623e705c121SKalle Valo kfree(nvm_buffer); 624e705c121SKalle Valo } 625e705c121SKalle Valo 626e705c121SKalle Valo /* Only if PNVM selected in the mod param - load external NVM */ 627e705c121SKalle Valo if (mvm->nvm_file_name) { 628e705c121SKalle Valo /* read External NVM file from the mod param */ 629e705c121SKalle Valo ret = iwl_mvm_read_external_nvm(mvm); 630e705c121SKalle Valo if (ret) { 631e705c121SKalle Valo /* choose the nvm_file name according to the 632e705c121SKalle Valo * HW step 633e705c121SKalle Valo */ 634e705c121SKalle Valo if (CSR_HW_REV_STEP(mvm->trans->hw_rev) == 635e705c121SKalle Valo SILICON_B_STEP) 636e705c121SKalle Valo mvm->nvm_file_name = nvm_file_B; 637e705c121SKalle Valo else 638e705c121SKalle Valo mvm->nvm_file_name = nvm_file_C; 639e705c121SKalle Valo 640488c28e1SOren Givon if ((ret == -EFAULT || ret == -ENOENT) && 641488c28e1SOren Givon mvm->nvm_file_name) { 642e705c121SKalle Valo /* in case nvm file was failed try again */ 643e705c121SKalle Valo ret = iwl_mvm_read_external_nvm(mvm); 644e705c121SKalle Valo if (ret) 645e705c121SKalle Valo return ret; 646e705c121SKalle Valo } else { 647e705c121SKalle Valo return ret; 648e705c121SKalle Valo } 649e705c121SKalle Valo } 650e705c121SKalle Valo } 651e705c121SKalle Valo 652e705c121SKalle Valo /* parse the relevant nvm sections */ 653e705c121SKalle Valo mvm->nvm_data = iwl_parse_nvm_sections(mvm); 654e705c121SKalle Valo if (!mvm->nvm_data) 655e705c121SKalle Valo return -ENODATA; 656e705c121SKalle Valo IWL_DEBUG_EEPROM(mvm->trans->dev, "nvm version = %x\n", 657e705c121SKalle Valo mvm->nvm_data->nvm_version); 658e705c121SKalle Valo 659e705c121SKalle Valo return 0; 660e705c121SKalle Valo } 661e705c121SKalle Valo 662e705c121SKalle Valo struct iwl_mcc_update_resp * 663e705c121SKalle Valo iwl_mvm_update_mcc(struct iwl_mvm *mvm, const char *alpha2, 664e705c121SKalle Valo enum iwl_mcc_source src_id) 665e705c121SKalle Valo { 666e705c121SKalle Valo struct iwl_mcc_update_cmd mcc_update_cmd = { 667e705c121SKalle Valo .mcc = cpu_to_le16(alpha2[0] << 8 | alpha2[1]), 668e705c121SKalle Valo .source_id = (u8)src_id, 669e705c121SKalle Valo }; 670e705c121SKalle Valo struct iwl_mcc_update_resp *mcc_resp, *resp_cp = NULL; 6716fa52430SMatti Gottlieb struct iwl_mcc_update_resp_v1 *mcc_resp_v1 = NULL; 672e705c121SKalle Valo struct iwl_rx_packet *pkt; 673e705c121SKalle Valo struct iwl_host_cmd cmd = { 674e705c121SKalle Valo .id = MCC_UPDATE_CMD, 675e705c121SKalle Valo .flags = CMD_WANT_SKB, 676e705c121SKalle Valo .data = { &mcc_update_cmd }, 677e705c121SKalle Valo }; 678e705c121SKalle Valo 679e705c121SKalle Valo int ret; 680e705c121SKalle Valo u32 status; 681e705c121SKalle Valo int resp_len, n_channels; 682e705c121SKalle Valo u16 mcc; 6836fa52430SMatti Gottlieb bool resp_v2 = fw_has_capa(&mvm->fw->ucode_capa, 6846fa52430SMatti Gottlieb IWL_UCODE_TLV_CAPA_LAR_SUPPORT_V2); 685e705c121SKalle Valo 686e705c121SKalle Valo if (WARN_ON_ONCE(!iwl_mvm_is_lar_supported(mvm))) 687e705c121SKalle Valo return ERR_PTR(-EOPNOTSUPP); 688e705c121SKalle Valo 689e705c121SKalle Valo cmd.len[0] = sizeof(struct iwl_mcc_update_cmd); 6906fa52430SMatti Gottlieb if (!resp_v2) 6916fa52430SMatti Gottlieb cmd.len[0] = sizeof(struct iwl_mcc_update_cmd_v1); 692e705c121SKalle Valo 693e705c121SKalle Valo IWL_DEBUG_LAR(mvm, "send MCC update to FW with '%c%c' src = %d\n", 694e705c121SKalle Valo alpha2[0], alpha2[1], src_id); 695e705c121SKalle Valo 696e705c121SKalle Valo ret = iwl_mvm_send_cmd(mvm, &cmd); 697e705c121SKalle Valo if (ret) 698e705c121SKalle Valo return ERR_PTR(ret); 699e705c121SKalle Valo 700e705c121SKalle Valo pkt = cmd.resp_pkt; 701e705c121SKalle Valo 702e705c121SKalle Valo /* Extract MCC response */ 7036fa52430SMatti Gottlieb if (resp_v2) { 704e705c121SKalle Valo mcc_resp = (void *)pkt->data; 7056fa52430SMatti Gottlieb n_channels = __le32_to_cpu(mcc_resp->n_channels); 7066fa52430SMatti Gottlieb } else { 7076fa52430SMatti Gottlieb mcc_resp_v1 = (void *)pkt->data; 7086fa52430SMatti Gottlieb n_channels = __le32_to_cpu(mcc_resp_v1->n_channels); 709e705c121SKalle Valo } 710e705c121SKalle Valo 7116fa52430SMatti Gottlieb resp_len = sizeof(struct iwl_mcc_update_resp) + n_channels * 7126fa52430SMatti Gottlieb sizeof(__le32); 713e705c121SKalle Valo 7146fa52430SMatti Gottlieb resp_cp = kzalloc(resp_len, GFP_KERNEL); 715e705c121SKalle Valo if (!resp_cp) { 716e705c121SKalle Valo ret = -ENOMEM; 717e705c121SKalle Valo goto exit; 718e705c121SKalle Valo } 719e705c121SKalle Valo 7206fa52430SMatti Gottlieb if (resp_v2) { 7216fa52430SMatti Gottlieb memcpy(resp_cp, mcc_resp, resp_len); 7226fa52430SMatti Gottlieb } else { 7236fa52430SMatti Gottlieb resp_cp->status = mcc_resp_v1->status; 7246fa52430SMatti Gottlieb resp_cp->mcc = mcc_resp_v1->mcc; 7256fa52430SMatti Gottlieb resp_cp->cap = mcc_resp_v1->cap; 7266fa52430SMatti Gottlieb resp_cp->source_id = mcc_resp_v1->source_id; 7276fa52430SMatti Gottlieb resp_cp->n_channels = mcc_resp_v1->n_channels; 7286fa52430SMatti Gottlieb memcpy(resp_cp->channels, mcc_resp_v1->channels, 7296fa52430SMatti Gottlieb n_channels * sizeof(__le32)); 7306fa52430SMatti Gottlieb } 7316fa52430SMatti Gottlieb 7326fa52430SMatti Gottlieb status = le32_to_cpu(resp_cp->status); 7336fa52430SMatti Gottlieb 7346fa52430SMatti Gottlieb mcc = le16_to_cpu(resp_cp->mcc); 7356fa52430SMatti Gottlieb 7366fa52430SMatti Gottlieb /* W/A for a FW/NVM issue - returns 0x00 for the world domain */ 7376fa52430SMatti Gottlieb if (mcc == 0) { 7386fa52430SMatti Gottlieb mcc = 0x3030; /* "00" - world */ 7396fa52430SMatti Gottlieb resp_cp->mcc = cpu_to_le16(mcc); 7406fa52430SMatti Gottlieb } 7416fa52430SMatti Gottlieb 7426fa52430SMatti Gottlieb IWL_DEBUG_LAR(mvm, 7436fa52430SMatti Gottlieb "MCC response status: 0x%x. new MCC: 0x%x ('%c%c') change: %d n_chans: %d\n", 7446fa52430SMatti Gottlieb status, mcc, mcc >> 8, mcc & 0xff, 7456fa52430SMatti Gottlieb !!(status == MCC_RESP_NEW_CHAN_PROFILE), n_channels); 7466fa52430SMatti Gottlieb 747e705c121SKalle Valo exit: 748e705c121SKalle Valo iwl_free_resp(&cmd); 749e705c121SKalle Valo if (ret) 750e705c121SKalle Valo return ERR_PTR(ret); 751e705c121SKalle Valo return resp_cp; 752e705c121SKalle Valo } 753e705c121SKalle Valo 754e705c121SKalle Valo #ifdef CONFIG_ACPI 755e705c121SKalle Valo #define WRD_METHOD "WRDD" 756e705c121SKalle Valo #define WRDD_WIFI (0x07) 757e705c121SKalle Valo #define WRDD_WIGIG (0x10) 758e705c121SKalle Valo 759e705c121SKalle Valo static u32 iwl_mvm_wrdd_get_mcc(struct iwl_mvm *mvm, union acpi_object *wrdd) 760e705c121SKalle Valo { 761e705c121SKalle Valo union acpi_object *mcc_pkg, *domain_type, *mcc_value; 762e705c121SKalle Valo u32 i; 763e705c121SKalle Valo 764e705c121SKalle Valo if (wrdd->type != ACPI_TYPE_PACKAGE || 765e705c121SKalle Valo wrdd->package.count < 2 || 766e705c121SKalle Valo wrdd->package.elements[0].type != ACPI_TYPE_INTEGER || 767e705c121SKalle Valo wrdd->package.elements[0].integer.value != 0) { 768e705c121SKalle Valo IWL_DEBUG_LAR(mvm, "Unsupported wrdd structure\n"); 769e705c121SKalle Valo return 0; 770e705c121SKalle Valo } 771e705c121SKalle Valo 772e705c121SKalle Valo for (i = 1 ; i < wrdd->package.count ; ++i) { 773e705c121SKalle Valo mcc_pkg = &wrdd->package.elements[i]; 774e705c121SKalle Valo 775e705c121SKalle Valo if (mcc_pkg->type != ACPI_TYPE_PACKAGE || 776e705c121SKalle Valo mcc_pkg->package.count < 2 || 777e705c121SKalle Valo mcc_pkg->package.elements[0].type != ACPI_TYPE_INTEGER || 778e705c121SKalle Valo mcc_pkg->package.elements[1].type != ACPI_TYPE_INTEGER) { 779e705c121SKalle Valo mcc_pkg = NULL; 780e705c121SKalle Valo continue; 781e705c121SKalle Valo } 782e705c121SKalle Valo 783e705c121SKalle Valo domain_type = &mcc_pkg->package.elements[0]; 784e705c121SKalle Valo if (domain_type->integer.value == WRDD_WIFI) 785e705c121SKalle Valo break; 786e705c121SKalle Valo 787e705c121SKalle Valo mcc_pkg = NULL; 788e705c121SKalle Valo } 789e705c121SKalle Valo 790e705c121SKalle Valo if (mcc_pkg) { 791e705c121SKalle Valo mcc_value = &mcc_pkg->package.elements[1]; 792e705c121SKalle Valo return mcc_value->integer.value; 793e705c121SKalle Valo } 794e705c121SKalle Valo 795e705c121SKalle Valo return 0; 796e705c121SKalle Valo } 797e705c121SKalle Valo 798e705c121SKalle Valo static int iwl_mvm_get_bios_mcc(struct iwl_mvm *mvm, char *mcc) 799e705c121SKalle Valo { 800e705c121SKalle Valo acpi_handle root_handle; 801e705c121SKalle Valo acpi_handle handle; 802e705c121SKalle Valo struct acpi_buffer wrdd = {ACPI_ALLOCATE_BUFFER, NULL}; 803e705c121SKalle Valo acpi_status status; 804e705c121SKalle Valo u32 mcc_val; 805e705c121SKalle Valo struct pci_dev *pdev = to_pci_dev(mvm->dev); 806e705c121SKalle Valo 807e705c121SKalle Valo root_handle = ACPI_HANDLE(&pdev->dev); 808e705c121SKalle Valo if (!root_handle) { 809e705c121SKalle Valo IWL_DEBUG_LAR(mvm, 810e705c121SKalle Valo "Could not retrieve root port ACPI handle\n"); 811e705c121SKalle Valo return -ENOENT; 812e705c121SKalle Valo } 813e705c121SKalle Valo 814e705c121SKalle Valo /* Get the method's handle */ 815e705c121SKalle Valo status = acpi_get_handle(root_handle, (acpi_string)WRD_METHOD, &handle); 816e705c121SKalle Valo if (ACPI_FAILURE(status)) { 817e705c121SKalle Valo IWL_DEBUG_LAR(mvm, "WRD method not found\n"); 818e705c121SKalle Valo return -ENOENT; 819e705c121SKalle Valo } 820e705c121SKalle Valo 821e705c121SKalle Valo /* Call WRDD with no arguments */ 822e705c121SKalle Valo status = acpi_evaluate_object(handle, NULL, NULL, &wrdd); 823e705c121SKalle Valo if (ACPI_FAILURE(status)) { 824e705c121SKalle Valo IWL_DEBUG_LAR(mvm, "WRDC invocation failed (0x%x)\n", status); 825e705c121SKalle Valo return -ENOENT; 826e705c121SKalle Valo } 827e705c121SKalle Valo 828e705c121SKalle Valo mcc_val = iwl_mvm_wrdd_get_mcc(mvm, wrdd.pointer); 829e705c121SKalle Valo kfree(wrdd.pointer); 830e705c121SKalle Valo if (!mcc_val) 831e705c121SKalle Valo return -ENOENT; 832e705c121SKalle Valo 833e705c121SKalle Valo mcc[0] = (mcc_val >> 8) & 0xff; 834e705c121SKalle Valo mcc[1] = mcc_val & 0xff; 835e705c121SKalle Valo mcc[2] = '\0'; 836e705c121SKalle Valo return 0; 837e705c121SKalle Valo } 838e705c121SKalle Valo #else /* CONFIG_ACPI */ 839e705c121SKalle Valo static int iwl_mvm_get_bios_mcc(struct iwl_mvm *mvm, char *mcc) 840e705c121SKalle Valo { 841e705c121SKalle Valo return -ENOENT; 842e705c121SKalle Valo } 843e705c121SKalle Valo #endif 844e705c121SKalle Valo 845e705c121SKalle Valo int iwl_mvm_init_mcc(struct iwl_mvm *mvm) 846e705c121SKalle Valo { 847e705c121SKalle Valo bool tlv_lar; 848e705c121SKalle Valo bool nvm_lar; 849e705c121SKalle Valo int retval; 850e705c121SKalle Valo struct ieee80211_regdomain *regd; 851e705c121SKalle Valo char mcc[3]; 852e705c121SKalle Valo 853e705c121SKalle Valo if (mvm->cfg->device_family == IWL_DEVICE_FAMILY_8000) { 854e705c121SKalle Valo tlv_lar = fw_has_capa(&mvm->fw->ucode_capa, 855e705c121SKalle Valo IWL_UCODE_TLV_CAPA_LAR_SUPPORT); 856e705c121SKalle Valo nvm_lar = mvm->nvm_data->lar_enabled; 857e705c121SKalle Valo if (tlv_lar != nvm_lar) 858e705c121SKalle Valo IWL_INFO(mvm, 859e705c121SKalle Valo "Conflict between TLV & NVM regarding enabling LAR (TLV = %s NVM =%s)\n", 860e705c121SKalle Valo tlv_lar ? "enabled" : "disabled", 861e705c121SKalle Valo nvm_lar ? "enabled" : "disabled"); 862e705c121SKalle Valo } 863e705c121SKalle Valo 864e705c121SKalle Valo if (!iwl_mvm_is_lar_supported(mvm)) 865e705c121SKalle Valo return 0; 866e705c121SKalle Valo 867e705c121SKalle Valo /* 868e705c121SKalle Valo * try to replay the last set MCC to FW. If it doesn't exist, 869e705c121SKalle Valo * queue an update to cfg80211 to retrieve the default alpha2 from FW. 870e705c121SKalle Valo */ 871e705c121SKalle Valo retval = iwl_mvm_init_fw_regd(mvm); 872e705c121SKalle Valo if (retval != -ENOENT) 873e705c121SKalle Valo return retval; 874e705c121SKalle Valo 875e705c121SKalle Valo /* 876e705c121SKalle Valo * Driver regulatory hint for initial update, this also informs the 877e705c121SKalle Valo * firmware we support wifi location updates. 878e705c121SKalle Valo * Disallow scans that might crash the FW while the LAR regdomain 879e705c121SKalle Valo * is not set. 880e705c121SKalle Valo */ 881e705c121SKalle Valo mvm->lar_regdom_set = false; 882e705c121SKalle Valo 883e705c121SKalle Valo regd = iwl_mvm_get_current_regdomain(mvm, NULL); 884e705c121SKalle Valo if (IS_ERR_OR_NULL(regd)) 885e705c121SKalle Valo return -EIO; 886e705c121SKalle Valo 887e705c121SKalle Valo if (iwl_mvm_is_wifi_mcc_supported(mvm) && 888e705c121SKalle Valo !iwl_mvm_get_bios_mcc(mvm, mcc)) { 889e705c121SKalle Valo kfree(regd); 890e705c121SKalle Valo regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, mcc, 891e705c121SKalle Valo MCC_SOURCE_BIOS, NULL); 892e705c121SKalle Valo if (IS_ERR_OR_NULL(regd)) 893e705c121SKalle Valo return -EIO; 894e705c121SKalle Valo } 895e705c121SKalle Valo 896e705c121SKalle Valo retval = regulatory_set_wiphy_regd_sync_rtnl(mvm->hw->wiphy, regd); 897e705c121SKalle Valo kfree(regd); 898e705c121SKalle Valo return retval; 899e705c121SKalle Valo } 900e705c121SKalle Valo 901e705c121SKalle Valo void iwl_mvm_rx_chub_update_mcc(struct iwl_mvm *mvm, 902e705c121SKalle Valo struct iwl_rx_cmd_buffer *rxb) 903e705c121SKalle Valo { 904e705c121SKalle Valo struct iwl_rx_packet *pkt = rxb_addr(rxb); 905e705c121SKalle Valo struct iwl_mcc_chub_notif *notif = (void *)pkt->data; 906e705c121SKalle Valo enum iwl_mcc_source src; 907e705c121SKalle Valo char mcc[3]; 908e705c121SKalle Valo struct ieee80211_regdomain *regd; 909e705c121SKalle Valo 910e705c121SKalle Valo lockdep_assert_held(&mvm->mutex); 911e705c121SKalle Valo 912e705c121SKalle Valo if (WARN_ON_ONCE(!iwl_mvm_is_lar_supported(mvm))) 913e705c121SKalle Valo return; 914e705c121SKalle Valo 915e705c121SKalle Valo mcc[0] = notif->mcc >> 8; 916e705c121SKalle Valo mcc[1] = notif->mcc & 0xff; 917e705c121SKalle Valo mcc[2] = '\0'; 918e705c121SKalle Valo src = notif->source_id; 919e705c121SKalle Valo 920e705c121SKalle Valo IWL_DEBUG_LAR(mvm, 921e705c121SKalle Valo "RX: received chub update mcc cmd (mcc '%s' src %d)\n", 922e705c121SKalle Valo mcc, src); 923e705c121SKalle Valo regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, mcc, src, NULL); 924e705c121SKalle Valo if (IS_ERR_OR_NULL(regd)) 925e705c121SKalle Valo return; 926e705c121SKalle Valo 927e705c121SKalle Valo regulatory_set_wiphy_regd(mvm->hw->wiphy, regd); 928e705c121SKalle Valo kfree(regd); 929e705c121SKalle Valo } 930