xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/fw.c (revision a5d9265e017f081f0dc133c0e2f45103d027b874)
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) 2012 - 2014 Intel Corporation. All rights reserved.
9  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
10  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
11  * Copyright(c) 2018        Intel Corporation
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of version 2 of the GNU General Public License as
15  * published by the Free Software Foundation.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * General Public License for more details.
21  *
22  * The full GNU General Public License is included in this distribution
23  * in the file called COPYING.
24  *
25  * Contact Information:
26  *  Intel Linux Wireless <linuxwifi@intel.com>
27  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28  *
29  * BSD LICENSE
30  *
31  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
32  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
33  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
34  * Copyright(c) 2018        Intel Corporation
35  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  *
41  *  * Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  *  * Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in
45  *    the documentation and/or other materials provided with the
46  *    distribution.
47  *  * Neither the name Intel Corporation nor the names of its
48  *    contributors may be used to endorse or promote products derived
49  *    from this software without specific prior written permission.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
57  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62  *
63  *****************************************************************************/
64 #include <net/mac80211.h>
65 #include <linux/netdevice.h>
66 
67 #include "iwl-trans.h"
68 #include "iwl-op-mode.h"
69 #include "fw/img.h"
70 #include "iwl-debug.h"
71 #include "iwl-csr.h" /* for iwl_mvm_rx_card_state_notif */
72 #include "iwl-io.h" /* for iwl_mvm_rx_card_state_notif */
73 #include "iwl-prph.h"
74 #include "fw/acpi.h"
75 
76 #include "mvm.h"
77 #include "fw/dbg.h"
78 #include "iwl-phy-db.h"
79 #include "iwl-modparams.h"
80 #include "iwl-nvm-parse.h"
81 
82 #define MVM_UCODE_ALIVE_TIMEOUT	HZ
83 #define MVM_UCODE_CALIB_TIMEOUT	(2*HZ)
84 
85 #define UCODE_VALID_OK	cpu_to_le32(0x1)
86 
87 struct iwl_mvm_alive_data {
88 	bool valid;
89 	u32 scd_base_addr;
90 };
91 
92 static int iwl_send_tx_ant_cfg(struct iwl_mvm *mvm, u8 valid_tx_ant)
93 {
94 	struct iwl_tx_ant_cfg_cmd tx_ant_cmd = {
95 		.valid = cpu_to_le32(valid_tx_ant),
96 	};
97 
98 	IWL_DEBUG_FW(mvm, "select valid tx ant: %u\n", valid_tx_ant);
99 	return iwl_mvm_send_cmd_pdu(mvm, TX_ANT_CONFIGURATION_CMD, 0,
100 				    sizeof(tx_ant_cmd), &tx_ant_cmd);
101 }
102 
103 static int iwl_send_rss_cfg_cmd(struct iwl_mvm *mvm)
104 {
105 	int i;
106 	struct iwl_rss_config_cmd cmd = {
107 		.flags = cpu_to_le32(IWL_RSS_ENABLE),
108 		.hash_mask = IWL_RSS_HASH_TYPE_IPV4_TCP |
109 			     IWL_RSS_HASH_TYPE_IPV4_UDP |
110 			     IWL_RSS_HASH_TYPE_IPV4_PAYLOAD |
111 			     IWL_RSS_HASH_TYPE_IPV6_TCP |
112 			     IWL_RSS_HASH_TYPE_IPV6_UDP |
113 			     IWL_RSS_HASH_TYPE_IPV6_PAYLOAD,
114 	};
115 
116 	if (mvm->trans->num_rx_queues == 1)
117 		return 0;
118 
119 	/* Do not direct RSS traffic to Q 0 which is our fallback queue */
120 	for (i = 0; i < ARRAY_SIZE(cmd.indirection_table); i++)
121 		cmd.indirection_table[i] =
122 			1 + (i % (mvm->trans->num_rx_queues - 1));
123 	netdev_rss_key_fill(cmd.secret_key, sizeof(cmd.secret_key));
124 
125 	return iwl_mvm_send_cmd_pdu(mvm, RSS_CONFIG_CMD, 0, sizeof(cmd), &cmd);
126 }
127 
128 static int iwl_configure_rxq(struct iwl_mvm *mvm)
129 {
130 	int i, num_queues, size;
131 	struct iwl_rfh_queue_config *cmd;
132 
133 	/* Do not configure default queue, it is configured via context info */
134 	num_queues = mvm->trans->num_rx_queues - 1;
135 
136 	size = sizeof(*cmd) + num_queues * sizeof(struct iwl_rfh_queue_data);
137 
138 	cmd = kzalloc(size, GFP_KERNEL);
139 	if (!cmd)
140 		return -ENOMEM;
141 
142 	cmd->num_queues = num_queues;
143 
144 	for (i = 0; i < num_queues; i++) {
145 		struct iwl_trans_rxq_dma_data data;
146 
147 		cmd->data[i].q_num = i + 1;
148 		iwl_trans_get_rxq_dma_data(mvm->trans, i + 1, &data);
149 
150 		cmd->data[i].fr_bd_cb = cpu_to_le64(data.fr_bd_cb);
151 		cmd->data[i].urbd_stts_wrptr =
152 			cpu_to_le64(data.urbd_stts_wrptr);
153 		cmd->data[i].ur_bd_cb = cpu_to_le64(data.ur_bd_cb);
154 		cmd->data[i].fr_bd_wid = cpu_to_le32(data.fr_bd_wid);
155 	}
156 
157 	return iwl_mvm_send_cmd_pdu(mvm,
158 				    WIDE_ID(DATA_PATH_GROUP,
159 					    RFH_QUEUE_CONFIG_CMD),
160 				    0, size, cmd);
161 }
162 
163 static int iwl_mvm_send_dqa_cmd(struct iwl_mvm *mvm)
164 {
165 	struct iwl_dqa_enable_cmd dqa_cmd = {
166 		.cmd_queue = cpu_to_le32(IWL_MVM_DQA_CMD_QUEUE),
167 	};
168 	u32 cmd_id = iwl_cmd_id(DQA_ENABLE_CMD, DATA_PATH_GROUP, 0);
169 	int ret;
170 
171 	ret = iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, sizeof(dqa_cmd), &dqa_cmd);
172 	if (ret)
173 		IWL_ERR(mvm, "Failed to send DQA enabling command: %d\n", ret);
174 	else
175 		IWL_DEBUG_FW(mvm, "Working in DQA mode\n");
176 
177 	return ret;
178 }
179 
180 void iwl_mvm_mfu_assert_dump_notif(struct iwl_mvm *mvm,
181 				   struct iwl_rx_cmd_buffer *rxb)
182 {
183 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
184 	struct iwl_mfu_assert_dump_notif *mfu_dump_notif = (void *)pkt->data;
185 	__le32 *dump_data = mfu_dump_notif->data;
186 	int n_words = le32_to_cpu(mfu_dump_notif->data_size) / sizeof(__le32);
187 	int i;
188 
189 	if (mfu_dump_notif->index_num == 0)
190 		IWL_INFO(mvm, "MFUART assert id 0x%x occurred\n",
191 			 le32_to_cpu(mfu_dump_notif->assert_id));
192 
193 	for (i = 0; i < n_words; i++)
194 		IWL_DEBUG_INFO(mvm,
195 			       "MFUART assert dump, dword %u: 0x%08x\n",
196 			       le16_to_cpu(mfu_dump_notif->index_num) *
197 			       n_words + i,
198 			       le32_to_cpu(dump_data[i]));
199 }
200 
201 static bool iwl_alive_fn(struct iwl_notif_wait_data *notif_wait,
202 			 struct iwl_rx_packet *pkt, void *data)
203 {
204 	struct iwl_mvm *mvm =
205 		container_of(notif_wait, struct iwl_mvm, notif_wait);
206 	struct iwl_mvm_alive_data *alive_data = data;
207 	struct mvm_alive_resp_v3 *palive3;
208 	struct mvm_alive_resp *palive;
209 	struct iwl_umac_alive *umac;
210 	struct iwl_lmac_alive *lmac1;
211 	struct iwl_lmac_alive *lmac2 = NULL;
212 	u16 status;
213 	u32 umac_error_event_table;
214 
215 	if (iwl_rx_packet_payload_len(pkt) == sizeof(*palive)) {
216 		palive = (void *)pkt->data;
217 		umac = &palive->umac_data;
218 		lmac1 = &palive->lmac_data[0];
219 		lmac2 = &palive->lmac_data[1];
220 		status = le16_to_cpu(palive->status);
221 	} else {
222 		palive3 = (void *)pkt->data;
223 		umac = &palive3->umac_data;
224 		lmac1 = &palive3->lmac_data;
225 		status = le16_to_cpu(palive3->status);
226 	}
227 
228 	mvm->error_event_table[0] = le32_to_cpu(lmac1->error_event_table_ptr);
229 	if (lmac2)
230 		mvm->error_event_table[1] =
231 			le32_to_cpu(lmac2->error_event_table_ptr);
232 	mvm->log_event_table = le32_to_cpu(lmac1->log_event_table_ptr);
233 
234 	umac_error_event_table = le32_to_cpu(umac->error_info_addr);
235 
236 	if (!umac_error_event_table) {
237 		mvm->support_umac_log = false;
238 	} else if (umac_error_event_table >=
239 		   mvm->trans->cfg->min_umac_error_event_table) {
240 		mvm->support_umac_log = true;
241 		mvm->umac_error_event_table = umac_error_event_table;
242 	} else {
243 		IWL_ERR(mvm,
244 			"Not valid error log pointer 0x%08X for %s uCode\n",
245 			mvm->umac_error_event_table,
246 			(mvm->fwrt.cur_fw_img == IWL_UCODE_INIT) ?
247 			"Init" : "RT");
248 		mvm->support_umac_log = false;
249 	}
250 
251 	alive_data->scd_base_addr = le32_to_cpu(lmac1->scd_base_ptr);
252 	alive_data->valid = status == IWL_ALIVE_STATUS_OK;
253 
254 	IWL_DEBUG_FW(mvm,
255 		     "Alive ucode status 0x%04x revision 0x%01X 0x%01X\n",
256 		     status, lmac1->ver_type, lmac1->ver_subtype);
257 
258 	if (lmac2)
259 		IWL_DEBUG_FW(mvm, "Alive ucode CDB\n");
260 
261 	IWL_DEBUG_FW(mvm,
262 		     "UMAC version: Major - 0x%x, Minor - 0x%x\n",
263 		     le32_to_cpu(umac->umac_major),
264 		     le32_to_cpu(umac->umac_minor));
265 
266 	return true;
267 }
268 
269 static bool iwl_wait_init_complete(struct iwl_notif_wait_data *notif_wait,
270 				   struct iwl_rx_packet *pkt, void *data)
271 {
272 	WARN_ON(pkt->hdr.cmd != INIT_COMPLETE_NOTIF);
273 
274 	return true;
275 }
276 
277 static bool iwl_wait_phy_db_entry(struct iwl_notif_wait_data *notif_wait,
278 				  struct iwl_rx_packet *pkt, void *data)
279 {
280 	struct iwl_phy_db *phy_db = data;
281 
282 	if (pkt->hdr.cmd != CALIB_RES_NOTIF_PHY_DB) {
283 		WARN_ON(pkt->hdr.cmd != INIT_COMPLETE_NOTIF);
284 		return true;
285 	}
286 
287 	WARN_ON(iwl_phy_db_set_section(phy_db, pkt));
288 
289 	return false;
290 }
291 
292 static int iwl_mvm_load_ucode_wait_alive(struct iwl_mvm *mvm,
293 					 enum iwl_ucode_type ucode_type)
294 {
295 	struct iwl_notification_wait alive_wait;
296 	struct iwl_mvm_alive_data alive_data = {};
297 	const struct fw_img *fw;
298 	int ret;
299 	enum iwl_ucode_type old_type = mvm->fwrt.cur_fw_img;
300 	static const u16 alive_cmd[] = { MVM_ALIVE };
301 
302 	set_bit(IWL_FWRT_STATUS_WAIT_ALIVE, &mvm->fwrt.status);
303 	if (ucode_type == IWL_UCODE_REGULAR &&
304 	    iwl_fw_dbg_conf_usniffer(mvm->fw, FW_DBG_START_FROM_ALIVE) &&
305 	    !(fw_has_capa(&mvm->fw->ucode_capa,
306 			  IWL_UCODE_TLV_CAPA_USNIFFER_UNIFIED)))
307 		fw = iwl_get_ucode_image(mvm->fw, IWL_UCODE_REGULAR_USNIFFER);
308 	else
309 		fw = iwl_get_ucode_image(mvm->fw, ucode_type);
310 	if (WARN_ON(!fw))
311 		return -EINVAL;
312 	iwl_fw_set_current_image(&mvm->fwrt, ucode_type);
313 	clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
314 
315 	iwl_init_notification_wait(&mvm->notif_wait, &alive_wait,
316 				   alive_cmd, ARRAY_SIZE(alive_cmd),
317 				   iwl_alive_fn, &alive_data);
318 
319 	ret = iwl_trans_start_fw(mvm->trans, fw, ucode_type == IWL_UCODE_INIT);
320 	if (ret) {
321 		iwl_fw_set_current_image(&mvm->fwrt, old_type);
322 		iwl_remove_notification(&mvm->notif_wait, &alive_wait);
323 		return ret;
324 	}
325 
326 	/*
327 	 * Some things may run in the background now, but we
328 	 * just wait for the ALIVE notification here.
329 	 */
330 	ret = iwl_wait_notification(&mvm->notif_wait, &alive_wait,
331 				    MVM_UCODE_ALIVE_TIMEOUT);
332 	if (ret) {
333 		struct iwl_trans *trans = mvm->trans;
334 
335 		if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_22000)
336 			IWL_ERR(mvm,
337 				"SecBoot CPU1 Status: 0x%x, CPU2 Status: 0x%x\n",
338 				iwl_read_prph(trans, UMAG_SB_CPU_1_STATUS),
339 				iwl_read_prph(trans, UMAG_SB_CPU_2_STATUS));
340 		else if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_8000)
341 			IWL_ERR(mvm,
342 				"SecBoot CPU1 Status: 0x%x, CPU2 Status: 0x%x\n",
343 				iwl_read_prph(trans, SB_CPU_1_STATUS),
344 				iwl_read_prph(trans, SB_CPU_2_STATUS));
345 		iwl_fw_set_current_image(&mvm->fwrt, old_type);
346 		return ret;
347 	}
348 
349 	if (!alive_data.valid) {
350 		IWL_ERR(mvm, "Loaded ucode is not valid!\n");
351 		iwl_fw_set_current_image(&mvm->fwrt, old_type);
352 		return -EIO;
353 	}
354 
355 	iwl_trans_fw_alive(mvm->trans, alive_data.scd_base_addr);
356 
357 	/*
358 	 * Note: all the queues are enabled as part of the interface
359 	 * initialization, but in firmware restart scenarios they
360 	 * could be stopped, so wake them up. In firmware restart,
361 	 * mac80211 will have the queues stopped as well until the
362 	 * reconfiguration completes. During normal startup, they
363 	 * will be empty.
364 	 */
365 
366 	memset(&mvm->queue_info, 0, sizeof(mvm->queue_info));
367 	/*
368 	 * Set a 'fake' TID for the command queue, since we use the
369 	 * hweight() of the tid_bitmap as a refcount now. Not that
370 	 * we ever even consider the command queue as one we might
371 	 * want to reuse, but be safe nevertheless.
372 	 */
373 	mvm->queue_info[IWL_MVM_DQA_CMD_QUEUE].tid_bitmap =
374 		BIT(IWL_MAX_TID_COUNT + 2);
375 
376 	set_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
377 #ifdef CONFIG_IWLWIFI_DEBUGFS
378 	iwl_fw_set_dbg_rec_on(&mvm->fwrt);
379 #endif
380 	clear_bit(IWL_FWRT_STATUS_WAIT_ALIVE, &mvm->fwrt.status);
381 
382 	return 0;
383 }
384 
385 static int iwl_run_unified_mvm_ucode(struct iwl_mvm *mvm, bool read_nvm)
386 {
387 	struct iwl_notification_wait init_wait;
388 	struct iwl_nvm_access_complete_cmd nvm_complete = {};
389 	struct iwl_init_extended_cfg_cmd init_cfg = {
390 		.init_flags = cpu_to_le32(BIT(IWL_INIT_NVM)),
391 	};
392 	static const u16 init_complete[] = {
393 		INIT_COMPLETE_NOTIF,
394 	};
395 	int ret;
396 
397 	lockdep_assert_held(&mvm->mutex);
398 
399 	iwl_init_notification_wait(&mvm->notif_wait,
400 				   &init_wait,
401 				   init_complete,
402 				   ARRAY_SIZE(init_complete),
403 				   iwl_wait_init_complete,
404 				   NULL);
405 
406 	/* Will also start the device */
407 	ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_REGULAR);
408 	if (ret) {
409 		IWL_ERR(mvm, "Failed to start RT ucode: %d\n", ret);
410 		iwl_fw_assert_error_dump(&mvm->fwrt);
411 		goto error;
412 	}
413 
414 	/* Send init config command to mark that we are sending NVM access
415 	 * commands
416 	 */
417 	ret = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(SYSTEM_GROUP,
418 						INIT_EXTENDED_CFG_CMD), 0,
419 				   sizeof(init_cfg), &init_cfg);
420 	if (ret) {
421 		IWL_ERR(mvm, "Failed to run init config command: %d\n",
422 			ret);
423 		goto error;
424 	}
425 
426 	/* Load NVM to NIC if needed */
427 	if (mvm->nvm_file_name) {
428 		iwl_read_external_nvm(mvm->trans, mvm->nvm_file_name,
429 				      mvm->nvm_sections);
430 		iwl_mvm_load_nvm_to_nic(mvm);
431 	}
432 
433 	if (IWL_MVM_PARSE_NVM && read_nvm) {
434 		ret = iwl_nvm_init(mvm);
435 		if (ret) {
436 			IWL_ERR(mvm, "Failed to read NVM: %d\n", ret);
437 			goto error;
438 		}
439 	}
440 
441 	ret = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(REGULATORY_AND_NVM_GROUP,
442 						NVM_ACCESS_COMPLETE), 0,
443 				   sizeof(nvm_complete), &nvm_complete);
444 	if (ret) {
445 		IWL_ERR(mvm, "Failed to run complete NVM access: %d\n",
446 			ret);
447 		goto error;
448 	}
449 
450 	/* We wait for the INIT complete notification */
451 	ret = iwl_wait_notification(&mvm->notif_wait, &init_wait,
452 				    MVM_UCODE_ALIVE_TIMEOUT);
453 	if (ret)
454 		return ret;
455 
456 	/* Read the NVM only at driver load time, no need to do this twice */
457 	if (!IWL_MVM_PARSE_NVM && read_nvm) {
458 		mvm->nvm_data = iwl_get_nvm(mvm->trans, mvm->fw);
459 		if (IS_ERR(mvm->nvm_data)) {
460 			ret = PTR_ERR(mvm->nvm_data);
461 			mvm->nvm_data = NULL;
462 			IWL_ERR(mvm, "Failed to read NVM: %d\n", ret);
463 			return ret;
464 		}
465 	}
466 
467 	return 0;
468 
469 error:
470 	iwl_remove_notification(&mvm->notif_wait, &init_wait);
471 	return ret;
472 }
473 
474 static int iwl_send_phy_cfg_cmd(struct iwl_mvm *mvm)
475 {
476 	struct iwl_phy_cfg_cmd phy_cfg_cmd;
477 	enum iwl_ucode_type ucode_type = mvm->fwrt.cur_fw_img;
478 
479 	/* Set parameters */
480 	phy_cfg_cmd.phy_cfg = cpu_to_le32(iwl_mvm_get_phy_config(mvm));
481 
482 	/* set flags extra PHY configuration flags from the device's cfg */
483 	phy_cfg_cmd.phy_cfg |= cpu_to_le32(mvm->cfg->extra_phy_cfg_flags);
484 
485 	phy_cfg_cmd.calib_control.event_trigger =
486 		mvm->fw->default_calib[ucode_type].event_trigger;
487 	phy_cfg_cmd.calib_control.flow_trigger =
488 		mvm->fw->default_calib[ucode_type].flow_trigger;
489 
490 	IWL_DEBUG_INFO(mvm, "Sending Phy CFG command: 0x%x\n",
491 		       phy_cfg_cmd.phy_cfg);
492 
493 	return iwl_mvm_send_cmd_pdu(mvm, PHY_CONFIGURATION_CMD, 0,
494 				    sizeof(phy_cfg_cmd), &phy_cfg_cmd);
495 }
496 
497 int iwl_run_init_mvm_ucode(struct iwl_mvm *mvm, bool read_nvm)
498 {
499 	struct iwl_notification_wait calib_wait;
500 	static const u16 init_complete[] = {
501 		INIT_COMPLETE_NOTIF,
502 		CALIB_RES_NOTIF_PHY_DB
503 	};
504 	int ret;
505 
506 	if (iwl_mvm_has_unified_ucode(mvm))
507 		return iwl_run_unified_mvm_ucode(mvm, true);
508 
509 	lockdep_assert_held(&mvm->mutex);
510 
511 	if (WARN_ON_ONCE(mvm->calibrating))
512 		return 0;
513 
514 	iwl_init_notification_wait(&mvm->notif_wait,
515 				   &calib_wait,
516 				   init_complete,
517 				   ARRAY_SIZE(init_complete),
518 				   iwl_wait_phy_db_entry,
519 				   mvm->phy_db);
520 
521 	/* Will also start the device */
522 	ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_INIT);
523 	if (ret) {
524 		IWL_ERR(mvm, "Failed to start INIT ucode: %d\n", ret);
525 		goto remove_notif;
526 	}
527 
528 	if (mvm->cfg->device_family < IWL_DEVICE_FAMILY_8000) {
529 		ret = iwl_mvm_send_bt_init_conf(mvm);
530 		if (ret)
531 			goto remove_notif;
532 	}
533 
534 	/* Read the NVM only at driver load time, no need to do this twice */
535 	if (read_nvm) {
536 		ret = iwl_nvm_init(mvm);
537 		if (ret) {
538 			IWL_ERR(mvm, "Failed to read NVM: %d\n", ret);
539 			goto remove_notif;
540 		}
541 	}
542 
543 	/* In case we read the NVM from external file, load it to the NIC */
544 	if (mvm->nvm_file_name)
545 		iwl_mvm_load_nvm_to_nic(mvm);
546 
547 	WARN_ONCE(mvm->nvm_data->nvm_version < mvm->trans->cfg->nvm_ver,
548 		  "Too old NVM version (0x%0x, required = 0x%0x)",
549 		  mvm->nvm_data->nvm_version, mvm->trans->cfg->nvm_ver);
550 
551 	/*
552 	 * abort after reading the nvm in case RF Kill is on, we will complete
553 	 * the init seq later when RF kill will switch to off
554 	 */
555 	if (iwl_mvm_is_radio_hw_killed(mvm)) {
556 		IWL_DEBUG_RF_KILL(mvm,
557 				  "jump over all phy activities due to RF kill\n");
558 		goto remove_notif;
559 	}
560 
561 	mvm->calibrating = true;
562 
563 	/* Send TX valid antennas before triggering calibrations */
564 	ret = iwl_send_tx_ant_cfg(mvm, iwl_mvm_get_valid_tx_ant(mvm));
565 	if (ret)
566 		goto remove_notif;
567 
568 	ret = iwl_send_phy_cfg_cmd(mvm);
569 	if (ret) {
570 		IWL_ERR(mvm, "Failed to run INIT calibrations: %d\n",
571 			ret);
572 		goto remove_notif;
573 	}
574 
575 	/*
576 	 * Some things may run in the background now, but we
577 	 * just wait for the calibration complete notification.
578 	 */
579 	ret = iwl_wait_notification(&mvm->notif_wait, &calib_wait,
580 				    MVM_UCODE_CALIB_TIMEOUT);
581 	if (!ret)
582 		goto out;
583 
584 	if (iwl_mvm_is_radio_hw_killed(mvm)) {
585 		IWL_DEBUG_RF_KILL(mvm, "RFKILL while calibrating.\n");
586 		ret = 0;
587 	} else {
588 		IWL_ERR(mvm, "Failed to run INIT calibrations: %d\n",
589 			ret);
590 	}
591 
592 	goto out;
593 
594 remove_notif:
595 	iwl_remove_notification(&mvm->notif_wait, &calib_wait);
596 out:
597 	mvm->calibrating = false;
598 	if (iwlmvm_mod_params.init_dbg && !mvm->nvm_data) {
599 		/* we want to debug INIT and we have no NVM - fake */
600 		mvm->nvm_data = kzalloc(sizeof(struct iwl_nvm_data) +
601 					sizeof(struct ieee80211_channel) +
602 					sizeof(struct ieee80211_rate),
603 					GFP_KERNEL);
604 		if (!mvm->nvm_data)
605 			return -ENOMEM;
606 		mvm->nvm_data->bands[0].channels = mvm->nvm_data->channels;
607 		mvm->nvm_data->bands[0].n_channels = 1;
608 		mvm->nvm_data->bands[0].n_bitrates = 1;
609 		mvm->nvm_data->bands[0].bitrates =
610 			(void *)mvm->nvm_data->channels + 1;
611 		mvm->nvm_data->bands[0].bitrates->hw_value = 10;
612 	}
613 
614 	return ret;
615 }
616 
617 static int iwl_mvm_config_ltr(struct iwl_mvm *mvm)
618 {
619 	struct iwl_ltr_config_cmd cmd = {
620 		.flags = cpu_to_le32(LTR_CFG_FLAG_FEATURE_ENABLE),
621 	};
622 
623 	if (!mvm->trans->ltr_enabled)
624 		return 0;
625 
626 	return iwl_mvm_send_cmd_pdu(mvm, LTR_CONFIG, 0,
627 				    sizeof(cmd), &cmd);
628 }
629 
630 #ifdef CONFIG_ACPI
631 static int iwl_mvm_sar_set_profile(struct iwl_mvm *mvm,
632 				   union acpi_object *table,
633 				   struct iwl_mvm_sar_profile *profile,
634 				   bool enabled)
635 {
636 	int i;
637 
638 	profile->enabled = enabled;
639 
640 	for (i = 0; i < ACPI_SAR_TABLE_SIZE; i++) {
641 		if ((table[i].type != ACPI_TYPE_INTEGER) ||
642 		    (table[i].integer.value > U8_MAX))
643 			return -EINVAL;
644 
645 		profile->table[i] = table[i].integer.value;
646 	}
647 
648 	return 0;
649 }
650 
651 static int iwl_mvm_sar_get_wrds_table(struct iwl_mvm *mvm)
652 {
653 	union acpi_object *wifi_pkg, *table, *data;
654 	bool enabled;
655 	int ret;
656 
657 	data = iwl_acpi_get_object(mvm->dev, ACPI_WRDS_METHOD);
658 	if (IS_ERR(data))
659 		return PTR_ERR(data);
660 
661 	wifi_pkg = iwl_acpi_get_wifi_pkg(mvm->dev, data,
662 					 ACPI_WRDS_WIFI_DATA_SIZE);
663 	if (IS_ERR(wifi_pkg)) {
664 		ret = PTR_ERR(wifi_pkg);
665 		goto out_free;
666 	}
667 
668 	if (wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER) {
669 		ret = -EINVAL;
670 		goto out_free;
671 	}
672 
673 	enabled = !!(wifi_pkg->package.elements[1].integer.value);
674 
675 	/* position of the actual table */
676 	table = &wifi_pkg->package.elements[2];
677 
678 	/* The profile from WRDS is officially profile 1, but goes
679 	 * into sar_profiles[0] (because we don't have a profile 0).
680 	 */
681 	ret = iwl_mvm_sar_set_profile(mvm, table, &mvm->sar_profiles[0],
682 				      enabled);
683 out_free:
684 	kfree(data);
685 	return ret;
686 }
687 
688 static int iwl_mvm_sar_get_ewrd_table(struct iwl_mvm *mvm)
689 {
690 	union acpi_object *wifi_pkg, *data;
691 	bool enabled;
692 	int i, n_profiles, ret;
693 
694 	data = iwl_acpi_get_object(mvm->dev, ACPI_EWRD_METHOD);
695 	if (IS_ERR(data))
696 		return PTR_ERR(data);
697 
698 	wifi_pkg = iwl_acpi_get_wifi_pkg(mvm->dev, data,
699 					 ACPI_EWRD_WIFI_DATA_SIZE);
700 	if (IS_ERR(wifi_pkg)) {
701 		ret = PTR_ERR(wifi_pkg);
702 		goto out_free;
703 	}
704 
705 	if ((wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER) ||
706 	    (wifi_pkg->package.elements[2].type != ACPI_TYPE_INTEGER)) {
707 		ret = -EINVAL;
708 		goto out_free;
709 	}
710 
711 	enabled = !!(wifi_pkg->package.elements[1].integer.value);
712 	n_profiles = wifi_pkg->package.elements[2].integer.value;
713 
714 	/*
715 	 * Check the validity of n_profiles.  The EWRD profiles start
716 	 * from index 1, so the maximum value allowed here is
717 	 * ACPI_SAR_PROFILES_NUM - 1.
718 	 */
719 	if (n_profiles <= 0 || n_profiles >= ACPI_SAR_PROFILE_NUM) {
720 		ret = -EINVAL;
721 		goto out_free;
722 	}
723 
724 	for (i = 0; i < n_profiles; i++) {
725 		/* the tables start at element 3 */
726 		static int pos = 3;
727 
728 		/* The EWRD profiles officially go from 2 to 4, but we
729 		 * save them in sar_profiles[1-3] (because we don't
730 		 * have profile 0).  So in the array we start from 1.
731 		 */
732 		ret = iwl_mvm_sar_set_profile(mvm,
733 					      &wifi_pkg->package.elements[pos],
734 					      &mvm->sar_profiles[i + 1],
735 					      enabled);
736 		if (ret < 0)
737 			break;
738 
739 		/* go to the next table */
740 		pos += ACPI_SAR_TABLE_SIZE;
741 	}
742 
743 out_free:
744 	kfree(data);
745 	return ret;
746 }
747 
748 static int iwl_mvm_sar_get_wgds_table(struct iwl_mvm *mvm)
749 {
750 	union acpi_object *wifi_pkg, *data;
751 	int i, j, ret;
752 	int idx = 1;
753 
754 	data = iwl_acpi_get_object(mvm->dev, ACPI_WGDS_METHOD);
755 	if (IS_ERR(data))
756 		return PTR_ERR(data);
757 
758 	wifi_pkg = iwl_acpi_get_wifi_pkg(mvm->dev, data,
759 					 ACPI_WGDS_WIFI_DATA_SIZE);
760 	if (IS_ERR(wifi_pkg)) {
761 		ret = PTR_ERR(wifi_pkg);
762 		goto out_free;
763 	}
764 
765 	for (i = 0; i < ACPI_NUM_GEO_PROFILES; i++) {
766 		for (j = 0; j < ACPI_GEO_TABLE_SIZE; j++) {
767 			union acpi_object *entry;
768 
769 			entry = &wifi_pkg->package.elements[idx++];
770 			if ((entry->type != ACPI_TYPE_INTEGER) ||
771 			    (entry->integer.value > U8_MAX)) {
772 				ret = -EINVAL;
773 				goto out_free;
774 			}
775 
776 			mvm->geo_profiles[i].values[j] = entry->integer.value;
777 		}
778 	}
779 	ret = 0;
780 out_free:
781 	kfree(data);
782 	return ret;
783 }
784 
785 int iwl_mvm_sar_select_profile(struct iwl_mvm *mvm, int prof_a, int prof_b)
786 {
787 	union {
788 		struct iwl_dev_tx_power_cmd v5;
789 		struct iwl_dev_tx_power_cmd_v4 v4;
790 	} cmd;
791 	int i, j, idx;
792 	int profs[ACPI_SAR_NUM_CHAIN_LIMITS] = { prof_a, prof_b };
793 	int len;
794 
795 	BUILD_BUG_ON(ACPI_SAR_NUM_CHAIN_LIMITS < 2);
796 	BUILD_BUG_ON(ACPI_SAR_NUM_CHAIN_LIMITS * ACPI_SAR_NUM_SUB_BANDS !=
797 		     ACPI_SAR_TABLE_SIZE);
798 
799 	cmd.v5.v3.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_CHAINS);
800 
801 	if (fw_has_api(&mvm->fw->ucode_capa,
802 		       IWL_UCODE_TLV_API_REDUCE_TX_POWER))
803 		len = sizeof(cmd.v5);
804 	else if (fw_has_capa(&mvm->fw->ucode_capa,
805 			     IWL_UCODE_TLV_CAPA_TX_POWER_ACK))
806 		len = sizeof(cmd.v4);
807 	else
808 		len = sizeof(cmd.v4.v3);
809 
810 	for (i = 0; i < ACPI_SAR_NUM_CHAIN_LIMITS; i++) {
811 		struct iwl_mvm_sar_profile *prof;
812 
813 		/* don't allow SAR to be disabled (profile 0 means disable) */
814 		if (profs[i] == 0)
815 			return -EPERM;
816 
817 		/* we are off by one, so allow up to ACPI_SAR_PROFILE_NUM */
818 		if (profs[i] > ACPI_SAR_PROFILE_NUM)
819 			return -EINVAL;
820 
821 		/* profiles go from 1 to 4, so decrement to access the array */
822 		prof = &mvm->sar_profiles[profs[i] - 1];
823 
824 		/* if the profile is disabled, do nothing */
825 		if (!prof->enabled) {
826 			IWL_DEBUG_RADIO(mvm, "SAR profile %d is disabled.\n",
827 					profs[i]);
828 			/* if one of the profiles is disabled, we fail all */
829 			return -ENOENT;
830 		}
831 
832 		IWL_DEBUG_RADIO(mvm, "  Chain[%d]:\n", i);
833 		for (j = 0; j < ACPI_SAR_NUM_SUB_BANDS; j++) {
834 			idx = (i * ACPI_SAR_NUM_SUB_BANDS) + j;
835 			cmd.v5.v3.per_chain_restriction[i][j] =
836 				cpu_to_le16(prof->table[idx]);
837 			IWL_DEBUG_RADIO(mvm, "    Band[%d] = %d * .125dBm\n",
838 					j, prof->table[idx]);
839 		}
840 	}
841 
842 	IWL_DEBUG_RADIO(mvm, "Sending REDUCE_TX_POWER_CMD per chain\n");
843 
844 	return iwl_mvm_send_cmd_pdu(mvm, REDUCE_TX_POWER_CMD, 0, len, &cmd);
845 }
846 
847 int iwl_mvm_get_sar_geo_profile(struct iwl_mvm *mvm)
848 {
849 	struct iwl_geo_tx_power_profiles_resp *resp;
850 	int ret;
851 
852 	struct iwl_geo_tx_power_profiles_cmd geo_cmd = {
853 		.ops = cpu_to_le32(IWL_PER_CHAIN_OFFSET_GET_CURRENT_TABLE),
854 	};
855 	struct iwl_host_cmd cmd = {
856 		.id =  WIDE_ID(PHY_OPS_GROUP, GEO_TX_POWER_LIMIT),
857 		.len = { sizeof(geo_cmd), },
858 		.flags = CMD_WANT_SKB,
859 		.data = { &geo_cmd },
860 	};
861 
862 	ret = iwl_mvm_send_cmd(mvm, &cmd);
863 	if (ret) {
864 		IWL_ERR(mvm, "Failed to get geographic profile info %d\n", ret);
865 		return ret;
866 	}
867 
868 	resp = (void *)cmd.resp_pkt->data;
869 	ret = le32_to_cpu(resp->profile_idx);
870 	if (WARN_ON(ret > ACPI_NUM_GEO_PROFILES)) {
871 		ret = -EIO;
872 		IWL_WARN(mvm, "Invalid geographic profile idx (%d)\n", ret);
873 	}
874 
875 	iwl_free_resp(&cmd);
876 	return ret;
877 }
878 
879 static int iwl_mvm_sar_geo_init(struct iwl_mvm *mvm)
880 {
881 	struct iwl_geo_tx_power_profiles_cmd cmd = {
882 		.ops = cpu_to_le32(IWL_PER_CHAIN_OFFSET_SET_TABLES),
883 	};
884 	int ret, i, j;
885 	u16 cmd_wide_id =  WIDE_ID(PHY_OPS_GROUP, GEO_TX_POWER_LIMIT);
886 
887 	/*
888 	 * This command is not supported on earlier firmware versions.
889 	 * Unfortunately, we don't have a TLV API flag to rely on, so
890 	 * rely on the major version which is in the first byte of
891 	 * ucode_ver.
892 	 */
893 	if (IWL_UCODE_SERIAL(mvm->fw->ucode_ver) < 41)
894 		return 0;
895 
896 	ret = iwl_mvm_sar_get_wgds_table(mvm);
897 	if (ret < 0) {
898 		IWL_DEBUG_RADIO(mvm,
899 				"Geo SAR BIOS table invalid or unavailable. (%d)\n",
900 				ret);
901 		/* we don't fail if the table is not available */
902 		return 0;
903 	}
904 
905 	IWL_DEBUG_RADIO(mvm, "Sending GEO_TX_POWER_LIMIT\n");
906 
907 	BUILD_BUG_ON(ACPI_NUM_GEO_PROFILES * ACPI_WGDS_NUM_BANDS *
908 		     ACPI_WGDS_TABLE_SIZE + 1 !=  ACPI_WGDS_WIFI_DATA_SIZE);
909 
910 	BUILD_BUG_ON(ACPI_NUM_GEO_PROFILES > IWL_NUM_GEO_PROFILES);
911 
912 	for (i = 0; i < ACPI_NUM_GEO_PROFILES; i++) {
913 		struct iwl_per_chain_offset *chain =
914 			(struct iwl_per_chain_offset *)&cmd.table[i];
915 
916 		for (j = 0; j < ACPI_WGDS_NUM_BANDS; j++) {
917 			u8 *value;
918 
919 			value = &mvm->geo_profiles[i].values[j *
920 				ACPI_GEO_PER_CHAIN_SIZE];
921 			chain[j].max_tx_power = cpu_to_le16(value[0]);
922 			chain[j].chain_a = value[1];
923 			chain[j].chain_b = value[2];
924 			IWL_DEBUG_RADIO(mvm,
925 					"SAR geographic profile[%d] Band[%d]: chain A = %d chain B = %d max_tx_power = %d\n",
926 					i, j, value[1], value[2], value[0]);
927 		}
928 	}
929 	return iwl_mvm_send_cmd_pdu(mvm, cmd_wide_id, 0, sizeof(cmd), &cmd);
930 }
931 
932 #else /* CONFIG_ACPI */
933 static int iwl_mvm_sar_get_wrds_table(struct iwl_mvm *mvm)
934 {
935 	return -ENOENT;
936 }
937 
938 static int iwl_mvm_sar_get_ewrd_table(struct iwl_mvm *mvm)
939 {
940 	return -ENOENT;
941 }
942 
943 static int iwl_mvm_sar_get_wgds_table(struct iwl_mvm *mvm)
944 {
945 	return -ENOENT;
946 }
947 
948 static int iwl_mvm_sar_geo_init(struct iwl_mvm *mvm)
949 {
950 	return 0;
951 }
952 
953 int iwl_mvm_sar_select_profile(struct iwl_mvm *mvm, int prof_a,
954 			       int prof_b)
955 {
956 	return -ENOENT;
957 }
958 
959 int iwl_mvm_get_sar_geo_profile(struct iwl_mvm *mvm)
960 {
961 	return -ENOENT;
962 }
963 #endif /* CONFIG_ACPI */
964 
965 static int iwl_mvm_sar_init(struct iwl_mvm *mvm)
966 {
967 	int ret;
968 
969 	ret = iwl_mvm_sar_get_wrds_table(mvm);
970 	if (ret < 0) {
971 		IWL_DEBUG_RADIO(mvm,
972 				"WRDS SAR BIOS table invalid or unavailable. (%d)\n",
973 				ret);
974 		/*
975 		 * If not available, don't fail and don't bother with EWRD.
976 		 * Return 1 to tell that we can't use WGDS either.
977 		 */
978 		return 1;
979 	}
980 
981 	ret = iwl_mvm_sar_get_ewrd_table(mvm);
982 	/* if EWRD is not available, we can still use WRDS, so don't fail */
983 	if (ret < 0)
984 		IWL_DEBUG_RADIO(mvm,
985 				"EWRD SAR BIOS table invalid or unavailable. (%d)\n",
986 				ret);
987 
988 	/* choose profile 1 (WRDS) as default for both chains */
989 	ret = iwl_mvm_sar_select_profile(mvm, 1, 1);
990 
991 	/*
992 	 * If we don't have profile 0 from BIOS, just skip it.  This
993 	 * means that SAR Geo will not be enabled either, even if we
994 	 * have other valid profiles.
995 	 */
996 	if (ret == -ENOENT)
997 		return 1;
998 
999 	return ret;
1000 }
1001 
1002 static int iwl_mvm_load_rt_fw(struct iwl_mvm *mvm)
1003 {
1004 	int ret;
1005 
1006 	if (iwl_mvm_has_unified_ucode(mvm))
1007 		return iwl_run_unified_mvm_ucode(mvm, false);
1008 
1009 	ret = iwl_run_init_mvm_ucode(mvm, false);
1010 
1011 	if (ret) {
1012 		IWL_ERR(mvm, "Failed to run INIT ucode: %d\n", ret);
1013 
1014 		if (iwlmvm_mod_params.init_dbg)
1015 			return 0;
1016 		return ret;
1017 	}
1018 
1019 	/*
1020 	 * Stop and start the transport without entering low power
1021 	 * mode. This will save the state of other components on the
1022 	 * device that are triggered by the INIT firwmare (MFUART).
1023 	 */
1024 	_iwl_trans_stop_device(mvm->trans, false);
1025 	ret = _iwl_trans_start_hw(mvm->trans, false);
1026 	if (ret)
1027 		return ret;
1028 
1029 	iwl_fw_dbg_apply_point(&mvm->fwrt, IWL_FW_INI_APPLY_EARLY);
1030 
1031 	ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_REGULAR);
1032 	if (ret)
1033 		return ret;
1034 
1035 	iwl_fw_dbg_apply_point(&mvm->fwrt, IWL_FW_INI_APPLY_AFTER_ALIVE);
1036 
1037 	return iwl_init_paging(&mvm->fwrt, mvm->fwrt.cur_fw_img);
1038 }
1039 
1040 int iwl_mvm_up(struct iwl_mvm *mvm)
1041 {
1042 	int ret, i;
1043 	struct ieee80211_channel *chan;
1044 	struct cfg80211_chan_def chandef;
1045 
1046 	lockdep_assert_held(&mvm->mutex);
1047 
1048 	ret = iwl_trans_start_hw(mvm->trans);
1049 	if (ret)
1050 		return ret;
1051 
1052 	ret = iwl_mvm_load_rt_fw(mvm);
1053 	if (ret) {
1054 		IWL_ERR(mvm, "Failed to start RT ucode: %d\n", ret);
1055 		iwl_fw_assert_error_dump(&mvm->fwrt);
1056 		goto error;
1057 	}
1058 
1059 	iwl_get_shared_mem_conf(&mvm->fwrt);
1060 
1061 	ret = iwl_mvm_sf_update(mvm, NULL, false);
1062 	if (ret)
1063 		IWL_ERR(mvm, "Failed to initialize Smart Fifo\n");
1064 
1065 	if (!mvm->trans->ini_valid) {
1066 		mvm->fwrt.dump.conf = FW_DBG_INVALID;
1067 		/* if we have a destination, assume EARLY START */
1068 		if (mvm->fw->dbg.dest_tlv)
1069 			mvm->fwrt.dump.conf = FW_DBG_START_FROM_ALIVE;
1070 		iwl_fw_start_dbg_conf(&mvm->fwrt, FW_DBG_START_FROM_ALIVE);
1071 	}
1072 
1073 	ret = iwl_send_tx_ant_cfg(mvm, iwl_mvm_get_valid_tx_ant(mvm));
1074 	if (ret)
1075 		goto error;
1076 
1077 	if (!iwl_mvm_has_unified_ucode(mvm)) {
1078 		/* Send phy db control command and then phy db calibration */
1079 		ret = iwl_send_phy_db_data(mvm->phy_db);
1080 		if (ret)
1081 			goto error;
1082 
1083 		ret = iwl_send_phy_cfg_cmd(mvm);
1084 		if (ret)
1085 			goto error;
1086 	}
1087 
1088 	ret = iwl_mvm_send_bt_init_conf(mvm);
1089 	if (ret)
1090 		goto error;
1091 
1092 	/* Init RSS configuration */
1093 	if (mvm->trans->cfg->device_family >= IWL_DEVICE_FAMILY_22000) {
1094 		ret = iwl_configure_rxq(mvm);
1095 		if (ret) {
1096 			IWL_ERR(mvm, "Failed to configure RX queues: %d\n",
1097 				ret);
1098 			goto error;
1099 		}
1100 	}
1101 
1102 	if (iwl_mvm_has_new_rx_api(mvm)) {
1103 		ret = iwl_send_rss_cfg_cmd(mvm);
1104 		if (ret) {
1105 			IWL_ERR(mvm, "Failed to configure RSS queues: %d\n",
1106 				ret);
1107 			goto error;
1108 		}
1109 	}
1110 
1111 	/* init the fw <-> mac80211 STA mapping */
1112 	for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++)
1113 		RCU_INIT_POINTER(mvm->fw_id_to_mac_id[i], NULL);
1114 
1115 	mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
1116 
1117 	/* reset quota debouncing buffer - 0xff will yield invalid data */
1118 	memset(&mvm->last_quota_cmd, 0xff, sizeof(mvm->last_quota_cmd));
1119 
1120 	ret = iwl_mvm_send_dqa_cmd(mvm);
1121 	if (ret)
1122 		goto error;
1123 
1124 	/* Add auxiliary station for scanning */
1125 	ret = iwl_mvm_add_aux_sta(mvm);
1126 	if (ret)
1127 		goto error;
1128 
1129 	/* Add all the PHY contexts */
1130 	chan = &mvm->hw->wiphy->bands[NL80211_BAND_2GHZ]->channels[0];
1131 	cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
1132 	for (i = 0; i < NUM_PHY_CTX; i++) {
1133 		/*
1134 		 * The channel used here isn't relevant as it's
1135 		 * going to be overwritten in the other flows.
1136 		 * For now use the first channel we have.
1137 		 */
1138 		ret = iwl_mvm_phy_ctxt_add(mvm, &mvm->phy_ctxts[i],
1139 					   &chandef, 1, 1);
1140 		if (ret)
1141 			goto error;
1142 	}
1143 
1144 #ifdef CONFIG_THERMAL
1145 	if (iwl_mvm_is_tt_in_fw(mvm)) {
1146 		/* in order to give the responsibility of ct-kill and
1147 		 * TX backoff to FW we need to send empty temperature reporting
1148 		 * cmd during init time
1149 		 */
1150 		iwl_mvm_send_temp_report_ths_cmd(mvm);
1151 	} else {
1152 		/* Initialize tx backoffs to the minimal possible */
1153 		iwl_mvm_tt_tx_backoff(mvm, 0);
1154 	}
1155 
1156 	/* TODO: read the budget from BIOS / Platform NVM */
1157 
1158 	/*
1159 	 * In case there is no budget from BIOS / Platform NVM the default
1160 	 * budget should be 2000mW (cooling state 0).
1161 	 */
1162 	if (iwl_mvm_is_ctdp_supported(mvm)) {
1163 		ret = iwl_mvm_ctdp_command(mvm, CTDP_CMD_OPERATION_START,
1164 					   mvm->cooling_dev.cur_state);
1165 		if (ret)
1166 			goto error;
1167 	}
1168 #else
1169 	/* Initialize tx backoffs to the minimal possible */
1170 	iwl_mvm_tt_tx_backoff(mvm, 0);
1171 #endif
1172 
1173 	WARN_ON(iwl_mvm_config_ltr(mvm));
1174 
1175 	ret = iwl_mvm_power_update_device(mvm);
1176 	if (ret)
1177 		goto error;
1178 
1179 	/*
1180 	 * RTNL is not taken during Ct-kill, but we don't need to scan/Tx
1181 	 * anyway, so don't init MCC.
1182 	 */
1183 	if (!test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status)) {
1184 		ret = iwl_mvm_init_mcc(mvm);
1185 		if (ret)
1186 			goto error;
1187 	}
1188 
1189 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1190 		mvm->scan_type = IWL_SCAN_TYPE_NOT_SET;
1191 		mvm->hb_scan_type = IWL_SCAN_TYPE_NOT_SET;
1192 		ret = iwl_mvm_config_scan(mvm);
1193 		if (ret)
1194 			goto error;
1195 	}
1196 
1197 	/* allow FW/transport low power modes if not during restart */
1198 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1199 		iwl_mvm_unref(mvm, IWL_MVM_REF_UCODE_DOWN);
1200 
1201 	ret = iwl_mvm_sar_init(mvm);
1202 	if (ret == 0) {
1203 		ret = iwl_mvm_sar_geo_init(mvm);
1204 	} else if (ret > 0 && !iwl_mvm_sar_get_wgds_table(mvm)) {
1205 		/*
1206 		 * If basic SAR is not available, we check for WGDS,
1207 		 * which should *not* be available either.  If it is
1208 		 * available, issue an error, because we can't use SAR
1209 		 * Geo without basic SAR.
1210 		 */
1211 		IWL_ERR(mvm, "BIOS contains WGDS but no WRDS\n");
1212 	}
1213 
1214 	if (ret < 0)
1215 		goto error;
1216 
1217 	iwl_mvm_leds_sync(mvm);
1218 
1219 	IWL_DEBUG_INFO(mvm, "RT uCode started.\n");
1220 	return 0;
1221  error:
1222 	if (!iwlmvm_mod_params.init_dbg || !ret)
1223 		iwl_mvm_stop_device(mvm);
1224 	return ret;
1225 }
1226 
1227 int iwl_mvm_load_d3_fw(struct iwl_mvm *mvm)
1228 {
1229 	int ret, i;
1230 
1231 	lockdep_assert_held(&mvm->mutex);
1232 
1233 	ret = iwl_trans_start_hw(mvm->trans);
1234 	if (ret)
1235 		return ret;
1236 
1237 	ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_WOWLAN);
1238 	if (ret) {
1239 		IWL_ERR(mvm, "Failed to start WoWLAN firmware: %d\n", ret);
1240 		goto error;
1241 	}
1242 
1243 	ret = iwl_send_tx_ant_cfg(mvm, iwl_mvm_get_valid_tx_ant(mvm));
1244 	if (ret)
1245 		goto error;
1246 
1247 	/* Send phy db control command and then phy db calibration*/
1248 	ret = iwl_send_phy_db_data(mvm->phy_db);
1249 	if (ret)
1250 		goto error;
1251 
1252 	ret = iwl_send_phy_cfg_cmd(mvm);
1253 	if (ret)
1254 		goto error;
1255 
1256 	/* init the fw <-> mac80211 STA mapping */
1257 	for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++)
1258 		RCU_INIT_POINTER(mvm->fw_id_to_mac_id[i], NULL);
1259 
1260 	/* Add auxiliary station for scanning */
1261 	ret = iwl_mvm_add_aux_sta(mvm);
1262 	if (ret)
1263 		goto error;
1264 
1265 	return 0;
1266  error:
1267 	iwl_mvm_stop_device(mvm);
1268 	return ret;
1269 }
1270 
1271 void iwl_mvm_rx_card_state_notif(struct iwl_mvm *mvm,
1272 				 struct iwl_rx_cmd_buffer *rxb)
1273 {
1274 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1275 	struct iwl_card_state_notif *card_state_notif = (void *)pkt->data;
1276 	u32 flags = le32_to_cpu(card_state_notif->flags);
1277 
1278 	IWL_DEBUG_RF_KILL(mvm, "Card state received: HW:%s SW:%s CT:%s\n",
1279 			  (flags & HW_CARD_DISABLED) ? "Kill" : "On",
1280 			  (flags & SW_CARD_DISABLED) ? "Kill" : "On",
1281 			  (flags & CT_KILL_CARD_DISABLED) ?
1282 			  "Reached" : "Not reached");
1283 }
1284 
1285 void iwl_mvm_rx_mfuart_notif(struct iwl_mvm *mvm,
1286 			     struct iwl_rx_cmd_buffer *rxb)
1287 {
1288 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1289 	struct iwl_mfuart_load_notif *mfuart_notif = (void *)pkt->data;
1290 
1291 	IWL_DEBUG_INFO(mvm,
1292 		       "MFUART: installed ver: 0x%08x, external ver: 0x%08x, status: 0x%08x, duration: 0x%08x\n",
1293 		       le32_to_cpu(mfuart_notif->installed_ver),
1294 		       le32_to_cpu(mfuart_notif->external_ver),
1295 		       le32_to_cpu(mfuart_notif->status),
1296 		       le32_to_cpu(mfuart_notif->duration));
1297 
1298 	if (iwl_rx_packet_payload_len(pkt) == sizeof(*mfuart_notif))
1299 		IWL_DEBUG_INFO(mvm,
1300 			       "MFUART: image size: 0x%08x\n",
1301 			       le32_to_cpu(mfuart_notif->image_size));
1302 }
1303