1e705c121SKalle Valo /******************************************************************************
2e705c121SKalle Valo  *
3e705c121SKalle Valo  * This file is provided under a dual BSD/GPLv2 license.  When using or
4e705c121SKalle Valo  * redistributing this file, you may do so under either license.
5e705c121SKalle Valo  *
6e705c121SKalle Valo  * GPL LICENSE SUMMARY
7e705c121SKalle Valo  *
8e705c121SKalle Valo  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9e705c121SKalle Valo  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
10bdccdb85SGolan Ben-Ami  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
1148e775e6SHaim Dreyfuss  * Copyright(c) 2018 - 2019        Intel Corporation
12e705c121SKalle Valo  *
13e705c121SKalle Valo  * This program is free software; you can redistribute it and/or modify
14e705c121SKalle Valo  * it under the terms of version 2 of the GNU General Public License as
15e705c121SKalle Valo  * published by the Free Software Foundation.
16e705c121SKalle Valo  *
17e705c121SKalle Valo  * This program is distributed in the hope that it will be useful, but
18e705c121SKalle Valo  * WITHOUT ANY WARRANTY; without even the implied warranty of
19e705c121SKalle Valo  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20e705c121SKalle Valo  * General Public License for more details.
21e705c121SKalle Valo  *
22e705c121SKalle Valo  * The full GNU General Public License is included in this distribution
23e705c121SKalle Valo  * in the file called COPYING.
24e705c121SKalle Valo  *
25e705c121SKalle Valo  * Contact Information:
26cb2f8277SEmmanuel Grumbach  *  Intel Linux Wireless <linuxwifi@intel.com>
27e705c121SKalle Valo  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28e705c121SKalle Valo  *
29e705c121SKalle Valo  * BSD LICENSE
30e705c121SKalle Valo  *
31e705c121SKalle Valo  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
32e705c121SKalle Valo  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
33bdccdb85SGolan Ben-Ami  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
3448e775e6SHaim Dreyfuss  * Copyright(c) 2018 - 2019       Intel Corporation
35e705c121SKalle Valo  * All rights reserved.
36e705c121SKalle Valo  *
37e705c121SKalle Valo  * Redistribution and use in source and binary forms, with or without
38e705c121SKalle Valo  * modification, are permitted provided that the following conditions
39e705c121SKalle Valo  * are met:
40e705c121SKalle Valo  *
41e705c121SKalle Valo  *  * Redistributions of source code must retain the above copyright
42e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer.
43e705c121SKalle Valo  *  * Redistributions in binary form must reproduce the above copyright
44e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer in
45e705c121SKalle Valo  *    the documentation and/or other materials provided with the
46e705c121SKalle Valo  *    distribution.
47e705c121SKalle Valo  *  * Neither the name Intel Corporation nor the names of its
48e705c121SKalle Valo  *    contributors may be used to endorse or promote products derived
49e705c121SKalle Valo  *    from this software without specific prior written permission.
50e705c121SKalle Valo  *
51e705c121SKalle Valo  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52e705c121SKalle Valo  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53e705c121SKalle Valo  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54e705c121SKalle Valo  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55e705c121SKalle Valo  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56e705c121SKalle Valo  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
57e705c121SKalle Valo  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58e705c121SKalle Valo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59e705c121SKalle Valo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60e705c121SKalle Valo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61e705c121SKalle Valo  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62e705c121SKalle Valo  *
63e705c121SKalle Valo  *****************************************************************************/
64e705c121SKalle Valo #include <net/mac80211.h>
65854d773eSSara Sharon #include <linux/netdevice.h>
66e705c121SKalle Valo 
67e705c121SKalle Valo #include "iwl-trans.h"
68e705c121SKalle Valo #include "iwl-op-mode.h"
69d962f9b1SJohannes Berg #include "fw/img.h"
70e705c121SKalle Valo #include "iwl-debug.h"
71e705c121SKalle Valo #include "iwl-csr.h" /* for iwl_mvm_rx_card_state_notif */
72e705c121SKalle Valo #include "iwl-io.h" /* for iwl_mvm_rx_card_state_notif */
73e705c121SKalle Valo #include "iwl-prph.h"
74813df5ceSLuca Coelho #include "fw/acpi.h"
75e705c121SKalle Valo 
76e705c121SKalle Valo #include "mvm.h"
777174beb6SJohannes Berg #include "fw/dbg.h"
78e705c121SKalle Valo #include "iwl-phy-db.h"
799c4f7d51SShaul Triebitz #include "iwl-modparams.h"
809c4f7d51SShaul Triebitz #include "iwl-nvm-parse.h"
81e705c121SKalle Valo 
82e705c121SKalle Valo #define MVM_UCODE_ALIVE_TIMEOUT	HZ
83e705c121SKalle Valo #define MVM_UCODE_CALIB_TIMEOUT	(2*HZ)
84e705c121SKalle Valo 
85e705c121SKalle Valo #define UCODE_VALID_OK	cpu_to_le32(0x1)
86e705c121SKalle Valo 
87e705c121SKalle Valo struct iwl_mvm_alive_data {
88e705c121SKalle Valo 	bool valid;
89e705c121SKalle Valo 	u32 scd_base_addr;
90e705c121SKalle Valo };
91e705c121SKalle Valo 
92e705c121SKalle Valo static int iwl_send_tx_ant_cfg(struct iwl_mvm *mvm, u8 valid_tx_ant)
93e705c121SKalle Valo {
94e705c121SKalle Valo 	struct iwl_tx_ant_cfg_cmd tx_ant_cmd = {
95e705c121SKalle Valo 		.valid = cpu_to_le32(valid_tx_ant),
96e705c121SKalle Valo 	};
97e705c121SKalle Valo 
98e705c121SKalle Valo 	IWL_DEBUG_FW(mvm, "select valid tx ant: %u\n", valid_tx_ant);
99e705c121SKalle Valo 	return iwl_mvm_send_cmd_pdu(mvm, TX_ANT_CONFIGURATION_CMD, 0,
100e705c121SKalle Valo 				    sizeof(tx_ant_cmd), &tx_ant_cmd);
101e705c121SKalle Valo }
102e705c121SKalle Valo 
10343413a97SSara Sharon static int iwl_send_rss_cfg_cmd(struct iwl_mvm *mvm)
10443413a97SSara Sharon {
10543413a97SSara Sharon 	int i;
10643413a97SSara Sharon 	struct iwl_rss_config_cmd cmd = {
10743413a97SSara Sharon 		.flags = cpu_to_le32(IWL_RSS_ENABLE),
108608dce95SSara Sharon 		.hash_mask = BIT(IWL_RSS_HASH_TYPE_IPV4_TCP) |
109608dce95SSara Sharon 			     BIT(IWL_RSS_HASH_TYPE_IPV4_UDP) |
110608dce95SSara Sharon 			     BIT(IWL_RSS_HASH_TYPE_IPV4_PAYLOAD) |
111608dce95SSara Sharon 			     BIT(IWL_RSS_HASH_TYPE_IPV6_TCP) |
112608dce95SSara Sharon 			     BIT(IWL_RSS_HASH_TYPE_IPV6_UDP) |
113608dce95SSara Sharon 			     BIT(IWL_RSS_HASH_TYPE_IPV6_PAYLOAD),
11443413a97SSara Sharon 	};
11543413a97SSara Sharon 
116f43495fdSSara Sharon 	if (mvm->trans->num_rx_queues == 1)
117f43495fdSSara Sharon 		return 0;
118f43495fdSSara Sharon 
119854d773eSSara Sharon 	/* Do not direct RSS traffic to Q 0 which is our fallback queue */
12043413a97SSara Sharon 	for (i = 0; i < ARRAY_SIZE(cmd.indirection_table); i++)
121854d773eSSara Sharon 		cmd.indirection_table[i] =
122854d773eSSara Sharon 			1 + (i % (mvm->trans->num_rx_queues - 1));
123854d773eSSara Sharon 	netdev_rss_key_fill(cmd.secret_key, sizeof(cmd.secret_key));
12443413a97SSara Sharon 
12543413a97SSara Sharon 	return iwl_mvm_send_cmd_pdu(mvm, RSS_CONFIG_CMD, 0, sizeof(cmd), &cmd);
12643413a97SSara Sharon }
12743413a97SSara Sharon 
1288edbfaa1SSara Sharon static int iwl_configure_rxq(struct iwl_mvm *mvm)
1298edbfaa1SSara Sharon {
130dbf592f3SJohannes Berg 	int i, num_queues, size, ret;
1318edbfaa1SSara Sharon 	struct iwl_rfh_queue_config *cmd;
132dbf592f3SJohannes Berg 	struct iwl_host_cmd hcmd = {
133dbf592f3SJohannes Berg 		.id = WIDE_ID(DATA_PATH_GROUP, RFH_QUEUE_CONFIG_CMD),
134dbf592f3SJohannes Berg 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
135dbf592f3SJohannes Berg 	};
1368edbfaa1SSara Sharon 
1378edbfaa1SSara Sharon 	/* Do not configure default queue, it is configured via context info */
1388edbfaa1SSara Sharon 	num_queues = mvm->trans->num_rx_queues - 1;
1398edbfaa1SSara Sharon 
140dbf592f3SJohannes Berg 	size = struct_size(cmd, data, num_queues);
1418edbfaa1SSara Sharon 
1428edbfaa1SSara Sharon 	cmd = kzalloc(size, GFP_KERNEL);
1438edbfaa1SSara Sharon 	if (!cmd)
1448edbfaa1SSara Sharon 		return -ENOMEM;
1458edbfaa1SSara Sharon 
1468edbfaa1SSara Sharon 	cmd->num_queues = num_queues;
1478edbfaa1SSara Sharon 
1488edbfaa1SSara Sharon 	for (i = 0; i < num_queues; i++) {
1498edbfaa1SSara Sharon 		struct iwl_trans_rxq_dma_data data;
1508edbfaa1SSara Sharon 
1518edbfaa1SSara Sharon 		cmd->data[i].q_num = i + 1;
1528edbfaa1SSara Sharon 		iwl_trans_get_rxq_dma_data(mvm->trans, i + 1, &data);
1538edbfaa1SSara Sharon 
1548edbfaa1SSara Sharon 		cmd->data[i].fr_bd_cb = cpu_to_le64(data.fr_bd_cb);
1558edbfaa1SSara Sharon 		cmd->data[i].urbd_stts_wrptr =
1568edbfaa1SSara Sharon 			cpu_to_le64(data.urbd_stts_wrptr);
1578edbfaa1SSara Sharon 		cmd->data[i].ur_bd_cb = cpu_to_le64(data.ur_bd_cb);
1588edbfaa1SSara Sharon 		cmd->data[i].fr_bd_wid = cpu_to_le32(data.fr_bd_wid);
1598edbfaa1SSara Sharon 	}
1608edbfaa1SSara Sharon 
161dbf592f3SJohannes Berg 	hcmd.data[0] = cmd;
162dbf592f3SJohannes Berg 	hcmd.len[0] = size;
163dbf592f3SJohannes Berg 
164dbf592f3SJohannes Berg 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
165dbf592f3SJohannes Berg 
166dbf592f3SJohannes Berg 	kfree(cmd);
167dbf592f3SJohannes Berg 
168dbf592f3SJohannes Berg 	return ret;
1698edbfaa1SSara Sharon }
1708edbfaa1SSara Sharon 
17197d5be7eSLiad Kaufman static int iwl_mvm_send_dqa_cmd(struct iwl_mvm *mvm)
17297d5be7eSLiad Kaufman {
17397d5be7eSLiad Kaufman 	struct iwl_dqa_enable_cmd dqa_cmd = {
17497d5be7eSLiad Kaufman 		.cmd_queue = cpu_to_le32(IWL_MVM_DQA_CMD_QUEUE),
17597d5be7eSLiad Kaufman 	};
17697d5be7eSLiad Kaufman 	u32 cmd_id = iwl_cmd_id(DQA_ENABLE_CMD, DATA_PATH_GROUP, 0);
17797d5be7eSLiad Kaufman 	int ret;
17897d5be7eSLiad Kaufman 
17997d5be7eSLiad Kaufman 	ret = iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, sizeof(dqa_cmd), &dqa_cmd);
18097d5be7eSLiad Kaufman 	if (ret)
18197d5be7eSLiad Kaufman 		IWL_ERR(mvm, "Failed to send DQA enabling command: %d\n", ret);
18297d5be7eSLiad Kaufman 	else
18397d5be7eSLiad Kaufman 		IWL_DEBUG_FW(mvm, "Working in DQA mode\n");
18497d5be7eSLiad Kaufman 
18597d5be7eSLiad Kaufman 	return ret;
18697d5be7eSLiad Kaufman }
18797d5be7eSLiad Kaufman 
188bdccdb85SGolan Ben-Ami void iwl_mvm_mfu_assert_dump_notif(struct iwl_mvm *mvm,
189bdccdb85SGolan Ben-Ami 				   struct iwl_rx_cmd_buffer *rxb)
190bdccdb85SGolan Ben-Ami {
191bdccdb85SGolan Ben-Ami 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
192bdccdb85SGolan Ben-Ami 	struct iwl_mfu_assert_dump_notif *mfu_dump_notif = (void *)pkt->data;
193bdccdb85SGolan Ben-Ami 	__le32 *dump_data = mfu_dump_notif->data;
194bdccdb85SGolan Ben-Ami 	int n_words = le32_to_cpu(mfu_dump_notif->data_size) / sizeof(__le32);
195bdccdb85SGolan Ben-Ami 	int i;
196bdccdb85SGolan Ben-Ami 
197bdccdb85SGolan Ben-Ami 	if (mfu_dump_notif->index_num == 0)
198bdccdb85SGolan Ben-Ami 		IWL_INFO(mvm, "MFUART assert id 0x%x occurred\n",
199bdccdb85SGolan Ben-Ami 			 le32_to_cpu(mfu_dump_notif->assert_id));
200bdccdb85SGolan Ben-Ami 
201bdccdb85SGolan Ben-Ami 	for (i = 0; i < n_words; i++)
202bdccdb85SGolan Ben-Ami 		IWL_DEBUG_INFO(mvm,
203bdccdb85SGolan Ben-Ami 			       "MFUART assert dump, dword %u: 0x%08x\n",
204bdccdb85SGolan Ben-Ami 			       le16_to_cpu(mfu_dump_notif->index_num) *
205bdccdb85SGolan Ben-Ami 			       n_words + i,
206bdccdb85SGolan Ben-Ami 			       le32_to_cpu(dump_data[i]));
207bdccdb85SGolan Ben-Ami }
208bdccdb85SGolan Ben-Ami 
209e705c121SKalle Valo static bool iwl_alive_fn(struct iwl_notif_wait_data *notif_wait,
210e705c121SKalle Valo 			 struct iwl_rx_packet *pkt, void *data)
211e705c121SKalle Valo {
212e705c121SKalle Valo 	struct iwl_mvm *mvm =
213e705c121SKalle Valo 		container_of(notif_wait, struct iwl_mvm, notif_wait);
214e705c121SKalle Valo 	struct iwl_mvm_alive_data *alive_data = data;
2155c228d63SSara Sharon 	struct mvm_alive_resp_v3 *palive3;
216e705c121SKalle Valo 	struct mvm_alive_resp *palive;
2175c228d63SSara Sharon 	struct iwl_umac_alive *umac;
2185c228d63SSara Sharon 	struct iwl_lmac_alive *lmac1;
2195c228d63SSara Sharon 	struct iwl_lmac_alive *lmac2 = NULL;
2205c228d63SSara Sharon 	u16 status;
22122463857SShahar S Matityahu 	u32 lmac_error_event_table, umac_error_event_table;
222e705c121SKalle Valo 
2235c228d63SSara Sharon 	if (iwl_rx_packet_payload_len(pkt) == sizeof(*palive)) {
224e705c121SKalle Valo 		palive = (void *)pkt->data;
2255c228d63SSara Sharon 		umac = &palive->umac_data;
2265c228d63SSara Sharon 		lmac1 = &palive->lmac_data[0];
2275c228d63SSara Sharon 		lmac2 = &palive->lmac_data[1];
2285c228d63SSara Sharon 		status = le16_to_cpu(palive->status);
2295c228d63SSara Sharon 	} else {
2305c228d63SSara Sharon 		palive3 = (void *)pkt->data;
2315c228d63SSara Sharon 		umac = &palive3->umac_data;
2325c228d63SSara Sharon 		lmac1 = &palive3->lmac_data;
2335c228d63SSara Sharon 		status = le16_to_cpu(palive3->status);
2345c228d63SSara Sharon 	}
235e705c121SKalle Valo 
23622463857SShahar S Matityahu 	lmac_error_event_table =
23722463857SShahar S Matityahu 		le32_to_cpu(lmac1->dbg_ptrs.error_event_table_ptr);
23822463857SShahar S Matityahu 	iwl_fw_lmac1_set_alive_err_table(mvm->trans, lmac_error_event_table);
239e705c121SKalle Valo 
24022463857SShahar S Matityahu 	if (lmac2)
24122463857SShahar S Matityahu 		mvm->trans->lmac_error_event_table[1] =
24222463857SShahar S Matityahu 			le32_to_cpu(lmac2->dbg_ptrs.error_event_table_ptr);
24322463857SShahar S Matityahu 
24422463857SShahar S Matityahu 	umac_error_event_table = le32_to_cpu(umac->dbg_ptrs.error_info_addr);
2455c228d63SSara Sharon 
2463485e76eSLuca Coelho 	if (!umac_error_event_table) {
2473485e76eSLuca Coelho 		mvm->support_umac_log = false;
2483485e76eSLuca Coelho 	} else if (umac_error_event_table >=
2493485e76eSLuca Coelho 		   mvm->trans->cfg->min_umac_error_event_table) {
2503485e76eSLuca Coelho 		mvm->support_umac_log = true;
2513485e76eSLuca Coelho 	} else {
252fb5b2846SLuca Coelho 		IWL_ERR(mvm,
253fb5b2846SLuca Coelho 			"Not valid error log pointer 0x%08X for %s uCode\n",
25422463857SShahar S Matityahu 			umac_error_event_table,
255fb5b2846SLuca Coelho 			(mvm->fwrt.cur_fw_img == IWL_UCODE_INIT) ?
256fb5b2846SLuca Coelho 			"Init" : "RT");
2573485e76eSLuca Coelho 		mvm->support_umac_log = false;
2583485e76eSLuca Coelho 	}
259fb5b2846SLuca Coelho 
26022463857SShahar S Matityahu 	if (mvm->support_umac_log)
26122463857SShahar S Matityahu 		iwl_fw_umac_set_alive_err_table(mvm->trans,
26222463857SShahar S Matityahu 						umac_error_event_table);
26322463857SShahar S Matityahu 
26422463857SShahar S Matityahu 	alive_data->scd_base_addr = le32_to_cpu(lmac1->dbg_ptrs.scd_base_ptr);
2655c228d63SSara Sharon 	alive_data->valid = status == IWL_ALIVE_STATUS_OK;
266e705c121SKalle Valo 
267e705c121SKalle Valo 	IWL_DEBUG_FW(mvm,
2685c228d63SSara Sharon 		     "Alive ucode status 0x%04x revision 0x%01X 0x%01X\n",
2695c228d63SSara Sharon 		     status, lmac1->ver_type, lmac1->ver_subtype);
2705c228d63SSara Sharon 
2715c228d63SSara Sharon 	if (lmac2)
2725c228d63SSara Sharon 		IWL_DEBUG_FW(mvm, "Alive ucode CDB\n");
273e705c121SKalle Valo 
274e705c121SKalle Valo 	IWL_DEBUG_FW(mvm,
275e705c121SKalle Valo 		     "UMAC version: Major - 0x%x, Minor - 0x%x\n",
2765c228d63SSara Sharon 		     le32_to_cpu(umac->umac_major),
2775c228d63SSara Sharon 		     le32_to_cpu(umac->umac_minor));
278e705c121SKalle Valo 
2790a3a3e9eSShahar S Matityahu 	iwl_fwrt_update_fw_versions(&mvm->fwrt, lmac1, umac);
2800a3a3e9eSShahar S Matityahu 
281e705c121SKalle Valo 	return true;
282e705c121SKalle Valo }
283e705c121SKalle Valo 
2841f370650SSara Sharon static bool iwl_wait_init_complete(struct iwl_notif_wait_data *notif_wait,
2851f370650SSara Sharon 				   struct iwl_rx_packet *pkt, void *data)
2861f370650SSara Sharon {
2871f370650SSara Sharon 	WARN_ON(pkt->hdr.cmd != INIT_COMPLETE_NOTIF);
2881f370650SSara Sharon 
2891f370650SSara Sharon 	return true;
2901f370650SSara Sharon }
2911f370650SSara Sharon 
292e705c121SKalle Valo static bool iwl_wait_phy_db_entry(struct iwl_notif_wait_data *notif_wait,
293e705c121SKalle Valo 				  struct iwl_rx_packet *pkt, void *data)
294e705c121SKalle Valo {
295e705c121SKalle Valo 	struct iwl_phy_db *phy_db = data;
296e705c121SKalle Valo 
297e705c121SKalle Valo 	if (pkt->hdr.cmd != CALIB_RES_NOTIF_PHY_DB) {
298e705c121SKalle Valo 		WARN_ON(pkt->hdr.cmd != INIT_COMPLETE_NOTIF);
299e705c121SKalle Valo 		return true;
300e705c121SKalle Valo 	}
301e705c121SKalle Valo 
302ce1f2778SSara Sharon 	WARN_ON(iwl_phy_db_set_section(phy_db, pkt));
303e705c121SKalle Valo 
304e705c121SKalle Valo 	return false;
305e705c121SKalle Valo }
306e705c121SKalle Valo 
307e705c121SKalle Valo static int iwl_mvm_load_ucode_wait_alive(struct iwl_mvm *mvm,
308e705c121SKalle Valo 					 enum iwl_ucode_type ucode_type)
309e705c121SKalle Valo {
310e705c121SKalle Valo 	struct iwl_notification_wait alive_wait;
31194a8d87cSLuca Coelho 	struct iwl_mvm_alive_data alive_data = {};
312e705c121SKalle Valo 	const struct fw_img *fw;
313cfbc6c4cSSara Sharon 	int ret;
314702e975dSJohannes Berg 	enum iwl_ucode_type old_type = mvm->fwrt.cur_fw_img;
315e705c121SKalle Valo 	static const u16 alive_cmd[] = { MVM_ALIVE };
316b3500b47SEmmanuel Grumbach 	bool run_in_rfkill =
317b3500b47SEmmanuel Grumbach 		ucode_type == IWL_UCODE_INIT || iwl_mvm_has_unified_ucode(mvm);
318e705c121SKalle Valo 
319e705c121SKalle Valo 	if (ucode_type == IWL_UCODE_REGULAR &&
3203d2d4422SGolan Ben-Ami 	    iwl_fw_dbg_conf_usniffer(mvm->fw, FW_DBG_START_FROM_ALIVE) &&
3213d2d4422SGolan Ben-Ami 	    !(fw_has_capa(&mvm->fw->ucode_capa,
3223d2d4422SGolan Ben-Ami 			  IWL_UCODE_TLV_CAPA_USNIFFER_UNIFIED)))
323612da1efSSharon Dvir 		fw = iwl_get_ucode_image(mvm->fw, IWL_UCODE_REGULAR_USNIFFER);
324e705c121SKalle Valo 	else
325612da1efSSharon Dvir 		fw = iwl_get_ucode_image(mvm->fw, ucode_type);
326e705c121SKalle Valo 	if (WARN_ON(!fw))
327e705c121SKalle Valo 		return -EINVAL;
328702e975dSJohannes Berg 	iwl_fw_set_current_image(&mvm->fwrt, ucode_type);
32965b280feSJohannes Berg 	clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
330e705c121SKalle Valo 
331e705c121SKalle Valo 	iwl_init_notification_wait(&mvm->notif_wait, &alive_wait,
332e705c121SKalle Valo 				   alive_cmd, ARRAY_SIZE(alive_cmd),
333e705c121SKalle Valo 				   iwl_alive_fn, &alive_data);
334e705c121SKalle Valo 
335b3500b47SEmmanuel Grumbach 	/*
336b3500b47SEmmanuel Grumbach 	 * We want to load the INIT firmware even in RFKILL
337b3500b47SEmmanuel Grumbach 	 * For the unified firmware case, the ucode_type is not
338b3500b47SEmmanuel Grumbach 	 * INIT, but we still need to run it.
339b3500b47SEmmanuel Grumbach 	 */
340b3500b47SEmmanuel Grumbach 	ret = iwl_trans_start_fw(mvm->trans, fw, run_in_rfkill);
341e705c121SKalle Valo 	if (ret) {
342702e975dSJohannes Berg 		iwl_fw_set_current_image(&mvm->fwrt, old_type);
343e705c121SKalle Valo 		iwl_remove_notification(&mvm->notif_wait, &alive_wait);
344e705c121SKalle Valo 		return ret;
345e705c121SKalle Valo 	}
346e705c121SKalle Valo 
347e705c121SKalle Valo 	/*
348e705c121SKalle Valo 	 * Some things may run in the background now, but we
349e705c121SKalle Valo 	 * just wait for the ALIVE notification here.
350e705c121SKalle Valo 	 */
351e705c121SKalle Valo 	ret = iwl_wait_notification(&mvm->notif_wait, &alive_wait,
352e705c121SKalle Valo 				    MVM_UCODE_ALIVE_TIMEOUT);
353e705c121SKalle Valo 	if (ret) {
354d6be9c1dSSara Sharon 		struct iwl_trans *trans = mvm->trans;
355d6be9c1dSSara Sharon 
35667b8261cSShahar S Matityahu 		if (ret == -ETIMEDOUT)
357700b3799SShahar S Matityahu 			iwl_fw_dbg_error_collect(&mvm->fwrt,
358700b3799SShahar S Matityahu 						 FW_DBG_TRIGGER_ALIVE_TIMEOUT);
35967b8261cSShahar S Matityahu 
3605f01df3fSGolan Ben Ami 		if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_22000)
361e705c121SKalle Valo 			IWL_ERR(mvm,
362e705c121SKalle Valo 				"SecBoot CPU1 Status: 0x%x, CPU2 Status: 0x%x\n",
363ea695b7cSShaul Triebitz 				iwl_read_umac_prph(trans, UMAG_SB_CPU_1_STATUS),
364ea695b7cSShaul Triebitz 				iwl_read_umac_prph(trans,
365ea695b7cSShaul Triebitz 						   UMAG_SB_CPU_2_STATUS));
3666e584873SSara Sharon 		else if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_8000)
367d6be9c1dSSara Sharon 			IWL_ERR(mvm,
368d6be9c1dSSara Sharon 				"SecBoot CPU1 Status: 0x%x, CPU2 Status: 0x%x\n",
369d6be9c1dSSara Sharon 				iwl_read_prph(trans, SB_CPU_1_STATUS),
370d6be9c1dSSara Sharon 				iwl_read_prph(trans, SB_CPU_2_STATUS));
371702e975dSJohannes Berg 		iwl_fw_set_current_image(&mvm->fwrt, old_type);
372e705c121SKalle Valo 		return ret;
373e705c121SKalle Valo 	}
374e705c121SKalle Valo 
375e705c121SKalle Valo 	if (!alive_data.valid) {
376e705c121SKalle Valo 		IWL_ERR(mvm, "Loaded ucode is not valid!\n");
377702e975dSJohannes Berg 		iwl_fw_set_current_image(&mvm->fwrt, old_type);
378e705c121SKalle Valo 		return -EIO;
379e705c121SKalle Valo 	}
380e705c121SKalle Valo 
381e705c121SKalle Valo 	iwl_trans_fw_alive(mvm->trans, alive_data.scd_base_addr);
382e705c121SKalle Valo 
383e705c121SKalle Valo 	/*
384e705c121SKalle Valo 	 * Note: all the queues are enabled as part of the interface
385e705c121SKalle Valo 	 * initialization, but in firmware restart scenarios they
386e705c121SKalle Valo 	 * could be stopped, so wake them up. In firmware restart,
387e705c121SKalle Valo 	 * mac80211 will have the queues stopped as well until the
388e705c121SKalle Valo 	 * reconfiguration completes. During normal startup, they
389e705c121SKalle Valo 	 * will be empty.
390e705c121SKalle Valo 	 */
391e705c121SKalle Valo 
392e705c121SKalle Valo 	memset(&mvm->queue_info, 0, sizeof(mvm->queue_info));
3931c14089eSJohannes Berg 	/*
3941c14089eSJohannes Berg 	 * Set a 'fake' TID for the command queue, since we use the
3951c14089eSJohannes Berg 	 * hweight() of the tid_bitmap as a refcount now. Not that
3961c14089eSJohannes Berg 	 * we ever even consider the command queue as one we might
3971c14089eSJohannes Berg 	 * want to reuse, but be safe nevertheless.
3981c14089eSJohannes Berg 	 */
3991c14089eSJohannes Berg 	mvm->queue_info[IWL_MVM_DQA_CMD_QUEUE].tid_bitmap =
4001c14089eSJohannes Berg 		BIT(IWL_MAX_TID_COUNT + 2);
401e705c121SKalle Valo 
40265b280feSJohannes Berg 	set_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
403f7805b33SLior Cohen #ifdef CONFIG_IWLWIFI_DEBUGFS
404f7805b33SLior Cohen 	iwl_fw_set_dbg_rec_on(&mvm->fwrt);
405f7805b33SLior Cohen #endif
406e705c121SKalle Valo 
407e705c121SKalle Valo 	return 0;
408e705c121SKalle Valo }
409e705c121SKalle Valo 
4108c5f47b1SJohannes Berg static int iwl_run_unified_mvm_ucode(struct iwl_mvm *mvm, bool read_nvm)
4118c5f47b1SJohannes Berg {
4128c5f47b1SJohannes Berg 	struct iwl_notification_wait init_wait;
4138c5f47b1SJohannes Berg 	struct iwl_nvm_access_complete_cmd nvm_complete = {};
4148c5f47b1SJohannes Berg 	struct iwl_init_extended_cfg_cmd init_cfg = {
4158c5f47b1SJohannes Berg 		.init_flags = cpu_to_le32(BIT(IWL_INIT_NVM)),
4168c5f47b1SJohannes Berg 	};
4178c5f47b1SJohannes Berg 	static const u16 init_complete[] = {
4188c5f47b1SJohannes Berg 		INIT_COMPLETE_NOTIF,
4198c5f47b1SJohannes Berg 	};
4208c5f47b1SJohannes Berg 	int ret;
4218c5f47b1SJohannes Berg 
4228c5f47b1SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
4238c5f47b1SJohannes Berg 
4248c5f47b1SJohannes Berg 	iwl_init_notification_wait(&mvm->notif_wait,
4258c5f47b1SJohannes Berg 				   &init_wait,
4268c5f47b1SJohannes Berg 				   init_complete,
4278c5f47b1SJohannes Berg 				   ARRAY_SIZE(init_complete),
4288c5f47b1SJohannes Berg 				   iwl_wait_init_complete,
4298c5f47b1SJohannes Berg 				   NULL);
4308c5f47b1SJohannes Berg 
43186ce5c74SShahar S Matityahu 	iwl_fw_dbg_apply_point(&mvm->fwrt, IWL_FW_INI_APPLY_EARLY);
43286ce5c74SShahar S Matityahu 
4338c5f47b1SJohannes Berg 	/* Will also start the device */
4348c5f47b1SJohannes Berg 	ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_REGULAR);
4358c5f47b1SJohannes Berg 	if (ret) {
4368c5f47b1SJohannes Berg 		IWL_ERR(mvm, "Failed to start RT ucode: %d\n", ret);
4378c5f47b1SJohannes Berg 		goto error;
4388c5f47b1SJohannes Berg 	}
43986ce5c74SShahar S Matityahu 	iwl_fw_dbg_apply_point(&mvm->fwrt, IWL_FW_INI_APPLY_AFTER_ALIVE);
4408c5f47b1SJohannes Berg 
4418c5f47b1SJohannes Berg 	/* Send init config command to mark that we are sending NVM access
4428c5f47b1SJohannes Berg 	 * commands
4438c5f47b1SJohannes Berg 	 */
4448c5f47b1SJohannes Berg 	ret = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(SYSTEM_GROUP,
445b3500b47SEmmanuel Grumbach 						INIT_EXTENDED_CFG_CMD),
446b3500b47SEmmanuel Grumbach 				   CMD_SEND_IN_RFKILL,
4478c5f47b1SJohannes Berg 				   sizeof(init_cfg), &init_cfg);
4488c5f47b1SJohannes Berg 	if (ret) {
4498c5f47b1SJohannes Berg 		IWL_ERR(mvm, "Failed to run init config command: %d\n",
4508c5f47b1SJohannes Berg 			ret);
4518c5f47b1SJohannes Berg 		goto error;
4528c5f47b1SJohannes Berg 	}
4538c5f47b1SJohannes Berg 
454e9e1ba3dSSara Sharon 	/* Load NVM to NIC if needed */
455e9e1ba3dSSara Sharon 	if (mvm->nvm_file_name) {
4569c4f7d51SShaul Triebitz 		iwl_read_external_nvm(mvm->trans, mvm->nvm_file_name,
4579c4f7d51SShaul Triebitz 				      mvm->nvm_sections);
4588c5f47b1SJohannes Berg 		iwl_mvm_load_nvm_to_nic(mvm);
459e9e1ba3dSSara Sharon 	}
4608c5f47b1SJohannes Berg 
461d4f3695eSSara Sharon 	if (IWL_MVM_PARSE_NVM && read_nvm) {
4625bd1d2c1SLuca Coelho 		ret = iwl_nvm_init(mvm);
463d4f3695eSSara Sharon 		if (ret) {
464d4f3695eSSara Sharon 			IWL_ERR(mvm, "Failed to read NVM: %d\n", ret);
465d4f3695eSSara Sharon 			goto error;
466d4f3695eSSara Sharon 		}
467d4f3695eSSara Sharon 	}
468d4f3695eSSara Sharon 
4698c5f47b1SJohannes Berg 	ret = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(REGULATORY_AND_NVM_GROUP,
470b3500b47SEmmanuel Grumbach 						NVM_ACCESS_COMPLETE),
471b3500b47SEmmanuel Grumbach 				   CMD_SEND_IN_RFKILL,
4728c5f47b1SJohannes Berg 				   sizeof(nvm_complete), &nvm_complete);
4738c5f47b1SJohannes Berg 	if (ret) {
4748c5f47b1SJohannes Berg 		IWL_ERR(mvm, "Failed to run complete NVM access: %d\n",
4758c5f47b1SJohannes Berg 			ret);
4768c5f47b1SJohannes Berg 		goto error;
4778c5f47b1SJohannes Berg 	}
4788c5f47b1SJohannes Berg 
4798c5f47b1SJohannes Berg 	/* We wait for the INIT complete notification */
480e9e1ba3dSSara Sharon 	ret = iwl_wait_notification(&mvm->notif_wait, &init_wait,
4818c5f47b1SJohannes Berg 				    MVM_UCODE_ALIVE_TIMEOUT);
482e9e1ba3dSSara Sharon 	if (ret)
483e9e1ba3dSSara Sharon 		return ret;
484e9e1ba3dSSara Sharon 
485e9e1ba3dSSara Sharon 	/* Read the NVM only at driver load time, no need to do this twice */
486d4f3695eSSara Sharon 	if (!IWL_MVM_PARSE_NVM && read_nvm) {
4874c625c56SShaul Triebitz 		mvm->nvm_data = iwl_get_nvm(mvm->trans, mvm->fw);
488c135cb56SShaul Triebitz 		if (IS_ERR(mvm->nvm_data)) {
489c135cb56SShaul Triebitz 			ret = PTR_ERR(mvm->nvm_data);
490c135cb56SShaul Triebitz 			mvm->nvm_data = NULL;
491e9e1ba3dSSara Sharon 			IWL_ERR(mvm, "Failed to read NVM: %d\n", ret);
492e9e1ba3dSSara Sharon 			return ret;
493e9e1ba3dSSara Sharon 		}
494e9e1ba3dSSara Sharon 	}
495e9e1ba3dSSara Sharon 
496b3500b47SEmmanuel Grumbach 	mvm->rfkill_safe_init_done = true;
497b3500b47SEmmanuel Grumbach 
498e9e1ba3dSSara Sharon 	return 0;
4998c5f47b1SJohannes Berg 
5008c5f47b1SJohannes Berg error:
5018c5f47b1SJohannes Berg 	iwl_remove_notification(&mvm->notif_wait, &init_wait);
5028c5f47b1SJohannes Berg 	return ret;
5038c5f47b1SJohannes Berg }
5048c5f47b1SJohannes Berg 
505e705c121SKalle Valo static int iwl_send_phy_cfg_cmd(struct iwl_mvm *mvm)
506e705c121SKalle Valo {
507e705c121SKalle Valo 	struct iwl_phy_cfg_cmd phy_cfg_cmd;
508702e975dSJohannes Berg 	enum iwl_ucode_type ucode_type = mvm->fwrt.cur_fw_img;
509e705c121SKalle Valo 
510e705c121SKalle Valo 	/* Set parameters */
511e705c121SKalle Valo 	phy_cfg_cmd.phy_cfg = cpu_to_le32(iwl_mvm_get_phy_config(mvm));
51286a2b204SLuca Coelho 
51386a2b204SLuca Coelho 	/* set flags extra PHY configuration flags from the device's cfg */
51486a2b204SLuca Coelho 	phy_cfg_cmd.phy_cfg |= cpu_to_le32(mvm->cfg->extra_phy_cfg_flags);
51586a2b204SLuca Coelho 
516e705c121SKalle Valo 	phy_cfg_cmd.calib_control.event_trigger =
517e705c121SKalle Valo 		mvm->fw->default_calib[ucode_type].event_trigger;
518e705c121SKalle Valo 	phy_cfg_cmd.calib_control.flow_trigger =
519e705c121SKalle Valo 		mvm->fw->default_calib[ucode_type].flow_trigger;
520e705c121SKalle Valo 
521e705c121SKalle Valo 	IWL_DEBUG_INFO(mvm, "Sending Phy CFG command: 0x%x\n",
522e705c121SKalle Valo 		       phy_cfg_cmd.phy_cfg);
523e705c121SKalle Valo 
524e705c121SKalle Valo 	return iwl_mvm_send_cmd_pdu(mvm, PHY_CONFIGURATION_CMD, 0,
525e705c121SKalle Valo 				    sizeof(phy_cfg_cmd), &phy_cfg_cmd);
526e705c121SKalle Valo }
527e705c121SKalle Valo 
528e705c121SKalle Valo int iwl_run_init_mvm_ucode(struct iwl_mvm *mvm, bool read_nvm)
529e705c121SKalle Valo {
530e705c121SKalle Valo 	struct iwl_notification_wait calib_wait;
531e705c121SKalle Valo 	static const u16 init_complete[] = {
532e705c121SKalle Valo 		INIT_COMPLETE_NOTIF,
533e705c121SKalle Valo 		CALIB_RES_NOTIF_PHY_DB
534e705c121SKalle Valo 	};
535e705c121SKalle Valo 	int ret;
536e705c121SKalle Valo 
5377d6222e2SJohannes Berg 	if (iwl_mvm_has_unified_ucode(mvm))
5388c5f47b1SJohannes Berg 		return iwl_run_unified_mvm_ucode(mvm, true);
5398c5f47b1SJohannes Berg 
540e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
541e705c121SKalle Valo 
542b3500b47SEmmanuel Grumbach 	if (WARN_ON_ONCE(mvm->rfkill_safe_init_done))
543e705c121SKalle Valo 		return 0;
544e705c121SKalle Valo 
545e705c121SKalle Valo 	iwl_init_notification_wait(&mvm->notif_wait,
546e705c121SKalle Valo 				   &calib_wait,
547e705c121SKalle Valo 				   init_complete,
548e705c121SKalle Valo 				   ARRAY_SIZE(init_complete),
549e705c121SKalle Valo 				   iwl_wait_phy_db_entry,
550e705c121SKalle Valo 				   mvm->phy_db);
551e705c121SKalle Valo 
552e705c121SKalle Valo 	/* Will also start the device */
553e705c121SKalle Valo 	ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_INIT);
554e705c121SKalle Valo 	if (ret) {
555e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to start INIT ucode: %d\n", ret);
55600e0c6c8SLuca Coelho 		goto remove_notif;
557e705c121SKalle Valo 	}
558e705c121SKalle Valo 
559b3de3ef4SEmmanuel Grumbach 	if (mvm->cfg->device_family < IWL_DEVICE_FAMILY_8000) {
560b3de3ef4SEmmanuel Grumbach 		ret = iwl_mvm_send_bt_init_conf(mvm);
561e705c121SKalle Valo 		if (ret)
56200e0c6c8SLuca Coelho 			goto remove_notif;
563b3de3ef4SEmmanuel Grumbach 	}
564e705c121SKalle Valo 
565e705c121SKalle Valo 	/* Read the NVM only at driver load time, no need to do this twice */
566e705c121SKalle Valo 	if (read_nvm) {
5675bd1d2c1SLuca Coelho 		ret = iwl_nvm_init(mvm);
568e705c121SKalle Valo 		if (ret) {
569e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to read NVM: %d\n", ret);
57000e0c6c8SLuca Coelho 			goto remove_notif;
571e705c121SKalle Valo 		}
572e705c121SKalle Valo 	}
573e705c121SKalle Valo 
574e705c121SKalle Valo 	/* In case we read the NVM from external file, load it to the NIC */
575e705c121SKalle Valo 	if (mvm->nvm_file_name)
576e705c121SKalle Valo 		iwl_mvm_load_nvm_to_nic(mvm);
577e705c121SKalle Valo 
57864866e5dSLuca Coelho 	WARN_ONCE(mvm->nvm_data->nvm_version < mvm->trans->cfg->nvm_ver,
57964866e5dSLuca Coelho 		  "Too old NVM version (0x%0x, required = 0x%0x)",
58064866e5dSLuca Coelho 		  mvm->nvm_data->nvm_version, mvm->trans->cfg->nvm_ver);
581e705c121SKalle Valo 
582e705c121SKalle Valo 	/*
583e705c121SKalle Valo 	 * abort after reading the nvm in case RF Kill is on, we will complete
584e705c121SKalle Valo 	 * the init seq later when RF kill will switch to off
585e705c121SKalle Valo 	 */
586e705c121SKalle Valo 	if (iwl_mvm_is_radio_hw_killed(mvm)) {
587e705c121SKalle Valo 		IWL_DEBUG_RF_KILL(mvm,
588e705c121SKalle Valo 				  "jump over all phy activities due to RF kill\n");
58900e0c6c8SLuca Coelho 		goto remove_notif;
590e705c121SKalle Valo 	}
591e705c121SKalle Valo 
592b3500b47SEmmanuel Grumbach 	mvm->rfkill_safe_init_done = true;
593e705c121SKalle Valo 
594e705c121SKalle Valo 	/* Send TX valid antennas before triggering calibrations */
595e705c121SKalle Valo 	ret = iwl_send_tx_ant_cfg(mvm, iwl_mvm_get_valid_tx_ant(mvm));
596e705c121SKalle Valo 	if (ret)
59700e0c6c8SLuca Coelho 		goto remove_notif;
598e705c121SKalle Valo 
599e705c121SKalle Valo 	ret = iwl_send_phy_cfg_cmd(mvm);
600e705c121SKalle Valo 	if (ret) {
601e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to run INIT calibrations: %d\n",
602e705c121SKalle Valo 			ret);
60300e0c6c8SLuca Coelho 		goto remove_notif;
604e705c121SKalle Valo 	}
605e705c121SKalle Valo 
606e705c121SKalle Valo 	/*
607e705c121SKalle Valo 	 * Some things may run in the background now, but we
608e705c121SKalle Valo 	 * just wait for the calibration complete notification.
609e705c121SKalle Valo 	 */
610e705c121SKalle Valo 	ret = iwl_wait_notification(&mvm->notif_wait, &calib_wait,
611e705c121SKalle Valo 				    MVM_UCODE_CALIB_TIMEOUT);
61200e0c6c8SLuca Coelho 	if (!ret)
613e705c121SKalle Valo 		goto out;
614e705c121SKalle Valo 
61500e0c6c8SLuca Coelho 	if (iwl_mvm_is_radio_hw_killed(mvm)) {
61600e0c6c8SLuca Coelho 		IWL_DEBUG_RF_KILL(mvm, "RFKILL while calibrating.\n");
61700e0c6c8SLuca Coelho 		ret = 0;
61800e0c6c8SLuca Coelho 	} else {
61900e0c6c8SLuca Coelho 		IWL_ERR(mvm, "Failed to run INIT calibrations: %d\n",
62000e0c6c8SLuca Coelho 			ret);
62100e0c6c8SLuca Coelho 	}
62200e0c6c8SLuca Coelho 
62300e0c6c8SLuca Coelho 	goto out;
62400e0c6c8SLuca Coelho 
62500e0c6c8SLuca Coelho remove_notif:
626e705c121SKalle Valo 	iwl_remove_notification(&mvm->notif_wait, &calib_wait);
627e705c121SKalle Valo out:
628b3500b47SEmmanuel Grumbach 	mvm->rfkill_safe_init_done = false;
629e705c121SKalle Valo 	if (iwlmvm_mod_params.init_dbg && !mvm->nvm_data) {
630e705c121SKalle Valo 		/* we want to debug INIT and we have no NVM - fake */
631e705c121SKalle Valo 		mvm->nvm_data = kzalloc(sizeof(struct iwl_nvm_data) +
632e705c121SKalle Valo 					sizeof(struct ieee80211_channel) +
633e705c121SKalle Valo 					sizeof(struct ieee80211_rate),
634e705c121SKalle Valo 					GFP_KERNEL);
635e705c121SKalle Valo 		if (!mvm->nvm_data)
636e705c121SKalle Valo 			return -ENOMEM;
637e705c121SKalle Valo 		mvm->nvm_data->bands[0].channels = mvm->nvm_data->channels;
638e705c121SKalle Valo 		mvm->nvm_data->bands[0].n_channels = 1;
639e705c121SKalle Valo 		mvm->nvm_data->bands[0].n_bitrates = 1;
640e705c121SKalle Valo 		mvm->nvm_data->bands[0].bitrates =
641e705c121SKalle Valo 			(void *)mvm->nvm_data->channels + 1;
642e705c121SKalle Valo 		mvm->nvm_data->bands[0].bitrates->hw_value = 10;
643e705c121SKalle Valo 	}
644e705c121SKalle Valo 
645e705c121SKalle Valo 	return ret;
646e705c121SKalle Valo }
647e705c121SKalle Valo 
648e705c121SKalle Valo static int iwl_mvm_config_ltr(struct iwl_mvm *mvm)
649e705c121SKalle Valo {
650e705c121SKalle Valo 	struct iwl_ltr_config_cmd cmd = {
651e705c121SKalle Valo 		.flags = cpu_to_le32(LTR_CFG_FLAG_FEATURE_ENABLE),
652e705c121SKalle Valo 	};
653e705c121SKalle Valo 
654e705c121SKalle Valo 	if (!mvm->trans->ltr_enabled)
655e705c121SKalle Valo 		return 0;
656e705c121SKalle Valo 
657e705c121SKalle Valo 	return iwl_mvm_send_cmd_pdu(mvm, LTR_CONFIG, 0,
658e705c121SKalle Valo 				    sizeof(cmd), &cmd);
659e705c121SKalle Valo }
660e705c121SKalle Valo 
661c386dacbSHaim Dreyfuss #ifdef CONFIG_ACPI
662e8698301SArnd Bergmann static inline int iwl_mvm_sar_set_profile(struct iwl_mvm *mvm,
663c386dacbSHaim Dreyfuss 					  union acpi_object *table,
664c386dacbSHaim Dreyfuss 					  struct iwl_mvm_sar_profile *profile,
665c386dacbSHaim Dreyfuss 					  bool enabled)
666da2830acSLuca Coelho {
667c386dacbSHaim Dreyfuss 	int i;
668da2830acSLuca Coelho 
669c386dacbSHaim Dreyfuss 	profile->enabled = enabled;
670da2830acSLuca Coelho 
671e7a3b8d8SLuca Coelho 	for (i = 0; i < ACPI_SAR_TABLE_SIZE; i++) {
672c386dacbSHaim Dreyfuss 		if ((table[i].type != ACPI_TYPE_INTEGER) ||
673c386dacbSHaim Dreyfuss 		    (table[i].integer.value > U8_MAX))
674da2830acSLuca Coelho 			return -EINVAL;
675da2830acSLuca Coelho 
676c386dacbSHaim Dreyfuss 		profile->table[i] = table[i].integer.value;
677da2830acSLuca Coelho 	}
678da2830acSLuca Coelho 
679da2830acSLuca Coelho 	return 0;
680da2830acSLuca Coelho }
681da2830acSLuca Coelho 
682c386dacbSHaim Dreyfuss static int iwl_mvm_sar_get_wrds_table(struct iwl_mvm *mvm)
683c386dacbSHaim Dreyfuss {
684813df5ceSLuca Coelho 	union acpi_object *wifi_pkg, *table, *data;
685c386dacbSHaim Dreyfuss 	bool enabled;
6860c3d7282SHaim Dreyfuss 	int ret, tbl_rev;
687da2830acSLuca Coelho 
688813df5ceSLuca Coelho 	data = iwl_acpi_get_object(mvm->dev, ACPI_WRDS_METHOD);
689813df5ceSLuca Coelho 	if (IS_ERR(data))
690813df5ceSLuca Coelho 		return PTR_ERR(data);
691da2830acSLuca Coelho 
6922fa388cfSLuca Coelho 	wifi_pkg = iwl_acpi_get_wifi_pkg(mvm->dev, data,
6930c3d7282SHaim Dreyfuss 					 ACPI_WRDS_WIFI_DATA_SIZE, &tbl_rev);
6940c3d7282SHaim Dreyfuss 	if (IS_ERR(wifi_pkg) || tbl_rev != 0) {
695c386dacbSHaim Dreyfuss 		ret = PTR_ERR(wifi_pkg);
696c386dacbSHaim Dreyfuss 		goto out_free;
697c386dacbSHaim Dreyfuss 	}
698da2830acSLuca Coelho 
699c386dacbSHaim Dreyfuss 	if (wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER) {
700c386dacbSHaim Dreyfuss 		ret = -EINVAL;
701c386dacbSHaim Dreyfuss 		goto out_free;
702c386dacbSHaim Dreyfuss 	}
703c386dacbSHaim Dreyfuss 
704c386dacbSHaim Dreyfuss 	enabled = !!(wifi_pkg->package.elements[1].integer.value);
705c386dacbSHaim Dreyfuss 
706c386dacbSHaim Dreyfuss 	/* position of the actual table */
707c386dacbSHaim Dreyfuss 	table = &wifi_pkg->package.elements[2];
708c386dacbSHaim Dreyfuss 
709c386dacbSHaim Dreyfuss 	/* The profile from WRDS is officially profile 1, but goes
710c386dacbSHaim Dreyfuss 	 * into sar_profiles[0] (because we don't have a profile 0).
711c386dacbSHaim Dreyfuss 	 */
712c386dacbSHaim Dreyfuss 	ret = iwl_mvm_sar_set_profile(mvm, table, &mvm->sar_profiles[0],
713c386dacbSHaim Dreyfuss 				      enabled);
714c386dacbSHaim Dreyfuss out_free:
715813df5ceSLuca Coelho 	kfree(data);
716da2830acSLuca Coelho 	return ret;
717da2830acSLuca Coelho }
718da2830acSLuca Coelho 
71969964905SLuca Coelho static int iwl_mvm_sar_get_ewrd_table(struct iwl_mvm *mvm)
72069964905SLuca Coelho {
721813df5ceSLuca Coelho 	union acpi_object *wifi_pkg, *data;
72269964905SLuca Coelho 	bool enabled;
7230c3d7282SHaim Dreyfuss 	int i, n_profiles, ret, tbl_rev;
72469964905SLuca Coelho 
725813df5ceSLuca Coelho 	data = iwl_acpi_get_object(mvm->dev, ACPI_EWRD_METHOD);
726813df5ceSLuca Coelho 	if (IS_ERR(data))
727813df5ceSLuca Coelho 		return PTR_ERR(data);
72869964905SLuca Coelho 
7292fa388cfSLuca Coelho 	wifi_pkg = iwl_acpi_get_wifi_pkg(mvm->dev, data,
7300c3d7282SHaim Dreyfuss 					 ACPI_EWRD_WIFI_DATA_SIZE, &tbl_rev);
7310c3d7282SHaim Dreyfuss 	if (IS_ERR(wifi_pkg) || tbl_rev != 0) {
73269964905SLuca Coelho 		ret = PTR_ERR(wifi_pkg);
73369964905SLuca Coelho 		goto out_free;
73469964905SLuca Coelho 	}
73569964905SLuca Coelho 
73669964905SLuca Coelho 	if ((wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER) ||
73769964905SLuca Coelho 	    (wifi_pkg->package.elements[2].type != ACPI_TYPE_INTEGER)) {
73869964905SLuca Coelho 		ret = -EINVAL;
73969964905SLuca Coelho 		goto out_free;
74069964905SLuca Coelho 	}
74169964905SLuca Coelho 
74269964905SLuca Coelho 	enabled = !!(wifi_pkg->package.elements[1].integer.value);
74369964905SLuca Coelho 	n_profiles = wifi_pkg->package.elements[2].integer.value;
74469964905SLuca Coelho 
7452e1976bbSLuca Coelho 	/*
7462e1976bbSLuca Coelho 	 * Check the validity of n_profiles.  The EWRD profiles start
7472e1976bbSLuca Coelho 	 * from index 1, so the maximum value allowed here is
7482e1976bbSLuca Coelho 	 * ACPI_SAR_PROFILES_NUM - 1.
7492e1976bbSLuca Coelho 	 */
7502e1976bbSLuca Coelho 	if (n_profiles <= 0 || n_profiles >= ACPI_SAR_PROFILE_NUM) {
751e2ef1476SSharon Dvir 		ret = -EINVAL;
752e2ef1476SSharon Dvir 		goto out_free;
753e2ef1476SSharon Dvir 	}
754e2ef1476SSharon Dvir 
75569964905SLuca Coelho 	for (i = 0; i < n_profiles; i++) {
75669964905SLuca Coelho 		/* the tables start at element 3 */
75769964905SLuca Coelho 		static int pos = 3;
75869964905SLuca Coelho 
75969964905SLuca Coelho 		/* The EWRD profiles officially go from 2 to 4, but we
76069964905SLuca Coelho 		 * save them in sar_profiles[1-3] (because we don't
76169964905SLuca Coelho 		 * have profile 0).  So in the array we start from 1.
76269964905SLuca Coelho 		 */
76369964905SLuca Coelho 		ret = iwl_mvm_sar_set_profile(mvm,
76469964905SLuca Coelho 					      &wifi_pkg->package.elements[pos],
76569964905SLuca Coelho 					      &mvm->sar_profiles[i + 1],
76669964905SLuca Coelho 					      enabled);
76769964905SLuca Coelho 		if (ret < 0)
76869964905SLuca Coelho 			break;
76969964905SLuca Coelho 
77069964905SLuca Coelho 		/* go to the next table */
771e7a3b8d8SLuca Coelho 		pos += ACPI_SAR_TABLE_SIZE;
77269964905SLuca Coelho 	}
77369964905SLuca Coelho 
77469964905SLuca Coelho out_free:
775813df5ceSLuca Coelho 	kfree(data);
77669964905SLuca Coelho 	return ret;
77769964905SLuca Coelho }
77869964905SLuca Coelho 
7797fe90e0eSHaim Dreyfuss static int iwl_mvm_sar_get_wgds_table(struct iwl_mvm *mvm)
780a6bff3cbSHaim Dreyfuss {
781813df5ceSLuca Coelho 	union acpi_object *wifi_pkg, *data;
7820c3d7282SHaim Dreyfuss 	int i, j, ret, tbl_rev;
7837fe90e0eSHaim Dreyfuss 	int idx = 1;
784a6bff3cbSHaim Dreyfuss 
785813df5ceSLuca Coelho 	data = iwl_acpi_get_object(mvm->dev, ACPI_WGDS_METHOD);
786813df5ceSLuca Coelho 	if (IS_ERR(data))
787813df5ceSLuca Coelho 		return PTR_ERR(data);
788a6bff3cbSHaim Dreyfuss 
7892fa388cfSLuca Coelho 	wifi_pkg = iwl_acpi_get_wifi_pkg(mvm->dev, data,
7900c3d7282SHaim Dreyfuss 					 ACPI_WGDS_WIFI_DATA_SIZE, &tbl_rev);
7910c3d7282SHaim Dreyfuss 	if (IS_ERR(wifi_pkg) || tbl_rev > 1) {
792a6bff3cbSHaim Dreyfuss 		ret = PTR_ERR(wifi_pkg);
793a6bff3cbSHaim Dreyfuss 		goto out_free;
794a6bff3cbSHaim Dreyfuss 	}
795a6bff3cbSHaim Dreyfuss 
7960c3d7282SHaim Dreyfuss 	mvm->geo_rev = tbl_rev;
797e7a3b8d8SLuca Coelho 	for (i = 0; i < ACPI_NUM_GEO_PROFILES; i++) {
798e7a3b8d8SLuca Coelho 		for (j = 0; j < ACPI_GEO_TABLE_SIZE; j++) {
799a6bff3cbSHaim Dreyfuss 			union acpi_object *entry;
800a6bff3cbSHaim Dreyfuss 
8017fe90e0eSHaim Dreyfuss 			entry = &wifi_pkg->package.elements[idx++];
802a6bff3cbSHaim Dreyfuss 			if ((entry->type != ACPI_TYPE_INTEGER) ||
803aae9d563SChristophe Jaillet 			    (entry->integer.value > U8_MAX)) {
804aae9d563SChristophe Jaillet 				ret = -EINVAL;
805aae9d563SChristophe Jaillet 				goto out_free;
806aae9d563SChristophe Jaillet 			}
807a6bff3cbSHaim Dreyfuss 
8087fe90e0eSHaim Dreyfuss 			mvm->geo_profiles[i].values[j] = entry->integer.value;
8097fe90e0eSHaim Dreyfuss 		}
810a6bff3cbSHaim Dreyfuss 	}
811a6bff3cbSHaim Dreyfuss 	ret = 0;
812a6bff3cbSHaim Dreyfuss out_free:
813813df5ceSLuca Coelho 	kfree(data);
814a6bff3cbSHaim Dreyfuss 	return ret;
815a6bff3cbSHaim Dreyfuss }
816a6bff3cbSHaim Dreyfuss 
81742ce76d6SLuca Coelho int iwl_mvm_sar_select_profile(struct iwl_mvm *mvm, int prof_a, int prof_b)
818da2830acSLuca Coelho {
8190791c2fcSHaim Dreyfuss 	union {
8200791c2fcSHaim Dreyfuss 		struct iwl_dev_tx_power_cmd v5;
8210791c2fcSHaim Dreyfuss 		struct iwl_dev_tx_power_cmd_v4 v4;
8220791c2fcSHaim Dreyfuss 	} cmd;
82342ce76d6SLuca Coelho 	int i, j, idx;
824e7a3b8d8SLuca Coelho 	int profs[ACPI_SAR_NUM_CHAIN_LIMITS] = { prof_a, prof_b };
8250791c2fcSHaim Dreyfuss 	int len;
826da2830acSLuca Coelho 
827e7a3b8d8SLuca Coelho 	BUILD_BUG_ON(ACPI_SAR_NUM_CHAIN_LIMITS < 2);
828e7a3b8d8SLuca Coelho 	BUILD_BUG_ON(ACPI_SAR_NUM_CHAIN_LIMITS * ACPI_SAR_NUM_SUB_BANDS !=
829e7a3b8d8SLuca Coelho 		     ACPI_SAR_TABLE_SIZE);
83042ce76d6SLuca Coelho 
8310791c2fcSHaim Dreyfuss 	cmd.v5.v3.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_CHAINS);
8320791c2fcSHaim Dreyfuss 
8330791c2fcSHaim Dreyfuss 	if (fw_has_api(&mvm->fw->ucode_capa,
8340791c2fcSHaim Dreyfuss 		       IWL_UCODE_TLV_API_REDUCE_TX_POWER))
8350791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v5);
8360791c2fcSHaim Dreyfuss 	else if (fw_has_capa(&mvm->fw->ucode_capa,
8370791c2fcSHaim Dreyfuss 			     IWL_UCODE_TLV_CAPA_TX_POWER_ACK))
8380791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v4);
8390791c2fcSHaim Dreyfuss 	else
8400791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v4.v3);
84155bfa4b9SLuca Coelho 
842e7a3b8d8SLuca Coelho 	for (i = 0; i < ACPI_SAR_NUM_CHAIN_LIMITS; i++) {
84342ce76d6SLuca Coelho 		struct iwl_mvm_sar_profile *prof;
84442ce76d6SLuca Coelho 
84542ce76d6SLuca Coelho 		/* don't allow SAR to be disabled (profile 0 means disable) */
84642ce76d6SLuca Coelho 		if (profs[i] == 0)
84742ce76d6SLuca Coelho 			return -EPERM;
84842ce76d6SLuca Coelho 
849e7a3b8d8SLuca Coelho 		/* we are off by one, so allow up to ACPI_SAR_PROFILE_NUM */
850e7a3b8d8SLuca Coelho 		if (profs[i] > ACPI_SAR_PROFILE_NUM)
85142ce76d6SLuca Coelho 			return -EINVAL;
85242ce76d6SLuca Coelho 
85342ce76d6SLuca Coelho 		/* profiles go from 1 to 4, so decrement to access the array */
85442ce76d6SLuca Coelho 		prof = &mvm->sar_profiles[profs[i] - 1];
85542ce76d6SLuca Coelho 
85642ce76d6SLuca Coelho 		/* if the profile is disabled, do nothing */
85742ce76d6SLuca Coelho 		if (!prof->enabled) {
85842ce76d6SLuca Coelho 			IWL_DEBUG_RADIO(mvm, "SAR profile %d is disabled.\n",
85942ce76d6SLuca Coelho 					profs[i]);
86042ce76d6SLuca Coelho 			/* if one of the profiles is disabled, we fail all */
86142ce76d6SLuca Coelho 			return -ENOENT;
86242ce76d6SLuca Coelho 		}
86342ce76d6SLuca Coelho 
8644fd445a2SHaim Dreyfuss 		IWL_DEBUG_INFO(mvm,
8654fd445a2SHaim Dreyfuss 			       "SAR EWRD: chain %d profile index %d\n",
8664fd445a2SHaim Dreyfuss 			       i, profs[i]);
86742ce76d6SLuca Coelho 		IWL_DEBUG_RADIO(mvm, "  Chain[%d]:\n", i);
868e7a3b8d8SLuca Coelho 		for (j = 0; j < ACPI_SAR_NUM_SUB_BANDS; j++) {
869e7a3b8d8SLuca Coelho 			idx = (i * ACPI_SAR_NUM_SUB_BANDS) + j;
8700791c2fcSHaim Dreyfuss 			cmd.v5.v3.per_chain_restriction[i][j] =
87142ce76d6SLuca Coelho 				cpu_to_le16(prof->table[idx]);
87242ce76d6SLuca Coelho 			IWL_DEBUG_RADIO(mvm, "    Band[%d] = %d * .125dBm\n",
87342ce76d6SLuca Coelho 					j, prof->table[idx]);
87442ce76d6SLuca Coelho 		}
87542ce76d6SLuca Coelho 	}
87642ce76d6SLuca Coelho 
87742ce76d6SLuca Coelho 	IWL_DEBUG_RADIO(mvm, "Sending REDUCE_TX_POWER_CMD per chain\n");
87842ce76d6SLuca Coelho 
87942ce76d6SLuca Coelho 	return iwl_mvm_send_cmd_pdu(mvm, REDUCE_TX_POWER_CMD, 0, len, &cmd);
88042ce76d6SLuca Coelho }
88142ce76d6SLuca Coelho 
8827fe90e0eSHaim Dreyfuss int iwl_mvm_get_sar_geo_profile(struct iwl_mvm *mvm)
8837fe90e0eSHaim Dreyfuss {
8847fe90e0eSHaim Dreyfuss 	struct iwl_geo_tx_power_profiles_resp *resp;
8857fe90e0eSHaim Dreyfuss 	int ret;
8860c3d7282SHaim Dreyfuss 	u16 len;
8870c3d7282SHaim Dreyfuss 	void *data;
8880c3d7282SHaim Dreyfuss 	struct iwl_geo_tx_power_profiles_cmd geo_cmd;
8890c3d7282SHaim Dreyfuss 	struct iwl_geo_tx_power_profiles_cmd_v1 geo_cmd_v1;
8900c3d7282SHaim Dreyfuss 	struct iwl_host_cmd cmd;
8917fe90e0eSHaim Dreyfuss 
8920c3d7282SHaim Dreyfuss 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_SAR_TABLE_VER)) {
8930c3d7282SHaim Dreyfuss 		geo_cmd.ops =
8940c3d7282SHaim Dreyfuss 			cpu_to_le32(IWL_PER_CHAIN_OFFSET_GET_CURRENT_TABLE);
8950c3d7282SHaim Dreyfuss 		len = sizeof(geo_cmd);
8960c3d7282SHaim Dreyfuss 		data = &geo_cmd;
8970c3d7282SHaim Dreyfuss 	} else {
8980c3d7282SHaim Dreyfuss 		geo_cmd_v1.ops =
8990c3d7282SHaim Dreyfuss 			cpu_to_le32(IWL_PER_CHAIN_OFFSET_GET_CURRENT_TABLE);
9000c3d7282SHaim Dreyfuss 		len = sizeof(geo_cmd_v1);
9010c3d7282SHaim Dreyfuss 		data = &geo_cmd_v1;
9020c3d7282SHaim Dreyfuss 	}
9030c3d7282SHaim Dreyfuss 
9040c3d7282SHaim Dreyfuss 	cmd = (struct iwl_host_cmd){
9057fe90e0eSHaim Dreyfuss 		.id =  WIDE_ID(PHY_OPS_GROUP, GEO_TX_POWER_LIMIT),
9060c3d7282SHaim Dreyfuss 		.len = { len, },
9077fe90e0eSHaim Dreyfuss 		.flags = CMD_WANT_SKB,
9080c3d7282SHaim Dreyfuss 		.data = { data },
9097fe90e0eSHaim Dreyfuss 	};
9107fe90e0eSHaim Dreyfuss 
9117fe90e0eSHaim Dreyfuss 	ret = iwl_mvm_send_cmd(mvm, &cmd);
9127fe90e0eSHaim Dreyfuss 	if (ret) {
9137fe90e0eSHaim Dreyfuss 		IWL_ERR(mvm, "Failed to get geographic profile info %d\n", ret);
9147fe90e0eSHaim Dreyfuss 		return ret;
9157fe90e0eSHaim Dreyfuss 	}
9167fe90e0eSHaim Dreyfuss 
9177fe90e0eSHaim Dreyfuss 	resp = (void *)cmd.resp_pkt->data;
9187fe90e0eSHaim Dreyfuss 	ret = le32_to_cpu(resp->profile_idx);
919e7a3b8d8SLuca Coelho 	if (WARN_ON(ret > ACPI_NUM_GEO_PROFILES)) {
9207fe90e0eSHaim Dreyfuss 		ret = -EIO;
9217fe90e0eSHaim Dreyfuss 		IWL_WARN(mvm, "Invalid geographic profile idx (%d)\n", ret);
9227fe90e0eSHaim Dreyfuss 	}
9237fe90e0eSHaim Dreyfuss 
9247fe90e0eSHaim Dreyfuss 	iwl_free_resp(&cmd);
9257fe90e0eSHaim Dreyfuss 	return ret;
9267fe90e0eSHaim Dreyfuss }
9277fe90e0eSHaim Dreyfuss 
928a6bff3cbSHaim Dreyfuss static int iwl_mvm_sar_geo_init(struct iwl_mvm *mvm)
929a6bff3cbSHaim Dreyfuss {
930a6bff3cbSHaim Dreyfuss 	struct iwl_geo_tx_power_profiles_cmd cmd = {
931a6bff3cbSHaim Dreyfuss 		.ops = cpu_to_le32(IWL_PER_CHAIN_OFFSET_SET_TABLES),
932a6bff3cbSHaim Dreyfuss 	};
9337fe90e0eSHaim Dreyfuss 	int ret, i, j;
934a6bff3cbSHaim Dreyfuss 	u16 cmd_wide_id =  WIDE_ID(PHY_OPS_GROUP, GEO_TX_POWER_LIMIT);
935a6bff3cbSHaim Dreyfuss 
936eca1e56cSEmmanuel Grumbach 	/*
937eca1e56cSEmmanuel Grumbach 	 * This command is not supported on earlier firmware versions.
938eca1e56cSEmmanuel Grumbach 	 * Unfortunately, we don't have a TLV API flag to rely on, so
939eca1e56cSEmmanuel Grumbach 	 * rely on the major version which is in the first byte of
940eca1e56cSEmmanuel Grumbach 	 * ucode_ver.
941eca1e56cSEmmanuel Grumbach 	 */
942eca1e56cSEmmanuel Grumbach 	if (IWL_UCODE_SERIAL(mvm->fw->ucode_ver) < 41)
943eca1e56cSEmmanuel Grumbach 		return 0;
944eca1e56cSEmmanuel Grumbach 
9457fe90e0eSHaim Dreyfuss 	ret = iwl_mvm_sar_get_wgds_table(mvm);
946a6bff3cbSHaim Dreyfuss 	if (ret < 0) {
947a6bff3cbSHaim Dreyfuss 		IWL_DEBUG_RADIO(mvm,
948a6bff3cbSHaim Dreyfuss 				"Geo SAR BIOS table invalid or unavailable. (%d)\n",
949a6bff3cbSHaim Dreyfuss 				ret);
950a6bff3cbSHaim Dreyfuss 		/* we don't fail if the table is not available */
951a6bff3cbSHaim Dreyfuss 		return 0;
952a6bff3cbSHaim Dreyfuss 	}
953a6bff3cbSHaim Dreyfuss 
954a6bff3cbSHaim Dreyfuss 	IWL_DEBUG_RADIO(mvm, "Sending GEO_TX_POWER_LIMIT\n");
955a6bff3cbSHaim Dreyfuss 
956e7a3b8d8SLuca Coelho 	BUILD_BUG_ON(ACPI_NUM_GEO_PROFILES * ACPI_WGDS_NUM_BANDS *
95766e83903SMatt Chen 		     ACPI_WGDS_TABLE_SIZE + 1 !=  ACPI_WGDS_WIFI_DATA_SIZE);
958a6bff3cbSHaim Dreyfuss 
959e7a3b8d8SLuca Coelho 	BUILD_BUG_ON(ACPI_NUM_GEO_PROFILES > IWL_NUM_GEO_PROFILES);
960e7a3b8d8SLuca Coelho 
961e7a3b8d8SLuca Coelho 	for (i = 0; i < ACPI_NUM_GEO_PROFILES; i++) {
962a6bff3cbSHaim Dreyfuss 		struct iwl_per_chain_offset *chain =
963a6bff3cbSHaim Dreyfuss 			(struct iwl_per_chain_offset *)&cmd.table[i];
964a6bff3cbSHaim Dreyfuss 
965a6bff3cbSHaim Dreyfuss 		for (j = 0; j < ACPI_WGDS_NUM_BANDS; j++) {
966a6bff3cbSHaim Dreyfuss 			u8 *value;
967a6bff3cbSHaim Dreyfuss 
9687fe90e0eSHaim Dreyfuss 			value = &mvm->geo_profiles[i].values[j *
969e7a3b8d8SLuca Coelho 				ACPI_GEO_PER_CHAIN_SIZE];
970a6bff3cbSHaim Dreyfuss 			chain[j].max_tx_power = cpu_to_le16(value[0]);
971a6bff3cbSHaim Dreyfuss 			chain[j].chain_a = value[1];
972a6bff3cbSHaim Dreyfuss 			chain[j].chain_b = value[2];
973a6bff3cbSHaim Dreyfuss 			IWL_DEBUG_RADIO(mvm,
974a6bff3cbSHaim Dreyfuss 					"SAR geographic profile[%d] Band[%d]: chain A = %d chain B = %d max_tx_power = %d\n",
975a6bff3cbSHaim Dreyfuss 					i, j, value[1], value[2], value[0]);
976a6bff3cbSHaim Dreyfuss 		}
977a6bff3cbSHaim Dreyfuss 	}
9780c3d7282SHaim Dreyfuss 
9790c3d7282SHaim Dreyfuss 	cmd.table_revision = cpu_to_le32(mvm->geo_rev);
9800c3d7282SHaim Dreyfuss 
9810c3d7282SHaim Dreyfuss 	if (!fw_has_api(&mvm->fw->ucode_capa,
9820c3d7282SHaim Dreyfuss 		       IWL_UCODE_TLV_API_SAR_TABLE_VER)) {
9830c3d7282SHaim Dreyfuss 		return iwl_mvm_send_cmd_pdu(mvm, cmd_wide_id, 0,
9840c3d7282SHaim Dreyfuss 				sizeof(struct iwl_geo_tx_power_profiles_cmd_v1),
9850c3d7282SHaim Dreyfuss 				&cmd);
9860c3d7282SHaim Dreyfuss 	}
9870c3d7282SHaim Dreyfuss 
988a6bff3cbSHaim Dreyfuss 	return iwl_mvm_send_cmd_pdu(mvm, cmd_wide_id, 0, sizeof(cmd), &cmd);
989a6bff3cbSHaim Dreyfuss }
990a6bff3cbSHaim Dreyfuss 
99169964905SLuca Coelho #else /* CONFIG_ACPI */
99269964905SLuca Coelho static int iwl_mvm_sar_get_wrds_table(struct iwl_mvm *mvm)
99369964905SLuca Coelho {
99469964905SLuca Coelho 	return -ENOENT;
99569964905SLuca Coelho }
99669964905SLuca Coelho 
99769964905SLuca Coelho static int iwl_mvm_sar_get_ewrd_table(struct iwl_mvm *mvm)
99869964905SLuca Coelho {
99969964905SLuca Coelho 	return -ENOENT;
100069964905SLuca Coelho }
1001a6bff3cbSHaim Dreyfuss 
10025d041c46SLuca Coelho static int iwl_mvm_sar_get_wgds_table(struct iwl_mvm *mvm)
10035d041c46SLuca Coelho {
10045d041c46SLuca Coelho 	return -ENOENT;
10055d041c46SLuca Coelho }
10065d041c46SLuca Coelho 
1007a6bff3cbSHaim Dreyfuss static int iwl_mvm_sar_geo_init(struct iwl_mvm *mvm)
1008a6bff3cbSHaim Dreyfuss {
1009a6bff3cbSHaim Dreyfuss 	return 0;
1010a6bff3cbSHaim Dreyfuss }
101118f1755dSLuca Coelho 
101218f1755dSLuca Coelho int iwl_mvm_sar_select_profile(struct iwl_mvm *mvm, int prof_a,
101318f1755dSLuca Coelho 			       int prof_b)
101418f1755dSLuca Coelho {
101518f1755dSLuca Coelho 	return -ENOENT;
101618f1755dSLuca Coelho }
101718f1755dSLuca Coelho 
101818f1755dSLuca Coelho int iwl_mvm_get_sar_geo_profile(struct iwl_mvm *mvm)
101918f1755dSLuca Coelho {
102018f1755dSLuca Coelho 	return -ENOENT;
102118f1755dSLuca Coelho }
102269964905SLuca Coelho #endif /* CONFIG_ACPI */
102369964905SLuca Coelho 
1024f130bb75SMordechay Goodstein void iwl_mvm_send_recovery_cmd(struct iwl_mvm *mvm, u32 flags)
1025f130bb75SMordechay Goodstein {
1026f130bb75SMordechay Goodstein 	u32 error_log_size = mvm->fw->ucode_capa.error_log_size;
1027f130bb75SMordechay Goodstein 	int ret;
1028f130bb75SMordechay Goodstein 	u32 resp;
1029f130bb75SMordechay Goodstein 
1030f130bb75SMordechay Goodstein 	struct iwl_fw_error_recovery_cmd recovery_cmd = {
1031f130bb75SMordechay Goodstein 		.flags = cpu_to_le32(flags),
1032f130bb75SMordechay Goodstein 		.buf_size = 0,
1033f130bb75SMordechay Goodstein 	};
1034f130bb75SMordechay Goodstein 	struct iwl_host_cmd host_cmd = {
1035f130bb75SMordechay Goodstein 		.id = WIDE_ID(SYSTEM_GROUP, FW_ERROR_RECOVERY_CMD),
1036f130bb75SMordechay Goodstein 		.flags = CMD_WANT_SKB,
1037f130bb75SMordechay Goodstein 		.data = {&recovery_cmd, },
1038f130bb75SMordechay Goodstein 		.len = {sizeof(recovery_cmd), },
1039f130bb75SMordechay Goodstein 	};
1040f130bb75SMordechay Goodstein 
1041f130bb75SMordechay Goodstein 	/* no error log was defined in TLV */
1042f130bb75SMordechay Goodstein 	if (!error_log_size)
1043f130bb75SMordechay Goodstein 		return;
1044f130bb75SMordechay Goodstein 
1045f130bb75SMordechay Goodstein 	if (flags & ERROR_RECOVERY_UPDATE_DB) {
1046f130bb75SMordechay Goodstein 		/* no buf was allocated while HW reset */
1047f130bb75SMordechay Goodstein 		if (!mvm->error_recovery_buf)
1048f130bb75SMordechay Goodstein 			return;
1049f130bb75SMordechay Goodstein 
1050f130bb75SMordechay Goodstein 		host_cmd.data[1] = mvm->error_recovery_buf;
1051f130bb75SMordechay Goodstein 		host_cmd.len[1] =  error_log_size;
1052f130bb75SMordechay Goodstein 		host_cmd.dataflags[1] = IWL_HCMD_DFL_NOCOPY;
1053f130bb75SMordechay Goodstein 		recovery_cmd.buf_size = cpu_to_le32(error_log_size);
1054f130bb75SMordechay Goodstein 	}
1055f130bb75SMordechay Goodstein 
1056f130bb75SMordechay Goodstein 	ret = iwl_mvm_send_cmd(mvm, &host_cmd);
1057f130bb75SMordechay Goodstein 	kfree(mvm->error_recovery_buf);
1058f130bb75SMordechay Goodstein 	mvm->error_recovery_buf = NULL;
1059f130bb75SMordechay Goodstein 
1060f130bb75SMordechay Goodstein 	if (ret) {
1061f130bb75SMordechay Goodstein 		IWL_ERR(mvm, "Failed to send recovery cmd %d\n", ret);
1062f130bb75SMordechay Goodstein 		return;
1063f130bb75SMordechay Goodstein 	}
1064f130bb75SMordechay Goodstein 
1065f130bb75SMordechay Goodstein 	/* skb respond is only relevant in ERROR_RECOVERY_UPDATE_DB */
1066f130bb75SMordechay Goodstein 	if (flags & ERROR_RECOVERY_UPDATE_DB) {
1067f130bb75SMordechay Goodstein 		resp = le32_to_cpu(*(__le32 *)host_cmd.resp_pkt->data);
1068f130bb75SMordechay Goodstein 		if (resp)
1069f130bb75SMordechay Goodstein 			IWL_ERR(mvm,
1070f130bb75SMordechay Goodstein 				"Failed to send recovery cmd blob was invalid %d\n",
1071f130bb75SMordechay Goodstein 				resp);
1072f130bb75SMordechay Goodstein 	}
1073f130bb75SMordechay Goodstein }
1074f130bb75SMordechay Goodstein 
107542ce76d6SLuca Coelho static int iwl_mvm_sar_init(struct iwl_mvm *mvm)
107642ce76d6SLuca Coelho {
107742ce76d6SLuca Coelho 	int ret;
107842ce76d6SLuca Coelho 
1079c386dacbSHaim Dreyfuss 	ret = iwl_mvm_sar_get_wrds_table(mvm);
1080da2830acSLuca Coelho 	if (ret < 0) {
1081da2830acSLuca Coelho 		IWL_DEBUG_RADIO(mvm,
108269964905SLuca Coelho 				"WRDS SAR BIOS table invalid or unavailable. (%d)\n",
1083da2830acSLuca Coelho 				ret);
10845d041c46SLuca Coelho 		/*
10855d041c46SLuca Coelho 		 * If not available, don't fail and don't bother with EWRD.
10865d041c46SLuca Coelho 		 * Return 1 to tell that we can't use WGDS either.
10875d041c46SLuca Coelho 		 */
10885d041c46SLuca Coelho 		return 1;
1089da2830acSLuca Coelho 	}
1090da2830acSLuca Coelho 
109169964905SLuca Coelho 	ret = iwl_mvm_sar_get_ewrd_table(mvm);
109269964905SLuca Coelho 	/* if EWRD is not available, we can still use WRDS, so don't fail */
109369964905SLuca Coelho 	if (ret < 0)
109469964905SLuca Coelho 		IWL_DEBUG_RADIO(mvm,
109569964905SLuca Coelho 				"EWRD SAR BIOS table invalid or unavailable. (%d)\n",
109669964905SLuca Coelho 				ret);
109769964905SLuca Coelho 
109842ce76d6SLuca Coelho 	/* choose profile 1 (WRDS) as default for both chains */
109942ce76d6SLuca Coelho 	ret = iwl_mvm_sar_select_profile(mvm, 1, 1);
110042ce76d6SLuca Coelho 
11015d041c46SLuca Coelho 	/*
11025d041c46SLuca Coelho 	 * If we don't have profile 0 from BIOS, just skip it.  This
11035d041c46SLuca Coelho 	 * means that SAR Geo will not be enabled either, even if we
11045d041c46SLuca Coelho 	 * have other valid profiles.
11055d041c46SLuca Coelho 	 */
110642ce76d6SLuca Coelho 	if (ret == -ENOENT)
11075d041c46SLuca Coelho 		return 1;
1108da2830acSLuca Coelho 
1109da2830acSLuca Coelho 	return ret;
1110da2830acSLuca Coelho }
1111da2830acSLuca Coelho 
11121f370650SSara Sharon static int iwl_mvm_load_rt_fw(struct iwl_mvm *mvm)
11131f370650SSara Sharon {
11141f370650SSara Sharon 	int ret;
11151f370650SSara Sharon 
11167d6222e2SJohannes Berg 	if (iwl_mvm_has_unified_ucode(mvm))
11171f370650SSara Sharon 		return iwl_run_unified_mvm_ucode(mvm, false);
11181f370650SSara Sharon 
11191f370650SSara Sharon 	ret = iwl_run_init_mvm_ucode(mvm, false);
11201f370650SSara Sharon 
11211f370650SSara Sharon 	if (ret) {
11221f370650SSara Sharon 		IWL_ERR(mvm, "Failed to run INIT ucode: %d\n", ret);
1123f4744258SLiad Kaufman 
1124f4744258SLiad Kaufman 		if (iwlmvm_mod_params.init_dbg)
1125f4744258SLiad Kaufman 			return 0;
11261f370650SSara Sharon 		return ret;
11271f370650SSara Sharon 	}
11281f370650SSara Sharon 
11291f370650SSara Sharon 	/*
11301f370650SSara Sharon 	 * Stop and start the transport without entering low power
11311f370650SSara Sharon 	 * mode. This will save the state of other components on the
11321f370650SSara Sharon 	 * device that are triggered by the INIT firwmare (MFUART).
11331f370650SSara Sharon 	 */
11341f370650SSara Sharon 	_iwl_trans_stop_device(mvm->trans, false);
11351f370650SSara Sharon 	ret = _iwl_trans_start_hw(mvm->trans, false);
11361f370650SSara Sharon 	if (ret)
11371f370650SSara Sharon 		return ret;
11381f370650SSara Sharon 
1139da2eb669SSara Sharon 	iwl_fw_dbg_apply_point(&mvm->fwrt, IWL_FW_INI_APPLY_EARLY);
1140da2eb669SSara Sharon 
11411f370650SSara Sharon 	ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_REGULAR);
11421f370650SSara Sharon 	if (ret)
11431f370650SSara Sharon 		return ret;
11441f370650SSara Sharon 
1145da2eb669SSara Sharon 	iwl_fw_dbg_apply_point(&mvm->fwrt, IWL_FW_INI_APPLY_AFTER_ALIVE);
1146da2eb669SSara Sharon 
1147702e975dSJohannes Berg 	return iwl_init_paging(&mvm->fwrt, mvm->fwrt.cur_fw_img);
11481f370650SSara Sharon }
11491f370650SSara Sharon 
1150e705c121SKalle Valo int iwl_mvm_up(struct iwl_mvm *mvm)
1151e705c121SKalle Valo {
1152e705c121SKalle Valo 	int ret, i;
1153e705c121SKalle Valo 	struct ieee80211_channel *chan;
1154e705c121SKalle Valo 	struct cfg80211_chan_def chandef;
1155e705c121SKalle Valo 
1156e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1157e705c121SKalle Valo 
1158e705c121SKalle Valo 	ret = iwl_trans_start_hw(mvm->trans);
1159e705c121SKalle Valo 	if (ret)
1160e705c121SKalle Valo 		return ret;
1161e705c121SKalle Valo 
11621f370650SSara Sharon 	ret = iwl_mvm_load_rt_fw(mvm);
1163e705c121SKalle Valo 	if (ret) {
1164e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to start RT ucode: %d\n", ret);
116572d3c7bbSJohannes Berg 		if (ret != -ERFKILL)
116672d3c7bbSJohannes Berg 			iwl_fw_dbg_error_collect(&mvm->fwrt,
116772d3c7bbSJohannes Berg 						 FW_DBG_TRIGGER_DRIVER);
1168e705c121SKalle Valo 		goto error;
1169e705c121SKalle Valo 	}
1170e705c121SKalle Valo 
1171d0b813fcSJohannes Berg 	iwl_get_shared_mem_conf(&mvm->fwrt);
1172e705c121SKalle Valo 
1173e705c121SKalle Valo 	ret = iwl_mvm_sf_update(mvm, NULL, false);
1174e705c121SKalle Valo 	if (ret)
1175e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to initialize Smart Fifo\n");
1176e705c121SKalle Valo 
11777a14c23dSSara Sharon 	if (!mvm->trans->ini_valid) {
11787174beb6SJohannes Berg 		mvm->fwrt.dump.conf = FW_DBG_INVALID;
1179e705c121SKalle Valo 		/* if we have a destination, assume EARLY START */
118017b809c9SSara Sharon 		if (mvm->fw->dbg.dest_tlv)
11817174beb6SJohannes Berg 			mvm->fwrt.dump.conf = FW_DBG_START_FROM_ALIVE;
11827174beb6SJohannes Berg 		iwl_fw_start_dbg_conf(&mvm->fwrt, FW_DBG_START_FROM_ALIVE);
11837a14c23dSSara Sharon 	}
1184e705c121SKalle Valo 
1185e705c121SKalle Valo 	ret = iwl_send_tx_ant_cfg(mvm, iwl_mvm_get_valid_tx_ant(mvm));
1186e705c121SKalle Valo 	if (ret)
1187e705c121SKalle Valo 		goto error;
1188e705c121SKalle Valo 
11897d6222e2SJohannes Berg 	if (!iwl_mvm_has_unified_ucode(mvm)) {
1190e705c121SKalle Valo 		/* Send phy db control command and then phy db calibration */
1191e705c121SKalle Valo 		ret = iwl_send_phy_db_data(mvm->phy_db);
1192e705c121SKalle Valo 		if (ret)
1193e705c121SKalle Valo 			goto error;
1194e705c121SKalle Valo 
1195e705c121SKalle Valo 		ret = iwl_send_phy_cfg_cmd(mvm);
1196e705c121SKalle Valo 		if (ret)
1197e705c121SKalle Valo 			goto error;
11981f370650SSara Sharon 	}
1199e705c121SKalle Valo 
1200b3de3ef4SEmmanuel Grumbach 	ret = iwl_mvm_send_bt_init_conf(mvm);
1201b3de3ef4SEmmanuel Grumbach 	if (ret)
1202b3de3ef4SEmmanuel Grumbach 		goto error;
1203b3de3ef4SEmmanuel Grumbach 
120443413a97SSara Sharon 	/* Init RSS configuration */
12058edbfaa1SSara Sharon 	if (mvm->trans->cfg->device_family >= IWL_DEVICE_FAMILY_22000) {
12068edbfaa1SSara Sharon 		ret = iwl_configure_rxq(mvm);
12078edbfaa1SSara Sharon 		if (ret) {
12088edbfaa1SSara Sharon 			IWL_ERR(mvm, "Failed to configure RX queues: %d\n",
12098edbfaa1SSara Sharon 				ret);
12108edbfaa1SSara Sharon 			goto error;
12118edbfaa1SSara Sharon 		}
12128edbfaa1SSara Sharon 	}
12138edbfaa1SSara Sharon 
12148edbfaa1SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm)) {
121543413a97SSara Sharon 		ret = iwl_send_rss_cfg_cmd(mvm);
121643413a97SSara Sharon 		if (ret) {
121743413a97SSara Sharon 			IWL_ERR(mvm, "Failed to configure RSS queues: %d\n",
121843413a97SSara Sharon 				ret);
121943413a97SSara Sharon 			goto error;
122043413a97SSara Sharon 		}
122143413a97SSara Sharon 	}
122243413a97SSara Sharon 
1223e705c121SKalle Valo 	/* init the fw <-> mac80211 STA mapping */
12240ae98812SSara Sharon 	for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++)
1225e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->fw_id_to_mac_id[i], NULL);
1226e705c121SKalle Valo 
12270ae98812SSara Sharon 	mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
1228e705c121SKalle Valo 
1229e705c121SKalle Valo 	/* reset quota debouncing buffer - 0xff will yield invalid data */
1230e705c121SKalle Valo 	memset(&mvm->last_quota_cmd, 0xff, sizeof(mvm->last_quota_cmd));
1231e705c121SKalle Valo 
123297d5be7eSLiad Kaufman 	ret = iwl_mvm_send_dqa_cmd(mvm);
123397d5be7eSLiad Kaufman 	if (ret)
123497d5be7eSLiad Kaufman 		goto error;
123597d5be7eSLiad Kaufman 
1236e705c121SKalle Valo 	/* Add auxiliary station for scanning */
1237e705c121SKalle Valo 	ret = iwl_mvm_add_aux_sta(mvm);
1238e705c121SKalle Valo 	if (ret)
1239e705c121SKalle Valo 		goto error;
1240e705c121SKalle Valo 
1241e705c121SKalle Valo 	/* Add all the PHY contexts */
124257fbcce3SJohannes Berg 	chan = &mvm->hw->wiphy->bands[NL80211_BAND_2GHZ]->channels[0];
1243e705c121SKalle Valo 	cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
1244e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++) {
1245e705c121SKalle Valo 		/*
1246e705c121SKalle Valo 		 * The channel used here isn't relevant as it's
1247e705c121SKalle Valo 		 * going to be overwritten in the other flows.
1248e705c121SKalle Valo 		 * For now use the first channel we have.
1249e705c121SKalle Valo 		 */
1250e705c121SKalle Valo 		ret = iwl_mvm_phy_ctxt_add(mvm, &mvm->phy_ctxts[i],
1251e705c121SKalle Valo 					   &chandef, 1, 1);
1252e705c121SKalle Valo 		if (ret)
1253e705c121SKalle Valo 			goto error;
1254e705c121SKalle Valo 	}
1255e705c121SKalle Valo 
1256c221daf2SChaya Rachel Ivgi #ifdef CONFIG_THERMAL
1257c221daf2SChaya Rachel Ivgi 	if (iwl_mvm_is_tt_in_fw(mvm)) {
1258c221daf2SChaya Rachel Ivgi 		/* in order to give the responsibility of ct-kill and
1259c221daf2SChaya Rachel Ivgi 		 * TX backoff to FW we need to send empty temperature reporting
1260c221daf2SChaya Rachel Ivgi 		 * cmd during init time
1261c221daf2SChaya Rachel Ivgi 		 */
1262c221daf2SChaya Rachel Ivgi 		iwl_mvm_send_temp_report_ths_cmd(mvm);
1263c221daf2SChaya Rachel Ivgi 	} else {
1264e705c121SKalle Valo 		/* Initialize tx backoffs to the minimal possible */
1265e705c121SKalle Valo 		iwl_mvm_tt_tx_backoff(mvm, 0);
1266c221daf2SChaya Rachel Ivgi 	}
12675c89e7bcSChaya Rachel Ivgi 
12685c89e7bcSChaya Rachel Ivgi 	/* TODO: read the budget from BIOS / Platform NVM */
1269944eafc2SChaya Rachel Ivgi 
1270944eafc2SChaya Rachel Ivgi 	/*
1271944eafc2SChaya Rachel Ivgi 	 * In case there is no budget from BIOS / Platform NVM the default
1272944eafc2SChaya Rachel Ivgi 	 * budget should be 2000mW (cooling state 0).
1273944eafc2SChaya Rachel Ivgi 	 */
1274944eafc2SChaya Rachel Ivgi 	if (iwl_mvm_is_ctdp_supported(mvm)) {
12755c89e7bcSChaya Rachel Ivgi 		ret = iwl_mvm_ctdp_command(mvm, CTDP_CMD_OPERATION_START,
12765c89e7bcSChaya Rachel Ivgi 					   mvm->cooling_dev.cur_state);
127775cfe338SLuca Coelho 		if (ret)
127875cfe338SLuca Coelho 			goto error;
127975cfe338SLuca Coelho 	}
1280c221daf2SChaya Rachel Ivgi #else
1281c221daf2SChaya Rachel Ivgi 	/* Initialize tx backoffs to the minimal possible */
1282c221daf2SChaya Rachel Ivgi 	iwl_mvm_tt_tx_backoff(mvm, 0);
1283c221daf2SChaya Rachel Ivgi #endif
1284e705c121SKalle Valo 
1285e705c121SKalle Valo 	WARN_ON(iwl_mvm_config_ltr(mvm));
1286e705c121SKalle Valo 
1287e705c121SKalle Valo 	ret = iwl_mvm_power_update_device(mvm);
1288e705c121SKalle Valo 	if (ret)
1289e705c121SKalle Valo 		goto error;
1290e705c121SKalle Valo 
1291e705c121SKalle Valo 	/*
1292e705c121SKalle Valo 	 * RTNL is not taken during Ct-kill, but we don't need to scan/Tx
1293e705c121SKalle Valo 	 * anyway, so don't init MCC.
1294e705c121SKalle Valo 	 */
1295e705c121SKalle Valo 	if (!test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status)) {
1296e705c121SKalle Valo 		ret = iwl_mvm_init_mcc(mvm);
1297e705c121SKalle Valo 		if (ret)
1298e705c121SKalle Valo 			goto error;
1299e705c121SKalle Valo 	}
1300e705c121SKalle Valo 
1301e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
13024ca87a5fSEmmanuel Grumbach 		mvm->scan_type = IWL_SCAN_TYPE_NOT_SET;
1303b66b5817SSara Sharon 		mvm->hb_scan_type = IWL_SCAN_TYPE_NOT_SET;
1304e705c121SKalle Valo 		ret = iwl_mvm_config_scan(mvm);
1305e705c121SKalle Valo 		if (ret)
1306e705c121SKalle Valo 			goto error;
1307e705c121SKalle Valo 	}
1308e705c121SKalle Valo 
1309e705c121SKalle Valo 	/* allow FW/transport low power modes if not during restart */
1310e705c121SKalle Valo 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1311e705c121SKalle Valo 		iwl_mvm_unref(mvm, IWL_MVM_REF_UCODE_DOWN);
1312e705c121SKalle Valo 
1313f130bb75SMordechay Goodstein 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1314f130bb75SMordechay Goodstein 		iwl_mvm_send_recovery_cmd(mvm, ERROR_RECOVERY_UPDATE_DB);
1315f130bb75SMordechay Goodstein 
131648e775e6SHaim Dreyfuss 	if (iwl_acpi_get_eckv(mvm->dev, &mvm->ext_clock_valid))
131748e775e6SHaim Dreyfuss 		IWL_DEBUG_INFO(mvm, "ECKV table doesn't exist in BIOS\n");
131848e775e6SHaim Dreyfuss 
1319da2830acSLuca Coelho 	ret = iwl_mvm_sar_init(mvm);
13205d041c46SLuca Coelho 	if (ret == 0) {
1321a6bff3cbSHaim Dreyfuss 		ret = iwl_mvm_sar_geo_init(mvm);
13225d041c46SLuca Coelho 	} else if (ret > 0 && !iwl_mvm_sar_get_wgds_table(mvm)) {
13235d041c46SLuca Coelho 		/*
13245d041c46SLuca Coelho 		 * If basic SAR is not available, we check for WGDS,
13255d041c46SLuca Coelho 		 * which should *not* be available either.  If it is
13265d041c46SLuca Coelho 		 * available, issue an error, because we can't use SAR
13275d041c46SLuca Coelho 		 * Geo without basic SAR.
13285d041c46SLuca Coelho 		 */
13295d041c46SLuca Coelho 		IWL_ERR(mvm, "BIOS contains WGDS but no WRDS\n");
13305d041c46SLuca Coelho 	}
13315d041c46SLuca Coelho 
13325d041c46SLuca Coelho 	if (ret < 0)
1333a6bff3cbSHaim Dreyfuss 		goto error;
1334a6bff3cbSHaim Dreyfuss 
13357089ae63SJohannes Berg 	iwl_mvm_leds_sync(mvm);
13367089ae63SJohannes Berg 
1337e705c121SKalle Valo 	IWL_DEBUG_INFO(mvm, "RT uCode started.\n");
1338e705c121SKalle Valo 	return 0;
1339e705c121SKalle Valo  error:
1340f4744258SLiad Kaufman 	if (!iwlmvm_mod_params.init_dbg || !ret)
1341fcb6b92aSChaya Rachel Ivgi 		iwl_mvm_stop_device(mvm);
1342e705c121SKalle Valo 	return ret;
1343e705c121SKalle Valo }
1344e705c121SKalle Valo 
1345e705c121SKalle Valo int iwl_mvm_load_d3_fw(struct iwl_mvm *mvm)
1346e705c121SKalle Valo {
1347e705c121SKalle Valo 	int ret, i;
1348e705c121SKalle Valo 
1349e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1350e705c121SKalle Valo 
1351e705c121SKalle Valo 	ret = iwl_trans_start_hw(mvm->trans);
1352e705c121SKalle Valo 	if (ret)
1353e705c121SKalle Valo 		return ret;
1354e705c121SKalle Valo 
1355e705c121SKalle Valo 	ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_WOWLAN);
1356e705c121SKalle Valo 	if (ret) {
1357e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to start WoWLAN firmware: %d\n", ret);
1358e705c121SKalle Valo 		goto error;
1359e705c121SKalle Valo 	}
1360e705c121SKalle Valo 
1361e705c121SKalle Valo 	ret = iwl_send_tx_ant_cfg(mvm, iwl_mvm_get_valid_tx_ant(mvm));
1362e705c121SKalle Valo 	if (ret)
1363e705c121SKalle Valo 		goto error;
1364e705c121SKalle Valo 
1365e705c121SKalle Valo 	/* Send phy db control command and then phy db calibration*/
1366e705c121SKalle Valo 	ret = iwl_send_phy_db_data(mvm->phy_db);
1367e705c121SKalle Valo 	if (ret)
1368e705c121SKalle Valo 		goto error;
1369e705c121SKalle Valo 
1370e705c121SKalle Valo 	ret = iwl_send_phy_cfg_cmd(mvm);
1371e705c121SKalle Valo 	if (ret)
1372e705c121SKalle Valo 		goto error;
1373e705c121SKalle Valo 
1374e705c121SKalle Valo 	/* init the fw <-> mac80211 STA mapping */
13750ae98812SSara Sharon 	for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++)
1376e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->fw_id_to_mac_id[i], NULL);
1377e705c121SKalle Valo 
1378e705c121SKalle Valo 	/* Add auxiliary station for scanning */
1379e705c121SKalle Valo 	ret = iwl_mvm_add_aux_sta(mvm);
1380e705c121SKalle Valo 	if (ret)
1381e705c121SKalle Valo 		goto error;
1382e705c121SKalle Valo 
1383e705c121SKalle Valo 	return 0;
1384e705c121SKalle Valo  error:
1385fcb6b92aSChaya Rachel Ivgi 	iwl_mvm_stop_device(mvm);
1386e705c121SKalle Valo 	return ret;
1387e705c121SKalle Valo }
1388e705c121SKalle Valo 
1389e705c121SKalle Valo void iwl_mvm_rx_card_state_notif(struct iwl_mvm *mvm,
1390e705c121SKalle Valo 				 struct iwl_rx_cmd_buffer *rxb)
1391e705c121SKalle Valo {
1392e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1393e705c121SKalle Valo 	struct iwl_card_state_notif *card_state_notif = (void *)pkt->data;
1394e705c121SKalle Valo 	u32 flags = le32_to_cpu(card_state_notif->flags);
1395e705c121SKalle Valo 
1396e705c121SKalle Valo 	IWL_DEBUG_RF_KILL(mvm, "Card state received: HW:%s SW:%s CT:%s\n",
1397e705c121SKalle Valo 			  (flags & HW_CARD_DISABLED) ? "Kill" : "On",
1398e705c121SKalle Valo 			  (flags & SW_CARD_DISABLED) ? "Kill" : "On",
1399e705c121SKalle Valo 			  (flags & CT_KILL_CARD_DISABLED) ?
1400e705c121SKalle Valo 			  "Reached" : "Not reached");
1401e705c121SKalle Valo }
1402e705c121SKalle Valo 
1403e705c121SKalle Valo void iwl_mvm_rx_mfuart_notif(struct iwl_mvm *mvm,
1404e705c121SKalle Valo 			     struct iwl_rx_cmd_buffer *rxb)
1405e705c121SKalle Valo {
1406e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1407e705c121SKalle Valo 	struct iwl_mfuart_load_notif *mfuart_notif = (void *)pkt->data;
1408e705c121SKalle Valo 
1409e705c121SKalle Valo 	IWL_DEBUG_INFO(mvm,
1410e705c121SKalle Valo 		       "MFUART: installed ver: 0x%08x, external ver: 0x%08x, status: 0x%08x, duration: 0x%08x\n",
1411e705c121SKalle Valo 		       le32_to_cpu(mfuart_notif->installed_ver),
1412e705c121SKalle Valo 		       le32_to_cpu(mfuart_notif->external_ver),
1413e705c121SKalle Valo 		       le32_to_cpu(mfuart_notif->status),
1414e705c121SKalle Valo 		       le32_to_cpu(mfuart_notif->duration));
14150c8d0a47SGolan Ben-Ami 
14160c8d0a47SGolan Ben-Ami 	if (iwl_rx_packet_payload_len(pkt) == sizeof(*mfuart_notif))
14170c8d0a47SGolan Ben-Ami 		IWL_DEBUG_INFO(mvm,
14180c8d0a47SGolan Ben-Ami 			       "MFUART: image size: 0x%08x\n",
14190c8d0a47SGolan Ben-Ami 			       le32_to_cpu(mfuart_notif->image_size));
1420e705c121SKalle Valo }
1421