xref: /freebsd/sys/contrib/dev/iwlwifi/mvm/ops.c (revision 62ff619dcc3540659a319be71c9a489f1659e14a)
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 static void iwl_mvm_me_conn_status(void *priv, const struct iwl_mei_conn_info *conn_info)
973 {
974 	struct iwl_mvm *mvm = priv;
975 	struct iwl_mvm_csme_conn_info *prev_conn_info, *curr_conn_info;
976 
977 	/*
978 	 * This is protected by the guarantee that this function will not be
979 	 * called twice on two different threads
980 	 */
981 	prev_conn_info = rcu_dereference_protected(mvm->csme_conn_info, true);
982 
983 	curr_conn_info = kzalloc(sizeof(*curr_conn_info), GFP_KERNEL);
984 	if (!curr_conn_info)
985 		return;
986 
987 	curr_conn_info->conn_info = *conn_info;
988 
989 	rcu_assign_pointer(mvm->csme_conn_info, curr_conn_info);
990 
991 	if (prev_conn_info)
992 		kfree_rcu(prev_conn_info, rcu_head);
993 }
994 
995 static void iwl_mvm_mei_rfkill(void *priv, bool blocked)
996 {
997 	struct iwl_mvm *mvm = priv;
998 
999 	mvm->mei_rfkill_blocked = blocked;
1000 	if (!mvm->hw_registered)
1001 		return;
1002 
1003 	wiphy_rfkill_set_hw_state_reason(mvm->hw->wiphy,
1004 					 mvm->mei_rfkill_blocked,
1005 					 RFKILL_HARD_BLOCK_NOT_OWNER);
1006 }
1007 
1008 static void iwl_mvm_mei_roaming_forbidden(void *priv, bool forbidden)
1009 {
1010 	struct iwl_mvm *mvm = priv;
1011 
1012 	if (!mvm->hw_registered || !mvm->csme_vif)
1013 		return;
1014 
1015 	iwl_mvm_send_roaming_forbidden_event(mvm, mvm->csme_vif, forbidden);
1016 }
1017 
1018 static void iwl_mvm_sap_connected_wk(struct work_struct *wk)
1019 {
1020 	struct iwl_mvm *mvm =
1021 		container_of(wk, struct iwl_mvm, sap_connected_wk);
1022 	int ret;
1023 
1024 	ret = iwl_mvm_start_get_nvm(mvm);
1025 	if (ret)
1026 		goto out_free;
1027 
1028 	ret = iwl_mvm_start_post_nvm(mvm);
1029 	if (ret)
1030 		goto out_free;
1031 
1032 	return;
1033 
1034 out_free:
1035 	IWL_ERR(mvm, "Couldn't get started...\n");
1036 	iwl_mei_start_unregister();
1037 	iwl_mei_unregister_complete();
1038 	iwl_fw_flush_dumps(&mvm->fwrt);
1039 	iwl_mvm_thermal_exit(mvm);
1040 	iwl_fw_runtime_free(&mvm->fwrt);
1041 	iwl_phy_db_free(mvm->phy_db);
1042 	kfree(mvm->scan_cmd);
1043 	iwl_trans_op_mode_leave(mvm->trans);
1044 	kfree(mvm->nvm_data);
1045 	kfree(mvm->mei_nvm_data);
1046 
1047 	ieee80211_free_hw(mvm->hw);
1048 }
1049 
1050 static void iwl_mvm_mei_sap_connected(void *priv)
1051 {
1052 	struct iwl_mvm *mvm = priv;
1053 
1054 	if (!mvm->hw_registered)
1055 		schedule_work(&mvm->sap_connected_wk);
1056 }
1057 
1058 static void iwl_mvm_mei_nic_stolen(void *priv)
1059 {
1060 	struct iwl_mvm *mvm = priv;
1061 
1062 	rtnl_lock();
1063 	cfg80211_shutdown_all_interfaces(mvm->hw->wiphy);
1064 	rtnl_unlock();
1065 }
1066 
1067 static const struct iwl_mei_ops mei_ops = {
1068 	.me_conn_status = iwl_mvm_me_conn_status,
1069 	.rfkill = iwl_mvm_mei_rfkill,
1070 	.roaming_forbidden = iwl_mvm_mei_roaming_forbidden,
1071 	.sap_connected = iwl_mvm_mei_sap_connected,
1072 	.nic_stolen = iwl_mvm_mei_nic_stolen,
1073 };
1074 
1075 static struct iwl_op_mode *
1076 iwl_op_mode_mvm_start(struct iwl_trans *trans, const struct iwl_cfg *cfg,
1077 		      const struct iwl_fw *fw, struct dentry *dbgfs_dir)
1078 {
1079 	struct ieee80211_hw *hw;
1080 	struct iwl_op_mode *op_mode;
1081 	struct iwl_mvm *mvm;
1082 	struct iwl_trans_config trans_cfg = {};
1083 	static const u8 no_reclaim_cmds[] = {
1084 		TX_CMD,
1085 	};
1086 	int scan_size;
1087 	u32 min_backoff;
1088 	struct iwl_mvm_csme_conn_info *csme_conn_info __maybe_unused;
1089 
1090 	/*
1091 	 * We use IWL_MVM_STATION_COUNT_MAX to check the validity of the station
1092 	 * index all over the driver - check that its value corresponds to the
1093 	 * array size.
1094 	 */
1095 	BUILD_BUG_ON(ARRAY_SIZE(mvm->fw_id_to_mac_id) !=
1096 		     IWL_MVM_STATION_COUNT_MAX);
1097 
1098 	/********************************
1099 	 * 1. Allocating and configuring HW data
1100 	 ********************************/
1101 	hw = ieee80211_alloc_hw(sizeof(struct iwl_op_mode) +
1102 				sizeof(struct iwl_mvm),
1103 				&iwl_mvm_hw_ops);
1104 	if (!hw)
1105 		return NULL;
1106 
1107 	hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_HE;
1108 
1109 	if (cfg->max_tx_agg_size)
1110 		hw->max_tx_aggregation_subframes = cfg->max_tx_agg_size;
1111 	else
1112 		hw->max_tx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_HE;
1113 
1114 	op_mode = hw->priv;
1115 
1116 	mvm = IWL_OP_MODE_GET_MVM(op_mode);
1117 	mvm->dev = trans->dev;
1118 	mvm->trans = trans;
1119 	mvm->cfg = cfg;
1120 	mvm->fw = fw;
1121 	mvm->hw = hw;
1122 
1123 	iwl_fw_runtime_init(&mvm->fwrt, trans, fw, &iwl_mvm_fwrt_ops, mvm,
1124 			    &iwl_mvm_sanitize_ops, mvm, dbgfs_dir);
1125 
1126 	iwl_mvm_get_acpi_tables(mvm);
1127 	iwl_uefi_get_sgom_table(trans, &mvm->fwrt);
1128 
1129 	mvm->init_status = 0;
1130 
1131 	if (iwl_mvm_has_new_rx_api(mvm)) {
1132 		op_mode->ops = &iwl_mvm_ops_mq;
1133 		trans->rx_mpdu_cmd_hdr_size =
1134 			(trans->trans_cfg->device_family >=
1135 			 IWL_DEVICE_FAMILY_AX210) ?
1136 			sizeof(struct iwl_rx_mpdu_desc) :
1137 			IWL_RX_DESC_SIZE_V1;
1138 	} else {
1139 		op_mode->ops = &iwl_mvm_ops;
1140 		trans->rx_mpdu_cmd_hdr_size =
1141 			sizeof(struct iwl_rx_mpdu_res_start);
1142 
1143 		if (WARN_ON(trans->num_rx_queues > 1))
1144 			goto out_free;
1145 	}
1146 
1147 	mvm->fw_restart = iwlwifi_mod_params.fw_restart ? -1 : 0;
1148 
1149 	if (iwl_mvm_has_new_tx_api(mvm)) {
1150 		/*
1151 		 * If we have the new TX/queue allocation API initialize them
1152 		 * all to invalid numbers. We'll rewrite the ones that we need
1153 		 * later, but that doesn't happen for all of them all of the
1154 		 * time (e.g. P2P Device is optional), and if a dynamic queue
1155 		 * ends up getting number 2 (IWL_MVM_DQA_P2P_DEVICE_QUEUE) then
1156 		 * iwl_mvm_is_static_queue() erroneously returns true, and we
1157 		 * might have things getting stuck.
1158 		 */
1159 		mvm->aux_queue = IWL_MVM_INVALID_QUEUE;
1160 		mvm->snif_queue = IWL_MVM_INVALID_QUEUE;
1161 		mvm->probe_queue = IWL_MVM_INVALID_QUEUE;
1162 		mvm->p2p_dev_queue = IWL_MVM_INVALID_QUEUE;
1163 	} else {
1164 		mvm->aux_queue = IWL_MVM_DQA_AUX_QUEUE;
1165 		mvm->snif_queue = IWL_MVM_DQA_INJECT_MONITOR_QUEUE;
1166 		mvm->probe_queue = IWL_MVM_DQA_AP_PROBE_RESP_QUEUE;
1167 		mvm->p2p_dev_queue = IWL_MVM_DQA_P2P_DEVICE_QUEUE;
1168 	}
1169 
1170 	mvm->sf_state = SF_UNINIT;
1171 	if (iwl_mvm_has_unified_ucode(mvm))
1172 		iwl_fw_set_current_image(&mvm->fwrt, IWL_UCODE_REGULAR);
1173 	else
1174 		iwl_fw_set_current_image(&mvm->fwrt, IWL_UCODE_INIT);
1175 	mvm->drop_bcn_ap_mode = true;
1176 
1177 	mutex_init(&mvm->mutex);
1178 	spin_lock_init(&mvm->async_handlers_lock);
1179 	INIT_LIST_HEAD(&mvm->time_event_list);
1180 	INIT_LIST_HEAD(&mvm->aux_roc_te_list);
1181 	INIT_LIST_HEAD(&mvm->async_handlers_list);
1182 	spin_lock_init(&mvm->time_event_lock);
1183 	INIT_LIST_HEAD(&mvm->ftm_initiator.loc_list);
1184 	INIT_LIST_HEAD(&mvm->ftm_initiator.pasn_list);
1185 	INIT_LIST_HEAD(&mvm->resp_pasn_list);
1186 
1187 	INIT_WORK(&mvm->async_handlers_wk, iwl_mvm_async_handlers_wk);
1188 	INIT_WORK(&mvm->roc_done_wk, iwl_mvm_roc_done_wk);
1189 	INIT_WORK(&mvm->sap_connected_wk, iwl_mvm_sap_connected_wk);
1190 	INIT_DELAYED_WORK(&mvm->tdls_cs.dwork, iwl_mvm_tdls_ch_switch_work);
1191 	INIT_DELAYED_WORK(&mvm->scan_timeout_dwork, iwl_mvm_scan_timeout_wk);
1192 	INIT_WORK(&mvm->add_stream_wk, iwl_mvm_add_new_dqa_stream_wk);
1193 	INIT_LIST_HEAD(&mvm->add_stream_txqs);
1194 
1195 	init_waitqueue_head(&mvm->rx_sync_waitq);
1196 
1197 	mvm->queue_sync_state = 0;
1198 
1199 	SET_IEEE80211_DEV(mvm->hw, mvm->trans->dev);
1200 
1201 	spin_lock_init(&mvm->tcm.lock);
1202 	INIT_DELAYED_WORK(&mvm->tcm.work, iwl_mvm_tcm_work);
1203 	mvm->tcm.ts = jiffies;
1204 	mvm->tcm.ll_ts = jiffies;
1205 	mvm->tcm.uapsd_nonagg_ts = jiffies;
1206 
1207 	INIT_DELAYED_WORK(&mvm->cs_tx_unblock_dwork, iwl_mvm_tx_unblock_dwork);
1208 
1209 	mvm->cmd_ver.d0i3_resp =
1210 		iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP, D0I3_END_CMD,
1211 					0);
1212 	/* we only support version 1 */
1213 	if (WARN_ON_ONCE(mvm->cmd_ver.d0i3_resp > 1))
1214 		goto out_free;
1215 
1216 	mvm->cmd_ver.range_resp =
1217 		iwl_fw_lookup_notif_ver(mvm->fw, LOCATION_GROUP,
1218 					TOF_RANGE_RESPONSE_NOTIF, 5);
1219 	/* we only support up to version 9 */
1220 	if (WARN_ON_ONCE(mvm->cmd_ver.range_resp > 9))
1221 		goto out_free;
1222 
1223 	/*
1224 	 * Populate the state variables that the transport layer needs
1225 	 * to know about.
1226 	 */
1227 	trans_cfg.op_mode = op_mode;
1228 	trans_cfg.no_reclaim_cmds = no_reclaim_cmds;
1229 	trans_cfg.n_no_reclaim_cmds = ARRAY_SIZE(no_reclaim_cmds);
1230 
1231 	switch (iwlwifi_mod_params.amsdu_size) {
1232 	case IWL_AMSDU_DEF:
1233 		trans_cfg.rx_buf_size = IWL_AMSDU_4K;
1234 		break;
1235 	case IWL_AMSDU_4K:
1236 		trans_cfg.rx_buf_size = IWL_AMSDU_4K;
1237 		break;
1238 	case IWL_AMSDU_8K:
1239 		trans_cfg.rx_buf_size = IWL_AMSDU_8K;
1240 		break;
1241 	case IWL_AMSDU_12K:
1242 		trans_cfg.rx_buf_size = IWL_AMSDU_12K;
1243 		break;
1244 	default:
1245 		pr_err("%s: Unsupported amsdu_size: %d\n", KBUILD_MODNAME,
1246 		       iwlwifi_mod_params.amsdu_size);
1247 		trans_cfg.rx_buf_size = IWL_AMSDU_4K;
1248 	}
1249 
1250 	trans->wide_cmd_header = true;
1251 	trans_cfg.bc_table_dword =
1252 		mvm->trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_AX210;
1253 
1254 	trans_cfg.command_groups = iwl_mvm_groups;
1255 	trans_cfg.command_groups_size = ARRAY_SIZE(iwl_mvm_groups);
1256 
1257 	trans_cfg.cmd_queue = IWL_MVM_DQA_CMD_QUEUE;
1258 	trans_cfg.cmd_fifo = IWL_MVM_TX_FIFO_CMD;
1259 	trans_cfg.scd_set_active = true;
1260 
1261 	trans_cfg.cb_data_offs = offsetof(struct ieee80211_tx_info,
1262 					  driver_data[2]);
1263 
1264 	/* Set a short watchdog for the command queue */
1265 	trans_cfg.cmd_q_wdg_timeout =
1266 		iwl_mvm_get_wd_timeout(mvm, NULL, false, true);
1267 
1268 	snprintf(mvm->hw->wiphy->fw_version,
1269 		 sizeof(mvm->hw->wiphy->fw_version),
1270 		 "%s", fw->fw_version);
1271 
1272 	trans_cfg.fw_reset_handshake = fw_has_capa(&mvm->fw->ucode_capa,
1273 						   IWL_UCODE_TLV_CAPA_FW_RESET_HANDSHAKE);
1274 
1275 	trans_cfg.queue_alloc_cmd_ver =
1276 		iwl_fw_lookup_cmd_ver(mvm->fw,
1277 				      WIDE_ID(DATA_PATH_GROUP,
1278 					      SCD_QUEUE_CONFIG_CMD),
1279 				      0);
1280 	mvm->sta_remove_requires_queue_remove =
1281 		trans_cfg.queue_alloc_cmd_ver > 0;
1282 
1283 	/* Configure transport layer */
1284 	iwl_trans_configure(mvm->trans, &trans_cfg);
1285 
1286 	trans->rx_mpdu_cmd = REPLY_RX_MPDU_CMD;
1287 	trans->dbg.dest_tlv = mvm->fw->dbg.dest_tlv;
1288 	trans->dbg.n_dest_reg = mvm->fw->dbg.n_dest_reg;
1289 	memcpy(trans->dbg.conf_tlv, mvm->fw->dbg.conf_tlv,
1290 	       sizeof(trans->dbg.conf_tlv));
1291 	trans->dbg.trigger_tlv = mvm->fw->dbg.trigger_tlv;
1292 
1293 	trans->iml = mvm->fw->iml;
1294 	trans->iml_len = mvm->fw->iml_len;
1295 
1296 	/* set up notification wait support */
1297 	iwl_notification_wait_init(&mvm->notif_wait);
1298 
1299 	/* Init phy db */
1300 	mvm->phy_db = iwl_phy_db_init(trans);
1301 	if (!mvm->phy_db) {
1302 		IWL_ERR(mvm, "Cannot init phy_db\n");
1303 		goto out_free;
1304 	}
1305 
1306 	IWL_INFO(mvm, "Detected %s, REV=0x%X\n",
1307 		 mvm->trans->name, mvm->trans->hw_rev);
1308 
1309 	if (iwlwifi_mod_params.nvm_file)
1310 		mvm->nvm_file_name = iwlwifi_mod_params.nvm_file;
1311 	else
1312 		IWL_DEBUG_EEPROM(mvm->trans->dev,
1313 				 "working without external nvm file\n");
1314 
1315 	scan_size = iwl_mvm_scan_size(mvm);
1316 
1317 	mvm->scan_cmd = kmalloc(scan_size, GFP_KERNEL);
1318 	if (!mvm->scan_cmd)
1319 		goto out_free;
1320 
1321 	/* invalidate ids to prevent accidental removal of sta_id 0 */
1322 	mvm->aux_sta.sta_id = IWL_MVM_INVALID_STA;
1323 	mvm->snif_sta.sta_id = IWL_MVM_INVALID_STA;
1324 
1325 	/* Set EBS as successful as long as not stated otherwise by the FW. */
1326 	mvm->last_ebs_successful = true;
1327 
1328 	min_backoff = iwl_mvm_min_backoff(mvm);
1329 	iwl_mvm_thermal_initialize(mvm, min_backoff);
1330 
1331 	if (!iwl_mvm_has_new_rx_stats_api(mvm))
1332 		memset(&mvm->rx_stats_v3, 0,
1333 		       sizeof(struct mvm_statistics_rx_v3));
1334 	else
1335 		memset(&mvm->rx_stats, 0, sizeof(struct mvm_statistics_rx));
1336 
1337 	mvm->debugfs_dir = dbgfs_dir;
1338 
1339 	mvm->mei_registered = !iwl_mei_register(mvm, &mei_ops);
1340 
1341 	if (iwl_mvm_start_get_nvm(mvm)) {
1342 		/*
1343 		 * Getting NVM failed while CSME is the owner, but we are
1344 		 * registered to MEI, we'll get the NVM later when it'll be
1345 		 * possible to get it from CSME.
1346 		 */
1347 		if (trans->csme_own && mvm->mei_registered)
1348 			return op_mode;
1349 
1350 		goto out_thermal_exit;
1351 	}
1352 
1353 
1354 	if (iwl_mvm_start_post_nvm(mvm))
1355 		goto out_thermal_exit;
1356 
1357 	return op_mode;
1358 
1359  out_thermal_exit:
1360 	iwl_mvm_thermal_exit(mvm);
1361 	if (mvm->mei_registered) {
1362 		iwl_mei_start_unregister();
1363 		iwl_mei_unregister_complete();
1364 	}
1365  out_free:
1366 	iwl_fw_flush_dumps(&mvm->fwrt);
1367 	iwl_fw_runtime_free(&mvm->fwrt);
1368 
1369 	if (iwlmvm_mod_params.init_dbg)
1370 		return op_mode;
1371 	iwl_phy_db_free(mvm->phy_db);
1372 	kfree(mvm->scan_cmd);
1373 	iwl_trans_op_mode_leave(trans);
1374 
1375 	ieee80211_free_hw(mvm->hw);
1376 	return NULL;
1377 }
1378 
1379 void iwl_mvm_stop_device(struct iwl_mvm *mvm)
1380 {
1381 	lockdep_assert_held(&mvm->mutex);
1382 
1383 	iwl_fw_cancel_timestamp(&mvm->fwrt);
1384 
1385 	clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
1386 
1387 	iwl_fw_dbg_stop_sync(&mvm->fwrt);
1388 	iwl_trans_stop_device(mvm->trans);
1389 	iwl_free_fw_paging(&mvm->fwrt);
1390 	iwl_fw_dump_conf_clear(&mvm->fwrt);
1391 	iwl_mvm_mei_device_down(mvm);
1392 }
1393 
1394 static void iwl_op_mode_mvm_stop(struct iwl_op_mode *op_mode)
1395 {
1396 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1397 	int i;
1398 
1399 	if (mvm->mei_registered) {
1400 		rtnl_lock();
1401 		iwl_mei_set_netdev(NULL);
1402 		rtnl_unlock();
1403 		iwl_mei_start_unregister();
1404 	}
1405 
1406 	/*
1407 	 * After we unregister from mei, the worker can't be scheduled
1408 	 * anymore.
1409 	 */
1410 	cancel_work_sync(&mvm->sap_connected_wk);
1411 
1412 	iwl_mvm_leds_exit(mvm);
1413 
1414 	iwl_mvm_thermal_exit(mvm);
1415 
1416 	/*
1417 	 * If we couldn't get ownership on the device and we couldn't
1418 	 * get the NVM from CSME, we haven't registered to mac80211.
1419 	 * In that case, we didn't fail op_mode_start, because we are
1420 	 * waiting for CSME to allow us to get the NVM to register to
1421 	 * mac80211. If that didn't happen, we haven't registered to
1422 	 * mac80211, hence the if below.
1423 	 */
1424 	if (mvm->hw_registered)
1425 		ieee80211_unregister_hw(mvm->hw);
1426 
1427 	kfree(mvm->scan_cmd);
1428 	kfree(mvm->mcast_filter_cmd);
1429 	mvm->mcast_filter_cmd = NULL;
1430 
1431 	kfree(mvm->error_recovery_buf);
1432 	mvm->error_recovery_buf = NULL;
1433 
1434 	iwl_trans_op_mode_leave(mvm->trans);
1435 
1436 	iwl_phy_db_free(mvm->phy_db);
1437 	mvm->phy_db = NULL;
1438 
1439 	kfree(mvm->nvm_data);
1440 	kfree(mvm->mei_nvm_data);
1441 	kfree(rcu_access_pointer(mvm->csme_conn_info));
1442 	kfree(mvm->temp_nvm_data);
1443 	for (i = 0; i < NVM_MAX_NUM_SECTIONS; i++)
1444 		kfree(mvm->nvm_sections[i].data);
1445 
1446 	cancel_delayed_work_sync(&mvm->tcm.work);
1447 
1448 	iwl_fw_runtime_free(&mvm->fwrt);
1449 	mutex_destroy(&mvm->mutex);
1450 
1451 	if (mvm->mei_registered)
1452 		iwl_mei_unregister_complete();
1453 
1454 	ieee80211_free_hw(mvm->hw);
1455 }
1456 
1457 struct iwl_async_handler_entry {
1458 	struct list_head list;
1459 	struct iwl_rx_cmd_buffer rxb;
1460 	enum iwl_rx_handler_context context;
1461 	void (*fn)(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb);
1462 };
1463 
1464 void iwl_mvm_async_handlers_purge(struct iwl_mvm *mvm)
1465 {
1466 	struct iwl_async_handler_entry *entry, *tmp;
1467 
1468 	spin_lock_bh(&mvm->async_handlers_lock);
1469 	list_for_each_entry_safe(entry, tmp, &mvm->async_handlers_list, list) {
1470 		iwl_free_rxb(&entry->rxb);
1471 		list_del(&entry->list);
1472 		kfree(entry);
1473 	}
1474 	spin_unlock_bh(&mvm->async_handlers_lock);
1475 }
1476 
1477 static void iwl_mvm_async_handlers_wk(struct work_struct *wk)
1478 {
1479 	struct iwl_mvm *mvm =
1480 		container_of(wk, struct iwl_mvm, async_handlers_wk);
1481 	struct iwl_async_handler_entry *entry, *tmp;
1482 	LIST_HEAD(local_list);
1483 
1484 	/* Ensure that we are not in stop flow (check iwl_mvm_mac_stop) */
1485 
1486 	/*
1487 	 * Sync with Rx path with a lock. Remove all the entries from this list,
1488 	 * add them to a local one (lock free), and then handle them.
1489 	 */
1490 	spin_lock_bh(&mvm->async_handlers_lock);
1491 	list_splice_init(&mvm->async_handlers_list, &local_list);
1492 	spin_unlock_bh(&mvm->async_handlers_lock);
1493 
1494 	list_for_each_entry_safe(entry, tmp, &local_list, list) {
1495 		if (entry->context == RX_HANDLER_ASYNC_LOCKED)
1496 			mutex_lock(&mvm->mutex);
1497 		entry->fn(mvm, &entry->rxb);
1498 		iwl_free_rxb(&entry->rxb);
1499 		list_del(&entry->list);
1500 		if (entry->context == RX_HANDLER_ASYNC_LOCKED)
1501 			mutex_unlock(&mvm->mutex);
1502 		kfree(entry);
1503 	}
1504 }
1505 
1506 static inline void iwl_mvm_rx_check_trigger(struct iwl_mvm *mvm,
1507 					    struct iwl_rx_packet *pkt)
1508 {
1509 	struct iwl_fw_dbg_trigger_tlv *trig;
1510 	struct iwl_fw_dbg_trigger_cmd *cmds_trig;
1511 	int i;
1512 
1513 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, NULL,
1514 				     FW_DBG_TRIGGER_FW_NOTIF);
1515 	if (!trig)
1516 		return;
1517 
1518 	cmds_trig = (void *)trig->data;
1519 
1520 	for (i = 0; i < ARRAY_SIZE(cmds_trig->cmds); i++) {
1521 		/* don't collect on CMD 0 */
1522 		if (!cmds_trig->cmds[i].cmd_id)
1523 			break;
1524 
1525 		if (cmds_trig->cmds[i].cmd_id != pkt->hdr.cmd ||
1526 		    cmds_trig->cmds[i].group_id != pkt->hdr.group_id)
1527 			continue;
1528 
1529 		iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
1530 					"CMD 0x%02x.%02x received",
1531 					pkt->hdr.group_id, pkt->hdr.cmd);
1532 		break;
1533 	}
1534 }
1535 
1536 static void iwl_mvm_rx_common(struct iwl_mvm *mvm,
1537 			      struct iwl_rx_cmd_buffer *rxb,
1538 			      struct iwl_rx_packet *pkt)
1539 {
1540 	unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
1541 	int i;
1542 	union iwl_dbg_tlv_tp_data tp_data = { .fw_pkt = pkt };
1543 
1544 	iwl_dbg_tlv_time_point(&mvm->fwrt,
1545 			       IWL_FW_INI_TIME_POINT_FW_RSP_OR_NOTIF, &tp_data);
1546 	iwl_mvm_rx_check_trigger(mvm, pkt);
1547 
1548 	/*
1549 	 * Do the notification wait before RX handlers so
1550 	 * even if the RX handler consumes the RXB we have
1551 	 * access to it in the notification wait entry.
1552 	 */
1553 	iwl_notification_wait_notify(&mvm->notif_wait, pkt);
1554 
1555 	for (i = 0; i < ARRAY_SIZE(iwl_mvm_rx_handlers); i++) {
1556 		const struct iwl_rx_handlers *rx_h = &iwl_mvm_rx_handlers[i];
1557 		struct iwl_async_handler_entry *entry;
1558 
1559 		if (rx_h->cmd_id != WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd))
1560 			continue;
1561 
1562 		if (unlikely(pkt_len < rx_h->min_size))
1563 			return;
1564 
1565 		if (rx_h->context == RX_HANDLER_SYNC) {
1566 			rx_h->fn(mvm, rxb);
1567 			return;
1568 		}
1569 
1570 		entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
1571 		/* we can't do much... */
1572 		if (!entry)
1573 			return;
1574 
1575 		entry->rxb._page = rxb_steal_page(rxb);
1576 		entry->rxb._offset = rxb->_offset;
1577 		entry->rxb._rx_page_order = rxb->_rx_page_order;
1578 		entry->fn = rx_h->fn;
1579 		entry->context = rx_h->context;
1580 		spin_lock(&mvm->async_handlers_lock);
1581 		list_add_tail(&entry->list, &mvm->async_handlers_list);
1582 		spin_unlock(&mvm->async_handlers_lock);
1583 		schedule_work(&mvm->async_handlers_wk);
1584 		break;
1585 	}
1586 }
1587 
1588 static void iwl_mvm_rx(struct iwl_op_mode *op_mode,
1589 		       struct napi_struct *napi,
1590 		       struct iwl_rx_cmd_buffer *rxb)
1591 {
1592 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1593 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1594 	u16 cmd = WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd);
1595 
1596 	if (likely(cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_MPDU_CMD)))
1597 		iwl_mvm_rx_rx_mpdu(mvm, napi, rxb);
1598 	else if (cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_PHY_CMD))
1599 		iwl_mvm_rx_rx_phy_cmd(mvm, rxb);
1600 	else
1601 		iwl_mvm_rx_common(mvm, rxb, pkt);
1602 }
1603 
1604 void iwl_mvm_rx_mq(struct iwl_op_mode *op_mode,
1605 		   struct napi_struct *napi,
1606 		   struct iwl_rx_cmd_buffer *rxb)
1607 {
1608 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1609 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1610 	u16 cmd = WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd);
1611 
1612 	if (likely(cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_MPDU_CMD)))
1613 		iwl_mvm_rx_mpdu_mq(mvm, napi, rxb, 0);
1614 	else if (unlikely(cmd == WIDE_ID(DATA_PATH_GROUP,
1615 					 RX_QUEUES_NOTIFICATION)))
1616 		iwl_mvm_rx_queue_notif(mvm, napi, rxb, 0);
1617 	else if (cmd == WIDE_ID(LEGACY_GROUP, FRAME_RELEASE))
1618 		iwl_mvm_rx_frame_release(mvm, napi, rxb, 0);
1619 	else if (cmd == WIDE_ID(LEGACY_GROUP, BAR_FRAME_RELEASE))
1620 		iwl_mvm_rx_bar_frame_release(mvm, napi, rxb, 0);
1621 	else if (cmd == WIDE_ID(DATA_PATH_GROUP, RX_NO_DATA_NOTIF))
1622 		iwl_mvm_rx_monitor_no_data(mvm, napi, rxb, 0);
1623 	else
1624 		iwl_mvm_rx_common(mvm, rxb, pkt);
1625 }
1626 
1627 static void iwl_mvm_async_cb(struct iwl_op_mode *op_mode,
1628 			     const struct iwl_device_cmd *cmd)
1629 {
1630 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1631 
1632 	/*
1633 	 * For now, we only set the CMD_WANT_ASYNC_CALLBACK for ADD_STA
1634 	 * commands that need to block the Tx queues.
1635 	 */
1636 	iwl_trans_block_txq_ptrs(mvm->trans, false);
1637 }
1638 
1639 static int iwl_mvm_is_static_queue(struct iwl_mvm *mvm, int queue)
1640 {
1641 	return queue == mvm->aux_queue || queue == mvm->probe_queue ||
1642 		queue == mvm->p2p_dev_queue || queue == mvm->snif_queue;
1643 }
1644 
1645 static void iwl_mvm_queue_state_change(struct iwl_op_mode *op_mode,
1646 				       int hw_queue, bool start)
1647 {
1648 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1649 	struct ieee80211_sta *sta;
1650 	struct ieee80211_txq *txq;
1651 	struct iwl_mvm_txq *mvmtxq;
1652 	int i;
1653 	unsigned long tid_bitmap;
1654 	struct iwl_mvm_sta *mvmsta;
1655 	u8 sta_id;
1656 
1657 	sta_id = iwl_mvm_has_new_tx_api(mvm) ?
1658 		mvm->tvqm_info[hw_queue].sta_id :
1659 		mvm->queue_info[hw_queue].ra_sta_id;
1660 
1661 	if (WARN_ON_ONCE(sta_id >= mvm->fw->ucode_capa.num_stations))
1662 		return;
1663 
1664 	rcu_read_lock();
1665 
1666 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1667 	if (IS_ERR_OR_NULL(sta))
1668 		goto out;
1669 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
1670 
1671 	if (iwl_mvm_is_static_queue(mvm, hw_queue)) {
1672 		if (!start)
1673 			ieee80211_stop_queues(mvm->hw);
1674 		else if (mvmsta->sta_state != IEEE80211_STA_NOTEXIST)
1675 			ieee80211_wake_queues(mvm->hw);
1676 
1677 		goto out;
1678 	}
1679 
1680 	if (iwl_mvm_has_new_tx_api(mvm)) {
1681 		int tid = mvm->tvqm_info[hw_queue].txq_tid;
1682 
1683 		tid_bitmap = BIT(tid);
1684 	} else {
1685 		tid_bitmap = mvm->queue_info[hw_queue].tid_bitmap;
1686 	}
1687 
1688 	for_each_set_bit(i, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1689 		int tid = i;
1690 
1691 		if (tid == IWL_MAX_TID_COUNT)
1692 			tid = IEEE80211_NUM_TIDS;
1693 
1694 		txq = sta->txq[tid];
1695 		mvmtxq = iwl_mvm_txq_from_mac80211(txq);
1696 		mvmtxq->stopped = !start;
1697 
1698 		if (start && mvmsta->sta_state != IEEE80211_STA_NOTEXIST)
1699 			iwl_mvm_mac_itxq_xmit(mvm->hw, txq);
1700 	}
1701 
1702 out:
1703 	rcu_read_unlock();
1704 }
1705 
1706 static void iwl_mvm_stop_sw_queue(struct iwl_op_mode *op_mode, int hw_queue)
1707 {
1708 	iwl_mvm_queue_state_change(op_mode, hw_queue, false);
1709 }
1710 
1711 static void iwl_mvm_wake_sw_queue(struct iwl_op_mode *op_mode, int hw_queue)
1712 {
1713 	iwl_mvm_queue_state_change(op_mode, hw_queue, true);
1714 }
1715 
1716 static void iwl_mvm_set_rfkill_state(struct iwl_mvm *mvm)
1717 {
1718 	bool state = iwl_mvm_is_radio_killed(mvm);
1719 
1720 	if (state)
1721 		wake_up(&mvm->rx_sync_waitq);
1722 
1723 	wiphy_rfkill_set_hw_state(mvm->hw->wiphy, state);
1724 }
1725 
1726 void iwl_mvm_set_hw_ctkill_state(struct iwl_mvm *mvm, bool state)
1727 {
1728 	if (state)
1729 		set_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status);
1730 	else
1731 		clear_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status);
1732 
1733 	iwl_mvm_set_rfkill_state(mvm);
1734 }
1735 
1736 struct iwl_mvm_csme_conn_info *iwl_mvm_get_csme_conn_info(struct iwl_mvm *mvm)
1737 {
1738 	return rcu_dereference_protected(mvm->csme_conn_info,
1739 					 lockdep_is_held(&mvm->mutex));
1740 }
1741 
1742 static bool iwl_mvm_set_hw_rfkill_state(struct iwl_op_mode *op_mode, bool state)
1743 {
1744 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1745 	bool rfkill_safe_init_done = READ_ONCE(mvm->rfkill_safe_init_done);
1746 	bool unified = iwl_mvm_has_unified_ucode(mvm);
1747 
1748 	if (state)
1749 		set_bit(IWL_MVM_STATUS_HW_RFKILL, &mvm->status);
1750 	else
1751 		clear_bit(IWL_MVM_STATUS_HW_RFKILL, &mvm->status);
1752 
1753 	iwl_mvm_set_rfkill_state(mvm);
1754 
1755 	 /* iwl_run_init_mvm_ucode is waiting for results, abort it. */
1756 	if (rfkill_safe_init_done)
1757 		iwl_abort_notification_waits(&mvm->notif_wait);
1758 
1759 	/*
1760 	 * Don't ask the transport to stop the firmware. We'll do it
1761 	 * after cfg80211 takes us down.
1762 	 */
1763 	if (unified)
1764 		return false;
1765 
1766 	/*
1767 	 * Stop the device if we run OPERATIONAL firmware or if we are in the
1768 	 * middle of the calibrations.
1769 	 */
1770 	return state && rfkill_safe_init_done;
1771 }
1772 
1773 static void iwl_mvm_free_skb(struct iwl_op_mode *op_mode, struct sk_buff *skb)
1774 {
1775 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1776 	struct ieee80211_tx_info *info;
1777 
1778 	info = IEEE80211_SKB_CB(skb);
1779 	iwl_trans_free_tx_cmd(mvm->trans, info->driver_data[1]);
1780 	ieee80211_free_txskb(mvm->hw, skb);
1781 }
1782 
1783 struct iwl_mvm_reprobe {
1784 	struct device *dev;
1785 	struct work_struct work;
1786 };
1787 
1788 static void iwl_mvm_reprobe_wk(struct work_struct *wk)
1789 {
1790 	struct iwl_mvm_reprobe *reprobe;
1791 
1792 	reprobe = container_of(wk, struct iwl_mvm_reprobe, work);
1793 	if (device_reprobe(reprobe->dev))
1794 		dev_err(reprobe->dev, "reprobe failed!\n");
1795 	put_device(reprobe->dev);
1796 	kfree(reprobe);
1797 	module_put(THIS_MODULE);
1798 }
1799 
1800 void iwl_mvm_nic_restart(struct iwl_mvm *mvm, bool fw_error)
1801 {
1802 	iwl_abort_notification_waits(&mvm->notif_wait);
1803 	iwl_dbg_tlv_del_timers(mvm->trans);
1804 
1805 	/*
1806 	 * This is a bit racy, but worst case we tell mac80211 about
1807 	 * a stopped/aborted scan when that was already done which
1808 	 * is not a problem. It is necessary to abort any os scan
1809 	 * here because mac80211 requires having the scan cleared
1810 	 * before restarting.
1811 	 * We'll reset the scan_status to NONE in restart cleanup in
1812 	 * the next start() call from mac80211. If restart isn't called
1813 	 * (no fw restart) scan status will stay busy.
1814 	 */
1815 	iwl_mvm_report_scan_aborted(mvm);
1816 
1817 	/*
1818 	 * If we're restarting already, don't cycle restarts.
1819 	 * If INIT fw asserted, it will likely fail again.
1820 	 * If WoWLAN fw asserted, don't restart either, mac80211
1821 	 * can't recover this since we're already half suspended.
1822 	 */
1823 	if (!mvm->fw_restart && fw_error) {
1824 		iwl_fw_error_collect(&mvm->fwrt, false);
1825 	} else if (test_bit(IWL_MVM_STATUS_STARTING,
1826 			    &mvm->status)) {
1827 		IWL_ERR(mvm, "Starting mac, retry will be triggered anyway\n");
1828 	} else if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1829 		struct iwl_mvm_reprobe *reprobe;
1830 
1831 		IWL_ERR(mvm,
1832 			"Firmware error during reconfiguration - reprobe!\n");
1833 
1834 		/*
1835 		 * get a module reference to avoid doing this while unloading
1836 		 * anyway and to avoid scheduling a work with code that's
1837 		 * being removed.
1838 		 */
1839 		if (!try_module_get(THIS_MODULE)) {
1840 			IWL_ERR(mvm, "Module is being unloaded - abort\n");
1841 			return;
1842 		}
1843 
1844 		reprobe = kzalloc(sizeof(*reprobe), GFP_ATOMIC);
1845 		if (!reprobe) {
1846 			module_put(THIS_MODULE);
1847 			return;
1848 		}
1849 		reprobe->dev = get_device(mvm->trans->dev);
1850 		INIT_WORK(&reprobe->work, iwl_mvm_reprobe_wk);
1851 		schedule_work(&reprobe->work);
1852 	} else if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
1853 			    &mvm->status)) {
1854 		IWL_ERR(mvm, "HW restart already requested, but not started\n");
1855 	} else if (mvm->fwrt.cur_fw_img == IWL_UCODE_REGULAR &&
1856 		   mvm->hw_registered &&
1857 		   !test_bit(STATUS_TRANS_DEAD, &mvm->trans->status)) {
1858 		/* This should be first thing before trying to collect any
1859 		 * data to avoid endless loops if any HW error happens while
1860 		 * collecting debug data.
1861 		 */
1862 		set_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
1863 
1864 		if (mvm->fw->ucode_capa.error_log_size) {
1865 			u32 src_size = mvm->fw->ucode_capa.error_log_size;
1866 			u32 src_addr = mvm->fw->ucode_capa.error_log_addr;
1867 			u8 *recover_buf = kzalloc(src_size, GFP_ATOMIC);
1868 
1869 			if (recover_buf) {
1870 				mvm->error_recovery_buf = recover_buf;
1871 				iwl_trans_read_mem_bytes(mvm->trans,
1872 							 src_addr,
1873 							 recover_buf,
1874 							 src_size);
1875 			}
1876 		}
1877 
1878 		iwl_fw_error_collect(&mvm->fwrt, false);
1879 
1880 		if (fw_error && mvm->fw_restart > 0) {
1881 			mvm->fw_restart--;
1882 			ieee80211_restart_hw(mvm->hw);
1883 		} else if (mvm->fwrt.trans->dbg.restart_required) {
1884 			IWL_DEBUG_INFO(mvm, "FW restart requested after debug collection\n");
1885 			mvm->fwrt.trans->dbg.restart_required = FALSE;
1886 			ieee80211_restart_hw(mvm->hw);
1887 		} else if (mvm->trans->trans_cfg->device_family <= IWL_DEVICE_FAMILY_8000) {
1888 			ieee80211_restart_hw(mvm->hw);
1889 		}
1890 	}
1891 }
1892 
1893 static void iwl_mvm_nic_error(struct iwl_op_mode *op_mode, bool sync)
1894 {
1895 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1896 
1897 	if (!test_bit(STATUS_TRANS_DEAD, &mvm->trans->status) &&
1898 	    !test_and_clear_bit(IWL_MVM_STATUS_SUPPRESS_ERROR_LOG_ONCE,
1899 				&mvm->status))
1900 		iwl_mvm_dump_nic_error_log(mvm);
1901 
1902 	if (sync) {
1903 		iwl_fw_error_collect(&mvm->fwrt, true);
1904 		/*
1905 		 * Currently, the only case for sync=true is during
1906 		 * shutdown, so just stop in this case. If/when that
1907 		 * changes, we need to be a bit smarter here.
1908 		 */
1909 		return;
1910 	}
1911 
1912 	/*
1913 	 * If the firmware crashes while we're already considering it
1914 	 * to be dead then don't ask for a restart, that cannot do
1915 	 * anything useful anyway.
1916 	 */
1917 	if (!test_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status))
1918 		return;
1919 
1920 	iwl_mvm_nic_restart(mvm, false);
1921 }
1922 
1923 static void iwl_mvm_cmd_queue_full(struct iwl_op_mode *op_mode)
1924 {
1925 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1926 
1927 	WARN_ON(1);
1928 	iwl_mvm_nic_restart(mvm, true);
1929 }
1930 
1931 static void iwl_op_mode_mvm_time_point(struct iwl_op_mode *op_mode,
1932 				       enum iwl_fw_ini_time_point tp_id,
1933 				       union iwl_dbg_tlv_tp_data *tp_data)
1934 {
1935 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1936 
1937 	iwl_dbg_tlv_time_point(&mvm->fwrt, tp_id, tp_data);
1938 }
1939 
1940 #define IWL_MVM_COMMON_OPS					\
1941 	/* these could be differentiated */			\
1942 	.async_cb = iwl_mvm_async_cb,				\
1943 	.queue_full = iwl_mvm_stop_sw_queue,			\
1944 	.queue_not_full = iwl_mvm_wake_sw_queue,		\
1945 	.hw_rf_kill = iwl_mvm_set_hw_rfkill_state,		\
1946 	.free_skb = iwl_mvm_free_skb,				\
1947 	.nic_error = iwl_mvm_nic_error,				\
1948 	.cmd_queue_full = iwl_mvm_cmd_queue_full,		\
1949 	.nic_config = iwl_mvm_nic_config,			\
1950 	/* as we only register one, these MUST be common! */	\
1951 	.start = iwl_op_mode_mvm_start,				\
1952 	.stop = iwl_op_mode_mvm_stop,				\
1953 	.time_point = iwl_op_mode_mvm_time_point
1954 
1955 static const struct iwl_op_mode_ops iwl_mvm_ops = {
1956 	IWL_MVM_COMMON_OPS,
1957 	.rx = iwl_mvm_rx,
1958 };
1959 
1960 static void iwl_mvm_rx_mq_rss(struct iwl_op_mode *op_mode,
1961 			      struct napi_struct *napi,
1962 			      struct iwl_rx_cmd_buffer *rxb,
1963 			      unsigned int queue)
1964 {
1965 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1966 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1967 	u16 cmd = WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd);
1968 
1969 	if (unlikely(queue >= mvm->trans->num_rx_queues))
1970 		return;
1971 
1972 	if (unlikely(cmd == WIDE_ID(LEGACY_GROUP, FRAME_RELEASE)))
1973 		iwl_mvm_rx_frame_release(mvm, napi, rxb, queue);
1974 	else if (unlikely(cmd == WIDE_ID(DATA_PATH_GROUP,
1975 					 RX_QUEUES_NOTIFICATION)))
1976 		iwl_mvm_rx_queue_notif(mvm, napi, rxb, queue);
1977 	else if (likely(cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_MPDU_CMD)))
1978 		iwl_mvm_rx_mpdu_mq(mvm, napi, rxb, queue);
1979 }
1980 
1981 static const struct iwl_op_mode_ops iwl_mvm_ops_mq = {
1982 	IWL_MVM_COMMON_OPS,
1983 	.rx = iwl_mvm_rx_mq,
1984 	.rx_rss = iwl_mvm_rx_mq_rss,
1985 };
1986