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 
67 static const guid_t intel_wifi_guid = GUID_INIT(0xF21202BF, 0x8F78, 0x4DC6,
68 						0xA5, 0xB3, 0x1F, 0x73,
69 						0x8E, 0x28, 0x5A, 0xDE);
70 
71 static 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 
93 void *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 }
114 IWL_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 */
121 void *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  */
141 int 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);
170 out:
171 	ACPI_FREE(obj);
172 	return ret;
173 }
174 IWL_EXPORT_SYMBOL(iwl_acpi_get_dsm_u8);
175 
176 union 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 
226 found:
227 	return wifi_pkg;
228 }
229 IWL_EXPORT_SYMBOL(iwl_acpi_get_wifi_pkg);
230 
231 int 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;
301 out_free:
302 	kfree(data);
303 	return ret;
304 }
305 IWL_EXPORT_SYMBOL(iwl_acpi_get_tas);
306 
307 int 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;
337 out_free:
338 	kfree(data);
339 	return ret;
340 }
341 IWL_EXPORT_SYMBOL(iwl_acpi_get_mcc);
342 
343 u64 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;
364 out_free:
365 	kfree(data);
366 out:
367 	return dflt_pwr_limit;
368 }
369 IWL_EXPORT_SYMBOL(iwl_acpi_get_pwr_limit);
370 
371 int 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 
397 out_free:
398 	kfree(data);
399 	return ret;
400 }
401 IWL_EXPORT_SYMBOL(iwl_acpi_get_eckv);
402 
403 int 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 }
421 IWL_EXPORT_SYMBOL(iwl_sar_set_profile);
422 
423 int 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 }
475 IWL_EXPORT_SYMBOL(iwl_sar_select_profile);
476 
477 int 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);
508 out_free:
509 	kfree(data);
510 	return ret;
511 }
512 IWL_EXPORT_SYMBOL(iwl_sar_get_wrds_table);
513 
514 int 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 
569 out_free:
570 	kfree(data);
571 	return ret;
572 }
573 IWL_EXPORT_SYMBOL(iwl_sar_get_ewrd_table);
574 
575 int 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;
608 out_free:
609 	kfree(data);
610 	return ret;
611 }
612 IWL_EXPORT_SYMBOL(iwl_sar_get_wgds_table);
613 
614 bool 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 }
633 IWL_EXPORT_SYMBOL(iwl_sar_geo_support);
634 
635 int 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 }
650 IWL_EXPORT_SYMBOL(iwl_validate_sar_geo_profile);
651 
652 int 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 }
694 IWL_EXPORT_SYMBOL(iwl_sar_geo_init);
695