1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3 * Copyright(c) 2020-2021 Intel Corporation
10 #include "fw/api/commands.h"
11 #include "fw/api/nvm-reg.h"
12 #include "fw/api/alive.h"
13 #include <linux/efi.h>
15 struct iwl_pnvm_section {
20 static bool iwl_pnvm_complete_fn(struct iwl_notif_wait_data *notif_wait,
21 struct iwl_rx_packet *pkt, void *data)
23 struct iwl_trans *trans = (struct iwl_trans *)data;
24 struct iwl_pnvm_init_complete_ntfy *pnvm_ntf = (void *)pkt->data;
27 "PNVM complete notification received with status %d\n",
28 le32_to_cpu(pnvm_ntf->status));
33 static int iwl_pnvm_handle_section(struct iwl_trans *trans, const u8 *data,
36 struct iwl_ucode_tlv *tlv;
38 u16 mac_type = 0, rf_id = 0;
39 u8 *pnvm_data = NULL, *tmp;
43 IWL_DEBUG_FW(trans, "Handling PNVM section\n");
45 while (len >= sizeof(*tlv)) {
46 u32 tlv_len, tlv_type;
51 tlv_len = le32_to_cpu(tlv->length);
52 tlv_type = le32_to_cpu(tlv->type);
55 IWL_ERR(trans, "invalid TLV len: %zd/%u\n",
64 case IWL_UCODE_TLV_PNVM_VERSION:
65 if (tlv_len < sizeof(__le32)) {
67 "Invalid size for IWL_UCODE_TLV_PNVM_VERSION (expected %zd, got %d)\n",
68 sizeof(__le32), tlv_len);
72 sha1 = le32_to_cpup((__le32 *)data);
75 "Got IWL_UCODE_TLV_PNVM_VERSION %0x\n",
78 case IWL_UCODE_TLV_HW_TYPE:
79 if (tlv_len < 2 * sizeof(__le16)) {
81 "Invalid size for IWL_UCODE_TLV_HW_TYPE (expected %zd, got %d)\n",
82 2 * sizeof(__le16), tlv_len);
86 mac_type = le16_to_cpup((__le16 *)data);
87 rf_id = le16_to_cpup((__le16 *)(data + sizeof(__le16)));
90 "Got IWL_UCODE_TLV_HW_TYPE mac_type 0x%0x rf_id 0x%0x\n",
93 if (mac_type != CSR_HW_REV_TYPE(trans->hw_rev) ||
94 rf_id != CSR_HW_RFID_TYPE(trans->hw_rf_id)) {
96 "HW mismatch, skipping PNVM section, mac_type 0x%0x, rf_id 0x%0x.\n",
97 CSR_HW_REV_TYPE(trans->hw_rev), trans->hw_rf_id);
103 case IWL_UCODE_TLV_SEC_RT: {
104 struct iwl_pnvm_section *section = (void *)data;
105 u32 data_len = tlv_len - sizeof(*section);
108 "Got IWL_UCODE_TLV_SEC_RT len %d\n",
111 /* TODO: remove, this is a deprecated separator */
112 if (le32_to_cpup((__le32 *)data) == 0xddddeeee) {
113 IWL_DEBUG_FW(trans, "Ignoring separator.\n");
117 IWL_DEBUG_FW(trans, "Adding data (size %d)\n",
120 tmp = krealloc(pnvm_data, size + data_len, GFP_KERNEL);
123 "Couldn't allocate (more) pnvm_data\n");
131 memcpy(pnvm_data + size, section->data, data_len);
137 case IWL_UCODE_TLV_PNVM_SKU:
139 "New PNVM section started, stop parsing.\n");
142 IWL_DEBUG_FW(trans, "Found TLV 0x%0x, len %d\n",
147 len -= ALIGN(tlv_len, 4);
148 data += ALIGN(tlv_len, 4);
153 IWL_DEBUG_FW(trans, "Empty PNVM, skipping.\n");
158 IWL_INFO(trans, "loaded PNVM version 0x%0x\n", sha1);
160 ret = iwl_trans_set_pnvm(trans, pnvm_data, size);
166 static int iwl_pnvm_parse(struct iwl_trans *trans, const u8 *data,
169 struct iwl_ucode_tlv *tlv;
171 IWL_DEBUG_FW(trans, "Parsing PNVM file\n");
173 while (len >= sizeof(*tlv)) {
174 u32 tlv_len, tlv_type;
179 tlv_len = le32_to_cpu(tlv->length);
180 tlv_type = le32_to_cpu(tlv->type);
183 IWL_ERR(trans, "invalid TLV len: %zd/%u\n",
188 if (tlv_type == IWL_UCODE_TLV_PNVM_SKU) {
189 struct iwl_sku_id *sku_id =
190 (void *)(data + sizeof(*tlv));
193 "Got IWL_UCODE_TLV_PNVM_SKU len %d\n",
195 IWL_DEBUG_FW(trans, "sku_id 0x%0x 0x%0x 0x%0x\n",
196 le32_to_cpu(sku_id->data[0]),
197 le32_to_cpu(sku_id->data[1]),
198 le32_to_cpu(sku_id->data[2]));
200 data += sizeof(*tlv) + ALIGN(tlv_len, 4);
201 len -= ALIGN(tlv_len, 4);
203 if (trans->sku_id[0] == le32_to_cpu(sku_id->data[0]) &&
204 trans->sku_id[1] == le32_to_cpu(sku_id->data[1]) &&
205 trans->sku_id[2] == le32_to_cpu(sku_id->data[2])) {
208 ret = iwl_pnvm_handle_section(trans, data, len);
212 IWL_DEBUG_FW(trans, "SKU ID didn't match!\n");
215 data += sizeof(*tlv) + ALIGN(tlv_len, 4);
216 len -= ALIGN(tlv_len, 4);
223 #if defined(CONFIG_EFI)
225 #define IWL_EFI_VAR_GUID EFI_GUID(0x92daaf2f, 0xc02b, 0x455b, \
226 0xb2, 0xec, 0xf5, 0xa3, \
227 0x59, 0x4f, 0x4a, 0xea)
229 #define IWL_UEFI_OEM_PNVM_NAME L"UefiCnvWlanOemSignedPnvm"
231 #define IWL_HARDCODED_PNVM_SIZE 4096
233 struct pnvm_sku_package {
242 static int iwl_pnvm_get_from_efi(struct iwl_trans *trans,
243 u8 **data, size_t *len)
245 struct efivar_entry *pnvm_efivar;
246 struct pnvm_sku_package *package;
247 unsigned long package_size;
250 pnvm_efivar = kzalloc(sizeof(*pnvm_efivar), GFP_KERNEL);
254 memcpy(&pnvm_efivar->var.VariableName, IWL_UEFI_OEM_PNVM_NAME,
255 sizeof(IWL_UEFI_OEM_PNVM_NAME));
256 pnvm_efivar->var.VendorGuid = IWL_EFI_VAR_GUID;
259 * TODO: we hardcode a maximum length here, because reading
260 * from the UEFI is not working. To implement this properly,
261 * we have to call efivar_entry_size().
263 package_size = IWL_HARDCODED_PNVM_SIZE;
265 package = kmalloc(package_size, GFP_KERNEL);
271 err = efivar_entry_get(pnvm_efivar, NULL, &package_size, package);
274 "PNVM UEFI variable not found %d (len %zd)\n",
279 IWL_DEBUG_FW(trans, "Read PNVM fro UEFI with size %zd\n", package_size);
281 *data = kmemdup(package->data, *len, GFP_KERNEL);
284 *len = package_size - sizeof(*package);
292 #else /* CONFIG_EFI */
293 static inline int iwl_pnvm_get_from_efi(struct iwl_trans *trans,
294 u8 **data, size_t *len)
298 #endif /* CONFIG_EFI */
300 static int iwl_pnvm_get_from_fs(struct iwl_trans *trans, u8 **data, size_t *len)
302 const struct firmware *pnvm;
307 * The prefix unfortunately includes a hyphen at the end, so
308 * don't add the dot here...
310 snprintf(pnvm_name, sizeof(pnvm_name), "%spnvm",
311 trans->cfg->fw_name_pre);
313 /* ...but replace the hyphen with the dot here. */
314 if (strlen(trans->cfg->fw_name_pre) < sizeof(pnvm_name))
315 pnvm_name[strlen(trans->cfg->fw_name_pre) - 1] = '.';
317 ret = firmware_request_nowarn(&pnvm, pnvm_name, trans->dev);
319 IWL_DEBUG_FW(trans, "PNVM file %s not found %d\n",
324 *data = kmemdup(pnvm->data, pnvm->size, GFP_KERNEL);
333 int iwl_pnvm_load(struct iwl_trans *trans,
334 struct iwl_notif_wait_data *notif_wait)
338 struct iwl_notification_wait pnvm_wait;
339 static const u16 ntf_cmds[] = { WIDE_ID(REGULATORY_AND_NVM_GROUP,
340 PNVM_INIT_COMPLETE_NTFY) };
343 /* if the SKU_ID is empty, there's nothing to do */
344 if (!trans->sku_id[0] && !trans->sku_id[1] && !trans->sku_id[2])
348 * If we already loaded (or tried to load) it before, we just
349 * need to set it again.
351 if (trans->pnvm_loaded) {
352 ret = iwl_trans_set_pnvm(trans, NULL, 0);
358 /* First attempt to get the PNVM from BIOS */
359 ret = iwl_pnvm_get_from_efi(trans, &data, &len);
363 /* If it's not available, try from the filesystem */
364 ret = iwl_pnvm_get_from_fs(trans, &data, &len);
367 * Pretend we've loaded it - at least we've tried and
368 * couldn't load it at all, so there's no point in
369 * trying again over and over.
371 trans->pnvm_loaded = true;
377 iwl_pnvm_parse(trans, data, len);
382 iwl_init_notification_wait(notif_wait, &pnvm_wait,
383 ntf_cmds, ARRAY_SIZE(ntf_cmds),
384 iwl_pnvm_complete_fn, trans);
386 /* kick the doorbell */
387 iwl_write_umac_prph(trans, UREG_DOORBELL_TO_ISR6,
388 UREG_DOORBELL_TO_ISR6_PNVM);
390 return iwl_wait_notification(notif_wait, &pnvm_wait,
391 MVM_UCODE_PNVM_TIMEOUT);
393 IWL_EXPORT_SYMBOL(iwl_pnvm_load);