xref: /freebsd/sys/contrib/dev/iwlwifi/mvm/nvm.c (revision 6b627f88584ce13118e0a24951b503c0b1f2d5a7)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2012-2014, 2018-2019, 2021-2025 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  */
iwl_nvm_write_chunk(struct iwl_mvm * mvm,u16 section,u16 offset,u16 length,const u8 * data)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 
iwl_nvm_read_chunk(struct iwl_mvm * mvm,u16 section,u16 offset,u16 length,u8 * data)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->info.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 
iwl_nvm_write_section(struct iwl_mvm * mvm,u16 section,const u8 * data,u16 length)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  */
iwl_nvm_read_section(struct iwl_mvm * mvm,u16 section,u8 * data,u32 size_read)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->mac_cfg->base->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->info.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 *
iwl_parse_nvm_sections(struct iwl_mvm * mvm)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->trans->mac_cfg->base->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->trans->mac_cfg->base->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->trans->mac_cfg->base->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 */
iwl_mvm_load_nvm_to_nic(struct iwl_mvm * mvm)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 
iwl_nvm_init(struct iwl_mvm * mvm)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 
314 	if (WARN_ON_ONCE(mvm->trans->mac_cfg->base->nvm_hw_section_num >= NVM_MAX_NUM_SECTIONS))
315 		return -EINVAL;
316 
317 	/* load NVM values from nic */
318 	/* Read From FW NVM */
319 	IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from NVM\n");
320 
321 	nvm_buffer = kmalloc(mvm->trans->mac_cfg->base->eeprom_size,
322 			     GFP_KERNEL);
323 	if (!nvm_buffer)
324 		return -ENOMEM;
325 	for (section = 0; section < NVM_MAX_NUM_SECTIONS; section++) {
326 		/* we override the constness for initial read */
327 		ret = iwl_nvm_read_section(mvm, section, nvm_buffer,
328 					   size_read);
329 		if (ret == -ENODATA) {
330 			ret = 0;
331 			continue;
332 		}
333 		if (ret < 0)
334 			break;
335 		size_read += ret;
336 		temp = kmemdup(nvm_buffer, ret, GFP_KERNEL);
337 		if (!temp) {
338 			ret = -ENOMEM;
339 			break;
340 		}
341 
342 		iwl_nvm_fixups(mvm->trans->info.hw_id, section, temp, ret);
343 
344 		mvm->nvm_sections[section].data = temp;
345 		mvm->nvm_sections[section].length = ret;
346 
347 #ifdef CONFIG_IWLWIFI_DEBUGFS
348 		switch (section) {
349 		case NVM_SECTION_TYPE_SW:
350 			mvm->nvm_sw_blob.data = temp;
351 			mvm->nvm_sw_blob.size  = ret;
352 			break;
353 		case NVM_SECTION_TYPE_CALIBRATION:
354 			mvm->nvm_calib_blob.data = temp;
355 			mvm->nvm_calib_blob.size  = ret;
356 			break;
357 		case NVM_SECTION_TYPE_PRODUCTION:
358 			mvm->nvm_prod_blob.data = temp;
359 			mvm->nvm_prod_blob.size  = ret;
360 			break;
361 		case NVM_SECTION_TYPE_PHY_SKU:
362 			mvm->nvm_phy_sku_blob.data = temp;
363 			mvm->nvm_phy_sku_blob.size  = ret;
364 			break;
365 		case NVM_SECTION_TYPE_REGULATORY_SDP:
366 		case NVM_SECTION_TYPE_REGULATORY:
367 			mvm->nvm_reg_blob.data = temp;
368 			mvm->nvm_reg_blob.size  = ret;
369 			break;
370 		default:
371 			if (section == mvm->trans->mac_cfg->base->nvm_hw_section_num) {
372 				mvm->nvm_hw_blob.data = temp;
373 				mvm->nvm_hw_blob.size = ret;
374 				break;
375 			}
376 		}
377 #endif
378 	}
379 	if (!size_read)
380 		IWL_ERR(mvm, "OTP is blank\n");
381 	kfree(nvm_buffer);
382 
383 	/* Only if PNVM selected in the mod param - load external NVM  */
384 	if (mvm->nvm_file_name) {
385 		/* read External NVM file from the mod param */
386 		ret = iwl_read_external_nvm(mvm->trans, mvm->nvm_file_name,
387 					    mvm->nvm_sections);
388 		if (ret)
389 			return ret;
390 	}
391 
392 	/* parse the relevant nvm sections */
393 	mvm->nvm_data = iwl_parse_nvm_sections(mvm);
394 	if (!mvm->nvm_data)
395 		return -ENODATA;
396 	IWL_DEBUG_EEPROM(mvm->trans->dev, "nvm version = %x\n",
397 			 mvm->nvm_data->nvm_version);
398 
399 	return ret < 0 ? ret : 0;
400 }
401 
402 struct iwl_mcc_update_resp_v8 *
iwl_mvm_update_mcc(struct iwl_mvm * mvm,const char * alpha2,enum iwl_mcc_source src_id)403 iwl_mvm_update_mcc(struct iwl_mvm *mvm, const char *alpha2,
404 		   enum iwl_mcc_source src_id)
405 {
406 	struct iwl_mcc_update_cmd mcc_update_cmd = {
407 		.mcc = cpu_to_le16(alpha2[0] << 8 | alpha2[1]),
408 		.source_id = (u8)src_id,
409 	};
410 	struct iwl_mcc_update_resp_v8 *resp_cp;
411 	struct iwl_rx_packet *pkt;
412 	struct iwl_host_cmd cmd = {
413 		.id = MCC_UPDATE_CMD,
414 		.flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
415 		.data = { &mcc_update_cmd },
416 	};
417 
418 	int ret, resp_ver;
419 	u32 status;
420 	int resp_len, n_channels;
421 	u16 mcc;
422 
423 	if (WARN_ON_ONCE(!iwl_mvm_is_lar_supported(mvm)))
424 		return ERR_PTR(-EOPNOTSUPP);
425 
426 	cmd.len[0] = sizeof(struct iwl_mcc_update_cmd);
427 
428 	IWL_DEBUG_LAR(mvm, "send MCC update to FW with '%c%c' src = %d\n",
429 		      alpha2[0], alpha2[1], src_id);
430 
431 	ret = iwl_mvm_send_cmd(mvm, &cmd);
432 	if (ret)
433 		return ERR_PTR(ret);
434 
435 	pkt = cmd.resp_pkt;
436 
437 	resp_ver = iwl_fw_lookup_notif_ver(mvm->fw, IWL_ALWAYS_LONG_GROUP,
438 					   MCC_UPDATE_CMD, 0);
439 
440 	/* Extract MCC response */
441 	if (resp_ver >= 8) {
442 		struct iwl_mcc_update_resp_v8 *mcc_resp_v8 = (void *)pkt->data;
443 
444 		n_channels =  __le32_to_cpu(mcc_resp_v8->n_channels);
445 		if (iwl_rx_packet_payload_len(pkt) !=
446 		    struct_size(mcc_resp_v8, channels, n_channels)) {
447 			resp_cp = ERR_PTR(-EINVAL);
448 			goto exit;
449 		}
450 		resp_len = struct_size(resp_cp, channels, n_channels);
451 		resp_cp = kzalloc(resp_len, GFP_KERNEL);
452 		if (!resp_cp) {
453 			resp_cp = ERR_PTR(-ENOMEM);
454 			goto exit;
455 		}
456 		resp_cp->status = mcc_resp_v8->status;
457 		resp_cp->mcc = mcc_resp_v8->mcc;
458 		resp_cp->cap = mcc_resp_v8->cap;
459 		resp_cp->source_id = mcc_resp_v8->source_id;
460 		resp_cp->time = mcc_resp_v8->time;
461 		resp_cp->geo_info = mcc_resp_v8->geo_info;
462 		resp_cp->n_channels = mcc_resp_v8->n_channels;
463 		memcpy(resp_cp->channels, mcc_resp_v8->channels,
464 		       n_channels * sizeof(__le32));
465 	} else if (fw_has_capa(&mvm->fw->ucode_capa,
466 			       IWL_UCODE_TLV_CAPA_MCC_UPDATE_11AX_SUPPORT)) {
467 		struct iwl_mcc_update_resp_v4 *mcc_resp_v4 = (void *)pkt->data;
468 
469 		n_channels =  __le32_to_cpu(mcc_resp_v4->n_channels);
470 		if (iwl_rx_packet_payload_len(pkt) !=
471 		    struct_size(mcc_resp_v4, channels, n_channels)) {
472 			resp_cp = ERR_PTR(-EINVAL);
473 			goto exit;
474 		}
475 		resp_len = struct_size(resp_cp, channels, n_channels);
476 		resp_cp = kzalloc(resp_len, GFP_KERNEL);
477 		if (!resp_cp) {
478 			resp_cp = ERR_PTR(-ENOMEM);
479 			goto exit;
480 		}
481 
482 		resp_cp->status = mcc_resp_v4->status;
483 		resp_cp->mcc = mcc_resp_v4->mcc;
484 		resp_cp->cap = cpu_to_le32(le16_to_cpu(mcc_resp_v4->cap));
485 		resp_cp->source_id = mcc_resp_v4->source_id;
486 		resp_cp->time = mcc_resp_v4->time;
487 		resp_cp->geo_info = mcc_resp_v4->geo_info;
488 		resp_cp->n_channels = mcc_resp_v4->n_channels;
489 		memcpy(resp_cp->channels, mcc_resp_v4->channels,
490 		       n_channels * sizeof(__le32));
491 	} else {
492 		struct iwl_mcc_update_resp_v3 *mcc_resp_v3 = (void *)pkt->data;
493 
494 		n_channels =  __le32_to_cpu(mcc_resp_v3->n_channels);
495 		if (iwl_rx_packet_payload_len(pkt) !=
496 		    struct_size(mcc_resp_v3, channels, n_channels)) {
497 			resp_cp = ERR_PTR(-EINVAL);
498 			goto exit;
499 		}
500 		resp_len = struct_size(resp_cp, channels, n_channels);
501 		resp_cp = kzalloc(resp_len, GFP_KERNEL);
502 		if (!resp_cp) {
503 			resp_cp = ERR_PTR(-ENOMEM);
504 			goto exit;
505 		}
506 
507 		resp_cp->status = mcc_resp_v3->status;
508 		resp_cp->mcc = mcc_resp_v3->mcc;
509 		resp_cp->cap = cpu_to_le32(mcc_resp_v3->cap);
510 		resp_cp->source_id = mcc_resp_v3->source_id;
511 		resp_cp->time = mcc_resp_v3->time;
512 		resp_cp->geo_info = mcc_resp_v3->geo_info;
513 		resp_cp->n_channels = mcc_resp_v3->n_channels;
514 		memcpy(resp_cp->channels, mcc_resp_v3->channels,
515 		       n_channels * sizeof(__le32));
516 	}
517 
518 	status = le32_to_cpu(resp_cp->status);
519 
520 	mcc = le16_to_cpu(resp_cp->mcc);
521 
522 	/* W/A for a FW/NVM issue - returns 0x00 for the world domain */
523 	if (mcc == 0) {
524 		mcc = 0x3030;  /* "00" - world */
525 		resp_cp->mcc = cpu_to_le16(mcc);
526 	}
527 
528 	IWL_DEBUG_LAR(mvm,
529 		      "MCC response status: 0x%x. new MCC: 0x%x ('%c%c') n_chans: %d\n",
530 		      status, mcc, mcc >> 8, mcc & 0xff, n_channels);
531 
532 exit:
533 	iwl_free_resp(&cmd);
534 	return resp_cp;
535 }
536 
iwl_mvm_init_mcc(struct iwl_mvm * mvm)537 int iwl_mvm_init_mcc(struct iwl_mvm *mvm)
538 {
539 	bool tlv_lar;
540 	bool nvm_lar;
541 	int retval;
542 	struct ieee80211_regdomain *regd;
543 	char mcc[3];
544 
545 	if (mvm->trans->cfg->nvm_type == IWL_NVM_EXT) {
546 		tlv_lar = fw_has_capa(&mvm->fw->ucode_capa,
547 				      IWL_UCODE_TLV_CAPA_LAR_SUPPORT);
548 		nvm_lar = mvm->nvm_data->lar_enabled;
549 		if (tlv_lar != nvm_lar)
550 			IWL_INFO(mvm,
551 				 "Conflict between TLV & NVM regarding enabling LAR (TLV = %s NVM =%s)\n",
552 				 tlv_lar ? "enabled" : "disabled",
553 				 nvm_lar ? "enabled" : "disabled");
554 	}
555 
556 	if (!iwl_mvm_is_lar_supported(mvm))
557 		return 0;
558 
559 	/*
560 	 * try to replay the last set MCC to FW. If it doesn't exist,
561 	 * queue an update to cfg80211 to retrieve the default alpha2 from FW.
562 	 */
563 	retval = iwl_mvm_init_fw_regd(mvm, true);
564 	if (retval != -ENOENT)
565 		return retval;
566 
567 	/*
568 	 * Driver regulatory hint for initial update, this also informs the
569 	 * firmware we support wifi location updates.
570 	 * Disallow scans that might crash the FW while the LAR regdomain
571 	 * is not set.
572 	 */
573 	mvm->lar_regdom_set = false;
574 
575 	regd = iwl_mvm_get_current_regdomain(mvm, NULL);
576 	if (IS_ERR_OR_NULL(regd))
577 		return -EIO;
578 
579 	if (iwl_mvm_is_wifi_mcc_supported(mvm) &&
580 	    !iwl_bios_get_mcc(&mvm->fwrt, mcc)) {
581 		kfree(regd);
582 		regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, mcc,
583 					     MCC_SOURCE_BIOS, NULL);
584 		if (IS_ERR_OR_NULL(regd))
585 			return -EIO;
586 	}
587 
588 	retval = regulatory_set_wiphy_regd_sync(mvm->hw->wiphy, regd);
589 	kfree(regd);
590 	return retval;
591 }
592 
iwl_mvm_rx_chub_update_mcc(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)593 void iwl_mvm_rx_chub_update_mcc(struct iwl_mvm *mvm,
594 				struct iwl_rx_cmd_buffer *rxb)
595 {
596 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
597 	struct iwl_mcc_chub_notif *notif = (void *)pkt->data;
598 	enum iwl_mcc_source src;
599 	char mcc[3];
600 	struct ieee80211_regdomain *regd;
601 	int wgds_tbl_idx;
602 	bool changed = false;
603 
604 	lockdep_assert_held(&mvm->mutex);
605 
606 	if (iwl_mvm_is_vif_assoc(mvm) && notif->source_id == MCC_SOURCE_WIFI) {
607 		IWL_DEBUG_LAR(mvm, "Ignore mcc update while associated\n");
608 		return;
609 	}
610 
611 	if (WARN_ON_ONCE(!iwl_mvm_is_lar_supported(mvm)))
612 		return;
613 
614 	mcc[0] = le16_to_cpu(notif->mcc) >> 8;
615 	mcc[1] = le16_to_cpu(notif->mcc) & 0xff;
616 	mcc[2] = '\0';
617 	src = notif->source_id;
618 
619 	IWL_DEBUG_LAR(mvm,
620 		      "RX: received chub update mcc cmd (mcc '%s' src %d)\n",
621 		      mcc, src);
622 	regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, mcc, src, &changed);
623 	if (IS_ERR_OR_NULL(regd))
624 		return;
625 
626 	if (!changed) {
627 		IWL_DEBUG_LAR(mvm, "RX: No change in the regulatory data\n");
628 		goto out;
629 	}
630 
631 	wgds_tbl_idx = iwl_mvm_get_sar_geo_profile(mvm);
632 	if (wgds_tbl_idx < 1)
633 		IWL_DEBUG_INFO(mvm,
634 			       "SAR WGDS is disabled or error received (%d)\n",
635 			       wgds_tbl_idx);
636 	else
637 		IWL_DEBUG_INFO(mvm, "SAR WGDS: geo profile %d is configured\n",
638 			       wgds_tbl_idx);
639 
640 	regulatory_set_wiphy_regd(mvm->hw->wiphy, regd);
641 
642 out:
643 	kfree(regd);
644 }
645