xref: /freebsd/sys/contrib/dev/iwlwifi/mvm/nvm.c (revision 47ef2a131091508e049ab10cad7f91a3c1342cd9)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2012-2014, 2018-2019, 2021-2024 Intel Corporation
4  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5  * Copyright (C) 2016-2017 Intel Deutschland GmbH
6  */
7 #include <linux/firmware.h>
8 #if defined(__linux__)
9 #include <linux/rtnetlink.h>
10 #endif
11 #include "iwl-trans.h"
12 #include "iwl-csr.h"
13 #include "mvm.h"
14 #include "iwl-nvm-utils.h"
15 #include "iwl-nvm-parse.h"
16 #include "iwl-prph.h"
17 #include "fw/acpi.h"
18 
19 /* Default NVM size to read */
20 #define IWL_NVM_DEFAULT_CHUNK_SIZE (2 * 1024)
21 
22 #define NVM_WRITE_OPCODE 1
23 #define NVM_READ_OPCODE 0
24 
25 /* load nvm chunk response */
26 enum {
27 	READ_NVM_CHUNK_SUCCEED = 0,
28 	READ_NVM_CHUNK_NOT_VALID_ADDRESS = 1
29 };
30 
31 /*
32  * prepare the NVM host command w/ the pointers to the nvm buffer
33  * and send it to fw
34  */
35 static int iwl_nvm_write_chunk(struct iwl_mvm *mvm, u16 section,
36 			       u16 offset, u16 length, const u8 *data)
37 {
38 	struct iwl_nvm_access_cmd nvm_access_cmd = {
39 		.offset = cpu_to_le16(offset),
40 		.length = cpu_to_le16(length),
41 		.type = cpu_to_le16(section),
42 		.op_code = NVM_WRITE_OPCODE,
43 	};
44 	struct iwl_host_cmd cmd = {
45 		.id = NVM_ACCESS_CMD,
46 		.len = { sizeof(struct iwl_nvm_access_cmd), length },
47 		.flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
48 		.data = { &nvm_access_cmd, data },
49 		/* data may come from vmalloc, so use _DUP */
50 		.dataflags = { 0, IWL_HCMD_DFL_DUP },
51 	};
52 	struct iwl_rx_packet *pkt;
53 	struct iwl_nvm_access_resp *nvm_resp;
54 	int ret;
55 
56 	ret = iwl_mvm_send_cmd(mvm, &cmd);
57 	if (ret)
58 		return ret;
59 
60 	pkt = cmd.resp_pkt;
61 	/* Extract & check NVM write response */
62 	nvm_resp = (void *)pkt->data;
63 	if (le16_to_cpu(nvm_resp->status) != READ_NVM_CHUNK_SUCCEED) {
64 		IWL_ERR(mvm,
65 			"NVM access write command failed for section %u (status = 0x%x)\n",
66 			section, le16_to_cpu(nvm_resp->status));
67 		ret = -EIO;
68 	}
69 
70 	iwl_free_resp(&cmd);
71 	return ret;
72 }
73 
74 static int iwl_nvm_read_chunk(struct iwl_mvm *mvm, u16 section,
75 			      u16 offset, u16 length, u8 *data)
76 {
77 	struct iwl_nvm_access_cmd nvm_access_cmd = {
78 		.offset = cpu_to_le16(offset),
79 		.length = cpu_to_le16(length),
80 		.type = cpu_to_le16(section),
81 		.op_code = NVM_READ_OPCODE,
82 	};
83 	struct iwl_nvm_access_resp *nvm_resp;
84 	struct iwl_rx_packet *pkt;
85 	struct iwl_host_cmd cmd = {
86 		.id = NVM_ACCESS_CMD,
87 		.flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
88 		.data = { &nvm_access_cmd, },
89 	};
90 	int ret, bytes_read, offset_read;
91 	u8 *resp_data;
92 
93 	cmd.len[0] = sizeof(struct iwl_nvm_access_cmd);
94 
95 	ret = iwl_mvm_send_cmd(mvm, &cmd);
96 	if (ret)
97 		return ret;
98 
99 	pkt = cmd.resp_pkt;
100 
101 	/* Extract NVM response */
102 	nvm_resp = (void *)pkt->data;
103 	ret = le16_to_cpu(nvm_resp->status);
104 	bytes_read = le16_to_cpu(nvm_resp->length);
105 	offset_read = le16_to_cpu(nvm_resp->offset);
106 	resp_data = nvm_resp->data;
107 	if (ret) {
108 		if ((offset != 0) &&
109 		    (ret == READ_NVM_CHUNK_NOT_VALID_ADDRESS)) {
110 			/*
111 			 * meaning of NOT_VALID_ADDRESS:
112 			 * driver try to read chunk from address that is
113 			 * multiple of 2K and got an error since addr is empty.
114 			 * meaning of (offset != 0): driver already
115 			 * read valid data from another chunk so this case
116 			 * is not an error.
117 			 */
118 			IWL_DEBUG_EEPROM(mvm->trans->dev,
119 					 "NVM access command failed on offset 0x%x since that section size is multiple 2K\n",
120 					 offset);
121 			ret = 0;
122 		} else {
123 			IWL_DEBUG_EEPROM(mvm->trans->dev,
124 					 "NVM access command failed with status %d (device: %s)\n",
125 					 ret, mvm->trans->name);
126 			ret = -ENODATA;
127 		}
128 		goto exit;
129 	}
130 
131 	if (offset_read != offset) {
132 		IWL_ERR(mvm, "NVM ACCESS response with invalid offset %d\n",
133 			offset_read);
134 		ret = -EINVAL;
135 		goto exit;
136 	}
137 
138 	/* Write data to NVM */
139 	memcpy(data + offset, resp_data, bytes_read);
140 	ret = bytes_read;
141 
142 exit:
143 	iwl_free_resp(&cmd);
144 	return ret;
145 }
146 
147 static int iwl_nvm_write_section(struct iwl_mvm *mvm, u16 section,
148 				 const u8 *data, u16 length)
149 {
150 	int offset = 0;
151 
152 	/* copy data in chunks of 2k (and remainder if any) */
153 
154 	while (offset < length) {
155 		int chunk_size, ret;
156 
157 		chunk_size = min(IWL_NVM_DEFAULT_CHUNK_SIZE,
158 				 length - offset);
159 
160 		ret = iwl_nvm_write_chunk(mvm, section, offset,
161 					  chunk_size, data + offset);
162 		if (ret < 0)
163 			return ret;
164 
165 		offset += chunk_size;
166 	}
167 
168 	return 0;
169 }
170 
171 /*
172  * Reads an NVM section completely.
173  * NICs prior to 7000 family doesn't have a real NVM, but just read
174  * section 0 which is the EEPROM. Because the EEPROM reading is unlimited
175  * by uCode, we need to manually check in this case that we don't
176  * overflow and try to read more than the EEPROM size.
177  * For 7000 family NICs, we supply the maximal size we can read, and
178  * the uCode fills the response with as much data as we can,
179  * without overflowing, so no check is needed.
180  */
181 static int iwl_nvm_read_section(struct iwl_mvm *mvm, u16 section,
182 				u8 *data, u32 size_read)
183 {
184 	u16 length, offset = 0;
185 	int ret;
186 
187 	/* Set nvm section read length */
188 	length = IWL_NVM_DEFAULT_CHUNK_SIZE;
189 
190 	ret = length;
191 
192 	/* Read the NVM until exhausted (reading less than requested) */
193 	while (ret == length) {
194 		/* Check no memory assumptions fail and cause an overflow */
195 		if ((size_read + offset + length) >
196 		    mvm->trans->trans_cfg->base_params->eeprom_size) {
197 			IWL_ERR(mvm, "EEPROM size is too small for NVM\n");
198 			return -ENOBUFS;
199 		}
200 
201 		ret = iwl_nvm_read_chunk(mvm, section, offset, length, data);
202 		if (ret < 0) {
203 			IWL_DEBUG_EEPROM(mvm->trans->dev,
204 					 "Cannot read NVM from section %d offset %d, length %d\n",
205 					 section, offset, length);
206 			return ret;
207 		}
208 		offset += ret;
209 	}
210 
211 	iwl_nvm_fixups(mvm->trans->hw_id, section, data, offset);
212 
213 	IWL_DEBUG_EEPROM(mvm->trans->dev,
214 			 "NVM section %d read completed\n", section);
215 	return offset;
216 }
217 
218 static struct iwl_nvm_data *
219 iwl_parse_nvm_sections(struct iwl_mvm *mvm)
220 {
221 	struct iwl_nvm_section *sections = mvm->nvm_sections;
222 	const __be16 *hw;
223 	const __le16 *sw, *calib, *regulatory, *mac_override, *phy_sku;
224 	u8 tx_ant = mvm->fw->valid_tx_ant;
225 	u8 rx_ant = mvm->fw->valid_rx_ant;
226 	int regulatory_type;
227 
228 	/* Checking for required sections */
229 	if (mvm->trans->cfg->nvm_type == IWL_NVM) {
230 		if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data ||
231 		    !mvm->nvm_sections[mvm->cfg->nvm_hw_section_num].data) {
232 			IWL_ERR(mvm, "Can't parse empty OTP/NVM sections\n");
233 			return NULL;
234 		}
235 	} else {
236 		if (mvm->trans->cfg->nvm_type == IWL_NVM_SDP)
237 			regulatory_type = NVM_SECTION_TYPE_REGULATORY_SDP;
238 		else
239 			regulatory_type = NVM_SECTION_TYPE_REGULATORY;
240 
241 		/* SW and REGULATORY sections are mandatory */
242 		if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data ||
243 		    !mvm->nvm_sections[regulatory_type].data) {
244 			IWL_ERR(mvm,
245 				"Can't parse empty family 8000 OTP/NVM sections\n");
246 			return NULL;
247 		}
248 		/* MAC_OVERRIDE or at least HW section must exist */
249 		if (!mvm->nvm_sections[mvm->cfg->nvm_hw_section_num].data &&
250 		    !mvm->nvm_sections[NVM_SECTION_TYPE_MAC_OVERRIDE].data) {
251 			IWL_ERR(mvm,
252 				"Can't parse mac_address, empty sections\n");
253 			return NULL;
254 		}
255 
256 		/* PHY_SKU section is mandatory in B0 */
257 		if (mvm->trans->cfg->nvm_type == IWL_NVM_EXT &&
258 		    !mvm->nvm_sections[NVM_SECTION_TYPE_PHY_SKU].data) {
259 			IWL_ERR(mvm,
260 				"Can't parse phy_sku in B0, empty sections\n");
261 			return NULL;
262 		}
263 	}
264 
265 	hw = (const __be16 *)sections[mvm->cfg->nvm_hw_section_num].data;
266 	sw = (const __le16 *)sections[NVM_SECTION_TYPE_SW].data;
267 	calib = (const __le16 *)sections[NVM_SECTION_TYPE_CALIBRATION].data;
268 	mac_override =
269 		(const __le16 *)sections[NVM_SECTION_TYPE_MAC_OVERRIDE].data;
270 	phy_sku = (const __le16 *)sections[NVM_SECTION_TYPE_PHY_SKU].data;
271 
272 	regulatory = mvm->trans->cfg->nvm_type == IWL_NVM_SDP ?
273 		(const __le16 *)sections[NVM_SECTION_TYPE_REGULATORY_SDP].data :
274 		(const __le16 *)sections[NVM_SECTION_TYPE_REGULATORY].data;
275 
276 	if (mvm->set_tx_ant)
277 		tx_ant &= mvm->set_tx_ant;
278 
279 	if (mvm->set_rx_ant)
280 		rx_ant &= mvm->set_rx_ant;
281 
282 	return iwl_parse_nvm_data(mvm->trans, mvm->cfg, mvm->fw, hw, sw, calib,
283 				  regulatory, mac_override, phy_sku,
284 				  tx_ant, rx_ant);
285 }
286 
287 /* Loads the NVM data stored in mvm->nvm_sections into the NIC */
288 int iwl_mvm_load_nvm_to_nic(struct iwl_mvm *mvm)
289 {
290 	int i, ret = 0;
291 	struct iwl_nvm_section *sections = mvm->nvm_sections;
292 
293 	IWL_DEBUG_EEPROM(mvm->trans->dev, "'Write to NVM\n");
294 
295 	for (i = 0; i < ARRAY_SIZE(mvm->nvm_sections); i++) {
296 		if (!mvm->nvm_sections[i].data || !mvm->nvm_sections[i].length)
297 			continue;
298 		ret = iwl_nvm_write_section(mvm, i, sections[i].data,
299 					    sections[i].length);
300 		if (ret < 0) {
301 			IWL_ERR(mvm, "iwl_mvm_send_cmd failed: %d\n", ret);
302 			break;
303 		}
304 	}
305 	return ret;
306 }
307 
308 int iwl_nvm_init(struct iwl_mvm *mvm)
309 {
310 	int ret, section;
311 	u32 size_read = 0;
312 	u8 *nvm_buffer, *temp;
313 	const char *nvm_file_C = mvm->cfg->default_nvm_file_C_step;
314 
315 	if (WARN_ON_ONCE(mvm->cfg->nvm_hw_section_num >= NVM_MAX_NUM_SECTIONS))
316 		return -EINVAL;
317 
318 	/* load NVM values from nic */
319 	/* Read From FW NVM */
320 	IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from NVM\n");
321 
322 	nvm_buffer = kmalloc(mvm->trans->trans_cfg->base_params->eeprom_size,
323 			     GFP_KERNEL);
324 	if (!nvm_buffer)
325 		return -ENOMEM;
326 	for (section = 0; section < NVM_MAX_NUM_SECTIONS; section++) {
327 		/* we override the constness for initial read */
328 		ret = iwl_nvm_read_section(mvm, section, nvm_buffer,
329 					   size_read);
330 		if (ret == -ENODATA) {
331 			ret = 0;
332 			continue;
333 		}
334 		if (ret < 0)
335 			break;
336 		size_read += ret;
337 		temp = kmemdup(nvm_buffer, ret, GFP_KERNEL);
338 		if (!temp) {
339 			ret = -ENOMEM;
340 			break;
341 		}
342 
343 		iwl_nvm_fixups(mvm->trans->hw_id, section, temp, ret);
344 
345 		mvm->nvm_sections[section].data = temp;
346 		mvm->nvm_sections[section].length = ret;
347 
348 #ifdef CONFIG_IWLWIFI_DEBUGFS
349 		switch (section) {
350 		case NVM_SECTION_TYPE_SW:
351 			mvm->nvm_sw_blob.data = temp;
352 			mvm->nvm_sw_blob.size  = ret;
353 			break;
354 		case NVM_SECTION_TYPE_CALIBRATION:
355 			mvm->nvm_calib_blob.data = temp;
356 			mvm->nvm_calib_blob.size  = ret;
357 			break;
358 		case NVM_SECTION_TYPE_PRODUCTION:
359 			mvm->nvm_prod_blob.data = temp;
360 			mvm->nvm_prod_blob.size  = ret;
361 			break;
362 		case NVM_SECTION_TYPE_PHY_SKU:
363 			mvm->nvm_phy_sku_blob.data = temp;
364 			mvm->nvm_phy_sku_blob.size  = ret;
365 			break;
366 		case NVM_SECTION_TYPE_REGULATORY_SDP:
367 		case NVM_SECTION_TYPE_REGULATORY:
368 			mvm->nvm_reg_blob.data = temp;
369 			mvm->nvm_reg_blob.size  = ret;
370 			break;
371 		default:
372 			if (section == mvm->cfg->nvm_hw_section_num) {
373 				mvm->nvm_hw_blob.data = temp;
374 				mvm->nvm_hw_blob.size = ret;
375 				break;
376 			}
377 		}
378 #endif
379 	}
380 	if (!size_read)
381 		IWL_ERR(mvm, "OTP is blank\n");
382 	kfree(nvm_buffer);
383 
384 	/* Only if PNVM selected in the mod param - load external NVM  */
385 	if (mvm->nvm_file_name) {
386 		/* read External NVM file from the mod param */
387 		ret = iwl_read_external_nvm(mvm->trans, mvm->nvm_file_name,
388 					    mvm->nvm_sections);
389 		if (ret) {
390 			mvm->nvm_file_name = nvm_file_C;
391 
392 			if ((ret == -EFAULT || ret == -ENOENT) &&
393 			    mvm->nvm_file_name) {
394 				/* in case nvm file was failed try again */
395 				ret = iwl_read_external_nvm(mvm->trans,
396 							    mvm->nvm_file_name,
397 							    mvm->nvm_sections);
398 				if (ret)
399 					return ret;
400 			} else {
401 				return ret;
402 			}
403 		}
404 	}
405 
406 	/* parse the relevant nvm sections */
407 	mvm->nvm_data = iwl_parse_nvm_sections(mvm);
408 	if (!mvm->nvm_data)
409 		return -ENODATA;
410 	IWL_DEBUG_EEPROM(mvm->trans->dev, "nvm version = %x\n",
411 			 mvm->nvm_data->nvm_version);
412 
413 	return ret < 0 ? ret : 0;
414 }
415 
416 struct iwl_mcc_update_resp_v8 *
417 iwl_mvm_update_mcc(struct iwl_mvm *mvm, const char *alpha2,
418 		   enum iwl_mcc_source src_id)
419 {
420 	struct iwl_mcc_update_cmd mcc_update_cmd = {
421 		.mcc = cpu_to_le16(alpha2[0] << 8 | alpha2[1]),
422 		.source_id = (u8)src_id,
423 	};
424 	struct iwl_mcc_update_resp_v8 *resp_cp;
425 	struct iwl_rx_packet *pkt;
426 	struct iwl_host_cmd cmd = {
427 		.id = MCC_UPDATE_CMD,
428 		.flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
429 		.data = { &mcc_update_cmd },
430 	};
431 
432 	int ret, resp_ver;
433 	u32 status;
434 	int resp_len, n_channels;
435 	u16 mcc;
436 
437 	if (WARN_ON_ONCE(!iwl_mvm_is_lar_supported(mvm)))
438 		return ERR_PTR(-EOPNOTSUPP);
439 
440 	cmd.len[0] = sizeof(struct iwl_mcc_update_cmd);
441 
442 	IWL_DEBUG_LAR(mvm, "send MCC update to FW with '%c%c' src = %d\n",
443 		      alpha2[0], alpha2[1], src_id);
444 
445 	ret = iwl_mvm_send_cmd(mvm, &cmd);
446 	if (ret)
447 		return ERR_PTR(ret);
448 
449 	pkt = cmd.resp_pkt;
450 
451 	resp_ver = iwl_fw_lookup_notif_ver(mvm->fw, IWL_ALWAYS_LONG_GROUP,
452 					   MCC_UPDATE_CMD, 0);
453 
454 	/* Extract MCC response */
455 	if (resp_ver >= 8) {
456 		struct iwl_mcc_update_resp_v8 *mcc_resp_v8 = (void *)pkt->data;
457 
458 		n_channels =  __le32_to_cpu(mcc_resp_v8->n_channels);
459 		if (iwl_rx_packet_payload_len(pkt) !=
460 		    struct_size(mcc_resp_v8, channels, n_channels)) {
461 			resp_cp = ERR_PTR(-EINVAL);
462 			goto exit;
463 		}
464 		resp_len = struct_size(resp_cp, channels, n_channels);
465 		resp_cp = kzalloc(resp_len, GFP_KERNEL);
466 		if (!resp_cp) {
467 			resp_cp = ERR_PTR(-ENOMEM);
468 			goto exit;
469 		}
470 		resp_cp->status = mcc_resp_v8->status;
471 		resp_cp->mcc = mcc_resp_v8->mcc;
472 		resp_cp->cap = mcc_resp_v8->cap;
473 		resp_cp->source_id = mcc_resp_v8->source_id;
474 		resp_cp->time = mcc_resp_v8->time;
475 		resp_cp->geo_info = mcc_resp_v8->geo_info;
476 		resp_cp->n_channels = mcc_resp_v8->n_channels;
477 		memcpy(resp_cp->channels, mcc_resp_v8->channels,
478 		       n_channels * sizeof(__le32));
479 	} else if (fw_has_capa(&mvm->fw->ucode_capa,
480 			       IWL_UCODE_TLV_CAPA_MCC_UPDATE_11AX_SUPPORT)) {
481 		struct iwl_mcc_update_resp_v4 *mcc_resp_v4 = (void *)pkt->data;
482 
483 		n_channels =  __le32_to_cpu(mcc_resp_v4->n_channels);
484 		if (iwl_rx_packet_payload_len(pkt) !=
485 		    struct_size(mcc_resp_v4, channels, n_channels)) {
486 			resp_cp = ERR_PTR(-EINVAL);
487 			goto exit;
488 		}
489 		resp_len = struct_size(resp_cp, channels, n_channels);
490 		resp_cp = kzalloc(resp_len, GFP_KERNEL);
491 		if (!resp_cp) {
492 			resp_cp = ERR_PTR(-ENOMEM);
493 			goto exit;
494 		}
495 
496 		resp_cp->status = mcc_resp_v4->status;
497 		resp_cp->mcc = mcc_resp_v4->mcc;
498 		resp_cp->cap = cpu_to_le32(le16_to_cpu(mcc_resp_v4->cap));
499 		resp_cp->source_id = mcc_resp_v4->source_id;
500 		resp_cp->time = mcc_resp_v4->time;
501 		resp_cp->geo_info = mcc_resp_v4->geo_info;
502 		resp_cp->n_channels = mcc_resp_v4->n_channels;
503 		memcpy(resp_cp->channels, mcc_resp_v4->channels,
504 		       n_channels * sizeof(__le32));
505 	} else {
506 		struct iwl_mcc_update_resp_v3 *mcc_resp_v3 = (void *)pkt->data;
507 
508 		n_channels =  __le32_to_cpu(mcc_resp_v3->n_channels);
509 		if (iwl_rx_packet_payload_len(pkt) !=
510 		    struct_size(mcc_resp_v3, channels, n_channels)) {
511 			resp_cp = ERR_PTR(-EINVAL);
512 			goto exit;
513 		}
514 		resp_len = struct_size(resp_cp, channels, n_channels);
515 		resp_cp = kzalloc(resp_len, GFP_KERNEL);
516 		if (!resp_cp) {
517 			resp_cp = ERR_PTR(-ENOMEM);
518 			goto exit;
519 		}
520 
521 		resp_cp->status = mcc_resp_v3->status;
522 		resp_cp->mcc = mcc_resp_v3->mcc;
523 		resp_cp->cap = cpu_to_le32(mcc_resp_v3->cap);
524 		resp_cp->source_id = mcc_resp_v3->source_id;
525 		resp_cp->time = mcc_resp_v3->time;
526 		resp_cp->geo_info = mcc_resp_v3->geo_info;
527 		resp_cp->n_channels = mcc_resp_v3->n_channels;
528 		memcpy(resp_cp->channels, mcc_resp_v3->channels,
529 		       n_channels * sizeof(__le32));
530 	}
531 
532 	status = le32_to_cpu(resp_cp->status);
533 
534 	mcc = le16_to_cpu(resp_cp->mcc);
535 
536 	/* W/A for a FW/NVM issue - returns 0x00 for the world domain */
537 	if (mcc == 0) {
538 		mcc = 0x3030;  /* "00" - world */
539 		resp_cp->mcc = cpu_to_le16(mcc);
540 	}
541 
542 	IWL_DEBUG_LAR(mvm,
543 		      "MCC response status: 0x%x. new MCC: 0x%x ('%c%c') n_chans: %d\n",
544 		      status, mcc, mcc >> 8, mcc & 0xff, n_channels);
545 
546 exit:
547 	iwl_free_resp(&cmd);
548 	return resp_cp;
549 }
550 
551 int iwl_mvm_init_mcc(struct iwl_mvm *mvm)
552 {
553 	bool tlv_lar;
554 	bool nvm_lar;
555 	int retval;
556 	struct ieee80211_regdomain *regd;
557 	char mcc[3];
558 
559 	if (mvm->cfg->nvm_type == IWL_NVM_EXT) {
560 		tlv_lar = fw_has_capa(&mvm->fw->ucode_capa,
561 				      IWL_UCODE_TLV_CAPA_LAR_SUPPORT);
562 		nvm_lar = mvm->nvm_data->lar_enabled;
563 		if (tlv_lar != nvm_lar)
564 			IWL_INFO(mvm,
565 				 "Conflict between TLV & NVM regarding enabling LAR (TLV = %s NVM =%s)\n",
566 				 tlv_lar ? "enabled" : "disabled",
567 				 nvm_lar ? "enabled" : "disabled");
568 	}
569 
570 	if (!iwl_mvm_is_lar_supported(mvm))
571 		return 0;
572 
573 	/*
574 	 * try to replay the last set MCC to FW. If it doesn't exist,
575 	 * queue an update to cfg80211 to retrieve the default alpha2 from FW.
576 	 */
577 	retval = iwl_mvm_init_fw_regd(mvm, true);
578 	if (retval != -ENOENT)
579 		return retval;
580 
581 	/*
582 	 * Driver regulatory hint for initial update, this also informs the
583 	 * firmware we support wifi location updates.
584 	 * Disallow scans that might crash the FW while the LAR regdomain
585 	 * is not set.
586 	 */
587 	mvm->lar_regdom_set = false;
588 
589 	regd = iwl_mvm_get_current_regdomain(mvm, NULL);
590 	if (IS_ERR_OR_NULL(regd))
591 		return -EIO;
592 
593 	if (iwl_mvm_is_wifi_mcc_supported(mvm) &&
594 	    !iwl_bios_get_mcc(&mvm->fwrt, mcc)) {
595 		kfree(regd);
596 		regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, mcc,
597 					     MCC_SOURCE_BIOS, NULL);
598 		if (IS_ERR_OR_NULL(regd))
599 			return -EIO;
600 	}
601 
602 	retval = regulatory_set_wiphy_regd_sync(mvm->hw->wiphy, regd);
603 	kfree(regd);
604 	return retval;
605 }
606 
607 void iwl_mvm_rx_chub_update_mcc(struct iwl_mvm *mvm,
608 				struct iwl_rx_cmd_buffer *rxb)
609 {
610 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
611 	struct iwl_mcc_chub_notif *notif = (void *)pkt->data;
612 	enum iwl_mcc_source src;
613 	char mcc[3];
614 	struct ieee80211_regdomain *regd;
615 	int wgds_tbl_idx;
616 
617 	lockdep_assert_held(&mvm->mutex);
618 
619 	if (iwl_mvm_is_vif_assoc(mvm) && notif->source_id == MCC_SOURCE_WIFI) {
620 		IWL_DEBUG_LAR(mvm, "Ignore mcc update while associated\n");
621 		return;
622 	}
623 
624 	if (WARN_ON_ONCE(!iwl_mvm_is_lar_supported(mvm)))
625 		return;
626 
627 	mcc[0] = le16_to_cpu(notif->mcc) >> 8;
628 	mcc[1] = le16_to_cpu(notif->mcc) & 0xff;
629 	mcc[2] = '\0';
630 	src = notif->source_id;
631 
632 	IWL_DEBUG_LAR(mvm,
633 		      "RX: received chub update mcc cmd (mcc '%s' src %d)\n",
634 		      mcc, src);
635 	regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, mcc, src, NULL);
636 	if (IS_ERR_OR_NULL(regd))
637 		return;
638 
639 	wgds_tbl_idx = iwl_mvm_get_sar_geo_profile(mvm);
640 	if (wgds_tbl_idx < 1)
641 		IWL_DEBUG_INFO(mvm,
642 			       "SAR WGDS is disabled or error received (%d)\n",
643 			       wgds_tbl_idx);
644 	else
645 		IWL_DEBUG_INFO(mvm, "SAR WGDS: geo profile %d is configured\n",
646 			       wgds_tbl_idx);
647 
648 	regulatory_set_wiphy_regd(mvm->hw->wiphy, regd);
649 	kfree(regd);
650 }
651