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