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