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