xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/ops.c (revision 235f0da3274690f540aa53fccf77d433e344e4b8)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2012-2014, 2018-2024 Intel Corporation
4  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5  * Copyright (C) 2016-2017 Intel Deutschland GmbH
6  */
7 #include <linux/module.h>
8 #include <linux/rtnetlink.h>
9 #include <linux/vmalloc.h>
10 #include <net/mac80211.h>
11 
12 #include "fw/notif-wait.h"
13 #include "iwl-trans.h"
14 #include "iwl-op-mode.h"
15 #include "fw/img.h"
16 #include "iwl-debug.h"
17 #include "iwl-drv.h"
18 #include "iwl-modparams.h"
19 #include "mvm.h"
20 #include "iwl-phy-db.h"
21 #include "iwl-nvm-utils.h"
22 #include "iwl-csr.h"
23 #include "iwl-io.h"
24 #include "iwl-prph.h"
25 #include "rs.h"
26 #include "fw/api/scan.h"
27 #include "fw/api/rfi.h"
28 #include "time-event.h"
29 #include "fw-api.h"
30 #include "fw/acpi.h"
31 #include "fw/uefi.h"
32 #include "time-sync.h"
33 
34 #define DRV_DESCRIPTION	"The new Intel(R) wireless AGN driver for Linux"
35 MODULE_DESCRIPTION(DRV_DESCRIPTION);
36 MODULE_LICENSE("GPL");
37 MODULE_IMPORT_NS(IWLWIFI);
38 
39 static const struct iwl_op_mode_ops iwl_mvm_ops;
40 static const struct iwl_op_mode_ops iwl_mvm_ops_mq;
41 
42 struct iwl_mvm_mod_params iwlmvm_mod_params = {
43 	.power_scheme = IWL_POWER_SCHEME_BPS,
44 };
45 
46 module_param_named(power_scheme, iwlmvm_mod_params.power_scheme, int, 0444);
47 MODULE_PARM_DESC(power_scheme,
48 		 "power management scheme: 1-active, 2-balanced, 3-low power, default: 2");
49 
50 /*
51  * module init and exit functions
52  */
53 static int __init iwl_mvm_init(void)
54 {
55 	int ret;
56 
57 	ret = iwl_mvm_rate_control_register();
58 	if (ret) {
59 		pr_err("Unable to register rate control algorithm: %d\n", ret);
60 		return ret;
61 	}
62 
63 	ret = iwl_opmode_register("iwlmvm", &iwl_mvm_ops);
64 	if (ret)
65 		pr_err("Unable to register MVM op_mode: %d\n", ret);
66 
67 	return ret;
68 }
69 module_init(iwl_mvm_init);
70 
71 static void __exit iwl_mvm_exit(void)
72 {
73 	iwl_opmode_deregister("iwlmvm");
74 	iwl_mvm_rate_control_unregister();
75 }
76 module_exit(iwl_mvm_exit);
77 
78 static void iwl_mvm_nic_config(struct iwl_op_mode *op_mode)
79 {
80 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
81 	u8 radio_cfg_type, radio_cfg_step, radio_cfg_dash;
82 	u32 reg_val;
83 	u32 phy_config = iwl_mvm_get_phy_config(mvm);
84 
85 	radio_cfg_type = (phy_config & FW_PHY_CFG_RADIO_TYPE) >>
86 			 FW_PHY_CFG_RADIO_TYPE_POS;
87 	radio_cfg_step = (phy_config & FW_PHY_CFG_RADIO_STEP) >>
88 			 FW_PHY_CFG_RADIO_STEP_POS;
89 	radio_cfg_dash = (phy_config & FW_PHY_CFG_RADIO_DASH) >>
90 			 FW_PHY_CFG_RADIO_DASH_POS;
91 
92 	IWL_DEBUG_INFO(mvm, "Radio type=0x%x-0x%x-0x%x\n", radio_cfg_type,
93 		       radio_cfg_step, radio_cfg_dash);
94 
95 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
96 		return;
97 
98 	/* SKU control */
99 	reg_val = CSR_HW_REV_STEP_DASH(mvm->trans->hw_rev);
100 
101 	/* radio configuration */
102 	reg_val |= radio_cfg_type << CSR_HW_IF_CONFIG_REG_POS_PHY_TYPE;
103 	reg_val |= radio_cfg_step << CSR_HW_IF_CONFIG_REG_POS_PHY_STEP;
104 	reg_val |= radio_cfg_dash << CSR_HW_IF_CONFIG_REG_POS_PHY_DASH;
105 
106 	WARN_ON((radio_cfg_type << CSR_HW_IF_CONFIG_REG_POS_PHY_TYPE) &
107 		 ~CSR_HW_IF_CONFIG_REG_MSK_PHY_TYPE);
108 
109 	/*
110 	 * TODO: Bits 7-8 of CSR in 8000 HW family and higher set the ADC
111 	 * sampling, and shouldn't be set to any non-zero value.
112 	 * The same is supposed to be true of the other HW, but unsetting
113 	 * them (such as the 7260) causes automatic tests to fail on seemingly
114 	 * unrelated errors. Need to further investigate this, but for now
115 	 * we'll separate cases.
116 	 */
117 	if (mvm->trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_8000)
118 		reg_val |= CSR_HW_IF_CONFIG_REG_BIT_RADIO_SI;
119 
120 	if (iwl_fw_dbg_is_d3_debug_enabled(&mvm->fwrt))
121 		reg_val |= CSR_HW_IF_CONFIG_REG_D3_DEBUG;
122 
123 	iwl_trans_set_bits_mask(mvm->trans, CSR_HW_IF_CONFIG_REG,
124 				CSR_HW_IF_CONFIG_REG_MSK_MAC_STEP_DASH |
125 				CSR_HW_IF_CONFIG_REG_MSK_PHY_TYPE |
126 				CSR_HW_IF_CONFIG_REG_MSK_PHY_STEP |
127 				CSR_HW_IF_CONFIG_REG_MSK_PHY_DASH |
128 				CSR_HW_IF_CONFIG_REG_BIT_RADIO_SI |
129 				CSR_HW_IF_CONFIG_REG_BIT_MAC_SI   |
130 				CSR_HW_IF_CONFIG_REG_D3_DEBUG,
131 				reg_val);
132 
133 	/*
134 	 * W/A : NIC is stuck in a reset state after Early PCIe power off
135 	 * (PCIe power is lost before PERST# is asserted), causing ME FW
136 	 * to lose ownership and not being able to obtain it back.
137 	 */
138 	if (!mvm->trans->cfg->apmg_not_supported)
139 		iwl_set_bits_mask_prph(mvm->trans, APMG_PS_CTRL_REG,
140 				       APMG_PS_CTRL_EARLY_PWR_OFF_RESET_DIS,
141 				       ~APMG_PS_CTRL_EARLY_PWR_OFF_RESET_DIS);
142 }
143 
144 static void iwl_mvm_rx_esr_mode_notif(struct iwl_mvm *mvm,
145 				      struct iwl_rx_cmd_buffer *rxb)
146 {
147 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
148 	struct iwl_mvm_esr_mode_notif *notif = (void *)pkt->data;
149 	struct ieee80211_vif *vif = iwl_mvm_get_bss_vif(mvm);
150 
151 	/* FW recommendations is only for entering EMLSR */
152 	if (IS_ERR_OR_NULL(vif) || iwl_mvm_vif_from_mac80211(vif)->esr_active)
153 		return;
154 
155 	if (le32_to_cpu(notif->action) == ESR_RECOMMEND_ENTER)
156 		iwl_mvm_unblock_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_FW);
157 	else
158 		iwl_mvm_block_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_FW,
159 				  iwl_mvm_get_primary_link(vif));
160 }
161 
162 static void iwl_mvm_rx_monitor_notif(struct iwl_mvm *mvm,
163 				     struct iwl_rx_cmd_buffer *rxb)
164 {
165 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
166 	struct iwl_datapath_monitor_notif *notif = (void *)pkt->data;
167 	struct ieee80211_supported_band *sband;
168 	const struct ieee80211_sta_he_cap *he_cap;
169 	struct ieee80211_vif *vif;
170 
171 	if (notif->type != cpu_to_le32(IWL_DP_MON_NOTIF_TYPE_EXT_CCA))
172 		return;
173 
174 	vif = iwl_mvm_get_vif_by_macid(mvm, notif->mac_id);
175 	if (!vif || vif->type != NL80211_IFTYPE_STATION)
176 		return;
177 
178 	if (!vif->bss_conf.chanreq.oper.chan ||
179 	    vif->bss_conf.chanreq.oper.chan->band != NL80211_BAND_2GHZ ||
180 	    vif->bss_conf.chanreq.oper.width < NL80211_CHAN_WIDTH_40)
181 		return;
182 
183 	if (!vif->cfg.assoc)
184 		return;
185 
186 	/* this shouldn't happen *again*, ignore it */
187 	if (mvm->cca_40mhz_workaround)
188 		return;
189 
190 	/*
191 	 * We'll decrement this on disconnect - so set to 2 since we'll
192 	 * still have to disconnect from the current AP first.
193 	 */
194 	mvm->cca_40mhz_workaround = 2;
195 
196 	/*
197 	 * This capability manipulation isn't really ideal, but it's the
198 	 * easiest choice - otherwise we'd have to do some major changes
199 	 * in mac80211 to support this, which isn't worth it. This does
200 	 * mean that userspace may have outdated information, but that's
201 	 * actually not an issue at all.
202 	 */
203 	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
204 
205 	WARN_ON(!sband->ht_cap.ht_supported);
206 	WARN_ON(!(sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40));
207 	sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
208 
209 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
210 
211 	if (he_cap) {
212 		/* we know that ours is writable */
213 		struct ieee80211_sta_he_cap *he = (void *)(uintptr_t)he_cap;
214 
215 		WARN_ON(!he->has_he);
216 		WARN_ON(!(he->he_cap_elem.phy_cap_info[0] &
217 				IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G));
218 		he->he_cap_elem.phy_cap_info[0] &=
219 			~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
220 	}
221 
222 	ieee80211_disconnect(vif, true);
223 }
224 
225 void iwl_mvm_update_link_smps(struct ieee80211_vif *vif,
226 			      struct ieee80211_bss_conf *link_conf)
227 {
228 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
229 	struct iwl_mvm *mvm = mvmvif->mvm;
230 	enum ieee80211_smps_mode mode = IEEE80211_SMPS_AUTOMATIC;
231 
232 	if (!link_conf)
233 		return;
234 
235 	if (mvm->fw_static_smps_request &&
236 	    link_conf->chanreq.oper.width == NL80211_CHAN_WIDTH_160 &&
237 	    link_conf->he_support)
238 		mode = IEEE80211_SMPS_STATIC;
239 
240 	iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_FW, mode,
241 			    link_conf->link_id);
242 }
243 
244 static void iwl_mvm_intf_dual_chain_req(void *data, u8 *mac,
245 					struct ieee80211_vif *vif)
246 {
247 	struct ieee80211_bss_conf *link_conf;
248 	unsigned int link_id;
249 
250 	rcu_read_lock();
251 
252 	for_each_vif_active_link(vif, link_conf, link_id)
253 		iwl_mvm_update_link_smps(vif, link_conf);
254 
255 	rcu_read_unlock();
256 }
257 
258 static void iwl_mvm_rx_thermal_dual_chain_req(struct iwl_mvm *mvm,
259 					      struct iwl_rx_cmd_buffer *rxb)
260 {
261 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
262 	struct iwl_thermal_dual_chain_request *req = (void *)pkt->data;
263 
264 	/*
265 	 * We could pass it to the iterator data, but also need to remember
266 	 * it for new interfaces that are added while in this state.
267 	 */
268 	mvm->fw_static_smps_request =
269 		req->event == cpu_to_le32(THERMAL_DUAL_CHAIN_REQ_DISABLE);
270 	ieee80211_iterate_interfaces(mvm->hw,
271 				     IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER,
272 				     iwl_mvm_intf_dual_chain_req, NULL);
273 }
274 
275 /**
276  * enum iwl_rx_handler_context: context for Rx handler
277  * @RX_HANDLER_SYNC : this means that it will be called in the Rx path
278  *	which can't acquire mvm->mutex.
279  * @RX_HANDLER_ASYNC_LOCKED : If the handler needs to hold mvm->mutex
280  *	(and only in this case!), it should be set as ASYNC. In that case,
281  *	it will be called from a worker with mvm->mutex held.
282  * @RX_HANDLER_ASYNC_UNLOCKED : in case the handler needs to lock the
283  *	mutex itself, it will be called from a worker without mvm->mutex held.
284  * @RX_HANDLER_ASYNC_LOCKED_WIPHY: If the handler needs to hold the wiphy lock
285  *	and mvm->mutex. Will be handled with the wiphy_work queue infra
286  *	instead of regular work queue.
287  */
288 enum iwl_rx_handler_context {
289 	RX_HANDLER_SYNC,
290 	RX_HANDLER_ASYNC_LOCKED,
291 	RX_HANDLER_ASYNC_UNLOCKED,
292 	RX_HANDLER_ASYNC_LOCKED_WIPHY,
293 };
294 
295 /**
296  * struct iwl_rx_handlers: handler for FW notification
297  * @cmd_id: command id
298  * @min_size: minimum size to expect for the notification
299  * @context: see &iwl_rx_handler_context
300  * @fn: the function is called when notification is received
301  */
302 struct iwl_rx_handlers {
303 	u16 cmd_id, min_size;
304 	enum iwl_rx_handler_context context;
305 	void (*fn)(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb);
306 };
307 
308 #define RX_HANDLER_NO_SIZE(_cmd_id, _fn, _context)		\
309 	{ .cmd_id = _cmd_id, .fn = _fn, .context = _context, }
310 #define RX_HANDLER_GRP_NO_SIZE(_grp, _cmd, _fn, _context)	\
311 	{ .cmd_id = WIDE_ID(_grp, _cmd), .fn = _fn, .context = _context, }
312 #define RX_HANDLER(_cmd_id, _fn, _context, _struct)		\
313 	{ .cmd_id = _cmd_id, .fn = _fn,				\
314 	  .context = _context, .min_size = sizeof(_struct), }
315 #define RX_HANDLER_GRP(_grp, _cmd, _fn, _context, _struct)	\
316 	{ .cmd_id = WIDE_ID(_grp, _cmd), .fn = _fn,		\
317 	  .context = _context, .min_size = sizeof(_struct), }
318 
319 /*
320  * Handlers for fw notifications
321  * Convention: RX_HANDLER(CMD_NAME, iwl_mvm_rx_CMD_NAME
322  * This list should be in order of frequency for performance purposes.
323  *
324  * The handler can be one from three contexts, see &iwl_rx_handler_context
325  */
326 static const struct iwl_rx_handlers iwl_mvm_rx_handlers[] = {
327 	RX_HANDLER(TX_CMD, iwl_mvm_rx_tx_cmd, RX_HANDLER_SYNC,
328 		   struct iwl_mvm_tx_resp),
329 	RX_HANDLER(BA_NOTIF, iwl_mvm_rx_ba_notif, RX_HANDLER_SYNC,
330 		   struct iwl_mvm_ba_notif),
331 
332 	RX_HANDLER_GRP(DATA_PATH_GROUP, TLC_MNG_UPDATE_NOTIF,
333 		       iwl_mvm_tlc_update_notif, RX_HANDLER_SYNC,
334 		       struct iwl_tlc_update_notif),
335 
336 	RX_HANDLER(BT_PROFILE_NOTIFICATION, iwl_mvm_rx_bt_coex_notif,
337 		   RX_HANDLER_ASYNC_LOCKED_WIPHY,
338 		   struct iwl_bt_coex_profile_notif),
339 	RX_HANDLER_NO_SIZE(BEACON_NOTIFICATION, iwl_mvm_rx_beacon_notif,
340 			   RX_HANDLER_ASYNC_LOCKED),
341 	RX_HANDLER_NO_SIZE(STATISTICS_NOTIFICATION, iwl_mvm_rx_statistics,
342 			   RX_HANDLER_ASYNC_LOCKED),
343 
344 	RX_HANDLER_GRP(STATISTICS_GROUP, STATISTICS_OPER_NOTIF,
345 		       iwl_mvm_handle_rx_system_oper_stats,
346 		       RX_HANDLER_ASYNC_LOCKED_WIPHY,
347 		       struct iwl_system_statistics_notif_oper),
348 	RX_HANDLER_GRP(STATISTICS_GROUP, STATISTICS_OPER_PART1_NOTIF,
349 		       iwl_mvm_handle_rx_system_oper_part1_stats,
350 		       RX_HANDLER_ASYNC_LOCKED,
351 		       struct iwl_system_statistics_part1_notif_oper),
352 	RX_HANDLER_GRP(SYSTEM_GROUP, SYSTEM_STATISTICS_END_NOTIF,
353 		       iwl_mvm_handle_rx_system_end_stats_notif,
354 		       RX_HANDLER_ASYNC_LOCKED,
355 		       struct iwl_system_statistics_end_notif),
356 
357 	RX_HANDLER(BA_WINDOW_STATUS_NOTIFICATION_ID,
358 		   iwl_mvm_window_status_notif, RX_HANDLER_SYNC,
359 		   struct iwl_ba_window_status_notif),
360 
361 	RX_HANDLER(TIME_EVENT_NOTIFICATION, iwl_mvm_rx_time_event_notif,
362 		   RX_HANDLER_SYNC, struct iwl_time_event_notif),
363 	RX_HANDLER_GRP(MAC_CONF_GROUP, SESSION_PROTECTION_NOTIF,
364 		       iwl_mvm_rx_session_protect_notif, RX_HANDLER_SYNC,
365 		       struct iwl_mvm_session_prot_notif),
366 	RX_HANDLER(MCC_CHUB_UPDATE_CMD, iwl_mvm_rx_chub_update_mcc,
367 		   RX_HANDLER_ASYNC_LOCKED, struct iwl_mcc_chub_notif),
368 
369 	RX_HANDLER(EOSP_NOTIFICATION, iwl_mvm_rx_eosp_notif, RX_HANDLER_SYNC,
370 		   struct iwl_mvm_eosp_notification),
371 
372 	RX_HANDLER(SCAN_ITERATION_COMPLETE,
373 		   iwl_mvm_rx_lmac_scan_iter_complete_notif, RX_HANDLER_SYNC,
374 		   struct iwl_lmac_scan_complete_notif),
375 	RX_HANDLER(SCAN_OFFLOAD_COMPLETE,
376 		   iwl_mvm_rx_lmac_scan_complete_notif,
377 		   RX_HANDLER_ASYNC_LOCKED, struct iwl_periodic_scan_complete),
378 	RX_HANDLER_NO_SIZE(MATCH_FOUND_NOTIFICATION,
379 			   iwl_mvm_rx_scan_match_found,
380 			   RX_HANDLER_SYNC),
381 	RX_HANDLER(SCAN_COMPLETE_UMAC, iwl_mvm_rx_umac_scan_complete_notif,
382 		   RX_HANDLER_ASYNC_LOCKED,
383 		   struct iwl_umac_scan_complete),
384 	RX_HANDLER(SCAN_ITERATION_COMPLETE_UMAC,
385 		   iwl_mvm_rx_umac_scan_iter_complete_notif, RX_HANDLER_SYNC,
386 		   struct iwl_umac_scan_iter_complete_notif),
387 
388 	RX_HANDLER(MISSED_BEACONS_NOTIFICATION, iwl_mvm_rx_missed_beacons_notif,
389 		   RX_HANDLER_ASYNC_LOCKED_WIPHY,
390 		   struct iwl_missed_beacons_notif),
391 
392 	RX_HANDLER(REPLY_ERROR, iwl_mvm_rx_fw_error, RX_HANDLER_SYNC,
393 		   struct iwl_error_resp),
394 	RX_HANDLER(PSM_UAPSD_AP_MISBEHAVING_NOTIFICATION,
395 		   iwl_mvm_power_uapsd_misbehaving_ap_notif, RX_HANDLER_SYNC,
396 		   struct iwl_uapsd_misbehaving_ap_notif),
397 	RX_HANDLER_NO_SIZE(DTS_MEASUREMENT_NOTIFICATION, iwl_mvm_temp_notif,
398 			   RX_HANDLER_ASYNC_LOCKED),
399 	RX_HANDLER_GRP_NO_SIZE(PHY_OPS_GROUP, DTS_MEASUREMENT_NOTIF_WIDE,
400 			       iwl_mvm_temp_notif, RX_HANDLER_ASYNC_UNLOCKED),
401 	RX_HANDLER_GRP(PHY_OPS_GROUP, CT_KILL_NOTIFICATION,
402 		       iwl_mvm_ct_kill_notif, RX_HANDLER_SYNC,
403 		       struct ct_kill_notif),
404 
405 	RX_HANDLER(TDLS_CHANNEL_SWITCH_NOTIFICATION, iwl_mvm_rx_tdls_notif,
406 		   RX_HANDLER_ASYNC_LOCKED,
407 		   struct iwl_tdls_channel_switch_notif),
408 	RX_HANDLER(MFUART_LOAD_NOTIFICATION, iwl_mvm_rx_mfuart_notif,
409 		   RX_HANDLER_SYNC, struct iwl_mfuart_load_notif_v1),
410 	RX_HANDLER_GRP(LOCATION_GROUP, TOF_RESPONDER_STATS,
411 		       iwl_mvm_ftm_responder_stats, RX_HANDLER_ASYNC_LOCKED,
412 		       struct iwl_ftm_responder_stats),
413 
414 	RX_HANDLER_GRP_NO_SIZE(LOCATION_GROUP, TOF_RANGE_RESPONSE_NOTIF,
415 			       iwl_mvm_ftm_range_resp, RX_HANDLER_ASYNC_LOCKED),
416 	RX_HANDLER_GRP_NO_SIZE(LOCATION_GROUP, TOF_LC_NOTIF,
417 			       iwl_mvm_ftm_lc_notif, RX_HANDLER_ASYNC_LOCKED),
418 
419 	RX_HANDLER_GRP(DEBUG_GROUP, MFU_ASSERT_DUMP_NTF,
420 		       iwl_mvm_mfu_assert_dump_notif, RX_HANDLER_SYNC,
421 		       struct iwl_mfu_assert_dump_notif),
422 	RX_HANDLER_GRP(PROT_OFFLOAD_GROUP, STORED_BEACON_NTF,
423 		       iwl_mvm_rx_stored_beacon_notif, RX_HANDLER_SYNC,
424 		       struct iwl_stored_beacon_notif_v2),
425 	RX_HANDLER_GRP(DATA_PATH_GROUP, MU_GROUP_MGMT_NOTIF,
426 		       iwl_mvm_mu_mimo_grp_notif, RX_HANDLER_SYNC,
427 		       struct iwl_mu_group_mgmt_notif),
428 	RX_HANDLER_GRP(DATA_PATH_GROUP, STA_PM_NOTIF,
429 		       iwl_mvm_sta_pm_notif, RX_HANDLER_SYNC,
430 		       struct iwl_mvm_pm_state_notification),
431 	RX_HANDLER_GRP(MAC_CONF_GROUP, PROBE_RESPONSE_DATA_NOTIF,
432 		       iwl_mvm_probe_resp_data_notif,
433 		       RX_HANDLER_ASYNC_LOCKED,
434 		       struct iwl_probe_resp_data_notif),
435 	RX_HANDLER_GRP(MAC_CONF_GROUP, CHANNEL_SWITCH_START_NOTIF,
436 		       iwl_mvm_channel_switch_start_notif,
437 		       RX_HANDLER_SYNC, struct iwl_channel_switch_start_notif),
438 	RX_HANDLER_GRP(MAC_CONF_GROUP, CHANNEL_SWITCH_ERROR_NOTIF,
439 		       iwl_mvm_channel_switch_error_notif,
440 		       RX_HANDLER_ASYNC_UNLOCKED,
441 		       struct iwl_channel_switch_error_notif),
442 
443 	RX_HANDLER_GRP(DATA_PATH_GROUP, ESR_MODE_NOTIF,
444 		       iwl_mvm_rx_esr_mode_notif,
445 		       RX_HANDLER_ASYNC_LOCKED_WIPHY,
446 		       struct iwl_mvm_esr_mode_notif),
447 
448 	RX_HANDLER_GRP(DATA_PATH_GROUP, MONITOR_NOTIF,
449 		       iwl_mvm_rx_monitor_notif, RX_HANDLER_ASYNC_LOCKED,
450 		       struct iwl_datapath_monitor_notif),
451 
452 	RX_HANDLER_GRP(DATA_PATH_GROUP, THERMAL_DUAL_CHAIN_REQUEST,
453 		       iwl_mvm_rx_thermal_dual_chain_req,
454 		       RX_HANDLER_ASYNC_LOCKED,
455 		       struct iwl_thermal_dual_chain_request),
456 
457 	RX_HANDLER_GRP(SYSTEM_GROUP, RFI_DEACTIVATE_NOTIF,
458 		       iwl_rfi_deactivate_notif_handler, RX_HANDLER_ASYNC_UNLOCKED,
459 		       struct iwl_rfi_deactivate_notif),
460 
461 	RX_HANDLER_GRP(LEGACY_GROUP,
462 		       WNM_80211V_TIMING_MEASUREMENT_NOTIFICATION,
463 		       iwl_mvm_time_sync_msmt_event, RX_HANDLER_SYNC,
464 		       struct iwl_time_msmt_notify),
465 	RX_HANDLER_GRP(LEGACY_GROUP,
466 		       WNM_80211V_TIMING_MEASUREMENT_CONFIRM_NOTIFICATION,
467 		       iwl_mvm_time_sync_msmt_confirm_event, RX_HANDLER_SYNC,
468 		       struct iwl_time_msmt_cfm_notify),
469 	RX_HANDLER_GRP(MAC_CONF_GROUP, ROC_NOTIF,
470 		       iwl_mvm_rx_roc_notif, RX_HANDLER_ASYNC_LOCKED,
471 		       struct iwl_roc_notif),
472 	RX_HANDLER_GRP(SCAN_GROUP, CHANNEL_SURVEY_NOTIF,
473 		       iwl_mvm_rx_channel_survey_notif, RX_HANDLER_ASYNC_LOCKED,
474 		       struct iwl_umac_scan_channel_survey_notif),
475 };
476 #undef RX_HANDLER
477 #undef RX_HANDLER_GRP
478 
479 /* Please keep this array *SORTED* by hex value.
480  * Access is done through binary search
481  */
482 static const struct iwl_hcmd_names iwl_mvm_legacy_names[] = {
483 	HCMD_NAME(UCODE_ALIVE_NTFY),
484 	HCMD_NAME(REPLY_ERROR),
485 	HCMD_NAME(ECHO_CMD),
486 	HCMD_NAME(INIT_COMPLETE_NOTIF),
487 	HCMD_NAME(PHY_CONTEXT_CMD),
488 	HCMD_NAME(DBG_CFG),
489 	HCMD_NAME(SCAN_CFG_CMD),
490 	HCMD_NAME(SCAN_REQ_UMAC),
491 	HCMD_NAME(SCAN_ABORT_UMAC),
492 	HCMD_NAME(SCAN_COMPLETE_UMAC),
493 	HCMD_NAME(BA_WINDOW_STATUS_NOTIFICATION_ID),
494 	HCMD_NAME(ADD_STA_KEY),
495 	HCMD_NAME(ADD_STA),
496 	HCMD_NAME(REMOVE_STA),
497 	HCMD_NAME(TX_CMD),
498 	HCMD_NAME(SCD_QUEUE_CFG),
499 	HCMD_NAME(TXPATH_FLUSH),
500 	HCMD_NAME(MGMT_MCAST_KEY),
501 	HCMD_NAME(WEP_KEY),
502 	HCMD_NAME(SHARED_MEM_CFG),
503 	HCMD_NAME(TDLS_CHANNEL_SWITCH_CMD),
504 	HCMD_NAME(MAC_CONTEXT_CMD),
505 	HCMD_NAME(TIME_EVENT_CMD),
506 	HCMD_NAME(TIME_EVENT_NOTIFICATION),
507 	HCMD_NAME(BINDING_CONTEXT_CMD),
508 	HCMD_NAME(TIME_QUOTA_CMD),
509 	HCMD_NAME(NON_QOS_TX_COUNTER_CMD),
510 	HCMD_NAME(LEDS_CMD),
511 	HCMD_NAME(LQ_CMD),
512 	HCMD_NAME(FW_PAGING_BLOCK_CMD),
513 	HCMD_NAME(SCAN_OFFLOAD_REQUEST_CMD),
514 	HCMD_NAME(SCAN_OFFLOAD_ABORT_CMD),
515 	HCMD_NAME(HOT_SPOT_CMD),
516 	HCMD_NAME(SCAN_OFFLOAD_PROFILES_QUERY_CMD),
517 	HCMD_NAME(BT_COEX_UPDATE_REDUCED_TXP),
518 	HCMD_NAME(BT_COEX_CI),
519 	HCMD_NAME(WNM_80211V_TIMING_MEASUREMENT_NOTIFICATION),
520 	HCMD_NAME(WNM_80211V_TIMING_MEASUREMENT_CONFIRM_NOTIFICATION),
521 	HCMD_NAME(PHY_CONFIGURATION_CMD),
522 	HCMD_NAME(CALIB_RES_NOTIF_PHY_DB),
523 	HCMD_NAME(PHY_DB_CMD),
524 	HCMD_NAME(SCAN_OFFLOAD_COMPLETE),
525 	HCMD_NAME(SCAN_OFFLOAD_UPDATE_PROFILES_CMD),
526 	HCMD_NAME(POWER_TABLE_CMD),
527 	HCMD_NAME(PSM_UAPSD_AP_MISBEHAVING_NOTIFICATION),
528 	HCMD_NAME(REPLY_THERMAL_MNG_BACKOFF),
529 	HCMD_NAME(NVM_ACCESS_CMD),
530 	HCMD_NAME(BEACON_NOTIFICATION),
531 	HCMD_NAME(BEACON_TEMPLATE_CMD),
532 	HCMD_NAME(TX_ANT_CONFIGURATION_CMD),
533 	HCMD_NAME(BT_CONFIG),
534 	HCMD_NAME(STATISTICS_CMD),
535 	HCMD_NAME(STATISTICS_NOTIFICATION),
536 	HCMD_NAME(EOSP_NOTIFICATION),
537 	HCMD_NAME(REDUCE_TX_POWER_CMD),
538 	HCMD_NAME(MISSED_BEACONS_NOTIFICATION),
539 	HCMD_NAME(TDLS_CONFIG_CMD),
540 	HCMD_NAME(MAC_PM_POWER_TABLE),
541 	HCMD_NAME(TDLS_CHANNEL_SWITCH_NOTIFICATION),
542 	HCMD_NAME(MFUART_LOAD_NOTIFICATION),
543 	HCMD_NAME(RSS_CONFIG_CMD),
544 	HCMD_NAME(SCAN_ITERATION_COMPLETE_UMAC),
545 	HCMD_NAME(REPLY_RX_PHY_CMD),
546 	HCMD_NAME(REPLY_RX_MPDU_CMD),
547 	HCMD_NAME(BAR_FRAME_RELEASE),
548 	HCMD_NAME(FRAME_RELEASE),
549 	HCMD_NAME(BA_NOTIF),
550 	HCMD_NAME(MCC_UPDATE_CMD),
551 	HCMD_NAME(MCC_CHUB_UPDATE_CMD),
552 	HCMD_NAME(MARKER_CMD),
553 	HCMD_NAME(BT_PROFILE_NOTIFICATION),
554 	HCMD_NAME(MCAST_FILTER_CMD),
555 	HCMD_NAME(REPLY_SF_CFG_CMD),
556 	HCMD_NAME(REPLY_BEACON_FILTERING_CMD),
557 	HCMD_NAME(D3_CONFIG_CMD),
558 	HCMD_NAME(PROT_OFFLOAD_CONFIG_CMD),
559 	HCMD_NAME(MATCH_FOUND_NOTIFICATION),
560 	HCMD_NAME(DTS_MEASUREMENT_NOTIFICATION),
561 	HCMD_NAME(WOWLAN_PATTERNS),
562 	HCMD_NAME(WOWLAN_CONFIGURATION),
563 	HCMD_NAME(WOWLAN_TSC_RSC_PARAM),
564 	HCMD_NAME(WOWLAN_TKIP_PARAM),
565 	HCMD_NAME(WOWLAN_KEK_KCK_MATERIAL),
566 	HCMD_NAME(WOWLAN_GET_STATUSES),
567 	HCMD_NAME(SCAN_ITERATION_COMPLETE),
568 	HCMD_NAME(D0I3_END_CMD),
569 	HCMD_NAME(LTR_CONFIG),
570 	HCMD_NAME(LDBG_CONFIG_CMD),
571 	HCMD_NAME(DEBUG_LOG_MSG),
572 };
573 
574 /* Please keep this array *SORTED* by hex value.
575  * Access is done through binary search
576  */
577 static const struct iwl_hcmd_names iwl_mvm_system_names[] = {
578 	HCMD_NAME(SHARED_MEM_CFG_CMD),
579 	HCMD_NAME(SOC_CONFIGURATION_CMD),
580 	HCMD_NAME(INIT_EXTENDED_CFG_CMD),
581 	HCMD_NAME(FW_ERROR_RECOVERY_CMD),
582 	HCMD_NAME(RFI_CONFIG_CMD),
583 	HCMD_NAME(RFI_GET_FREQ_TABLE_CMD),
584 	HCMD_NAME(SYSTEM_FEATURES_CONTROL_CMD),
585 	HCMD_NAME(SYSTEM_STATISTICS_CMD),
586 	HCMD_NAME(SYSTEM_STATISTICS_END_NOTIF),
587 	HCMD_NAME(RFI_DEACTIVATE_NOTIF),
588 };
589 
590 /* Please keep this array *SORTED* by hex value.
591  * Access is done through binary search
592  */
593 static const struct iwl_hcmd_names iwl_mvm_mac_conf_names[] = {
594 	HCMD_NAME(LOW_LATENCY_CMD),
595 	HCMD_NAME(CHANNEL_SWITCH_TIME_EVENT_CMD),
596 	HCMD_NAME(SESSION_PROTECTION_CMD),
597 	HCMD_NAME(CANCEL_CHANNEL_SWITCH_CMD),
598 	HCMD_NAME(MAC_CONFIG_CMD),
599 	HCMD_NAME(LINK_CONFIG_CMD),
600 	HCMD_NAME(STA_CONFIG_CMD),
601 	HCMD_NAME(AUX_STA_CMD),
602 	HCMD_NAME(STA_REMOVE_CMD),
603 	HCMD_NAME(STA_DISABLE_TX_CMD),
604 	HCMD_NAME(ROC_CMD),
605 	HCMD_NAME(ROC_NOTIF),
606 	HCMD_NAME(CHANNEL_SWITCH_ERROR_NOTIF),
607 	HCMD_NAME(MISSED_VAP_NOTIF),
608 	HCMD_NAME(SESSION_PROTECTION_NOTIF),
609 	HCMD_NAME(PROBE_RESPONSE_DATA_NOTIF),
610 	HCMD_NAME(CHANNEL_SWITCH_START_NOTIF),
611 };
612 
613 /* Please keep this array *SORTED* by hex value.
614  * Access is done through binary search
615  */
616 static const struct iwl_hcmd_names iwl_mvm_phy_names[] = {
617 	HCMD_NAME(CMD_DTS_MEASUREMENT_TRIGGER_WIDE),
618 	HCMD_NAME(CTDP_CONFIG_CMD),
619 	HCMD_NAME(TEMP_REPORTING_THRESHOLDS_CMD),
620 	HCMD_NAME(PER_CHAIN_LIMIT_OFFSET_CMD),
621 	HCMD_NAME(AP_TX_POWER_CONSTRAINTS_CMD),
622 	HCMD_NAME(CT_KILL_NOTIFICATION),
623 	HCMD_NAME(DTS_MEASUREMENT_NOTIF_WIDE),
624 };
625 
626 /* Please keep this array *SORTED* by hex value.
627  * Access is done through binary search
628  */
629 static const struct iwl_hcmd_names iwl_mvm_data_path_names[] = {
630 	HCMD_NAME(DQA_ENABLE_CMD),
631 	HCMD_NAME(UPDATE_MU_GROUPS_CMD),
632 	HCMD_NAME(TRIGGER_RX_QUEUES_NOTIF_CMD),
633 	HCMD_NAME(WNM_PLATFORM_PTM_REQUEST_CMD),
634 	HCMD_NAME(WNM_80211V_TIMING_MEASUREMENT_CONFIG_CMD),
635 	HCMD_NAME(STA_HE_CTXT_CMD),
636 	HCMD_NAME(RLC_CONFIG_CMD),
637 	HCMD_NAME(RFH_QUEUE_CONFIG_CMD),
638 	HCMD_NAME(TLC_MNG_CONFIG_CMD),
639 	HCMD_NAME(CHEST_COLLECTOR_FILTER_CONFIG_CMD),
640 	HCMD_NAME(SCD_QUEUE_CONFIG_CMD),
641 	HCMD_NAME(SEC_KEY_CMD),
642 	HCMD_NAME(ESR_MODE_NOTIF),
643 	HCMD_NAME(MONITOR_NOTIF),
644 	HCMD_NAME(THERMAL_DUAL_CHAIN_REQUEST),
645 	HCMD_NAME(STA_PM_NOTIF),
646 	HCMD_NAME(MU_GROUP_MGMT_NOTIF),
647 	HCMD_NAME(RX_QUEUES_NOTIFICATION),
648 };
649 
650 /* Please keep this array *SORTED* by hex value.
651  * Access is done through binary search
652  */
653 static const struct iwl_hcmd_names iwl_mvm_statistics_names[] = {
654 	HCMD_NAME(STATISTICS_OPER_NOTIF),
655 	HCMD_NAME(STATISTICS_OPER_PART1_NOTIF),
656 };
657 
658 /* Please keep this array *SORTED* by hex value.
659  * Access is done through binary search
660  */
661 static const struct iwl_hcmd_names iwl_mvm_debug_names[] = {
662 	HCMD_NAME(LMAC_RD_WR),
663 	HCMD_NAME(UMAC_RD_WR),
664 	HCMD_NAME(HOST_EVENT_CFG),
665 	HCMD_NAME(DBGC_SUSPEND_RESUME),
666 	HCMD_NAME(BUFFER_ALLOCATION),
667 	HCMD_NAME(GET_TAS_STATUS),
668 	HCMD_NAME(FW_DUMP_COMPLETE_CMD),
669 	HCMD_NAME(FW_CLEAR_BUFFER),
670 	HCMD_NAME(MFU_ASSERT_DUMP_NTF),
671 };
672 
673 /* Please keep this array *SORTED* by hex value.
674  * Access is done through binary search
675  */
676 static const struct iwl_hcmd_names iwl_mvm_scan_names[] = {
677 	HCMD_NAME(CHANNEL_SURVEY_NOTIF),
678 	HCMD_NAME(OFFLOAD_MATCH_INFO_NOTIF),
679 };
680 
681 /* Please keep this array *SORTED* by hex value.
682  * Access is done through binary search
683  */
684 static const struct iwl_hcmd_names iwl_mvm_location_names[] = {
685 	HCMD_NAME(TOF_RANGE_REQ_CMD),
686 	HCMD_NAME(TOF_CONFIG_CMD),
687 	HCMD_NAME(TOF_RANGE_ABORT_CMD),
688 	HCMD_NAME(TOF_RANGE_REQ_EXT_CMD),
689 	HCMD_NAME(TOF_RESPONDER_CONFIG_CMD),
690 	HCMD_NAME(TOF_RESPONDER_DYN_CONFIG_CMD),
691 	HCMD_NAME(TOF_LC_NOTIF),
692 	HCMD_NAME(TOF_RESPONDER_STATS),
693 	HCMD_NAME(TOF_MCSI_DEBUG_NOTIF),
694 	HCMD_NAME(TOF_RANGE_RESPONSE_NOTIF),
695 };
696 
697 /* Please keep this array *SORTED* by hex value.
698  * Access is done through binary search
699  */
700 static const struct iwl_hcmd_names iwl_mvm_prot_offload_names[] = {
701 	HCMD_NAME(WOWLAN_WAKE_PKT_NOTIFICATION),
702 	HCMD_NAME(WOWLAN_INFO_NOTIFICATION),
703 	HCMD_NAME(D3_END_NOTIFICATION),
704 	HCMD_NAME(STORED_BEACON_NTF),
705 };
706 
707 /* Please keep this array *SORTED* by hex value.
708  * Access is done through binary search
709  */
710 static const struct iwl_hcmd_names iwl_mvm_regulatory_and_nvm_names[] = {
711 	HCMD_NAME(NVM_ACCESS_COMPLETE),
712 	HCMD_NAME(NVM_GET_INFO),
713 	HCMD_NAME(TAS_CONFIG),
714 };
715 
716 static const struct iwl_hcmd_arr iwl_mvm_groups[] = {
717 	[LEGACY_GROUP] = HCMD_ARR(iwl_mvm_legacy_names),
718 	[LONG_GROUP] = HCMD_ARR(iwl_mvm_legacy_names),
719 	[SYSTEM_GROUP] = HCMD_ARR(iwl_mvm_system_names),
720 	[MAC_CONF_GROUP] = HCMD_ARR(iwl_mvm_mac_conf_names),
721 	[PHY_OPS_GROUP] = HCMD_ARR(iwl_mvm_phy_names),
722 	[DATA_PATH_GROUP] = HCMD_ARR(iwl_mvm_data_path_names),
723 	[SCAN_GROUP] = HCMD_ARR(iwl_mvm_scan_names),
724 	[LOCATION_GROUP] = HCMD_ARR(iwl_mvm_location_names),
725 	[PROT_OFFLOAD_GROUP] = HCMD_ARR(iwl_mvm_prot_offload_names),
726 	[REGULATORY_AND_NVM_GROUP] =
727 		HCMD_ARR(iwl_mvm_regulatory_and_nvm_names),
728 	[DEBUG_GROUP] = HCMD_ARR(iwl_mvm_debug_names),
729 	[STATISTICS_GROUP] = HCMD_ARR(iwl_mvm_statistics_names),
730 };
731 
732 /* this forward declaration can avoid to export the function */
733 static void iwl_mvm_async_handlers_wk(struct work_struct *wk);
734 static void iwl_mvm_async_handlers_wiphy_wk(struct wiphy *wiphy,
735 					    struct wiphy_work *work);
736 
737 static u32 iwl_mvm_min_backoff(struct iwl_mvm *mvm)
738 {
739 	const struct iwl_pwr_tx_backoff *backoff = mvm->cfg->pwr_tx_backoffs;
740 	u64 dflt_pwr_limit;
741 
742 	if (!backoff)
743 		return 0;
744 
745 	iwl_bios_get_pwr_limit(&mvm->fwrt, &dflt_pwr_limit);
746 
747 	while (backoff->pwr) {
748 		if (dflt_pwr_limit >= backoff->pwr)
749 			return backoff->backoff;
750 
751 		backoff++;
752 	}
753 
754 	return 0;
755 }
756 
757 static void iwl_mvm_tx_unblock_dwork(struct work_struct *work)
758 {
759 	struct iwl_mvm *mvm =
760 		container_of(work, struct iwl_mvm, cs_tx_unblock_dwork.work);
761 	struct ieee80211_vif *tx_blocked_vif;
762 	struct iwl_mvm_vif *mvmvif;
763 
764 	guard(mvm)(mvm);
765 
766 	tx_blocked_vif =
767 		rcu_dereference_protected(mvm->csa_tx_blocked_vif,
768 					  lockdep_is_held(&mvm->mutex));
769 
770 	if (!tx_blocked_vif)
771 		return;
772 
773 	mvmvif = iwl_mvm_vif_from_mac80211(tx_blocked_vif);
774 	iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
775 	RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
776 }
777 
778 static void iwl_mvm_fwrt_dump_start(void *ctx)
779 {
780 	struct iwl_mvm *mvm = ctx;
781 
782 	mutex_lock(&mvm->mutex);
783 }
784 
785 static void iwl_mvm_fwrt_dump_end(void *ctx)
786 {
787 	struct iwl_mvm *mvm = ctx;
788 
789 	mutex_unlock(&mvm->mutex);
790 }
791 
792 static int iwl_mvm_fwrt_send_hcmd(void *ctx, struct iwl_host_cmd *host_cmd)
793 {
794 	struct iwl_mvm *mvm = (struct iwl_mvm *)ctx;
795 
796 	guard(mvm)(mvm);
797 	return iwl_mvm_send_cmd(mvm, host_cmd);
798 }
799 
800 static bool iwl_mvm_d3_debug_enable(void *ctx)
801 {
802 	return IWL_MVM_D3_DEBUG;
803 }
804 
805 static const struct iwl_fw_runtime_ops iwl_mvm_fwrt_ops = {
806 	.dump_start = iwl_mvm_fwrt_dump_start,
807 	.dump_end = iwl_mvm_fwrt_dump_end,
808 	.send_hcmd = iwl_mvm_fwrt_send_hcmd,
809 	.d3_debug_enable = iwl_mvm_d3_debug_enable,
810 };
811 
812 static int iwl_mvm_start_get_nvm(struct iwl_mvm *mvm)
813 {
814 	struct iwl_trans *trans = mvm->trans;
815 	int ret;
816 
817 	if (trans->csme_own) {
818 		if (WARN(!mvm->mei_registered,
819 			 "csme is owner, but we aren't registered to iwlmei\n"))
820 			goto get_nvm_from_fw;
821 
822 		mvm->mei_nvm_data = iwl_mei_get_nvm();
823 		if (mvm->mei_nvm_data) {
824 			/*
825 			 * mvm->mei_nvm_data is set and because of that,
826 			 * we'll load the NVM from the FW when we'll get
827 			 * ownership.
828 			 */
829 			mvm->nvm_data =
830 				iwl_parse_mei_nvm_data(trans, trans->cfg,
831 						       mvm->mei_nvm_data,
832 						       mvm->fw,
833 						       mvm->set_tx_ant,
834 						       mvm->set_rx_ant);
835 			return 0;
836 		}
837 
838 		IWL_ERR(mvm,
839 			"Got a NULL NVM from CSME, trying to get it from the device\n");
840 	}
841 
842 get_nvm_from_fw:
843 	rtnl_lock();
844 	wiphy_lock(mvm->hw->wiphy);
845 	mutex_lock(&mvm->mutex);
846 
847 	ret = iwl_trans_start_hw(mvm->trans);
848 	if (ret) {
849 		mutex_unlock(&mvm->mutex);
850 		wiphy_unlock(mvm->hw->wiphy);
851 		rtnl_unlock();
852 		return ret;
853 	}
854 
855 	ret = iwl_run_init_mvm_ucode(mvm);
856 	if (ret && ret != -ERFKILL)
857 		iwl_fw_dbg_error_collect(&mvm->fwrt, FW_DBG_TRIGGER_DRIVER);
858 	if (!ret && iwl_mvm_is_lar_supported(mvm)) {
859 		mvm->hw->wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
860 		ret = iwl_mvm_init_mcc(mvm);
861 	}
862 
863 	iwl_mvm_stop_device(mvm);
864 
865 	mutex_unlock(&mvm->mutex);
866 	wiphy_unlock(mvm->hw->wiphy);
867 	rtnl_unlock();
868 
869 	if (ret)
870 		IWL_ERR(mvm, "Failed to run INIT ucode: %d\n", ret);
871 
872 	/* no longer need this regardless of failure or not */
873 	mvm->fw_product_reset = false;
874 
875 	return ret;
876 }
877 
878 static int iwl_mvm_start_post_nvm(struct iwl_mvm *mvm)
879 {
880 	struct iwl_mvm_csme_conn_info *csme_conn_info __maybe_unused;
881 	int ret;
882 
883 	iwl_mvm_toggle_tx_ant(mvm, &mvm->mgmt_last_antenna_idx);
884 
885 	ret = iwl_mvm_mac_setup_register(mvm);
886 	if (ret)
887 		return ret;
888 
889 	mvm->hw_registered = true;
890 
891 	iwl_mvm_dbgfs_register(mvm);
892 
893 	wiphy_rfkill_set_hw_state_reason(mvm->hw->wiphy,
894 					 mvm->mei_rfkill_blocked,
895 					 RFKILL_HARD_BLOCK_NOT_OWNER);
896 
897 	iwl_mvm_mei_set_sw_rfkill_state(mvm);
898 
899 	return 0;
900 }
901 
902 struct iwl_mvm_frob_txf_data {
903 	u8 *buf;
904 	size_t buflen;
905 };
906 
907 static void iwl_mvm_frob_txf_key_iter(struct ieee80211_hw *hw,
908 				      struct ieee80211_vif *vif,
909 				      struct ieee80211_sta *sta,
910 				      struct ieee80211_key_conf *key,
911 				      void *data)
912 {
913 	struct iwl_mvm_frob_txf_data *txf = data;
914 	u8 keylen, match, matchend;
915 	u8 *keydata;
916 	size_t i;
917 
918 	switch (key->cipher) {
919 	case WLAN_CIPHER_SUITE_CCMP:
920 		keydata = key->key;
921 		keylen = key->keylen;
922 		break;
923 	case WLAN_CIPHER_SUITE_WEP40:
924 	case WLAN_CIPHER_SUITE_WEP104:
925 	case WLAN_CIPHER_SUITE_TKIP:
926 		/*
927 		 * WEP has short keys which might show up in the payload,
928 		 * and then you can deduce the key, so in this case just
929 		 * remove all FIFO data.
930 		 * For TKIP, we don't know the phase 2 keys here, so same.
931 		 */
932 		memset(txf->buf, 0xBB, txf->buflen);
933 		return;
934 	default:
935 		return;
936 	}
937 
938 	/* scan for key material and clear it out */
939 	match = 0;
940 	for (i = 0; i < txf->buflen; i++) {
941 		if (txf->buf[i] != keydata[match]) {
942 			match = 0;
943 			continue;
944 		}
945 		match++;
946 		if (match == keylen) {
947 			memset(txf->buf + i - keylen, 0xAA, keylen);
948 			match = 0;
949 		}
950 	}
951 
952 	/* we're dealing with a FIFO, so check wrapped around data */
953 	matchend = match;
954 	for (i = 0; match && i < keylen - match; i++) {
955 		if (txf->buf[i] != keydata[match])
956 			break;
957 		match++;
958 		if (match == keylen) {
959 			memset(txf->buf, 0xAA, i + 1);
960 			memset(txf->buf + txf->buflen - matchend, 0xAA,
961 			       matchend);
962 			break;
963 		}
964 	}
965 }
966 
967 static void iwl_mvm_frob_txf(void *ctx, void *buf, size_t buflen)
968 {
969 	struct iwl_mvm_frob_txf_data txf = {
970 		.buf = buf,
971 		.buflen = buflen,
972 	};
973 	struct iwl_mvm *mvm = ctx;
974 
975 	/* embedded key material exists only on old API */
976 	if (iwl_mvm_has_new_tx_api(mvm))
977 		return;
978 
979 	rcu_read_lock();
980 	ieee80211_iter_keys_rcu(mvm->hw, NULL, iwl_mvm_frob_txf_key_iter, &txf);
981 	rcu_read_unlock();
982 }
983 
984 static void iwl_mvm_frob_hcmd(void *ctx, void *hcmd, size_t len)
985 {
986 	/* we only use wide headers for commands */
987 	struct iwl_cmd_header_wide *hdr = hcmd;
988 	unsigned int frob_start = sizeof(*hdr), frob_end = 0;
989 
990 	if (len < sizeof(hdr))
991 		return;
992 
993 	/* all the commands we care about are in LONG_GROUP */
994 	if (hdr->group_id != LONG_GROUP)
995 		return;
996 
997 	switch (hdr->cmd) {
998 	case WEP_KEY:
999 	case WOWLAN_TKIP_PARAM:
1000 	case WOWLAN_KEK_KCK_MATERIAL:
1001 	case ADD_STA_KEY:
1002 		/*
1003 		 * blank out everything here, easier than dealing
1004 		 * with the various versions of the command
1005 		 */
1006 		frob_end = INT_MAX;
1007 		break;
1008 	case MGMT_MCAST_KEY:
1009 		frob_start = offsetof(struct iwl_mvm_mgmt_mcast_key_cmd, igtk);
1010 		BUILD_BUG_ON(offsetof(struct iwl_mvm_mgmt_mcast_key_cmd, igtk) !=
1011 			     offsetof(struct iwl_mvm_mgmt_mcast_key_cmd_v1, igtk));
1012 
1013 		frob_end = offsetofend(struct iwl_mvm_mgmt_mcast_key_cmd, igtk);
1014 		BUILD_BUG_ON(offsetof(struct iwl_mvm_mgmt_mcast_key_cmd, igtk) <
1015 			     offsetof(struct iwl_mvm_mgmt_mcast_key_cmd_v1, igtk));
1016 		break;
1017 	}
1018 
1019 	if (frob_start >= frob_end)
1020 		return;
1021 
1022 	if (frob_end > len)
1023 		frob_end = len;
1024 
1025 	memset((u8 *)hcmd + frob_start, 0xAA, frob_end - frob_start);
1026 }
1027 
1028 static void iwl_mvm_frob_mem(void *ctx, u32 mem_addr, void *mem, size_t buflen)
1029 {
1030 	const struct iwl_dump_exclude *excl;
1031 	struct iwl_mvm *mvm = ctx;
1032 	int i;
1033 
1034 	switch (mvm->fwrt.cur_fw_img) {
1035 	case IWL_UCODE_INIT:
1036 	default:
1037 		/* not relevant */
1038 		return;
1039 	case IWL_UCODE_REGULAR:
1040 	case IWL_UCODE_REGULAR_USNIFFER:
1041 		excl = mvm->fw->dump_excl;
1042 		break;
1043 	case IWL_UCODE_WOWLAN:
1044 		excl = mvm->fw->dump_excl_wowlan;
1045 		break;
1046 	}
1047 
1048 	BUILD_BUG_ON(sizeof(mvm->fw->dump_excl) !=
1049 		     sizeof(mvm->fw->dump_excl_wowlan));
1050 
1051 	for (i = 0; i < ARRAY_SIZE(mvm->fw->dump_excl); i++) {
1052 		u32 start, end;
1053 
1054 		if (!excl[i].addr || !excl[i].size)
1055 			continue;
1056 
1057 		start = excl[i].addr;
1058 		end = start + excl[i].size;
1059 
1060 		if (end <= mem_addr || start >= mem_addr + buflen)
1061 			continue;
1062 
1063 		if (start < mem_addr)
1064 			start = mem_addr;
1065 
1066 		if (end > mem_addr + buflen)
1067 			end = mem_addr + buflen;
1068 
1069 		memset((u8 *)mem + start - mem_addr, 0xAA, end - start);
1070 	}
1071 }
1072 
1073 static const struct iwl_dump_sanitize_ops iwl_mvm_sanitize_ops = {
1074 	.frob_txf = iwl_mvm_frob_txf,
1075 	.frob_hcmd = iwl_mvm_frob_hcmd,
1076 	.frob_mem = iwl_mvm_frob_mem,
1077 };
1078 
1079 static void iwl_mvm_me_conn_status(void *priv, const struct iwl_mei_conn_info *conn_info)
1080 {
1081 	struct iwl_mvm *mvm = priv;
1082 	struct iwl_mvm_csme_conn_info *prev_conn_info, *curr_conn_info;
1083 
1084 	/*
1085 	 * This is protected by the guarantee that this function will not be
1086 	 * called twice on two different threads
1087 	 */
1088 	prev_conn_info = rcu_dereference_protected(mvm->csme_conn_info, true);
1089 
1090 	curr_conn_info = kzalloc(sizeof(*curr_conn_info), GFP_KERNEL);
1091 	if (!curr_conn_info)
1092 		return;
1093 
1094 	curr_conn_info->conn_info = *conn_info;
1095 
1096 	rcu_assign_pointer(mvm->csme_conn_info, curr_conn_info);
1097 
1098 	if (prev_conn_info)
1099 		kfree_rcu(prev_conn_info, rcu_head);
1100 }
1101 
1102 static void iwl_mvm_mei_rfkill(void *priv, bool blocked,
1103 			       bool csme_taking_ownership)
1104 {
1105 	struct iwl_mvm *mvm = priv;
1106 
1107 	if (blocked && !csme_taking_ownership)
1108 		return;
1109 
1110 	mvm->mei_rfkill_blocked = blocked;
1111 	if (!mvm->hw_registered)
1112 		return;
1113 
1114 	wiphy_rfkill_set_hw_state_reason(mvm->hw->wiphy,
1115 					 mvm->mei_rfkill_blocked,
1116 					 RFKILL_HARD_BLOCK_NOT_OWNER);
1117 }
1118 
1119 static void iwl_mvm_mei_roaming_forbidden(void *priv, bool forbidden)
1120 {
1121 	struct iwl_mvm *mvm = priv;
1122 
1123 	if (!mvm->hw_registered || !mvm->csme_vif)
1124 		return;
1125 
1126 	iwl_mvm_send_roaming_forbidden_event(mvm, mvm->csme_vif, forbidden);
1127 }
1128 
1129 static void iwl_mvm_sap_connected_wk(struct work_struct *wk)
1130 {
1131 	struct iwl_mvm *mvm =
1132 		container_of(wk, struct iwl_mvm, sap_connected_wk);
1133 	int ret;
1134 
1135 	ret = iwl_mvm_start_get_nvm(mvm);
1136 	if (ret)
1137 		goto out_free;
1138 
1139 	ret = iwl_mvm_start_post_nvm(mvm);
1140 	if (ret)
1141 		goto out_free;
1142 
1143 	return;
1144 
1145 out_free:
1146 	IWL_ERR(mvm, "Couldn't get started...\n");
1147 	iwl_mei_start_unregister();
1148 	iwl_mei_unregister_complete();
1149 	iwl_fw_flush_dumps(&mvm->fwrt);
1150 	iwl_mvm_thermal_exit(mvm);
1151 	iwl_fw_runtime_free(&mvm->fwrt);
1152 	iwl_phy_db_free(mvm->phy_db);
1153 	kfree(mvm->scan_cmd);
1154 	iwl_trans_op_mode_leave(mvm->trans);
1155 	kfree(mvm->nvm_data);
1156 	kfree(mvm->mei_nvm_data);
1157 
1158 	ieee80211_free_hw(mvm->hw);
1159 }
1160 
1161 static void iwl_mvm_mei_sap_connected(void *priv)
1162 {
1163 	struct iwl_mvm *mvm = priv;
1164 
1165 	if (!mvm->hw_registered)
1166 		schedule_work(&mvm->sap_connected_wk);
1167 }
1168 
1169 static void iwl_mvm_mei_nic_stolen(void *priv)
1170 {
1171 	struct iwl_mvm *mvm = priv;
1172 
1173 	rtnl_lock();
1174 	cfg80211_shutdown_all_interfaces(mvm->hw->wiphy);
1175 	rtnl_unlock();
1176 }
1177 
1178 static const struct iwl_mei_ops mei_ops = {
1179 	.me_conn_status = iwl_mvm_me_conn_status,
1180 	.rfkill = iwl_mvm_mei_rfkill,
1181 	.roaming_forbidden = iwl_mvm_mei_roaming_forbidden,
1182 	.sap_connected = iwl_mvm_mei_sap_connected,
1183 	.nic_stolen = iwl_mvm_mei_nic_stolen,
1184 };
1185 
1186 static void iwl_mvm_find_link_selection_vif(void *_data, u8 *mac,
1187 					    struct ieee80211_vif *vif)
1188 {
1189 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1190 
1191 	if (ieee80211_vif_is_mld(vif) && mvmvif->authorized)
1192 		iwl_mvm_select_links(mvmvif->mvm, vif);
1193 }
1194 
1195 static void iwl_mvm_trig_link_selection(struct wiphy *wiphy,
1196 					struct wiphy_work *wk)
1197 {
1198 	struct iwl_mvm *mvm =
1199 		container_of(wk, struct iwl_mvm, trig_link_selection_wk);
1200 
1201 	mutex_lock(&mvm->mutex);
1202 	ieee80211_iterate_active_interfaces(mvm->hw,
1203 					    IEEE80211_IFACE_ITER_NORMAL,
1204 					    iwl_mvm_find_link_selection_vif,
1205 					    NULL);
1206 	mutex_unlock(&mvm->mutex);
1207 }
1208 
1209 static struct iwl_op_mode *
1210 iwl_op_mode_mvm_start(struct iwl_trans *trans, const struct iwl_cfg *cfg,
1211 		      const struct iwl_fw *fw, struct dentry *dbgfs_dir)
1212 {
1213 	struct ieee80211_hw *hw;
1214 	struct iwl_op_mode *op_mode;
1215 	struct iwl_mvm *mvm;
1216 	struct iwl_trans_config trans_cfg = {};
1217 	static const u8 no_reclaim_cmds[] = {
1218 		TX_CMD,
1219 	};
1220 	u32 max_agg;
1221 	size_t scan_size;
1222 	u32 min_backoff;
1223 	struct iwl_mvm_csme_conn_info *csme_conn_info __maybe_unused;
1224 
1225 	/*
1226 	 * We use IWL_MVM_STATION_COUNT_MAX to check the validity of the station
1227 	 * index all over the driver - check that its value corresponds to the
1228 	 * array size.
1229 	 */
1230 	BUILD_BUG_ON(ARRAY_SIZE(mvm->fw_id_to_mac_id) !=
1231 		     IWL_MVM_STATION_COUNT_MAX);
1232 
1233 	/********************************
1234 	 * 1. Allocating and configuring HW data
1235 	 ********************************/
1236 	hw = ieee80211_alloc_hw(sizeof(struct iwl_op_mode) +
1237 				sizeof(struct iwl_mvm),
1238 				iwl_mvm_has_mld_api(fw) ? &iwl_mvm_mld_hw_ops :
1239 				&iwl_mvm_hw_ops);
1240 	if (!hw)
1241 		return NULL;
1242 
1243 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_BZ)
1244 		max_agg = 512;
1245 	else
1246 		max_agg = IEEE80211_MAX_AMPDU_BUF_HE;
1247 
1248 	hw->max_rx_aggregation_subframes = max_agg;
1249 
1250 	if (cfg->max_tx_agg_size)
1251 		hw->max_tx_aggregation_subframes = cfg->max_tx_agg_size;
1252 	else
1253 		hw->max_tx_aggregation_subframes = max_agg;
1254 
1255 	op_mode = hw->priv;
1256 
1257 	mvm = IWL_OP_MODE_GET_MVM(op_mode);
1258 	mvm->dev = trans->dev;
1259 	mvm->trans = trans;
1260 	mvm->cfg = cfg;
1261 	mvm->fw = fw;
1262 	mvm->hw = hw;
1263 
1264 	iwl_fw_runtime_init(&mvm->fwrt, trans, fw, &iwl_mvm_fwrt_ops, mvm,
1265 			    &iwl_mvm_sanitize_ops, mvm, dbgfs_dir);
1266 
1267 	iwl_mvm_get_bios_tables(mvm);
1268 	iwl_uefi_get_sgom_table(trans, &mvm->fwrt);
1269 	iwl_uefi_get_step_table(trans);
1270 
1271 	mvm->init_status = 0;
1272 
1273 	if (iwl_mvm_has_new_rx_api(mvm)) {
1274 		op_mode->ops = &iwl_mvm_ops_mq;
1275 		trans->rx_mpdu_cmd_hdr_size =
1276 			(trans->trans_cfg->device_family >=
1277 			 IWL_DEVICE_FAMILY_AX210) ?
1278 			sizeof(struct iwl_rx_mpdu_desc) :
1279 			IWL_RX_DESC_SIZE_V1;
1280 	} else {
1281 		op_mode->ops = &iwl_mvm_ops;
1282 		trans->rx_mpdu_cmd_hdr_size =
1283 			sizeof(struct iwl_rx_mpdu_res_start);
1284 
1285 		if (WARN_ON(trans->num_rx_queues > 1))
1286 			goto out_free;
1287 	}
1288 
1289 	mvm->fw_restart = iwlwifi_mod_params.fw_restart ? -1 : 0;
1290 
1291 	if (iwl_mvm_has_new_tx_api(mvm)) {
1292 		/*
1293 		 * If we have the new TX/queue allocation API initialize them
1294 		 * all to invalid numbers. We'll rewrite the ones that we need
1295 		 * later, but that doesn't happen for all of them all of the
1296 		 * time (e.g. P2P Device is optional), and if a dynamic queue
1297 		 * ends up getting number 2 (IWL_MVM_DQA_P2P_DEVICE_QUEUE) then
1298 		 * iwl_mvm_is_static_queue() erroneously returns true, and we
1299 		 * might have things getting stuck.
1300 		 */
1301 		mvm->aux_queue = IWL_MVM_INVALID_QUEUE;
1302 		mvm->snif_queue = IWL_MVM_INVALID_QUEUE;
1303 		mvm->probe_queue = IWL_MVM_INVALID_QUEUE;
1304 		mvm->p2p_dev_queue = IWL_MVM_INVALID_QUEUE;
1305 	} else {
1306 		mvm->aux_queue = IWL_MVM_DQA_AUX_QUEUE;
1307 		mvm->snif_queue = IWL_MVM_DQA_INJECT_MONITOR_QUEUE;
1308 		mvm->probe_queue = IWL_MVM_DQA_AP_PROBE_RESP_QUEUE;
1309 		mvm->p2p_dev_queue = IWL_MVM_DQA_P2P_DEVICE_QUEUE;
1310 	}
1311 
1312 	mvm->sf_state = SF_UNINIT;
1313 	if (iwl_mvm_has_unified_ucode(mvm))
1314 		iwl_fw_set_current_image(&mvm->fwrt, IWL_UCODE_REGULAR);
1315 	else
1316 		iwl_fw_set_current_image(&mvm->fwrt, IWL_UCODE_INIT);
1317 	mvm->drop_bcn_ap_mode = true;
1318 
1319 	mutex_init(&mvm->mutex);
1320 	spin_lock_init(&mvm->async_handlers_lock);
1321 	INIT_LIST_HEAD(&mvm->time_event_list);
1322 	INIT_LIST_HEAD(&mvm->aux_roc_te_list);
1323 	INIT_LIST_HEAD(&mvm->async_handlers_list);
1324 	spin_lock_init(&mvm->time_event_lock);
1325 	INIT_LIST_HEAD(&mvm->ftm_initiator.loc_list);
1326 	INIT_LIST_HEAD(&mvm->ftm_initiator.pasn_list);
1327 	INIT_LIST_HEAD(&mvm->resp_pasn_list);
1328 
1329 	INIT_WORK(&mvm->async_handlers_wk, iwl_mvm_async_handlers_wk);
1330 	INIT_WORK(&mvm->roc_done_wk, iwl_mvm_roc_done_wk);
1331 	INIT_WORK(&mvm->sap_connected_wk, iwl_mvm_sap_connected_wk);
1332 	INIT_DELAYED_WORK(&mvm->tdls_cs.dwork, iwl_mvm_tdls_ch_switch_work);
1333 	INIT_DELAYED_WORK(&mvm->scan_timeout_dwork, iwl_mvm_scan_timeout_wk);
1334 	INIT_WORK(&mvm->add_stream_wk, iwl_mvm_add_new_dqa_stream_wk);
1335 	INIT_LIST_HEAD(&mvm->add_stream_txqs);
1336 	spin_lock_init(&mvm->add_stream_lock);
1337 
1338 	wiphy_work_init(&mvm->async_handlers_wiphy_wk,
1339 			iwl_mvm_async_handlers_wiphy_wk);
1340 
1341 	wiphy_work_init(&mvm->trig_link_selection_wk,
1342 			iwl_mvm_trig_link_selection);
1343 
1344 	init_waitqueue_head(&mvm->rx_sync_waitq);
1345 
1346 	mvm->queue_sync_state = 0;
1347 
1348 	SET_IEEE80211_DEV(mvm->hw, mvm->trans->dev);
1349 
1350 	spin_lock_init(&mvm->tcm.lock);
1351 	INIT_DELAYED_WORK(&mvm->tcm.work, iwl_mvm_tcm_work);
1352 	mvm->tcm.ts = jiffies;
1353 	mvm->tcm.ll_ts = jiffies;
1354 	mvm->tcm.uapsd_nonagg_ts = jiffies;
1355 
1356 	INIT_DELAYED_WORK(&mvm->cs_tx_unblock_dwork, iwl_mvm_tx_unblock_dwork);
1357 
1358 	mvm->cmd_ver.range_resp =
1359 		iwl_fw_lookup_notif_ver(mvm->fw, LOCATION_GROUP,
1360 					TOF_RANGE_RESPONSE_NOTIF, 5);
1361 	/* we only support up to version 9 */
1362 	if (WARN_ON_ONCE(mvm->cmd_ver.range_resp > 9))
1363 		goto out_free;
1364 
1365 	/*
1366 	 * Populate the state variables that the transport layer needs
1367 	 * to know about.
1368 	 */
1369 	trans_cfg.op_mode = op_mode;
1370 	trans_cfg.no_reclaim_cmds = no_reclaim_cmds;
1371 	trans_cfg.n_no_reclaim_cmds = ARRAY_SIZE(no_reclaim_cmds);
1372 
1373 	trans_cfg.rx_buf_size = iwl_amsdu_size_to_rxb_size();
1374 
1375 	trans->wide_cmd_header = true;
1376 	trans_cfg.bc_table_dword =
1377 		mvm->trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_AX210;
1378 
1379 	trans_cfg.command_groups = iwl_mvm_groups;
1380 	trans_cfg.command_groups_size = ARRAY_SIZE(iwl_mvm_groups);
1381 
1382 	trans_cfg.cmd_queue = IWL_MVM_DQA_CMD_QUEUE;
1383 	trans_cfg.cmd_fifo = IWL_MVM_TX_FIFO_CMD;
1384 	trans_cfg.scd_set_active = true;
1385 
1386 	trans_cfg.cb_data_offs = offsetof(struct ieee80211_tx_info,
1387 					  driver_data[2]);
1388 
1389 	/* Set a short watchdog for the command queue */
1390 	trans_cfg.cmd_q_wdg_timeout =
1391 		iwl_mvm_get_wd_timeout(mvm, NULL, false, true);
1392 
1393 	snprintf(mvm->hw->wiphy->fw_version,
1394 		 sizeof(mvm->hw->wiphy->fw_version),
1395 		 "%.31s", fw->fw_version);
1396 
1397 	trans_cfg.fw_reset_handshake = fw_has_capa(&mvm->fw->ucode_capa,
1398 						   IWL_UCODE_TLV_CAPA_FW_RESET_HANDSHAKE);
1399 
1400 	trans_cfg.queue_alloc_cmd_ver =
1401 		iwl_fw_lookup_cmd_ver(mvm->fw,
1402 				      WIDE_ID(DATA_PATH_GROUP,
1403 					      SCD_QUEUE_CONFIG_CMD),
1404 				      0);
1405 	mvm->sta_remove_requires_queue_remove =
1406 		trans_cfg.queue_alloc_cmd_ver > 0;
1407 
1408 	mvm->mld_api_is_used = iwl_mvm_has_mld_api(mvm->fw);
1409 
1410 	/* Configure transport layer */
1411 	iwl_trans_configure(mvm->trans, &trans_cfg);
1412 
1413 	trans->rx_mpdu_cmd = REPLY_RX_MPDU_CMD;
1414 	trans->dbg.dest_tlv = mvm->fw->dbg.dest_tlv;
1415 	trans->dbg.n_dest_reg = mvm->fw->dbg.n_dest_reg;
1416 	memcpy(trans->dbg.conf_tlv, mvm->fw->dbg.conf_tlv,
1417 	       sizeof(trans->dbg.conf_tlv));
1418 	trans->dbg.trigger_tlv = mvm->fw->dbg.trigger_tlv;
1419 
1420 	trans->iml = mvm->fw->iml;
1421 	trans->iml_len = mvm->fw->iml_len;
1422 
1423 	/* set up notification wait support */
1424 	iwl_notification_wait_init(&mvm->notif_wait);
1425 
1426 	/* Init phy db */
1427 	mvm->phy_db = iwl_phy_db_init(trans);
1428 	if (!mvm->phy_db) {
1429 		IWL_ERR(mvm, "Cannot init phy_db\n");
1430 		goto out_free;
1431 	}
1432 
1433 	if (iwlwifi_mod_params.nvm_file)
1434 		mvm->nvm_file_name = iwlwifi_mod_params.nvm_file;
1435 	else
1436 		IWL_DEBUG_EEPROM(mvm->trans->dev,
1437 				 "working without external nvm file\n");
1438 
1439 	scan_size = iwl_mvm_scan_size(mvm);
1440 
1441 	mvm->scan_cmd = kmalloc(scan_size, GFP_KERNEL);
1442 	if (!mvm->scan_cmd)
1443 		goto out_free;
1444 	mvm->scan_cmd_size = scan_size;
1445 
1446 	/* invalidate ids to prevent accidental removal of sta_id 0 */
1447 	mvm->aux_sta.sta_id = IWL_MVM_INVALID_STA;
1448 	mvm->snif_sta.sta_id = IWL_MVM_INVALID_STA;
1449 
1450 	/* Set EBS as successful as long as not stated otherwise by the FW. */
1451 	mvm->last_ebs_successful = true;
1452 
1453 	min_backoff = iwl_mvm_min_backoff(mvm);
1454 	iwl_mvm_thermal_initialize(mvm, min_backoff);
1455 
1456 	if (!iwl_mvm_has_new_rx_stats_api(mvm))
1457 		memset(&mvm->rx_stats_v3, 0,
1458 		       sizeof(struct mvm_statistics_rx_v3));
1459 	else
1460 		memset(&mvm->rx_stats, 0, sizeof(struct mvm_statistics_rx));
1461 
1462 	iwl_mvm_ftm_initiator_smooth_config(mvm);
1463 
1464 	iwl_mvm_init_time_sync(&mvm->time_sync);
1465 
1466 	mvm->debugfs_dir = dbgfs_dir;
1467 
1468 	mvm->mei_registered = !iwl_mei_register(mvm, &mei_ops);
1469 
1470 	iwl_mvm_mei_scan_filter_init(&mvm->mei_scan_filter);
1471 
1472 	if (iwl_mvm_start_get_nvm(mvm)) {
1473 		/*
1474 		 * Getting NVM failed while CSME is the owner, but we are
1475 		 * registered to MEI, we'll get the NVM later when it'll be
1476 		 * possible to get it from CSME.
1477 		 */
1478 		if (trans->csme_own && mvm->mei_registered)
1479 			return op_mode;
1480 
1481 		goto out_thermal_exit;
1482 	}
1483 
1484 
1485 	if (iwl_mvm_start_post_nvm(mvm))
1486 		goto out_thermal_exit;
1487 
1488 	return op_mode;
1489 
1490  out_thermal_exit:
1491 	iwl_mvm_thermal_exit(mvm);
1492 	if (mvm->mei_registered) {
1493 		iwl_mei_start_unregister();
1494 		iwl_mei_unregister_complete();
1495 	}
1496  out_free:
1497 	iwl_fw_flush_dumps(&mvm->fwrt);
1498 	iwl_fw_runtime_free(&mvm->fwrt);
1499 
1500 	iwl_phy_db_free(mvm->phy_db);
1501 	kfree(mvm->scan_cmd);
1502 	iwl_trans_op_mode_leave(trans);
1503 
1504 	ieee80211_free_hw(mvm->hw);
1505 	return NULL;
1506 }
1507 
1508 void iwl_mvm_stop_device(struct iwl_mvm *mvm)
1509 {
1510 	lockdep_assert_held(&mvm->mutex);
1511 
1512 	iwl_fw_cancel_timestamp(&mvm->fwrt);
1513 
1514 	clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
1515 
1516 	iwl_mvm_pause_tcm(mvm, false);
1517 
1518 	iwl_fw_dbg_stop_sync(&mvm->fwrt);
1519 	iwl_trans_stop_device(mvm->trans);
1520 	iwl_free_fw_paging(&mvm->fwrt);
1521 	iwl_fw_dump_conf_clear(&mvm->fwrt);
1522 	iwl_mvm_mei_device_state(mvm, false);
1523 }
1524 
1525 static void iwl_op_mode_mvm_stop(struct iwl_op_mode *op_mode)
1526 {
1527 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1528 	int i;
1529 
1530 	if (mvm->mei_registered) {
1531 		rtnl_lock();
1532 		iwl_mei_set_netdev(NULL);
1533 		rtnl_unlock();
1534 		iwl_mei_start_unregister();
1535 	}
1536 
1537 	/*
1538 	 * After we unregister from mei, the worker can't be scheduled
1539 	 * anymore.
1540 	 */
1541 	cancel_work_sync(&mvm->sap_connected_wk);
1542 
1543 	iwl_mvm_leds_exit(mvm);
1544 
1545 	iwl_mvm_thermal_exit(mvm);
1546 
1547 	/*
1548 	 * If we couldn't get ownership on the device and we couldn't
1549 	 * get the NVM from CSME, we haven't registered to mac80211.
1550 	 * In that case, we didn't fail op_mode_start, because we are
1551 	 * waiting for CSME to allow us to get the NVM to register to
1552 	 * mac80211. If that didn't happen, we haven't registered to
1553 	 * mac80211, hence the if below.
1554 	 */
1555 	if (mvm->hw_registered)
1556 		ieee80211_unregister_hw(mvm->hw);
1557 
1558 	kfree(mvm->scan_cmd);
1559 	kfree(mvm->mcast_filter_cmd);
1560 	mvm->mcast_filter_cmd = NULL;
1561 
1562 	kfree(mvm->error_recovery_buf);
1563 	mvm->error_recovery_buf = NULL;
1564 
1565 	iwl_mvm_ptp_remove(mvm);
1566 
1567 	iwl_trans_op_mode_leave(mvm->trans);
1568 
1569 	iwl_phy_db_free(mvm->phy_db);
1570 	mvm->phy_db = NULL;
1571 
1572 	kfree(mvm->nvm_data);
1573 	kfree(mvm->mei_nvm_data);
1574 	kfree(rcu_access_pointer(mvm->csme_conn_info));
1575 	kfree(mvm->temp_nvm_data);
1576 	for (i = 0; i < NVM_MAX_NUM_SECTIONS; i++)
1577 		kfree(mvm->nvm_sections[i].data);
1578 	kfree(mvm->acs_survey);
1579 
1580 	cancel_delayed_work_sync(&mvm->tcm.work);
1581 
1582 	iwl_fw_runtime_free(&mvm->fwrt);
1583 	mutex_destroy(&mvm->mutex);
1584 
1585 	if (mvm->mei_registered)
1586 		iwl_mei_unregister_complete();
1587 
1588 	ieee80211_free_hw(mvm->hw);
1589 }
1590 
1591 struct iwl_async_handler_entry {
1592 	struct list_head list;
1593 	struct iwl_rx_cmd_buffer rxb;
1594 	enum iwl_rx_handler_context context;
1595 	void (*fn)(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb);
1596 };
1597 
1598 void iwl_mvm_async_handlers_purge(struct iwl_mvm *mvm)
1599 {
1600 	struct iwl_async_handler_entry *entry, *tmp;
1601 
1602 	spin_lock_bh(&mvm->async_handlers_lock);
1603 	list_for_each_entry_safe(entry, tmp, &mvm->async_handlers_list, list) {
1604 		iwl_free_rxb(&entry->rxb);
1605 		list_del(&entry->list);
1606 		kfree(entry);
1607 	}
1608 	spin_unlock_bh(&mvm->async_handlers_lock);
1609 }
1610 
1611 /*
1612  * This function receives a bitmap of rx async handler contexts
1613  * (&iwl_rx_handler_context) to handle, and runs only them
1614  */
1615 static void iwl_mvm_async_handlers_by_context(struct iwl_mvm *mvm,
1616 					      u8 contexts)
1617 {
1618 	struct iwl_async_handler_entry *entry, *tmp;
1619 	LIST_HEAD(local_list);
1620 
1621 	/*
1622 	 * Sync with Rx path with a lock. Remove all the entries of the
1623 	 * wanted contexts from this list, add them to a local one (lock free),
1624 	 * and then handle them.
1625 	 */
1626 	spin_lock_bh(&mvm->async_handlers_lock);
1627 	list_for_each_entry_safe(entry, tmp, &mvm->async_handlers_list, list) {
1628 		if (!(BIT(entry->context) & contexts))
1629 			continue;
1630 		list_del(&entry->list);
1631 		list_add_tail(&entry->list, &local_list);
1632 	}
1633 	spin_unlock_bh(&mvm->async_handlers_lock);
1634 
1635 	list_for_each_entry_safe(entry, tmp, &local_list, list) {
1636 		if (entry->context != RX_HANDLER_ASYNC_UNLOCKED)
1637 			mutex_lock(&mvm->mutex);
1638 		entry->fn(mvm, &entry->rxb);
1639 		iwl_free_rxb(&entry->rxb);
1640 		list_del(&entry->list);
1641 		if (entry->context != RX_HANDLER_ASYNC_UNLOCKED)
1642 			mutex_unlock(&mvm->mutex);
1643 		kfree(entry);
1644 	}
1645 }
1646 
1647 static void iwl_mvm_async_handlers_wiphy_wk(struct wiphy *wiphy,
1648 					    struct wiphy_work *wk)
1649 {
1650 	struct iwl_mvm *mvm =
1651 		container_of(wk, struct iwl_mvm, async_handlers_wiphy_wk);
1652 	u8 contexts = BIT(RX_HANDLER_ASYNC_LOCKED_WIPHY);
1653 
1654 	iwl_mvm_async_handlers_by_context(mvm, contexts);
1655 }
1656 
1657 static void iwl_mvm_async_handlers_wk(struct work_struct *wk)
1658 {
1659 	struct iwl_mvm *mvm =
1660 		container_of(wk, struct iwl_mvm, async_handlers_wk);
1661 	u8 contexts = BIT(RX_HANDLER_ASYNC_LOCKED) |
1662 		      BIT(RX_HANDLER_ASYNC_UNLOCKED);
1663 
1664 	iwl_mvm_async_handlers_by_context(mvm, contexts);
1665 }
1666 
1667 static inline void iwl_mvm_rx_check_trigger(struct iwl_mvm *mvm,
1668 					    struct iwl_rx_packet *pkt)
1669 {
1670 	struct iwl_fw_dbg_trigger_tlv *trig;
1671 	struct iwl_fw_dbg_trigger_cmd *cmds_trig;
1672 	int i;
1673 
1674 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, NULL,
1675 				     FW_DBG_TRIGGER_FW_NOTIF);
1676 	if (!trig)
1677 		return;
1678 
1679 	cmds_trig = (void *)trig->data;
1680 
1681 	for (i = 0; i < ARRAY_SIZE(cmds_trig->cmds); i++) {
1682 		/* don't collect on CMD 0 */
1683 		if (!cmds_trig->cmds[i].cmd_id)
1684 			break;
1685 
1686 		if (cmds_trig->cmds[i].cmd_id != pkt->hdr.cmd ||
1687 		    cmds_trig->cmds[i].group_id != pkt->hdr.group_id)
1688 			continue;
1689 
1690 		iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
1691 					"CMD 0x%02x.%02x received",
1692 					pkt->hdr.group_id, pkt->hdr.cmd);
1693 		break;
1694 	}
1695 }
1696 
1697 static void iwl_mvm_rx_common(struct iwl_mvm *mvm,
1698 			      struct iwl_rx_cmd_buffer *rxb,
1699 			      struct iwl_rx_packet *pkt)
1700 {
1701 	unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
1702 	int i;
1703 	union iwl_dbg_tlv_tp_data tp_data = { .fw_pkt = pkt };
1704 
1705 	iwl_dbg_tlv_time_point(&mvm->fwrt,
1706 			       IWL_FW_INI_TIME_POINT_FW_RSP_OR_NOTIF, &tp_data);
1707 	iwl_mvm_rx_check_trigger(mvm, pkt);
1708 
1709 	/*
1710 	 * Do the notification wait before RX handlers so
1711 	 * even if the RX handler consumes the RXB we have
1712 	 * access to it in the notification wait entry.
1713 	 */
1714 	iwl_notification_wait_notify(&mvm->notif_wait, pkt);
1715 
1716 	for (i = 0; i < ARRAY_SIZE(iwl_mvm_rx_handlers); i++) {
1717 		const struct iwl_rx_handlers *rx_h = &iwl_mvm_rx_handlers[i];
1718 		struct iwl_async_handler_entry *entry;
1719 
1720 		if (rx_h->cmd_id != WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd))
1721 			continue;
1722 
1723 		if (IWL_FW_CHECK(mvm, pkt_len < rx_h->min_size,
1724 				 "unexpected notification 0x%04x size %d, need %d\n",
1725 				 rx_h->cmd_id, pkt_len, rx_h->min_size))
1726 			return;
1727 
1728 		if (rx_h->context == RX_HANDLER_SYNC) {
1729 			rx_h->fn(mvm, rxb);
1730 			return;
1731 		}
1732 
1733 		entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
1734 		/* we can't do much... */
1735 		if (!entry)
1736 			return;
1737 
1738 		entry->rxb._page = rxb_steal_page(rxb);
1739 		entry->rxb._offset = rxb->_offset;
1740 		entry->rxb._rx_page_order = rxb->_rx_page_order;
1741 		entry->fn = rx_h->fn;
1742 		entry->context = rx_h->context;
1743 		spin_lock(&mvm->async_handlers_lock);
1744 		list_add_tail(&entry->list, &mvm->async_handlers_list);
1745 		spin_unlock(&mvm->async_handlers_lock);
1746 		if (rx_h->context == RX_HANDLER_ASYNC_LOCKED_WIPHY)
1747 			wiphy_work_queue(mvm->hw->wiphy,
1748 					 &mvm->async_handlers_wiphy_wk);
1749 		else
1750 			schedule_work(&mvm->async_handlers_wk);
1751 		break;
1752 	}
1753 }
1754 
1755 static void iwl_mvm_rx(struct iwl_op_mode *op_mode,
1756 		       struct napi_struct *napi,
1757 		       struct iwl_rx_cmd_buffer *rxb)
1758 {
1759 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1760 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1761 	u16 cmd = WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd);
1762 
1763 	if (likely(cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_MPDU_CMD)))
1764 		iwl_mvm_rx_rx_mpdu(mvm, napi, rxb);
1765 	else if (cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_PHY_CMD))
1766 		iwl_mvm_rx_rx_phy_cmd(mvm, rxb);
1767 	else
1768 		iwl_mvm_rx_common(mvm, rxb, pkt);
1769 }
1770 
1771 void iwl_mvm_rx_mq(struct iwl_op_mode *op_mode,
1772 		   struct napi_struct *napi,
1773 		   struct iwl_rx_cmd_buffer *rxb)
1774 {
1775 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1776 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1777 	u16 cmd = WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd);
1778 
1779 	if (likely(cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_MPDU_CMD)))
1780 		iwl_mvm_rx_mpdu_mq(mvm, napi, rxb, 0);
1781 	else if (unlikely(cmd == WIDE_ID(DATA_PATH_GROUP,
1782 					 RX_QUEUES_NOTIFICATION)))
1783 		iwl_mvm_rx_queue_notif(mvm, napi, rxb, 0);
1784 	else if (cmd == WIDE_ID(LEGACY_GROUP, FRAME_RELEASE))
1785 		iwl_mvm_rx_frame_release(mvm, napi, rxb, 0);
1786 	else if (cmd == WIDE_ID(LEGACY_GROUP, BAR_FRAME_RELEASE))
1787 		iwl_mvm_rx_bar_frame_release(mvm, napi, rxb, 0);
1788 	else if (cmd == WIDE_ID(DATA_PATH_GROUP, RX_NO_DATA_NOTIF))
1789 		iwl_mvm_rx_monitor_no_data(mvm, napi, rxb, 0);
1790 	else
1791 		iwl_mvm_rx_common(mvm, rxb, pkt);
1792 }
1793 
1794 static int iwl_mvm_is_static_queue(struct iwl_mvm *mvm, int queue)
1795 {
1796 	return queue == mvm->aux_queue || queue == mvm->probe_queue ||
1797 		queue == mvm->p2p_dev_queue || queue == mvm->snif_queue;
1798 }
1799 
1800 static void iwl_mvm_queue_state_change(struct iwl_op_mode *op_mode,
1801 				       int hw_queue, bool start)
1802 {
1803 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1804 	struct ieee80211_sta *sta;
1805 	struct ieee80211_txq *txq;
1806 	struct iwl_mvm_txq *mvmtxq;
1807 	int i;
1808 	unsigned long tid_bitmap;
1809 	struct iwl_mvm_sta *mvmsta;
1810 	u8 sta_id;
1811 
1812 	sta_id = iwl_mvm_has_new_tx_api(mvm) ?
1813 		mvm->tvqm_info[hw_queue].sta_id :
1814 		mvm->queue_info[hw_queue].ra_sta_id;
1815 
1816 	if (WARN_ON_ONCE(sta_id >= mvm->fw->ucode_capa.num_stations))
1817 		return;
1818 
1819 	rcu_read_lock();
1820 
1821 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1822 	if (IS_ERR_OR_NULL(sta))
1823 		goto out;
1824 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
1825 
1826 	if (iwl_mvm_is_static_queue(mvm, hw_queue)) {
1827 		if (!start)
1828 			ieee80211_stop_queues(mvm->hw);
1829 		else if (mvmsta->sta_state != IEEE80211_STA_NOTEXIST)
1830 			ieee80211_wake_queues(mvm->hw);
1831 
1832 		goto out;
1833 	}
1834 
1835 	if (iwl_mvm_has_new_tx_api(mvm)) {
1836 		int tid = mvm->tvqm_info[hw_queue].txq_tid;
1837 
1838 		tid_bitmap = BIT(tid);
1839 	} else {
1840 		tid_bitmap = mvm->queue_info[hw_queue].tid_bitmap;
1841 	}
1842 
1843 	for_each_set_bit(i, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1844 		int tid = i;
1845 
1846 		if (tid == IWL_MAX_TID_COUNT)
1847 			tid = IEEE80211_NUM_TIDS;
1848 
1849 		txq = sta->txq[tid];
1850 		mvmtxq = iwl_mvm_txq_from_mac80211(txq);
1851 		if (start)
1852 			clear_bit(IWL_MVM_TXQ_STATE_STOP_FULL, &mvmtxq->state);
1853 		else
1854 			set_bit(IWL_MVM_TXQ_STATE_STOP_FULL, &mvmtxq->state);
1855 
1856 		if (start && mvmsta->sta_state != IEEE80211_STA_NOTEXIST) {
1857 			local_bh_disable();
1858 			iwl_mvm_mac_itxq_xmit(mvm->hw, txq);
1859 			local_bh_enable();
1860 		}
1861 	}
1862 
1863 out:
1864 	rcu_read_unlock();
1865 }
1866 
1867 static void iwl_mvm_stop_sw_queue(struct iwl_op_mode *op_mode, int hw_queue)
1868 {
1869 	iwl_mvm_queue_state_change(op_mode, hw_queue, false);
1870 }
1871 
1872 static void iwl_mvm_wake_sw_queue(struct iwl_op_mode *op_mode, int hw_queue)
1873 {
1874 	iwl_mvm_queue_state_change(op_mode, hw_queue, true);
1875 }
1876 
1877 static void iwl_mvm_set_rfkill_state(struct iwl_mvm *mvm)
1878 {
1879 	wiphy_rfkill_set_hw_state(mvm->hw->wiphy,
1880 				  iwl_mvm_is_radio_killed(mvm));
1881 }
1882 
1883 void iwl_mvm_set_hw_ctkill_state(struct iwl_mvm *mvm, bool state)
1884 {
1885 	if (state)
1886 		set_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status);
1887 	else
1888 		clear_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status);
1889 
1890 	iwl_mvm_set_rfkill_state(mvm);
1891 }
1892 
1893 struct iwl_mvm_csme_conn_info *iwl_mvm_get_csme_conn_info(struct iwl_mvm *mvm)
1894 {
1895 	return rcu_dereference_protected(mvm->csme_conn_info,
1896 					 lockdep_is_held(&mvm->mutex));
1897 }
1898 
1899 static bool iwl_mvm_set_hw_rfkill_state(struct iwl_op_mode *op_mode, bool state)
1900 {
1901 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1902 	bool rfkill_safe_init_done = READ_ONCE(mvm->rfkill_safe_init_done);
1903 	bool unified = iwl_mvm_has_unified_ucode(mvm);
1904 
1905 	if (state)
1906 		set_bit(IWL_MVM_STATUS_HW_RFKILL, &mvm->status);
1907 	else
1908 		clear_bit(IWL_MVM_STATUS_HW_RFKILL, &mvm->status);
1909 
1910 	iwl_mvm_set_rfkill_state(mvm);
1911 
1912 	 /* iwl_run_init_mvm_ucode is waiting for results, abort it. */
1913 	if (rfkill_safe_init_done)
1914 		iwl_abort_notification_waits(&mvm->notif_wait);
1915 
1916 	/*
1917 	 * Don't ask the transport to stop the firmware. We'll do it
1918 	 * after cfg80211 takes us down.
1919 	 */
1920 	if (unified)
1921 		return false;
1922 
1923 	/*
1924 	 * Stop the device if we run OPERATIONAL firmware or if we are in the
1925 	 * middle of the calibrations.
1926 	 */
1927 	return state && rfkill_safe_init_done;
1928 }
1929 
1930 static void iwl_mvm_free_skb(struct iwl_op_mode *op_mode, struct sk_buff *skb)
1931 {
1932 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1933 	struct ieee80211_tx_info *info;
1934 
1935 	info = IEEE80211_SKB_CB(skb);
1936 	iwl_trans_free_tx_cmd(mvm->trans, info->driver_data[1]);
1937 	ieee80211_free_txskb(mvm->hw, skb);
1938 }
1939 
1940 struct iwl_mvm_reprobe {
1941 	struct device *dev;
1942 	struct work_struct work;
1943 };
1944 
1945 static void iwl_mvm_reprobe_wk(struct work_struct *wk)
1946 {
1947 	struct iwl_mvm_reprobe *reprobe;
1948 
1949 	reprobe = container_of(wk, struct iwl_mvm_reprobe, work);
1950 	if (device_reprobe(reprobe->dev))
1951 		dev_err(reprobe->dev, "reprobe failed!\n");
1952 	put_device(reprobe->dev);
1953 	kfree(reprobe);
1954 	module_put(THIS_MODULE);
1955 }
1956 
1957 void iwl_mvm_nic_restart(struct iwl_mvm *mvm, bool fw_error)
1958 {
1959 	iwl_abort_notification_waits(&mvm->notif_wait);
1960 	iwl_dbg_tlv_del_timers(mvm->trans);
1961 
1962 	/*
1963 	 * This is a bit racy, but worst case we tell mac80211 about
1964 	 * a stopped/aborted scan when that was already done which
1965 	 * is not a problem. It is necessary to abort any os scan
1966 	 * here because mac80211 requires having the scan cleared
1967 	 * before restarting.
1968 	 * We'll reset the scan_status to NONE in restart cleanup in
1969 	 * the next start() call from mac80211. If restart isn't called
1970 	 * (no fw restart) scan status will stay busy.
1971 	 */
1972 	iwl_mvm_report_scan_aborted(mvm);
1973 
1974 	/*
1975 	 * If we're restarting already, don't cycle restarts.
1976 	 * If INIT fw asserted, it will likely fail again.
1977 	 * If WoWLAN fw asserted, don't restart either, mac80211
1978 	 * can't recover this since we're already half suspended.
1979 	 */
1980 	if (!mvm->fw_restart && fw_error) {
1981 		iwl_fw_error_collect(&mvm->fwrt, false);
1982 	} else if (test_bit(IWL_MVM_STATUS_STARTING,
1983 			    &mvm->status)) {
1984 		IWL_ERR(mvm, "Starting mac, retry will be triggered anyway\n");
1985 	} else if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1986 		struct iwl_mvm_reprobe *reprobe;
1987 
1988 		IWL_ERR(mvm,
1989 			"Firmware error during reconfiguration - reprobe!\n");
1990 
1991 		/*
1992 		 * get a module reference to avoid doing this while unloading
1993 		 * anyway and to avoid scheduling a work with code that's
1994 		 * being removed.
1995 		 */
1996 		if (!try_module_get(THIS_MODULE)) {
1997 			IWL_ERR(mvm, "Module is being unloaded - abort\n");
1998 			return;
1999 		}
2000 
2001 		reprobe = kzalloc(sizeof(*reprobe), GFP_ATOMIC);
2002 		if (!reprobe) {
2003 			module_put(THIS_MODULE);
2004 			return;
2005 		}
2006 		reprobe->dev = get_device(mvm->trans->dev);
2007 		INIT_WORK(&reprobe->work, iwl_mvm_reprobe_wk);
2008 		schedule_work(&reprobe->work);
2009 	} else if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
2010 			    &mvm->status)) {
2011 		IWL_ERR(mvm, "HW restart already requested, but not started\n");
2012 	} else if (mvm->fwrt.cur_fw_img == IWL_UCODE_REGULAR &&
2013 		   mvm->hw_registered &&
2014 		   !test_bit(STATUS_TRANS_DEAD, &mvm->trans->status)) {
2015 		/* This should be first thing before trying to collect any
2016 		 * data to avoid endless loops if any HW error happens while
2017 		 * collecting debug data.
2018 		 */
2019 		set_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
2020 
2021 		if (mvm->fw->ucode_capa.error_log_size) {
2022 			u32 src_size = mvm->fw->ucode_capa.error_log_size;
2023 			u32 src_addr = mvm->fw->ucode_capa.error_log_addr;
2024 			u8 *recover_buf = kzalloc(src_size, GFP_ATOMIC);
2025 
2026 			if (recover_buf) {
2027 				mvm->error_recovery_buf = recover_buf;
2028 				iwl_trans_read_mem_bytes(mvm->trans,
2029 							 src_addr,
2030 							 recover_buf,
2031 							 src_size);
2032 			}
2033 		}
2034 
2035 		iwl_fw_error_collect(&mvm->fwrt, false);
2036 
2037 		if (fw_error && mvm->fw_restart > 0) {
2038 			mvm->fw_restart--;
2039 			ieee80211_restart_hw(mvm->hw);
2040 		} else if (mvm->fwrt.trans->dbg.restart_required) {
2041 			IWL_DEBUG_INFO(mvm, "FW restart requested after debug collection\n");
2042 			mvm->fwrt.trans->dbg.restart_required = false;
2043 			ieee80211_restart_hw(mvm->hw);
2044 		} else if (mvm->trans->trans_cfg->device_family <= IWL_DEVICE_FAMILY_8000) {
2045 			ieee80211_restart_hw(mvm->hw);
2046 		}
2047 	}
2048 }
2049 
2050 static void iwl_mvm_nic_error(struct iwl_op_mode *op_mode, bool sync)
2051 {
2052 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
2053 
2054 	if (!test_bit(STATUS_TRANS_DEAD, &mvm->trans->status) &&
2055 	    !test_and_clear_bit(IWL_MVM_STATUS_SUPPRESS_ERROR_LOG_ONCE,
2056 				&mvm->status))
2057 		iwl_mvm_dump_nic_error_log(mvm);
2058 
2059 	if (sync) {
2060 		iwl_fw_error_collect(&mvm->fwrt, true);
2061 		/*
2062 		 * Currently, the only case for sync=true is during
2063 		 * shutdown, so just stop in this case. If/when that
2064 		 * changes, we need to be a bit smarter here.
2065 		 */
2066 		return;
2067 	}
2068 
2069 	/*
2070 	 * If the firmware crashes while we're already considering it
2071 	 * to be dead then don't ask for a restart, that cannot do
2072 	 * anything useful anyway.
2073 	 */
2074 	if (!test_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status))
2075 		return;
2076 
2077 	iwl_mvm_nic_restart(mvm, false);
2078 }
2079 
2080 static void iwl_mvm_cmd_queue_full(struct iwl_op_mode *op_mode)
2081 {
2082 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
2083 
2084 	WARN_ON(1);
2085 	iwl_mvm_nic_restart(mvm, true);
2086 }
2087 
2088 static void iwl_op_mode_mvm_time_point(struct iwl_op_mode *op_mode,
2089 				       enum iwl_fw_ini_time_point tp_id,
2090 				       union iwl_dbg_tlv_tp_data *tp_data)
2091 {
2092 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
2093 
2094 	iwl_dbg_tlv_time_point(&mvm->fwrt, tp_id, tp_data);
2095 }
2096 
2097 static void iwl_op_mode_mvm_device_powered_off(struct iwl_op_mode *op_mode)
2098 {
2099 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
2100 
2101 	mutex_lock(&mvm->mutex);
2102 	clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
2103 	mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
2104 	iwl_mvm_stop_device(mvm);
2105 #ifdef CONFIG_PM
2106 	mvm->fast_resume = false;
2107 #endif
2108 	mutex_unlock(&mvm->mutex);
2109 }
2110 
2111 #define IWL_MVM_COMMON_OPS					\
2112 	/* these could be differentiated */			\
2113 	.queue_full = iwl_mvm_stop_sw_queue,			\
2114 	.queue_not_full = iwl_mvm_wake_sw_queue,		\
2115 	.hw_rf_kill = iwl_mvm_set_hw_rfkill_state,		\
2116 	.free_skb = iwl_mvm_free_skb,				\
2117 	.nic_error = iwl_mvm_nic_error,				\
2118 	.cmd_queue_full = iwl_mvm_cmd_queue_full,		\
2119 	.nic_config = iwl_mvm_nic_config,			\
2120 	/* as we only register one, these MUST be common! */	\
2121 	.start = iwl_op_mode_mvm_start,				\
2122 	.stop = iwl_op_mode_mvm_stop,				\
2123 	.time_point = iwl_op_mode_mvm_time_point,		\
2124 	.device_powered_off = iwl_op_mode_mvm_device_powered_off
2125 
2126 static const struct iwl_op_mode_ops iwl_mvm_ops = {
2127 	IWL_MVM_COMMON_OPS,
2128 	.rx = iwl_mvm_rx,
2129 };
2130 
2131 static void iwl_mvm_rx_mq_rss(struct iwl_op_mode *op_mode,
2132 			      struct napi_struct *napi,
2133 			      struct iwl_rx_cmd_buffer *rxb,
2134 			      unsigned int queue)
2135 {
2136 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
2137 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2138 	u16 cmd = WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd);
2139 
2140 	if (unlikely(queue >= mvm->trans->num_rx_queues))
2141 		return;
2142 
2143 	if (unlikely(cmd == WIDE_ID(LEGACY_GROUP, FRAME_RELEASE)))
2144 		iwl_mvm_rx_frame_release(mvm, napi, rxb, queue);
2145 	else if (unlikely(cmd == WIDE_ID(DATA_PATH_GROUP,
2146 					 RX_QUEUES_NOTIFICATION)))
2147 		iwl_mvm_rx_queue_notif(mvm, napi, rxb, queue);
2148 	else if (likely(cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_MPDU_CMD)))
2149 		iwl_mvm_rx_mpdu_mq(mvm, napi, rxb, queue);
2150 }
2151 
2152 static const struct iwl_op_mode_ops iwl_mvm_ops_mq = {
2153 	IWL_MVM_COMMON_OPS,
2154 	.rx = iwl_mvm_rx_mq,
2155 	.rx_rss = iwl_mvm_rx_mq_rss,
2156 };
2157