1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 /****************************************************************************** 3 * 4 * Copyright(c) 2020 Intel Corporation 5 * 6 *****************************************************************************/ 7 8 #include "iwl-drv.h" 9 #include "pnvm.h" 10 #include "iwl-prph.h" 11 #include "iwl-io.h" 12 #include "fw/api/commands.h" 13 #include "fw/api/nvm-reg.h" 14 #include "fw/api/alive.h" 15 16 struct iwl_pnvm_section { 17 __le32 offset; 18 const u8 data[]; 19 } __packed; 20 21 static bool iwl_pnvm_complete_fn(struct iwl_notif_wait_data *notif_wait, 22 struct iwl_rx_packet *pkt, void *data) 23 { 24 struct iwl_trans *trans = (struct iwl_trans *)data; 25 struct iwl_pnvm_init_complete_ntfy *pnvm_ntf = (void *)pkt->data; 26 27 IWL_DEBUG_FW(trans, 28 "PNVM complete notification received with status %d\n", 29 le32_to_cpu(pnvm_ntf->status)); 30 31 return true; 32 } 33 34 static int iwl_pnvm_handle_section(struct iwl_trans *trans, const u8 *data, 35 size_t len) 36 { 37 struct iwl_ucode_tlv *tlv; 38 u32 sha1 = 0; 39 u16 mac_type = 0, rf_id = 0; 40 u8 *pnvm_data = NULL, *tmp; 41 u32 size = 0; 42 int ret; 43 44 IWL_DEBUG_FW(trans, "Handling PNVM section\n"); 45 46 while (len >= sizeof(*tlv)) { 47 u32 tlv_len, tlv_type; 48 49 len -= sizeof(*tlv); 50 tlv = (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 ret = -EINVAL; 59 goto out; 60 } 61 62 data += sizeof(*tlv); 63 64 switch (tlv_type) { 65 case IWL_UCODE_TLV_PNVM_VERSION: 66 if (tlv_len < sizeof(__le32)) { 67 IWL_DEBUG_FW(trans, 68 "Invalid size for IWL_UCODE_TLV_PNVM_VERSION (expected %zd, got %d)\n", 69 sizeof(__le32), tlv_len); 70 break; 71 } 72 73 sha1 = le32_to_cpup((__le32 *)data); 74 75 IWL_DEBUG_FW(trans, 76 "Got IWL_UCODE_TLV_PNVM_VERSION %0x\n", 77 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 mac_type = le16_to_cpup((__le16 *)data); 88 rf_id = le16_to_cpup((__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 IWL_DEBUG_FW(trans, 97 "HW mismatch, skipping PNVM section, mac_type 0x%0x, rf_id 0x%0x.\n", 98 CSR_HW_REV_TYPE(trans->hw_rev), trans->hw_rf_id); 99 ret = -ENOENT; 100 goto out; 101 } 102 103 break; 104 case IWL_UCODE_TLV_SEC_RT: { 105 struct iwl_pnvm_section *section = (void *)data; 106 u32 data_len = tlv_len - sizeof(*section); 107 108 IWL_DEBUG_FW(trans, 109 "Got IWL_UCODE_TLV_SEC_RT len %d\n", 110 tlv_len); 111 112 /* TODO: remove, this is a deprecated separator */ 113 if (le32_to_cpup((__le32 *)data) == 0xddddeeee) { 114 IWL_DEBUG_FW(trans, "Ignoring separator.\n"); 115 break; 116 } 117 118 IWL_DEBUG_FW(trans, "Adding data (size %d)\n", 119 data_len); 120 121 tmp = krealloc(pnvm_data, size + data_len, GFP_KERNEL); 122 if (!tmp) { 123 IWL_DEBUG_FW(trans, 124 "Couldn't allocate (more) pnvm_data\n"); 125 126 ret = -ENOMEM; 127 goto out; 128 } 129 130 pnvm_data = tmp; 131 132 memcpy(pnvm_data + size, section->data, data_len); 133 134 size += data_len; 135 136 break; 137 } 138 case IWL_UCODE_TLV_PNVM_SKU: 139 IWL_DEBUG_FW(trans, 140 "New PNVM section started, stop parsing.\n"); 141 goto done; 142 default: 143 IWL_DEBUG_FW(trans, "Found TLV 0x%0x, len %d\n", 144 tlv_type, tlv_len); 145 break; 146 } 147 148 len -= ALIGN(tlv_len, 4); 149 data += ALIGN(tlv_len, 4); 150 } 151 152 done: 153 if (!size) { 154 IWL_DEBUG_FW(trans, "Empty PNVM, skipping.\n"); 155 ret = -ENOENT; 156 goto out; 157 } 158 159 IWL_INFO(trans, "loaded PNVM version 0x%0x\n", sha1); 160 161 ret = iwl_trans_set_pnvm(trans, pnvm_data, size); 162 out: 163 kfree(pnvm_data); 164 return ret; 165 } 166 167 static int iwl_pnvm_parse(struct iwl_trans *trans, const u8 *data, 168 size_t len) 169 { 170 struct iwl_ucode_tlv *tlv; 171 172 IWL_DEBUG_FW(trans, "Parsing PNVM file\n"); 173 174 while (len >= sizeof(*tlv)) { 175 u32 tlv_len, tlv_type; 176 177 len -= sizeof(*tlv); 178 tlv = (void *)data; 179 180 tlv_len = le32_to_cpu(tlv->length); 181 tlv_type = le32_to_cpu(tlv->type); 182 183 if (len < tlv_len) { 184 IWL_ERR(trans, "invalid TLV len: %zd/%u\n", 185 len, tlv_len); 186 return -EINVAL; 187 } 188 189 if (tlv_type == IWL_UCODE_TLV_PNVM_SKU) { 190 struct iwl_sku_id *sku_id = 191 (void *)(data + sizeof(*tlv)); 192 193 IWL_DEBUG_FW(trans, 194 "Got IWL_UCODE_TLV_PNVM_SKU len %d\n", 195 tlv_len); 196 IWL_DEBUG_FW(trans, "sku_id 0x%0x 0x%0x 0x%0x\n", 197 le32_to_cpu(sku_id->data[0]), 198 le32_to_cpu(sku_id->data[1]), 199 le32_to_cpu(sku_id->data[2])); 200 201 if (trans->sku_id[0] == le32_to_cpu(sku_id->data[0]) && 202 trans->sku_id[1] == le32_to_cpu(sku_id->data[1]) && 203 trans->sku_id[2] == le32_to_cpu(sku_id->data[2])) { 204 int ret; 205 206 data += sizeof(*tlv) + ALIGN(tlv_len, 4); 207 len -= ALIGN(tlv_len, 4); 208 209 ret = iwl_pnvm_handle_section(trans, data, len); 210 if (!ret) 211 return 0; 212 } else { 213 IWL_DEBUG_FW(trans, "SKU ID didn't match!\n"); 214 } 215 } else { 216 data += sizeof(*tlv) + ALIGN(tlv_len, 4); 217 len -= ALIGN(tlv_len, 4); 218 } 219 } 220 221 return -ENOENT; 222 } 223 224 int iwl_pnvm_load(struct iwl_trans *trans, 225 struct iwl_notif_wait_data *notif_wait) 226 { 227 const struct firmware *pnvm; 228 struct iwl_notification_wait pnvm_wait; 229 static const u16 ntf_cmds[] = { WIDE_ID(REGULATORY_AND_NVM_GROUP, 230 PNVM_INIT_COMPLETE_NTFY) }; 231 char pnvm_name[64]; 232 int ret; 233 234 /* if the SKU_ID is empty, there's nothing to do */ 235 if (!trans->sku_id[0] && !trans->sku_id[1] && !trans->sku_id[2]) 236 return 0; 237 238 /* if we already have it, nothing to do either */ 239 if (trans->pnvm_loaded) 240 return 0; 241 242 /* 243 * The prefix unfortunately includes a hyphen at the end, so 244 * don't add the dot here... 245 */ 246 snprintf(pnvm_name, sizeof(pnvm_name), "%spnvm", 247 trans->cfg->fw_name_pre); 248 249 /* ...but replace the hyphen with the dot here. */ 250 if (strlen(trans->cfg->fw_name_pre) < sizeof(pnvm_name)) 251 pnvm_name[strlen(trans->cfg->fw_name_pre) - 1] = '.'; 252 253 ret = firmware_request_nowarn(&pnvm, pnvm_name, trans->dev); 254 if (ret) { 255 IWL_DEBUG_FW(trans, "PNVM file %s not found %d\n", 256 pnvm_name, ret); 257 } else { 258 iwl_pnvm_parse(trans, pnvm->data, pnvm->size); 259 260 release_firmware(pnvm); 261 } 262 263 iwl_init_notification_wait(notif_wait, &pnvm_wait, 264 ntf_cmds, ARRAY_SIZE(ntf_cmds), 265 iwl_pnvm_complete_fn, trans); 266 267 /* kick the doorbell */ 268 iwl_write_umac_prph(trans, UREG_DOORBELL_TO_ISR6, 269 UREG_DOORBELL_TO_ISR6_PNVM); 270 271 return iwl_wait_notification(notif_wait, &pnvm_wait, 272 MVM_UCODE_PNVM_TIMEOUT); 273 } 274 IWL_EXPORT_SYMBOL(iwl_pnvm_load); 275