1b3e4c0f3SLuca Coelho // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2cdda18fbSLuca Coelho /* 3cdda18fbSLuca Coelho * Copyright(c) 2020-2021 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; 3969725928SLuca Coelho u8 *pnvm_data = NULL, *tmp; 400f673c16SJohannes Berg bool hw_match = false; 4169725928SLuca Coelho u32 size = 0; 4269725928SLuca Coelho int ret; 4369725928SLuca Coelho 4469725928SLuca Coelho IWL_DEBUG_FW(trans, "Handling PNVM section\n"); 4569725928SLuca Coelho 4669725928SLuca Coelho while (len >= sizeof(*tlv)) { 4769725928SLuca Coelho u32 tlv_len, tlv_type; 4869725928SLuca Coelho 4969725928SLuca Coelho len -= sizeof(*tlv); 5073c289baSBjoern A. Zeeb tlv = (const void *)data; 5169725928SLuca Coelho 5269725928SLuca Coelho tlv_len = le32_to_cpu(tlv->length); 5369725928SLuca Coelho tlv_type = le32_to_cpu(tlv->type); 5469725928SLuca Coelho 5569725928SLuca Coelho if (len < tlv_len) { 5669725928SLuca Coelho IWL_ERR(trans, "invalid TLV len: %zd/%u\n", 5769725928SLuca Coelho len, tlv_len); 5869725928SLuca Coelho ret = -EINVAL; 5969725928SLuca Coelho goto out; 6069725928SLuca Coelho } 6169725928SLuca Coelho 6269725928SLuca Coelho data += sizeof(*tlv); 6369725928SLuca Coelho 6469725928SLuca Coelho switch (tlv_type) { 6569725928SLuca Coelho case IWL_UCODE_TLV_PNVM_VERSION: 6669725928SLuca Coelho if (tlv_len < sizeof(__le32)) { 6769725928SLuca Coelho IWL_DEBUG_FW(trans, 6869725928SLuca Coelho "Invalid size for IWL_UCODE_TLV_PNVM_VERSION (expected %zd, got %d)\n", 6969725928SLuca Coelho sizeof(__le32), tlv_len); 7069725928SLuca Coelho break; 7169725928SLuca Coelho } 7269725928SLuca Coelho 7373c289baSBjoern A. Zeeb sha1 = le32_to_cpup((const __le32 *)data); 7469725928SLuca Coelho 7569725928SLuca Coelho IWL_DEBUG_FW(trans, 7669725928SLuca Coelho "Got IWL_UCODE_TLV_PNVM_VERSION %0x\n", 7769725928SLuca Coelho sha1); 7869725928SLuca Coelho break; 7969725928SLuca Coelho case IWL_UCODE_TLV_HW_TYPE: 8069725928SLuca Coelho if (tlv_len < 2 * sizeof(__le16)) { 8169725928SLuca Coelho IWL_DEBUG_FW(trans, 8269725928SLuca Coelho "Invalid size for IWL_UCODE_TLV_HW_TYPE (expected %zd, got %d)\n", 8369725928SLuca Coelho 2 * sizeof(__le16), tlv_len); 8469725928SLuca Coelho break; 8569725928SLuca Coelho } 8669725928SLuca Coelho 870f673c16SJohannes Berg if (hw_match) 880f673c16SJohannes Berg break; 890f673c16SJohannes Berg 9073c289baSBjoern A. Zeeb mac_type = le16_to_cpup((const __le16 *)data); 9173c289baSBjoern A. Zeeb rf_id = le16_to_cpup((const __le16 *)(data + sizeof(__le16))); 9269725928SLuca Coelho 9369725928SLuca Coelho IWL_DEBUG_FW(trans, 9469725928SLuca Coelho "Got IWL_UCODE_TLV_HW_TYPE mac_type 0x%0x rf_id 0x%0x\n", 9569725928SLuca Coelho mac_type, rf_id); 9669725928SLuca Coelho 970f673c16SJohannes Berg if (mac_type == CSR_HW_REV_TYPE(trans->hw_rev) && 980f673c16SJohannes Berg rf_id == CSR_HW_RFID_TYPE(trans->hw_rf_id)) 990f673c16SJohannes Berg hw_match = true; 10069725928SLuca Coelho break; 10169725928SLuca Coelho case IWL_UCODE_TLV_SEC_RT: { 10273c289baSBjoern A. Zeeb const struct iwl_pnvm_section *section = (const void *)data; 10369725928SLuca Coelho u32 data_len = tlv_len - sizeof(*section); 10469725928SLuca Coelho 10569725928SLuca Coelho IWL_DEBUG_FW(trans, 10669725928SLuca Coelho "Got IWL_UCODE_TLV_SEC_RT len %d\n", 10769725928SLuca Coelho tlv_len); 10869725928SLuca Coelho 10969725928SLuca Coelho /* TODO: remove, this is a deprecated separator */ 11073c289baSBjoern A. Zeeb if (le32_to_cpup((const __le32 *)data) == 0xddddeeee) { 11169725928SLuca Coelho IWL_DEBUG_FW(trans, "Ignoring separator.\n"); 11269725928SLuca Coelho break; 11369725928SLuca Coelho } 11469725928SLuca Coelho 11569725928SLuca Coelho IWL_DEBUG_FW(trans, "Adding data (size %d)\n", 11669725928SLuca Coelho data_len); 11769725928SLuca Coelho 11869725928SLuca Coelho tmp = krealloc(pnvm_data, size + data_len, GFP_KERNEL); 11969725928SLuca Coelho if (!tmp) { 12069725928SLuca Coelho IWL_DEBUG_FW(trans, 12169725928SLuca Coelho "Couldn't allocate (more) pnvm_data\n"); 12269725928SLuca Coelho 12369725928SLuca Coelho ret = -ENOMEM; 12469725928SLuca Coelho goto out; 12569725928SLuca Coelho } 12669725928SLuca Coelho 12769725928SLuca Coelho pnvm_data = tmp; 12869725928SLuca Coelho 12969725928SLuca Coelho memcpy(pnvm_data + size, section->data, data_len); 13069725928SLuca Coelho 13169725928SLuca Coelho size += data_len; 13269725928SLuca Coelho 13369725928SLuca Coelho break; 13469725928SLuca Coelho } 13569725928SLuca Coelho case IWL_UCODE_TLV_PNVM_SKU: 13669725928SLuca Coelho IWL_DEBUG_FW(trans, 13769725928SLuca Coelho "New PNVM section started, stop parsing.\n"); 13869725928SLuca Coelho goto done; 13969725928SLuca Coelho default: 14069725928SLuca Coelho IWL_DEBUG_FW(trans, "Found TLV 0x%0x, len %d\n", 14169725928SLuca Coelho tlv_type, tlv_len); 14269725928SLuca Coelho break; 14369725928SLuca Coelho } 14469725928SLuca Coelho 14569725928SLuca Coelho len -= ALIGN(tlv_len, 4); 14669725928SLuca Coelho data += ALIGN(tlv_len, 4); 14769725928SLuca Coelho } 14869725928SLuca Coelho 14969725928SLuca Coelho done: 1500f673c16SJohannes Berg if (!hw_match) { 1510f673c16SJohannes Berg IWL_DEBUG_FW(trans, 1520f673c16SJohannes Berg "HW mismatch, skipping PNVM section (need mac_type 0x%x rf_id 0x%x)\n", 1530f673c16SJohannes Berg CSR_HW_REV_TYPE(trans->hw_rev), 1540f673c16SJohannes Berg CSR_HW_RFID_TYPE(trans->hw_rf_id)); 1550f673c16SJohannes Berg ret = -ENOENT; 1560f673c16SJohannes Berg goto out; 1570f673c16SJohannes Berg } 1580f673c16SJohannes Berg 15969725928SLuca Coelho if (!size) { 16069725928SLuca Coelho IWL_DEBUG_FW(trans, "Empty PNVM, skipping.\n"); 16169725928SLuca Coelho ret = -ENOENT; 16269725928SLuca Coelho goto out; 16369725928SLuca Coelho } 16469725928SLuca Coelho 1652c5769e3SJohannes Berg IWL_INFO(trans, "loaded PNVM version %08x\n", sha1); 16669725928SLuca Coelho 16769725928SLuca Coelho ret = iwl_trans_set_pnvm(trans, pnvm_data, size); 16869725928SLuca Coelho out: 16969725928SLuca Coelho kfree(pnvm_data); 17069725928SLuca Coelho return ret; 17169725928SLuca Coelho } 17269725928SLuca Coelho 17369725928SLuca Coelho static int iwl_pnvm_parse(struct iwl_trans *trans, const u8 *data, 17469725928SLuca Coelho size_t len) 17569725928SLuca Coelho { 17673c289baSBjoern A. Zeeb const struct iwl_ucode_tlv *tlv; 17769725928SLuca Coelho 17869725928SLuca Coelho IWL_DEBUG_FW(trans, "Parsing PNVM file\n"); 17969725928SLuca Coelho 18069725928SLuca Coelho while (len >= sizeof(*tlv)) { 18169725928SLuca Coelho u32 tlv_len, tlv_type; 18269725928SLuca Coelho 18369725928SLuca Coelho len -= sizeof(*tlv); 184*86e8e657SJohannes Berg tlv = (const void *)data; 18569725928SLuca Coelho 18669725928SLuca Coelho tlv_len = le32_to_cpu(tlv->length); 18769725928SLuca Coelho tlv_type = le32_to_cpu(tlv->type); 18869725928SLuca Coelho 18969725928SLuca Coelho if (len < tlv_len) { 19069725928SLuca Coelho IWL_ERR(trans, "invalid TLV len: %zd/%u\n", 19169725928SLuca Coelho len, tlv_len); 19269725928SLuca Coelho return -EINVAL; 19369725928SLuca Coelho } 19469725928SLuca Coelho 19569725928SLuca Coelho if (tlv_type == IWL_UCODE_TLV_PNVM_SKU) { 19673c289baSBjoern A. Zeeb const struct iwl_sku_id *sku_id = 19773c289baSBjoern A. Zeeb (const void *)(data + sizeof(*tlv)); 19869725928SLuca Coelho 19969725928SLuca Coelho IWL_DEBUG_FW(trans, 20069725928SLuca Coelho "Got IWL_UCODE_TLV_PNVM_SKU len %d\n", 20169725928SLuca Coelho tlv_len); 20269725928SLuca Coelho IWL_DEBUG_FW(trans, "sku_id 0x%0x 0x%0x 0x%0x\n", 20369725928SLuca Coelho le32_to_cpu(sku_id->data[0]), 20469725928SLuca Coelho le32_to_cpu(sku_id->data[1]), 20569725928SLuca Coelho le32_to_cpu(sku_id->data[2])); 20669725928SLuca Coelho 207ff11a8eeSLuca Coelho data += sizeof(*tlv) + ALIGN(tlv_len, 4); 208ff11a8eeSLuca Coelho len -= ALIGN(tlv_len, 4); 209ff11a8eeSLuca Coelho 21069725928SLuca Coelho if (trans->sku_id[0] == le32_to_cpu(sku_id->data[0]) && 21169725928SLuca Coelho trans->sku_id[1] == le32_to_cpu(sku_id->data[1]) && 21269725928SLuca Coelho trans->sku_id[2] == le32_to_cpu(sku_id->data[2])) { 21369725928SLuca Coelho int ret; 21469725928SLuca Coelho 21569725928SLuca Coelho ret = iwl_pnvm_handle_section(trans, data, len); 21669725928SLuca Coelho if (!ret) 21769725928SLuca Coelho return 0; 21869725928SLuca Coelho } else { 21969725928SLuca Coelho IWL_DEBUG_FW(trans, "SKU ID didn't match!\n"); 22069725928SLuca Coelho } 22169725928SLuca Coelho } else { 22269725928SLuca Coelho data += sizeof(*tlv) + ALIGN(tlv_len, 4); 22369725928SLuca Coelho len -= ALIGN(tlv_len, 4); 22469725928SLuca Coelho } 22569725928SLuca Coelho } 22669725928SLuca Coelho 22769725928SLuca Coelho return -ENOENT; 22869725928SLuca Coelho } 22969725928SLuca Coelho 230cdda18fbSLuca Coelho static int iwl_pnvm_get_from_fs(struct iwl_trans *trans, u8 **data, size_t *len) 231b3e4c0f3SLuca Coelho { 2321c58bed4SJohannes Berg const struct firmware *pnvm; 233b05c1d14SDror Moshe char pnvm_name[MAX_PNVM_NAME]; 23445010c08SChristophe JAILLET size_t new_len; 235cdda18fbSLuca Coelho int ret; 23669725928SLuca Coelho 237b05c1d14SDror Moshe iwl_pnvm_get_fs_name(trans, pnvm_name, sizeof(pnvm_name)); 23869725928SLuca Coelho 23969725928SLuca Coelho ret = firmware_request_nowarn(&pnvm, pnvm_name, trans->dev); 24069725928SLuca Coelho if (ret) { 24169725928SLuca Coelho IWL_DEBUG_FW(trans, "PNVM file %s not found %d\n", 24269725928SLuca Coelho pnvm_name, ret); 243cdda18fbSLuca Coelho return ret; 244cdda18fbSLuca Coelho } 245cdda18fbSLuca Coelho 24645010c08SChristophe JAILLET new_len = pnvm->size; 247cdda18fbSLuca Coelho *data = kmemdup(pnvm->data, pnvm->size, GFP_KERNEL); 24845010c08SChristophe JAILLET release_firmware(pnvm); 24945010c08SChristophe JAILLET 250cdda18fbSLuca Coelho if (!*data) 251cdda18fbSLuca Coelho return -ENOMEM; 252cdda18fbSLuca Coelho 25345010c08SChristophe JAILLET *len = new_len; 254cdda18fbSLuca Coelho 255cdda18fbSLuca Coelho return 0; 256cdda18fbSLuca Coelho } 257cdda18fbSLuca Coelho 258cdda18fbSLuca Coelho int iwl_pnvm_load(struct iwl_trans *trans, 259cdda18fbSLuca Coelho struct iwl_notif_wait_data *notif_wait) 260cdda18fbSLuca Coelho { 261cdda18fbSLuca Coelho u8 *data; 262cdda18fbSLuca Coelho size_t len; 26384c3c995SLuca Coelho struct pnvm_sku_package *package; 264cdda18fbSLuca Coelho struct iwl_notification_wait pnvm_wait; 265cdda18fbSLuca Coelho static const u16 ntf_cmds[] = { WIDE_ID(REGULATORY_AND_NVM_GROUP, 266cdda18fbSLuca Coelho PNVM_INIT_COMPLETE_NTFY) }; 267cdda18fbSLuca Coelho int ret; 268cdda18fbSLuca Coelho 269cdda18fbSLuca Coelho /* if the SKU_ID is empty, there's nothing to do */ 270cdda18fbSLuca Coelho if (!trans->sku_id[0] && !trans->sku_id[1] && !trans->sku_id[2]) 271cdda18fbSLuca Coelho return 0; 272cdda18fbSLuca Coelho 273cdda18fbSLuca Coelho /* 274cdda18fbSLuca Coelho * If we already loaded (or tried to load) it before, we just 275cdda18fbSLuca Coelho * need to set it again. 276cdda18fbSLuca Coelho */ 277cdda18fbSLuca Coelho if (trans->pnvm_loaded) { 278cdda18fbSLuca Coelho ret = iwl_trans_set_pnvm(trans, NULL, 0); 279cdda18fbSLuca Coelho if (ret) 280cdda18fbSLuca Coelho return ret; 281cdda18fbSLuca Coelho goto skip_parse; 282cdda18fbSLuca Coelho } 283cdda18fbSLuca Coelho 284a1a6a4cfSLuca Coelho /* First attempt to get the PNVM from BIOS */ 28584c3c995SLuca Coelho package = iwl_uefi_get_pnvm(trans, &len); 28684c3c995SLuca Coelho if (!IS_ERR_OR_NULL(package)) { 287e864a77fSJohannes Berg if (len >= sizeof(*package)) { 2880f892441SJohannes Berg /* we need only the data */ 2890f892441SJohannes Berg len -= sizeof(*package); 29084c3c995SLuca Coelho data = kmemdup(package->data, len, GFP_KERNEL); 291e864a77fSJohannes Berg } else { 292e864a77fSJohannes Berg data = NULL; 293e864a77fSJohannes Berg } 29484c3c995SLuca Coelho 29584c3c995SLuca Coelho /* free package regardless of whether kmemdup succeeded */ 29684c3c995SLuca Coelho kfree(package); 29784c3c995SLuca Coelho 2980f892441SJohannes Berg if (data) 299a1a6a4cfSLuca Coelho goto parse; 30084c3c995SLuca Coelho } 301a1a6a4cfSLuca Coelho 302a1a6a4cfSLuca Coelho /* If it's not available, try from the filesystem */ 303cdda18fbSLuca Coelho ret = iwl_pnvm_get_from_fs(trans, &data, &len); 304cdda18fbSLuca Coelho if (ret) { 30582a08d0cSJohannes Berg /* 30682a08d0cSJohannes Berg * Pretend we've loaded it - at least we've tried and 30782a08d0cSJohannes Berg * couldn't load it at all, so there's no point in 30882a08d0cSJohannes Berg * trying again over and over. 30982a08d0cSJohannes Berg */ 31082a08d0cSJohannes Berg trans->pnvm_loaded = true; 31169725928SLuca Coelho 312cdda18fbSLuca Coelho goto skip_parse; 3131c58bed4SJohannes Berg } 314b3e4c0f3SLuca Coelho 315a1a6a4cfSLuca Coelho parse: 316cdda18fbSLuca Coelho iwl_pnvm_parse(trans, data, len); 317cdda18fbSLuca Coelho 318cdda18fbSLuca Coelho kfree(data); 319cdda18fbSLuca Coelho 320cdda18fbSLuca Coelho skip_parse: 3219dad325fSLuca Coelho data = NULL; 3229dad325fSLuca Coelho /* now try to get the reduce power table, if not loaded yet */ 3239dad325fSLuca Coelho if (!trans->reduce_power_loaded) { 3249dad325fSLuca Coelho data = iwl_uefi_get_reduced_power(trans, &len); 3259dad325fSLuca Coelho if (IS_ERR_OR_NULL(data)) { 3269dad325fSLuca Coelho /* 3279dad325fSLuca Coelho * Pretend we've loaded it - at least we've tried and 3289dad325fSLuca Coelho * couldn't load it at all, so there's no point in 3299dad325fSLuca Coelho * trying again over and over. 3309dad325fSLuca Coelho */ 3319dad325fSLuca Coelho trans->reduce_power_loaded = true; 3329dad325fSLuca Coelho 3339dad325fSLuca Coelho goto skip_reduce_power; 3349dad325fSLuca Coelho } 3359dad325fSLuca Coelho } 3369dad325fSLuca Coelho 3379dad325fSLuca Coelho ret = iwl_trans_set_reduce_power(trans, data, len); 3389dad325fSLuca Coelho if (ret) 3399dad325fSLuca Coelho IWL_DEBUG_FW(trans, 3409dad325fSLuca Coelho "Failed to set reduce power table %d\n", 3419dad325fSLuca Coelho ret); 3429dad325fSLuca Coelho kfree(data); 3439dad325fSLuca Coelho 3449dad325fSLuca Coelho skip_reduce_power: 345b3e4c0f3SLuca Coelho iwl_init_notification_wait(notif_wait, &pnvm_wait, 346b3e4c0f3SLuca Coelho ntf_cmds, ARRAY_SIZE(ntf_cmds), 347b3e4c0f3SLuca Coelho iwl_pnvm_complete_fn, trans); 348b3e4c0f3SLuca Coelho 349b3e4c0f3SLuca Coelho /* kick the doorbell */ 350b3e4c0f3SLuca Coelho iwl_write_umac_prph(trans, UREG_DOORBELL_TO_ISR6, 351b3e4c0f3SLuca Coelho UREG_DOORBELL_TO_ISR6_PNVM); 352b3e4c0f3SLuca Coelho 353b3e4c0f3SLuca Coelho return iwl_wait_notification(notif_wait, &pnvm_wait, 354b3e4c0f3SLuca Coelho MVM_UCODE_PNVM_TIMEOUT); 355b3e4c0f3SLuca Coelho } 356b3e4c0f3SLuca Coelho IWL_EXPORT_SYMBOL(iwl_pnvm_load); 357