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 		  const struct iwl_ucode_capabilities *capa)
276 {
277 	u8 *data;
278 	size_t length;
279 	struct iwl_notification_wait pnvm_wait;
280 	static const u16 ntf_cmds[] = { WIDE_ID(REGULATORY_AND_NVM_GROUP,
281 						PNVM_INIT_COMPLETE_NTFY) };
282 	struct iwl_pnvm_image pnvm_data;
283 	int ret;
284 
285 	/* if the SKU_ID is empty, there's nothing to do */
286 	if (!trans->sku_id[0] && !trans->sku_id[1] && !trans->sku_id[2])
287 		return 0;
288 
289 	/* failed to get/parse the image in the past, no use to try again */
290 	if (trans->fail_to_parse_pnvm_image)
291 		goto reduce_tables;
292 
293 	/* get the image, parse and load it, if not loaded yet */
294 	if (!trans->pnvm_loaded) {
295 		data = iwl_get_pnvm_image(trans, &length);
296 		if (!data) {
297 			trans->fail_to_parse_pnvm_image = true;
298 			goto reduce_tables;
299 		}
300 		ret = iwl_pnvm_parse(trans, data, length, &pnvm_data);
301 		if (ret) {
302 			trans->fail_to_parse_pnvm_image = true;
303 			kfree(data);
304 			goto reduce_tables;
305 		}
306 
307 		ret = iwl_trans_load_pnvm(trans, &pnvm_data, capa);
308 		/* can only free data after pvnm_data use, but
309 		 * pnvm_data.version used below is not a pointer
310 		 */
311 		kfree(data);
312 		if (ret)
313 			goto reduce_tables;
314 		IWL_INFO(trans, "loaded PNVM version %08x\n",
315 			 pnvm_data.version);
316 	}
317 
318 	iwl_trans_set_pnvm(trans, capa);
319 
320 reduce_tables:
321 	/* now try to get the reduce power table, if not loaded yet */
322 	if (trans->failed_to_load_reduce_power_image)
323 		goto notification;
324 
325 	if (!trans->reduce_power_loaded) {
326 		memset(&pnvm_data, 0, sizeof(pnvm_data));
327 		data = iwl_uefi_get_reduced_power(trans, &length);
328 		if (IS_ERR(data)) {
329 			ret = PTR_ERR(data);
330 			trans->failed_to_load_reduce_power_image = true;
331 			goto notification;
332 		}
333 
334 		ret = iwl_uefi_reduce_power_parse(trans, data, length,
335 						  &pnvm_data);
336 		if (ret) {
337 			trans->failed_to_load_reduce_power_image = true;
338 			kfree(data);
339 			goto notification;
340 		}
341 
342 		ret = iwl_trans_load_reduce_power(trans, &pnvm_data, capa);
343 		kfree(data);
344 		if (ret) {
345 			IWL_DEBUG_FW(trans,
346 				     "Failed to load reduce power table %d\n",
347 				     ret);
348 			trans->failed_to_load_reduce_power_image = true;
349 			goto notification;
350 		}
351 	}
352 	iwl_trans_set_reduce_power(trans, capa);
353 
354 notification:
355 	iwl_init_notification_wait(notif_wait, &pnvm_wait,
356 				   ntf_cmds, ARRAY_SIZE(ntf_cmds),
357 				   iwl_pnvm_complete_fn, trans);
358 
359 	/* kick the doorbell */
360 	iwl_write_umac_prph(trans, UREG_DOORBELL_TO_ISR6,
361 			    UREG_DOORBELL_TO_ISR6_PNVM);
362 
363 	return iwl_wait_notification(notif_wait, &pnvm_wait,
364 				     MVM_UCODE_PNVM_TIMEOUT);
365 }
366 IWL_EXPORT_SYMBOL(iwl_pnvm_load);
367