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