1b3e4c0f3SLuca Coelho // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2cdda18fbSLuca Coelho /* 38f55564cSJohannes Berg * Copyright(c) 2020-2022 Intel Corporation 4cdda18fbSLuca Coelho */ 5b3e4c0f3SLuca Coelho 6b3e4c0f3SLuca Coelho #include "iwl-drv.h" 7b3e4c0f3SLuca Coelho #include "pnvm.h" 8b3e4c0f3SLuca Coelho #include "iwl-prph.h" 9b3e4c0f3SLuca Coelho #include "iwl-io.h" 10b3e4c0f3SLuca Coelho #include "fw/api/commands.h" 11b3e4c0f3SLuca Coelho #include "fw/api/nvm-reg.h" 1269725928SLuca Coelho #include "fw/api/alive.h" 1384c3c995SLuca Coelho #include "fw/uefi.h" 1469725928SLuca Coelho 1569725928SLuca Coelho struct iwl_pnvm_section { 1669725928SLuca Coelho __le32 offset; 1769725928SLuca Coelho const u8 data[]; 1869725928SLuca Coelho } __packed; 19b3e4c0f3SLuca Coelho 20b3e4c0f3SLuca Coelho static bool iwl_pnvm_complete_fn(struct iwl_notif_wait_data *notif_wait, 21b3e4c0f3SLuca Coelho struct iwl_rx_packet *pkt, void *data) 22b3e4c0f3SLuca Coelho { 23b3e4c0f3SLuca Coelho struct iwl_trans *trans = (struct iwl_trans *)data; 24b3e4c0f3SLuca Coelho struct iwl_pnvm_init_complete_ntfy *pnvm_ntf = (void *)pkt->data; 25b3e4c0f3SLuca Coelho 26b3e4c0f3SLuca Coelho IWL_DEBUG_FW(trans, 272a1d2fcfSLuca Coelho "PNVM complete notification received with status 0x%0x\n", 28b3e4c0f3SLuca Coelho le32_to_cpu(pnvm_ntf->status)); 29b3e4c0f3SLuca Coelho 30b3e4c0f3SLuca Coelho return true; 31b3e4c0f3SLuca Coelho } 32b3e4c0f3SLuca Coelho 3369725928SLuca Coelho static int iwl_pnvm_handle_section(struct iwl_trans *trans, const u8 *data, 3469725928SLuca Coelho size_t len) 3569725928SLuca Coelho { 3673c289baSBjoern A. Zeeb const struct iwl_ucode_tlv *tlv; 3769725928SLuca Coelho u32 sha1 = 0; 3869725928SLuca Coelho u16 mac_type = 0, rf_id = 0; 39*5f408503SAlon Giladi struct iwl_pnvm_image pnvm_data = {}; 400f673c16SJohannes Berg bool hw_match = false; 4169725928SLuca Coelho 4269725928SLuca Coelho IWL_DEBUG_FW(trans, "Handling PNVM section\n"); 4369725928SLuca Coelho 4469725928SLuca Coelho while (len >= sizeof(*tlv)) { 4569725928SLuca Coelho u32 tlv_len, tlv_type; 4669725928SLuca Coelho 4769725928SLuca Coelho len -= sizeof(*tlv); 4873c289baSBjoern A. Zeeb tlv = (const void *)data; 4969725928SLuca Coelho 5069725928SLuca Coelho tlv_len = le32_to_cpu(tlv->length); 5169725928SLuca Coelho tlv_type = le32_to_cpu(tlv->type); 5269725928SLuca Coelho 5369725928SLuca Coelho if (len < tlv_len) { 5469725928SLuca Coelho IWL_ERR(trans, "invalid TLV len: %zd/%u\n", 5569725928SLuca Coelho len, tlv_len); 56*5f408503SAlon Giladi return -EINVAL; 5769725928SLuca Coelho } 5869725928SLuca Coelho 5969725928SLuca Coelho data += sizeof(*tlv); 6069725928SLuca Coelho 6169725928SLuca Coelho switch (tlv_type) { 6269725928SLuca Coelho case IWL_UCODE_TLV_PNVM_VERSION: 6369725928SLuca Coelho if (tlv_len < sizeof(__le32)) { 6469725928SLuca Coelho IWL_DEBUG_FW(trans, 6569725928SLuca Coelho "Invalid size for IWL_UCODE_TLV_PNVM_VERSION (expected %zd, got %d)\n", 6669725928SLuca Coelho sizeof(__le32), tlv_len); 6769725928SLuca Coelho break; 6869725928SLuca Coelho } 6969725928SLuca Coelho 7073c289baSBjoern A. Zeeb sha1 = le32_to_cpup((const __le32 *)data); 7169725928SLuca Coelho 7269725928SLuca Coelho IWL_DEBUG_FW(trans, 7369725928SLuca Coelho "Got IWL_UCODE_TLV_PNVM_VERSION %0x\n", 7469725928SLuca Coelho sha1); 7569725928SLuca Coelho break; 7669725928SLuca Coelho case IWL_UCODE_TLV_HW_TYPE: 7769725928SLuca Coelho if (tlv_len < 2 * sizeof(__le16)) { 7869725928SLuca Coelho IWL_DEBUG_FW(trans, 7969725928SLuca Coelho "Invalid size for IWL_UCODE_TLV_HW_TYPE (expected %zd, got %d)\n", 8069725928SLuca Coelho 2 * sizeof(__le16), tlv_len); 8169725928SLuca Coelho break; 8269725928SLuca Coelho } 8369725928SLuca Coelho 840f673c16SJohannes Berg if (hw_match) 850f673c16SJohannes Berg break; 860f673c16SJohannes Berg 8773c289baSBjoern A. Zeeb mac_type = le16_to_cpup((const __le16 *)data); 8873c289baSBjoern A. Zeeb rf_id = le16_to_cpup((const __le16 *)(data + sizeof(__le16))); 8969725928SLuca Coelho 9069725928SLuca Coelho IWL_DEBUG_FW(trans, 9169725928SLuca Coelho "Got IWL_UCODE_TLV_HW_TYPE mac_type 0x%0x rf_id 0x%0x\n", 9269725928SLuca Coelho mac_type, rf_id); 9369725928SLuca Coelho 940f673c16SJohannes Berg if (mac_type == CSR_HW_REV_TYPE(trans->hw_rev) && 950f673c16SJohannes Berg rf_id == CSR_HW_RFID_TYPE(trans->hw_rf_id)) 960f673c16SJohannes Berg hw_match = true; 9769725928SLuca Coelho break; 9869725928SLuca Coelho case IWL_UCODE_TLV_SEC_RT: { 9973c289baSBjoern A. Zeeb const struct iwl_pnvm_section *section = (const void *)data; 10069725928SLuca Coelho u32 data_len = tlv_len - sizeof(*section); 10169725928SLuca Coelho 10269725928SLuca Coelho IWL_DEBUG_FW(trans, 10369725928SLuca Coelho "Got IWL_UCODE_TLV_SEC_RT len %d\n", 10469725928SLuca Coelho tlv_len); 10569725928SLuca Coelho 10669725928SLuca Coelho /* TODO: remove, this is a deprecated separator */ 10773c289baSBjoern A. Zeeb if (le32_to_cpup((const __le32 *)data) == 0xddddeeee) { 10869725928SLuca Coelho IWL_DEBUG_FW(trans, "Ignoring separator.\n"); 10969725928SLuca Coelho break; 11069725928SLuca Coelho } 11169725928SLuca Coelho 112*5f408503SAlon Giladi if (pnvm_data.n_chunks == IPC_DRAM_MAP_ENTRY_NUM_MAX) { 113*5f408503SAlon Giladi IWL_DEBUG_FW(trans, 114*5f408503SAlon Giladi "too many payloads to allocate in DRAM.\n"); 115*5f408503SAlon Giladi return -EINVAL; 116*5f408503SAlon Giladi } 117*5f408503SAlon Giladi 11869725928SLuca Coelho IWL_DEBUG_FW(trans, "Adding data (size %d)\n", 11969725928SLuca Coelho data_len); 12069725928SLuca Coelho 121*5f408503SAlon Giladi pnvm_data.chunks[pnvm_data.n_chunks].data = section->data; 122*5f408503SAlon Giladi pnvm_data.chunks[pnvm_data.n_chunks].len = data_len; 123*5f408503SAlon Giladi pnvm_data.n_chunks++; 12469725928SLuca Coelho 12569725928SLuca Coelho break; 12669725928SLuca Coelho } 12769725928SLuca Coelho case IWL_UCODE_TLV_PNVM_SKU: 12869725928SLuca Coelho IWL_DEBUG_FW(trans, 12969725928SLuca Coelho "New PNVM section started, stop parsing.\n"); 13069725928SLuca Coelho goto done; 13169725928SLuca Coelho default: 13269725928SLuca Coelho IWL_DEBUG_FW(trans, "Found TLV 0x%0x, len %d\n", 13369725928SLuca Coelho tlv_type, tlv_len); 13469725928SLuca Coelho break; 13569725928SLuca Coelho } 13669725928SLuca Coelho 13769725928SLuca Coelho len -= ALIGN(tlv_len, 4); 13869725928SLuca Coelho data += ALIGN(tlv_len, 4); 13969725928SLuca Coelho } 14069725928SLuca Coelho 14169725928SLuca Coelho done: 1420f673c16SJohannes Berg if (!hw_match) { 1430f673c16SJohannes Berg IWL_DEBUG_FW(trans, 1440f673c16SJohannes Berg "HW mismatch, skipping PNVM section (need mac_type 0x%x rf_id 0x%x)\n", 1450f673c16SJohannes Berg CSR_HW_REV_TYPE(trans->hw_rev), 1460f673c16SJohannes Berg CSR_HW_RFID_TYPE(trans->hw_rf_id)); 147*5f408503SAlon Giladi return -ENOENT; 1480f673c16SJohannes Berg } 1490f673c16SJohannes Berg 150*5f408503SAlon Giladi if (!pnvm_data.n_chunks) { 15169725928SLuca Coelho IWL_DEBUG_FW(trans, "Empty PNVM, skipping.\n"); 152*5f408503SAlon Giladi return -ENOENT; 15369725928SLuca Coelho } 15469725928SLuca Coelho 1552c5769e3SJohannes Berg IWL_INFO(trans, "loaded PNVM version %08x\n", sha1); 15669725928SLuca Coelho 157*5f408503SAlon Giladi return iwl_trans_set_pnvm(trans, &pnvm_data); 15869725928SLuca Coelho } 15969725928SLuca Coelho 16069725928SLuca Coelho static int iwl_pnvm_parse(struct iwl_trans *trans, const u8 *data, 16169725928SLuca Coelho size_t len) 16269725928SLuca Coelho { 16373c289baSBjoern A. Zeeb const struct iwl_ucode_tlv *tlv; 16469725928SLuca Coelho 16569725928SLuca Coelho IWL_DEBUG_FW(trans, "Parsing PNVM file\n"); 16669725928SLuca Coelho 16769725928SLuca Coelho while (len >= sizeof(*tlv)) { 16869725928SLuca Coelho u32 tlv_len, tlv_type; 16969725928SLuca Coelho 17069725928SLuca Coelho len -= sizeof(*tlv); 17186e8e657SJohannes Berg tlv = (const void *)data; 17269725928SLuca Coelho 17369725928SLuca Coelho tlv_len = le32_to_cpu(tlv->length); 17469725928SLuca Coelho tlv_type = le32_to_cpu(tlv->type); 17569725928SLuca Coelho 17669725928SLuca Coelho if (len < tlv_len) { 17769725928SLuca Coelho IWL_ERR(trans, "invalid TLV len: %zd/%u\n", 17869725928SLuca Coelho len, tlv_len); 17969725928SLuca Coelho return -EINVAL; 18069725928SLuca Coelho } 18169725928SLuca Coelho 18269725928SLuca Coelho if (tlv_type == IWL_UCODE_TLV_PNVM_SKU) { 18373c289baSBjoern A. Zeeb const struct iwl_sku_id *sku_id = 18473c289baSBjoern A. Zeeb (const void *)(data + sizeof(*tlv)); 18569725928SLuca Coelho 18669725928SLuca Coelho IWL_DEBUG_FW(trans, 18769725928SLuca Coelho "Got IWL_UCODE_TLV_PNVM_SKU len %d\n", 18869725928SLuca Coelho tlv_len); 18969725928SLuca Coelho IWL_DEBUG_FW(trans, "sku_id 0x%0x 0x%0x 0x%0x\n", 19069725928SLuca Coelho le32_to_cpu(sku_id->data[0]), 19169725928SLuca Coelho le32_to_cpu(sku_id->data[1]), 19269725928SLuca Coelho le32_to_cpu(sku_id->data[2])); 19369725928SLuca Coelho 194ff11a8eeSLuca Coelho data += sizeof(*tlv) + ALIGN(tlv_len, 4); 195ff11a8eeSLuca Coelho len -= ALIGN(tlv_len, 4); 196ff11a8eeSLuca Coelho 19769725928SLuca Coelho if (trans->sku_id[0] == le32_to_cpu(sku_id->data[0]) && 19869725928SLuca Coelho trans->sku_id[1] == le32_to_cpu(sku_id->data[1]) && 19969725928SLuca Coelho trans->sku_id[2] == le32_to_cpu(sku_id->data[2])) { 20069725928SLuca Coelho int ret; 20169725928SLuca Coelho 20269725928SLuca Coelho ret = iwl_pnvm_handle_section(trans, data, len); 20369725928SLuca Coelho if (!ret) 20469725928SLuca Coelho return 0; 20569725928SLuca Coelho } else { 20669725928SLuca Coelho IWL_DEBUG_FW(trans, "SKU ID didn't match!\n"); 20769725928SLuca Coelho } 20869725928SLuca Coelho } else { 20969725928SLuca Coelho data += sizeof(*tlv) + ALIGN(tlv_len, 4); 21069725928SLuca Coelho len -= ALIGN(tlv_len, 4); 21169725928SLuca Coelho } 21269725928SLuca Coelho } 21369725928SLuca Coelho 21469725928SLuca Coelho return -ENOENT; 21569725928SLuca Coelho } 21669725928SLuca Coelho 217cdda18fbSLuca Coelho static int iwl_pnvm_get_from_fs(struct iwl_trans *trans, u8 **data, size_t *len) 218b3e4c0f3SLuca Coelho { 2191c58bed4SJohannes Berg const struct firmware *pnvm; 220b05c1d14SDror Moshe char pnvm_name[MAX_PNVM_NAME]; 22145010c08SChristophe JAILLET size_t new_len; 222cdda18fbSLuca Coelho int ret; 22369725928SLuca Coelho 224b05c1d14SDror Moshe iwl_pnvm_get_fs_name(trans, pnvm_name, sizeof(pnvm_name)); 22569725928SLuca Coelho 22669725928SLuca Coelho ret = firmware_request_nowarn(&pnvm, pnvm_name, trans->dev); 22769725928SLuca Coelho if (ret) { 22869725928SLuca Coelho IWL_DEBUG_FW(trans, "PNVM file %s not found %d\n", 22969725928SLuca Coelho pnvm_name, ret); 230cdda18fbSLuca Coelho return ret; 231cdda18fbSLuca Coelho } 232cdda18fbSLuca Coelho 23345010c08SChristophe JAILLET new_len = pnvm->size; 234cdda18fbSLuca Coelho *data = kmemdup(pnvm->data, pnvm->size, GFP_KERNEL); 23545010c08SChristophe JAILLET release_firmware(pnvm); 23645010c08SChristophe JAILLET 237cdda18fbSLuca Coelho if (!*data) 238cdda18fbSLuca Coelho return -ENOMEM; 239cdda18fbSLuca Coelho 24045010c08SChristophe JAILLET *len = new_len; 241cdda18fbSLuca Coelho 242cdda18fbSLuca Coelho return 0; 243cdda18fbSLuca Coelho } 244cdda18fbSLuca Coelho 245cdda18fbSLuca Coelho int iwl_pnvm_load(struct iwl_trans *trans, 246cdda18fbSLuca Coelho struct iwl_notif_wait_data *notif_wait) 247cdda18fbSLuca Coelho { 248cdda18fbSLuca Coelho u8 *data; 249cdda18fbSLuca Coelho size_t len; 25084c3c995SLuca Coelho struct pnvm_sku_package *package; 251cdda18fbSLuca Coelho struct iwl_notification_wait pnvm_wait; 252cdda18fbSLuca Coelho static const u16 ntf_cmds[] = { WIDE_ID(REGULATORY_AND_NVM_GROUP, 253cdda18fbSLuca Coelho PNVM_INIT_COMPLETE_NTFY) }; 254cdda18fbSLuca Coelho int ret; 255cdda18fbSLuca Coelho 256cdda18fbSLuca Coelho /* if the SKU_ID is empty, there's nothing to do */ 257cdda18fbSLuca Coelho if (!trans->sku_id[0] && !trans->sku_id[1] && !trans->sku_id[2]) 258cdda18fbSLuca Coelho return 0; 259cdda18fbSLuca Coelho 260cdda18fbSLuca Coelho /* 261cdda18fbSLuca Coelho * If we already loaded (or tried to load) it before, we just 262cdda18fbSLuca Coelho * need to set it again. 263cdda18fbSLuca Coelho */ 264cdda18fbSLuca Coelho if (trans->pnvm_loaded) { 265*5f408503SAlon Giladi ret = iwl_trans_set_pnvm(trans, NULL); 266cdda18fbSLuca Coelho if (ret) 267cdda18fbSLuca Coelho return ret; 268cdda18fbSLuca Coelho goto skip_parse; 269cdda18fbSLuca Coelho } 270cdda18fbSLuca Coelho 271a1a6a4cfSLuca Coelho /* First attempt to get the PNVM from BIOS */ 27284c3c995SLuca Coelho package = iwl_uefi_get_pnvm(trans, &len); 27384c3c995SLuca Coelho if (!IS_ERR_OR_NULL(package)) { 274e864a77fSJohannes Berg if (len >= sizeof(*package)) { 2750f892441SJohannes Berg /* we need only the data */ 2760f892441SJohannes Berg len -= sizeof(*package); 27784c3c995SLuca Coelho data = kmemdup(package->data, len, GFP_KERNEL); 278e864a77fSJohannes Berg } else { 279e864a77fSJohannes Berg data = NULL; 280e864a77fSJohannes Berg } 28184c3c995SLuca Coelho 28284c3c995SLuca Coelho /* free package regardless of whether kmemdup succeeded */ 28384c3c995SLuca Coelho kfree(package); 28484c3c995SLuca Coelho 2850f892441SJohannes Berg if (data) 286a1a6a4cfSLuca Coelho goto parse; 28784c3c995SLuca Coelho } 288a1a6a4cfSLuca Coelho 289a1a6a4cfSLuca Coelho /* If it's not available, try from the filesystem */ 290cdda18fbSLuca Coelho ret = iwl_pnvm_get_from_fs(trans, &data, &len); 291cdda18fbSLuca Coelho if (ret) { 29282a08d0cSJohannes Berg /* 29382a08d0cSJohannes Berg * Pretend we've loaded it - at least we've tried and 29482a08d0cSJohannes Berg * couldn't load it at all, so there's no point in 29582a08d0cSJohannes Berg * trying again over and over. 29682a08d0cSJohannes Berg */ 29782a08d0cSJohannes Berg trans->pnvm_loaded = true; 29869725928SLuca Coelho 299cdda18fbSLuca Coelho goto skip_parse; 3001c58bed4SJohannes Berg } 301b3e4c0f3SLuca Coelho 302a1a6a4cfSLuca Coelho parse: 303cdda18fbSLuca Coelho iwl_pnvm_parse(trans, data, len); 304cdda18fbSLuca Coelho 305cdda18fbSLuca Coelho kfree(data); 306cdda18fbSLuca Coelho 307cdda18fbSLuca Coelho skip_parse: 3089dad325fSLuca Coelho /* now try to get the reduce power table, if not loaded yet */ 3099dad325fSLuca Coelho if (!trans->reduce_power_loaded) { 3109dad325fSLuca Coelho data = iwl_uefi_get_reduced_power(trans, &len); 3119dad325fSLuca Coelho if (IS_ERR_OR_NULL(data)) { 3129dad325fSLuca Coelho /* 3139dad325fSLuca Coelho * Pretend we've loaded it - at least we've tried and 3149dad325fSLuca Coelho * couldn't load it at all, so there's no point in 3159dad325fSLuca Coelho * trying again over and over. 3169dad325fSLuca Coelho */ 3179dad325fSLuca Coelho trans->reduce_power_loaded = true; 3188f55564cSJohannes Berg } else { 3199dad325fSLuca Coelho ret = iwl_trans_set_reduce_power(trans, data, len); 3209dad325fSLuca Coelho if (ret) 3219dad325fSLuca Coelho IWL_DEBUG_FW(trans, 3229dad325fSLuca Coelho "Failed to set reduce power table %d\n", 3239dad325fSLuca Coelho ret); 3249dad325fSLuca Coelho kfree(data); 3258f55564cSJohannes Berg } 3268f55564cSJohannes Berg } 3279dad325fSLuca Coelho 328b3e4c0f3SLuca Coelho iwl_init_notification_wait(notif_wait, &pnvm_wait, 329b3e4c0f3SLuca Coelho ntf_cmds, ARRAY_SIZE(ntf_cmds), 330b3e4c0f3SLuca Coelho iwl_pnvm_complete_fn, trans); 331b3e4c0f3SLuca Coelho 332b3e4c0f3SLuca Coelho /* kick the doorbell */ 333b3e4c0f3SLuca Coelho iwl_write_umac_prph(trans, UREG_DOORBELL_TO_ISR6, 334b3e4c0f3SLuca Coelho UREG_DOORBELL_TO_ISR6_PNVM); 335b3e4c0f3SLuca Coelho 336b3e4c0f3SLuca Coelho return iwl_wait_notification(notif_wait, &pnvm_wait, 337b3e4c0f3SLuca Coelho MVM_UCODE_PNVM_TIMEOUT); 338b3e4c0f3SLuca Coelho } 339b3e4c0f3SLuca Coelho IWL_EXPORT_SYMBOL(iwl_pnvm_load); 340