Path: blob/main/sys/contrib/dev/iwlwifi/mvm/nvm.c
107611 views
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause1/*2* Copyright (C) 2012-2014, 2018-2019, 2021-2025 Intel Corporation3* Copyright (C) 2013-2015 Intel Mobile Communications GmbH4* Copyright (C) 2016-2017 Intel Deutschland GmbH5*/6#include <linux/firmware.h>7#if defined(__linux__)8#include <linux/rtnetlink.h>9#endif10#include "iwl-trans.h"11#include "iwl-csr.h"12#include "mvm.h"13#include "iwl-nvm-utils.h"14#include "iwl-nvm-parse.h"15#include "iwl-prph.h"16#include "fw/acpi.h"1718/* Default NVM size to read */19#define IWL_NVM_DEFAULT_CHUNK_SIZE (2 * 1024)2021#define NVM_WRITE_OPCODE 122#define NVM_READ_OPCODE 02324/* load nvm chunk response */25enum {26READ_NVM_CHUNK_SUCCEED = 0,27READ_NVM_CHUNK_NOT_VALID_ADDRESS = 128};2930/*31* prepare the NVM host command w/ the pointers to the nvm buffer32* and send it to fw33*/34static int iwl_nvm_write_chunk(struct iwl_mvm *mvm, u16 section,35u16 offset, u16 length, const u8 *data)36{37struct iwl_nvm_access_cmd nvm_access_cmd = {38.offset = cpu_to_le16(offset),39.length = cpu_to_le16(length),40.type = cpu_to_le16(section),41.op_code = NVM_WRITE_OPCODE,42};43struct iwl_host_cmd cmd = {44.id = NVM_ACCESS_CMD,45.len = { sizeof(struct iwl_nvm_access_cmd), length },46.flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,47.data = { &nvm_access_cmd, data },48/* data may come from vmalloc, so use _DUP */49.dataflags = { 0, IWL_HCMD_DFL_DUP },50};51struct iwl_rx_packet *pkt;52struct iwl_nvm_access_resp *nvm_resp;53int ret;5455ret = iwl_mvm_send_cmd(mvm, &cmd);56if (ret)57return ret;5859pkt = cmd.resp_pkt;60/* Extract & check NVM write response */61nvm_resp = (void *)pkt->data;62if (le16_to_cpu(nvm_resp->status) != READ_NVM_CHUNK_SUCCEED) {63IWL_ERR(mvm,64"NVM access write command failed for section %u (status = 0x%x)\n",65section, le16_to_cpu(nvm_resp->status));66ret = -EIO;67}6869iwl_free_resp(&cmd);70return ret;71}7273static int iwl_nvm_read_chunk(struct iwl_mvm *mvm, u16 section,74u16 offset, u16 length, u8 *data)75{76struct iwl_nvm_access_cmd nvm_access_cmd = {77.offset = cpu_to_le16(offset),78.length = cpu_to_le16(length),79.type = cpu_to_le16(section),80.op_code = NVM_READ_OPCODE,81};82struct iwl_nvm_access_resp *nvm_resp;83struct iwl_rx_packet *pkt;84struct iwl_host_cmd cmd = {85.id = NVM_ACCESS_CMD,86.flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,87.data = { &nvm_access_cmd, },88};89int ret, bytes_read, offset_read;90u8 *resp_data;9192cmd.len[0] = sizeof(struct iwl_nvm_access_cmd);9394ret = iwl_mvm_send_cmd(mvm, &cmd);95if (ret)96return ret;9798pkt = cmd.resp_pkt;99100/* Extract NVM response */101nvm_resp = (void *)pkt->data;102ret = le16_to_cpu(nvm_resp->status);103bytes_read = le16_to_cpu(nvm_resp->length);104offset_read = le16_to_cpu(nvm_resp->offset);105resp_data = nvm_resp->data;106if (ret) {107if ((offset != 0) &&108(ret == READ_NVM_CHUNK_NOT_VALID_ADDRESS)) {109/*110* meaning of NOT_VALID_ADDRESS:111* driver try to read chunk from address that is112* multiple of 2K and got an error since addr is empty.113* meaning of (offset != 0): driver already114* read valid data from another chunk so this case115* is not an error.116*/117IWL_DEBUG_EEPROM(mvm->trans->dev,118"NVM access command failed on offset 0x%x since that section size is multiple 2K\n",119offset);120ret = 0;121} else {122IWL_DEBUG_EEPROM(mvm->trans->dev,123"NVM access command failed with status %d (device: %s)\n",124ret, mvm->trans->info.name);125ret = -ENODATA;126}127goto exit;128}129130if (offset_read != offset) {131IWL_ERR(mvm, "NVM ACCESS response with invalid offset %d\n",132offset_read);133ret = -EINVAL;134goto exit;135}136137/* Write data to NVM */138memcpy(data + offset, resp_data, bytes_read);139ret = bytes_read;140141exit:142iwl_free_resp(&cmd);143return ret;144}145146static int iwl_nvm_write_section(struct iwl_mvm *mvm, u16 section,147const u8 *data, u16 length)148{149int offset = 0;150151/* copy data in chunks of 2k (and remainder if any) */152153while (offset < length) {154int chunk_size, ret;155156chunk_size = min(IWL_NVM_DEFAULT_CHUNK_SIZE,157length - offset);158159ret = iwl_nvm_write_chunk(mvm, section, offset,160chunk_size, data + offset);161if (ret < 0)162return ret;163164offset += chunk_size;165}166167return 0;168}169170/*171* Reads an NVM section completely.172* NICs prior to 7000 family doesn't have a real NVM, but just read173* section 0 which is the EEPROM. Because the EEPROM reading is unlimited174* by uCode, we need to manually check in this case that we don't175* overflow and try to read more than the EEPROM size.176* For 7000 family NICs, we supply the maximal size we can read, and177* the uCode fills the response with as much data as we can,178* without overflowing, so no check is needed.179*/180static int iwl_nvm_read_section(struct iwl_mvm *mvm, u16 section,181u8 *data, u32 size_read)182{183u16 length, offset = 0;184int ret;185186/* Set nvm section read length */187length = IWL_NVM_DEFAULT_CHUNK_SIZE;188189ret = length;190191/* Read the NVM until exhausted (reading less than requested) */192while (ret == length) {193/* Check no memory assumptions fail and cause an overflow */194if ((size_read + offset + length) >195mvm->trans->mac_cfg->base->eeprom_size) {196IWL_ERR(mvm, "EEPROM size is too small for NVM\n");197return -ENOBUFS;198}199200ret = iwl_nvm_read_chunk(mvm, section, offset, length, data);201if (ret < 0) {202IWL_DEBUG_EEPROM(mvm->trans->dev,203"Cannot read NVM from section %d offset %d, length %d\n",204section, offset, length);205return ret;206}207offset += ret;208}209210iwl_nvm_fixups(mvm->trans->info.hw_id, section, data, offset);211212IWL_DEBUG_EEPROM(mvm->trans->dev,213"NVM section %d read completed\n", section);214return offset;215}216217static struct iwl_nvm_data *218iwl_parse_nvm_sections(struct iwl_mvm *mvm)219{220struct iwl_nvm_section *sections = mvm->nvm_sections;221const __be16 *hw;222const __le16 *sw, *calib, *regulatory, *mac_override, *phy_sku;223u8 tx_ant = mvm->fw->valid_tx_ant;224u8 rx_ant = mvm->fw->valid_rx_ant;225int regulatory_type;226227/* Checking for required sections */228if (mvm->trans->cfg->nvm_type == IWL_NVM) {229if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data ||230!mvm->nvm_sections[mvm->trans->mac_cfg->base->nvm_hw_section_num].data) {231IWL_ERR(mvm, "Can't parse empty OTP/NVM sections\n");232return NULL;233}234} else {235if (mvm->trans->cfg->nvm_type == IWL_NVM_SDP)236regulatory_type = NVM_SECTION_TYPE_REGULATORY_SDP;237else238regulatory_type = NVM_SECTION_TYPE_REGULATORY;239240/* SW and REGULATORY sections are mandatory */241if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data ||242!mvm->nvm_sections[regulatory_type].data) {243IWL_ERR(mvm,244"Can't parse empty family 8000 OTP/NVM sections\n");245return NULL;246}247/* MAC_OVERRIDE or at least HW section must exist */248if (!mvm->nvm_sections[mvm->trans->mac_cfg->base->nvm_hw_section_num].data &&249!mvm->nvm_sections[NVM_SECTION_TYPE_MAC_OVERRIDE].data) {250IWL_ERR(mvm,251"Can't parse mac_address, empty sections\n");252return NULL;253}254255/* PHY_SKU section is mandatory in B0 */256if (mvm->trans->cfg->nvm_type == IWL_NVM_EXT &&257!mvm->nvm_sections[NVM_SECTION_TYPE_PHY_SKU].data) {258IWL_ERR(mvm,259"Can't parse phy_sku in B0, empty sections\n");260return NULL;261}262}263264hw = (const __be16 *)sections[mvm->trans->mac_cfg->base->nvm_hw_section_num].data;265sw = (const __le16 *)sections[NVM_SECTION_TYPE_SW].data;266calib = (const __le16 *)sections[NVM_SECTION_TYPE_CALIBRATION].data;267mac_override =268(const __le16 *)sections[NVM_SECTION_TYPE_MAC_OVERRIDE].data;269phy_sku = (const __le16 *)sections[NVM_SECTION_TYPE_PHY_SKU].data;270271regulatory = mvm->trans->cfg->nvm_type == IWL_NVM_SDP ?272(const __le16 *)sections[NVM_SECTION_TYPE_REGULATORY_SDP].data :273(const __le16 *)sections[NVM_SECTION_TYPE_REGULATORY].data;274275if (mvm->set_tx_ant)276tx_ant &= mvm->set_tx_ant;277278if (mvm->set_rx_ant)279rx_ant &= mvm->set_rx_ant;280281return iwl_parse_nvm_data(mvm->trans, mvm->cfg, mvm->fw, hw, sw, calib,282regulatory, mac_override, phy_sku,283tx_ant, rx_ant);284}285286/* Loads the NVM data stored in mvm->nvm_sections into the NIC */287int iwl_mvm_load_nvm_to_nic(struct iwl_mvm *mvm)288{289int i, ret = 0;290struct iwl_nvm_section *sections = mvm->nvm_sections;291292IWL_DEBUG_EEPROM(mvm->trans->dev, "'Write to NVM\n");293294for (i = 0; i < ARRAY_SIZE(mvm->nvm_sections); i++) {295if (!mvm->nvm_sections[i].data || !mvm->nvm_sections[i].length)296continue;297ret = iwl_nvm_write_section(mvm, i, sections[i].data,298sections[i].length);299if (ret < 0) {300IWL_ERR(mvm, "iwl_mvm_send_cmd failed: %d\n", ret);301break;302}303}304return ret;305}306307int iwl_nvm_init(struct iwl_mvm *mvm)308{309int ret, section;310u32 size_read = 0;311u8 *nvm_buffer, *temp;312313if (WARN_ON_ONCE(mvm->trans->mac_cfg->base->nvm_hw_section_num >= NVM_MAX_NUM_SECTIONS))314return -EINVAL;315316/* load NVM values from nic */317/* Read From FW NVM */318IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from NVM\n");319320nvm_buffer = kmalloc(mvm->trans->mac_cfg->base->eeprom_size,321GFP_KERNEL);322if (!nvm_buffer)323return -ENOMEM;324for (section = 0; section < NVM_MAX_NUM_SECTIONS; section++) {325/* we override the constness for initial read */326ret = iwl_nvm_read_section(mvm, section, nvm_buffer,327size_read);328if (ret == -ENODATA) {329ret = 0;330continue;331}332if (ret < 0)333break;334size_read += ret;335temp = kmemdup(nvm_buffer, ret, GFP_KERNEL);336if (!temp) {337ret = -ENOMEM;338break;339}340341iwl_nvm_fixups(mvm->trans->info.hw_id, section, temp, ret);342343mvm->nvm_sections[section].data = temp;344mvm->nvm_sections[section].length = ret;345346#ifdef CONFIG_IWLWIFI_DEBUGFS347switch (section) {348case NVM_SECTION_TYPE_SW:349mvm->nvm_sw_blob.data = temp;350mvm->nvm_sw_blob.size = ret;351break;352case NVM_SECTION_TYPE_CALIBRATION:353mvm->nvm_calib_blob.data = temp;354mvm->nvm_calib_blob.size = ret;355break;356case NVM_SECTION_TYPE_PRODUCTION:357mvm->nvm_prod_blob.data = temp;358mvm->nvm_prod_blob.size = ret;359break;360case NVM_SECTION_TYPE_PHY_SKU:361mvm->nvm_phy_sku_blob.data = temp;362mvm->nvm_phy_sku_blob.size = ret;363break;364case NVM_SECTION_TYPE_REGULATORY_SDP:365case NVM_SECTION_TYPE_REGULATORY:366mvm->nvm_reg_blob.data = temp;367mvm->nvm_reg_blob.size = ret;368break;369default:370if (section == mvm->trans->mac_cfg->base->nvm_hw_section_num) {371mvm->nvm_hw_blob.data = temp;372mvm->nvm_hw_blob.size = ret;373break;374}375}376#endif377}378if (!size_read)379IWL_ERR(mvm, "OTP is blank\n");380kfree(nvm_buffer);381382/* Only if PNVM selected in the mod param - load external NVM */383if (mvm->nvm_file_name) {384/* read External NVM file from the mod param */385ret = iwl_read_external_nvm(mvm->trans, mvm->nvm_file_name,386mvm->nvm_sections);387if (ret)388return ret;389}390391/* parse the relevant nvm sections */392mvm->nvm_data = iwl_parse_nvm_sections(mvm);393if (!mvm->nvm_data)394return -ENODATA;395IWL_DEBUG_EEPROM(mvm->trans->dev, "nvm version = %x\n",396mvm->nvm_data->nvm_version);397398return ret < 0 ? ret : 0;399}400401struct iwl_mcc_update_resp_v8 *402iwl_mvm_update_mcc(struct iwl_mvm *mvm, const char *alpha2,403enum iwl_mcc_source src_id)404{405struct iwl_mcc_update_cmd mcc_update_cmd = {406.mcc = cpu_to_le16(alpha2[0] << 8 | alpha2[1]),407.source_id = (u8)src_id,408};409struct iwl_mcc_update_resp_v8 *resp_cp;410struct iwl_rx_packet *pkt;411struct iwl_host_cmd cmd = {412.id = MCC_UPDATE_CMD,413.flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,414.data = { &mcc_update_cmd },415};416417int ret, resp_ver;418u32 status;419int resp_len, n_channels;420u16 mcc;421422if (WARN_ON_ONCE(!iwl_mvm_is_lar_supported(mvm)))423return ERR_PTR(-EOPNOTSUPP);424425cmd.len[0] = sizeof(struct iwl_mcc_update_cmd);426427IWL_DEBUG_LAR(mvm, "send MCC update to FW with '%c%c' src = %d\n",428alpha2[0], alpha2[1], src_id);429430ret = iwl_mvm_send_cmd(mvm, &cmd);431if (ret)432return ERR_PTR(ret);433434pkt = cmd.resp_pkt;435436resp_ver = iwl_fw_lookup_notif_ver(mvm->fw, IWL_ALWAYS_LONG_GROUP,437MCC_UPDATE_CMD, 0);438439/* Extract MCC response */440if (resp_ver >= 8) {441struct iwl_mcc_update_resp_v8 *mcc_resp_v8 = (void *)pkt->data;442443n_channels = __le32_to_cpu(mcc_resp_v8->n_channels);444if (iwl_rx_packet_payload_len(pkt) !=445struct_size(mcc_resp_v8, channels, n_channels)) {446resp_cp = ERR_PTR(-EINVAL);447goto exit;448}449resp_len = struct_size(resp_cp, channels, n_channels);450resp_cp = kzalloc(resp_len, GFP_KERNEL);451if (!resp_cp) {452resp_cp = ERR_PTR(-ENOMEM);453goto exit;454}455resp_cp->status = mcc_resp_v8->status;456resp_cp->mcc = mcc_resp_v8->mcc;457resp_cp->cap = mcc_resp_v8->cap;458resp_cp->source_id = mcc_resp_v8->source_id;459resp_cp->time = mcc_resp_v8->time;460resp_cp->geo_info = mcc_resp_v8->geo_info;461resp_cp->n_channels = mcc_resp_v8->n_channels;462memcpy(resp_cp->channels, mcc_resp_v8->channels,463n_channels * sizeof(__le32));464} else if (fw_has_capa(&mvm->fw->ucode_capa,465IWL_UCODE_TLV_CAPA_MCC_UPDATE_11AX_SUPPORT)) {466struct iwl_mcc_update_resp_v4 *mcc_resp_v4 = (void *)pkt->data;467468n_channels = __le32_to_cpu(mcc_resp_v4->n_channels);469if (iwl_rx_packet_payload_len(pkt) !=470struct_size(mcc_resp_v4, channels, n_channels)) {471resp_cp = ERR_PTR(-EINVAL);472goto exit;473}474resp_len = struct_size(resp_cp, channels, n_channels);475resp_cp = kzalloc(resp_len, GFP_KERNEL);476if (!resp_cp) {477resp_cp = ERR_PTR(-ENOMEM);478goto exit;479}480481resp_cp->status = mcc_resp_v4->status;482resp_cp->mcc = mcc_resp_v4->mcc;483resp_cp->cap = cpu_to_le32(le16_to_cpu(mcc_resp_v4->cap));484resp_cp->source_id = mcc_resp_v4->source_id;485resp_cp->time = mcc_resp_v4->time;486resp_cp->geo_info = mcc_resp_v4->geo_info;487resp_cp->n_channels = mcc_resp_v4->n_channels;488memcpy(resp_cp->channels, mcc_resp_v4->channels,489n_channels * sizeof(__le32));490} else {491struct iwl_mcc_update_resp_v3 *mcc_resp_v3 = (void *)pkt->data;492493n_channels = __le32_to_cpu(mcc_resp_v3->n_channels);494if (iwl_rx_packet_payload_len(pkt) !=495struct_size(mcc_resp_v3, channels, n_channels)) {496resp_cp = ERR_PTR(-EINVAL);497goto exit;498}499resp_len = struct_size(resp_cp, channels, n_channels);500resp_cp = kzalloc(resp_len, GFP_KERNEL);501if (!resp_cp) {502resp_cp = ERR_PTR(-ENOMEM);503goto exit;504}505506resp_cp->status = mcc_resp_v3->status;507resp_cp->mcc = mcc_resp_v3->mcc;508resp_cp->cap = cpu_to_le32(mcc_resp_v3->cap);509resp_cp->source_id = mcc_resp_v3->source_id;510resp_cp->time = mcc_resp_v3->time;511resp_cp->geo_info = mcc_resp_v3->geo_info;512resp_cp->n_channels = mcc_resp_v3->n_channels;513memcpy(resp_cp->channels, mcc_resp_v3->channels,514n_channels * sizeof(__le32));515}516517status = le32_to_cpu(resp_cp->status);518519mcc = le16_to_cpu(resp_cp->mcc);520521/* W/A for a FW/NVM issue - returns 0x00 for the world domain */522if (mcc == 0) {523mcc = 0x3030; /* "00" - world */524resp_cp->mcc = cpu_to_le16(mcc);525}526527IWL_DEBUG_LAR(mvm,528"MCC response status: 0x%x. new MCC: 0x%x ('%c%c') n_chans: %d\n",529status, mcc, mcc >> 8, mcc & 0xff, n_channels);530531exit:532iwl_free_resp(&cmd);533return resp_cp;534}535536int iwl_mvm_init_mcc(struct iwl_mvm *mvm)537{538bool tlv_lar;539bool nvm_lar;540int retval;541struct ieee80211_regdomain *regd;542char mcc[3];543544if (mvm->trans->cfg->nvm_type == IWL_NVM_EXT) {545tlv_lar = fw_has_capa(&mvm->fw->ucode_capa,546IWL_UCODE_TLV_CAPA_LAR_SUPPORT);547nvm_lar = mvm->nvm_data->lar_enabled;548if (tlv_lar != nvm_lar)549IWL_INFO(mvm,550"Conflict between TLV & NVM regarding enabling LAR (TLV = %s NVM =%s)\n",551tlv_lar ? "enabled" : "disabled",552nvm_lar ? "enabled" : "disabled");553}554555if (!iwl_mvm_is_lar_supported(mvm))556return 0;557558/*559* try to replay the last set MCC to FW. If it doesn't exist,560* queue an update to cfg80211 to retrieve the default alpha2 from FW.561*/562retval = iwl_mvm_init_fw_regd(mvm, true);563if (retval != -ENOENT)564return retval;565566/*567* Driver regulatory hint for initial update, this also informs the568* firmware we support wifi location updates.569* Disallow scans that might crash the FW while the LAR regdomain570* is not set.571*/572mvm->lar_regdom_set = false;573574regd = iwl_mvm_get_current_regdomain(mvm, NULL);575if (IS_ERR_OR_NULL(regd))576return -EIO;577578if (iwl_mvm_is_wifi_mcc_supported(mvm) &&579!iwl_bios_get_mcc(&mvm->fwrt, mcc)) {580kfree(regd);581regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, mcc,582MCC_SOURCE_BIOS, NULL);583if (IS_ERR_OR_NULL(regd))584return -EIO;585}586587retval = regulatory_set_wiphy_regd_sync(mvm->hw->wiphy, regd);588kfree(regd);589return retval;590}591592void iwl_mvm_rx_chub_update_mcc(struct iwl_mvm *mvm,593struct iwl_rx_cmd_buffer *rxb)594{595struct iwl_rx_packet *pkt = rxb_addr(rxb);596struct iwl_mcc_chub_notif *notif = (void *)pkt->data;597enum iwl_mcc_source src;598char mcc[3];599struct ieee80211_regdomain *regd;600int wgds_tbl_idx;601bool changed = false;602603lockdep_assert_held(&mvm->mutex);604605if (iwl_mvm_is_vif_assoc(mvm) && notif->source_id == MCC_SOURCE_WIFI) {606IWL_DEBUG_LAR(mvm, "Ignore mcc update while associated\n");607return;608}609610if (WARN_ON_ONCE(!iwl_mvm_is_lar_supported(mvm)))611return;612613mcc[0] = le16_to_cpu(notif->mcc) >> 8;614mcc[1] = le16_to_cpu(notif->mcc) & 0xff;615mcc[2] = '\0';616src = notif->source_id;617618IWL_DEBUG_LAR(mvm,619"RX: received chub update mcc cmd (mcc '%s' src %d)\n",620mcc, src);621regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, mcc, src, &changed);622if (IS_ERR_OR_NULL(regd))623return;624625if (!changed) {626IWL_DEBUG_LAR(mvm, "RX: No change in the regulatory data\n");627goto out;628}629630wgds_tbl_idx = iwl_mvm_get_sar_geo_profile(mvm);631if (wgds_tbl_idx < 1)632IWL_DEBUG_INFO(mvm,633"SAR WGDS is disabled or error received (%d)\n",634wgds_tbl_idx);635else636IWL_DEBUG_INFO(mvm, "SAR WGDS: geo profile %d is configured\n",637wgds_tbl_idx);638639regulatory_set_wiphy_regd(mvm->hw->wiphy, regd);640641out:642kfree(regd);643}644645646