1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 /* 3 * Copyright(c) 2020-2023 Intel Corporation 4 */ 5 6 #include "iwl-drv.h" 7 #include "pnvm.h" 8 #include "iwl-prph.h" 9 #include "iwl-io.h" 10 #include "fw/api/commands.h" 11 #include "fw/api/nvm-reg.h" 12 #include "fw/api/alive.h" 13 #include "fw/uefi.h" 14 15 struct iwl_pnvm_section { 16 __le32 offset; 17 const u8 data[]; 18 } __packed; 19 20 static bool iwl_pnvm_complete_fn(struct iwl_notif_wait_data *notif_wait, 21 struct iwl_rx_packet *pkt, void *data) 22 { 23 struct iwl_trans *trans = (struct iwl_trans *)data; 24 struct iwl_pnvm_init_complete_ntfy *pnvm_ntf = (void *)pkt->data; 25 26 IWL_DEBUG_FW(trans, 27 "PNVM complete notification received with status 0x%0x\n", 28 le32_to_cpu(pnvm_ntf->status)); 29 30 return true; 31 } 32 33 static int iwl_pnvm_handle_section(struct iwl_trans *trans, const u8 *data, 34 size_t len, 35 struct iwl_pnvm_image *pnvm_data) 36 { 37 const struct iwl_ucode_tlv *tlv; 38 u32 sha1 = 0; 39 u16 mac_type = 0, rf_id = 0; 40 bool hw_match = false; 41 42 IWL_DEBUG_FW(trans, "Handling PNVM section\n"); 43 44 memset(pnvm_data, 0, sizeof(*pnvm_data)); 45 46 while (len >= sizeof(*tlv)) { 47 u32 tlv_len, tlv_type; 48 49 len -= sizeof(*tlv); 50 tlv = (const void *)data; 51 52 tlv_len = le32_to_cpu(tlv->length); 53 tlv_type = le32_to_cpu(tlv->type); 54 55 if (len < tlv_len) { 56 IWL_ERR(trans, "invalid TLV len: %zd/%u\n", 57 len, tlv_len); 58 return -EINVAL; 59 } 60 61 data += sizeof(*tlv); 62 63 switch (tlv_type) { 64 case IWL_UCODE_TLV_PNVM_VERSION: 65 if (tlv_len < sizeof(__le32)) { 66 IWL_DEBUG_FW(trans, 67 "Invalid size for IWL_UCODE_TLV_PNVM_VERSION (expected %zd, got %d)\n", 68 sizeof(__le32), tlv_len); 69 break; 70 } 71 72 sha1 = le32_to_cpup((const __le32 *)data); 73 74 IWL_DEBUG_FW(trans, 75 "Got IWL_UCODE_TLV_PNVM_VERSION %0x\n", 76 sha1); 77 pnvm_data->version = sha1; 78 break; 79 case IWL_UCODE_TLV_HW_TYPE: 80 if (tlv_len < 2 * sizeof(__le16)) { 81 IWL_DEBUG_FW(trans, 82 "Invalid size for IWL_UCODE_TLV_HW_TYPE (expected %zd, got %d)\n", 83 2 * sizeof(__le16), tlv_len); 84 break; 85 } 86 87 if (hw_match) 88 break; 89 90 mac_type = le16_to_cpup((const __le16 *)data); 91 rf_id = le16_to_cpup((const __le16 *)(data + sizeof(__le16))); 92 93 IWL_DEBUG_FW(trans, 94 "Got IWL_UCODE_TLV_HW_TYPE mac_type 0x%0x rf_id 0x%0x\n", 95 mac_type, rf_id); 96 97 if (mac_type == CSR_HW_REV_TYPE(trans->hw_rev) && 98 rf_id == CSR_HW_RFID_TYPE(trans->hw_rf_id)) 99 hw_match = true; 100 break; 101 case IWL_UCODE_TLV_SEC_RT: { 102 const struct iwl_pnvm_section *section = (const void *)data; 103 u32 data_len = tlv_len - sizeof(*section); 104 105 IWL_DEBUG_FW(trans, 106 "Got IWL_UCODE_TLV_SEC_RT len %d\n", 107 tlv_len); 108 109 /* TODO: remove, this is a deprecated separator */ 110 if (le32_to_cpup((const __le32 *)data) == 0xddddeeee) { 111 IWL_DEBUG_FW(trans, "Ignoring separator.\n"); 112 break; 113 } 114 115 if (pnvm_data->n_chunks == IPC_DRAM_MAP_ENTRY_NUM_MAX) { 116 IWL_DEBUG_FW(trans, 117 "too many payloads to allocate in DRAM.\n"); 118 return -EINVAL; 119 } 120 121 IWL_DEBUG_FW(trans, "Adding data (size %d)\n", 122 data_len); 123 124 pnvm_data->chunks[pnvm_data->n_chunks].data = section->data; 125 pnvm_data->chunks[pnvm_data->n_chunks].len = data_len; 126 pnvm_data->n_chunks++; 127 128 break; 129 } 130 case IWL_UCODE_TLV_PNVM_SKU: 131 IWL_DEBUG_FW(trans, 132 "New PNVM section started, stop parsing.\n"); 133 goto done; 134 default: 135 IWL_DEBUG_FW(trans, "Found TLV 0x%0x, len %d\n", 136 tlv_type, tlv_len); 137 break; 138 } 139 140 len -= ALIGN(tlv_len, 4); 141 data += ALIGN(tlv_len, 4); 142 } 143 144 done: 145 if (!hw_match) { 146 IWL_DEBUG_FW(trans, 147 "HW mismatch, skipping PNVM section (need mac_type 0x%x rf_id 0x%x)\n", 148 CSR_HW_REV_TYPE(trans->hw_rev), 149 CSR_HW_RFID_TYPE(trans->hw_rf_id)); 150 return -ENOENT; 151 } 152 153 if (!pnvm_data->n_chunks) { 154 IWL_DEBUG_FW(trans, "Empty PNVM, skipping.\n"); 155 return -ENOENT; 156 } 157 158 return 0; 159 } 160 161 static int iwl_pnvm_parse(struct iwl_trans *trans, const u8 *data, 162 size_t len, 163 struct iwl_pnvm_image *pnvm_data) 164 { 165 const struct iwl_ucode_tlv *tlv; 166 167 IWL_DEBUG_FW(trans, "Parsing PNVM file\n"); 168 169 while (len >= sizeof(*tlv)) { 170 u32 tlv_len, tlv_type; 171 172 len -= sizeof(*tlv); 173 tlv = (const void *)data; 174 175 tlv_len = le32_to_cpu(tlv->length); 176 tlv_type = le32_to_cpu(tlv->type); 177 178 if (len < tlv_len) { 179 IWL_ERR(trans, "invalid TLV len: %zd/%u\n", 180 len, tlv_len); 181 return -EINVAL; 182 } 183 184 if (tlv_type == IWL_UCODE_TLV_PNVM_SKU) { 185 const struct iwl_sku_id *sku_id = 186 (const void *)(data + sizeof(*tlv)); 187 188 IWL_DEBUG_FW(trans, 189 "Got IWL_UCODE_TLV_PNVM_SKU len %d\n", 190 tlv_len); 191 IWL_DEBUG_FW(trans, "sku_id 0x%0x 0x%0x 0x%0x\n", 192 le32_to_cpu(sku_id->data[0]), 193 le32_to_cpu(sku_id->data[1]), 194 le32_to_cpu(sku_id->data[2])); 195 196 data += sizeof(*tlv) + ALIGN(tlv_len, 4); 197 len -= ALIGN(tlv_len, 4); 198 199 if (trans->sku_id[0] == le32_to_cpu(sku_id->data[0]) && 200 trans->sku_id[1] == le32_to_cpu(sku_id->data[1]) && 201 trans->sku_id[2] == le32_to_cpu(sku_id->data[2])) { 202 int ret; 203 204 ret = iwl_pnvm_handle_section(trans, data, len, 205 pnvm_data); 206 if (!ret) 207 return 0; 208 } else { 209 IWL_DEBUG_FW(trans, "SKU ID didn't match!\n"); 210 } 211 } else { 212 data += sizeof(*tlv) + ALIGN(tlv_len, 4); 213 len -= ALIGN(tlv_len, 4); 214 } 215 } 216 217 return -ENOENT; 218 } 219 220 static int iwl_pnvm_get_from_fs(struct iwl_trans *trans, u8 **data, size_t *len) 221 { 222 const struct firmware *pnvm; 223 char pnvm_name[MAX_PNVM_NAME]; 224 size_t new_len; 225 int ret; 226 227 iwl_pnvm_get_fs_name(trans, pnvm_name, sizeof(pnvm_name)); 228 229 ret = firmware_request_nowarn(&pnvm, pnvm_name, trans->dev); 230 if (ret) { 231 IWL_DEBUG_FW(trans, "PNVM file %s not found %d\n", 232 pnvm_name, ret); 233 return ret; 234 } 235 236 new_len = pnvm->size; 237 *data = kmemdup(pnvm->data, pnvm->size, GFP_KERNEL); 238 release_firmware(pnvm); 239 240 if (!*data) 241 return -ENOMEM; 242 243 *len = new_len; 244 245 return 0; 246 } 247 248 static u8 *iwl_get_pnvm_image(struct iwl_trans *trans_p, size_t *len) 249 { 250 struct pnvm_sku_package *package; 251 u8 *image = NULL; 252 253 /* First attempt to get the PNVM from BIOS */ 254 package = iwl_uefi_get_pnvm(trans_p, len); 255 if (!IS_ERR_OR_NULL(package)) { 256 if (*len >= sizeof(*package)) { 257 /* we need only the data */ 258 *len -= sizeof(*package); 259 image = kmemdup(package->data, *len, GFP_KERNEL); 260 } 261 /* free package regardless of whether kmemdup succeeded */ 262 kfree(package); 263 if (image) 264 return image; 265 } 266 267 /* If it's not available, try from the filesystem */ 268 if (iwl_pnvm_get_from_fs(trans_p, &image, len)) 269 return NULL; 270 return image; 271 } 272 273 int iwl_pnvm_load(struct iwl_trans *trans, 274 struct iwl_notif_wait_data *notif_wait) 275 { 276 u8 *data; 277 size_t length; 278 struct iwl_notification_wait pnvm_wait; 279 static const u16 ntf_cmds[] = { WIDE_ID(REGULATORY_AND_NVM_GROUP, 280 PNVM_INIT_COMPLETE_NTFY) }; 281 struct iwl_pnvm_image pnvm_data; 282 int ret; 283 284 /* if the SKU_ID is empty, there's nothing to do */ 285 if (!trans->sku_id[0] && !trans->sku_id[1] && !trans->sku_id[2]) 286 return 0; 287 288 /* failed to get/parse the image in the past, no use to try again */ 289 if (trans->fail_to_parse_pnvm_image) 290 goto reduce_tables; 291 292 /* get the image, parse and load it, if not loaded yet */ 293 if (!trans->pnvm_loaded) { 294 data = iwl_get_pnvm_image(trans, &length); 295 if (!data) { 296 trans->fail_to_parse_pnvm_image = true; 297 goto reduce_tables; 298 } 299 ret = iwl_pnvm_parse(trans, data, length, &pnvm_data); 300 if (ret) { 301 trans->fail_to_parse_pnvm_image = true; 302 kfree(data); 303 goto reduce_tables; 304 } 305 306 ret = iwl_trans_load_pnvm(trans, &pnvm_data); 307 /* can only free data after pvnm_data use, but 308 * pnvm_data.version used below is not a pointer 309 */ 310 kfree(data); 311 if (ret) 312 goto reduce_tables; 313 IWL_INFO(trans, "loaded PNVM version %08x\n", 314 pnvm_data.version); 315 } 316 317 iwl_trans_set_pnvm(trans); 318 319 reduce_tables: 320 /* now try to get the reduce power table, if not loaded yet */ 321 if (!trans->reduce_power_loaded) { 322 data = iwl_uefi_get_reduced_power(trans, &length); 323 if (IS_ERR_OR_NULL(data)) { 324 /* 325 * Pretend we've loaded it - at least we've tried and 326 * couldn't load it at all, so there's no point in 327 * trying again over and over. 328 */ 329 trans->reduce_power_loaded = true; 330 } else { 331 ret = iwl_trans_set_reduce_power(trans, data, length); 332 if (ret) 333 IWL_DEBUG_FW(trans, 334 "Failed to set reduce power table %d\n", 335 ret); 336 kfree(data); 337 } 338 } 339 340 iwl_init_notification_wait(notif_wait, &pnvm_wait, 341 ntf_cmds, ARRAY_SIZE(ntf_cmds), 342 iwl_pnvm_complete_fn, trans); 343 344 /* kick the doorbell */ 345 iwl_write_umac_prph(trans, UREG_DOORBELL_TO_ISR6, 346 UREG_DOORBELL_TO_ISR6_PNVM); 347 348 return iwl_wait_notification(notif_wait, &pnvm_wait, 349 MVM_UCODE_PNVM_TIMEOUT); 350 } 351 IWL_EXPORT_SYMBOL(iwl_pnvm_load); 352