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