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