• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
iwl_send_tx_ant_cfg(struct iwl_mvm * mvm,u8 valid_tx_ant)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 
iwl_send_rss_cfg_cmd(struct iwl_mvm * mvm)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 
iwl_configure_rxq(struct iwl_mvm * mvm)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 
iwl_mvm_send_dqa_cmd(struct iwl_mvm * mvm)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 
iwl_mvm_mfu_assert_dump_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)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 
iwl_alive_fn(struct iwl_notif_wait_data * notif_wait,struct iwl_rx_packet * pkt,void * data)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 
iwl_wait_init_complete(struct iwl_notif_wait_data * notif_wait,struct iwl_rx_packet * pkt,void * data)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 
iwl_wait_phy_db_entry(struct iwl_notif_wait_data * notif_wait,struct iwl_rx_packet * pkt,void * data)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 
iwl_mvm_load_ucode_wait_alive(struct iwl_mvm * mvm,enum iwl_ucode_type ucode_type)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 
iwl_run_unified_mvm_ucode(struct iwl_mvm * mvm,bool read_nvm)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
iwl_mvm_phy_filter_init(struct iwl_mvm * mvm,struct iwl_phy_specific_cfg * phy_filters)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 
iwl_mvm_phy_filter_init(struct iwl_mvm * mvm,struct iwl_phy_specific_cfg * phy_filters)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 
iwl_send_phy_cfg_cmd(struct iwl_mvm * mvm)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 
iwl_run_init_mvm_ucode(struct iwl_mvm * mvm,bool read_nvm)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 
iwl_mvm_config_ltr(struct iwl_mvm * mvm)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
iwl_mvm_sar_select_profile(struct iwl_mvm * mvm,int prof_a,int prof_b)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 
iwl_mvm_get_sar_geo_profile(struct iwl_mvm * mvm)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 
iwl_mvm_sar_geo_init(struct iwl_mvm * mvm)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 	} else if (fw_has_api(&mvm->fwrt.fw->ucode_capa,
900 			      IWL_UCODE_TLV_API_SAR_TABLE_VER)) {
901 		len = sizeof(cmd.v2);
902 		n_bands = ARRAY_SIZE(cmd.v2.table[0]);
903 	} else {
904 		len = sizeof(cmd.v1);
905 		n_bands = ARRAY_SIZE(cmd.v1.table[0]);
906 	}
907 
908 	BUILD_BUG_ON(offsetof(struct iwl_geo_tx_power_profiles_cmd_v1, table) !=
909 		     offsetof(struct iwl_geo_tx_power_profiles_cmd_v2, table) ||
910 		     offsetof(struct iwl_geo_tx_power_profiles_cmd_v2, table) !=
911 		     offsetof(struct iwl_geo_tx_power_profiles_cmd_v3, table));
912 	/* the table is at the same position for all versions, so set use v1 */
913 	ret = iwl_sar_geo_init(&mvm->fwrt, &cmd.v1.table[0][0], n_bands);
914 
915 	/*
916 	 * It is a valid scenario to not support SAR, or miss wgds table,
917 	 * but in that case there is no need to send the command.
918 	 */
919 	if (ret)
920 		return 0;
921 
922 	/*
923 	 * Set the revision on versions that contain it.
924 	 * This must be done after calling iwl_sar_geo_init().
925 	 */
926 	if (cmd_ver == 3)
927 		cmd.v3.table_revision = cpu_to_le32(mvm->fwrt.geo_rev);
928 	else if (fw_has_api(&mvm->fwrt.fw->ucode_capa,
929 			    IWL_UCODE_TLV_API_SAR_TABLE_VER))
930 		cmd.v2.table_revision = cpu_to_le32(mvm->fwrt.geo_rev);
931 
932 	return iwl_mvm_send_cmd_pdu(mvm,
933 				    WIDE_ID(PHY_OPS_GROUP, GEO_TX_POWER_LIMIT),
934 				    0, len, &cmd);
935 }
936 
iwl_mvm_get_ppag_table(struct iwl_mvm * mvm)937 static int iwl_mvm_get_ppag_table(struct iwl_mvm *mvm)
938 {
939 	union acpi_object *wifi_pkg, *data, *enabled;
940 	int i, j, ret, tbl_rev, num_sub_bands;
941 	int idx = 2;
942 	s8 *gain;
943 
944 	/*
945 	 * The 'enabled' field is the same in v1 and v2 so we can just
946 	 * use v1 to access it.
947 	 */
948 	mvm->fwrt.ppag_table.v1.enabled = cpu_to_le32(0);
949 	data = iwl_acpi_get_object(mvm->dev, ACPI_PPAG_METHOD);
950 	if (IS_ERR(data))
951 		return PTR_ERR(data);
952 
953 	/* try to read ppag table revision 1 */
954 	wifi_pkg = iwl_acpi_get_wifi_pkg(mvm->dev, data,
955 					 ACPI_PPAG_WIFI_DATA_SIZE_V2, &tbl_rev);
956 	if (!IS_ERR(wifi_pkg)) {
957 		if (tbl_rev != 1) {
958 			ret = -EINVAL;
959 			goto out_free;
960 		}
961 		num_sub_bands = IWL_NUM_SUB_BANDS_V2;
962 		gain = mvm->fwrt.ppag_table.v2.gain[0];
963 		mvm->fwrt.ppag_ver = 2;
964 		IWL_DEBUG_RADIO(mvm, "Reading PPAG table v2 (tbl_rev=1)\n");
965 		goto read_table;
966 	}
967 
968 	/* try to read ppag table revision 0 */
969 	wifi_pkg = iwl_acpi_get_wifi_pkg(mvm->dev, data,
970 					 ACPI_PPAG_WIFI_DATA_SIZE, &tbl_rev);
971 	if (!IS_ERR(wifi_pkg)) {
972 		if (tbl_rev != 0) {
973 			ret = -EINVAL;
974 			goto out_free;
975 		}
976 		num_sub_bands = IWL_NUM_SUB_BANDS;
977 		gain = mvm->fwrt.ppag_table.v1.gain[0];
978 		mvm->fwrt.ppag_ver = 1;
979 		IWL_DEBUG_RADIO(mvm, "Reading PPAG table v1 (tbl_rev=0)\n");
980 		goto read_table;
981 	}
982 	ret = PTR_ERR(wifi_pkg);
983 	goto out_free;
984 
985 read_table:
986 	enabled = &wifi_pkg->package.elements[1];
987 	if (enabled->type != ACPI_TYPE_INTEGER ||
988 	    (enabled->integer.value != 0 && enabled->integer.value != 1)) {
989 		ret = -EINVAL;
990 		goto out_free;
991 	}
992 
993 	mvm->fwrt.ppag_table.v1.enabled = cpu_to_le32(enabled->integer.value);
994 	if (!mvm->fwrt.ppag_table.v1.enabled) {
995 		ret = 0;
996 		goto out_free;
997 	}
998 
999 	/*
1000 	 * read, verify gain values and save them into the PPAG table.
1001 	 * first sub-band (j=0) corresponds to Low-Band (2.4GHz), and the
1002 	 * following sub-bands to High-Band (5GHz).
1003 	 */
1004 	for (i = 0; i < IWL_NUM_CHAIN_LIMITS; i++) {
1005 		for (j = 0; j < num_sub_bands; j++) {
1006 			union acpi_object *ent;
1007 
1008 			ent = &wifi_pkg->package.elements[idx++];
1009 			if (ent->type != ACPI_TYPE_INTEGER) {
1010 				ret = -EINVAL;
1011 				goto out_free;
1012 			}
1013 
1014 			gain[i * num_sub_bands + j] = ent->integer.value;
1015 
1016 			if ((j == 0 &&
1017 			     (gain[i * num_sub_bands + j] > ACPI_PPAG_MAX_LB ||
1018 			      gain[i * num_sub_bands + j] < ACPI_PPAG_MIN_LB)) ||
1019 			    (j != 0 &&
1020 			     (gain[i * num_sub_bands + j] > ACPI_PPAG_MAX_HB ||
1021 			      gain[i * num_sub_bands + j] < ACPI_PPAG_MIN_HB))) {
1022 				mvm->fwrt.ppag_table.v1.enabled = cpu_to_le32(0);
1023 				ret = -EINVAL;
1024 				goto out_free;
1025 			}
1026 		}
1027 	}
1028 	ret = 0;
1029 out_free:
1030 	kfree(data);
1031 	return ret;
1032 }
1033 
iwl_mvm_ppag_send_cmd(struct iwl_mvm * mvm)1034 int iwl_mvm_ppag_send_cmd(struct iwl_mvm *mvm)
1035 {
1036 	u8 cmd_ver;
1037 	int i, j, ret, num_sub_bands, cmd_size;
1038 	s8 *gain;
1039 
1040 	if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_SET_PPAG)) {
1041 		IWL_DEBUG_RADIO(mvm,
1042 				"PPAG capability not supported by FW, command not sent.\n");
1043 		return 0;
1044 	}
1045 	if (!mvm->fwrt.ppag_table.v1.enabled) {
1046 		IWL_DEBUG_RADIO(mvm, "PPAG not enabled, command not sent.\n");
1047 		return 0;
1048 	}
1049 
1050 	cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, PHY_OPS_GROUP,
1051 					PER_PLATFORM_ANT_GAIN_CMD,
1052 					IWL_FW_CMD_VER_UNKNOWN);
1053 	if (cmd_ver == 1) {
1054 		num_sub_bands = IWL_NUM_SUB_BANDS;
1055 		gain = mvm->fwrt.ppag_table.v1.gain[0];
1056 		cmd_size = sizeof(mvm->fwrt.ppag_table.v1);
1057 		if (mvm->fwrt.ppag_ver == 2) {
1058 			IWL_DEBUG_RADIO(mvm,
1059 					"PPAG table is v2 but FW supports v1, sending truncated table\n");
1060 		}
1061 	} else if (cmd_ver == 2) {
1062 		num_sub_bands = IWL_NUM_SUB_BANDS_V2;
1063 		gain = mvm->fwrt.ppag_table.v2.gain[0];
1064 		cmd_size = sizeof(mvm->fwrt.ppag_table.v2);
1065 		if (mvm->fwrt.ppag_ver == 1) {
1066 			IWL_DEBUG_RADIO(mvm,
1067 					"PPAG table is v1 but FW supports v2, sending padded table\n");
1068 		}
1069 	} else {
1070 		IWL_DEBUG_RADIO(mvm, "Unsupported PPAG command version\n");
1071 		return 0;
1072 	}
1073 
1074 	for (i = 0; i < IWL_NUM_CHAIN_LIMITS; i++) {
1075 		for (j = 0; j < num_sub_bands; j++) {
1076 			IWL_DEBUG_RADIO(mvm,
1077 					"PPAG table: chain[%d] band[%d]: gain = %d\n",
1078 					i, j, gain[i * num_sub_bands + j]);
1079 		}
1080 	}
1081 	IWL_DEBUG_RADIO(mvm, "Sending PER_PLATFORM_ANT_GAIN_CMD\n");
1082 	ret = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(PHY_OPS_GROUP,
1083 						PER_PLATFORM_ANT_GAIN_CMD),
1084 				   0, cmd_size, &mvm->fwrt.ppag_table);
1085 	if (ret < 0)
1086 		IWL_ERR(mvm, "failed to send PER_PLATFORM_ANT_GAIN_CMD (%d)\n",
1087 			ret);
1088 
1089 	return ret;
1090 }
1091 
iwl_mvm_ppag_init(struct iwl_mvm * mvm)1092 static int iwl_mvm_ppag_init(struct iwl_mvm *mvm)
1093 {
1094 	int ret;
1095 
1096 	ret = iwl_mvm_get_ppag_table(mvm);
1097 	if (ret < 0) {
1098 		IWL_DEBUG_RADIO(mvm,
1099 				"PPAG BIOS table invalid or unavailable. (%d)\n",
1100 				ret);
1101 		return 0;
1102 	}
1103 	return iwl_mvm_ppag_send_cmd(mvm);
1104 }
1105 
iwl_mvm_tas_init(struct iwl_mvm * mvm)1106 static void iwl_mvm_tas_init(struct iwl_mvm *mvm)
1107 {
1108 	int ret;
1109 	struct iwl_tas_config_cmd cmd = {};
1110 	int list_size;
1111 
1112 	BUILD_BUG_ON(ARRAY_SIZE(cmd.block_list_array) <
1113 		     APCI_WTAS_BLACK_LIST_MAX);
1114 
1115 	if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_TAS_CFG)) {
1116 		IWL_DEBUG_RADIO(mvm, "TAS not enabled in FW\n");
1117 		return;
1118 	}
1119 
1120 	ret = iwl_acpi_get_tas(&mvm->fwrt, cmd.block_list_array, &list_size);
1121 	if (ret < 0) {
1122 		IWL_DEBUG_RADIO(mvm,
1123 				"TAS table invalid or unavailable. (%d)\n",
1124 				ret);
1125 		return;
1126 	}
1127 
1128 	if (list_size < 0)
1129 		return;
1130 
1131 	/* list size if TAS enabled can only be non-negative */
1132 	cmd.block_list_size = cpu_to_le32((u32)list_size);
1133 
1134 	ret = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(REGULATORY_AND_NVM_GROUP,
1135 						TAS_CONFIG),
1136 				   0, sizeof(cmd), &cmd);
1137 	if (ret < 0)
1138 		IWL_DEBUG_RADIO(mvm, "failed to send TAS_CONFIG (%d)\n", ret);
1139 }
1140 
iwl_mvm_eval_dsm_indonesia_5g2(struct iwl_mvm * mvm)1141 static u8 iwl_mvm_eval_dsm_indonesia_5g2(struct iwl_mvm *mvm)
1142 {
1143 	int ret = iwl_acpi_get_dsm_u8((&mvm->fwrt)->dev, 0,
1144 				      DSM_FUNC_ENABLE_INDONESIA_5G2);
1145 
1146 	if (ret < 0)
1147 		IWL_DEBUG_RADIO(mvm,
1148 				"Failed to evaluate DSM function ENABLE_INDONESIA_5G2, ret=%d\n",
1149 				ret);
1150 
1151 	else if (ret >= DSM_VALUE_INDONESIA_MAX)
1152 		IWL_DEBUG_RADIO(mvm,
1153 				"DSM function ENABLE_INDONESIA_5G2 return invalid value, ret=%d\n",
1154 				ret);
1155 
1156 	else if (ret == DSM_VALUE_INDONESIA_ENABLE) {
1157 		IWL_DEBUG_RADIO(mvm,
1158 				"Evaluated DSM function ENABLE_INDONESIA_5G2: Enabling 5g2\n");
1159 		return DSM_VALUE_INDONESIA_ENABLE;
1160 	}
1161 	/* default behaviour is disabled */
1162 	return DSM_VALUE_INDONESIA_DISABLE;
1163 }
1164 
iwl_mvm_eval_dsm_disable_srd(struct iwl_mvm * mvm)1165 static u8 iwl_mvm_eval_dsm_disable_srd(struct iwl_mvm *mvm)
1166 {
1167 	int ret = iwl_acpi_get_dsm_u8((&mvm->fwrt)->dev, 0,
1168 				      DSM_FUNC_DISABLE_SRD);
1169 
1170 	if (ret < 0)
1171 		IWL_DEBUG_RADIO(mvm,
1172 				"Failed to evaluate DSM function DISABLE_SRD, ret=%d\n",
1173 				ret);
1174 
1175 	else if (ret >= DSM_VALUE_SRD_MAX)
1176 		IWL_DEBUG_RADIO(mvm,
1177 				"DSM function DISABLE_SRD return invalid value, ret=%d\n",
1178 				ret);
1179 
1180 	else if (ret == DSM_VALUE_SRD_PASSIVE) {
1181 		IWL_DEBUG_RADIO(mvm,
1182 				"Evaluated DSM function DISABLE_SRD: setting SRD to passive\n");
1183 		return DSM_VALUE_SRD_PASSIVE;
1184 
1185 	} else if (ret == DSM_VALUE_SRD_DISABLE) {
1186 		IWL_DEBUG_RADIO(mvm,
1187 				"Evaluated DSM function DISABLE_SRD: disabling SRD\n");
1188 		return DSM_VALUE_SRD_DISABLE;
1189 	}
1190 	/* default behaviour is active */
1191 	return DSM_VALUE_SRD_ACTIVE;
1192 }
1193 
iwl_mvm_lari_cfg(struct iwl_mvm * mvm)1194 static void iwl_mvm_lari_cfg(struct iwl_mvm *mvm)
1195 {
1196 	u8 ret;
1197 	int cmd_ret;
1198 	struct iwl_lari_config_change_cmd cmd = {};
1199 
1200 	if (iwl_mvm_eval_dsm_indonesia_5g2(mvm) == DSM_VALUE_INDONESIA_ENABLE)
1201 		cmd.config_bitmap |=
1202 			cpu_to_le32(LARI_CONFIG_ENABLE_5G2_IN_INDONESIA_MSK);
1203 
1204 	ret = iwl_mvm_eval_dsm_disable_srd(mvm);
1205 	if (ret == DSM_VALUE_SRD_PASSIVE)
1206 		cmd.config_bitmap |=
1207 			cpu_to_le32(LARI_CONFIG_CHANGE_ETSI_TO_PASSIVE_MSK);
1208 
1209 	else if (ret == DSM_VALUE_SRD_DISABLE)
1210 		cmd.config_bitmap |=
1211 			cpu_to_le32(LARI_CONFIG_CHANGE_ETSI_TO_DISABLED_MSK);
1212 
1213 	/* apply more config masks here */
1214 
1215 	if (cmd.config_bitmap) {
1216 		IWL_DEBUG_RADIO(mvm, "sending LARI_CONFIG_CHANGE\n");
1217 		cmd_ret = iwl_mvm_send_cmd_pdu(mvm,
1218 					       WIDE_ID(REGULATORY_AND_NVM_GROUP,
1219 						       LARI_CONFIG_CHANGE),
1220 					       0, sizeof(cmd), &cmd);
1221 		if (cmd_ret < 0)
1222 			IWL_DEBUG_RADIO(mvm,
1223 					"Failed to send LARI_CONFIG_CHANGE (%d)\n",
1224 					cmd_ret);
1225 	}
1226 }
1227 #else /* CONFIG_ACPI */
1228 
iwl_mvm_sar_select_profile(struct iwl_mvm * mvm,int prof_a,int prof_b)1229 inline int iwl_mvm_sar_select_profile(struct iwl_mvm *mvm,
1230 				      int prof_a, int prof_b)
1231 {
1232 	return -ENOENT;
1233 }
1234 
iwl_mvm_get_sar_geo_profile(struct iwl_mvm * mvm)1235 inline int iwl_mvm_get_sar_geo_profile(struct iwl_mvm *mvm)
1236 {
1237 	return -ENOENT;
1238 }
1239 
iwl_mvm_sar_geo_init(struct iwl_mvm * mvm)1240 static int iwl_mvm_sar_geo_init(struct iwl_mvm *mvm)
1241 {
1242 	return 0;
1243 }
1244 
iwl_mvm_ppag_send_cmd(struct iwl_mvm * mvm)1245 int iwl_mvm_ppag_send_cmd(struct iwl_mvm *mvm)
1246 {
1247 	return -ENOENT;
1248 }
1249 
iwl_mvm_ppag_init(struct iwl_mvm * mvm)1250 static int iwl_mvm_ppag_init(struct iwl_mvm *mvm)
1251 {
1252 	return 0;
1253 }
1254 
iwl_mvm_tas_init(struct iwl_mvm * mvm)1255 static void iwl_mvm_tas_init(struct iwl_mvm *mvm)
1256 {
1257 }
1258 
iwl_mvm_lari_cfg(struct iwl_mvm * mvm)1259 static void iwl_mvm_lari_cfg(struct iwl_mvm *mvm)
1260 {
1261 }
1262 #endif /* CONFIG_ACPI */
1263 
iwl_mvm_send_recovery_cmd(struct iwl_mvm * mvm,u32 flags)1264 void iwl_mvm_send_recovery_cmd(struct iwl_mvm *mvm, u32 flags)
1265 {
1266 	u32 error_log_size = mvm->fw->ucode_capa.error_log_size;
1267 	int ret;
1268 	u32 resp;
1269 
1270 	struct iwl_fw_error_recovery_cmd recovery_cmd = {
1271 		.flags = cpu_to_le32(flags),
1272 		.buf_size = 0,
1273 	};
1274 	struct iwl_host_cmd host_cmd = {
1275 		.id = WIDE_ID(SYSTEM_GROUP, FW_ERROR_RECOVERY_CMD),
1276 		.flags = CMD_WANT_SKB,
1277 		.data = {&recovery_cmd, },
1278 		.len = {sizeof(recovery_cmd), },
1279 	};
1280 
1281 	/* no error log was defined in TLV */
1282 	if (!error_log_size)
1283 		return;
1284 
1285 	if (flags & ERROR_RECOVERY_UPDATE_DB) {
1286 		/* no buf was allocated while HW reset */
1287 		if (!mvm->error_recovery_buf)
1288 			return;
1289 
1290 		host_cmd.data[1] = mvm->error_recovery_buf;
1291 		host_cmd.len[1] =  error_log_size;
1292 		host_cmd.dataflags[1] = IWL_HCMD_DFL_NOCOPY;
1293 		recovery_cmd.buf_size = cpu_to_le32(error_log_size);
1294 	}
1295 
1296 	ret = iwl_mvm_send_cmd(mvm, &host_cmd);
1297 	kfree(mvm->error_recovery_buf);
1298 	mvm->error_recovery_buf = NULL;
1299 
1300 	if (ret) {
1301 		IWL_ERR(mvm, "Failed to send recovery cmd %d\n", ret);
1302 		return;
1303 	}
1304 
1305 	/* skb respond is only relevant in ERROR_RECOVERY_UPDATE_DB */
1306 	if (flags & ERROR_RECOVERY_UPDATE_DB) {
1307 		resp = le32_to_cpu(*(__le32 *)host_cmd.resp_pkt->data);
1308 		if (resp)
1309 			IWL_ERR(mvm,
1310 				"Failed to send recovery cmd blob was invalid %d\n",
1311 				resp);
1312 	}
1313 }
1314 
iwl_mvm_sar_init(struct iwl_mvm * mvm)1315 static int iwl_mvm_sar_init(struct iwl_mvm *mvm)
1316 {
1317 	int ret;
1318 
1319 	ret = iwl_sar_get_wrds_table(&mvm->fwrt);
1320 	if (ret < 0) {
1321 		IWL_DEBUG_RADIO(mvm,
1322 				"WRDS SAR BIOS table invalid or unavailable. (%d)\n",
1323 				ret);
1324 		/*
1325 		 * If not available, don't fail and don't bother with EWRD.
1326 		 * Return 1 to tell that we can't use WGDS either.
1327 		 */
1328 		return 1;
1329 	}
1330 
1331 	ret = iwl_sar_get_ewrd_table(&mvm->fwrt);
1332 	/* if EWRD is not available, we can still use WRDS, so don't fail */
1333 	if (ret < 0)
1334 		IWL_DEBUG_RADIO(mvm,
1335 				"EWRD SAR BIOS table invalid or unavailable. (%d)\n",
1336 				ret);
1337 
1338 	return iwl_mvm_sar_select_profile(mvm, 1, 1);
1339 }
1340 
iwl_mvm_load_rt_fw(struct iwl_mvm * mvm)1341 static int iwl_mvm_load_rt_fw(struct iwl_mvm *mvm)
1342 {
1343 	int ret;
1344 
1345 	if (iwl_mvm_has_unified_ucode(mvm))
1346 		return iwl_run_unified_mvm_ucode(mvm, false);
1347 
1348 	ret = iwl_run_init_mvm_ucode(mvm, false);
1349 
1350 	if (ret) {
1351 		IWL_ERR(mvm, "Failed to run INIT ucode: %d\n", ret);
1352 
1353 		if (iwlmvm_mod_params.init_dbg)
1354 			return 0;
1355 		return ret;
1356 	}
1357 
1358 	iwl_fw_dbg_stop_sync(&mvm->fwrt);
1359 	iwl_trans_stop_device(mvm->trans);
1360 	ret = iwl_trans_start_hw(mvm->trans);
1361 	if (ret)
1362 		return ret;
1363 
1364 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_EARLY, NULL);
1365 
1366 	mvm->rfkill_safe_init_done = false;
1367 	ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_REGULAR);
1368 	if (ret)
1369 		return ret;
1370 
1371 	mvm->rfkill_safe_init_done = true;
1372 
1373 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_AFTER_ALIVE,
1374 			       NULL);
1375 
1376 	return iwl_init_paging(&mvm->fwrt, mvm->fwrt.cur_fw_img);
1377 }
1378 
iwl_mvm_up(struct iwl_mvm * mvm)1379 int iwl_mvm_up(struct iwl_mvm *mvm)
1380 {
1381 	int ret, i;
1382 	struct ieee80211_channel *chan;
1383 	struct cfg80211_chan_def chandef;
1384 	struct ieee80211_supported_band *sband = NULL;
1385 
1386 	lockdep_assert_held(&mvm->mutex);
1387 
1388 	ret = iwl_trans_start_hw(mvm->trans);
1389 	if (ret)
1390 		return ret;
1391 
1392 	ret = iwl_mvm_load_rt_fw(mvm);
1393 	if (ret) {
1394 		IWL_ERR(mvm, "Failed to start RT ucode: %d\n", ret);
1395 		if (ret != -ERFKILL)
1396 			iwl_fw_dbg_error_collect(&mvm->fwrt,
1397 						 FW_DBG_TRIGGER_DRIVER);
1398 		goto error;
1399 	}
1400 
1401 	iwl_get_shared_mem_conf(&mvm->fwrt);
1402 
1403 	ret = iwl_mvm_sf_update(mvm, NULL, false);
1404 	if (ret)
1405 		IWL_ERR(mvm, "Failed to initialize Smart Fifo\n");
1406 
1407 	if (!iwl_trans_dbg_ini_valid(mvm->trans)) {
1408 		mvm->fwrt.dump.conf = FW_DBG_INVALID;
1409 		/* if we have a destination, assume EARLY START */
1410 		if (mvm->fw->dbg.dest_tlv)
1411 			mvm->fwrt.dump.conf = FW_DBG_START_FROM_ALIVE;
1412 		iwl_fw_start_dbg_conf(&mvm->fwrt, FW_DBG_START_FROM_ALIVE);
1413 	}
1414 
1415 	ret = iwl_send_tx_ant_cfg(mvm, iwl_mvm_get_valid_tx_ant(mvm));
1416 	if (ret)
1417 		goto error;
1418 
1419 	if (!iwl_mvm_has_unified_ucode(mvm)) {
1420 		/* Send phy db control command and then phy db calibration */
1421 		ret = iwl_send_phy_db_data(mvm->phy_db);
1422 		if (ret)
1423 			goto error;
1424 	}
1425 
1426 	ret = iwl_send_phy_cfg_cmd(mvm);
1427 	if (ret)
1428 		goto error;
1429 
1430 	ret = iwl_mvm_send_bt_init_conf(mvm);
1431 	if (ret)
1432 		goto error;
1433 
1434 	if (fw_has_capa(&mvm->fw->ucode_capa,
1435 			IWL_UCODE_TLV_CAPA_SOC_LATENCY_SUPPORT)) {
1436 		ret = iwl_set_soc_latency(&mvm->fwrt);
1437 		if (ret)
1438 			goto error;
1439 	}
1440 
1441 	/* Init RSS configuration */
1442 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_22000) {
1443 		ret = iwl_configure_rxq(mvm);
1444 		if (ret) {
1445 			IWL_ERR(mvm, "Failed to configure RX queues: %d\n",
1446 				ret);
1447 			goto error;
1448 		}
1449 	}
1450 
1451 	if (iwl_mvm_has_new_rx_api(mvm)) {
1452 		ret = iwl_send_rss_cfg_cmd(mvm);
1453 		if (ret) {
1454 			IWL_ERR(mvm, "Failed to configure RSS queues: %d\n",
1455 				ret);
1456 			goto error;
1457 		}
1458 	}
1459 
1460 	/* init the fw <-> mac80211 STA mapping */
1461 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++)
1462 		RCU_INIT_POINTER(mvm->fw_id_to_mac_id[i], NULL);
1463 
1464 	mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
1465 
1466 	/* reset quota debouncing buffer - 0xff will yield invalid data */
1467 	memset(&mvm->last_quota_cmd, 0xff, sizeof(mvm->last_quota_cmd));
1468 
1469 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_DQA_SUPPORT)) {
1470 		ret = iwl_mvm_send_dqa_cmd(mvm);
1471 		if (ret)
1472 			goto error;
1473 	}
1474 
1475 	/*
1476 	 * Add auxiliary station for scanning.
1477 	 * Newer versions of this command implies that the fw uses
1478 	 * internal aux station for all aux activities that don't
1479 	 * requires a dedicated data queue.
1480 	 */
1481 	if (iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP,
1482 				  ADD_STA,
1483 				  0) < 12) {
1484 		 /*
1485 		  * In old version the aux station uses mac id like other
1486 		  * station and not lmac id
1487 		  */
1488 		ret = iwl_mvm_add_aux_sta(mvm, MAC_INDEX_AUX);
1489 		if (ret)
1490 			goto error;
1491 	}
1492 
1493 	/* Add all the PHY contexts */
1494 	i = 0;
1495 	while (!sband && i < NUM_NL80211_BANDS)
1496 		sband = mvm->hw->wiphy->bands[i++];
1497 
1498 	if (WARN_ON_ONCE(!sband)) {
1499 		ret = -ENODEV;
1500 		goto error;
1501 	}
1502 
1503 	chan = &sband->channels[0];
1504 
1505 	cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
1506 	for (i = 0; i < NUM_PHY_CTX; i++) {
1507 		/*
1508 		 * The channel used here isn't relevant as it's
1509 		 * going to be overwritten in the other flows.
1510 		 * For now use the first channel we have.
1511 		 */
1512 		ret = iwl_mvm_phy_ctxt_add(mvm, &mvm->phy_ctxts[i],
1513 					   &chandef, 1, 1);
1514 		if (ret)
1515 			goto error;
1516 	}
1517 
1518 	if (iwl_mvm_is_tt_in_fw(mvm)) {
1519 		/* in order to give the responsibility of ct-kill and
1520 		 * TX backoff to FW we need to send empty temperature reporting
1521 		 * cmd during init time
1522 		 */
1523 		iwl_mvm_send_temp_report_ths_cmd(mvm);
1524 	} else {
1525 		/* Initialize tx backoffs to the minimal possible */
1526 		iwl_mvm_tt_tx_backoff(mvm, 0);
1527 	}
1528 
1529 #ifdef CONFIG_THERMAL
1530 	/* TODO: read the budget from BIOS / Platform NVM */
1531 
1532 	/*
1533 	 * In case there is no budget from BIOS / Platform NVM the default
1534 	 * budget should be 2000mW (cooling state 0).
1535 	 */
1536 	if (iwl_mvm_is_ctdp_supported(mvm)) {
1537 		ret = iwl_mvm_ctdp_command(mvm, CTDP_CMD_OPERATION_START,
1538 					   mvm->cooling_dev.cur_state);
1539 		if (ret)
1540 			goto error;
1541 	}
1542 #endif
1543 
1544 	if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_SET_LTR_GEN2))
1545 		WARN_ON(iwl_mvm_config_ltr(mvm));
1546 
1547 	ret = iwl_mvm_power_update_device(mvm);
1548 	if (ret)
1549 		goto error;
1550 
1551 	iwl_mvm_lari_cfg(mvm);
1552 	/*
1553 	 * RTNL is not taken during Ct-kill, but we don't need to scan/Tx
1554 	 * anyway, so don't init MCC.
1555 	 */
1556 	if (!test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status)) {
1557 		ret = iwl_mvm_init_mcc(mvm);
1558 		if (ret)
1559 			goto error;
1560 	}
1561 
1562 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1563 		mvm->scan_type = IWL_SCAN_TYPE_NOT_SET;
1564 		mvm->hb_scan_type = IWL_SCAN_TYPE_NOT_SET;
1565 		ret = iwl_mvm_config_scan(mvm);
1566 		if (ret)
1567 			goto error;
1568 	}
1569 
1570 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1571 		iwl_mvm_send_recovery_cmd(mvm, ERROR_RECOVERY_UPDATE_DB);
1572 
1573 	if (iwl_acpi_get_eckv(mvm->dev, &mvm->ext_clock_valid))
1574 		IWL_DEBUG_INFO(mvm, "ECKV table doesn't exist in BIOS\n");
1575 
1576 	ret = iwl_mvm_ppag_init(mvm);
1577 	if (ret)
1578 		goto error;
1579 
1580 	ret = iwl_mvm_sar_init(mvm);
1581 	if (ret == 0) {
1582 		ret = iwl_mvm_sar_geo_init(mvm);
1583 	} else if (ret == -ENOENT && !iwl_sar_get_wgds_table(&mvm->fwrt)) {
1584 		/*
1585 		 * If basic SAR is not available, we check for WGDS,
1586 		 * which should *not* be available either.  If it is
1587 		 * available, issue an error, because we can't use SAR
1588 		 * Geo without basic SAR.
1589 		 */
1590 		IWL_ERR(mvm, "BIOS contains WGDS but no WRDS\n");
1591 	}
1592 
1593 	if (ret < 0)
1594 		goto error;
1595 
1596 	iwl_mvm_tas_init(mvm);
1597 	iwl_mvm_leds_sync(mvm);
1598 
1599 	iwl_mvm_ftm_initiator_smooth_config(mvm);
1600 
1601 	IWL_DEBUG_INFO(mvm, "RT uCode started.\n");
1602 	return 0;
1603  error:
1604 	if (!iwlmvm_mod_params.init_dbg || !ret)
1605 		iwl_mvm_stop_device(mvm);
1606 	return ret;
1607 }
1608 
iwl_mvm_load_d3_fw(struct iwl_mvm * mvm)1609 int iwl_mvm_load_d3_fw(struct iwl_mvm *mvm)
1610 {
1611 	int ret, i;
1612 
1613 	lockdep_assert_held(&mvm->mutex);
1614 
1615 	ret = iwl_trans_start_hw(mvm->trans);
1616 	if (ret)
1617 		return ret;
1618 
1619 	ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_WOWLAN);
1620 	if (ret) {
1621 		IWL_ERR(mvm, "Failed to start WoWLAN firmware: %d\n", ret);
1622 		goto error;
1623 	}
1624 
1625 	ret = iwl_send_tx_ant_cfg(mvm, iwl_mvm_get_valid_tx_ant(mvm));
1626 	if (ret)
1627 		goto error;
1628 
1629 	/* Send phy db control command and then phy db calibration*/
1630 	ret = iwl_send_phy_db_data(mvm->phy_db);
1631 	if (ret)
1632 		goto error;
1633 
1634 	ret = iwl_send_phy_cfg_cmd(mvm);
1635 	if (ret)
1636 		goto error;
1637 
1638 	/* init the fw <-> mac80211 STA mapping */
1639 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++)
1640 		RCU_INIT_POINTER(mvm->fw_id_to_mac_id[i], NULL);
1641 
1642 	if (iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP,
1643 				  ADD_STA,
1644 				  0) < 12) {
1645 		/*
1646 		 * Add auxiliary station for scanning.
1647 		 * Newer versions of this command implies that the fw uses
1648 		 * internal aux station for all aux activities that don't
1649 		 * requires a dedicated data queue.
1650 		 * In old version the aux station uses mac id like other
1651 		 * station and not lmac id
1652 		 */
1653 		ret = iwl_mvm_add_aux_sta(mvm, MAC_INDEX_AUX);
1654 		if (ret)
1655 			goto error;
1656 	}
1657 
1658 	return 0;
1659  error:
1660 	iwl_mvm_stop_device(mvm);
1661 	return ret;
1662 }
1663 
iwl_mvm_rx_card_state_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)1664 void iwl_mvm_rx_card_state_notif(struct iwl_mvm *mvm,
1665 				 struct iwl_rx_cmd_buffer *rxb)
1666 {
1667 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1668 	struct iwl_card_state_notif *card_state_notif = (void *)pkt->data;
1669 	u32 flags = le32_to_cpu(card_state_notif->flags);
1670 
1671 	IWL_DEBUG_RF_KILL(mvm, "Card state received: HW:%s SW:%s CT:%s\n",
1672 			  (flags & HW_CARD_DISABLED) ? "Kill" : "On",
1673 			  (flags & SW_CARD_DISABLED) ? "Kill" : "On",
1674 			  (flags & CT_KILL_CARD_DISABLED) ?
1675 			  "Reached" : "Not reached");
1676 }
1677 
iwl_mvm_rx_mfuart_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)1678 void iwl_mvm_rx_mfuart_notif(struct iwl_mvm *mvm,
1679 			     struct iwl_rx_cmd_buffer *rxb)
1680 {
1681 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1682 	struct iwl_mfuart_load_notif *mfuart_notif = (void *)pkt->data;
1683 
1684 	IWL_DEBUG_INFO(mvm,
1685 		       "MFUART: installed ver: 0x%08x, external ver: 0x%08x, status: 0x%08x, duration: 0x%08x\n",
1686 		       le32_to_cpu(mfuart_notif->installed_ver),
1687 		       le32_to_cpu(mfuart_notif->external_ver),
1688 		       le32_to_cpu(mfuart_notif->status),
1689 		       le32_to_cpu(mfuart_notif->duration));
1690 
1691 	if (iwl_rx_packet_payload_len(pkt) == sizeof(*mfuart_notif))
1692 		IWL_DEBUG_INFO(mvm,
1693 			       "MFUART: image size: 0x%08x\n",
1694 			       le32_to_cpu(mfuart_notif->image_size));
1695 }
1696