Loading...
1// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2/*
3 * Copyright (C) 2017 Intel Deutschland GmbH
4 * Copyright (C) 2019-2023 Intel Corporation
5 */
6#include <linux/uuid.h>
7#include <linux/dmi.h>
8#include "iwl-drv.h"
9#include "iwl-debug.h"
10#include "acpi.h"
11#include "fw/runtime.h"
12
13const guid_t iwl_guid = GUID_INIT(0xF21202BF, 0x8F78, 0x4DC6,
14 0xA5, 0xB3, 0x1F, 0x73,
15 0x8E, 0x28, 0x5A, 0xDE);
16IWL_EXPORT_SYMBOL(iwl_guid);
17
18const guid_t iwl_rfi_guid = GUID_INIT(0x7266172C, 0x220B, 0x4B29,
19 0x81, 0x4F, 0x75, 0xE4,
20 0xDD, 0x26, 0xB5, 0xFD);
21IWL_EXPORT_SYMBOL(iwl_rfi_guid);
22
23static const struct dmi_system_id dmi_ppag_approved_list[] = {
24 { .ident = "HP",
25 .matches = {
26 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
27 },
28 },
29 { .ident = "SAMSUNG",
30 .matches = {
31 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD"),
32 },
33 },
34 { .ident = "MSFT",
35 .matches = {
36 DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
37 },
38 },
39 { .ident = "ASUS",
40 .matches = {
41 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
42 },
43 },
44 { .ident = "GOOGLE-HP",
45 .matches = {
46 DMI_MATCH(DMI_SYS_VENDOR, "Google"),
47 DMI_MATCH(DMI_BOARD_VENDOR, "HP"),
48 },
49 },
50 { .ident = "GOOGLE-ASUS",
51 .matches = {
52 DMI_MATCH(DMI_SYS_VENDOR, "Google"),
53 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTek COMPUTER INC."),
54 },
55 },
56 { .ident = "GOOGLE-SAMSUNG",
57 .matches = {
58 DMI_MATCH(DMI_SYS_VENDOR, "Google"),
59 DMI_MATCH(DMI_BOARD_VENDOR, "SAMSUNG ELECTRONICS CO., LTD"),
60 },
61 },
62 { .ident = "DELL",
63 .matches = {
64 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
65 },
66 },
67 { .ident = "DELL",
68 .matches = {
69 DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
70 },
71 },
72 { .ident = "RAZER",
73 .matches = {
74 DMI_MATCH(DMI_SYS_VENDOR, "Razer"),
75 },
76 },
77 {}
78};
79
80static int iwl_acpi_get_handle(struct device *dev, acpi_string method,
81 acpi_handle *ret_handle)
82{
83 acpi_handle root_handle;
84 acpi_status status;
85
86 root_handle = ACPI_HANDLE(dev);
87 if (!root_handle) {
88 IWL_DEBUG_DEV_RADIO(dev,
89 "ACPI: Could not retrieve root port handle\n");
90 return -ENOENT;
91 }
92
93 status = acpi_get_handle(root_handle, method, ret_handle);
94 if (ACPI_FAILURE(status)) {
95 IWL_DEBUG_DEV_RADIO(dev,
96 "ACPI: %s method not found\n", method);
97 return -ENOENT;
98 }
99 return 0;
100}
101
102static void *iwl_acpi_get_object(struct device *dev, acpi_string method)
103{
104 struct acpi_buffer buf = {ACPI_ALLOCATE_BUFFER, NULL};
105 acpi_handle handle;
106 acpi_status status;
107 int ret;
108
109 ret = iwl_acpi_get_handle(dev, method, &handle);
110 if (ret)
111 return ERR_PTR(-ENOENT);
112
113 /* Call the method with no arguments */
114 status = acpi_evaluate_object(handle, NULL, NULL, &buf);
115 if (ACPI_FAILURE(status)) {
116 IWL_DEBUG_DEV_RADIO(dev,
117 "ACPI: %s method invocation failed (status: 0x%x)\n",
118 method, status);
119 return ERR_PTR(-ENOENT);
120 }
121 return buf.pointer;
122}
123
124/*
125 * Generic function for evaluating a method defined in the device specific
126 * method (DSM) interface. The returned acpi object must be freed by calling
127 * function.
128 */
129static void *iwl_acpi_get_dsm_object(struct device *dev, int rev, int func,
130 union acpi_object *args,
131 const guid_t *guid)
132{
133 union acpi_object *obj;
134
135 obj = acpi_evaluate_dsm(ACPI_HANDLE(dev), guid, rev, func,
136 args);
137 if (!obj) {
138 IWL_DEBUG_DEV_RADIO(dev,
139 "ACPI: DSM method invocation failed (rev: %d, func:%d)\n",
140 rev, func);
141 return ERR_PTR(-ENOENT);
142 }
143 return obj;
144}
145
146/*
147 * Generic function to evaluate a DSM with no arguments
148 * and an integer return value,
149 * (as an integer object or inside a buffer object),
150 * verify and assign the value in the "value" parameter.
151 * return 0 in success and the appropriate errno otherwise.
152 */
153static int iwl_acpi_get_dsm_integer(struct device *dev, int rev, int func,
154 const guid_t *guid, u64 *value,
155 size_t expected_size)
156{
157 union acpi_object *obj;
158 int ret = 0;
159
160 obj = iwl_acpi_get_dsm_object(dev, rev, func, NULL, guid);
161 if (IS_ERR(obj)) {
162 IWL_DEBUG_DEV_RADIO(dev,
163 "Failed to get DSM object. func= %d\n",
164 func);
165 return -ENOENT;
166 }
167
168 if (obj->type == ACPI_TYPE_INTEGER) {
169 *value = obj->integer.value;
170 } else if (obj->type == ACPI_TYPE_BUFFER) {
171 __le64 le_value = 0;
172
173 if (WARN_ON_ONCE(expected_size > sizeof(le_value)))
174 return -EINVAL;
175
176 /* if the buffer size doesn't match the expected size */
177 if (obj->buffer.length != expected_size)
178 IWL_DEBUG_DEV_RADIO(dev,
179 "ACPI: DSM invalid buffer size, padding or truncating (%d)\n",
180 obj->buffer.length);
181
182 /* assuming LE from Intel BIOS spec */
183 memcpy(&le_value, obj->buffer.pointer,
184 min_t(size_t, expected_size, (size_t)obj->buffer.length));
185 *value = le64_to_cpu(le_value);
186 } else {
187 IWL_DEBUG_DEV_RADIO(dev,
188 "ACPI: DSM method did not return a valid object, type=%d\n",
189 obj->type);
190 ret = -EINVAL;
191 goto out;
192 }
193
194 IWL_DEBUG_DEV_RADIO(dev,
195 "ACPI: DSM method evaluated: func=%d, ret=%d\n",
196 func, ret);
197out:
198 ACPI_FREE(obj);
199 return ret;
200}
201
202/*
203 * Evaluate a DSM with no arguments and a u8 return value,
204 */
205int iwl_acpi_get_dsm_u8(struct device *dev, int rev, int func,
206 const guid_t *guid, u8 *value)
207{
208 int ret;
209 u64 val;
210
211 ret = iwl_acpi_get_dsm_integer(dev, rev, func,
212 guid, &val, sizeof(u8));
213
214 if (ret < 0)
215 return ret;
216
217 /* cast val (u64) to be u8 */
218 *value = (u8)val;
219 return 0;
220}
221IWL_EXPORT_SYMBOL(iwl_acpi_get_dsm_u8);
222
223/*
224 * Evaluate a DSM with no arguments and a u32 return value,
225 */
226int iwl_acpi_get_dsm_u32(struct device *dev, int rev, int func,
227 const guid_t *guid, u32 *value)
228{
229 int ret;
230 u64 val;
231
232 ret = iwl_acpi_get_dsm_integer(dev, rev, func,
233 guid, &val, sizeof(u32));
234
235 if (ret < 0)
236 return ret;
237
238 /* cast val (u64) to be u32 */
239 *value = (u32)val;
240 return 0;
241}
242IWL_EXPORT_SYMBOL(iwl_acpi_get_dsm_u32);
243
244static union acpi_object *
245iwl_acpi_get_wifi_pkg_range(struct device *dev,
246 union acpi_object *data,
247 int min_data_size,
248 int max_data_size,
249 int *tbl_rev)
250{
251 int i;
252 union acpi_object *wifi_pkg;
253
254 /*
255 * We need at least one entry in the wifi package that
256 * describes the domain, and one more entry, otherwise there's
257 * no point in reading it.
258 */
259 if (WARN_ON_ONCE(min_data_size < 2 || min_data_size > max_data_size))
260 return ERR_PTR(-EINVAL);
261
262 /*
263 * We need at least two packages, one for the revision and one
264 * for the data itself. Also check that the revision is valid
265 * (i.e. it is an integer (each caller has to check by itself
266 * if the returned revision is supported)).
267 */
268 if (data->type != ACPI_TYPE_PACKAGE ||
269 data->package.count < 2 ||
270 data->package.elements[0].type != ACPI_TYPE_INTEGER) {
271 IWL_DEBUG_DEV_RADIO(dev, "Invalid packages structure\n");
272 return ERR_PTR(-EINVAL);
273 }
274
275 *tbl_rev = data->package.elements[0].integer.value;
276
277 /* loop through all the packages to find the one for WiFi */
278 for (i = 1; i < data->package.count; i++) {
279 union acpi_object *domain;
280
281 wifi_pkg = &data->package.elements[i];
282
283 /* skip entries that are not a package with the right size */
284 if (wifi_pkg->type != ACPI_TYPE_PACKAGE ||
285 wifi_pkg->package.count < min_data_size ||
286 wifi_pkg->package.count > max_data_size)
287 continue;
288
289 domain = &wifi_pkg->package.elements[0];
290 if (domain->type == ACPI_TYPE_INTEGER &&
291 domain->integer.value == ACPI_WIFI_DOMAIN)
292 goto found;
293 }
294
295 return ERR_PTR(-ENOENT);
296
297found:
298 return wifi_pkg;
299}
300
301static union acpi_object *
302iwl_acpi_get_wifi_pkg(struct device *dev,
303 union acpi_object *data,
304 int data_size, int *tbl_rev)
305{
306 return iwl_acpi_get_wifi_pkg_range(dev, data, data_size, data_size,
307 tbl_rev);
308}
309
310
311int iwl_acpi_get_tas(struct iwl_fw_runtime *fwrt,
312 union iwl_tas_config_cmd *cmd, int fw_ver)
313{
314 union acpi_object *wifi_pkg, *data;
315 int ret, tbl_rev, i, block_list_size, enabled;
316
317 data = iwl_acpi_get_object(fwrt->dev, ACPI_WTAS_METHOD);
318 if (IS_ERR(data))
319 return PTR_ERR(data);
320
321 /* try to read wtas table revision 1 or revision 0*/
322 wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
323 ACPI_WTAS_WIFI_DATA_SIZE,
324 &tbl_rev);
325 if (IS_ERR(wifi_pkg)) {
326 ret = PTR_ERR(wifi_pkg);
327 goto out_free;
328 }
329
330 if (tbl_rev == 1 && wifi_pkg->package.elements[1].type ==
331 ACPI_TYPE_INTEGER) {
332 u32 tas_selection =
333 (u32)wifi_pkg->package.elements[1].integer.value;
334 u16 override_iec =
335 (tas_selection & ACPI_WTAS_OVERRIDE_IEC_MSK) >> ACPI_WTAS_OVERRIDE_IEC_POS;
336 u16 enabled_iec = (tas_selection & ACPI_WTAS_ENABLE_IEC_MSK) >>
337 ACPI_WTAS_ENABLE_IEC_POS;
338 u8 usa_tas_uhb = (tas_selection & ACPI_WTAS_USA_UHB_MSK) >> ACPI_WTAS_USA_UHB_POS;
339
340
341 enabled = tas_selection & ACPI_WTAS_ENABLED_MSK;
342 if (fw_ver <= 3) {
343 cmd->v3.override_tas_iec = cpu_to_le16(override_iec);
344 cmd->v3.enable_tas_iec = cpu_to_le16(enabled_iec);
345 } else {
346 cmd->v4.usa_tas_uhb_allowed = usa_tas_uhb;
347 cmd->v4.override_tas_iec = (u8)override_iec;
348 cmd->v4.enable_tas_iec = (u8)enabled_iec;
349 }
350
351 } else if (tbl_rev == 0 &&
352 wifi_pkg->package.elements[1].type == ACPI_TYPE_INTEGER) {
353 enabled = !!wifi_pkg->package.elements[1].integer.value;
354 } else {
355 ret = -EINVAL;
356 goto out_free;
357 }
358
359 if (!enabled) {
360 IWL_DEBUG_RADIO(fwrt, "TAS not enabled\n");
361 ret = 0;
362 goto out_free;
363 }
364
365 IWL_DEBUG_RADIO(fwrt, "Reading TAS table revision %d\n", tbl_rev);
366 if (wifi_pkg->package.elements[2].type != ACPI_TYPE_INTEGER ||
367 wifi_pkg->package.elements[2].integer.value >
368 APCI_WTAS_BLACK_LIST_MAX) {
369 IWL_DEBUG_RADIO(fwrt, "TAS invalid array size %llu\n",
370 wifi_pkg->package.elements[2].integer.value);
371 ret = -EINVAL;
372 goto out_free;
373 }
374 block_list_size = wifi_pkg->package.elements[2].integer.value;
375 cmd->v4.block_list_size = cpu_to_le32(block_list_size);
376
377 IWL_DEBUG_RADIO(fwrt, "TAS array size %u\n", block_list_size);
378 if (block_list_size > APCI_WTAS_BLACK_LIST_MAX) {
379 IWL_DEBUG_RADIO(fwrt, "TAS invalid array size value %u\n",
380 block_list_size);
381 ret = -EINVAL;
382 goto out_free;
383 }
384
385 for (i = 0; i < block_list_size; i++) {
386 u32 country;
387
388 if (wifi_pkg->package.elements[3 + i].type !=
389 ACPI_TYPE_INTEGER) {
390 IWL_DEBUG_RADIO(fwrt,
391 "TAS invalid array elem %d\n", 3 + i);
392 ret = -EINVAL;
393 goto out_free;
394 }
395
396 country = wifi_pkg->package.elements[3 + i].integer.value;
397 cmd->v4.block_list_array[i] = cpu_to_le32(country);
398 IWL_DEBUG_RADIO(fwrt, "TAS block list country %d\n", country);
399 }
400
401 ret = 1;
402out_free:
403 kfree(data);
404 return ret;
405}
406IWL_EXPORT_SYMBOL(iwl_acpi_get_tas);
407
408int iwl_acpi_get_mcc(struct device *dev, char *mcc)
409{
410 union acpi_object *wifi_pkg, *data;
411 u32 mcc_val;
412 int ret, tbl_rev;
413
414 data = iwl_acpi_get_object(dev, ACPI_WRDD_METHOD);
415 if (IS_ERR(data))
416 return PTR_ERR(data);
417
418 wifi_pkg = iwl_acpi_get_wifi_pkg(dev, data, ACPI_WRDD_WIFI_DATA_SIZE,
419 &tbl_rev);
420 if (IS_ERR(wifi_pkg)) {
421 ret = PTR_ERR(wifi_pkg);
422 goto out_free;
423 }
424
425 if (wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER ||
426 tbl_rev != 0) {
427 ret = -EINVAL;
428 goto out_free;
429 }
430
431 mcc_val = wifi_pkg->package.elements[1].integer.value;
432
433 mcc[0] = (mcc_val >> 8) & 0xff;
434 mcc[1] = mcc_val & 0xff;
435 mcc[2] = '\0';
436
437 ret = 0;
438out_free:
439 kfree(data);
440 return ret;
441}
442IWL_EXPORT_SYMBOL(iwl_acpi_get_mcc);
443
444u64 iwl_acpi_get_pwr_limit(struct device *dev)
445{
446 union acpi_object *data, *wifi_pkg;
447 u64 dflt_pwr_limit;
448 int tbl_rev;
449
450 data = iwl_acpi_get_object(dev, ACPI_SPLC_METHOD);
451 if (IS_ERR(data)) {
452 dflt_pwr_limit = 0;
453 goto out;
454 }
455
456 wifi_pkg = iwl_acpi_get_wifi_pkg(dev, data,
457 ACPI_SPLC_WIFI_DATA_SIZE, &tbl_rev);
458 if (IS_ERR(wifi_pkg) || tbl_rev != 0 ||
459 wifi_pkg->package.elements[1].integer.value != ACPI_TYPE_INTEGER) {
460 dflt_pwr_limit = 0;
461 goto out_free;
462 }
463
464 dflt_pwr_limit = wifi_pkg->package.elements[1].integer.value;
465out_free:
466 kfree(data);
467out:
468 return dflt_pwr_limit;
469}
470IWL_EXPORT_SYMBOL(iwl_acpi_get_pwr_limit);
471
472int iwl_acpi_get_eckv(struct device *dev, u32 *extl_clk)
473{
474 union acpi_object *wifi_pkg, *data;
475 int ret, tbl_rev;
476
477 data = iwl_acpi_get_object(dev, ACPI_ECKV_METHOD);
478 if (IS_ERR(data))
479 return PTR_ERR(data);
480
481 wifi_pkg = iwl_acpi_get_wifi_pkg(dev, data, ACPI_ECKV_WIFI_DATA_SIZE,
482 &tbl_rev);
483 if (IS_ERR(wifi_pkg)) {
484 ret = PTR_ERR(wifi_pkg);
485 goto out_free;
486 }
487
488 if (wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER ||
489 tbl_rev != 0) {
490 ret = -EINVAL;
491 goto out_free;
492 }
493
494 *extl_clk = wifi_pkg->package.elements[1].integer.value;
495
496 ret = 0;
497
498out_free:
499 kfree(data);
500 return ret;
501}
502IWL_EXPORT_SYMBOL(iwl_acpi_get_eckv);
503
504static int iwl_sar_set_profile(union acpi_object *table,
505 struct iwl_sar_profile *profile,
506 bool enabled, u8 num_chains, u8 num_sub_bands)
507{
508 int i, j, idx = 0;
509
510 /*
511 * The table from ACPI is flat, but we store it in a
512 * structured array.
513 */
514 for (i = 0; i < ACPI_SAR_NUM_CHAINS_REV2; i++) {
515 for (j = 0; j < ACPI_SAR_NUM_SUB_BANDS_REV2; j++) {
516 /* if we don't have the values, use the default */
517 if (i >= num_chains || j >= num_sub_bands) {
518 profile->chains[i].subbands[j] = 0;
519 } else {
520 if (table[idx].type != ACPI_TYPE_INTEGER ||
521 table[idx].integer.value > U8_MAX)
522 return -EINVAL;
523
524 profile->chains[i].subbands[j] =
525 table[idx].integer.value;
526
527 idx++;
528 }
529 }
530 }
531
532 /* Only if all values were valid can the profile be enabled */
533 profile->enabled = enabled;
534
535 return 0;
536}
537
538static int iwl_sar_fill_table(struct iwl_fw_runtime *fwrt,
539 __le16 *per_chain, u32 n_subbands,
540 int prof_a, int prof_b)
541{
542 int profs[ACPI_SAR_NUM_CHAINS_REV0] = { prof_a, prof_b };
543 int i, j;
544
545 for (i = 0; i < ACPI_SAR_NUM_CHAINS_REV0; i++) {
546 struct iwl_sar_profile *prof;
547
548 /* don't allow SAR to be disabled (profile 0 means disable) */
549 if (profs[i] == 0)
550 return -EPERM;
551
552 /* we are off by one, so allow up to ACPI_SAR_PROFILE_NUM */
553 if (profs[i] > ACPI_SAR_PROFILE_NUM)
554 return -EINVAL;
555
556 /* profiles go from 1 to 4, so decrement to access the array */
557 prof = &fwrt->sar_profiles[profs[i] - 1];
558
559 /* if the profile is disabled, do nothing */
560 if (!prof->enabled) {
561 IWL_DEBUG_RADIO(fwrt, "SAR profile %d is disabled.\n",
562 profs[i]);
563 /*
564 * if one of the profiles is disabled, we
565 * ignore all of them and return 1 to
566 * differentiate disabled from other failures.
567 */
568 return 1;
569 }
570
571 IWL_DEBUG_INFO(fwrt,
572 "SAR EWRD: chain %d profile index %d\n",
573 i, profs[i]);
574 IWL_DEBUG_RADIO(fwrt, " Chain[%d]:\n", i);
575 for (j = 0; j < n_subbands; j++) {
576 per_chain[i * n_subbands + j] =
577 cpu_to_le16(prof->chains[i].subbands[j]);
578 IWL_DEBUG_RADIO(fwrt, " Band[%d] = %d * .125dBm\n",
579 j, prof->chains[i].subbands[j]);
580 }
581 }
582
583 return 0;
584}
585
586int iwl_sar_select_profile(struct iwl_fw_runtime *fwrt,
587 __le16 *per_chain, u32 n_tables, u32 n_subbands,
588 int prof_a, int prof_b)
589{
590 int i, ret = 0;
591
592 for (i = 0; i < n_tables; i++) {
593 ret = iwl_sar_fill_table(fwrt,
594 &per_chain[i * n_subbands * ACPI_SAR_NUM_CHAINS_REV0],
595 n_subbands, prof_a, prof_b);
596 if (ret)
597 break;
598 }
599
600 return ret;
601}
602IWL_EXPORT_SYMBOL(iwl_sar_select_profile);
603
604int iwl_sar_get_wrds_table(struct iwl_fw_runtime *fwrt)
605{
606 union acpi_object *wifi_pkg, *table, *data;
607 int ret, tbl_rev;
608 u32 flags;
609 u8 num_chains, num_sub_bands;
610
611 data = iwl_acpi_get_object(fwrt->dev, ACPI_WRDS_METHOD);
612 if (IS_ERR(data))
613 return PTR_ERR(data);
614
615 /* start by trying to read revision 2 */
616 wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
617 ACPI_WRDS_WIFI_DATA_SIZE_REV2,
618 &tbl_rev);
619 if (!IS_ERR(wifi_pkg)) {
620 if (tbl_rev != 2) {
621 ret = -EINVAL;
622 goto out_free;
623 }
624
625 num_chains = ACPI_SAR_NUM_CHAINS_REV2;
626 num_sub_bands = ACPI_SAR_NUM_SUB_BANDS_REV2;
627
628 goto read_table;
629 }
630
631 /* then try revision 1 */
632 wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
633 ACPI_WRDS_WIFI_DATA_SIZE_REV1,
634 &tbl_rev);
635 if (!IS_ERR(wifi_pkg)) {
636 if (tbl_rev != 1) {
637 ret = -EINVAL;
638 goto out_free;
639 }
640
641 num_chains = ACPI_SAR_NUM_CHAINS_REV1;
642 num_sub_bands = ACPI_SAR_NUM_SUB_BANDS_REV1;
643
644 goto read_table;
645 }
646
647 /* then finally revision 0 */
648 wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
649 ACPI_WRDS_WIFI_DATA_SIZE_REV0,
650 &tbl_rev);
651 if (!IS_ERR(wifi_pkg)) {
652 if (tbl_rev != 0) {
653 ret = -EINVAL;
654 goto out_free;
655 }
656
657 num_chains = ACPI_SAR_NUM_CHAINS_REV0;
658 num_sub_bands = ACPI_SAR_NUM_SUB_BANDS_REV0;
659
660 goto read_table;
661 }
662
663 ret = PTR_ERR(wifi_pkg);
664 goto out_free;
665
666read_table:
667 if (wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER) {
668 ret = -EINVAL;
669 goto out_free;
670 }
671
672 IWL_DEBUG_RADIO(fwrt, "Reading WRDS tbl_rev=%d\n", tbl_rev);
673
674 flags = wifi_pkg->package.elements[1].integer.value;
675 fwrt->reduced_power_flags = flags >> IWL_REDUCE_POWER_FLAGS_POS;
676
677 /* position of the actual table */
678 table = &wifi_pkg->package.elements[2];
679
680 /* The profile from WRDS is officially profile 1, but goes
681 * into sar_profiles[0] (because we don't have a profile 0).
682 */
683 ret = iwl_sar_set_profile(table, &fwrt->sar_profiles[0],
684 flags & IWL_SAR_ENABLE_MSK,
685 num_chains, num_sub_bands);
686out_free:
687 kfree(data);
688 return ret;
689}
690IWL_EXPORT_SYMBOL(iwl_sar_get_wrds_table);
691
692int iwl_sar_get_ewrd_table(struct iwl_fw_runtime *fwrt)
693{
694 union acpi_object *wifi_pkg, *data;
695 bool enabled;
696 int i, n_profiles, tbl_rev, pos;
697 int ret = 0;
698 u8 num_chains, num_sub_bands;
699
700 data = iwl_acpi_get_object(fwrt->dev, ACPI_EWRD_METHOD);
701 if (IS_ERR(data))
702 return PTR_ERR(data);
703
704 /* start by trying to read revision 2 */
705 wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
706 ACPI_EWRD_WIFI_DATA_SIZE_REV2,
707 &tbl_rev);
708 if (!IS_ERR(wifi_pkg)) {
709 if (tbl_rev != 2) {
710 ret = -EINVAL;
711 goto out_free;
712 }
713
714 num_chains = ACPI_SAR_NUM_CHAINS_REV2;
715 num_sub_bands = ACPI_SAR_NUM_SUB_BANDS_REV2;
716
717 goto read_table;
718 }
719
720 /* then try revision 1 */
721 wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
722 ACPI_EWRD_WIFI_DATA_SIZE_REV1,
723 &tbl_rev);
724 if (!IS_ERR(wifi_pkg)) {
725 if (tbl_rev != 1) {
726 ret = -EINVAL;
727 goto out_free;
728 }
729
730 num_chains = ACPI_SAR_NUM_CHAINS_REV1;
731 num_sub_bands = ACPI_SAR_NUM_SUB_BANDS_REV1;
732
733 goto read_table;
734 }
735
736 /* then finally revision 0 */
737 wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
738 ACPI_EWRD_WIFI_DATA_SIZE_REV0,
739 &tbl_rev);
740 if (!IS_ERR(wifi_pkg)) {
741 if (tbl_rev != 0) {
742 ret = -EINVAL;
743 goto out_free;
744 }
745
746 num_chains = ACPI_SAR_NUM_CHAINS_REV0;
747 num_sub_bands = ACPI_SAR_NUM_SUB_BANDS_REV0;
748
749 goto read_table;
750 }
751
752 ret = PTR_ERR(wifi_pkg);
753 goto out_free;
754
755read_table:
756 if (wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER ||
757 wifi_pkg->package.elements[2].type != ACPI_TYPE_INTEGER) {
758 ret = -EINVAL;
759 goto out_free;
760 }
761
762 enabled = !!(wifi_pkg->package.elements[1].integer.value);
763 n_profiles = wifi_pkg->package.elements[2].integer.value;
764
765 /*
766 * Check the validity of n_profiles. The EWRD profiles start
767 * from index 1, so the maximum value allowed here is
768 * ACPI_SAR_PROFILES_NUM - 1.
769 */
770 if (n_profiles <= 0 || n_profiles >= ACPI_SAR_PROFILE_NUM) {
771 ret = -EINVAL;
772 goto out_free;
773 }
774
775 /* the tables start at element 3 */
776 pos = 3;
777
778 for (i = 0; i < n_profiles; i++) {
779 /* The EWRD profiles officially go from 2 to 4, but we
780 * save them in sar_profiles[1-3] (because we don't
781 * have profile 0). So in the array we start from 1.
782 */
783 ret = iwl_sar_set_profile(&wifi_pkg->package.elements[pos],
784 &fwrt->sar_profiles[i + 1], enabled,
785 num_chains, num_sub_bands);
786 if (ret < 0)
787 break;
788
789 /* go to the next table */
790 pos += num_chains * num_sub_bands;
791 }
792
793out_free:
794 kfree(data);
795 return ret;
796}
797IWL_EXPORT_SYMBOL(iwl_sar_get_ewrd_table);
798
799int iwl_sar_get_wgds_table(struct iwl_fw_runtime *fwrt)
800{
801 union acpi_object *wifi_pkg, *data;
802 int i, j, k, ret, tbl_rev;
803 u8 num_bands, num_profiles;
804 static const struct {
805 u8 revisions;
806 u8 bands;
807 u8 profiles;
808 u8 min_profiles;
809 } rev_data[] = {
810 {
811 .revisions = BIT(3),
812 .bands = ACPI_GEO_NUM_BANDS_REV2,
813 .profiles = ACPI_NUM_GEO_PROFILES_REV3,
814 .min_profiles = 3,
815 },
816 {
817 .revisions = BIT(2),
818 .bands = ACPI_GEO_NUM_BANDS_REV2,
819 .profiles = ACPI_NUM_GEO_PROFILES,
820 },
821 {
822 .revisions = BIT(0) | BIT(1),
823 .bands = ACPI_GEO_NUM_BANDS_REV0,
824 .profiles = ACPI_NUM_GEO_PROFILES,
825 },
826 };
827 int idx;
828 /* start from one to skip the domain */
829 int entry_idx = 1;
830
831 BUILD_BUG_ON(ACPI_NUM_GEO_PROFILES_REV3 != IWL_NUM_GEO_PROFILES_V3);
832 BUILD_BUG_ON(ACPI_NUM_GEO_PROFILES != IWL_NUM_GEO_PROFILES);
833
834 data = iwl_acpi_get_object(fwrt->dev, ACPI_WGDS_METHOD);
835 if (IS_ERR(data))
836 return PTR_ERR(data);
837
838 /* read the highest revision we understand first */
839 for (idx = 0; idx < ARRAY_SIZE(rev_data); idx++) {
840 /* min_profiles != 0 requires num_profiles header */
841 u32 hdr_size = 1 + !!rev_data[idx].min_profiles;
842 u32 profile_size = ACPI_GEO_PER_CHAIN_SIZE *
843 rev_data[idx].bands;
844 u32 max_size = hdr_size + profile_size * rev_data[idx].profiles;
845 u32 min_size;
846
847 if (!rev_data[idx].min_profiles)
848 min_size = max_size;
849 else
850 min_size = hdr_size +
851 profile_size * rev_data[idx].min_profiles;
852
853 wifi_pkg = iwl_acpi_get_wifi_pkg_range(fwrt->dev, data,
854 min_size, max_size,
855 &tbl_rev);
856 if (!IS_ERR(wifi_pkg)) {
857 if (!(BIT(tbl_rev) & rev_data[idx].revisions))
858 continue;
859
860 num_bands = rev_data[idx].bands;
861 num_profiles = rev_data[idx].profiles;
862
863 if (rev_data[idx].min_profiles) {
864 /* read header that says # of profiles */
865 union acpi_object *entry;
866
867 entry = &wifi_pkg->package.elements[entry_idx];
868 entry_idx++;
869 if (entry->type != ACPI_TYPE_INTEGER ||
870 entry->integer.value > num_profiles) {
871 ret = -EINVAL;
872 goto out_free;
873 }
874 num_profiles = entry->integer.value;
875
876 /*
877 * this also validates >= min_profiles since we
878 * otherwise wouldn't have gotten the data when
879 * looking up in ACPI
880 */
881 if (wifi_pkg->package.count !=
882 hdr_size + profile_size * num_profiles) {
883 ret = -EINVAL;
884 goto out_free;
885 }
886 }
887 goto read_table;
888 }
889 }
890
891 if (idx < ARRAY_SIZE(rev_data))
892 ret = PTR_ERR(wifi_pkg);
893 else
894 ret = -ENOENT;
895 goto out_free;
896
897read_table:
898 fwrt->geo_rev = tbl_rev;
899 for (i = 0; i < num_profiles; i++) {
900 for (j = 0; j < ACPI_GEO_NUM_BANDS_REV2; j++) {
901 union acpi_object *entry;
902
903 /*
904 * num_bands is either 2 or 3, if it's only 2 then
905 * fill the third band (6 GHz) with the values from
906 * 5 GHz (second band)
907 */
908 if (j >= num_bands) {
909 fwrt->geo_profiles[i].bands[j].max =
910 fwrt->geo_profiles[i].bands[1].max;
911 } else {
912 entry = &wifi_pkg->package.elements[entry_idx];
913 entry_idx++;
914 if (entry->type != ACPI_TYPE_INTEGER ||
915 entry->integer.value > U8_MAX) {
916 ret = -EINVAL;
917 goto out_free;
918 }
919
920 fwrt->geo_profiles[i].bands[j].max =
921 entry->integer.value;
922 }
923
924 for (k = 0; k < ACPI_GEO_NUM_CHAINS; k++) {
925 /* same here as above */
926 if (j >= num_bands) {
927 fwrt->geo_profiles[i].bands[j].chains[k] =
928 fwrt->geo_profiles[i].bands[1].chains[k];
929 } else {
930 entry = &wifi_pkg->package.elements[entry_idx];
931 entry_idx++;
932 if (entry->type != ACPI_TYPE_INTEGER ||
933 entry->integer.value > U8_MAX) {
934 ret = -EINVAL;
935 goto out_free;
936 }
937
938 fwrt->geo_profiles[i].bands[j].chains[k] =
939 entry->integer.value;
940 }
941 }
942 }
943 }
944
945 fwrt->geo_num_profiles = num_profiles;
946 fwrt->geo_enabled = true;
947 ret = 0;
948out_free:
949 kfree(data);
950 return ret;
951}
952IWL_EXPORT_SYMBOL(iwl_sar_get_wgds_table);
953
954bool iwl_sar_geo_support(struct iwl_fw_runtime *fwrt)
955{
956 /*
957 * The PER_CHAIN_LIMIT_OFFSET_CMD command is not supported on
958 * earlier firmware versions. Unfortunately, we don't have a
959 * TLV API flag to rely on, so rely on the major version which
960 * is in the first byte of ucode_ver. This was implemented
961 * initially on version 38 and then backported to 17. It was
962 * also backported to 29, but only for 7265D devices. The
963 * intention was to have it in 36 as well, but not all 8000
964 * family got this feature enabled. The 8000 family is the
965 * only one using version 36, so skip this version entirely.
966 */
967 return IWL_UCODE_SERIAL(fwrt->fw->ucode_ver) >= 38 ||
968 (IWL_UCODE_SERIAL(fwrt->fw->ucode_ver) == 17 &&
969 fwrt->trans->hw_rev != CSR_HW_REV_TYPE_3160) ||
970 (IWL_UCODE_SERIAL(fwrt->fw->ucode_ver) == 29 &&
971 ((fwrt->trans->hw_rev & CSR_HW_REV_TYPE_MSK) ==
972 CSR_HW_REV_TYPE_7265D));
973}
974IWL_EXPORT_SYMBOL(iwl_sar_geo_support);
975
976int iwl_sar_geo_init(struct iwl_fw_runtime *fwrt,
977 struct iwl_per_chain_offset *table,
978 u32 n_bands, u32 n_profiles)
979{
980 int i, j;
981
982 if (!fwrt->geo_enabled)
983 return -ENODATA;
984
985 if (!iwl_sar_geo_support(fwrt))
986 return -EOPNOTSUPP;
987
988 for (i = 0; i < n_profiles; i++) {
989 for (j = 0; j < n_bands; j++) {
990 struct iwl_per_chain_offset *chain =
991 &table[i * n_bands + j];
992
993 chain->max_tx_power =
994 cpu_to_le16(fwrt->geo_profiles[i].bands[j].max);
995 chain->chain_a = fwrt->geo_profiles[i].bands[j].chains[0];
996 chain->chain_b = fwrt->geo_profiles[i].bands[j].chains[1];
997 IWL_DEBUG_RADIO(fwrt,
998 "SAR geographic profile[%d] Band[%d]: chain A = %d chain B = %d max_tx_power = %d\n",
999 i, j,
1000 fwrt->geo_profiles[i].bands[j].chains[0],
1001 fwrt->geo_profiles[i].bands[j].chains[1],
1002 fwrt->geo_profiles[i].bands[j].max);
1003 }
1004 }
1005
1006 return 0;
1007}
1008IWL_EXPORT_SYMBOL(iwl_sar_geo_init);
1009
1010__le32 iwl_acpi_get_lari_config_bitmap(struct iwl_fw_runtime *fwrt)
1011{
1012 int ret;
1013 u8 value;
1014 u32 val;
1015 __le32 config_bitmap = 0;
1016
1017 /*
1018 * Evaluate func 'DSM_FUNC_ENABLE_INDONESIA_5G2'.
1019 * Setting config_bitmap Indonesia bit is valid only for HR/JF.
1020 */
1021 switch (CSR_HW_RFID_TYPE(fwrt->trans->hw_rf_id)) {
1022 case IWL_CFG_RF_TYPE_HR1:
1023 case IWL_CFG_RF_TYPE_HR2:
1024 case IWL_CFG_RF_TYPE_JF1:
1025 case IWL_CFG_RF_TYPE_JF2:
1026 ret = iwl_acpi_get_dsm_u8(fwrt->dev, 0,
1027 DSM_FUNC_ENABLE_INDONESIA_5G2,
1028 &iwl_guid, &value);
1029
1030 if (!ret && value == DSM_VALUE_INDONESIA_ENABLE)
1031 config_bitmap |=
1032 cpu_to_le32(LARI_CONFIG_ENABLE_5G2_IN_INDONESIA_MSK);
1033 break;
1034 default:
1035 break;
1036 }
1037
1038 /*
1039 ** Evaluate func 'DSM_FUNC_DISABLE_SRD'
1040 */
1041 ret = iwl_acpi_get_dsm_u8(fwrt->dev, 0,
1042 DSM_FUNC_DISABLE_SRD,
1043 &iwl_guid, &value);
1044 if (!ret) {
1045 if (value == DSM_VALUE_SRD_PASSIVE)
1046 config_bitmap |=
1047 cpu_to_le32(LARI_CONFIG_CHANGE_ETSI_TO_PASSIVE_MSK);
1048 else if (value == DSM_VALUE_SRD_DISABLE)
1049 config_bitmap |=
1050 cpu_to_le32(LARI_CONFIG_CHANGE_ETSI_TO_DISABLED_MSK);
1051 }
1052
1053 if (fw_has_capa(&fwrt->fw->ucode_capa,
1054 IWL_UCODE_TLV_CAPA_CHINA_22_REG_SUPPORT)) {
1055 /*
1056 ** Evaluate func 'DSM_FUNC_REGULATORY_CONFIG'
1057 */
1058 ret = iwl_acpi_get_dsm_u32(fwrt->dev, 0,
1059 DSM_FUNC_REGULATORY_CONFIG,
1060 &iwl_guid, &val);
1061 /*
1062 * China 2022 enable if the BIOS object does not exist or
1063 * if it is enabled in BIOS.
1064 */
1065 if (ret < 0 || val & DSM_MASK_CHINA_22_REG)
1066 config_bitmap |=
1067 cpu_to_le32(LARI_CONFIG_ENABLE_CHINA_22_REG_SUPPORT_MSK);
1068 }
1069
1070 return config_bitmap;
1071}
1072IWL_EXPORT_SYMBOL(iwl_acpi_get_lari_config_bitmap);
1073
1074int iwl_acpi_get_ppag_table(struct iwl_fw_runtime *fwrt)
1075{
1076 union acpi_object *wifi_pkg, *data, *flags;
1077 int i, j, ret, tbl_rev, num_sub_bands = 0;
1078 int idx = 2;
1079 u8 cmd_ver;
1080
1081 fwrt->ppag_flags = 0;
1082 fwrt->ppag_table_valid = false;
1083
1084 data = iwl_acpi_get_object(fwrt->dev, ACPI_PPAG_METHOD);
1085 if (IS_ERR(data))
1086 return PTR_ERR(data);
1087
1088 /* try to read ppag table rev 2 or 1 (both have the same data size) */
1089 wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
1090 ACPI_PPAG_WIFI_DATA_SIZE_V2, &tbl_rev);
1091
1092 if (!IS_ERR(wifi_pkg)) {
1093 if (tbl_rev == 1 || tbl_rev == 2) {
1094 num_sub_bands = IWL_NUM_SUB_BANDS_V2;
1095 IWL_DEBUG_RADIO(fwrt,
1096 "Reading PPAG table v2 (tbl_rev=%d)\n",
1097 tbl_rev);
1098 goto read_table;
1099 } else {
1100 ret = -EINVAL;
1101 goto out_free;
1102 }
1103 }
1104
1105 /* try to read ppag table revision 0 */
1106 wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
1107 ACPI_PPAG_WIFI_DATA_SIZE_V1, &tbl_rev);
1108
1109 if (!IS_ERR(wifi_pkg)) {
1110 if (tbl_rev != 0) {
1111 ret = -EINVAL;
1112 goto out_free;
1113 }
1114 num_sub_bands = IWL_NUM_SUB_BANDS_V1;
1115 IWL_DEBUG_RADIO(fwrt, "Reading PPAG table v1 (tbl_rev=0)\n");
1116 goto read_table;
1117 }
1118
1119 ret = PTR_ERR(wifi_pkg);
1120 goto out_free;
1121
1122read_table:
1123 fwrt->ppag_ver = tbl_rev;
1124 flags = &wifi_pkg->package.elements[1];
1125
1126 if (flags->type != ACPI_TYPE_INTEGER) {
1127 ret = -EINVAL;
1128 goto out_free;
1129 }
1130
1131 fwrt->ppag_flags = flags->integer.value & ACPI_PPAG_MASK;
1132 cmd_ver = iwl_fw_lookup_cmd_ver(fwrt->fw,
1133 WIDE_ID(PHY_OPS_GROUP,
1134 PER_PLATFORM_ANT_GAIN_CMD),
1135 IWL_FW_CMD_VER_UNKNOWN);
1136 if (cmd_ver == IWL_FW_CMD_VER_UNKNOWN) {
1137 ret = -EINVAL;
1138 goto out_free;
1139 }
1140 if (!fwrt->ppag_flags && cmd_ver <= 3) {
1141 ret = 0;
1142 goto out_free;
1143 }
1144
1145 /*
1146 * read, verify gain values and save them into the PPAG table.
1147 * first sub-band (j=0) corresponds to Low-Band (2.4GHz), and the
1148 * following sub-bands to High-Band (5GHz).
1149 */
1150 for (i = 0; i < IWL_NUM_CHAIN_LIMITS; i++) {
1151 for (j = 0; j < num_sub_bands; j++) {
1152 union acpi_object *ent;
1153
1154 ent = &wifi_pkg->package.elements[idx++];
1155 if (ent->type != ACPI_TYPE_INTEGER) {
1156 ret = -EINVAL;
1157 goto out_free;
1158 }
1159
1160 fwrt->ppag_chains[i].subbands[j] = ent->integer.value;
1161 /* from ver 4 the fw deals with out of range values */
1162 if (cmd_ver >= 4)
1163 continue;
1164 if ((j == 0 &&
1165 (fwrt->ppag_chains[i].subbands[j] > ACPI_PPAG_MAX_LB ||
1166 fwrt->ppag_chains[i].subbands[j] < ACPI_PPAG_MIN_LB)) ||
1167 (j != 0 &&
1168 (fwrt->ppag_chains[i].subbands[j] > ACPI_PPAG_MAX_HB ||
1169 fwrt->ppag_chains[i].subbands[j] < ACPI_PPAG_MIN_HB))) {
1170 ret = -EINVAL;
1171 goto out_free;
1172 }
1173 }
1174 }
1175
1176 fwrt->ppag_table_valid = true;
1177 ret = 0;
1178
1179out_free:
1180 kfree(data);
1181 return ret;
1182}
1183IWL_EXPORT_SYMBOL(iwl_acpi_get_ppag_table);
1184
1185int iwl_read_ppag_table(struct iwl_fw_runtime *fwrt, union iwl_ppag_table_cmd *cmd,
1186 int *cmd_size)
1187{
1188 u8 cmd_ver;
1189 int i, j, num_sub_bands;
1190 s8 *gain;
1191
1192 /* many firmware images for JF lie about this */
1193 if (CSR_HW_RFID_TYPE(fwrt->trans->hw_rf_id) ==
1194 CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_JF))
1195 return -EOPNOTSUPP;
1196
1197 if (!fw_has_capa(&fwrt->fw->ucode_capa, IWL_UCODE_TLV_CAPA_SET_PPAG)) {
1198 IWL_DEBUG_RADIO(fwrt,
1199 "PPAG capability not supported by FW, command not sent.\n");
1200 return -EINVAL;
1201 }
1202
1203 cmd_ver = iwl_fw_lookup_cmd_ver(fwrt->fw,
1204 WIDE_ID(PHY_OPS_GROUP,
1205 PER_PLATFORM_ANT_GAIN_CMD),
1206 IWL_FW_CMD_VER_UNKNOWN);
1207 if (!fwrt->ppag_table_valid || (cmd_ver <= 3 && !fwrt->ppag_flags)) {
1208 IWL_DEBUG_RADIO(fwrt, "PPAG not enabled, command not sent.\n");
1209 return -EINVAL;
1210 }
1211
1212 /* The 'flags' field is the same in v1 and in v2 so we can just
1213 * use v1 to access it.
1214 */
1215 cmd->v1.flags = cpu_to_le32(fwrt->ppag_flags);
1216
1217 IWL_DEBUG_RADIO(fwrt, "PPAG cmd ver is %d\n", cmd_ver);
1218 if (cmd_ver == 1) {
1219 num_sub_bands = IWL_NUM_SUB_BANDS_V1;
1220 gain = cmd->v1.gain[0];
1221 *cmd_size = sizeof(cmd->v1);
1222 if (fwrt->ppag_ver == 1 || fwrt->ppag_ver == 2) {
1223 /* in this case FW supports revision 0 */
1224 IWL_DEBUG_RADIO(fwrt,
1225 "PPAG table rev is %d, send truncated table\n",
1226 fwrt->ppag_ver);
1227 }
1228 } else if (cmd_ver >= 2 && cmd_ver <= 4) {
1229 num_sub_bands = IWL_NUM_SUB_BANDS_V2;
1230 gain = cmd->v2.gain[0];
1231 *cmd_size = sizeof(cmd->v2);
1232 if (fwrt->ppag_ver == 0) {
1233 /* in this case FW supports revisions 1 or 2 */
1234 IWL_DEBUG_RADIO(fwrt,
1235 "PPAG table rev is 0, send padded table\n");
1236 }
1237 } else {
1238 IWL_DEBUG_RADIO(fwrt, "Unsupported PPAG command version\n");
1239 return -EINVAL;
1240 }
1241
1242 /* ppag mode */
1243 IWL_DEBUG_RADIO(fwrt,
1244 "PPAG MODE bits were read from bios: %d\n",
1245 cmd->v1.flags & cpu_to_le32(ACPI_PPAG_MASK));
1246 if ((cmd_ver == 1 && !fw_has_capa(&fwrt->fw->ucode_capa,
1247 IWL_UCODE_TLV_CAPA_PPAG_CHINA_BIOS_SUPPORT)) ||
1248 (cmd_ver == 2 && fwrt->ppag_ver == 2)) {
1249 cmd->v1.flags &= cpu_to_le32(IWL_PPAG_ETSI_MASK);
1250 IWL_DEBUG_RADIO(fwrt, "masking ppag China bit\n");
1251 } else {
1252 IWL_DEBUG_RADIO(fwrt, "isn't masking ppag China bit\n");
1253 }
1254
1255 IWL_DEBUG_RADIO(fwrt,
1256 "PPAG MODE bits going to be sent: %d\n",
1257 cmd->v1.flags & cpu_to_le32(ACPI_PPAG_MASK));
1258
1259 for (i = 0; i < IWL_NUM_CHAIN_LIMITS; i++) {
1260 for (j = 0; j < num_sub_bands; j++) {
1261 gain[i * num_sub_bands + j] =
1262 fwrt->ppag_chains[i].subbands[j];
1263 IWL_DEBUG_RADIO(fwrt,
1264 "PPAG table: chain[%d] band[%d]: gain = %d\n",
1265 i, j, gain[i * num_sub_bands + j]);
1266 }
1267 }
1268
1269 return 0;
1270}
1271IWL_EXPORT_SYMBOL(iwl_read_ppag_table);
1272
1273bool iwl_acpi_is_ppag_approved(struct iwl_fw_runtime *fwrt)
1274{
1275
1276 if (!dmi_check_system(dmi_ppag_approved_list)) {
1277 IWL_DEBUG_RADIO(fwrt,
1278 "System vendor '%s' is not in the approved list, disabling PPAG.\n",
1279 dmi_get_system_info(DMI_SYS_VENDOR));
1280 fwrt->ppag_flags = 0;
1281 return false;
1282 }
1283
1284 return true;
1285}
1286IWL_EXPORT_SYMBOL(iwl_acpi_is_ppag_approved);
1287
1288void iwl_acpi_get_phy_filters(struct iwl_fw_runtime *fwrt,
1289 struct iwl_phy_specific_cfg *filters)
1290{
1291 struct iwl_phy_specific_cfg tmp = {};
1292 union acpi_object *wifi_pkg, *data;
1293 int tbl_rev, i;
1294
1295 data = iwl_acpi_get_object(fwrt->dev, ACPI_WPFC_METHOD);
1296 if (IS_ERR(data))
1297 return;
1298
1299 /* try to read wtas table revision 1 or revision 0*/
1300 wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
1301 ACPI_WPFC_WIFI_DATA_SIZE,
1302 &tbl_rev);
1303 if (IS_ERR(wifi_pkg))
1304 goto out_free;
1305
1306 if (tbl_rev != 0)
1307 goto out_free;
1308
1309 BUILD_BUG_ON(ARRAY_SIZE(filters->filter_cfg_chains) != ACPI_WPFC_WIFI_DATA_SIZE);
1310
1311 for (i = 0; i < ARRAY_SIZE(filters->filter_cfg_chains); i++) {
1312 if (wifi_pkg->package.elements[i].type != ACPI_TYPE_INTEGER)
1313 return;
1314 tmp.filter_cfg_chains[i] =
1315 cpu_to_le32(wifi_pkg->package.elements[i].integer.value);
1316 }
1317
1318 IWL_DEBUG_RADIO(fwrt, "Loaded WPFC filter config from ACPI\n");
1319 *filters = tmp;
1320out_free:
1321 kfree(data);
1322}
1323IWL_EXPORT_SYMBOL(iwl_acpi_get_phy_filters);
1/******************************************************************************
2 *
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
8 * Copyright(c) 2017 Intel Deutschland GmbH
9 * Copyright (C) 2019 - 2020 Intel Corporation
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * The full GNU General Public License is included in this distribution
21 * in the file called COPYING.
22 *
23 * Contact Information:
24 * Intel Linux Wireless <linuxwifi@intel.com>
25 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26 *
27 * BSD LICENSE
28 *
29 * Copyright(c) 2017 Intel Deutschland GmbH
30 * Copyright (C) 2019 - 2020 Intel Corporation
31 * All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 *
37 * * Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * * Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in
41 * the documentation and/or other materials provided with the
42 * distribution.
43 * * Neither the name Intel Corporation nor the names of its
44 * contributors may be used to endorse or promote products derived
45 * from this software without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
48 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
49 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
50 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
51 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
54 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
55 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
56 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
57 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 *
59 *****************************************************************************/
60
61#include <linux/uuid.h>
62#include "iwl-drv.h"
63#include "iwl-debug.h"
64#include "acpi.h"
65#include "fw/runtime.h"
66
67static const guid_t intel_wifi_guid = GUID_INIT(0xF21202BF, 0x8F78, 0x4DC6,
68 0xA5, 0xB3, 0x1F, 0x73,
69 0x8E, 0x28, 0x5A, 0xDE);
70
71static int iwl_acpi_get_handle(struct device *dev, acpi_string method,
72 acpi_handle *ret_handle)
73{
74 acpi_handle root_handle;
75 acpi_status status;
76
77 root_handle = ACPI_HANDLE(dev);
78 if (!root_handle) {
79 IWL_DEBUG_DEV_RADIO(dev,
80 "ACPI: Could not retrieve root port handle\n");
81 return -ENOENT;
82 }
83
84 status = acpi_get_handle(root_handle, method, ret_handle);
85 if (ACPI_FAILURE(status)) {
86 IWL_DEBUG_DEV_RADIO(dev,
87 "ACPI: %s method not found\n", method);
88 return -ENOENT;
89 }
90 return 0;
91}
92
93void *iwl_acpi_get_object(struct device *dev, acpi_string method)
94{
95 struct acpi_buffer buf = {ACPI_ALLOCATE_BUFFER, NULL};
96 acpi_handle handle;
97 acpi_status status;
98 int ret;
99
100 ret = iwl_acpi_get_handle(dev, method, &handle);
101 if (ret)
102 return ERR_PTR(-ENOENT);
103
104 /* Call the method with no arguments */
105 status = acpi_evaluate_object(handle, NULL, NULL, &buf);
106 if (ACPI_FAILURE(status)) {
107 IWL_DEBUG_DEV_RADIO(dev,
108 "ACPI: %s method invocation failed (status: 0x%x)\n",
109 method, status);
110 return ERR_PTR(-ENOENT);
111 }
112 return buf.pointer;
113}
114IWL_EXPORT_SYMBOL(iwl_acpi_get_object);
115
116/**
117* Generic function for evaluating a method defined in the device specific
118* method (DSM) interface. The returned acpi object must be freed by calling
119* function.
120*/
121void *iwl_acpi_get_dsm_object(struct device *dev, int rev, int func,
122 union acpi_object *args)
123{
124 union acpi_object *obj;
125
126 obj = acpi_evaluate_dsm(ACPI_HANDLE(dev), &intel_wifi_guid, rev, func,
127 args);
128 if (!obj) {
129 IWL_DEBUG_DEV_RADIO(dev,
130 "ACPI: DSM method invocation failed (rev: %d, func:%d)\n",
131 rev, func);
132 return ERR_PTR(-ENOENT);
133 }
134 return obj;
135}
136
137/**
138 * Evaluate a DSM with no arguments and a single u8 return value (inside a
139 * buffer object), verify and return that value.
140 */
141int iwl_acpi_get_dsm_u8(struct device *dev, int rev, int func)
142{
143 union acpi_object *obj;
144 int ret;
145
146 obj = iwl_acpi_get_dsm_object(dev, rev, func, NULL);
147 if (IS_ERR(obj))
148 return -ENOENT;
149
150 if (obj->type != ACPI_TYPE_BUFFER) {
151 IWL_DEBUG_DEV_RADIO(dev,
152 "ACPI: DSM method did not return a valid object, type=%d\n",
153 obj->type);
154 ret = -EINVAL;
155 goto out;
156 }
157
158 if (obj->buffer.length != sizeof(u8)) {
159 IWL_DEBUG_DEV_RADIO(dev,
160 "ACPI: DSM method returned invalid buffer, length=%d\n",
161 obj->buffer.length);
162 ret = -EINVAL;
163 goto out;
164 }
165
166 ret = obj->buffer.pointer[0];
167 IWL_DEBUG_DEV_RADIO(dev,
168 "ACPI: DSM method evaluated: func=%d, ret=%d\n",
169 func, ret);
170out:
171 ACPI_FREE(obj);
172 return ret;
173}
174IWL_EXPORT_SYMBOL(iwl_acpi_get_dsm_u8);
175
176union acpi_object *iwl_acpi_get_wifi_pkg(struct device *dev,
177 union acpi_object *data,
178 int data_size, int *tbl_rev)
179{
180 int i;
181 union acpi_object *wifi_pkg;
182
183 /*
184 * We need at least one entry in the wifi package that
185 * describes the domain, and one more entry, otherwise there's
186 * no point in reading it.
187 */
188 if (WARN_ON_ONCE(data_size < 2))
189 return ERR_PTR(-EINVAL);
190
191 /*
192 * We need at least two packages, one for the revision and one
193 * for the data itself. Also check that the revision is valid
194 * (i.e. it is an integer smaller than 2, as we currently support only
195 * 2 revisions).
196 */
197 if (data->type != ACPI_TYPE_PACKAGE ||
198 data->package.count < 2 ||
199 data->package.elements[0].type != ACPI_TYPE_INTEGER ||
200 data->package.elements[0].integer.value > 1) {
201 IWL_DEBUG_DEV_RADIO(dev, "Unsupported packages structure\n");
202 return ERR_PTR(-EINVAL);
203 }
204
205 *tbl_rev = data->package.elements[0].integer.value;
206
207 /* loop through all the packages to find the one for WiFi */
208 for (i = 1; i < data->package.count; i++) {
209 union acpi_object *domain;
210
211 wifi_pkg = &data->package.elements[i];
212
213 /* skip entries that are not a package with the right size */
214 if (wifi_pkg->type != ACPI_TYPE_PACKAGE ||
215 wifi_pkg->package.count != data_size)
216 continue;
217
218 domain = &wifi_pkg->package.elements[0];
219 if (domain->type == ACPI_TYPE_INTEGER &&
220 domain->integer.value == ACPI_WIFI_DOMAIN)
221 goto found;
222 }
223
224 return ERR_PTR(-ENOENT);
225
226found:
227 return wifi_pkg;
228}
229IWL_EXPORT_SYMBOL(iwl_acpi_get_wifi_pkg);
230
231int iwl_acpi_get_tas(struct iwl_fw_runtime *fwrt,
232 __le32 *black_list_array,
233 int *black_list_size)
234{
235 union acpi_object *wifi_pkg, *data;
236 int ret, tbl_rev, i;
237 bool enabled;
238
239 data = iwl_acpi_get_object(fwrt->dev, ACPI_WTAS_METHOD);
240 if (IS_ERR(data))
241 return PTR_ERR(data);
242
243 wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
244 ACPI_WTAS_WIFI_DATA_SIZE,
245 &tbl_rev);
246 if (IS_ERR(wifi_pkg)) {
247 ret = PTR_ERR(wifi_pkg);
248 goto out_free;
249 }
250
251 if (wifi_pkg->package.elements[0].type != ACPI_TYPE_INTEGER ||
252 tbl_rev != 0) {
253 ret = -EINVAL;
254 goto out_free;
255 }
256
257 enabled = !!wifi_pkg->package.elements[0].integer.value;
258
259 if (!enabled) {
260 *black_list_size = -1;
261 IWL_DEBUG_RADIO(fwrt, "TAS not enabled\n");
262 ret = 0;
263 goto out_free;
264 }
265
266 if (wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER ||
267 wifi_pkg->package.elements[1].integer.value >
268 APCI_WTAS_BLACK_LIST_MAX) {
269 IWL_DEBUG_RADIO(fwrt, "TAS invalid array size %llu\n",
270 wifi_pkg->package.elements[1].integer.value);
271 ret = -EINVAL;
272 goto out_free;
273 }
274 *black_list_size = wifi_pkg->package.elements[1].integer.value;
275
276 IWL_DEBUG_RADIO(fwrt, "TAS array size %d\n", *black_list_size);
277 if (*black_list_size > APCI_WTAS_BLACK_LIST_MAX) {
278 IWL_DEBUG_RADIO(fwrt, "TAS invalid array size value %u\n",
279 *black_list_size);
280 ret = -EINVAL;
281 goto out_free;
282 }
283
284 for (i = 0; i < *black_list_size; i++) {
285 u32 country;
286
287 if (wifi_pkg->package.elements[2 + i].type !=
288 ACPI_TYPE_INTEGER) {
289 IWL_DEBUG_RADIO(fwrt,
290 "TAS invalid array elem %d\n", 2 + i);
291 ret = -EINVAL;
292 goto out_free;
293 }
294
295 country = wifi_pkg->package.elements[2 + i].integer.value;
296 black_list_array[i] = cpu_to_le32(country);
297 IWL_DEBUG_RADIO(fwrt, "TAS black list country %d\n", country);
298 }
299
300 ret = 0;
301out_free:
302 kfree(data);
303 return ret;
304}
305IWL_EXPORT_SYMBOL(iwl_acpi_get_tas);
306
307int iwl_acpi_get_mcc(struct device *dev, char *mcc)
308{
309 union acpi_object *wifi_pkg, *data;
310 u32 mcc_val;
311 int ret, tbl_rev;
312
313 data = iwl_acpi_get_object(dev, ACPI_WRDD_METHOD);
314 if (IS_ERR(data))
315 return PTR_ERR(data);
316
317 wifi_pkg = iwl_acpi_get_wifi_pkg(dev, data, ACPI_WRDD_WIFI_DATA_SIZE,
318 &tbl_rev);
319 if (IS_ERR(wifi_pkg)) {
320 ret = PTR_ERR(wifi_pkg);
321 goto out_free;
322 }
323
324 if (wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER ||
325 tbl_rev != 0) {
326 ret = -EINVAL;
327 goto out_free;
328 }
329
330 mcc_val = wifi_pkg->package.elements[1].integer.value;
331
332 mcc[0] = (mcc_val >> 8) & 0xff;
333 mcc[1] = mcc_val & 0xff;
334 mcc[2] = '\0';
335
336 ret = 0;
337out_free:
338 kfree(data);
339 return ret;
340}
341IWL_EXPORT_SYMBOL(iwl_acpi_get_mcc);
342
343u64 iwl_acpi_get_pwr_limit(struct device *dev)
344{
345 union acpi_object *data, *wifi_pkg;
346 u64 dflt_pwr_limit;
347 int tbl_rev;
348
349 data = iwl_acpi_get_object(dev, ACPI_SPLC_METHOD);
350 if (IS_ERR(data)) {
351 dflt_pwr_limit = 0;
352 goto out;
353 }
354
355 wifi_pkg = iwl_acpi_get_wifi_pkg(dev, data,
356 ACPI_SPLC_WIFI_DATA_SIZE, &tbl_rev);
357 if (IS_ERR(wifi_pkg) || tbl_rev != 0 ||
358 wifi_pkg->package.elements[1].integer.value != ACPI_TYPE_INTEGER) {
359 dflt_pwr_limit = 0;
360 goto out_free;
361 }
362
363 dflt_pwr_limit = wifi_pkg->package.elements[1].integer.value;
364out_free:
365 kfree(data);
366out:
367 return dflt_pwr_limit;
368}
369IWL_EXPORT_SYMBOL(iwl_acpi_get_pwr_limit);
370
371int iwl_acpi_get_eckv(struct device *dev, u32 *extl_clk)
372{
373 union acpi_object *wifi_pkg, *data;
374 int ret, tbl_rev;
375
376 data = iwl_acpi_get_object(dev, ACPI_ECKV_METHOD);
377 if (IS_ERR(data))
378 return PTR_ERR(data);
379
380 wifi_pkg = iwl_acpi_get_wifi_pkg(dev, data, ACPI_ECKV_WIFI_DATA_SIZE,
381 &tbl_rev);
382 if (IS_ERR(wifi_pkg)) {
383 ret = PTR_ERR(wifi_pkg);
384 goto out_free;
385 }
386
387 if (wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER ||
388 tbl_rev != 0) {
389 ret = -EINVAL;
390 goto out_free;
391 }
392
393 *extl_clk = wifi_pkg->package.elements[1].integer.value;
394
395 ret = 0;
396
397out_free:
398 kfree(data);
399 return ret;
400}
401IWL_EXPORT_SYMBOL(iwl_acpi_get_eckv);
402
403int iwl_sar_set_profile(union acpi_object *table,
404 struct iwl_sar_profile *profile,
405 bool enabled)
406{
407 int i;
408
409 profile->enabled = enabled;
410
411 for (i = 0; i < ACPI_SAR_TABLE_SIZE; i++) {
412 if (table[i].type != ACPI_TYPE_INTEGER ||
413 table[i].integer.value > U8_MAX)
414 return -EINVAL;
415
416 profile->table[i] = table[i].integer.value;
417 }
418
419 return 0;
420}
421IWL_EXPORT_SYMBOL(iwl_sar_set_profile);
422
423int iwl_sar_select_profile(struct iwl_fw_runtime *fwrt,
424 __le16 per_chain_restriction[][IWL_NUM_SUB_BANDS],
425 int prof_a, int prof_b)
426{
427 int i, j, idx;
428 int profs[ACPI_SAR_NUM_CHAIN_LIMITS] = { prof_a, prof_b };
429
430 BUILD_BUG_ON(ACPI_SAR_NUM_CHAIN_LIMITS < 2);
431 BUILD_BUG_ON(ACPI_SAR_NUM_CHAIN_LIMITS * ACPI_SAR_NUM_SUB_BANDS !=
432 ACPI_SAR_TABLE_SIZE);
433
434 for (i = 0; i < ACPI_SAR_NUM_CHAIN_LIMITS; i++) {
435 struct iwl_sar_profile *prof;
436
437 /* don't allow SAR to be disabled (profile 0 means disable) */
438 if (profs[i] == 0)
439 return -EPERM;
440
441 /* we are off by one, so allow up to ACPI_SAR_PROFILE_NUM */
442 if (profs[i] > ACPI_SAR_PROFILE_NUM)
443 return -EINVAL;
444
445 /* profiles go from 1 to 4, so decrement to access the array */
446 prof = &fwrt->sar_profiles[profs[i] - 1];
447
448 /* if the profile is disabled, do nothing */
449 if (!prof->enabled) {
450 IWL_DEBUG_RADIO(fwrt, "SAR profile %d is disabled.\n",
451 profs[i]);
452 /*
453 * if one of the profiles is disabled, we
454 * ignore all of them and return 1 to
455 * differentiate disabled from other failures.
456 */
457 return 1;
458 }
459
460 IWL_DEBUG_INFO(fwrt,
461 "SAR EWRD: chain %d profile index %d\n",
462 i, profs[i]);
463 IWL_DEBUG_RADIO(fwrt, " Chain[%d]:\n", i);
464 for (j = 0; j < ACPI_SAR_NUM_SUB_BANDS; j++) {
465 idx = (i * ACPI_SAR_NUM_SUB_BANDS) + j;
466 per_chain_restriction[i][j] =
467 cpu_to_le16(prof->table[idx]);
468 IWL_DEBUG_RADIO(fwrt, " Band[%d] = %d * .125dBm\n",
469 j, prof->table[idx]);
470 }
471 }
472
473 return 0;
474}
475IWL_EXPORT_SYMBOL(iwl_sar_select_profile);
476
477int iwl_sar_get_wrds_table(struct iwl_fw_runtime *fwrt)
478{
479 union acpi_object *wifi_pkg, *table, *data;
480 bool enabled;
481 int ret, tbl_rev;
482
483 data = iwl_acpi_get_object(fwrt->dev, ACPI_WRDS_METHOD);
484 if (IS_ERR(data))
485 return PTR_ERR(data);
486
487 wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
488 ACPI_WRDS_WIFI_DATA_SIZE, &tbl_rev);
489 if (IS_ERR(wifi_pkg) || tbl_rev != 0) {
490 ret = PTR_ERR(wifi_pkg);
491 goto out_free;
492 }
493
494 if (wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER) {
495 ret = -EINVAL;
496 goto out_free;
497 }
498
499 enabled = !!(wifi_pkg->package.elements[1].integer.value);
500
501 /* position of the actual table */
502 table = &wifi_pkg->package.elements[2];
503
504 /* The profile from WRDS is officially profile 1, but goes
505 * into sar_profiles[0] (because we don't have a profile 0).
506 */
507 ret = iwl_sar_set_profile(table, &fwrt->sar_profiles[0], enabled);
508out_free:
509 kfree(data);
510 return ret;
511}
512IWL_EXPORT_SYMBOL(iwl_sar_get_wrds_table);
513
514int iwl_sar_get_ewrd_table(struct iwl_fw_runtime *fwrt)
515{
516 union acpi_object *wifi_pkg, *data;
517 bool enabled;
518 int i, n_profiles, tbl_rev, pos;
519 int ret = 0;
520
521 data = iwl_acpi_get_object(fwrt->dev, ACPI_EWRD_METHOD);
522 if (IS_ERR(data))
523 return PTR_ERR(data);
524
525 wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
526 ACPI_EWRD_WIFI_DATA_SIZE, &tbl_rev);
527 if (IS_ERR(wifi_pkg) || tbl_rev != 0) {
528 ret = PTR_ERR(wifi_pkg);
529 goto out_free;
530 }
531
532 if (wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER ||
533 wifi_pkg->package.elements[2].type != ACPI_TYPE_INTEGER) {
534 ret = -EINVAL;
535 goto out_free;
536 }
537
538 enabled = !!(wifi_pkg->package.elements[1].integer.value);
539 n_profiles = wifi_pkg->package.elements[2].integer.value;
540
541 /*
542 * Check the validity of n_profiles. The EWRD profiles start
543 * from index 1, so the maximum value allowed here is
544 * ACPI_SAR_PROFILES_NUM - 1.
545 */
546 if (n_profiles <= 0 || n_profiles >= ACPI_SAR_PROFILE_NUM) {
547 ret = -EINVAL;
548 goto out_free;
549 }
550
551 /* the tables start at element 3 */
552 pos = 3;
553
554 for (i = 0; i < n_profiles; i++) {
555 /* The EWRD profiles officially go from 2 to 4, but we
556 * save them in sar_profiles[1-3] (because we don't
557 * have profile 0). So in the array we start from 1.
558 */
559 ret = iwl_sar_set_profile(&wifi_pkg->package.elements[pos],
560 &fwrt->sar_profiles[i + 1],
561 enabled);
562 if (ret < 0)
563 break;
564
565 /* go to the next table */
566 pos += ACPI_SAR_TABLE_SIZE;
567 }
568
569out_free:
570 kfree(data);
571 return ret;
572}
573IWL_EXPORT_SYMBOL(iwl_sar_get_ewrd_table);
574
575int iwl_sar_get_wgds_table(struct iwl_fw_runtime *fwrt)
576{
577 union acpi_object *wifi_pkg, *data;
578 int i, j, ret, tbl_rev;
579 int idx = 1;
580
581 data = iwl_acpi_get_object(fwrt->dev, ACPI_WGDS_METHOD);
582 if (IS_ERR(data))
583 return PTR_ERR(data);
584
585 wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
586 ACPI_WGDS_WIFI_DATA_SIZE, &tbl_rev);
587 if (IS_ERR(wifi_pkg) || tbl_rev > 1) {
588 ret = PTR_ERR(wifi_pkg);
589 goto out_free;
590 }
591
592 fwrt->geo_rev = tbl_rev;
593 for (i = 0; i < ACPI_NUM_GEO_PROFILES; i++) {
594 for (j = 0; j < ACPI_GEO_TABLE_SIZE; j++) {
595 union acpi_object *entry;
596
597 entry = &wifi_pkg->package.elements[idx++];
598 if (entry->type != ACPI_TYPE_INTEGER ||
599 entry->integer.value > U8_MAX) {
600 ret = -EINVAL;
601 goto out_free;
602 }
603
604 fwrt->geo_profiles[i].values[j] = entry->integer.value;
605 }
606 }
607 ret = 0;
608out_free:
609 kfree(data);
610 return ret;
611}
612IWL_EXPORT_SYMBOL(iwl_sar_get_wgds_table);
613
614bool iwl_sar_geo_support(struct iwl_fw_runtime *fwrt)
615{
616 /*
617 * The GEO_TX_POWER_LIMIT command is not supported on earlier
618 * firmware versions. Unfortunately, we don't have a TLV API
619 * flag to rely on, so rely on the major version which is in
620 * the first byte of ucode_ver. This was implemented
621 * initially on version 38 and then backported to 17. It was
622 * also backported to 29, but only for 7265D devices. The
623 * intention was to have it in 36 as well, but not all 8000
624 * family got this feature enabled. The 8000 family is the
625 * only one using version 36, so skip this version entirely.
626 */
627 return IWL_UCODE_SERIAL(fwrt->fw->ucode_ver) >= 38 ||
628 IWL_UCODE_SERIAL(fwrt->fw->ucode_ver) == 17 ||
629 (IWL_UCODE_SERIAL(fwrt->fw->ucode_ver) == 29 &&
630 ((fwrt->trans->hw_rev & CSR_HW_REV_TYPE_MSK) ==
631 CSR_HW_REV_TYPE_7265D));
632}
633IWL_EXPORT_SYMBOL(iwl_sar_geo_support);
634
635int iwl_validate_sar_geo_profile(struct iwl_fw_runtime *fwrt,
636 struct iwl_host_cmd *cmd)
637{
638 struct iwl_geo_tx_power_profiles_resp *resp;
639 int ret;
640
641 resp = (void *)cmd->resp_pkt->data;
642 ret = le32_to_cpu(resp->profile_idx);
643 if (WARN_ON(ret > ACPI_NUM_GEO_PROFILES)) {
644 ret = -EIO;
645 IWL_WARN(fwrt, "Invalid geographic profile idx (%d)\n", ret);
646 }
647
648 return ret;
649}
650IWL_EXPORT_SYMBOL(iwl_validate_sar_geo_profile);
651
652int iwl_sar_geo_init(struct iwl_fw_runtime *fwrt,
653 struct iwl_per_chain_offset_group *table)
654{
655 int ret, i, j;
656
657 if (!iwl_sar_geo_support(fwrt))
658 return -EOPNOTSUPP;
659
660 ret = iwl_sar_get_wgds_table(fwrt);
661 if (ret < 0) {
662 IWL_DEBUG_RADIO(fwrt,
663 "Geo SAR BIOS table invalid or unavailable. (%d)\n",
664 ret);
665 /* we don't fail if the table is not available */
666 return -ENOENT;
667 }
668
669 BUILD_BUG_ON(ACPI_NUM_GEO_PROFILES * ACPI_WGDS_NUM_BANDS *
670 ACPI_WGDS_TABLE_SIZE + 1 != ACPI_WGDS_WIFI_DATA_SIZE);
671
672 BUILD_BUG_ON(ACPI_NUM_GEO_PROFILES > IWL_NUM_GEO_PROFILES);
673
674 for (i = 0; i < ACPI_NUM_GEO_PROFILES; i++) {
675 struct iwl_per_chain_offset *chain =
676 (struct iwl_per_chain_offset *)&table[i];
677
678 for (j = 0; j < ACPI_WGDS_NUM_BANDS; j++) {
679 u8 *value;
680
681 value = &fwrt->geo_profiles[i].values[j *
682 ACPI_GEO_PER_CHAIN_SIZE];
683 chain[j].max_tx_power = cpu_to_le16(value[0]);
684 chain[j].chain_a = value[1];
685 chain[j].chain_b = value[2];
686 IWL_DEBUG_RADIO(fwrt,
687 "SAR geographic profile[%d] Band[%d]: chain A = %d chain B = %d max_tx_power = %d\n",
688 i, j, value[1], value[2], value[0]);
689 }
690 }
691
692 return 0;
693}
694IWL_EXPORT_SYMBOL(iwl_sar_geo_init);