xref: /linux/drivers/net/wireless/intel/iwlwifi/fw/uefi.c (revision 37a93dd5c49b5fda807fd204edf2547c3493319c)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright(c) 2021-2025 Intel Corporation
4  */
5 
6 #include "iwl-drv.h"
7 #include "pnvm.h"
8 #include "iwl-prph.h"
9 #include "iwl-io.h"
10 
11 #include "fw/uefi.h"
12 #include "fw/api/alive.h"
13 #include <linux/efi.h>
14 #include "fw/runtime.h"
15 
16 #define IWL_EFI_WIFI_GUID	EFI_GUID(0x92daaf2f, 0xc02b, 0x455b,	\
17 					 0xb2, 0xec, 0xf5, 0xa3,	\
18 					 0x59, 0x4f, 0x4a, 0xea)
19 #define IWL_EFI_WIFI_BT_GUID	EFI_GUID(0xe65d8884, 0xd4af, 0x4b20,	\
20 					 0x8d, 0x03, 0x77, 0x2e,	\
21 					 0xcc, 0x3d, 0xa5, 0x31)
22 
23 struct iwl_uefi_pnvm_mem_desc {
24 	__le32 addr;
25 	__le32 size;
26 	const u8 data[];
27 } __packed;
28 
iwl_uefi_get_variable(efi_char16_t * name,efi_guid_t * guid,unsigned long * data_size)29 static void *iwl_uefi_get_variable(efi_char16_t *name, efi_guid_t *guid,
30 				   unsigned long *data_size)
31 {
32 	efi_status_t status;
33 	void *data;
34 
35 	if (!data_size)
36 		return ERR_PTR(-EINVAL);
37 
38 	if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE))
39 		return ERR_PTR(-ENODEV);
40 
41 	/* first call with NULL data to get the exact entry size */
42 	*data_size = 0;
43 	status = efi.get_variable(name, guid, NULL, data_size, NULL);
44 	if (status != EFI_BUFFER_TOO_SMALL || !*data_size)
45 		return ERR_PTR(-EIO);
46 
47 	data = kmalloc(*data_size, GFP_KERNEL);
48 	if (!data)
49 		return ERR_PTR(-ENOMEM);
50 
51 	status = efi.get_variable(name, guid, NULL, data_size, data);
52 	if (status != EFI_SUCCESS) {
53 		kfree(data);
54 		return ERR_PTR(-ENOENT);
55 	}
56 
57 	return data;
58 }
59 
iwl_uefi_get_pnvm(struct iwl_trans * trans,size_t * len)60 void *iwl_uefi_get_pnvm(struct iwl_trans *trans, size_t *len)
61 {
62 	unsigned long package_size;
63 	void *data;
64 
65 	*len = 0;
66 
67 	data = iwl_uefi_get_variable(IWL_UEFI_OEM_PNVM_NAME, &IWL_EFI_WIFI_GUID,
68 				     &package_size);
69 	if (IS_ERR(data)) {
70 		IWL_DEBUG_FW(trans,
71 			     "PNVM UEFI variable not found 0x%lx (len %lu)\n",
72 			     PTR_ERR(data), package_size);
73 		return data;
74 	}
75 
76 	IWL_DEBUG_FW(trans, "Read PNVM from UEFI with size %lu\n", package_size);
77 	*len = package_size;
78 
79 	return data;
80 }
81 
82 static void *
iwl_uefi_get_verified_variable_guid(struct iwl_trans * trans,efi_guid_t * guid,efi_char16_t * uefi_var_name,char * var_name,unsigned int expected_size,unsigned long * size)83 iwl_uefi_get_verified_variable_guid(struct iwl_trans *trans,
84 				    efi_guid_t *guid,
85 				    efi_char16_t *uefi_var_name,
86 				    char *var_name,
87 				    unsigned int expected_size,
88 				    unsigned long *size)
89 {
90 	void *var;
91 	unsigned long var_size;
92 
93 	var = iwl_uefi_get_variable(uefi_var_name, guid, &var_size);
94 
95 	if (IS_ERR(var)) {
96 		IWL_DEBUG_RADIO(trans,
97 				"%s UEFI variable not found 0x%lx\n", var_name,
98 				PTR_ERR(var));
99 		return var;
100 	}
101 
102 	if (var_size < expected_size) {
103 		IWL_DEBUG_RADIO(trans,
104 				"Invalid %s UEFI variable len (%lu)\n",
105 				var_name, var_size);
106 		kfree(var);
107 		return ERR_PTR(-EINVAL);
108 	}
109 
110 	IWL_DEBUG_RADIO(trans, "%s from UEFI with size %lu\n", var_name,
111 			var_size);
112 
113 	if (size)
114 		*size = var_size;
115 	return var;
116 }
117 
118 static void *
iwl_uefi_get_verified_variable(struct iwl_trans * trans,efi_char16_t * uefi_var_name,char * var_name,unsigned int expected_size,unsigned long * size)119 iwl_uefi_get_verified_variable(struct iwl_trans *trans,
120 			       efi_char16_t *uefi_var_name,
121 			       char *var_name,
122 			       unsigned int expected_size,
123 			       unsigned long *size)
124 {
125 	return iwl_uefi_get_verified_variable_guid(trans, &IWL_EFI_WIFI_GUID,
126 						   uefi_var_name, var_name,
127 						   expected_size, size);
128 }
129 
iwl_uefi_handle_tlv_mem_desc(struct iwl_trans * trans,const u8 * data,u32 tlv_len,struct iwl_pnvm_image * pnvm_data)130 int iwl_uefi_handle_tlv_mem_desc(struct iwl_trans *trans, const u8 *data,
131 				 u32 tlv_len, struct iwl_pnvm_image *pnvm_data)
132 {
133 	const struct iwl_uefi_pnvm_mem_desc *desc = (const void *)data;
134 	u32 data_len;
135 
136 	if (tlv_len < sizeof(*desc)) {
137 		IWL_DEBUG_FW(trans, "TLV len (%d) is too small\n", tlv_len);
138 		return -EINVAL;
139 	}
140 
141 	data_len = tlv_len - sizeof(*desc);
142 
143 	IWL_DEBUG_FW(trans,
144 		     "Handle IWL_UCODE_TLV_MEM_DESC, len %d data_len %d\n",
145 		     tlv_len, data_len);
146 
147 	if (le32_to_cpu(desc->size) != data_len) {
148 		IWL_DEBUG_FW(trans, "invalid mem desc size %d\n", desc->size);
149 		return -EINVAL;
150 	}
151 
152 	if (pnvm_data->n_chunks == IPC_DRAM_MAP_ENTRY_NUM_MAX) {
153 		IWL_DEBUG_FW(trans, "too many payloads to allocate in DRAM.\n");
154 		return -EINVAL;
155 	}
156 
157 	IWL_DEBUG_FW(trans, "Adding data (size %d)\n", data_len);
158 
159 	pnvm_data->chunks[pnvm_data->n_chunks].data = desc->data;
160 	pnvm_data->chunks[pnvm_data->n_chunks].len = data_len;
161 	pnvm_data->n_chunks++;
162 
163 	return 0;
164 }
165 
iwl_uefi_reduce_power_section(struct iwl_trans * trans,const u8 * data,size_t len,struct iwl_pnvm_image * pnvm_data)166 static int iwl_uefi_reduce_power_section(struct iwl_trans *trans,
167 					 const u8 *data, size_t len,
168 					 struct iwl_pnvm_image *pnvm_data)
169 {
170 	const struct iwl_ucode_tlv *tlv;
171 
172 	IWL_DEBUG_FW(trans, "Handling REDUCE_POWER section\n");
173 	memset(pnvm_data, 0, sizeof(*pnvm_data));
174 
175 	while (len >= sizeof(*tlv)) {
176 		u32 tlv_len, tlv_type;
177 
178 		len -= sizeof(*tlv);
179 		tlv = (const void *)data;
180 
181 		tlv_len = le32_to_cpu(tlv->length);
182 		tlv_type = le32_to_cpu(tlv->type);
183 
184 		if (len < tlv_len) {
185 			IWL_ERR(trans, "invalid TLV len: %zd/%u\n",
186 				len, tlv_len);
187 			return -EINVAL;
188 		}
189 
190 		data += sizeof(*tlv);
191 
192 		switch (tlv_type) {
193 		case IWL_UCODE_TLV_MEM_DESC:
194 			if (iwl_uefi_handle_tlv_mem_desc(trans, data, tlv_len,
195 							 pnvm_data))
196 				return -EINVAL;
197 			break;
198 		case IWL_UCODE_TLV_PNVM_SKU:
199 			IWL_DEBUG_FW(trans,
200 				     "New REDUCE_POWER section started, stop parsing.\n");
201 			goto done;
202 		default:
203 			IWL_DEBUG_FW(trans, "Found TLV 0x%0x, len %d\n",
204 				     tlv_type, tlv_len);
205 			break;
206 		}
207 
208 		len -= ALIGN(tlv_len, 4);
209 		data += ALIGN(tlv_len, 4);
210 	}
211 
212 done:
213 	if (!pnvm_data->n_chunks) {
214 		IWL_DEBUG_FW(trans, "Empty REDUCE_POWER, skipping.\n");
215 		return -ENOENT;
216 	}
217 	return 0;
218 }
219 
iwl_uefi_reduce_power_parse(struct iwl_trans * trans,const u8 * data,size_t len,struct iwl_pnvm_image * pnvm_data,__le32 sku_id[3])220 int iwl_uefi_reduce_power_parse(struct iwl_trans *trans,
221 				const u8 *data, size_t len,
222 				struct iwl_pnvm_image *pnvm_data,
223 				__le32 sku_id[3])
224 {
225 	const struct iwl_ucode_tlv *tlv;
226 
227 	IWL_DEBUG_FW(trans, "Parsing REDUCE_POWER data\n");
228 
229 	while (len >= sizeof(*tlv)) {
230 		u32 tlv_len, tlv_type;
231 
232 		len -= sizeof(*tlv);
233 		tlv = (const void *)data;
234 
235 		tlv_len = le32_to_cpu(tlv->length);
236 		tlv_type = le32_to_cpu(tlv->type);
237 
238 		if (len < tlv_len) {
239 			IWL_ERR(trans, "invalid TLV len: %zd/%u\n",
240 				len, tlv_len);
241 			return -EINVAL;
242 		}
243 
244 		if (tlv_type == IWL_UCODE_TLV_PNVM_SKU) {
245 			const struct iwl_sku_id *tlv_sku_id =
246 				(const void *)(data + sizeof(*tlv));
247 
248 			IWL_DEBUG_FW(trans,
249 				     "Got IWL_UCODE_TLV_PNVM_SKU len %d\n",
250 				     tlv_len);
251 			IWL_DEBUG_FW(trans, "sku_id 0x%0x 0x%0x 0x%0x\n",
252 				     le32_to_cpu(tlv_sku_id->data[0]),
253 				     le32_to_cpu(tlv_sku_id->data[1]),
254 				     le32_to_cpu(tlv_sku_id->data[2]));
255 
256 			data += sizeof(*tlv) + ALIGN(tlv_len, 4);
257 			len -= ALIGN(tlv_len, 4);
258 
259 			if (sku_id[0] == tlv_sku_id->data[0] &&
260 			    sku_id[1] == tlv_sku_id->data[1] &&
261 			    sku_id[2] == tlv_sku_id->data[2]) {
262 				int ret = iwl_uefi_reduce_power_section(trans,
263 								    data, len,
264 								    pnvm_data);
265 				if (!ret)
266 					return 0;
267 			} else {
268 				IWL_DEBUG_FW(trans, "SKU ID didn't match!\n");
269 			}
270 		} else {
271 			data += sizeof(*tlv) + ALIGN(tlv_len, 4);
272 			len -= ALIGN(tlv_len, 4);
273 		}
274 	}
275 
276 	return -ENOENT;
277 }
278 
iwl_uefi_get_reduced_power(struct iwl_trans * trans,size_t * len)279 u8 *iwl_uefi_get_reduced_power(struct iwl_trans *trans, size_t *len)
280 {
281 	struct pnvm_sku_package *package;
282 	unsigned long package_size;
283 	u8 *data;
284 
285 	package = iwl_uefi_get_verified_variable(trans,
286 						 IWL_UEFI_REDUCED_POWER_NAME,
287 						 "Reduced Power",
288 						 sizeof(*package),
289 						 &package_size);
290 	if (IS_ERR(package))
291 		return ERR_CAST(package);
292 
293 	IWL_DEBUG_FW(trans, "rev %d, total_size %d, n_skus %d\n",
294 		     package->rev, package->total_size, package->n_skus);
295 
296 	*len = package_size - sizeof(*package);
297 	data = kmemdup(package->data, *len, GFP_KERNEL);
298 	if (!data) {
299 		kfree(package);
300 		return ERR_PTR(-ENOMEM);
301 	}
302 
303 	kfree(package);
304 
305 	return data;
306 }
307 
iwl_uefi_step_parse(struct uefi_cnv_common_step_data * common_step_data,struct iwl_trans * trans)308 static int iwl_uefi_step_parse(struct uefi_cnv_common_step_data *common_step_data,
309 			       struct iwl_trans *trans)
310 {
311 	if (common_step_data->revision != 1)
312 		return -EINVAL;
313 
314 	trans->conf.mbx_addr_0_step =
315 		(u32)common_step_data->revision |
316 		(u32)common_step_data->cnvi_eq_channel << 8 |
317 		(u32)common_step_data->cnvr_eq_channel << 16 |
318 		(u32)common_step_data->radio1 << 24;
319 	trans->conf.mbx_addr_1_step = (u32)common_step_data->radio2;
320 	return 0;
321 }
322 
iwl_uefi_get_step_table(struct iwl_trans * trans)323 void iwl_uefi_get_step_table(struct iwl_trans *trans)
324 {
325 	struct uefi_cnv_common_step_data *data;
326 	int ret;
327 
328 	if (trans->mac_cfg->device_family < IWL_DEVICE_FAMILY_AX210)
329 		return;
330 
331 	data = iwl_uefi_get_verified_variable_guid(trans, &IWL_EFI_WIFI_BT_GUID,
332 						   IWL_UEFI_STEP_NAME,
333 						   "STEP", sizeof(*data), NULL);
334 	if (IS_ERR(data))
335 		return;
336 
337 	ret = iwl_uefi_step_parse(data, trans);
338 	if (ret < 0)
339 		IWL_DEBUG_FW(trans, "Cannot read STEP tables. rev is invalid\n");
340 
341 	kfree(data);
342 }
343 IWL_EXPORT_SYMBOL(iwl_uefi_get_step_table);
344 
iwl_uefi_sgom_parse(struct uefi_cnv_wlan_sgom_data * sgom_data,struct iwl_fw_runtime * fwrt)345 static int iwl_uefi_sgom_parse(struct uefi_cnv_wlan_sgom_data *sgom_data,
346 			       struct iwl_fw_runtime *fwrt)
347 {
348 	int i, j;
349 
350 	if (sgom_data->revision != 1)
351 		return -EINVAL;
352 
353 	memcpy(fwrt->sgom_table.offset_map, sgom_data->offset_map,
354 	       sizeof(fwrt->sgom_table.offset_map));
355 
356 	for (i = 0; i < MCC_TO_SAR_OFFSET_TABLE_ROW_SIZE; i++) {
357 		for (j = 0; j < MCC_TO_SAR_OFFSET_TABLE_COL_SIZE; j++) {
358 			/* since each byte is composed of to values, */
359 			/* one for each letter, */
360 			/* extract and check each of them separately */
361 			u8 value = fwrt->sgom_table.offset_map[i][j];
362 			u8 low = value & 0xF;
363 			u8 high = (value & 0xF0) >> 4;
364 
365 			if (high > fwrt->geo_num_profiles)
366 				high = 0;
367 			if (low > fwrt->geo_num_profiles)
368 				low = 0;
369 			fwrt->sgom_table.offset_map[i][j] = (high << 4) | low;
370 		}
371 	}
372 
373 	fwrt->sgom_enabled = true;
374 	return 0;
375 }
376 
iwl_uefi_get_sgom_table(struct iwl_trans * trans,struct iwl_fw_runtime * fwrt)377 void iwl_uefi_get_sgom_table(struct iwl_trans *trans,
378 			     struct iwl_fw_runtime *fwrt)
379 {
380 	struct uefi_cnv_wlan_sgom_data *data;
381 	int ret;
382 
383 	if (!fwrt->geo_enabled)
384 		return;
385 
386 	data = iwl_uefi_get_verified_variable(trans, IWL_UEFI_SGOM_NAME,
387 					      "SGOM", sizeof(*data), NULL);
388 	if (IS_ERR(data))
389 		return;
390 
391 	ret = iwl_uefi_sgom_parse(data, fwrt);
392 	if (ret < 0)
393 		IWL_DEBUG_FW(trans, "Cannot read SGOM tables. rev is invalid\n");
394 
395 	kfree(data);
396 }
397 IWL_EXPORT_SYMBOL(iwl_uefi_get_sgom_table);
398 
iwl_uefi_uats_parse(struct uefi_cnv_wlan_uats_data * uats_data,struct iwl_fw_runtime * fwrt)399 static int iwl_uefi_uats_parse(struct uefi_cnv_wlan_uats_data *uats_data,
400 			       struct iwl_fw_runtime *fwrt)
401 {
402 	if (uats_data->revision != 1)
403 		return -EINVAL;
404 
405 	memcpy(fwrt->uats_table.mcc_to_ap_type_map,
406 	       uats_data->mcc_to_ap_type_map,
407 	       sizeof(fwrt->uats_table.mcc_to_ap_type_map));
408 
409 	fwrt->uats_valid = true;
410 
411 	return 0;
412 }
413 
iwl_uefi_get_uats_table(struct iwl_trans * trans,struct iwl_fw_runtime * fwrt)414 void iwl_uefi_get_uats_table(struct iwl_trans *trans,
415 			     struct iwl_fw_runtime *fwrt)
416 {
417 	struct uefi_cnv_wlan_uats_data *data;
418 	int ret;
419 
420 	data = iwl_uefi_get_verified_variable(trans, IWL_UEFI_UATS_NAME,
421 					      "UATS", sizeof(*data), NULL);
422 	if (IS_ERR(data))
423 		return;
424 
425 	ret = iwl_uefi_uats_parse(data, fwrt);
426 	if (ret < 0)
427 		IWL_DEBUG_FW(trans, "Cannot read UATS table. rev is invalid\n");
428 	kfree(data);
429 }
430 IWL_EXPORT_SYMBOL(iwl_uefi_get_uats_table);
431 
iwl_uefi_set_sar_profile(struct iwl_fw_runtime * fwrt,struct uefi_sar_profile * uefi_sar_prof,u8 prof_index,bool enabled)432 static void iwl_uefi_set_sar_profile(struct iwl_fw_runtime *fwrt,
433 				     struct uefi_sar_profile *uefi_sar_prof,
434 				     u8 prof_index, bool enabled)
435 {
436 	memcpy(&fwrt->sar_profiles[prof_index].chains, uefi_sar_prof,
437 	       sizeof(struct uefi_sar_profile));
438 
439 	fwrt->sar_profiles[prof_index].enabled = enabled & IWL_SAR_ENABLE_MSK;
440 }
441 
iwl_uefi_get_wrds_table(struct iwl_fw_runtime * fwrt)442 int iwl_uefi_get_wrds_table(struct iwl_fw_runtime *fwrt)
443 {
444 	struct uefi_cnv_var_wrds *data;
445 	int ret = 0;
446 
447 	data = iwl_uefi_get_verified_variable(fwrt->trans, IWL_UEFI_WRDS_NAME,
448 					      "WRDS", sizeof(*data), NULL);
449 	if (IS_ERR(data))
450 		return -EINVAL;
451 
452 	if (data->revision != IWL_UEFI_WRDS_REVISION) {
453 		ret = -EINVAL;
454 		IWL_DEBUG_RADIO(fwrt, "Unsupported UEFI WRDS revision:%d\n",
455 				data->revision);
456 		goto out;
457 	}
458 
459 	/* The profile from WRDS is officially profile 1, but goes
460 	 * into sar_profiles[0] (because we don't have a profile 0).
461 	 */
462 	iwl_uefi_set_sar_profile(fwrt, &data->sar_profile, 0, data->mode);
463 out:
464 	kfree(data);
465 	return ret;
466 }
467 
iwl_uefi_get_ewrd_table(struct iwl_fw_runtime * fwrt)468 int iwl_uefi_get_ewrd_table(struct iwl_fw_runtime *fwrt)
469 {
470 	struct uefi_cnv_var_ewrd *data;
471 	int i, ret = 0;
472 
473 	data = iwl_uefi_get_verified_variable(fwrt->trans, IWL_UEFI_EWRD_NAME,
474 					      "EWRD", sizeof(*data), NULL);
475 	if (IS_ERR(data))
476 		return -EINVAL;
477 
478 	if (data->revision != IWL_UEFI_EWRD_REVISION) {
479 		ret = -EINVAL;
480 		IWL_DEBUG_RADIO(fwrt, "Unsupported UEFI EWRD revision:%d\n",
481 				data->revision);
482 		goto out;
483 	}
484 
485 	if (data->num_profiles >= BIOS_SAR_MAX_PROFILE_NUM) {
486 		ret = -EINVAL;
487 		goto out;
488 	}
489 
490 	for (i = 0; i < data->num_profiles; i++)
491 		/* The EWRD profiles officially go from 2 to 4, but we
492 		 * save them in sar_profiles[1-3] (because we don't
493 		 * have profile 0).  So in the array we start from 1.
494 		 */
495 		iwl_uefi_set_sar_profile(fwrt, &data->sar_profiles[i], i + 1,
496 					 data->mode);
497 
498 out:
499 	kfree(data);
500 	return ret;
501 }
502 
iwl_uefi_get_wgds_table(struct iwl_fw_runtime * fwrt)503 int iwl_uefi_get_wgds_table(struct iwl_fw_runtime *fwrt)
504 {
505 	struct uefi_cnv_var_wgds *data;
506 	int i, ret = 0;
507 
508 	data = iwl_uefi_get_verified_variable(fwrt->trans, IWL_UEFI_WGDS_NAME,
509 					      "WGDS", sizeof(*data), NULL);
510 	if (IS_ERR(data))
511 		return -EINVAL;
512 
513 	if (data->revision != IWL_UEFI_WGDS_REVISION) {
514 		ret = -EINVAL;
515 		IWL_DEBUG_RADIO(fwrt, "Unsupported UEFI WGDS revision:%d\n",
516 				data->revision);
517 		goto out;
518 	}
519 
520 	if (data->num_profiles < BIOS_GEO_MIN_PROFILE_NUM ||
521 	    data->num_profiles > BIOS_GEO_MAX_PROFILE_NUM) {
522 		ret = -EINVAL;
523 		IWL_DEBUG_RADIO(fwrt, "Invalid number of profiles in WGDS: %d\n",
524 				data->num_profiles);
525 		goto out;
526 	}
527 
528 	fwrt->geo_rev = data->revision;
529 	for (i = 0; i < data->num_profiles; i++)
530 		memcpy(&fwrt->geo_profiles[i], &data->geo_profiles[i],
531 		       sizeof(struct iwl_geo_profile));
532 
533 	fwrt->geo_num_profiles = data->num_profiles;
534 	fwrt->geo_enabled = true;
535 out:
536 	kfree(data);
537 	return ret;
538 }
539 
iwl_uefi_get_ppag_table(struct iwl_fw_runtime * fwrt)540 int iwl_uefi_get_ppag_table(struct iwl_fw_runtime *fwrt)
541 {
542 	struct uefi_cnv_var_ppag *data;
543 	int ret = 0;
544 
545 	data = iwl_uefi_get_verified_variable(fwrt->trans, IWL_UEFI_PPAG_NAME,
546 					      "PPAG", sizeof(*data), NULL);
547 	if (IS_ERR(data))
548 		return -EINVAL;
549 
550 	if (data->revision < IWL_UEFI_MIN_PPAG_REV ||
551 	    data->revision > IWL_UEFI_MAX_PPAG_REV) {
552 		ret = -EINVAL;
553 		IWL_DEBUG_RADIO(fwrt, "Unsupported UEFI PPAG revision:%d\n",
554 				data->revision);
555 		goto out;
556 	}
557 
558 	fwrt->ppag_bios_rev = data->revision;
559 	fwrt->ppag_flags = iwl_bios_get_ppag_flags(data->ppag_modes,
560 						   fwrt->ppag_bios_rev);
561 
562 	BUILD_BUG_ON(sizeof(fwrt->ppag_chains) != sizeof(data->ppag_chains));
563 	memcpy(&fwrt->ppag_chains, &data->ppag_chains,
564 	       sizeof(data->ppag_chains));
565 	fwrt->ppag_bios_source = BIOS_SOURCE_UEFI;
566 out:
567 	kfree(data);
568 	return ret;
569 }
570 
iwl_uefi_get_tas_table(struct iwl_fw_runtime * fwrt,struct iwl_tas_data * tas_data)571 int iwl_uefi_get_tas_table(struct iwl_fw_runtime *fwrt,
572 			   struct iwl_tas_data *tas_data)
573 {
574 	struct uefi_cnv_var_wtas *uefi_tas;
575 	int ret, enabled;
576 
577 	uefi_tas = iwl_uefi_get_verified_variable(fwrt->trans, IWL_UEFI_WTAS_NAME,
578 						  "WTAS", sizeof(*uefi_tas), NULL);
579 	if (IS_ERR(uefi_tas))
580 		return -EINVAL;
581 
582 	if (uefi_tas->revision < IWL_UEFI_MIN_WTAS_REVISION ||
583 	    uefi_tas->revision > IWL_UEFI_MAX_WTAS_REVISION) {
584 		ret = -EINVAL;
585 		IWL_DEBUG_RADIO(fwrt, "Unsupported UEFI WTAS revision:%d\n",
586 				uefi_tas->revision);
587 		goto out;
588 	}
589 
590 	IWL_DEBUG_RADIO(fwrt, "TAS selection as read from BIOS: 0x%x\n",
591 			uefi_tas->tas_selection);
592 
593 	enabled = uefi_tas->tas_selection & IWL_WTAS_ENABLED_MSK;
594 	tas_data->table_source = BIOS_SOURCE_UEFI;
595 	tas_data->table_revision = uefi_tas->revision;
596 	tas_data->tas_selection = uefi_tas->tas_selection;
597 
598 	IWL_DEBUG_RADIO(fwrt, "TAS %s enabled\n",
599 			enabled ? "is" : "not");
600 
601 	IWL_DEBUG_RADIO(fwrt, "Reading TAS table revision %d\n",
602 			uefi_tas->revision);
603 	if (uefi_tas->black_list_size > IWL_WTAS_BLACK_LIST_MAX) {
604 		IWL_DEBUG_RADIO(fwrt, "TAS invalid array size %d\n",
605 				uefi_tas->black_list_size);
606 		ret = -EINVAL;
607 		goto out;
608 	}
609 
610 	tas_data->block_list_size = uefi_tas->black_list_size;
611 	IWL_DEBUG_RADIO(fwrt, "TAS array size %u\n", uefi_tas->black_list_size);
612 
613 	for (u8 i = 0; i < uefi_tas->black_list_size; i++) {
614 		tas_data->block_list_array[i] = uefi_tas->black_list[i];
615 		IWL_DEBUG_RADIO(fwrt, "TAS block list country %d\n",
616 				uefi_tas->black_list[i]);
617 	}
618 	ret = enabled;
619 out:
620 	kfree(uefi_tas);
621 	return ret;
622 }
623 
iwl_uefi_get_pwr_limit(struct iwl_fw_runtime * fwrt,u64 * dflt_pwr_limit)624 int iwl_uefi_get_pwr_limit(struct iwl_fw_runtime *fwrt,
625 			   u64 *dflt_pwr_limit)
626 {
627 	struct uefi_cnv_var_splc *data;
628 	int ret = 0;
629 
630 	data = iwl_uefi_get_verified_variable(fwrt->trans, IWL_UEFI_SPLC_NAME,
631 					      "SPLC", sizeof(*data), NULL);
632 	if (IS_ERR(data))
633 		return -EINVAL;
634 
635 	if (data->revision != IWL_UEFI_SPLC_REVISION) {
636 		ret = -EINVAL;
637 		IWL_DEBUG_RADIO(fwrt, "Unsupported UEFI SPLC revision:%d\n",
638 				data->revision);
639 		goto out;
640 	}
641 	*dflt_pwr_limit = data->default_pwr_limit;
642 out:
643 	kfree(data);
644 	return ret;
645 }
646 
iwl_uefi_get_mcc(struct iwl_fw_runtime * fwrt,char * mcc)647 int iwl_uefi_get_mcc(struct iwl_fw_runtime *fwrt, char *mcc)
648 {
649 	struct uefi_cnv_var_wrdd *data;
650 	int ret = 0;
651 
652 	data = iwl_uefi_get_verified_variable(fwrt->trans, IWL_UEFI_WRDD_NAME,
653 					      "WRDD", sizeof(*data), NULL);
654 	if (IS_ERR(data))
655 		return -EINVAL;
656 
657 	if (data->revision != IWL_UEFI_WRDD_REVISION) {
658 		ret = -EINVAL;
659 		IWL_DEBUG_RADIO(fwrt, "Unsupported UEFI WRDD revision:%d\n",
660 				data->revision);
661 		goto out;
662 	}
663 
664 	if (data->mcc != BIOS_MCC_CHINA) {
665 		ret = -EINVAL;
666 		IWL_DEBUG_RADIO(fwrt, "UEFI WRDD is supported only for CN\n");
667 		goto out;
668 	}
669 
670 	mcc[0] = (data->mcc >> 8) & 0xff;
671 	mcc[1] = data->mcc & 0xff;
672 	mcc[2] = '\0';
673 out:
674 	kfree(data);
675 	return ret;
676 }
677 
iwl_uefi_get_eckv(struct iwl_fw_runtime * fwrt,u32 * extl_clk)678 int iwl_uefi_get_eckv(struct iwl_fw_runtime *fwrt, u32 *extl_clk)
679 {
680 	struct uefi_cnv_var_eckv *data;
681 	int ret = 0;
682 
683 	data = iwl_uefi_get_verified_variable_guid(fwrt->trans,
684 						   &IWL_EFI_WIFI_BT_GUID,
685 						   IWL_UEFI_ECKV_NAME,
686 						   "ECKV", sizeof(*data), NULL);
687 	if (IS_ERR(data))
688 		return -EINVAL;
689 
690 	if (data->revision != IWL_UEFI_ECKV_REVISION) {
691 		ret = -EINVAL;
692 		IWL_DEBUG_RADIO(fwrt, "Unsupported UEFI ECKV revision:%d\n",
693 				data->revision);
694 		goto out;
695 	}
696 	*extl_clk = data->ext_clock_valid;
697 out:
698 	kfree(data);
699 	return ret;
700 }
701 
iwl_uefi_get_wbem(struct iwl_fw_runtime * fwrt,u32 * value)702 int iwl_uefi_get_wbem(struct iwl_fw_runtime *fwrt, u32 *value)
703 {
704 	struct uefi_cnv_wlan_wbem_data *data;
705 	int ret = 0;
706 
707 	data = iwl_uefi_get_verified_variable(fwrt->trans, IWL_UEFI_WBEM_NAME,
708 					      "WBEM", sizeof(*data), NULL);
709 	if (IS_ERR(data))
710 		return -EINVAL;
711 
712 	if (data->revision != IWL_UEFI_WBEM_REVISION) {
713 		ret = -EINVAL;
714 		IWL_DEBUG_RADIO(fwrt, "Unsupported UEFI WBEM revision:%d\n",
715 				data->revision);
716 		goto out;
717 	}
718 	*value = data->wbem_320mhz_per_mcc & IWL_UEFI_WBEM_REV0_MASK;
719 	IWL_DEBUG_RADIO(fwrt, "Loaded WBEM config from UEFI\n");
720 out:
721 	kfree(data);
722 	return ret;
723 }
724 
iwl_uefi_load_dsm_values(struct iwl_fw_runtime * fwrt)725 static int iwl_uefi_load_dsm_values(struct iwl_fw_runtime *fwrt)
726 {
727 	struct uefi_cnv_var_general_cfg *data;
728 	int ret = -EINVAL;
729 
730 	BUILD_BUG_ON(ARRAY_SIZE(data->functions) < ARRAY_SIZE(fwrt->dsm_values));
731 
732 	data = iwl_uefi_get_verified_variable(fwrt->trans, IWL_UEFI_DSM_NAME,
733 					      "DSM", sizeof(*data), NULL);
734 	if (IS_ERR(data))
735 		return -EINVAL;
736 
737 	if (data->revision != IWL_UEFI_DSM_REVISION) {
738 		IWL_DEBUG_RADIO(fwrt, "Unsupported UEFI DSM revision:%d\n",
739 				data->revision);
740 		goto out;
741 	}
742 	fwrt->dsm_revision = data->revision;
743 	fwrt->dsm_source = BIOS_SOURCE_UEFI;
744 
745 	fwrt->dsm_funcs_valid = data->functions[DSM_FUNC_QUERY];
746 
747 	/*
748 	 * Make sure we don't load the DSM values twice. Set this only after we
749 	 * validated the DSM table so that if the table in UEFI is not valid,
750 	 * we will fallback to ACPI.
751 	 */
752 	fwrt->dsm_funcs_valid |= BIT(DSM_FUNC_QUERY);
753 
754 	for (int func = 1; func < ARRAY_SIZE(fwrt->dsm_values); func++) {
755 		if (!(fwrt->dsm_funcs_valid & BIT(func))) {
756 			IWL_DEBUG_RADIO(fwrt, "DSM func %d not in 0x%x\n",
757 					func, fwrt->dsm_funcs_valid);
758 			continue;
759 		}
760 
761 		fwrt->dsm_values[func] = data->functions[func];
762 
763 		IWL_DEBUG_RADIO(fwrt,
764 				"UEFI: DSM func=%d: value=%d\n", func,
765 				fwrt->dsm_values[func]);
766 	}
767 	ret = 0;
768 
769 out:
770 	kfree(data);
771 	return ret;
772 }
773 
iwl_uefi_get_dsm(struct iwl_fw_runtime * fwrt,enum iwl_dsm_funcs func,u32 * value)774 int iwl_uefi_get_dsm(struct iwl_fw_runtime *fwrt, enum iwl_dsm_funcs func,
775 		     u32 *value)
776 {
777 	/* Not supported function index */
778 	if (func >= DSM_FUNC_NUM_FUNCS || func == 5)
779 		return -EOPNOTSUPP;
780 
781 	if (!fwrt->dsm_funcs_valid) {
782 		int ret = iwl_uefi_load_dsm_values(fwrt);
783 
784 		if (ret)
785 			return ret;
786 	}
787 
788 	if (!(fwrt->dsm_funcs_valid & BIT(func))) {
789 		IWL_DEBUG_RADIO(fwrt, "DSM func %d not in 0x%x\n",
790 				func, fwrt->dsm_funcs_valid);
791 		return -EINVAL;
792 	}
793 
794 	*value = fwrt->dsm_values[func];
795 
796 	IWL_DEBUG_RADIO(fwrt,
797 			"UEFI: DSM func=%d: value=%d\n", func, *value);
798 
799 	return 0;
800 }
801 
iwl_uefi_get_puncturing(struct iwl_fw_runtime * fwrt)802 int iwl_uefi_get_puncturing(struct iwl_fw_runtime *fwrt)
803 {
804 	struct uefi_cnv_var_puncturing_data *data;
805 	/* default value is not enabled if there is any issue in reading
806 	 * uefi variable or revision is not supported
807 	 */
808 	int puncturing = 0;
809 
810 	data = iwl_uefi_get_verified_variable(fwrt->trans,
811 					      IWL_UEFI_PUNCTURING_NAME,
812 					      "UefiCnvWlanPuncturing",
813 					      sizeof(*data), NULL);
814 	if (IS_ERR(data))
815 		return puncturing;
816 
817 	if (data->revision != IWL_UEFI_PUNCTURING_REVISION) {
818 		IWL_DEBUG_RADIO(fwrt, "Unsupported UEFI PUNCTURING rev:%d\n",
819 				data->revision);
820 	} else {
821 		puncturing = data->puncturing & IWL_UEFI_PUNCTURING_REV0_MASK;
822 		IWL_DEBUG_RADIO(fwrt, "Loaded puncturing bits from UEFI: %d\n",
823 				puncturing);
824 	}
825 
826 	kfree(data);
827 	return puncturing;
828 }
829 IWL_EXPORT_SYMBOL(iwl_uefi_get_puncturing);
830 
iwl_uefi_get_dsbr(struct iwl_fw_runtime * fwrt,u32 * value)831 int iwl_uefi_get_dsbr(struct iwl_fw_runtime *fwrt, u32 *value)
832 {
833 	struct uefi_cnv_wlan_dsbr_data *data;
834 	int ret = 0;
835 
836 	data = iwl_uefi_get_verified_variable_guid(fwrt->trans,
837 						   &IWL_EFI_WIFI_BT_GUID,
838 						   IWL_UEFI_DSBR_NAME, "DSBR",
839 						   sizeof(*data), NULL);
840 	if (IS_ERR(data))
841 		return -EINVAL;
842 
843 	if (data->revision != IWL_UEFI_DSBR_REVISION) {
844 		ret = -EINVAL;
845 		IWL_DEBUG_RADIO(fwrt, "Unsupported UEFI DSBR revision:%d\n",
846 				data->revision);
847 		goto out;
848 	}
849 	*value = data->config;
850 	IWL_DEBUG_RADIO(fwrt, "Loaded DSBR config from UEFI value: 0x%x\n",
851 			*value);
852 out:
853 	kfree(data);
854 	return ret;
855 }
856 
iwl_uefi_get_phy_filters(struct iwl_fw_runtime * fwrt)857 int iwl_uefi_get_phy_filters(struct iwl_fw_runtime *fwrt)
858 {
859 	struct uefi_cnv_wpfc_data *data __free(kfree);
860 	struct iwl_phy_specific_cfg *filters = &fwrt->phy_filters;
861 
862 	data = iwl_uefi_get_verified_variable(fwrt->trans, IWL_UEFI_WPFC_NAME,
863 					      "WPFC", sizeof(*data), NULL);
864 	if (IS_ERR(data))
865 		return -EINVAL;
866 
867 	if (data->revision != 0) {
868 		IWL_DEBUG_RADIO(fwrt, "Unsupported UEFI WPFC revision:%d\n",
869 			data->revision);
870 		return -EINVAL;
871 	}
872 
873 	BUILD_BUG_ON(ARRAY_SIZE(filters->filter_cfg_chains) !=
874 		     ARRAY_SIZE(data->chains));
875 
876 	for (int i = 0; i < ARRAY_SIZE(filters->filter_cfg_chains); i++) {
877 		filters->filter_cfg_chains[i] = cpu_to_le32(data->chains[i]);
878 		IWL_DEBUG_RADIO(fwrt, "WPFC: chain %d: %u\n", i, data->chains[i]);
879 	}
880 
881 	IWL_DEBUG_RADIO(fwrt, "Loaded WPFC config from UEFI\n");
882 	return 0;
883 }
884