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