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