Path: blob/main/sys/contrib/dev/iwlwifi/fw/pnvm.c
108088 views
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause1/*2* Copyright(c) 2020-2025 Intel Corporation3*/45#include "iwl-drv.h"6#include "pnvm.h"7#include "iwl-prph.h"8#include "iwl-io.h"9#include "fw/api/commands.h"10#include "fw/api/nvm-reg.h"11#include "fw/api/alive.h"12#include "fw/uefi.h"13#include "fw/img.h"1415#define IWL_PNVM_REDUCED_CAP_BIT BIT(25)1617struct iwl_pnvm_section {18__le32 offset;19const u8 data[];20} __packed;2122static bool iwl_pnvm_complete_fn(struct iwl_notif_wait_data *notif_wait,23struct iwl_rx_packet *pkt, void *data)24{25struct iwl_trans *trans = (struct iwl_trans *)data;26struct iwl_pnvm_init_complete_ntfy *pnvm_ntf = (void *)pkt->data;2728IWL_DEBUG_FW(trans,29"PNVM complete notification received with status 0x%0x\n",30le32_to_cpu(pnvm_ntf->status));3132return true;33}3435static int iwl_pnvm_handle_section(struct iwl_trans *trans, const u8 *data,36size_t len,37struct iwl_pnvm_image *pnvm_data)38{39const struct iwl_ucode_tlv *tlv;40u32 sha1 = 0;41u16 mac_type = 0, rf_id = 0;42bool hw_match = false;4344IWL_DEBUG_FW(trans, "Handling PNVM section\n");4546memset(pnvm_data, 0, sizeof(*pnvm_data));4748while (len >= sizeof(*tlv)) {49u32 tlv_len, tlv_type;5051len -= sizeof(*tlv);52tlv = (const void *)data;5354tlv_len = le32_to_cpu(tlv->length);55tlv_type = le32_to_cpu(tlv->type);5657if (len < tlv_len) {58IWL_ERR(trans, "invalid TLV len: %zd/%u\n",59len, tlv_len);60return -EINVAL;61}6263data += sizeof(*tlv);6465switch (tlv_type) {66case IWL_UCODE_TLV_PNVM_VERSION:67if (tlv_len < sizeof(__le32)) {68IWL_DEBUG_FW(trans,69"Invalid size for IWL_UCODE_TLV_PNVM_VERSION (expected %zd, got %d)\n",70sizeof(__le32), tlv_len);71break;72}7374sha1 = le32_to_cpup((const __le32 *)data);7576IWL_DEBUG_FW(trans,77"Got IWL_UCODE_TLV_PNVM_VERSION %0x\n",78sha1);79pnvm_data->version = sha1;80break;81case IWL_UCODE_TLV_HW_TYPE:82if (tlv_len < 2 * sizeof(__le16)) {83IWL_DEBUG_FW(trans,84"Invalid size for IWL_UCODE_TLV_HW_TYPE (expected %zd, got %d)\n",852 * sizeof(__le16), tlv_len);86break;87}8889if (hw_match)90break;9192mac_type = le16_to_cpup((const __le16 *)data);93rf_id = le16_to_cpup((const __le16 *)(data + sizeof(__le16)));9495IWL_DEBUG_FW(trans,96"Got IWL_UCODE_TLV_HW_TYPE mac_type 0x%0x rf_id 0x%0x\n",97mac_type, rf_id);9899if (mac_type == CSR_HW_REV_TYPE(trans->info.hw_rev) &&100rf_id == CSR_HW_RFID_TYPE(trans->info.hw_rf_id))101hw_match = true;102break;103case IWL_UCODE_TLV_SEC_RT: {104const struct iwl_pnvm_section *section = (const void *)data;105u32 data_len = tlv_len - sizeof(*section);106107IWL_DEBUG_FW(trans,108"Got IWL_UCODE_TLV_SEC_RT len %d\n",109tlv_len);110111/* TODO: remove, this is a deprecated separator */112if (le32_to_cpup((const __le32 *)data) == 0xddddeeee) {113IWL_DEBUG_FW(trans, "Ignoring separator.\n");114break;115}116117if (pnvm_data->n_chunks == IPC_DRAM_MAP_ENTRY_NUM_MAX) {118IWL_DEBUG_FW(trans,119"too many payloads to allocate in DRAM.\n");120return -EINVAL;121}122123IWL_DEBUG_FW(trans, "Adding data (size %d)\n",124data_len);125126pnvm_data->chunks[pnvm_data->n_chunks].data = section->data;127pnvm_data->chunks[pnvm_data->n_chunks].len = data_len;128pnvm_data->n_chunks++;129130break;131}132case IWL_UCODE_TLV_MEM_DESC:133if (iwl_uefi_handle_tlv_mem_desc(trans, data, tlv_len,134pnvm_data))135return -EINVAL;136break;137case IWL_UCODE_TLV_PNVM_SKU:138IWL_DEBUG_FW(trans,139"New PNVM section started, stop parsing.\n");140goto done;141default:142IWL_DEBUG_FW(trans, "Found TLV 0x%0x, len %d\n",143tlv_type, tlv_len);144break;145}146147len -= ALIGN(tlv_len, 4);148data += ALIGN(tlv_len, 4);149}150151done:152if (!hw_match) {153IWL_DEBUG_FW(trans,154"HW mismatch, skipping PNVM section (need mac_type 0x%x rf_id 0x%x)\n",155CSR_HW_REV_TYPE(trans->info.hw_rev),156CSR_HW_RFID_TYPE(trans->info.hw_rf_id));157return -ENOENT;158}159160if (!pnvm_data->n_chunks) {161IWL_DEBUG_FW(trans, "Empty PNVM, skipping.\n");162return -ENOENT;163}164165return 0;166}167168static int iwl_pnvm_parse(struct iwl_trans *trans, const u8 *data,169size_t len,170struct iwl_pnvm_image *pnvm_data,171__le32 sku_id[3])172{173const struct iwl_ucode_tlv *tlv;174175IWL_DEBUG_FW(trans, "Parsing PNVM file\n");176177while (len >= sizeof(*tlv)) {178u32 tlv_len, tlv_type;179u32 rf_type;180181len -= sizeof(*tlv);182tlv = (const void *)data;183184tlv_len = le32_to_cpu(tlv->length);185tlv_type = le32_to_cpu(tlv->type);186187if (len < tlv_len) {188IWL_ERR(trans, "invalid TLV len: %zd/%u\n",189len, tlv_len);190return -EINVAL;191}192193if (tlv_type == IWL_UCODE_TLV_PNVM_SKU) {194const struct iwl_sku_id *tlv_sku_id =195(const void *)(data + sizeof(*tlv));196197IWL_DEBUG_FW(trans,198"Got IWL_UCODE_TLV_PNVM_SKU len %d\n",199tlv_len);200IWL_DEBUG_FW(trans, "sku_id 0x%0x 0x%0x 0x%0x\n",201le32_to_cpu(tlv_sku_id->data[0]),202le32_to_cpu(tlv_sku_id->data[1]),203le32_to_cpu(tlv_sku_id->data[2]));204205data += sizeof(*tlv) + ALIGN(tlv_len, 4);206len -= ALIGN(tlv_len, 4);207208trans->reduced_cap_sku = false;209rf_type = CSR_HW_RFID_TYPE(trans->info.hw_rf_id);210if ((sku_id[0] & cpu_to_le32(IWL_PNVM_REDUCED_CAP_BIT)) &&211rf_type == IWL_CFG_RF_TYPE_FM)212trans->reduced_cap_sku = true;213214IWL_DEBUG_FW(trans,215"Reduced SKU device %d\n",216trans->reduced_cap_sku);217218if (sku_id[0] == tlv_sku_id->data[0] &&219sku_id[1] == tlv_sku_id->data[1] &&220sku_id[2] == tlv_sku_id->data[2]) {221int ret;222223ret = iwl_pnvm_handle_section(trans, data, len,224pnvm_data);225if (!ret)226return 0;227} else {228IWL_DEBUG_FW(trans, "SKU ID didn't match!\n");229}230} else {231data += sizeof(*tlv) + ALIGN(tlv_len, 4);232len -= ALIGN(tlv_len, 4);233}234}235236return -ENOENT;237}238239static int iwl_pnvm_get_from_fs(struct iwl_trans *trans, u8 **data, size_t *len)240{241const struct firmware *pnvm;242char pnvm_name[MAX_PNVM_NAME];243size_t new_len;244int ret;245246iwl_pnvm_get_fs_name(trans, pnvm_name, sizeof(pnvm_name));247248ret = firmware_request_nowarn(&pnvm, pnvm_name, trans->dev);249if (ret) {250IWL_DEBUG_FW(trans, "PNVM file %s not found %d\n",251pnvm_name, ret);252return ret;253}254255new_len = pnvm->size;256*data = kvmemdup(pnvm->data, pnvm->size, GFP_KERNEL);257release_firmware(pnvm);258259if (!*data)260return -ENOMEM;261262*len = new_len;263264return 0;265}266267static const u8 *iwl_get_pnvm_image(struct iwl_trans *trans_p, size_t *len,268__le32 sku_id[3], const struct iwl_fw *fw)269{270struct pnvm_sku_package *package;271u8 *image = NULL;272273/* Get PNVM from BIOS for non-Intel SKU */274if (sku_id[2]) {275package = iwl_uefi_get_pnvm(trans_p, len);276if (!IS_ERR_OR_NULL(package)) {277if (*len >= sizeof(*package)) {278/* we need only the data */279*len -= sizeof(*package);280image = kvmemdup(package->data,281*len, GFP_KERNEL);282}283/*284* free package regardless of whether kmemdup285* succeeded286*/287kfree(package);288if (image)289return image;290}291}292293if (fw->pnvm_data) {294*len = fw->pnvm_size;295296return fw->pnvm_data;297}298299/* If it's not available, or for Intel SKU, try from the filesystem */300if (iwl_pnvm_get_from_fs(trans_p, &image, len))301return NULL;302return image;303}304305static void306iwl_pnvm_load_pnvm_to_trans(struct iwl_trans *trans,307const struct iwl_fw *fw,308__le32 sku_id[3])309{310struct iwl_pnvm_image *pnvm_data = NULL;311const u8 *data = NULL;312size_t length;313int ret;314315/* failed to get/parse the image in the past, no use trying again */316if (trans->fail_to_parse_pnvm_image)317return;318319if (trans->pnvm_loaded)320goto set;321322data = iwl_get_pnvm_image(trans, &length, sku_id, fw);323if (!data) {324trans->fail_to_parse_pnvm_image = true;325return;326}327328pnvm_data = kzalloc(sizeof(*pnvm_data), GFP_KERNEL);329if (!pnvm_data)330goto free;331332ret = iwl_pnvm_parse(trans, data, length, pnvm_data, sku_id);333if (ret) {334trans->fail_to_parse_pnvm_image = true;335goto free;336}337338ret = iwl_trans_load_pnvm(trans, pnvm_data, &fw->ucode_capa);339if (ret)340goto free;341IWL_DEBUG_INFO(trans, "loaded PNVM version %08x\n", pnvm_data->version);342343set:344iwl_trans_set_pnvm(trans, &fw->ucode_capa);345free:346/* free only if it was allocated, i.e. not just embedded PNVM data */347if (data != fw->pnvm_data)348kvfree(data);349kfree(pnvm_data);350}351352static void353iwl_pnvm_load_reduce_power_to_trans(struct iwl_trans *trans,354const struct iwl_ucode_capabilities *capa,355__le32 sku_id[3])356{357struct iwl_pnvm_image *pnvm_data = NULL;358u8 *data = NULL;359size_t length;360int ret;361362if (trans->failed_to_load_reduce_power_image)363return;364365if (trans->reduce_power_loaded)366goto set;367368data = iwl_uefi_get_reduced_power(trans, &length);369if (IS_ERR(data)) {370trans->failed_to_load_reduce_power_image = true;371return;372}373374pnvm_data = kzalloc(sizeof(*pnvm_data), GFP_KERNEL);375if (!pnvm_data)376goto free;377378ret = iwl_uefi_reduce_power_parse(trans, data, length, pnvm_data,379sku_id);380if (ret) {381trans->failed_to_load_reduce_power_image = true;382goto free;383}384385ret = iwl_trans_load_reduce_power(trans, pnvm_data, capa);386if (ret) {387IWL_DEBUG_FW(trans,388"Failed to load reduce power table %d\n",389ret);390trans->failed_to_load_reduce_power_image = true;391goto free;392}393394set:395iwl_trans_set_reduce_power(trans, capa);396free:397kfree(data);398kfree(pnvm_data);399}400401int iwl_pnvm_load(struct iwl_trans *trans,402struct iwl_notif_wait_data *notif_wait,403const struct iwl_fw *fw, __le32 sku_id[3])404{405struct iwl_notification_wait pnvm_wait;406static const u16 ntf_cmds[] = { WIDE_ID(REGULATORY_AND_NVM_GROUP,407PNVM_INIT_COMPLETE_NTFY) };408409/* if the SKU_ID is empty, there's nothing to do */410if (!sku_id[0] && !sku_id[1] && !sku_id[2])411return 0;412413iwl_pnvm_load_pnvm_to_trans(trans, fw, sku_id);414iwl_pnvm_load_reduce_power_to_trans(trans, &fw->ucode_capa, sku_id);415416iwl_init_notification_wait(notif_wait, &pnvm_wait,417ntf_cmds, ARRAY_SIZE(ntf_cmds),418iwl_pnvm_complete_fn, trans);419420/* kick the doorbell */421iwl_write_umac_prph(trans, UREG_DOORBELL_TO_ISR6,422UREG_DOORBELL_TO_ISR6_PNVM);423424return iwl_wait_notification(notif_wait, &pnvm_wait,425MVM_UCODE_PNVM_TIMEOUT);426}427IWL_EXPORT_SYMBOL(iwl_pnvm_load);428429430