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