xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/fw.c (revision b1d515352b88395fa7401468e1a60087d55359ff)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2012-2014, 2018-2026 Intel Corporation
4  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5  * Copyright (C) 2016-2017 Intel Deutschland GmbH
6  */
7 #include <net/mac80211.h>
8 #include <linux/netdevice.h>
9 #include <linux/dmi.h>
10 
11 #include "iwl-trans.h"
12 #include "iwl-op-mode.h"
13 #include "fw/img.h"
14 #include "iwl-debug.h"
15 #include "iwl-prph.h"
16 #include "fw/acpi.h"
17 #include "fw/pnvm.h"
18 #include "fw/uefi.h"
19 #include "fw/regulatory.h"
20 
21 #include "mvm.h"
22 #include "fw/dbg.h"
23 #include "iwl-phy-db.h"
24 #include "iwl-modparams.h"
25 #include "iwl-nvm-parse.h"
26 #include "time-sync.h"
27 
28 #define MVM_UCODE_ALIVE_TIMEOUT	(2 * HZ)
29 #define MVM_UCODE_CALIB_TIMEOUT	(2 * HZ)
30 
31 struct iwl_mvm_alive_data {
32 	__le32 sku_id[3];
33 	bool valid;
34 };
35 
36 static int iwl_send_tx_ant_cfg(struct iwl_mvm *mvm, u8 valid_tx_ant)
37 {
38 	struct iwl_tx_ant_cfg_cmd tx_ant_cmd = {
39 		.valid = cpu_to_le32(valid_tx_ant),
40 	};
41 
42 	IWL_DEBUG_FW(mvm, "select valid tx ant: %u\n", valid_tx_ant);
43 	return iwl_mvm_send_cmd_pdu(mvm, TX_ANT_CONFIGURATION_CMD, 0,
44 				    sizeof(tx_ant_cmd), &tx_ant_cmd);
45 }
46 
47 static int iwl_send_rss_cfg_cmd(struct iwl_mvm *mvm)
48 {
49 	int i;
50 	struct iwl_rss_config_cmd cmd = {
51 		.flags = cpu_to_le32(IWL_RSS_ENABLE),
52 		.hash_mask = BIT(IWL_RSS_HASH_TYPE_IPV4_TCP) |
53 			     BIT(IWL_RSS_HASH_TYPE_IPV4_UDP) |
54 			     BIT(IWL_RSS_HASH_TYPE_IPV4_PAYLOAD) |
55 			     BIT(IWL_RSS_HASH_TYPE_IPV6_TCP) |
56 			     BIT(IWL_RSS_HASH_TYPE_IPV6_UDP) |
57 			     BIT(IWL_RSS_HASH_TYPE_IPV6_PAYLOAD),
58 	};
59 
60 	if (mvm->trans->info.num_rxqs == 1)
61 		return 0;
62 
63 	/* Do not direct RSS traffic to Q 0 which is our fallback queue */
64 	for (i = 0; i < ARRAY_SIZE(cmd.indirection_table); i++)
65 		cmd.indirection_table[i] =
66 			1 + (i % (mvm->trans->info.num_rxqs - 1));
67 	netdev_rss_key_fill(cmd.secret_key, sizeof(cmd.secret_key));
68 
69 	return iwl_mvm_send_cmd_pdu(mvm, RSS_CONFIG_CMD, 0, sizeof(cmd), &cmd);
70 }
71 
72 static int iwl_mvm_send_dqa_cmd(struct iwl_mvm *mvm)
73 {
74 	struct iwl_dqa_enable_cmd dqa_cmd = {
75 		.cmd_queue = cpu_to_le32(IWL_MVM_DQA_CMD_QUEUE),
76 	};
77 	u32 cmd_id = WIDE_ID(DATA_PATH_GROUP, DQA_ENABLE_CMD);
78 	int ret;
79 
80 	ret = iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, sizeof(dqa_cmd), &dqa_cmd);
81 	if (ret)
82 		IWL_ERR(mvm, "Failed to send DQA enabling command: %d\n", ret);
83 	else
84 		IWL_DEBUG_FW(mvm, "Working in DQA mode\n");
85 
86 	return ret;
87 }
88 
89 void iwl_mvm_mfu_assert_dump_notif(struct iwl_mvm *mvm,
90 				   struct iwl_rx_cmd_buffer *rxb)
91 {
92 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
93 	struct iwl_mfu_assert_dump_notif *mfu_dump_notif = (void *)pkt->data;
94 
95 	if (mfu_dump_notif->index_num == 0)
96 		IWL_INFO(mvm, "MFUART assert id 0x%x occurred\n",
97 			 le32_to_cpu(mfu_dump_notif->assert_id));
98 }
99 
100 static bool iwl_alive_fn(struct iwl_notif_wait_data *notif_wait,
101 			 struct iwl_rx_packet *pkt, void *data)
102 {
103 	unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
104 	struct iwl_mvm *mvm =
105 		container_of(notif_wait, struct iwl_mvm, notif_wait);
106 	struct iwl_mvm_alive_data *alive_data = data;
107 	struct iwl_umac_alive *umac;
108 	struct iwl_lmac_alive *lmac1;
109 	struct iwl_lmac_alive *lmac2 = NULL;
110 	u16 status;
111 	u32 lmac_error_event_table, umac_error_table;
112 	u32 version = iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
113 					      UCODE_ALIVE_NTFY, 0);
114 	u32 i;
115 
116 
117 	if (version >= 6) {
118 		struct iwl_alive_ntf_v7 *palive;
119 
120 		if (pkt_len < sizeof(*palive))
121 			return false;
122 
123 		palive = (void *)pkt->data;
124 
125 		umac = &palive->umac_data;
126 		lmac1 = &palive->lmac_data[0];
127 		lmac2 = &palive->lmac_data[1];
128 		status = le16_to_cpu(palive->status);
129 
130 		BUILD_BUG_ON(sizeof(palive->sku_id.data) !=
131 			     sizeof(alive_data->sku_id));
132 		memcpy(alive_data->sku_id, palive->sku_id.data,
133 		       sizeof(palive->sku_id.data));
134 
135 		IWL_DEBUG_FW(mvm, "Got sku_id: 0x0%x 0x0%x 0x0%x\n",
136 			     le32_to_cpu(alive_data->sku_id[0]),
137 			     le32_to_cpu(alive_data->sku_id[1]),
138 			     le32_to_cpu(alive_data->sku_id[2]));
139 
140 		mvm->trans->dbg.imr_data.imr_enable =
141 			le32_to_cpu(palive->imr.enabled);
142 		mvm->trans->dbg.imr_data.imr_size =
143 			le32_to_cpu(palive->imr.size);
144 		mvm->trans->dbg.imr_data.imr2sram_remainbyte =
145 			mvm->trans->dbg.imr_data.imr_size;
146 		mvm->trans->dbg.imr_data.imr_base_addr =
147 			palive->imr.base_addr;
148 		mvm->trans->dbg.imr_data.imr_curr_addr =
149 			le64_to_cpu(mvm->trans->dbg.imr_data.imr_base_addr);
150 		IWL_DEBUG_FW(mvm, "IMR Enabled: 0x0%x  size 0x0%x Address 0x%016llx\n",
151 			     mvm->trans->dbg.imr_data.imr_enable,
152 			     mvm->trans->dbg.imr_data.imr_size,
153 			     le64_to_cpu(mvm->trans->dbg.imr_data.imr_base_addr));
154 
155 		if (!mvm->trans->dbg.imr_data.imr_enable) {
156 			for (i = 0; i < ARRAY_SIZE(mvm->trans->dbg.active_regions); i++) {
157 				struct iwl_ucode_tlv *reg_tlv;
158 				struct iwl_fw_ini_region_tlv *reg;
159 
160 				reg_tlv = mvm->trans->dbg.active_regions[i];
161 				if (!reg_tlv)
162 					continue;
163 
164 				reg = (void *)reg_tlv->data;
165 				/*
166 				 * We have only one DRAM IMR region, so we
167 				 * can break as soon as we find the first
168 				 * one.
169 				 */
170 				if (reg->type == IWL_FW_INI_REGION_DRAM_IMR) {
171 					mvm->trans->dbg.unsupported_region_msk |= BIT(i);
172 					break;
173 				}
174 			}
175 		}
176 
177 		if (version >= 8) {
178 			const struct iwl_alive_ntf *palive_v8 =
179 				(void *)pkt->data;
180 
181 			if (pkt_len < sizeof(*palive_v8))
182 				return false;
183 
184 			IWL_DEBUG_FW(mvm, "platform id: 0x%llx\n",
185 				     palive_v8->platform_id);
186 		}
187 	} else if (iwl_rx_packet_payload_len(pkt) ==
188 		   sizeof(struct iwl_alive_ntf_v3)) {
189 		struct iwl_alive_ntf_v3 *palive3;
190 
191 		if (pkt_len < sizeof(*palive3))
192 			return false;
193 
194 		palive3 = (void *)pkt->data;
195 		umac = &palive3->umac_data;
196 		lmac1 = &palive3->lmac_data;
197 		status = le16_to_cpu(palive3->status);
198 	} else {
199 		WARN(1, "unsupported alive notification (size %d)\n",
200 		     iwl_rx_packet_payload_len(pkt));
201 		/* get timeout later */
202 		return false;
203 	}
204 
205 	lmac_error_event_table =
206 		le32_to_cpu(lmac1->dbg_ptrs.error_event_table_ptr);
207 	iwl_fw_lmac1_set_alive_err_table(mvm->trans, lmac_error_event_table);
208 
209 	if (lmac2)
210 		mvm->trans->dbg.lmac_error_event_table[1] =
211 			le32_to_cpu(lmac2->dbg_ptrs.error_event_table_ptr);
212 
213 	umac_error_table = le32_to_cpu(umac->dbg_ptrs.error_info_addr) &
214 							~FW_ADDR_CACHE_CONTROL;
215 
216 	if (umac_error_table) {
217 		iwl_fw_umac_set_alive_err_table(mvm->trans,
218 						umac_error_table);
219 	}
220 
221 	alive_data->valid = status == IWL_ALIVE_STATUS_OK;
222 
223 	IWL_DEBUG_FW(mvm,
224 		     "Alive ucode status 0x%04x revision 0x%01X 0x%01X\n",
225 		     status, lmac1->ver_type, lmac1->ver_subtype);
226 
227 	if (lmac2)
228 		IWL_DEBUG_FW(mvm, "Alive ucode CDB\n");
229 
230 	IWL_DEBUG_FW(mvm,
231 		     "UMAC version: Major - 0x%x, Minor - 0x%x\n",
232 		     le32_to_cpu(umac->umac_major),
233 		     le32_to_cpu(umac->umac_minor));
234 
235 	iwl_fwrt_update_fw_versions(&mvm->fwrt, lmac1, umac);
236 
237 	return true;
238 }
239 
240 static bool iwl_wait_init_complete(struct iwl_notif_wait_data *notif_wait,
241 				   struct iwl_rx_packet *pkt, void *data)
242 {
243 	WARN_ON(pkt->hdr.cmd != INIT_COMPLETE_NOTIF);
244 
245 	return true;
246 }
247 
248 static bool iwl_wait_phy_db_entry(struct iwl_notif_wait_data *notif_wait,
249 				  struct iwl_rx_packet *pkt, void *data)
250 {
251 	struct iwl_phy_db *phy_db = data;
252 
253 	if (pkt->hdr.cmd != CALIB_RES_NOTIF_PHY_DB) {
254 		WARN_ON(pkt->hdr.cmd != INIT_COMPLETE_NOTIF);
255 		return true;
256 	}
257 
258 	WARN_ON(iwl_phy_db_set_section(phy_db, pkt));
259 
260 	return false;
261 }
262 
263 static void iwl_mvm_print_pd_notification(struct iwl_mvm *mvm)
264 {
265 #define IWL_FW_PRINT_REG_INFO(reg_name) \
266 	IWL_ERR(mvm, #reg_name ": 0x%x\n", iwl_trans_read_umac_prph(trans, reg_name))
267 
268 	struct iwl_trans *trans = mvm->trans;
269 	enum iwl_device_family device_family = trans->mac_cfg->device_family;
270 
271 	if (device_family < IWL_DEVICE_FAMILY_8000)
272 		return;
273 
274 	if (device_family <= IWL_DEVICE_FAMILY_9000)
275 		IWL_FW_PRINT_REG_INFO(WFPM_ARC1_PD_NOTIFICATION);
276 	else
277 		IWL_FW_PRINT_REG_INFO(WFPM_LMAC1_PD_NOTIFICATION);
278 
279 	IWL_FW_PRINT_REG_INFO(HPM_SECONDARY_DEVICE_STATE);
280 
281 	/* print OPT info */
282 	IWL_FW_PRINT_REG_INFO(WFPM_MAC_OTP_CFG7_ADDR);
283 	IWL_FW_PRINT_REG_INFO(WFPM_MAC_OTP_CFG7_DATA);
284 }
285 
286 static int iwl_mvm_load_ucode_wait_alive(struct iwl_mvm *mvm,
287 					 enum iwl_ucode_type ucode_type)
288 {
289 	struct iwl_notification_wait alive_wait;
290 	struct iwl_mvm_alive_data alive_data = {};
291 	int ret;
292 	enum iwl_ucode_type old_type = mvm->fwrt.cur_fw_img;
293 	static const u16 alive_cmd[] = { UCODE_ALIVE_NTFY };
294 	bool run_in_rfkill =
295 		ucode_type == IWL_UCODE_INIT || iwl_mvm_has_unified_ucode(mvm);
296 	u8 count;
297 	struct iwl_pc_data *pc_data;
298 
299 	if (ucode_type == IWL_UCODE_REGULAR &&
300 	    iwl_fw_dbg_conf_usniffer(mvm->fw, FW_DBG_START_FROM_ALIVE) &&
301 	    !(fw_has_capa(&mvm->fw->ucode_capa,
302 			  IWL_UCODE_TLV_CAPA_USNIFFER_UNIFIED)))
303 		ucode_type = IWL_UCODE_REGULAR_USNIFFER;
304 	iwl_fw_set_current_image(&mvm->fwrt, ucode_type);
305 	clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
306 
307 	iwl_init_notification_wait(&mvm->notif_wait, &alive_wait,
308 				   alive_cmd, ARRAY_SIZE(alive_cmd),
309 				   iwl_alive_fn, &alive_data);
310 
311 	/*
312 	 * We want to load the INIT firmware even in RFKILL
313 	 * For the unified firmware case, the ucode_type is not
314 	 * INIT, but we still need to run it.
315 	 */
316 	ret = iwl_trans_start_fw(mvm->trans, mvm->fw, ucode_type,
317 				 run_in_rfkill);
318 	if (ret) {
319 		iwl_fw_set_current_image(&mvm->fwrt, old_type);
320 		iwl_remove_notification(&mvm->notif_wait, &alive_wait);
321 		return ret;
322 	}
323 
324 	/*
325 	 * Some things may run in the background now, but we
326 	 * just wait for the ALIVE notification here.
327 	 */
328 	ret = iwl_wait_notification(&mvm->notif_wait, &alive_wait,
329 				    MVM_UCODE_ALIVE_TIMEOUT);
330 
331 	if (mvm->trans->mac_cfg->device_family ==
332 	    IWL_DEVICE_FAMILY_AX210) {
333 		/* print these registers regardless of alive fail/success */
334 		IWL_INFO(mvm, "WFPM_UMAC_PD_NOTIFICATION: 0x%x\n",
335 			 iwl_trans_read_umac_prph(mvm->trans, WFPM_ARC1_PD_NOTIFICATION));
336 		IWL_INFO(mvm, "WFPM_LMAC2_PD_NOTIFICATION: 0x%x\n",
337 			 iwl_trans_read_umac_prph(mvm->trans, WFPM_LMAC2_PD_NOTIFICATION));
338 		IWL_INFO(mvm, "WFPM_AUTH_KEY_0: 0x%x\n",
339 			 iwl_trans_read_umac_prph(mvm->trans, SB_MODIFY_CFG_FLAG));
340 		IWL_INFO(mvm, "CNVI_SCU_SEQ_DATA_DW9: 0x%x\n",
341 			 iwl_trans_read_prph(mvm->trans, CNVI_SCU_SEQ_DATA_DW9));
342 	}
343 
344 	if (ret) {
345 		struct iwl_trans *trans = mvm->trans;
346 
347 		/* SecBoot info */
348 		if (trans->mac_cfg->device_family >=
349 					IWL_DEVICE_FAMILY_22000) {
350 			IWL_ERR(mvm,
351 				"SecBoot CPU1 Status: 0x%x, CPU2 Status: 0x%x\n",
352 				iwl_trans_read_umac_prph(trans, UMAG_SB_CPU_1_STATUS),
353 				iwl_trans_read_umac_prph(trans,
354 							 UMAG_SB_CPU_2_STATUS));
355 		} else if (trans->mac_cfg->device_family >=
356 			   IWL_DEVICE_FAMILY_8000) {
357 			IWL_ERR(mvm,
358 				"SecBoot CPU1 Status: 0x%x, CPU2 Status: 0x%x\n",
359 				iwl_trans_read_prph(trans, SB_CPU_1_STATUS),
360 				iwl_trans_read_prph(trans, SB_CPU_2_STATUS));
361 		}
362 
363 		iwl_mvm_print_pd_notification(mvm);
364 
365 		/* LMAC/UMAC PC info */
366 		if (trans->mac_cfg->device_family >=
367 					IWL_DEVICE_FAMILY_22000) {
368 			pc_data = trans->dbg.pc_data;
369 			for (count = 0; count < trans->dbg.num_pc;
370 			     count++, pc_data++)
371 				IWL_ERR(mvm, "%s: 0x%x\n",
372 					pc_data->pc_name,
373 					pc_data->pc_address);
374 		} else if (trans->mac_cfg->device_family >=
375 					IWL_DEVICE_FAMILY_9000) {
376 			IWL_ERR(mvm, "UMAC PC: 0x%x\n",
377 				iwl_trans_read_umac_prph(trans,
378 							 UREG_UMAC_CURRENT_PC));
379 			IWL_ERR(mvm, "LMAC PC: 0x%x\n",
380 				iwl_trans_read_umac_prph(trans,
381 							 UREG_LMAC1_CURRENT_PC));
382 			if (iwl_mvm_is_cdb_supported(mvm))
383 				IWL_ERR(mvm, "LMAC2 PC: 0x%x\n",
384 					iwl_trans_read_umac_prph(trans,
385 								 UREG_LMAC2_CURRENT_PC));
386 		}
387 
388 		if (ret == -ETIMEDOUT && !mvm->fw_product_reset)
389 			iwl_fw_dbg_error_collect(&mvm->fwrt,
390 						 FW_DBG_TRIGGER_ALIVE_TIMEOUT);
391 
392 		iwl_fw_set_current_image(&mvm->fwrt, old_type);
393 		return ret;
394 	}
395 
396 	if (!alive_data.valid) {
397 		IWL_ERR(mvm, "Loaded ucode is not valid!\n");
398 		iwl_fw_set_current_image(&mvm->fwrt, old_type);
399 		return -EIO;
400 	}
401 
402 	/* if reached this point, Alive notification was received */
403 	iwl_mei_alive_notif(true);
404 
405 	iwl_trans_fw_alive(mvm->trans);
406 
407 	ret = iwl_pnvm_load(mvm->trans, &mvm->notif_wait,
408 			    mvm->fw, alive_data.sku_id);
409 	if (ret) {
410 		IWL_ERR(mvm, "Timeout waiting for PNVM load!\n");
411 		iwl_fw_set_current_image(&mvm->fwrt, old_type);
412 		return ret;
413 	}
414 
415 	/*
416 	 * Note: all the queues are enabled as part of the interface
417 	 * initialization, but in firmware restart scenarios they
418 	 * could be stopped, so wake them up. In firmware restart,
419 	 * mac80211 will have the queues stopped as well until the
420 	 * reconfiguration completes. During normal startup, they
421 	 * will be empty.
422 	 */
423 
424 	memset(&mvm->queue_info, 0, sizeof(mvm->queue_info));
425 	/*
426 	 * Set a 'fake' TID for the command queue, since we use the
427 	 * hweight() of the tid_bitmap as a refcount now. Not that
428 	 * we ever even consider the command queue as one we might
429 	 * want to reuse, but be safe nevertheless.
430 	 */
431 	mvm->queue_info[IWL_MVM_DQA_CMD_QUEUE].tid_bitmap =
432 		BIT(IWL_MAX_TID_COUNT + 2);
433 
434 	set_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
435 #ifdef CONFIG_IWLWIFI_DEBUGFS
436 	iwl_fw_set_dbg_rec_on(&mvm->fwrt);
437 #endif
438 
439 	/*
440 	 * For pre-MLD API (MLD API doesn't use the timestamps):
441 	 * All the BSSes in the BSS table include the GP2 in the system
442 	 * at the beacon Rx time, this is of course no longer relevant
443 	 * since we are resetting the firmware.
444 	 * Purge all the BSS table.
445 	 */
446 	if (!mvm->mld_api_is_used)
447 		cfg80211_bss_flush(mvm->hw->wiphy);
448 
449 	return 0;
450 }
451 
452 static void iwl_mvm_phy_filter_init(struct iwl_mvm *mvm,
453 				    struct iwl_phy_specific_cfg *phy_filters)
454 {
455 #ifdef CONFIG_ACPI
456 	*phy_filters = mvm->fwrt.phy_filters;
457 #endif /* CONFIG_ACPI */
458 }
459 
460 static void iwl_mvm_uats_init(struct iwl_mvm *mvm)
461 {
462 	struct iwl_mcc_allowed_ap_type_cmd_v1 *cmd __free(kfree) = NULL;
463 	int cmd_id = WIDE_ID(REGULATORY_AND_NVM_GROUP,
464 			     MCC_ALLOWED_AP_TYPE_CMD);
465 	struct iwl_host_cmd hcmd = {
466 		.id = cmd_id,
467 		.len[0] = sizeof(*cmd),
468 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
469 	};
470 	u8 cmd_ver;
471 	int ret;
472 
473 	if (mvm->trans->mac_cfg->device_family < IWL_DEVICE_FAMILY_AX210 ||
474 	    !mvm->trans->cfg->uhb_supported) {
475 		IWL_DEBUG_RADIO(mvm, "UATS feature is not supported\n");
476 		return;
477 	}
478 
479 	cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id,
480 					IWL_FW_CMD_VER_UNKNOWN);
481 	if (cmd_ver != 1) {
482 		IWL_DEBUG_RADIO(mvm,
483 				"MCC_ALLOWED_AP_TYPE_CMD ver %d not supported\n",
484 				cmd_ver);
485 		return;
486 	}
487 
488 	iwl_uefi_get_uats_table(mvm->trans, &mvm->fwrt);
489 
490 	if (!mvm->fwrt.ap_type_cmd_valid)
491 		return;
492 
493 	/* Since we free the command immediately after iwl_mvm_send_cmd, we
494 	 * must send this command in SYNC mode.
495 	 */
496 	lockdep_assert_held(&mvm->mutex);
497 
498 	cmd = kzalloc_obj(*cmd);
499 	if (!cmd)
500 		return;
501 
502 	BUILD_BUG_ON(sizeof(mvm->fwrt.ap_type_cmd.mcc_to_ap_type_map) !=
503 		     sizeof(cmd->mcc_to_ap_type_map));
504 
505 	memcpy(cmd->mcc_to_ap_type_map,
506 	       mvm->fwrt.ap_type_cmd.mcc_to_ap_type_map,
507 	       sizeof(mvm->fwrt.ap_type_cmd.mcc_to_ap_type_map));
508 
509 	hcmd.data[0] = cmd;
510 
511 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
512 	if (ret < 0)
513 		IWL_ERR(mvm, "failed to send MCC_ALLOWED_AP_TYPE_CMD (%d)\n",
514 			ret);
515 	else
516 		IWL_DEBUG_RADIO(mvm, "MCC_ALLOWED_AP_TYPE_CMD sent to FW\n");
517 }
518 
519 static int iwl_mvm_sgom_init(struct iwl_mvm *mvm)
520 {
521 	u8 cmd_ver;
522 	int ret;
523 	struct iwl_host_cmd cmd = {
524 		.id = WIDE_ID(REGULATORY_AND_NVM_GROUP,
525 			      SAR_OFFSET_MAPPING_TABLE_CMD),
526 		.flags = 0,
527 		.data[0] = &mvm->fwrt.sgom_table,
528 		.len[0] =  sizeof(mvm->fwrt.sgom_table),
529 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
530 	};
531 
532 	if (!mvm->fwrt.sgom_enabled) {
533 		IWL_DEBUG_RADIO(mvm, "SGOM table is disabled\n");
534 		return 0;
535 	}
536 
537 	cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd.id,
538 					IWL_FW_CMD_VER_UNKNOWN);
539 
540 	if (cmd_ver != 2) {
541 		IWL_DEBUG_RADIO(mvm, "command version is unsupported. version = %d\n",
542 				cmd_ver);
543 		return 0;
544 	}
545 
546 	ret = iwl_mvm_send_cmd(mvm, &cmd);
547 	if (ret < 0)
548 		IWL_ERR(mvm, "failed to send SAR_OFFSET_MAPPING_CMD (%d)\n", ret);
549 
550 	return ret;
551 }
552 
553 static int iwl_send_phy_cfg_cmd(struct iwl_mvm *mvm)
554 {
555 	u32 cmd_id = PHY_CONFIGURATION_CMD;
556 	struct iwl_phy_cfg_cmd_v3 phy_cfg_cmd;
557 	enum iwl_ucode_type ucode_type = mvm->fwrt.cur_fw_img;
558 	u8 cmd_ver;
559 	size_t cmd_size;
560 
561 	if (iwl_mvm_has_unified_ucode(mvm) &&
562 	    !mvm->trans->cfg->tx_with_siso_diversity)
563 		return 0;
564 
565 	if (mvm->trans->cfg->tx_with_siso_diversity) {
566 		/*
567 		 * TODO: currently we don't set the antenna but letting the NIC
568 		 * to decide which antenna to use. This should come from BIOS.
569 		 */
570 		phy_cfg_cmd.phy_cfg =
571 			cpu_to_le32(FW_PHY_CFG_CHAIN_SAD_ENABLED);
572 	}
573 
574 	/* Set parameters */
575 	phy_cfg_cmd.phy_cfg = cpu_to_le32(iwl_mvm_get_phy_config(mvm));
576 
577 	/* set flags extra PHY configuration flags from the device's cfg */
578 	phy_cfg_cmd.phy_cfg |=
579 		cpu_to_le32(mvm->trans->mac_cfg->extra_phy_cfg_flags);
580 
581 	phy_cfg_cmd.calib_control.event_trigger =
582 		mvm->fw->default_calib[ucode_type].event_trigger;
583 	phy_cfg_cmd.calib_control.flow_trigger =
584 		mvm->fw->default_calib[ucode_type].flow_trigger;
585 
586 	cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id,
587 					IWL_FW_CMD_VER_UNKNOWN);
588 	if (cmd_ver >= 3)
589 		iwl_mvm_phy_filter_init(mvm, &phy_cfg_cmd.phy_specific_cfg);
590 
591 	IWL_DEBUG_INFO(mvm, "Sending Phy CFG command: 0x%x\n",
592 		       phy_cfg_cmd.phy_cfg);
593 	cmd_size = (cmd_ver == 3) ? sizeof(struct iwl_phy_cfg_cmd_v3) :
594 				    sizeof(struct iwl_phy_cfg_cmd_v1);
595 	return iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, cmd_size, &phy_cfg_cmd);
596 }
597 
598 static int iwl_run_unified_mvm_ucode(struct iwl_mvm *mvm)
599 {
600 	struct iwl_notification_wait init_wait;
601 	struct iwl_nvm_access_complete_cmd nvm_complete = {};
602 	struct iwl_init_extended_cfg_cmd init_cfg = {
603 		.init_flags = cpu_to_le32(BIT(IWL_INIT_NVM)),
604 	};
605 	static const u16 init_complete[] = {
606 		INIT_COMPLETE_NOTIF,
607 	};
608 	u32 sb_cfg;
609 	int ret;
610 
611 	if (mvm->trans->cfg->tx_with_siso_diversity)
612 		init_cfg.init_flags |= cpu_to_le32(BIT(IWL_INIT_PHY));
613 
614 	lockdep_assert_held(&mvm->mutex);
615 
616 	mvm->rfkill_safe_init_done = false;
617 
618 	if (mvm->trans->mac_cfg->device_family == IWL_DEVICE_FAMILY_AX210) {
619 		sb_cfg = iwl_trans_read_umac_prph(mvm->trans, SB_MODIFY_CFG_FLAG);
620 		/* if needed, we'll reset this on our way out later */
621 		mvm->fw_product_reset = sb_cfg == SB_CFG_RESIDES_IN_ROM;
622 		if (mvm->fw_product_reset && iwl_mei_pldr_req())
623 			return -EBUSY;
624 	}
625 
626 	iwl_init_notification_wait(&mvm->notif_wait,
627 				   &init_wait,
628 				   init_complete,
629 				   ARRAY_SIZE(init_complete),
630 				   iwl_wait_init_complete,
631 				   NULL);
632 
633 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_EARLY, NULL);
634 
635 	/* Will also start the device */
636 	ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_REGULAR);
637 	if (ret) {
638 		IWL_ERR(mvm, "Failed to start RT ucode: %d\n", ret);
639 
640 		/* if we needed reset then fail here, but notify and remove */
641 		if (mvm->fw_product_reset) {
642 			iwl_mei_alive_notif(false);
643 			iwl_trans_reset(mvm->trans, IWL_RESET_MODE_RESCAN);
644 		}
645 
646 		goto error;
647 	}
648 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_AFTER_ALIVE,
649 			       NULL);
650 
651 	/* Send init config command to mark that we are sending NVM access
652 	 * commands
653 	 */
654 	ret = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(SYSTEM_GROUP,
655 						INIT_EXTENDED_CFG_CMD),
656 				   CMD_SEND_IN_RFKILL,
657 				   sizeof(init_cfg), &init_cfg);
658 	if (ret) {
659 		IWL_ERR(mvm, "Failed to run init config command: %d\n",
660 			ret);
661 		goto error;
662 	}
663 
664 	/* Load NVM to NIC if needed */
665 	if (mvm->nvm_file_name) {
666 		ret = iwl_read_external_nvm(mvm->trans, mvm->nvm_file_name,
667 					    mvm->nvm_sections);
668 		if (ret)
669 			goto error;
670 		ret = iwl_mvm_load_nvm_to_nic(mvm);
671 		if (ret)
672 			goto error;
673 	}
674 
675 	ret = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(REGULATORY_AND_NVM_GROUP,
676 						NVM_ACCESS_COMPLETE),
677 				   CMD_SEND_IN_RFKILL,
678 				   sizeof(nvm_complete), &nvm_complete);
679 	if (ret) {
680 		IWL_ERR(mvm, "Failed to run complete NVM access: %d\n",
681 			ret);
682 		goto error;
683 	}
684 
685 	ret = iwl_send_phy_cfg_cmd(mvm);
686 	if (ret) {
687 		IWL_ERR(mvm, "Failed to run PHY configuration: %d\n",
688 			ret);
689 		goto error;
690 	}
691 
692 	/* We wait for the INIT complete notification */
693 	ret = iwl_wait_notification(&mvm->notif_wait, &init_wait,
694 				    MVM_UCODE_ALIVE_TIMEOUT);
695 	if (ret)
696 		return ret;
697 
698 	/* Read the NVM only at driver load time, no need to do this twice */
699 	if (!mvm->nvm_data) {
700 		mvm->nvm_data = iwl_get_nvm(mvm->trans, mvm->fw,
701 					    mvm->set_tx_ant, mvm->set_rx_ant);
702 		if (IS_ERR(mvm->nvm_data)) {
703 			ret = PTR_ERR(mvm->nvm_data);
704 			mvm->nvm_data = NULL;
705 			IWL_ERR(mvm, "Failed to read NVM: %d\n", ret);
706 			return ret;
707 		}
708 	}
709 
710 	mvm->rfkill_safe_init_done = true;
711 
712 	return 0;
713 
714 error:
715 	iwl_remove_notification(&mvm->notif_wait, &init_wait);
716 	return ret;
717 }
718 
719 int iwl_run_init_mvm_ucode(struct iwl_mvm *mvm)
720 {
721 	struct iwl_notification_wait calib_wait;
722 	static const u16 init_complete[] = {
723 		INIT_COMPLETE_NOTIF,
724 		CALIB_RES_NOTIF_PHY_DB
725 	};
726 	int ret;
727 
728 	if (iwl_mvm_has_unified_ucode(mvm))
729 		return iwl_run_unified_mvm_ucode(mvm);
730 
731 	lockdep_assert_held(&mvm->mutex);
732 
733 	mvm->rfkill_safe_init_done = false;
734 
735 	iwl_init_notification_wait(&mvm->notif_wait,
736 				   &calib_wait,
737 				   init_complete,
738 				   ARRAY_SIZE(init_complete),
739 				   iwl_wait_phy_db_entry,
740 				   mvm->phy_db);
741 
742 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_EARLY, NULL);
743 
744 	/* Will also start the device */
745 	ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_INIT);
746 	if (ret) {
747 		IWL_ERR(mvm, "Failed to start INIT ucode: %d\n", ret);
748 		goto remove_notif;
749 	}
750 
751 	if (mvm->trans->mac_cfg->device_family < IWL_DEVICE_FAMILY_8000) {
752 		ret = iwl_mvm_send_bt_init_conf(mvm);
753 		if (ret)
754 			goto remove_notif;
755 	}
756 
757 	/* Read the NVM only at driver load time, no need to do this twice */
758 	if (!mvm->nvm_data) {
759 		ret = iwl_nvm_init(mvm);
760 		if (ret) {
761 			IWL_ERR(mvm, "Failed to read NVM: %d\n", ret);
762 			goto remove_notif;
763 		}
764 	}
765 
766 	/* In case we read the NVM from external file, load it to the NIC */
767 	if (mvm->nvm_file_name) {
768 		ret = iwl_mvm_load_nvm_to_nic(mvm);
769 		if (ret)
770 			goto remove_notif;
771 	}
772 
773 	WARN_ONCE(mvm->nvm_data->nvm_version < mvm->trans->cfg->nvm_ver,
774 		  "Too old NVM version (0x%0x, required = 0x%0x)",
775 		  mvm->nvm_data->nvm_version, mvm->trans->cfg->nvm_ver);
776 
777 	/*
778 	 * abort after reading the nvm in case RF Kill is on, we will complete
779 	 * the init seq later when RF kill will switch to off
780 	 */
781 	if (iwl_mvm_is_radio_hw_killed(mvm)) {
782 		IWL_DEBUG_RF_KILL(mvm,
783 				  "jump over all phy activities due to RF kill\n");
784 		goto remove_notif;
785 	}
786 
787 	mvm->rfkill_safe_init_done = true;
788 
789 	/* Send TX valid antennas before triggering calibrations */
790 	ret = iwl_send_tx_ant_cfg(mvm, iwl_mvm_get_valid_tx_ant(mvm));
791 	if (ret)
792 		goto remove_notif;
793 
794 	ret = iwl_send_phy_cfg_cmd(mvm);
795 	if (ret) {
796 		IWL_ERR(mvm, "Failed to run INIT calibrations: %d\n",
797 			ret);
798 		goto remove_notif;
799 	}
800 
801 	/*
802 	 * Some things may run in the background now, but we
803 	 * just wait for the calibration complete notification.
804 	 */
805 	ret = iwl_wait_notification(&mvm->notif_wait, &calib_wait,
806 				    MVM_UCODE_CALIB_TIMEOUT);
807 	if (!ret)
808 		goto out;
809 
810 	if (iwl_mvm_is_radio_hw_killed(mvm)) {
811 		IWL_DEBUG_RF_KILL(mvm, "RFKILL while calibrating.\n");
812 		ret = 0;
813 	} else {
814 		IWL_ERR(mvm, "Failed to run INIT calibrations: %d\n",
815 			ret);
816 	}
817 
818 	goto out;
819 
820 remove_notif:
821 	iwl_remove_notification(&mvm->notif_wait, &calib_wait);
822 out:
823 	mvm->rfkill_safe_init_done = false;
824 	if (!mvm->nvm_data) {
825 		/* we want to debug INIT and we have no NVM - fake */
826 		mvm->nvm_data = kzalloc(sizeof(struct iwl_nvm_data) +
827 					sizeof(struct ieee80211_channel) +
828 					sizeof(struct ieee80211_rate),
829 					GFP_KERNEL);
830 		if (!mvm->nvm_data)
831 			return -ENOMEM;
832 		mvm->nvm_data->bands[0].channels = mvm->nvm_data->channels;
833 		mvm->nvm_data->bands[0].n_channels = 1;
834 		mvm->nvm_data->bands[0].n_bitrates = 1;
835 		mvm->nvm_data->bands[0].bitrates =
836 			(void *)(mvm->nvm_data->channels + 1);
837 		mvm->nvm_data->bands[0].bitrates->hw_value = 10;
838 	}
839 
840 	return ret;
841 }
842 
843 static int iwl_mvm_config_ltr(struct iwl_mvm *mvm)
844 {
845 	struct iwl_ltr_config_cmd cmd = {
846 		.flags = cpu_to_le32(LTR_CFG_FLAG_FEATURE_ENABLE),
847 	};
848 
849 	if (!iwl_trans_is_ltr_enabled(mvm->trans))
850 		return 0;
851 
852 	return iwl_mvm_send_cmd_pdu(mvm, LTR_CONFIG, 0,
853 				    sizeof(cmd), &cmd);
854 }
855 
856 int iwl_mvm_sar_select_profile(struct iwl_mvm *mvm, int prof_a, int prof_b)
857 {
858 	u32 cmd_id = REDUCE_TX_POWER_CMD;
859 	struct iwl_dev_tx_power_cmd_v3_v8 cmd = {
860 		.common.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_CHAINS),
861 	};
862 	struct iwl_dev_tx_power_cmd cmd_v9_v10 = {
863 		.common.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_CHAINS),
864 	};
865 	__le16 *per_chain;
866 	int ret;
867 	u16 len = 0;
868 	u32 n_subbands;
869 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id, 3);
870 	void *cmd_data = &cmd;
871 
872 	if (cmd_ver == 10) {
873 		len = sizeof(cmd_v9_v10.v10);
874 		n_subbands = IWL_NUM_SUB_BANDS_V2;
875 		per_chain = &cmd_v9_v10.v10.per_chain[0][0][0];
876 		cmd_v9_v10.v10.flags =
877 			cpu_to_le32(mvm->fwrt.reduced_power_flags);
878 	} else if (cmd_ver == 9) {
879 		len = sizeof(cmd_v9_v10.v9);
880 		n_subbands = IWL_NUM_SUB_BANDS_V1;
881 		per_chain = &cmd_v9_v10.v9.per_chain[0][0];
882 	} else if (cmd_ver == 8) {
883 		len = sizeof(cmd.v8);
884 		n_subbands = IWL_NUM_SUB_BANDS_V2;
885 		per_chain = cmd.v8.per_chain[0][0];
886 		cmd.v8.flags = cpu_to_le32(mvm->fwrt.reduced_power_flags);
887 	} else if (fw_has_api(&mvm->fw->ucode_capa,
888 			      IWL_UCODE_TLV_API_REDUCE_TX_POWER)) {
889 		len = sizeof(cmd.v5);
890 		n_subbands = IWL_NUM_SUB_BANDS_V1;
891 		per_chain = cmd.v5.per_chain[0][0];
892 	} else if (fw_has_capa(&mvm->fw->ucode_capa,
893 			       IWL_UCODE_TLV_CAPA_TX_POWER_ACK)) {
894 		len = sizeof(cmd.v4);
895 		n_subbands = IWL_NUM_SUB_BANDS_V1;
896 		per_chain = cmd.v4.per_chain[0][0];
897 	} else {
898 		len = sizeof(cmd.v3);
899 		n_subbands = IWL_NUM_SUB_BANDS_V1;
900 		per_chain = cmd.v3.per_chain[0][0];
901 	}
902 
903 	/* all structs have the same common part, add its length */
904 	len += sizeof(cmd.common);
905 
906 	if (cmd_ver < 9)
907 		len += sizeof(cmd.per_band);
908 	else
909 		cmd_data = &cmd_v9_v10;
910 
911 	ret = iwl_sar_fill_profile(&mvm->fwrt, per_chain,
912 				   IWL_NUM_CHAIN_TABLES,
913 				   n_subbands, prof_a, prof_b,
914 				   mvm->fwrt.sar_profiles, false);
915 
916 	/* return on error or if the profile is disabled (positive number) */
917 	if (ret)
918 		return ret;
919 
920 	iwl_mei_set_power_limit(per_chain);
921 
922 	IWL_DEBUG_RADIO(mvm, "Sending REDUCE_TX_POWER_CMD per chain\n");
923 	return iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, len, cmd_data);
924 }
925 
926 int iwl_mvm_get_sar_geo_profile(struct iwl_mvm *mvm)
927 {
928 	union iwl_geo_tx_power_profiles_cmd geo_tx_cmd = {};
929 	struct iwl_geo_tx_power_profiles_resp *resp;
930 	u16 len;
931 	int ret;
932 	struct iwl_host_cmd cmd = {
933 		.id = WIDE_ID(PHY_OPS_GROUP, PER_CHAIN_LIMIT_OFFSET_CMD),
934 		.flags = CMD_WANT_SKB,
935 		.data = { &geo_tx_cmd },
936 	};
937 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd.id,
938 					   IWL_FW_CMD_VER_UNKNOWN);
939 
940 	/* the ops field is at the same spot for all versions, so set in v1 */
941 	geo_tx_cmd.v1.ops =
942 		cpu_to_le32(IWL_PER_CHAIN_OFFSET_GET_CURRENT_TABLE);
943 
944 	if (cmd_ver == 5)
945 		len = sizeof(geo_tx_cmd.v5);
946 	else if (cmd_ver == 4)
947 		len = sizeof(geo_tx_cmd.v4);
948 	else if (cmd_ver == 3)
949 		len = sizeof(geo_tx_cmd.v3);
950 	else if (fw_has_api(&mvm->fwrt.fw->ucode_capa,
951 			    IWL_UCODE_TLV_API_SAR_TABLE_VER))
952 		len = sizeof(geo_tx_cmd.v2);
953 	else
954 		len = sizeof(geo_tx_cmd.v1);
955 
956 	if (!iwl_sar_geo_support(&mvm->fwrt))
957 		return -EOPNOTSUPP;
958 
959 	cmd.len[0] = len;
960 
961 	ret = iwl_mvm_send_cmd(mvm, &cmd);
962 	if (ret) {
963 		IWL_ERR(mvm, "Failed to get geographic profile info %d\n", ret);
964 		return ret;
965 	}
966 
967 	if (IWL_FW_CHECK(mvm,
968 			 iwl_rx_packet_payload_len(cmd.resp_pkt) !=
969 			 sizeof(*resp),
970 			 "Wrong size for iwl_geo_tx_power_profiles_resp: %d\n",
971 			 iwl_rx_packet_payload_len(cmd.resp_pkt))) {
972 		ret = -EIO;
973 		goto out;
974 	}
975 
976 	resp = (void *)cmd.resp_pkt->data;
977 	ret = le32_to_cpu(resp->profile_idx);
978 
979 	if (WARN_ON(ret > BIOS_GEO_MAX_PROFILE_NUM))
980 		ret = -EIO;
981 
982 out:
983 	iwl_free_resp(&cmd);
984 	return ret;
985 }
986 
987 static int iwl_mvm_sar_geo_init(struct iwl_mvm *mvm)
988 {
989 	u32 cmd_id = WIDE_ID(PHY_OPS_GROUP, PER_CHAIN_LIMIT_OFFSET_CMD);
990 	union iwl_geo_tx_power_profiles_cmd cmd = {};
991 	u16 len;
992 	u32 n_bands;
993 	u32 n_profiles;
994 	__le32 sk = cpu_to_le32(0);
995 	int ret;
996 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id,
997 					   IWL_FW_CMD_VER_UNKNOWN);
998 
999 	BUILD_BUG_ON(offsetof(struct iwl_geo_tx_power_profiles_cmd_v1, ops) !=
1000 		     offsetof(struct iwl_geo_tx_power_profiles_cmd_v2, ops) ||
1001 		     offsetof(struct iwl_geo_tx_power_profiles_cmd_v2, ops) !=
1002 		     offsetof(struct iwl_geo_tx_power_profiles_cmd_v3, ops) ||
1003 		     offsetof(struct iwl_geo_tx_power_profiles_cmd_v3, ops) !=
1004 		     offsetof(struct iwl_geo_tx_power_profiles_cmd_v4, ops) ||
1005 		     offsetof(struct iwl_geo_tx_power_profiles_cmd_v4, ops) !=
1006 		     offsetof(struct iwl_geo_tx_power_profiles_cmd_v5, ops));
1007 
1008 	/* the ops field is at the same spot for all versions, so set in v1 */
1009 	cmd.v1.ops = cpu_to_le32(IWL_PER_CHAIN_OFFSET_SET_TABLES);
1010 
1011 	/* Only set to South Korea if the table revision is 1 */
1012 	if (mvm->fwrt.geo_rev == 1)
1013 		sk = cpu_to_le32(1);
1014 
1015 	if (cmd_ver == 5) {
1016 		len = sizeof(cmd.v5);
1017 		n_bands = ARRAY_SIZE(cmd.v5.table[0]);
1018 		n_profiles = BIOS_GEO_MAX_PROFILE_NUM;
1019 		cmd.v5.table_revision = sk;
1020 	} else if (cmd_ver == 4) {
1021 		len = sizeof(cmd.v4);
1022 		n_bands = ARRAY_SIZE(cmd.v4.table[0]);
1023 		n_profiles = BIOS_GEO_MAX_PROFILE_NUM;
1024 		cmd.v4.table_revision = sk;
1025 	} else if (cmd_ver == 3) {
1026 		len = sizeof(cmd.v3);
1027 		n_bands = ARRAY_SIZE(cmd.v3.table[0]);
1028 		n_profiles = BIOS_GEO_MIN_PROFILE_NUM;
1029 		cmd.v3.table_revision = sk;
1030 	} else if (fw_has_api(&mvm->fwrt.fw->ucode_capa,
1031 			      IWL_UCODE_TLV_API_SAR_TABLE_VER)) {
1032 		len = sizeof(cmd.v2);
1033 		n_bands = ARRAY_SIZE(cmd.v2.table[0]);
1034 		n_profiles = BIOS_GEO_MIN_PROFILE_NUM;
1035 		cmd.v2.table_revision = sk;
1036 	} else {
1037 		len = sizeof(cmd.v1);
1038 		n_bands = ARRAY_SIZE(cmd.v1.table[0]);
1039 		n_profiles = BIOS_GEO_MIN_PROFILE_NUM;
1040 	}
1041 
1042 	BUILD_BUG_ON(offsetof(struct iwl_geo_tx_power_profiles_cmd_v1, table) !=
1043 		     offsetof(struct iwl_geo_tx_power_profiles_cmd_v2, table) ||
1044 		     offsetof(struct iwl_geo_tx_power_profiles_cmd_v2, table) !=
1045 		     offsetof(struct iwl_geo_tx_power_profiles_cmd_v3, table) ||
1046 		     offsetof(struct iwl_geo_tx_power_profiles_cmd_v3, table) !=
1047 		     offsetof(struct iwl_geo_tx_power_profiles_cmd_v4, table) ||
1048 		     offsetof(struct iwl_geo_tx_power_profiles_cmd_v4, table) !=
1049 		     offsetof(struct iwl_geo_tx_power_profiles_cmd_v5, table));
1050 	/* the table is at the same position for all versions, so set use v1 */
1051 	ret = iwl_sar_geo_fill_table(&mvm->fwrt, &cmd.v1.table[0][0],
1052 				     n_bands, n_profiles);
1053 
1054 	/*
1055 	 * It is a valid scenario to not support SAR, or miss wgds table,
1056 	 * but in that case there is no need to send the command.
1057 	 */
1058 	if (ret)
1059 		return 0;
1060 
1061 	return iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, len, &cmd);
1062 }
1063 
1064 static bool iwl_mvm_ppag_value_valid(struct iwl_fw_runtime *fwrt, int chain,
1065 				     int subband)
1066 {
1067 	s8 ppag_val = fwrt->ppag_chains[chain].subbands[subband];
1068 
1069 	if ((subband == 0 &&
1070 	     (ppag_val > IWL_PPAG_MAX_LB || ppag_val < IWL_PPAG_MIN_LB)) ||
1071 	    (subband != 0 &&
1072 	     (ppag_val > IWL_PPAG_MAX_HB || ppag_val < IWL_PPAG_MIN_HB))) {
1073 		IWL_DEBUG_RADIO(fwrt, "Invalid PPAG value: %d\n", ppag_val);
1074 		return false;
1075 	}
1076 	return true;
1077 }
1078 
1079 static int iwl_mvm_fill_ppag_table(struct iwl_fw_runtime *fwrt,
1080 				   union iwl_ppag_table_cmd *cmd,
1081 				   int *cmd_size)
1082 {
1083 	u8 cmd_ver;
1084 	int i, j, num_sub_bands;
1085 	s8 *gain;
1086 	bool send_ppag_always;
1087 
1088 	/* many firmware images for JF lie about this */
1089 	if (CSR_HW_RFID_TYPE(fwrt->trans->info.hw_rf_id) ==
1090 	    CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_JF))
1091 		return -EOPNOTSUPP;
1092 
1093 	if (!fw_has_capa(&fwrt->fw->ucode_capa, IWL_UCODE_TLV_CAPA_SET_PPAG)) {
1094 		IWL_DEBUG_RADIO(fwrt,
1095 				"PPAG capability not supported by FW, command not sent.\n");
1096 		return -EINVAL;
1097 	}
1098 
1099 	cmd_ver = iwl_fw_lookup_cmd_ver(fwrt->fw,
1100 					WIDE_ID(PHY_OPS_GROUP,
1101 						PER_PLATFORM_ANT_GAIN_CMD), 1);
1102 	/*
1103 	 * Starting from ver 4, driver needs to send the PPAG CMD regardless
1104 	 * if PPAG is enabled/disabled or valid/invalid.
1105 	 */
1106 	send_ppag_always = cmd_ver > 3;
1107 
1108 	/* Don't send PPAG if it is disabled */
1109 	if (!send_ppag_always && !fwrt->ppag_flags) {
1110 		IWL_DEBUG_RADIO(fwrt, "PPAG not enabled, command not sent.\n");
1111 		return -EINVAL;
1112 	}
1113 
1114 	IWL_DEBUG_RADIO(fwrt, "PPAG cmd ver is %d\n", cmd_ver);
1115 	if (cmd_ver == 1) {
1116 		num_sub_bands = IWL_NUM_SUB_BANDS_V1;
1117 		gain = cmd->v1.gain[0];
1118 		*cmd_size = sizeof(cmd->v1);
1119 		cmd->v1.flags =
1120 			cpu_to_le32(fwrt->ppag_flags & IWL_PPAG_CMD_V1_MASK);
1121 		if (fwrt->ppag_bios_rev >= 1) {
1122 			/* in this case FW supports revision 0 */
1123 			IWL_DEBUG_RADIO(fwrt,
1124 					"PPAG table rev is %d, send truncated table\n",
1125 					fwrt->ppag_bios_rev);
1126 		}
1127 	} else if (cmd_ver == 5) {
1128 		num_sub_bands = IWL_NUM_SUB_BANDS_V2;
1129 		gain = cmd->v5.gain[0];
1130 		*cmd_size = sizeof(cmd->v5);
1131 		cmd->v5.flags =
1132 			cpu_to_le32(fwrt->ppag_flags & IWL_PPAG_CMD_V5_MASK);
1133 		if (fwrt->ppag_bios_rev == 0) {
1134 			/* in this case FW supports revisions 1,2 or 3 */
1135 			IWL_DEBUG_RADIO(fwrt,
1136 					"PPAG table rev is 0, send padded table\n");
1137 		}
1138 	} else if (cmd_ver == 7) {
1139 		num_sub_bands = IWL_NUM_SUB_BANDS_V2;
1140 		gain = cmd->v7.gain[0];
1141 		*cmd_size = sizeof(cmd->v7);
1142 		cmd->v7.ppag_config_info.hdr.table_source =
1143 			fwrt->ppag_bios_source;
1144 		cmd->v7.ppag_config_info.hdr.table_revision =
1145 			fwrt->ppag_bios_rev;
1146 		cmd->v7.ppag_config_info.value = cpu_to_le32(fwrt->ppag_flags);
1147 	} else {
1148 		IWL_DEBUG_RADIO(fwrt, "Unsupported PPAG command version\n");
1149 		return -EINVAL;
1150 	}
1151 
1152 	/* ppag mode */
1153 	IWL_DEBUG_RADIO(fwrt,
1154 			"PPAG MODE bits were read from bios: %d\n",
1155 			fwrt->ppag_flags);
1156 
1157 	if (cmd_ver == 1 &&
1158 	    !fw_has_capa(&fwrt->fw->ucode_capa,
1159 			 IWL_UCODE_TLV_CAPA_PPAG_CHINA_BIOS_SUPPORT)) {
1160 		cmd->v1.flags &= cpu_to_le32(IWL_PPAG_ETSI_MASK);
1161 		IWL_DEBUG_RADIO(fwrt, "masking ppag China bit\n");
1162 	} else {
1163 		IWL_DEBUG_RADIO(fwrt, "isn't masking ppag China bit\n");
1164 	}
1165 
1166 	/* The 'flags' field is the same in v1 and v5 so we can just
1167 	 * use v1 to access it.
1168 	 */
1169 	IWL_DEBUG_RADIO(fwrt,
1170 			"PPAG MODE bits going to be sent: %d\n",
1171 			(cmd_ver < 7) ? le32_to_cpu(cmd->v1.flags) :
1172 					le32_to_cpu(cmd->v7.ppag_config_info.value));
1173 
1174 	for (i = 0; i < IWL_NUM_CHAIN_LIMITS; i++) {
1175 		for (j = 0; j < num_sub_bands; j++) {
1176 			if (!send_ppag_always &&
1177 			    !iwl_mvm_ppag_value_valid(fwrt, i, j))
1178 				return -EINVAL;
1179 
1180 			gain[i * num_sub_bands + j] =
1181 				fwrt->ppag_chains[i].subbands[j];
1182 			IWL_DEBUG_RADIO(fwrt,
1183 					"PPAG table: chain[%d] band[%d]: gain = %d\n",
1184 					i, j, gain[i * num_sub_bands + j]);
1185 		}
1186 	}
1187 
1188 	return 0;
1189 }
1190 
1191 int iwl_mvm_ppag_send_cmd(struct iwl_mvm *mvm)
1192 {
1193 	union iwl_ppag_table_cmd cmd;
1194 	int ret, cmd_size;
1195 
1196 	ret = iwl_mvm_fill_ppag_table(&mvm->fwrt, &cmd, &cmd_size);
1197 	/* Not supporting PPAG table is a valid scenario */
1198 	if (ret < 0)
1199 		return 0;
1200 
1201 	IWL_DEBUG_RADIO(mvm, "Sending PER_PLATFORM_ANT_GAIN_CMD\n");
1202 	ret = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(PHY_OPS_GROUP,
1203 						PER_PLATFORM_ANT_GAIN_CMD),
1204 				   0, cmd_size, &cmd);
1205 	if (ret < 0)
1206 		IWL_ERR(mvm, "failed to send PER_PLATFORM_ANT_GAIN_CMD (%d)\n",
1207 			ret);
1208 
1209 	return ret;
1210 }
1211 
1212 static int iwl_mvm_ppag_init(struct iwl_mvm *mvm)
1213 {
1214 	/* no need to read the table, done in INIT stage */
1215 	if (!(iwl_is_ppag_approved(&mvm->fwrt)))
1216 		return 0;
1217 
1218 	return iwl_mvm_ppag_send_cmd(mvm);
1219 }
1220 
1221 static void iwl_mvm_tas_init(struct iwl_mvm *mvm)
1222 {
1223 	u32 cmd_id = WIDE_ID(REGULATORY_AND_NVM_GROUP, TAS_CONFIG);
1224 	int fw_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id,
1225 					   IWL_FW_CMD_VER_UNKNOWN);
1226 	struct iwl_tas_selection_data selection_data = {};
1227 	struct iwl_tas_config_cmd_v2_v4 cmd_v2_v4 = {};
1228 	struct iwl_tas_config_cmd cmd_v5 = {};
1229 	struct iwl_tas_data data = {};
1230 	void *cmd_data = &cmd_v2_v4;
1231 	int cmd_size;
1232 	int ret;
1233 
1234 	BUILD_BUG_ON(ARRAY_SIZE(data.block_list_array) !=
1235 		     IWL_WTAS_BLACK_LIST_MAX);
1236 	BUILD_BUG_ON(ARRAY_SIZE(cmd_v2_v4.common.block_list_array) !=
1237 		     IWL_WTAS_BLACK_LIST_MAX);
1238 	BUILD_BUG_ON(ARRAY_SIZE(cmd_v5.block_list_array) !=
1239 		     IWL_WTAS_BLACK_LIST_MAX);
1240 
1241 	if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_TAS_CFG)) {
1242 		IWL_DEBUG_RADIO(mvm, "TAS not enabled in FW\n");
1243 		return;
1244 	}
1245 
1246 	ret = iwl_bios_get_tas_table(&mvm->fwrt, &data);
1247 	if (ret < 0) {
1248 		IWL_DEBUG_RADIO(mvm,
1249 				"TAS table invalid or unavailable. (%d)\n",
1250 				ret);
1251 		return;
1252 	}
1253 
1254 	if (ret == 0 && fw_ver < 5)
1255 		return;
1256 
1257 	if (!iwl_is_tas_approved()) {
1258 		IWL_DEBUG_RADIO(mvm,
1259 				"System vendor '%s' is not in the approved list, disabling TAS in US and Canada.\n",
1260 				dmi_get_system_info(DMI_SYS_VENDOR) ?: "<unknown>");
1261 		if ((!iwl_add_mcc_to_tas_block_list(data.block_list_array,
1262 						    &data.block_list_size,
1263 						    IWL_MCC_US)) ||
1264 		    (!iwl_add_mcc_to_tas_block_list(data.block_list_array,
1265 						    &data.block_list_size,
1266 						    IWL_MCC_CANADA))) {
1267 			IWL_DEBUG_RADIO(mvm,
1268 					"Unable to add US/Canada to TAS block list, disabling TAS\n");
1269 			return;
1270 		}
1271 	} else {
1272 		IWL_DEBUG_RADIO(mvm,
1273 				"System vendor '%s' is in the approved list.\n",
1274 				dmi_get_system_info(DMI_SYS_VENDOR) ?: "<unknown>");
1275 	}
1276 
1277 	if (fw_ver < 5) {
1278 		selection_data = iwl_parse_tas_selection(data.tas_selection,
1279 							 data.table_revision);
1280 		cmd_v2_v4.common.block_list_size =
1281 			cpu_to_le32(data.block_list_size);
1282 		for (u8 i = 0; i < data.block_list_size; i++)
1283 			cmd_v2_v4.common.block_list_array[i] =
1284 				cpu_to_le32(data.block_list_array[i]);
1285 	}
1286 
1287 	if (fw_ver == 5) {
1288 		cmd_size = sizeof(cmd_v5);
1289 		cmd_data = &cmd_v5;
1290 		cmd_v5.block_list_size = cpu_to_le16(data.block_list_size);
1291 		for (u16 i = 0; i < data.block_list_size; i++)
1292 			cmd_v5.block_list_array[i] =
1293 				cpu_to_le16(data.block_list_array[i]);
1294 		cmd_v5.tas_config_info.hdr.table_source = data.table_source;
1295 		cmd_v5.tas_config_info.hdr.table_revision =
1296 			data.table_revision;
1297 		cmd_v5.tas_config_info.value = cpu_to_le32(data.tas_selection);
1298 	} else if (fw_ver == 4) {
1299 		cmd_size = sizeof(cmd_v2_v4.common) + sizeof(cmd_v2_v4.v4);
1300 		cmd_v2_v4.v4.override_tas_iec = selection_data.override_tas_iec;
1301 		cmd_v2_v4.v4.enable_tas_iec = selection_data.enable_tas_iec;
1302 		cmd_v2_v4.v4.usa_tas_uhb_allowed =
1303 			selection_data.usa_tas_uhb_allowed;
1304 		if (fw_has_capa(&mvm->fw->ucode_capa,
1305 				IWL_UCODE_TLV_CAPA_UHB_CANADA_TAS_SUPPORT) &&
1306 		    selection_data.canada_tas_uhb_allowed)
1307 			cmd_v2_v4.v4.uhb_allowed_flags = TAS_UHB_ALLOWED_CANADA;
1308 	} else if (fw_ver == 3) {
1309 		cmd_size = sizeof(cmd_v2_v4.common) + sizeof(cmd_v2_v4.v3);
1310 		cmd_v2_v4.v3.override_tas_iec =
1311 			cpu_to_le16(selection_data.override_tas_iec);
1312 		cmd_v2_v4.v3.enable_tas_iec =
1313 			cpu_to_le16(selection_data.enable_tas_iec);
1314 	} else if (fw_ver == 2) {
1315 		cmd_size = sizeof(cmd_v2_v4.common);
1316 	} else {
1317 		return;
1318 	}
1319 
1320 	ret = iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, cmd_size, cmd_data);
1321 	if (ret < 0)
1322 		IWL_DEBUG_RADIO(mvm, "failed to send TAS_CONFIG (%d)\n", ret);
1323 }
1324 
1325 static __le32 iwl_mvm_get_lari_config_bitmap(struct iwl_fw_runtime *fwrt)
1326 {
1327 	int ret;
1328 	u32 val;
1329 	__le32 config_bitmap = 0;
1330 
1331 	switch (CSR_HW_RFID_TYPE(fwrt->trans->info.hw_rf_id)) {
1332 	case IWL_CFG_RF_TYPE_HR1:
1333 	case IWL_CFG_RF_TYPE_HR2:
1334 	case IWL_CFG_RF_TYPE_JF1:
1335 	case IWL_CFG_RF_TYPE_JF2:
1336 		ret = iwl_bios_get_dsm(fwrt, DSM_FUNC_ENABLE_INDONESIA_5G2,
1337 				       &val);
1338 
1339 		if (!ret && val == DSM_VALUE_INDONESIA_ENABLE)
1340 			config_bitmap |=
1341 			    cpu_to_le32(LARI_CONFIG_ENABLE_5G2_IN_INDONESIA_MSK);
1342 		break;
1343 	default:
1344 		break;
1345 	}
1346 
1347 	ret = iwl_bios_get_dsm(fwrt, DSM_FUNC_DISABLE_SRD, &val);
1348 	if (!ret) {
1349 		if (val == DSM_VALUE_SRD_PASSIVE)
1350 			config_bitmap |=
1351 				cpu_to_le32(LARI_CONFIG_CHANGE_ETSI_TO_PASSIVE_MSK);
1352 		else if (val == DSM_VALUE_SRD_DISABLE)
1353 			config_bitmap |=
1354 				cpu_to_le32(LARI_CONFIG_CHANGE_ETSI_TO_DISABLED_MSK);
1355 	}
1356 
1357 	if (fw_has_capa(&fwrt->fw->ucode_capa,
1358 			IWL_UCODE_TLV_CAPA_CHINA_22_REG_SUPPORT)) {
1359 		ret = iwl_bios_get_dsm(fwrt, DSM_FUNC_REGULATORY_CONFIG,
1360 				       &val);
1361 		/*
1362 		 * China 2022 enable if the BIOS object does not exist or
1363 		 * if it is enabled in BIOS.
1364 		 */
1365 		if (ret < 0 || val & DSM_MASK_CHINA_22_REG)
1366 			config_bitmap |=
1367 				cpu_to_le32(LARI_CONFIG_ENABLE_CHINA_22_REG_SUPPORT_MSK);
1368 	}
1369 
1370 	return config_bitmap;
1371 }
1372 
1373 static size_t iwl_mvm_get_lari_config_cmd_size(u8 cmd_ver)
1374 {
1375 	size_t cmd_size;
1376 
1377 	switch (cmd_ver) {
1378 	case 12:
1379 		cmd_size = offsetof(struct iwl_lari_config_change_cmd,
1380 				    oem_11bn_allow_bitmap);
1381 		break;
1382 	case 8:
1383 		cmd_size = sizeof(struct iwl_lari_config_change_cmd_v8);
1384 		break;
1385 	case 6:
1386 		cmd_size = sizeof(struct iwl_lari_config_change_cmd_v6);
1387 		break;
1388 	default:
1389 		cmd_size = sizeof(struct iwl_lari_config_change_cmd_v1);
1390 		break;
1391 	}
1392 	return cmd_size;
1393 }
1394 
1395 static int iwl_mvm_fill_lari_config(struct iwl_fw_runtime *fwrt,
1396 				    struct iwl_lari_config_change_cmd *cmd,
1397 				    size_t *cmd_size)
1398 {
1399 	int ret;
1400 	u32 value;
1401 	bool has_raw_dsm_capa = fw_has_capa(&fwrt->fw->ucode_capa,
1402 					    IWL_UCODE_TLV_CAPA_FW_ACCEPTS_RAW_DSM_TABLE);
1403 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(fwrt->fw,
1404 					   WIDE_ID(REGULATORY_AND_NVM_GROUP,
1405 						   LARI_CONFIG_CHANGE), 1);
1406 
1407 	memset(cmd, 0, sizeof(*cmd));
1408 	*cmd_size = iwl_mvm_get_lari_config_cmd_size(cmd_ver);
1409 
1410 	cmd->config_bitmap = iwl_mvm_get_lari_config_bitmap(fwrt);
1411 
1412 	ret = iwl_bios_get_dsm(fwrt, DSM_FUNC_11AX_ENABLEMENT, &value);
1413 	if (!ret) {
1414 		if (!has_raw_dsm_capa)
1415 			value &= DSM_11AX_ALLOW_BITMAP;
1416 		cmd->oem_11ax_allow_bitmap = cpu_to_le32(value);
1417 	}
1418 
1419 	ret = iwl_bios_get_dsm(fwrt, DSM_FUNC_ENABLE_UNII4_CHAN, &value);
1420 	if (!ret) {
1421 		if (!has_raw_dsm_capa)
1422 			value &= DSM_UNII4_ALLOW_BITMAP;
1423 
1424 		/* Since version 12, bits 4 and 5 are supported
1425 		 * regardless of this capability, By pass this masking
1426 		 * if firmware has capability of accepting raw DSM table.
1427 		 */
1428 		if (!has_raw_dsm_capa && cmd_ver < 12 &&
1429 		    !fw_has_capa(&fwrt->fw->ucode_capa,
1430 				 IWL_UCODE_TLV_CAPA_BIOS_OVERRIDE_5G9_FOR_CA))
1431 			value &= ~(DSM_VALUE_UNII4_CANADA_OVERRIDE_MSK |
1432 				   DSM_VALUE_UNII4_CANADA_EN_MSK);
1433 
1434 		cmd->oem_unii4_allow_bitmap = cpu_to_le32(value);
1435 	}
1436 
1437 	ret = iwl_bios_get_dsm(fwrt, DSM_FUNC_ACTIVATE_CHANNEL, &value);
1438 	if (!ret) {
1439 		if (!has_raw_dsm_capa)
1440 			value &= CHAN_STATE_ACTIVE_BITMAP_CMD_V12;
1441 
1442 		if (!has_raw_dsm_capa && cmd_ver < 8)
1443 			value &= ~ACTIVATE_5G2_IN_WW_MASK;
1444 
1445 		/* Since version 12, bits 5 and 6 are supported
1446 		 * regardless of this capability, By pass this masking
1447 		 * if firmware has capability of accepting raw DSM table.
1448 		 */
1449 		if (!has_raw_dsm_capa && cmd_ver < 12 &&
1450 		    !fw_has_capa(&fwrt->fw->ucode_capa,
1451 				 IWL_UCODE_TLV_CAPA_BIOS_OVERRIDE_UNII4_US_CA))
1452 			value &= CHAN_STATE_ACTIVE_BITMAP_CMD_V8;
1453 
1454 		cmd->chan_state_active_bitmap = cpu_to_le32(value);
1455 	}
1456 
1457 	ret = iwl_bios_get_dsm(fwrt, DSM_FUNC_ENABLE_6E, &value);
1458 	if (!ret)
1459 		cmd->oem_uhb_allow_bitmap = cpu_to_le32(value);
1460 
1461 	ret = iwl_bios_get_dsm(fwrt, DSM_FUNC_FORCE_DISABLE_CHANNELS, &value);
1462 	if (!ret) {
1463 		if (!has_raw_dsm_capa)
1464 			value &= DSM_FORCE_DISABLE_CHANNELS_ALLOWED_BITMAP;
1465 		cmd->force_disable_channels_bitmap = cpu_to_le32(value);
1466 	}
1467 
1468 	ret = iwl_bios_get_dsm(fwrt, DSM_FUNC_ENERGY_DETECTION_THRESHOLD,
1469 			       &value);
1470 	if (!ret) {
1471 		if (!has_raw_dsm_capa)
1472 			value &= DSM_EDT_ALLOWED_BITMAP;
1473 		cmd->edt_bitmap = cpu_to_le32(value);
1474 	}
1475 
1476 	ret = iwl_bios_get_wbem(fwrt, &value);
1477 	if (!ret)
1478 		cmd->oem_320mhz_allow_bitmap = cpu_to_le32(value);
1479 
1480 	ret = iwl_bios_get_dsm(fwrt, DSM_FUNC_ENABLE_11BE, &value);
1481 	if (!ret)
1482 		cmd->oem_11be_allow_bitmap = cpu_to_le32(value);
1483 
1484 	if (cmd->config_bitmap ||
1485 	    cmd->oem_uhb_allow_bitmap ||
1486 	    cmd->oem_11ax_allow_bitmap ||
1487 	    cmd->oem_unii4_allow_bitmap ||
1488 	    cmd->chan_state_active_bitmap ||
1489 	    cmd->force_disable_channels_bitmap ||
1490 	    cmd->edt_bitmap ||
1491 	    cmd->oem_320mhz_allow_bitmap ||
1492 	    cmd->oem_11be_allow_bitmap) {
1493 		IWL_DEBUG_RADIO(fwrt,
1494 				"sending LARI_CONFIG_CHANGE, config_bitmap=0x%x, oem_11ax_allow_bitmap=0x%x\n",
1495 				le32_to_cpu(cmd->config_bitmap),
1496 				le32_to_cpu(cmd->oem_11ax_allow_bitmap));
1497 		IWL_DEBUG_RADIO(fwrt,
1498 				"sending LARI_CONFIG_CHANGE, oem_unii4_allow_bitmap=0x%x, chan_state_active_bitmap=0x%x, cmd_ver=%d\n",
1499 				le32_to_cpu(cmd->oem_unii4_allow_bitmap),
1500 				le32_to_cpu(cmd->chan_state_active_bitmap),
1501 				cmd_ver);
1502 		IWL_DEBUG_RADIO(fwrt,
1503 				"sending LARI_CONFIG_CHANGE, oem_uhb_allow_bitmap=0x%x, force_disable_channels_bitmap=0x%x\n",
1504 				le32_to_cpu(cmd->oem_uhb_allow_bitmap),
1505 				le32_to_cpu(cmd->force_disable_channels_bitmap));
1506 		IWL_DEBUG_RADIO(fwrt,
1507 				"sending LARI_CONFIG_CHANGE, edt_bitmap=0x%x, oem_320mhz_allow_bitmap=0x%x\n",
1508 				le32_to_cpu(cmd->edt_bitmap),
1509 				le32_to_cpu(cmd->oem_320mhz_allow_bitmap));
1510 		IWL_DEBUG_RADIO(fwrt,
1511 				"sending LARI_CONFIG_CHANGE, oem_11be_allow_bitmap=0x%x\n",
1512 				le32_to_cpu(cmd->oem_11be_allow_bitmap));
1513 	} else {
1514 		return 1;
1515 	}
1516 
1517 	return 0;
1518 }
1519 
1520 static void iwl_mvm_send_lari_cfg_extension(struct iwl_mvm *mvm)
1521 {
1522 	struct iwl_fw_runtime *fwrt = &mvm->fwrt;
1523 	struct iwl_lari_config_extension_cmd cmd = {};
1524 	u32 cmd_id = WIDE_ID(REGULATORY_AND_NVM_GROUP,
1525 			     LARI_CONFIG_EXTENSION);
1526 	u32 value;
1527 	int ret;
1528 
1529 	if (iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id, 0) != 1)
1530 		return;
1531 
1532 	ret = iwl_bios_get_dsm(fwrt, DSM_FUNC_REGULATORY_CONFIG, &value);
1533 	if (ret)
1534 		return;
1535 
1536 	cmd.dsm_table_hdr.table_source = fwrt->dsm_source;
1537 	cmd.dsm_table_hdr.table_revision = fwrt->dsm_revision;
1538 	cmd.oem_uhb_allow_extension_bitmap = cpu_to_le32(value);
1539 
1540 	IWL_DEBUG_RADIO(mvm,
1541 			"sending LARI_CONFIG_EXTENSION, oem_uhb_allow_extension_bitmap=0x%x\n",
1542 			le32_to_cpu(cmd.oem_uhb_allow_extension_bitmap));
1543 	ret = iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, sizeof(cmd), &cmd);
1544 	if (ret < 0)
1545 		IWL_DEBUG_RADIO(mvm,
1546 				"Failed to send LARI_CONFIG_EXTENSION (%d)\n",
1547 				ret);
1548 }
1549 
1550 static void iwl_mvm_lari_cfg(struct iwl_mvm *mvm)
1551 {
1552 	struct iwl_lari_config_change_cmd cmd;
1553 	size_t cmd_size;
1554 	int ret;
1555 
1556 	if (mvm->fw->ucode_ver < 26)
1557 		return;
1558 
1559 	/*
1560 	 * LARI_CONFIG_CHANGE triggers a profile update, so send
1561 	 * LARI_CONFIG_EXTENSION first to make sure its data is applied
1562 	 * in the same update.
1563 	 */
1564 	iwl_mvm_send_lari_cfg_extension(mvm);
1565 	ret = iwl_mvm_fill_lari_config(&mvm->fwrt, &cmd, &cmd_size);
1566 	if (!ret) {
1567 		ret = iwl_mvm_send_cmd_pdu(mvm,
1568 					   WIDE_ID(REGULATORY_AND_NVM_GROUP,
1569 						   LARI_CONFIG_CHANGE),
1570 					   0, cmd_size, &cmd);
1571 		if (ret < 0)
1572 			IWL_DEBUG_RADIO(mvm,
1573 					"Failed to send LARI_CONFIG_CHANGE (%d)\n",
1574 					ret);
1575 	}
1576 }
1577 
1578 void iwl_mvm_get_bios_tables(struct iwl_mvm *mvm)
1579 {
1580 	int ret;
1581 
1582 	iwl_bios_get_guid_lock_status(&mvm->fwrt);
1583 
1584 	/* read PPAG table */
1585 	ret = iwl_bios_get_ppag_table(&mvm->fwrt);
1586 	if (ret < 0) {
1587 		IWL_DEBUG_RADIO(mvm,
1588 				"PPAG BIOS table invalid or unavailable. (%d)\n",
1589 				ret);
1590 	}
1591 
1592 	/* read SAR tables */
1593 	ret = iwl_bios_get_wrds_table(&mvm->fwrt);
1594 	if (ret < 0) {
1595 		IWL_DEBUG_RADIO(mvm,
1596 				"WRDS SAR BIOS table invalid or unavailable. (%d)\n",
1597 				ret);
1598 		/*
1599 		 * If not available, don't fail and don't bother with EWRD and
1600 		 * WGDS */
1601 
1602 		if (!iwl_bios_get_wgds_table(&mvm->fwrt)) {
1603 			/*
1604 			 * If basic SAR is not available, we check for WGDS,
1605 			 * which should *not* be available either.  If it is
1606 			 * available, issue an error, because we can't use SAR
1607 			 * Geo without basic SAR.
1608 			 */
1609 			IWL_ERR(mvm, "BIOS contains WGDS but no WRDS\n");
1610 		}
1611 
1612 	} else {
1613 		ret = iwl_bios_get_ewrd_table(&mvm->fwrt);
1614 		/* if EWRD is not available, we can still use
1615 		* WRDS, so don't fail */
1616 		if (ret < 0)
1617 			IWL_DEBUG_RADIO(mvm,
1618 					"EWRD SAR BIOS table invalid or unavailable. (%d)\n",
1619 					ret);
1620 
1621 		/* read geo SAR table */
1622 		if (iwl_sar_geo_support(&mvm->fwrt)) {
1623 			ret = iwl_bios_get_wgds_table(&mvm->fwrt);
1624 			if (ret < 0)
1625 				IWL_DEBUG_RADIO(mvm,
1626 						"Geo SAR BIOS table invalid or unavailable. (%d)\n",
1627 						ret);
1628 				/* we don't fail if the table is not available */
1629 		}
1630 	}
1631 
1632 	iwl_acpi_get_phy_filters(&mvm->fwrt);
1633 
1634 	if (iwl_bios_get_eckv(&mvm->fwrt, &mvm->ext_clock_valid))
1635 		IWL_DEBUG_RADIO(mvm, "ECKV table doesn't exist in BIOS\n");
1636 }
1637 
1638 static void iwl_mvm_disconnect_iterator(void *data, u8 *mac,
1639 					struct ieee80211_vif *vif)
1640 {
1641 	if (vif->type == NL80211_IFTYPE_STATION)
1642 		ieee80211_hw_restart_disconnect(vif);
1643 }
1644 
1645 void iwl_mvm_send_recovery_cmd(struct iwl_mvm *mvm, u32 flags)
1646 {
1647 	u32 error_log_size = mvm->fw->ucode_capa.error_log_size;
1648 	u32 status = 0;
1649 	int ret;
1650 
1651 	struct iwl_fw_error_recovery_cmd recovery_cmd = {
1652 		.flags = cpu_to_le32(flags),
1653 		.buf_size = 0,
1654 	};
1655 	struct iwl_host_cmd host_cmd = {
1656 		.id = WIDE_ID(SYSTEM_GROUP, FW_ERROR_RECOVERY_CMD),
1657 		.data = {&recovery_cmd, },
1658 		.len = {sizeof(recovery_cmd), },
1659 	};
1660 
1661 	/* no error log was defined in TLV */
1662 	if (!error_log_size)
1663 		return;
1664 
1665 	if (flags & ERROR_RECOVERY_UPDATE_DB) {
1666 		/* no buf was allocated while HW reset */
1667 		if (!mvm->error_recovery_buf)
1668 			return;
1669 
1670 		host_cmd.data[1] = mvm->error_recovery_buf;
1671 		host_cmd.len[1] =  error_log_size;
1672 		host_cmd.dataflags[1] = IWL_HCMD_DFL_NOCOPY;
1673 		recovery_cmd.buf_size = cpu_to_le32(error_log_size);
1674 	}
1675 
1676 	ret = iwl_mvm_send_cmd_status(mvm, &host_cmd, &status);
1677 	kfree(mvm->error_recovery_buf);
1678 	mvm->error_recovery_buf = NULL;
1679 
1680 	if (ret) {
1681 		IWL_ERR(mvm, "Failed to send recovery cmd %d\n", ret);
1682 		return;
1683 	}
1684 
1685 	/* skb respond is only relevant in ERROR_RECOVERY_UPDATE_DB */
1686 	if (flags & ERROR_RECOVERY_UPDATE_DB) {
1687 		if (status) {
1688 			IWL_ERR(mvm,
1689 				"Failed to send recovery cmd blob was invalid %d\n",
1690 				status);
1691 
1692 			ieee80211_iterate_interfaces(mvm->hw, 0,
1693 						     iwl_mvm_disconnect_iterator,
1694 						     mvm);
1695 		}
1696 	}
1697 }
1698 
1699 static int iwl_mvm_sar_init(struct iwl_mvm *mvm)
1700 {
1701 	return iwl_mvm_sar_select_profile(mvm, 1, 1);
1702 }
1703 
1704 static int iwl_mvm_load_rt_fw(struct iwl_mvm *mvm)
1705 {
1706 	int ret;
1707 
1708 	if (iwl_mvm_has_unified_ucode(mvm))
1709 		return iwl_run_unified_mvm_ucode(mvm);
1710 
1711 	ret = iwl_run_init_mvm_ucode(mvm);
1712 
1713 	if (ret) {
1714 		IWL_ERR(mvm, "Failed to run INIT ucode: %d\n", ret);
1715 		return ret;
1716 	}
1717 
1718 	iwl_fw_dbg_stop_sync(&mvm->fwrt);
1719 	iwl_trans_stop_device(mvm->trans);
1720 	ret = iwl_trans_start_hw(mvm->trans);
1721 	if (ret)
1722 		return ret;
1723 
1724 	mvm->rfkill_safe_init_done = false;
1725 	ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_REGULAR);
1726 	if (ret)
1727 		return ret;
1728 
1729 	mvm->rfkill_safe_init_done = true;
1730 
1731 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_AFTER_ALIVE,
1732 			       NULL);
1733 
1734 	return iwl_init_paging(&mvm->fwrt, mvm->fwrt.cur_fw_img);
1735 }
1736 
1737 int iwl_mvm_up(struct iwl_mvm *mvm)
1738 {
1739 	int ret, i;
1740 	struct ieee80211_supported_band *sband = NULL;
1741 
1742 	lockdep_assert_wiphy(mvm->hw->wiphy);
1743 	lockdep_assert_held(&mvm->mutex);
1744 
1745 	ret = iwl_trans_start_hw(mvm->trans);
1746 	if (ret)
1747 		return ret;
1748 
1749 	ret = iwl_mvm_load_rt_fw(mvm);
1750 	if (ret) {
1751 		IWL_ERR(mvm, "Failed to start RT ucode: %d\n", ret);
1752 		if (ret != -ERFKILL && !mvm->fw_product_reset)
1753 			iwl_fw_dbg_error_collect(&mvm->fwrt,
1754 						 FW_DBG_TRIGGER_DRIVER);
1755 		goto error;
1756 	}
1757 
1758 	/* FW loaded successfully */
1759 	mvm->fw_product_reset = false;
1760 
1761 	iwl_fw_disable_dbg_asserts(&mvm->fwrt);
1762 	iwl_get_shared_mem_conf(&mvm->fwrt);
1763 
1764 	ret = iwl_mvm_sf_update(mvm, NULL, false);
1765 	if (ret)
1766 		IWL_ERR(mvm, "Failed to initialize Smart Fifo\n");
1767 
1768 	if (!iwl_trans_dbg_ini_valid(mvm->trans)) {
1769 		mvm->fwrt.dump.conf = FW_DBG_INVALID;
1770 		/* if we have a destination, assume EARLY START */
1771 		if (mvm->fw->dbg.dest_tlv)
1772 			mvm->fwrt.dump.conf = FW_DBG_START_FROM_ALIVE;
1773 		iwl_fw_start_dbg_conf(&mvm->fwrt, FW_DBG_START_FROM_ALIVE);
1774 	}
1775 
1776 	ret = iwl_send_tx_ant_cfg(mvm, iwl_mvm_get_valid_tx_ant(mvm));
1777 	if (ret)
1778 		goto error;
1779 
1780 	if (!iwl_mvm_has_unified_ucode(mvm)) {
1781 		/* Send phy db control command and then phy db calibration */
1782 		ret = iwl_send_phy_db_data(mvm->phy_db);
1783 		if (ret)
1784 			goto error;
1785 		ret = iwl_send_phy_cfg_cmd(mvm);
1786 		if (ret)
1787 			goto error;
1788 	}
1789 
1790 	ret = iwl_mvm_send_bt_init_conf(mvm);
1791 	if (ret)
1792 		goto error;
1793 
1794 	if (fw_has_capa(&mvm->fw->ucode_capa,
1795 			IWL_UCODE_TLV_CAPA_SOC_LATENCY_SUPPORT)) {
1796 		ret = iwl_set_soc_latency(&mvm->fwrt);
1797 		if (ret)
1798 			goto error;
1799 	}
1800 
1801 	iwl_mvm_lari_cfg(mvm);
1802 
1803 	/* Init RSS configuration */
1804 	ret = iwl_configure_rxq(&mvm->fwrt);
1805 	if (ret)
1806 		goto error;
1807 
1808 	if (iwl_mvm_has_new_rx_api(mvm)) {
1809 		ret = iwl_send_rss_cfg_cmd(mvm);
1810 		if (ret) {
1811 			IWL_ERR(mvm, "Failed to configure RSS queues: %d\n",
1812 				ret);
1813 			goto error;
1814 		}
1815 	}
1816 
1817 	/* init the fw <-> mac80211 STA mapping */
1818 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
1819 		RCU_INIT_POINTER(mvm->fw_id_to_mac_id[i], NULL);
1820 		RCU_INIT_POINTER(mvm->fw_id_to_link_sta[i], NULL);
1821 	}
1822 
1823 	mvm->tdls_cs.peer.sta_id = IWL_INVALID_STA;
1824 
1825 	/* reset quota debouncing buffer - 0xff will yield invalid data */
1826 	memset(&mvm->last_quota_cmd, 0xff, sizeof(mvm->last_quota_cmd));
1827 
1828 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_DQA_SUPPORT)) {
1829 		ret = iwl_mvm_send_dqa_cmd(mvm);
1830 		if (ret)
1831 			goto error;
1832 	}
1833 
1834 	/*
1835 	 * Add auxiliary station for scanning.
1836 	 * Newer versions of this command implies that the fw uses
1837 	 * internal aux station for all aux activities that don't
1838 	 * requires a dedicated data queue.
1839 	 */
1840 	if (!iwl_mvm_has_new_station_api(mvm->fw)) {
1841 		 /*
1842 		  * In old version the aux station uses mac id like other
1843 		  * station and not lmac id
1844 		  */
1845 		ret = iwl_mvm_add_aux_sta(mvm, MAC_INDEX_AUX);
1846 		if (ret)
1847 			goto error;
1848 	}
1849 
1850 	/* Add all the PHY contexts */
1851 	i = 0;
1852 	while (!sband && i < NUM_NL80211_BANDS)
1853 		sband = mvm->hw->wiphy->bands[i++];
1854 
1855 	if (WARN_ON_ONCE(!sband)) {
1856 		ret = -ENODEV;
1857 		goto error;
1858 	}
1859 
1860 	if (iwl_mvm_is_tt_in_fw(mvm)) {
1861 		/* in order to give the responsibility of ct-kill and
1862 		 * TX backoff to FW we need to send empty temperature reporting
1863 		 * cmd during init time
1864 		 */
1865 		iwl_mvm_send_temp_report_ths_cmd(mvm);
1866 	} else {
1867 		/* Initialize tx backoffs to the minimal possible */
1868 		iwl_mvm_tt_tx_backoff(mvm, 0);
1869 	}
1870 
1871 #ifdef CONFIG_THERMAL
1872 	/* TODO: read the budget from BIOS / Platform NVM */
1873 
1874 	/*
1875 	 * In case there is no budget from BIOS / Platform NVM the default
1876 	 * budget should be 2000mW (cooling state 0).
1877 	 */
1878 	if (iwl_mvm_is_ctdp_supported(mvm)) {
1879 		ret = iwl_mvm_ctdp_command(mvm, CTDP_CMD_OPERATION_START,
1880 					   mvm->cooling_dev.cur_state);
1881 		if (ret)
1882 			goto error;
1883 	}
1884 #endif
1885 
1886 	if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_SET_LTR_GEN2))
1887 		WARN_ON(iwl_mvm_config_ltr(mvm));
1888 
1889 	ret = iwl_mvm_power_update_device(mvm);
1890 	if (ret)
1891 		goto error;
1892 
1893 	/*
1894 	 * RTNL is not taken during Ct-kill, but we don't need to scan/Tx
1895 	 * anyway, so don't init MCC.
1896 	 */
1897 	if (!test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status)) {
1898 		ret = iwl_mvm_init_mcc(mvm);
1899 		if (ret)
1900 			goto error;
1901 	}
1902 
1903 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1904 		mvm->scan_type = IWL_SCAN_TYPE_NOT_SET;
1905 		mvm->hb_scan_type = IWL_SCAN_TYPE_NOT_SET;
1906 		ret = iwl_mvm_config_scan(mvm);
1907 		if (ret)
1908 			goto error;
1909 	}
1910 
1911 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1912 		iwl_mvm_send_recovery_cmd(mvm, ERROR_RECOVERY_UPDATE_DB);
1913 
1914 		if (mvm->time_sync.active)
1915 			iwl_mvm_time_sync_config(mvm, mvm->time_sync.peer_addr,
1916 						 IWL_TIME_SYNC_PROTOCOL_TM |
1917 						 IWL_TIME_SYNC_PROTOCOL_FTM);
1918 	}
1919 
1920 	if (!mvm->ptp_data.ptp_clock)
1921 		iwl_mvm_ptp_init(mvm);
1922 
1923 	ret = iwl_mvm_ppag_init(mvm);
1924 	if (ret)
1925 		goto error;
1926 
1927 	ret = iwl_mvm_sar_init(mvm);
1928 	if (ret == 0)
1929 		ret = iwl_mvm_sar_geo_init(mvm);
1930 	if (ret < 0)
1931 		goto error;
1932 
1933 	ret = iwl_mvm_sgom_init(mvm);
1934 	if (ret)
1935 		goto error;
1936 
1937 	iwl_mvm_tas_init(mvm);
1938 	iwl_mvm_leds_sync(mvm);
1939 	iwl_mvm_uats_init(mvm);
1940 
1941 	if (iwl_rfi_supported(mvm)) {
1942 		if (iwl_rfi_is_enabled_in_bios(&mvm->fwrt))
1943 			iwl_rfi_send_config_cmd(mvm, NULL);
1944 	}
1945 
1946 	iwl_mvm_mei_device_state(mvm, true);
1947 
1948 	IWL_DEBUG_INFO(mvm, "RT uCode started.\n");
1949 	return 0;
1950  error:
1951 	iwl_mvm_stop_device(mvm);
1952 	return ret;
1953 }
1954 
1955 int iwl_mvm_load_d3_fw(struct iwl_mvm *mvm)
1956 {
1957 	int ret, i;
1958 
1959 	lockdep_assert_wiphy(mvm->hw->wiphy);
1960 	lockdep_assert_held(&mvm->mutex);
1961 
1962 	ret = iwl_trans_start_hw(mvm->trans);
1963 	if (ret)
1964 		return ret;
1965 
1966 	ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_WOWLAN);
1967 	if (ret) {
1968 		IWL_ERR(mvm, "Failed to start WoWLAN firmware: %d\n", ret);
1969 		goto error;
1970 	}
1971 
1972 	ret = iwl_send_tx_ant_cfg(mvm, iwl_mvm_get_valid_tx_ant(mvm));
1973 	if (ret)
1974 		goto error;
1975 
1976 	/* Send phy db control command and then phy db calibration*/
1977 	ret = iwl_send_phy_db_data(mvm->phy_db);
1978 	if (ret)
1979 		goto error;
1980 
1981 	ret = iwl_send_phy_cfg_cmd(mvm);
1982 	if (ret)
1983 		goto error;
1984 
1985 	/* init the fw <-> mac80211 STA mapping */
1986 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
1987 		RCU_INIT_POINTER(mvm->fw_id_to_mac_id[i], NULL);
1988 		RCU_INIT_POINTER(mvm->fw_id_to_link_sta[i], NULL);
1989 	}
1990 
1991 	if (!iwl_mvm_has_new_station_api(mvm->fw)) {
1992 		/*
1993 		 * Add auxiliary station for scanning.
1994 		 * Newer versions of this command implies that the fw uses
1995 		 * internal aux station for all aux activities that don't
1996 		 * requires a dedicated data queue.
1997 		 * In old version the aux station uses mac id like other
1998 		 * station and not lmac id
1999 		 */
2000 		ret = iwl_mvm_add_aux_sta(mvm, MAC_INDEX_AUX);
2001 		if (ret)
2002 			goto error;
2003 	}
2004 
2005 	return 0;
2006  error:
2007 	iwl_mvm_stop_device(mvm);
2008 	return ret;
2009 }
2010 
2011 void iwl_mvm_rx_mfuart_notif(struct iwl_mvm *mvm,
2012 			     struct iwl_rx_cmd_buffer *rxb)
2013 {
2014 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2015 	struct iwl_mfuart_load_notif *mfuart_notif = (void *)pkt->data;
2016 
2017 	IWL_DEBUG_INFO(mvm,
2018 		       "MFUART: installed ver: 0x%08x, external ver: 0x%08x, status: 0x%08x, duration: 0x%08x\n",
2019 		       le32_to_cpu(mfuart_notif->installed_ver),
2020 		       le32_to_cpu(mfuart_notif->external_ver),
2021 		       le32_to_cpu(mfuart_notif->status),
2022 		       le32_to_cpu(mfuart_notif->duration));
2023 
2024 	if (iwl_rx_packet_payload_len(pkt) == sizeof(*mfuart_notif))
2025 		IWL_DEBUG_INFO(mvm,
2026 			       "MFUART: image size: 0x%08x\n",
2027 			       le32_to_cpu(mfuart_notif->image_size));
2028 }
2029