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(BCAST_FILTER_CMD),
473 	HCMD_NAME(MCAST_FILTER_CMD),
474 	HCMD_NAME(REPLY_SF_CFG_CMD),
475 	HCMD_NAME(REPLY_BEACON_FILTERING_CMD),
476 	HCMD_NAME(D3_CONFIG_CMD),
477 	HCMD_NAME(PROT_OFFLOAD_CONFIG_CMD),
478 	HCMD_NAME(OFFLOADS_QUERY_CMD),
479 	HCMD_NAME(MATCH_FOUND_NOTIFICATION),
480 	HCMD_NAME(DTS_MEASUREMENT_NOTIFICATION),
481 	HCMD_NAME(WOWLAN_PATTERNS),
482 	HCMD_NAME(WOWLAN_CONFIGURATION),
483 	HCMD_NAME(WOWLAN_TSC_RSC_PARAM),
484 	HCMD_NAME(WOWLAN_TKIP_PARAM),
485 	HCMD_NAME(WOWLAN_KEK_KCK_MATERIAL),
486 	HCMD_NAME(WOWLAN_GET_STATUSES),
487 	HCMD_NAME(SCAN_ITERATION_COMPLETE),
488 	HCMD_NAME(D0I3_END_CMD),
489 	HCMD_NAME(LTR_CONFIG),
490 	HCMD_NAME(LDBG_CONFIG_CMD),
491 };
492 
493 /* Please keep this array *SORTED* by hex value.
494  * Access is done through binary search
495  */
496 static const struct iwl_hcmd_names iwl_mvm_system_names[] = {
497 	HCMD_NAME(SHARED_MEM_CFG_CMD),
498 	HCMD_NAME(INIT_EXTENDED_CFG_CMD),
499 	HCMD_NAME(FW_ERROR_RECOVERY_CMD),
500 	HCMD_NAME(RFI_CONFIG_CMD),
501 	HCMD_NAME(RFI_GET_FREQ_TABLE_CMD),
502 	HCMD_NAME(SYSTEM_FEATURES_CONTROL_CMD),
503 };
504 
505 /* Please keep this array *SORTED* by hex value.
506  * Access is done through binary search
507  */
508 static const struct iwl_hcmd_names iwl_mvm_mac_conf_names[] = {
509 	HCMD_NAME(CHANNEL_SWITCH_TIME_EVENT_CMD),
510 	HCMD_NAME(SESSION_PROTECTION_CMD),
511 	HCMD_NAME(SESSION_PROTECTION_NOTIF),
512 	HCMD_NAME(CHANNEL_SWITCH_START_NOTIF),
513 };
514 
515 /* Please keep this array *SORTED* by hex value.
516  * Access is done through binary search
517  */
518 static const struct iwl_hcmd_names iwl_mvm_phy_names[] = {
519 	HCMD_NAME(CMD_DTS_MEASUREMENT_TRIGGER_WIDE),
520 	HCMD_NAME(CTDP_CONFIG_CMD),
521 	HCMD_NAME(TEMP_REPORTING_THRESHOLDS_CMD),
522 	HCMD_NAME(PER_CHAIN_LIMIT_OFFSET_CMD),
523 	HCMD_NAME(CT_KILL_NOTIFICATION),
524 	HCMD_NAME(DTS_MEASUREMENT_NOTIF_WIDE),
525 };
526 
527 /* Please keep this array *SORTED* by hex value.
528  * Access is done through binary search
529  */
530 static const struct iwl_hcmd_names iwl_mvm_data_path_names[] = {
531 	HCMD_NAME(DQA_ENABLE_CMD),
532 	HCMD_NAME(UPDATE_MU_GROUPS_CMD),
533 	HCMD_NAME(TRIGGER_RX_QUEUES_NOTIF_CMD),
534 	HCMD_NAME(STA_HE_CTXT_CMD),
535 	HCMD_NAME(RLC_CONFIG_CMD),
536 	HCMD_NAME(RFH_QUEUE_CONFIG_CMD),
537 	HCMD_NAME(TLC_MNG_CONFIG_CMD),
538 	HCMD_NAME(CHEST_COLLECTOR_FILTER_CONFIG_CMD),
539 	HCMD_NAME(MONITOR_NOTIF),
540 	HCMD_NAME(THERMAL_DUAL_CHAIN_REQUEST),
541 	HCMD_NAME(STA_PM_NOTIF),
542 	HCMD_NAME(MU_GROUP_MGMT_NOTIF),
543 	HCMD_NAME(RX_QUEUES_NOTIFICATION),
544 };
545 
546 /* Please keep this array *SORTED* by hex value.
547  * Access is done through binary search
548  */
549 static const struct iwl_hcmd_names iwl_mvm_location_names[] = {
550 	HCMD_NAME(TOF_RANGE_REQ_CMD),
551 	HCMD_NAME(TOF_CONFIG_CMD),
552 	HCMD_NAME(TOF_RANGE_ABORT_CMD),
553 	HCMD_NAME(TOF_RANGE_REQ_EXT_CMD),
554 	HCMD_NAME(TOF_RESPONDER_CONFIG_CMD),
555 	HCMD_NAME(TOF_RESPONDER_DYN_CONFIG_CMD),
556 	HCMD_NAME(TOF_LC_NOTIF),
557 	HCMD_NAME(TOF_RESPONDER_STATS),
558 	HCMD_NAME(TOF_MCSI_DEBUG_NOTIF),
559 	HCMD_NAME(TOF_RANGE_RESPONSE_NOTIF),
560 };
561 
562 /* Please keep this array *SORTED* by hex value.
563  * Access is done through binary search
564  */
565 static const struct iwl_hcmd_names iwl_mvm_prot_offload_names[] = {
566 	HCMD_NAME(STORED_BEACON_NTF),
567 };
568 
569 /* Please keep this array *SORTED* by hex value.
570  * Access is done through binary search
571  */
572 static const struct iwl_hcmd_names iwl_mvm_regulatory_and_nvm_names[] = {
573 	HCMD_NAME(NVM_ACCESS_COMPLETE),
574 	HCMD_NAME(NVM_GET_INFO),
575 	HCMD_NAME(TAS_CONFIG),
576 };
577 
578 static const struct iwl_hcmd_arr iwl_mvm_groups[] = {
579 	[LEGACY_GROUP] = HCMD_ARR(iwl_mvm_legacy_names),
580 	[LONG_GROUP] = HCMD_ARR(iwl_mvm_legacy_names),
581 	[SYSTEM_GROUP] = HCMD_ARR(iwl_mvm_system_names),
582 	[MAC_CONF_GROUP] = HCMD_ARR(iwl_mvm_mac_conf_names),
583 	[PHY_OPS_GROUP] = HCMD_ARR(iwl_mvm_phy_names),
584 	[DATA_PATH_GROUP] = HCMD_ARR(iwl_mvm_data_path_names),
585 	[LOCATION_GROUP] = HCMD_ARR(iwl_mvm_location_names),
586 	[PROT_OFFLOAD_GROUP] = HCMD_ARR(iwl_mvm_prot_offload_names),
587 	[REGULATORY_AND_NVM_GROUP] =
588 		HCMD_ARR(iwl_mvm_regulatory_and_nvm_names),
589 };
590 
591 /* this forward declaration can avoid to export the function */
592 static void iwl_mvm_async_handlers_wk(struct work_struct *wk);
593 
594 static u32 iwl_mvm_min_backoff(struct iwl_mvm *mvm)
595 {
596 	const struct iwl_pwr_tx_backoff *backoff = mvm->cfg->pwr_tx_backoffs;
597 	u64 dflt_pwr_limit;
598 
599 	if (!backoff)
600 		return 0;
601 
602 	dflt_pwr_limit = iwl_acpi_get_pwr_limit(mvm->dev);
603 
604 	while (backoff->pwr) {
605 		if (dflt_pwr_limit >= backoff->pwr)
606 			return backoff->backoff;
607 
608 		backoff++;
609 	}
610 
611 	return 0;
612 }
613 
614 static void iwl_mvm_tx_unblock_dwork(struct work_struct *work)
615 {
616 	struct iwl_mvm *mvm =
617 		container_of(work, struct iwl_mvm, cs_tx_unblock_dwork.work);
618 	struct ieee80211_vif *tx_blocked_vif;
619 	struct iwl_mvm_vif *mvmvif;
620 
621 	mutex_lock(&mvm->mutex);
622 
623 	tx_blocked_vif =
624 		rcu_dereference_protected(mvm->csa_tx_blocked_vif,
625 					  lockdep_is_held(&mvm->mutex));
626 
627 	if (!tx_blocked_vif)
628 		goto unlock;
629 
630 	mvmvif = iwl_mvm_vif_from_mac80211(tx_blocked_vif);
631 	iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
632 	RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
633 unlock:
634 	mutex_unlock(&mvm->mutex);
635 }
636 
637 static int iwl_mvm_fwrt_dump_start(void *ctx)
638 {
639 	struct iwl_mvm *mvm = ctx;
640 
641 	mutex_lock(&mvm->mutex);
642 
643 	return 0;
644 }
645 
646 static void iwl_mvm_fwrt_dump_end(void *ctx)
647 {
648 	struct iwl_mvm *mvm = ctx;
649 
650 	mutex_unlock(&mvm->mutex);
651 }
652 
653 static bool iwl_mvm_fwrt_fw_running(void *ctx)
654 {
655 	return iwl_mvm_firmware_running(ctx);
656 }
657 
658 static int iwl_mvm_fwrt_send_hcmd(void *ctx, struct iwl_host_cmd *host_cmd)
659 {
660 	struct iwl_mvm *mvm = (struct iwl_mvm *)ctx;
661 	int ret;
662 
663 	mutex_lock(&mvm->mutex);
664 	ret = iwl_mvm_send_cmd(mvm, host_cmd);
665 	mutex_unlock(&mvm->mutex);
666 
667 	return ret;
668 }
669 
670 static bool iwl_mvm_d3_debug_enable(void *ctx)
671 {
672 	return IWL_MVM_D3_DEBUG;
673 }
674 
675 static const struct iwl_fw_runtime_ops iwl_mvm_fwrt_ops = {
676 	.dump_start = iwl_mvm_fwrt_dump_start,
677 	.dump_end = iwl_mvm_fwrt_dump_end,
678 	.fw_running = iwl_mvm_fwrt_fw_running,
679 	.send_hcmd = iwl_mvm_fwrt_send_hcmd,
680 	.d3_debug_enable = iwl_mvm_d3_debug_enable,
681 };
682 
683 static int iwl_mvm_start_get_nvm(struct iwl_mvm *mvm)
684 {
685 	struct iwl_trans *trans = mvm->trans;
686 	int ret;
687 
688 	if (trans->csme_own) {
689 		if (WARN(!mvm->mei_registered,
690 			 "csme is owner, but we aren't registered to iwlmei\n"))
691 			goto get_nvm_from_fw;
692 
693 		mvm->mei_nvm_data = iwl_mei_get_nvm();
694 		if (mvm->mei_nvm_data) {
695 			/*
696 			 * mvm->mei_nvm_data is set and because of that,
697 			 * we'll load the NVM from the FW when we'll get
698 			 * ownership.
699 			 */
700 			mvm->nvm_data =
701 				iwl_parse_mei_nvm_data(trans, trans->cfg,
702 						       mvm->mei_nvm_data, mvm->fw);
703 			return 0;
704 		}
705 
706 		IWL_ERR(mvm,
707 			"Got a NULL NVM from CSME, trying to get it from the device\n");
708 	}
709 
710 get_nvm_from_fw:
711 	rtnl_lock();
712 	wiphy_lock(mvm->hw->wiphy);
713 	mutex_lock(&mvm->mutex);
714 
715 	ret = iwl_trans_start_hw(mvm->trans);
716 	if (ret) {
717 		mutex_unlock(&mvm->mutex);
718 		wiphy_unlock(mvm->hw->wiphy);
719 		rtnl_unlock();
720 		return ret;
721 	}
722 
723 	ret = iwl_run_init_mvm_ucode(mvm);
724 	if (ret && ret != -ERFKILL)
725 		iwl_fw_dbg_error_collect(&mvm->fwrt, FW_DBG_TRIGGER_DRIVER);
726 	if (!ret && iwl_mvm_is_lar_supported(mvm)) {
727 		mvm->hw->wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
728 		ret = iwl_mvm_init_mcc(mvm);
729 	}
730 
731 	if (!iwlmvm_mod_params.init_dbg || !ret)
732 		iwl_mvm_stop_device(mvm);
733 
734 	mutex_unlock(&mvm->mutex);
735 	wiphy_unlock(mvm->hw->wiphy);
736 	rtnl_unlock();
737 
738 	if (ret)
739 		IWL_ERR(mvm, "Failed to run INIT ucode: %d\n", ret);
740 
741 	return ret;
742 }
743 
744 static int iwl_mvm_start_post_nvm(struct iwl_mvm *mvm)
745 {
746 	struct iwl_mvm_csme_conn_info *csme_conn_info __maybe_unused;
747 	int ret;
748 
749 	iwl_mvm_toggle_tx_ant(mvm, &mvm->mgmt_last_antenna_idx);
750 
751 	ret = iwl_mvm_mac_setup_register(mvm);
752 	if (ret)
753 		return ret;
754 
755 	mvm->hw_registered = true;
756 
757 	iwl_mvm_dbgfs_register(mvm);
758 
759 	wiphy_rfkill_set_hw_state_reason(mvm->hw->wiphy,
760 					 mvm->mei_rfkill_blocked,
761 					 RFKILL_HARD_BLOCK_NOT_OWNER);
762 
763 	iwl_mvm_mei_set_sw_rfkill_state(mvm);
764 
765 	return 0;
766 }
767 
768 struct iwl_mvm_frob_txf_data {
769 	u8 *buf;
770 	size_t buflen;
771 };
772 
773 static void iwl_mvm_frob_txf_key_iter(struct ieee80211_hw *hw,
774 				      struct ieee80211_vif *vif,
775 				      struct ieee80211_sta *sta,
776 				      struct ieee80211_key_conf *key,
777 				      void *data)
778 {
779 	struct iwl_mvm_frob_txf_data *txf = data;
780 	u8 keylen, match, matchend;
781 	u8 *keydata;
782 	size_t i;
783 
784 	switch (key->cipher) {
785 	case WLAN_CIPHER_SUITE_CCMP:
786 		keydata = key->key;
787 		keylen = key->keylen;
788 		break;
789 	case WLAN_CIPHER_SUITE_WEP40:
790 	case WLAN_CIPHER_SUITE_WEP104:
791 	case WLAN_CIPHER_SUITE_TKIP:
792 		/*
793 		 * WEP has short keys which might show up in the payload,
794 		 * and then you can deduce the key, so in this case just
795 		 * remove all FIFO data.
796 		 * For TKIP, we don't know the phase 2 keys here, so same.
797 		 */
798 		memset(txf->buf, 0xBB, txf->buflen);
799 		return;
800 	default:
801 		return;
802 	}
803 
804 	/* scan for key material and clear it out */
805 	match = 0;
806 	for (i = 0; i < txf->buflen; i++) {
807 		if (txf->buf[i] != keydata[match]) {
808 			match = 0;
809 			continue;
810 		}
811 		match++;
812 		if (match == keylen) {
813 			memset(txf->buf + i - keylen, 0xAA, keylen);
814 			match = 0;
815 		}
816 	}
817 
818 	/* we're dealing with a FIFO, so check wrapped around data */
819 	matchend = match;
820 	for (i = 0; match && i < keylen - match; i++) {
821 		if (txf->buf[i] != keydata[match])
822 			break;
823 		match++;
824 		if (match == keylen) {
825 			memset(txf->buf, 0xAA, i + 1);
826 			memset(txf->buf + txf->buflen - matchend, 0xAA,
827 			       matchend);
828 			break;
829 		}
830 	}
831 }
832 
833 static void iwl_mvm_frob_txf(void *ctx, void *buf, size_t buflen)
834 {
835 	struct iwl_mvm_frob_txf_data txf = {
836 		.buf = buf,
837 		.buflen = buflen,
838 	};
839 	struct iwl_mvm *mvm = ctx;
840 
841 	/* embedded key material exists only on old API */
842 	if (iwl_mvm_has_new_tx_api(mvm))
843 		return;
844 
845 	rcu_read_lock();
846 	ieee80211_iter_keys_rcu(mvm->hw, NULL, iwl_mvm_frob_txf_key_iter, &txf);
847 	rcu_read_unlock();
848 }
849 
850 static void iwl_mvm_frob_hcmd(void *ctx, void *hcmd, size_t len)
851 {
852 	/* we only use wide headers for commands */
853 	struct iwl_cmd_header_wide *hdr = hcmd;
854 	unsigned int frob_start = sizeof(*hdr), frob_end = 0;
855 
856 	if (len < sizeof(hdr))
857 		return;
858 
859 	/* all the commands we care about are in LONG_GROUP */
860 	if (hdr->group_id != LONG_GROUP)
861 		return;
862 
863 	switch (hdr->cmd) {
864 	case WEP_KEY:
865 	case WOWLAN_TKIP_PARAM:
866 	case WOWLAN_KEK_KCK_MATERIAL:
867 	case ADD_STA_KEY:
868 		/*
869 		 * blank out everything here, easier than dealing
870 		 * with the various versions of the command
871 		 */
872 		frob_end = INT_MAX;
873 		break;
874 	case MGMT_MCAST_KEY:
875 		frob_start = offsetof(struct iwl_mvm_mgmt_mcast_key_cmd, igtk);
876 		BUILD_BUG_ON(offsetof(struct iwl_mvm_mgmt_mcast_key_cmd, igtk) !=
877 			     offsetof(struct iwl_mvm_mgmt_mcast_key_cmd_v1, igtk));
878 
879 		frob_end = offsetofend(struct iwl_mvm_mgmt_mcast_key_cmd, igtk);
880 		BUILD_BUG_ON(offsetof(struct iwl_mvm_mgmt_mcast_key_cmd, igtk) <
881 			     offsetof(struct iwl_mvm_mgmt_mcast_key_cmd_v1, igtk));
882 		break;
883 	}
884 
885 	if (frob_start >= frob_end)
886 		return;
887 
888 	if (frob_end > len)
889 		frob_end = len;
890 
891 	memset((u8 *)hcmd + frob_start, 0xAA, frob_end - frob_start);
892 }
893 
894 static void iwl_mvm_frob_mem(void *ctx, u32 mem_addr, void *mem, size_t buflen)
895 {
896 	const struct iwl_dump_exclude *excl;
897 	struct iwl_mvm *mvm = ctx;
898 	int i;
899 
900 	switch (mvm->fwrt.cur_fw_img) {
901 	case IWL_UCODE_INIT:
902 	default:
903 		/* not relevant */
904 		return;
905 	case IWL_UCODE_REGULAR:
906 	case IWL_UCODE_REGULAR_USNIFFER:
907 		excl = mvm->fw->dump_excl;
908 		break;
909 	case IWL_UCODE_WOWLAN:
910 		excl = mvm->fw->dump_excl_wowlan;
911 		break;
912 	}
913 
914 	BUILD_BUG_ON(sizeof(mvm->fw->dump_excl) !=
915 		     sizeof(mvm->fw->dump_excl_wowlan));
916 
917 	for (i = 0; i < ARRAY_SIZE(mvm->fw->dump_excl); i++) {
918 		u32 start, end;
919 
920 		if (!excl[i].addr || !excl[i].size)
921 			continue;
922 
923 		start = excl[i].addr;
924 		end = start + excl[i].size;
925 
926 		if (end <= mem_addr || start >= mem_addr + buflen)
927 			continue;
928 
929 		if (start < mem_addr)
930 			start = mem_addr;
931 
932 		if (end > mem_addr + buflen)
933 			end = mem_addr + buflen;
934 
935 		memset((u8 *)mem + start - mem_addr, 0xAA, end - start);
936 	}
937 }
938 
939 static const struct iwl_dump_sanitize_ops iwl_mvm_sanitize_ops = {
940 	.frob_txf = iwl_mvm_frob_txf,
941 	.frob_hcmd = iwl_mvm_frob_hcmd,
942 	.frob_mem = iwl_mvm_frob_mem,
943 };
944 
945 static void iwl_mvm_me_conn_status(void *priv, const struct iwl_mei_conn_info *conn_info)
946 {
947 	struct iwl_mvm *mvm = priv;
948 	struct iwl_mvm_csme_conn_info *prev_conn_info, *curr_conn_info;
949 
950 	/*
951 	 * This is protected by the guarantee that this function will not be
952 	 * called twice on two different threads
953 	 */
954 	prev_conn_info = rcu_dereference_protected(mvm->csme_conn_info, true);
955 
956 	curr_conn_info = kzalloc(sizeof(*curr_conn_info), GFP_KERNEL);
957 	if (!curr_conn_info)
958 		return;
959 
960 	curr_conn_info->conn_info = *conn_info;
961 
962 	rcu_assign_pointer(mvm->csme_conn_info, curr_conn_info);
963 
964 	if (prev_conn_info)
965 		kfree_rcu(prev_conn_info, rcu_head);
966 }
967 
968 static void iwl_mvm_mei_rfkill(void *priv, bool blocked)
969 {
970 	struct iwl_mvm *mvm = priv;
971 
972 	mvm->mei_rfkill_blocked = blocked;
973 	if (!mvm->hw_registered)
974 		return;
975 
976 	wiphy_rfkill_set_hw_state_reason(mvm->hw->wiphy,
977 					 mvm->mei_rfkill_blocked,
978 					 RFKILL_HARD_BLOCK_NOT_OWNER);
979 }
980 
981 static void iwl_mvm_mei_roaming_forbidden(void *priv, bool forbidden)
982 {
983 	struct iwl_mvm *mvm = priv;
984 
985 	if (!mvm->hw_registered || !mvm->csme_vif)
986 		return;
987 
988 	iwl_mvm_send_roaming_forbidden_event(mvm, mvm->csme_vif, forbidden);
989 }
990 
991 static void iwl_mvm_sap_connected_wk(struct work_struct *wk)
992 {
993 	struct iwl_mvm *mvm =
994 		container_of(wk, struct iwl_mvm, sap_connected_wk);
995 	int ret;
996 
997 	ret = iwl_mvm_start_get_nvm(mvm);
998 	if (ret)
999 		goto out_free;
1000 
1001 	ret = iwl_mvm_start_post_nvm(mvm);
1002 	if (ret)
1003 		goto out_free;
1004 
1005 	return;
1006 
1007 out_free:
1008 	IWL_ERR(mvm, "Couldn't get started...\n");
1009 	iwl_mei_start_unregister();
1010 	iwl_mei_unregister_complete();
1011 	iwl_fw_flush_dumps(&mvm->fwrt);
1012 	iwl_mvm_thermal_exit(mvm);
1013 	iwl_fw_runtime_free(&mvm->fwrt);
1014 	iwl_phy_db_free(mvm->phy_db);
1015 	kfree(mvm->scan_cmd);
1016 	iwl_trans_op_mode_leave(mvm->trans);
1017 	kfree(mvm->nvm_data);
1018 	kfree(mvm->mei_nvm_data);
1019 
1020 	ieee80211_free_hw(mvm->hw);
1021 }
1022 
1023 static void iwl_mvm_mei_sap_connected(void *priv)
1024 {
1025 	struct iwl_mvm *mvm = priv;
1026 
1027 	if (!mvm->hw_registered)
1028 		schedule_work(&mvm->sap_connected_wk);
1029 }
1030 
1031 static void iwl_mvm_mei_nic_stolen(void *priv)
1032 {
1033 	struct iwl_mvm *mvm = priv;
1034 
1035 	rtnl_lock();
1036 	cfg80211_shutdown_all_interfaces(mvm->hw->wiphy);
1037 	rtnl_unlock();
1038 }
1039 
1040 static const struct iwl_mei_ops mei_ops = {
1041 	.me_conn_status = iwl_mvm_me_conn_status,
1042 	.rfkill = iwl_mvm_mei_rfkill,
1043 	.roaming_forbidden = iwl_mvm_mei_roaming_forbidden,
1044 	.sap_connected = iwl_mvm_mei_sap_connected,
1045 	.nic_stolen = iwl_mvm_mei_nic_stolen,
1046 };
1047 
1048 static struct iwl_op_mode *
1049 iwl_op_mode_mvm_start(struct iwl_trans *trans, const struct iwl_cfg *cfg,
1050 		      const struct iwl_fw *fw, struct dentry *dbgfs_dir)
1051 {
1052 	struct ieee80211_hw *hw;
1053 	struct iwl_op_mode *op_mode;
1054 	struct iwl_mvm *mvm;
1055 	struct iwl_trans_config trans_cfg = {};
1056 	static const u8 no_reclaim_cmds[] = {
1057 		TX_CMD,
1058 	};
1059 	int scan_size;
1060 	u32 min_backoff;
1061 	struct iwl_mvm_csme_conn_info *csme_conn_info __maybe_unused;
1062 
1063 	/*
1064 	 * We use IWL_MVM_STATION_COUNT_MAX to check the validity of the station
1065 	 * index all over the driver - check that its value corresponds to the
1066 	 * array size.
1067 	 */
1068 	BUILD_BUG_ON(ARRAY_SIZE(mvm->fw_id_to_mac_id) !=
1069 		     IWL_MVM_STATION_COUNT_MAX);
1070 
1071 	/********************************
1072 	 * 1. Allocating and configuring HW data
1073 	 ********************************/
1074 	hw = ieee80211_alloc_hw(sizeof(struct iwl_op_mode) +
1075 				sizeof(struct iwl_mvm),
1076 				&iwl_mvm_hw_ops);
1077 	if (!hw)
1078 		return NULL;
1079 
1080 	hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF;
1081 
1082 	if (cfg->max_tx_agg_size)
1083 		hw->max_tx_aggregation_subframes = cfg->max_tx_agg_size;
1084 	else
1085 		hw->max_tx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF;
1086 
1087 	op_mode = hw->priv;
1088 
1089 	mvm = IWL_OP_MODE_GET_MVM(op_mode);
1090 	mvm->dev = trans->dev;
1091 	mvm->trans = trans;
1092 	mvm->cfg = cfg;
1093 	mvm->fw = fw;
1094 	mvm->hw = hw;
1095 
1096 	iwl_fw_runtime_init(&mvm->fwrt, trans, fw, &iwl_mvm_fwrt_ops, mvm,
1097 			    &iwl_mvm_sanitize_ops, mvm, dbgfs_dir);
1098 
1099 	iwl_mvm_get_acpi_tables(mvm);
1100 	iwl_uefi_get_sgom_table(trans, &mvm->fwrt);
1101 
1102 	mvm->init_status = 0;
1103 
1104 	if (iwl_mvm_has_new_rx_api(mvm)) {
1105 		op_mode->ops = &iwl_mvm_ops_mq;
1106 		trans->rx_mpdu_cmd_hdr_size =
1107 			(trans->trans_cfg->device_family >=
1108 			 IWL_DEVICE_FAMILY_AX210) ?
1109 			sizeof(struct iwl_rx_mpdu_desc) :
1110 			IWL_RX_DESC_SIZE_V1;
1111 	} else {
1112 		op_mode->ops = &iwl_mvm_ops;
1113 		trans->rx_mpdu_cmd_hdr_size =
1114 			sizeof(struct iwl_rx_mpdu_res_start);
1115 
1116 		if (WARN_ON(trans->num_rx_queues > 1))
1117 			goto out_free;
1118 	}
1119 
1120 	mvm->fw_restart = iwlwifi_mod_params.fw_restart ? -1 : 0;
1121 
1122 	if (iwl_mvm_has_new_tx_api(mvm)) {
1123 		/*
1124 		 * If we have the new TX/queue allocation API initialize them
1125 		 * all to invalid numbers. We'll rewrite the ones that we need
1126 		 * later, but that doesn't happen for all of them all of the
1127 		 * time (e.g. P2P Device is optional), and if a dynamic queue
1128 		 * ends up getting number 2 (IWL_MVM_DQA_P2P_DEVICE_QUEUE) then
1129 		 * iwl_mvm_is_static_queue() erroneously returns true, and we
1130 		 * might have things getting stuck.
1131 		 */
1132 		mvm->aux_queue = IWL_MVM_INVALID_QUEUE;
1133 		mvm->snif_queue = IWL_MVM_INVALID_QUEUE;
1134 		mvm->probe_queue = IWL_MVM_INVALID_QUEUE;
1135 		mvm->p2p_dev_queue = IWL_MVM_INVALID_QUEUE;
1136 	} else {
1137 		mvm->aux_queue = IWL_MVM_DQA_AUX_QUEUE;
1138 		mvm->snif_queue = IWL_MVM_DQA_INJECT_MONITOR_QUEUE;
1139 		mvm->probe_queue = IWL_MVM_DQA_AP_PROBE_RESP_QUEUE;
1140 		mvm->p2p_dev_queue = IWL_MVM_DQA_P2P_DEVICE_QUEUE;
1141 	}
1142 
1143 	mvm->sf_state = SF_UNINIT;
1144 	if (iwl_mvm_has_unified_ucode(mvm))
1145 		iwl_fw_set_current_image(&mvm->fwrt, IWL_UCODE_REGULAR);
1146 	else
1147 		iwl_fw_set_current_image(&mvm->fwrt, IWL_UCODE_INIT);
1148 	mvm->drop_bcn_ap_mode = true;
1149 
1150 	mutex_init(&mvm->mutex);
1151 	spin_lock_init(&mvm->async_handlers_lock);
1152 	INIT_LIST_HEAD(&mvm->time_event_list);
1153 	INIT_LIST_HEAD(&mvm->aux_roc_te_list);
1154 	INIT_LIST_HEAD(&mvm->async_handlers_list);
1155 	spin_lock_init(&mvm->time_event_lock);
1156 	INIT_LIST_HEAD(&mvm->ftm_initiator.loc_list);
1157 	INIT_LIST_HEAD(&mvm->ftm_initiator.pasn_list);
1158 	INIT_LIST_HEAD(&mvm->resp_pasn_list);
1159 
1160 	INIT_WORK(&mvm->async_handlers_wk, iwl_mvm_async_handlers_wk);
1161 	INIT_WORK(&mvm->roc_done_wk, iwl_mvm_roc_done_wk);
1162 	INIT_WORK(&mvm->sap_connected_wk, iwl_mvm_sap_connected_wk);
1163 	INIT_DELAYED_WORK(&mvm->tdls_cs.dwork, iwl_mvm_tdls_ch_switch_work);
1164 	INIT_DELAYED_WORK(&mvm->scan_timeout_dwork, iwl_mvm_scan_timeout_wk);
1165 	INIT_WORK(&mvm->add_stream_wk, iwl_mvm_add_new_dqa_stream_wk);
1166 	INIT_LIST_HEAD(&mvm->add_stream_txqs);
1167 
1168 	init_waitqueue_head(&mvm->rx_sync_waitq);
1169 
1170 	mvm->queue_sync_state = 0;
1171 
1172 	SET_IEEE80211_DEV(mvm->hw, mvm->trans->dev);
1173 
1174 	spin_lock_init(&mvm->tcm.lock);
1175 	INIT_DELAYED_WORK(&mvm->tcm.work, iwl_mvm_tcm_work);
1176 	mvm->tcm.ts = jiffies;
1177 	mvm->tcm.ll_ts = jiffies;
1178 	mvm->tcm.uapsd_nonagg_ts = jiffies;
1179 
1180 	INIT_DELAYED_WORK(&mvm->cs_tx_unblock_dwork, iwl_mvm_tx_unblock_dwork);
1181 
1182 	mvm->cmd_ver.d0i3_resp =
1183 		iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP, D0I3_END_CMD,
1184 					0);
1185 	/* we only support version 1 */
1186 	if (WARN_ON_ONCE(mvm->cmd_ver.d0i3_resp > 1))
1187 		goto out_free;
1188 
1189 	mvm->cmd_ver.range_resp =
1190 		iwl_fw_lookup_notif_ver(mvm->fw, LOCATION_GROUP,
1191 					TOF_RANGE_RESPONSE_NOTIF, 5);
1192 	/* we only support up to version 9 */
1193 	if (WARN_ON_ONCE(mvm->cmd_ver.range_resp > 9))
1194 		goto out_free;
1195 
1196 	/*
1197 	 * Populate the state variables that the transport layer needs
1198 	 * to know about.
1199 	 */
1200 	trans_cfg.op_mode = op_mode;
1201 	trans_cfg.no_reclaim_cmds = no_reclaim_cmds;
1202 	trans_cfg.n_no_reclaim_cmds = ARRAY_SIZE(no_reclaim_cmds);
1203 
1204 	switch (iwlwifi_mod_params.amsdu_size) {
1205 	case IWL_AMSDU_DEF:
1206 		trans_cfg.rx_buf_size = IWL_AMSDU_4K;
1207 		break;
1208 	case IWL_AMSDU_4K:
1209 		trans_cfg.rx_buf_size = IWL_AMSDU_4K;
1210 		break;
1211 	case IWL_AMSDU_8K:
1212 		trans_cfg.rx_buf_size = IWL_AMSDU_8K;
1213 		break;
1214 	case IWL_AMSDU_12K:
1215 		trans_cfg.rx_buf_size = IWL_AMSDU_12K;
1216 		break;
1217 	default:
1218 		pr_err("%s: Unsupported amsdu_size: %d\n", KBUILD_MODNAME,
1219 		       iwlwifi_mod_params.amsdu_size);
1220 		trans_cfg.rx_buf_size = IWL_AMSDU_4K;
1221 	}
1222 
1223 	trans->wide_cmd_header = true;
1224 	trans_cfg.bc_table_dword =
1225 		mvm->trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_AX210;
1226 
1227 	trans_cfg.command_groups = iwl_mvm_groups;
1228 	trans_cfg.command_groups_size = ARRAY_SIZE(iwl_mvm_groups);
1229 
1230 	trans_cfg.cmd_queue = IWL_MVM_DQA_CMD_QUEUE;
1231 	trans_cfg.cmd_fifo = IWL_MVM_TX_FIFO_CMD;
1232 	trans_cfg.scd_set_active = true;
1233 
1234 	trans_cfg.cb_data_offs = offsetof(struct ieee80211_tx_info,
1235 					  driver_data[2]);
1236 
1237 	/* Set a short watchdog for the command queue */
1238 	trans_cfg.cmd_q_wdg_timeout =
1239 		iwl_mvm_get_wd_timeout(mvm, NULL, false, true);
1240 
1241 	snprintf(mvm->hw->wiphy->fw_version,
1242 		 sizeof(mvm->hw->wiphy->fw_version),
1243 		 "%s", fw->fw_version);
1244 
1245 	trans_cfg.fw_reset_handshake = fw_has_capa(&mvm->fw->ucode_capa,
1246 						   IWL_UCODE_TLV_CAPA_FW_RESET_HANDSHAKE);
1247 
1248 	/* Configure transport layer */
1249 	iwl_trans_configure(mvm->trans, &trans_cfg);
1250 
1251 	trans->rx_mpdu_cmd = REPLY_RX_MPDU_CMD;
1252 	trans->dbg.dest_tlv = mvm->fw->dbg.dest_tlv;
1253 	trans->dbg.n_dest_reg = mvm->fw->dbg.n_dest_reg;
1254 	memcpy(trans->dbg.conf_tlv, mvm->fw->dbg.conf_tlv,
1255 	       sizeof(trans->dbg.conf_tlv));
1256 	trans->dbg.trigger_tlv = mvm->fw->dbg.trigger_tlv;
1257 
1258 	trans->iml = mvm->fw->iml;
1259 	trans->iml_len = mvm->fw->iml_len;
1260 
1261 	/* set up notification wait support */
1262 	iwl_notification_wait_init(&mvm->notif_wait);
1263 
1264 	/* Init phy db */
1265 	mvm->phy_db = iwl_phy_db_init(trans);
1266 	if (!mvm->phy_db) {
1267 		IWL_ERR(mvm, "Cannot init phy_db\n");
1268 		goto out_free;
1269 	}
1270 
1271 	IWL_INFO(mvm, "Detected %s, REV=0x%X\n",
1272 		 mvm->trans->name, mvm->trans->hw_rev);
1273 
1274 	if (iwlwifi_mod_params.nvm_file)
1275 		mvm->nvm_file_name = iwlwifi_mod_params.nvm_file;
1276 	else
1277 		IWL_DEBUG_EEPROM(mvm->trans->dev,
1278 				 "working without external nvm file\n");
1279 
1280 	scan_size = iwl_mvm_scan_size(mvm);
1281 
1282 	mvm->scan_cmd = kmalloc(scan_size, GFP_KERNEL);
1283 	if (!mvm->scan_cmd)
1284 		goto out_free;
1285 
1286 	/* invalidate ids to prevent accidental removal of sta_id 0 */
1287 	mvm->aux_sta.sta_id = IWL_MVM_INVALID_STA;
1288 	mvm->snif_sta.sta_id = IWL_MVM_INVALID_STA;
1289 
1290 	/* Set EBS as successful as long as not stated otherwise by the FW. */
1291 	mvm->last_ebs_successful = true;
1292 
1293 	min_backoff = iwl_mvm_min_backoff(mvm);
1294 	iwl_mvm_thermal_initialize(mvm, min_backoff);
1295 
1296 	if (!iwl_mvm_has_new_rx_stats_api(mvm))
1297 		memset(&mvm->rx_stats_v3, 0,
1298 		       sizeof(struct mvm_statistics_rx_v3));
1299 	else
1300 		memset(&mvm->rx_stats, 0, sizeof(struct mvm_statistics_rx));
1301 
1302 	mvm->debugfs_dir = dbgfs_dir;
1303 
1304 	mvm->mei_registered = !iwl_mei_register(mvm, &mei_ops);
1305 
1306 	if (iwl_mvm_start_get_nvm(mvm)) {
1307 		/*
1308 		 * Getting NVM failed while CSME is the owner, but we are
1309 		 * registered to MEI, we'll get the NVM later when it'll be
1310 		 * possible to get it from CSME.
1311 		 */
1312 		if (trans->csme_own && mvm->mei_registered)
1313 			return op_mode;
1314 
1315 		goto out_thermal_exit;
1316 	}
1317 
1318 
1319 	if (iwl_mvm_start_post_nvm(mvm))
1320 		goto out_thermal_exit;
1321 
1322 	return op_mode;
1323 
1324  out_thermal_exit:
1325 	iwl_mvm_thermal_exit(mvm);
1326 	if (mvm->mei_registered) {
1327 		iwl_mei_start_unregister();
1328 		iwl_mei_unregister_complete();
1329 	}
1330  out_free:
1331 	iwl_fw_flush_dumps(&mvm->fwrt);
1332 	iwl_fw_runtime_free(&mvm->fwrt);
1333 
1334 	if (iwlmvm_mod_params.init_dbg)
1335 		return op_mode;
1336 	iwl_phy_db_free(mvm->phy_db);
1337 	kfree(mvm->scan_cmd);
1338 	iwl_trans_op_mode_leave(trans);
1339 
1340 	ieee80211_free_hw(mvm->hw);
1341 	return NULL;
1342 }
1343 
1344 void iwl_mvm_stop_device(struct iwl_mvm *mvm)
1345 {
1346 	lockdep_assert_held(&mvm->mutex);
1347 
1348 	iwl_fw_cancel_timestamp(&mvm->fwrt);
1349 
1350 	clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
1351 
1352 	iwl_fw_dbg_stop_sync(&mvm->fwrt);
1353 	iwl_trans_stop_device(mvm->trans);
1354 	iwl_free_fw_paging(&mvm->fwrt);
1355 	iwl_fw_dump_conf_clear(&mvm->fwrt);
1356 	iwl_mvm_mei_device_down(mvm);
1357 }
1358 
1359 static void iwl_op_mode_mvm_stop(struct iwl_op_mode *op_mode)
1360 {
1361 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1362 	int i;
1363 
1364 	if (mvm->mei_registered) {
1365 		rtnl_lock();
1366 		iwl_mei_set_netdev(NULL);
1367 		rtnl_unlock();
1368 		iwl_mei_start_unregister();
1369 	}
1370 
1371 	/*
1372 	 * After we unregister from mei, the worker can't be scheduled
1373 	 * anymore.
1374 	 */
1375 	cancel_work_sync(&mvm->sap_connected_wk);
1376 
1377 	iwl_mvm_leds_exit(mvm);
1378 
1379 	iwl_mvm_thermal_exit(mvm);
1380 
1381 	/*
1382 	 * If we couldn't get ownership on the device and we couldn't
1383 	 * get the NVM from CSME, we haven't registered to mac80211.
1384 	 * In that case, we didn't fail op_mode_start, because we are
1385 	 * waiting for CSME to allow us to get the NVM to register to
1386 	 * mac80211. If that didn't happen, we haven't registered to
1387 	 * mac80211, hence the if below.
1388 	 */
1389 	if (mvm->hw_registered)
1390 		ieee80211_unregister_hw(mvm->hw);
1391 
1392 	kfree(mvm->scan_cmd);
1393 	kfree(mvm->mcast_filter_cmd);
1394 	mvm->mcast_filter_cmd = NULL;
1395 
1396 	kfree(mvm->error_recovery_buf);
1397 	mvm->error_recovery_buf = NULL;
1398 
1399 	iwl_trans_op_mode_leave(mvm->trans);
1400 
1401 	iwl_phy_db_free(mvm->phy_db);
1402 	mvm->phy_db = NULL;
1403 
1404 	kfree(mvm->nvm_data);
1405 	kfree(mvm->mei_nvm_data);
1406 	kfree(rcu_access_pointer(mvm->csme_conn_info));
1407 	kfree(mvm->temp_nvm_data);
1408 	for (i = 0; i < NVM_MAX_NUM_SECTIONS; i++)
1409 		kfree(mvm->nvm_sections[i].data);
1410 
1411 	cancel_delayed_work_sync(&mvm->tcm.work);
1412 
1413 	iwl_fw_runtime_free(&mvm->fwrt);
1414 	mutex_destroy(&mvm->mutex);
1415 
1416 	if (mvm->mei_registered)
1417 		iwl_mei_unregister_complete();
1418 
1419 	ieee80211_free_hw(mvm->hw);
1420 }
1421 
1422 struct iwl_async_handler_entry {
1423 	struct list_head list;
1424 	struct iwl_rx_cmd_buffer rxb;
1425 	enum iwl_rx_handler_context context;
1426 	void (*fn)(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb);
1427 };
1428 
1429 void iwl_mvm_async_handlers_purge(struct iwl_mvm *mvm)
1430 {
1431 	struct iwl_async_handler_entry *entry, *tmp;
1432 
1433 	spin_lock_bh(&mvm->async_handlers_lock);
1434 	list_for_each_entry_safe(entry, tmp, &mvm->async_handlers_list, list) {
1435 		iwl_free_rxb(&entry->rxb);
1436 		list_del(&entry->list);
1437 		kfree(entry);
1438 	}
1439 	spin_unlock_bh(&mvm->async_handlers_lock);
1440 }
1441 
1442 static void iwl_mvm_async_handlers_wk(struct work_struct *wk)
1443 {
1444 	struct iwl_mvm *mvm =
1445 		container_of(wk, struct iwl_mvm, async_handlers_wk);
1446 	struct iwl_async_handler_entry *entry, *tmp;
1447 	LIST_HEAD(local_list);
1448 
1449 	/* Ensure that we are not in stop flow (check iwl_mvm_mac_stop) */
1450 
1451 	/*
1452 	 * Sync with Rx path with a lock. Remove all the entries from this list,
1453 	 * add them to a local one (lock free), and then handle them.
1454 	 */
1455 	spin_lock_bh(&mvm->async_handlers_lock);
1456 	list_splice_init(&mvm->async_handlers_list, &local_list);
1457 	spin_unlock_bh(&mvm->async_handlers_lock);
1458 
1459 	list_for_each_entry_safe(entry, tmp, &local_list, list) {
1460 		if (entry->context == RX_HANDLER_ASYNC_LOCKED)
1461 			mutex_lock(&mvm->mutex);
1462 		entry->fn(mvm, &entry->rxb);
1463 		iwl_free_rxb(&entry->rxb);
1464 		list_del(&entry->list);
1465 		if (entry->context == RX_HANDLER_ASYNC_LOCKED)
1466 			mutex_unlock(&mvm->mutex);
1467 		kfree(entry);
1468 	}
1469 }
1470 
1471 static inline void iwl_mvm_rx_check_trigger(struct iwl_mvm *mvm,
1472 					    struct iwl_rx_packet *pkt)
1473 {
1474 	struct iwl_fw_dbg_trigger_tlv *trig;
1475 	struct iwl_fw_dbg_trigger_cmd *cmds_trig;
1476 	int i;
1477 
1478 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, NULL,
1479 				     FW_DBG_TRIGGER_FW_NOTIF);
1480 	if (!trig)
1481 		return;
1482 
1483 	cmds_trig = (void *)trig->data;
1484 
1485 	for (i = 0; i < ARRAY_SIZE(cmds_trig->cmds); i++) {
1486 		/* don't collect on CMD 0 */
1487 		if (!cmds_trig->cmds[i].cmd_id)
1488 			break;
1489 
1490 		if (cmds_trig->cmds[i].cmd_id != pkt->hdr.cmd ||
1491 		    cmds_trig->cmds[i].group_id != pkt->hdr.group_id)
1492 			continue;
1493 
1494 		iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
1495 					"CMD 0x%02x.%02x received",
1496 					pkt->hdr.group_id, pkt->hdr.cmd);
1497 		break;
1498 	}
1499 }
1500 
1501 static void iwl_mvm_rx_common(struct iwl_mvm *mvm,
1502 			      struct iwl_rx_cmd_buffer *rxb,
1503 			      struct iwl_rx_packet *pkt)
1504 {
1505 	unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
1506 	int i;
1507 	union iwl_dbg_tlv_tp_data tp_data = { .fw_pkt = pkt };
1508 
1509 	iwl_dbg_tlv_time_point(&mvm->fwrt,
1510 			       IWL_FW_INI_TIME_POINT_FW_RSP_OR_NOTIF, &tp_data);
1511 	iwl_mvm_rx_check_trigger(mvm, pkt);
1512 
1513 	/*
1514 	 * Do the notification wait before RX handlers so
1515 	 * even if the RX handler consumes the RXB we have
1516 	 * access to it in the notification wait entry.
1517 	 */
1518 	iwl_notification_wait_notify(&mvm->notif_wait, pkt);
1519 
1520 	for (i = 0; i < ARRAY_SIZE(iwl_mvm_rx_handlers); i++) {
1521 		const struct iwl_rx_handlers *rx_h = &iwl_mvm_rx_handlers[i];
1522 		struct iwl_async_handler_entry *entry;
1523 
1524 		if (rx_h->cmd_id != WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd))
1525 			continue;
1526 
1527 		if (unlikely(pkt_len < rx_h->min_size))
1528 			return;
1529 
1530 		if (rx_h->context == RX_HANDLER_SYNC) {
1531 			rx_h->fn(mvm, rxb);
1532 			return;
1533 		}
1534 
1535 		entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
1536 		/* we can't do much... */
1537 		if (!entry)
1538 			return;
1539 
1540 		entry->rxb._page = rxb_steal_page(rxb);
1541 		entry->rxb._offset = rxb->_offset;
1542 		entry->rxb._rx_page_order = rxb->_rx_page_order;
1543 		entry->fn = rx_h->fn;
1544 		entry->context = rx_h->context;
1545 		spin_lock(&mvm->async_handlers_lock);
1546 		list_add_tail(&entry->list, &mvm->async_handlers_list);
1547 		spin_unlock(&mvm->async_handlers_lock);
1548 		schedule_work(&mvm->async_handlers_wk);
1549 		break;
1550 	}
1551 }
1552 
1553 static void iwl_mvm_rx(struct iwl_op_mode *op_mode,
1554 		       struct napi_struct *napi,
1555 		       struct iwl_rx_cmd_buffer *rxb)
1556 {
1557 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1558 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1559 	u16 cmd = WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd);
1560 
1561 	if (likely(cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_MPDU_CMD)))
1562 		iwl_mvm_rx_rx_mpdu(mvm, napi, rxb);
1563 	else if (cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_PHY_CMD))
1564 		iwl_mvm_rx_rx_phy_cmd(mvm, rxb);
1565 	else
1566 		iwl_mvm_rx_common(mvm, rxb, pkt);
1567 }
1568 
1569 void iwl_mvm_rx_mq(struct iwl_op_mode *op_mode,
1570 		   struct napi_struct *napi,
1571 		   struct iwl_rx_cmd_buffer *rxb)
1572 {
1573 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1574 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1575 	u16 cmd = WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd);
1576 
1577 	if (likely(cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_MPDU_CMD)))
1578 		iwl_mvm_rx_mpdu_mq(mvm, napi, rxb, 0);
1579 	else if (unlikely(cmd == WIDE_ID(DATA_PATH_GROUP,
1580 					 RX_QUEUES_NOTIFICATION)))
1581 		iwl_mvm_rx_queue_notif(mvm, napi, rxb, 0);
1582 	else if (cmd == WIDE_ID(LEGACY_GROUP, FRAME_RELEASE))
1583 		iwl_mvm_rx_frame_release(mvm, napi, rxb, 0);
1584 	else if (cmd == WIDE_ID(LEGACY_GROUP, BAR_FRAME_RELEASE))
1585 		iwl_mvm_rx_bar_frame_release(mvm, napi, rxb, 0);
1586 	else if (cmd == WIDE_ID(DATA_PATH_GROUP, RX_NO_DATA_NOTIF))
1587 		iwl_mvm_rx_monitor_no_data(mvm, napi, rxb, 0);
1588 	else
1589 		iwl_mvm_rx_common(mvm, rxb, pkt);
1590 }
1591 
1592 static void iwl_mvm_async_cb(struct iwl_op_mode *op_mode,
1593 			     const struct iwl_device_cmd *cmd)
1594 {
1595 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1596 
1597 	/*
1598 	 * For now, we only set the CMD_WANT_ASYNC_CALLBACK for ADD_STA
1599 	 * commands that need to block the Tx queues.
1600 	 */
1601 	iwl_trans_block_txq_ptrs(mvm->trans, false);
1602 }
1603 
1604 static int iwl_mvm_is_static_queue(struct iwl_mvm *mvm, int queue)
1605 {
1606 	return queue == mvm->aux_queue || queue == mvm->probe_queue ||
1607 		queue == mvm->p2p_dev_queue || queue == mvm->snif_queue;
1608 }
1609 
1610 static void iwl_mvm_queue_state_change(struct iwl_op_mode *op_mode,
1611 				       int hw_queue, bool start)
1612 {
1613 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1614 	struct ieee80211_sta *sta;
1615 	struct ieee80211_txq *txq;
1616 	struct iwl_mvm_txq *mvmtxq;
1617 	int i;
1618 	unsigned long tid_bitmap;
1619 	struct iwl_mvm_sta *mvmsta;
1620 	u8 sta_id;
1621 
1622 	sta_id = iwl_mvm_has_new_tx_api(mvm) ?
1623 		mvm->tvqm_info[hw_queue].sta_id :
1624 		mvm->queue_info[hw_queue].ra_sta_id;
1625 
1626 	if (WARN_ON_ONCE(sta_id >= mvm->fw->ucode_capa.num_stations))
1627 		return;
1628 
1629 	rcu_read_lock();
1630 
1631 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1632 	if (IS_ERR_OR_NULL(sta))
1633 		goto out;
1634 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
1635 
1636 	if (iwl_mvm_is_static_queue(mvm, hw_queue)) {
1637 		if (!start)
1638 			ieee80211_stop_queues(mvm->hw);
1639 		else if (mvmsta->sta_state != IEEE80211_STA_NOTEXIST)
1640 			ieee80211_wake_queues(mvm->hw);
1641 
1642 		goto out;
1643 	}
1644 
1645 	if (iwl_mvm_has_new_tx_api(mvm)) {
1646 		int tid = mvm->tvqm_info[hw_queue].txq_tid;
1647 
1648 		tid_bitmap = BIT(tid);
1649 	} else {
1650 		tid_bitmap = mvm->queue_info[hw_queue].tid_bitmap;
1651 	}
1652 
1653 	for_each_set_bit(i, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1654 		int tid = i;
1655 
1656 		if (tid == IWL_MAX_TID_COUNT)
1657 			tid = IEEE80211_NUM_TIDS;
1658 
1659 		txq = sta->txq[tid];
1660 		mvmtxq = iwl_mvm_txq_from_mac80211(txq);
1661 		mvmtxq->stopped = !start;
1662 
1663 		if (start && mvmsta->sta_state != IEEE80211_STA_NOTEXIST)
1664 			iwl_mvm_mac_itxq_xmit(mvm->hw, txq);
1665 	}
1666 
1667 out:
1668 	rcu_read_unlock();
1669 }
1670 
1671 static void iwl_mvm_stop_sw_queue(struct iwl_op_mode *op_mode, int hw_queue)
1672 {
1673 	iwl_mvm_queue_state_change(op_mode, hw_queue, false);
1674 }
1675 
1676 static void iwl_mvm_wake_sw_queue(struct iwl_op_mode *op_mode, int hw_queue)
1677 {
1678 	iwl_mvm_queue_state_change(op_mode, hw_queue, true);
1679 }
1680 
1681 static void iwl_mvm_set_rfkill_state(struct iwl_mvm *mvm)
1682 {
1683 	bool state = iwl_mvm_is_radio_killed(mvm);
1684 
1685 	if (state)
1686 		wake_up(&mvm->rx_sync_waitq);
1687 
1688 	wiphy_rfkill_set_hw_state(mvm->hw->wiphy, state);
1689 }
1690 
1691 void iwl_mvm_set_hw_ctkill_state(struct iwl_mvm *mvm, bool state)
1692 {
1693 	if (state)
1694 		set_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status);
1695 	else
1696 		clear_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status);
1697 
1698 	iwl_mvm_set_rfkill_state(mvm);
1699 }
1700 
1701 struct iwl_mvm_csme_conn_info *iwl_mvm_get_csme_conn_info(struct iwl_mvm *mvm)
1702 {
1703 	return rcu_dereference_protected(mvm->csme_conn_info,
1704 					 lockdep_is_held(&mvm->mutex));
1705 }
1706 
1707 static bool iwl_mvm_set_hw_rfkill_state(struct iwl_op_mode *op_mode, bool state)
1708 {
1709 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1710 	bool rfkill_safe_init_done = READ_ONCE(mvm->rfkill_safe_init_done);
1711 	bool unified = iwl_mvm_has_unified_ucode(mvm);
1712 
1713 	if (state)
1714 		set_bit(IWL_MVM_STATUS_HW_RFKILL, &mvm->status);
1715 	else
1716 		clear_bit(IWL_MVM_STATUS_HW_RFKILL, &mvm->status);
1717 
1718 	iwl_mvm_set_rfkill_state(mvm);
1719 
1720 	 /* iwl_run_init_mvm_ucode is waiting for results, abort it. */
1721 	if (rfkill_safe_init_done)
1722 		iwl_abort_notification_waits(&mvm->notif_wait);
1723 
1724 	/*
1725 	 * Don't ask the transport to stop the firmware. We'll do it
1726 	 * after cfg80211 takes us down.
1727 	 */
1728 	if (unified)
1729 		return false;
1730 
1731 	/*
1732 	 * Stop the device if we run OPERATIONAL firmware or if we are in the
1733 	 * middle of the calibrations.
1734 	 */
1735 	return state && rfkill_safe_init_done;
1736 }
1737 
1738 static void iwl_mvm_free_skb(struct iwl_op_mode *op_mode, struct sk_buff *skb)
1739 {
1740 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1741 	struct ieee80211_tx_info *info;
1742 
1743 	info = IEEE80211_SKB_CB(skb);
1744 	iwl_trans_free_tx_cmd(mvm->trans, info->driver_data[1]);
1745 	ieee80211_free_txskb(mvm->hw, skb);
1746 }
1747 
1748 struct iwl_mvm_reprobe {
1749 	struct device *dev;
1750 	struct work_struct work;
1751 };
1752 
1753 static void iwl_mvm_reprobe_wk(struct work_struct *wk)
1754 {
1755 	struct iwl_mvm_reprobe *reprobe;
1756 
1757 	reprobe = container_of(wk, struct iwl_mvm_reprobe, work);
1758 	if (device_reprobe(reprobe->dev))
1759 		dev_err(reprobe->dev, "reprobe failed!\n");
1760 	put_device(reprobe->dev);
1761 	kfree(reprobe);
1762 	module_put(THIS_MODULE);
1763 }
1764 
1765 void iwl_mvm_nic_restart(struct iwl_mvm *mvm, bool fw_error)
1766 {
1767 	iwl_abort_notification_waits(&mvm->notif_wait);
1768 	iwl_dbg_tlv_del_timers(mvm->trans);
1769 
1770 	/*
1771 	 * This is a bit racy, but worst case we tell mac80211 about
1772 	 * a stopped/aborted scan when that was already done which
1773 	 * is not a problem. It is necessary to abort any os scan
1774 	 * here because mac80211 requires having the scan cleared
1775 	 * before restarting.
1776 	 * We'll reset the scan_status to NONE in restart cleanup in
1777 	 * the next start() call from mac80211. If restart isn't called
1778 	 * (no fw restart) scan status will stay busy.
1779 	 */
1780 	iwl_mvm_report_scan_aborted(mvm);
1781 
1782 	/*
1783 	 * If we're restarting already, don't cycle restarts.
1784 	 * If INIT fw asserted, it will likely fail again.
1785 	 * If WoWLAN fw asserted, don't restart either, mac80211
1786 	 * can't recover this since we're already half suspended.
1787 	 */
1788 	if (!mvm->fw_restart && fw_error) {
1789 		iwl_fw_error_collect(&mvm->fwrt, false);
1790 	} else if (test_bit(IWL_MVM_STATUS_STARTING,
1791 			    &mvm->status)) {
1792 		IWL_ERR(mvm, "Starting mac, retry will be triggered anyway\n");
1793 	} else if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1794 		struct iwl_mvm_reprobe *reprobe;
1795 
1796 		IWL_ERR(mvm,
1797 			"Firmware error during reconfiguration - reprobe!\n");
1798 
1799 		/*
1800 		 * get a module reference to avoid doing this while unloading
1801 		 * anyway and to avoid scheduling a work with code that's
1802 		 * being removed.
1803 		 */
1804 		if (!try_module_get(THIS_MODULE)) {
1805 			IWL_ERR(mvm, "Module is being unloaded - abort\n");
1806 			return;
1807 		}
1808 
1809 		reprobe = kzalloc(sizeof(*reprobe), GFP_ATOMIC);
1810 		if (!reprobe) {
1811 			module_put(THIS_MODULE);
1812 			return;
1813 		}
1814 		reprobe->dev = get_device(mvm->trans->dev);
1815 		INIT_WORK(&reprobe->work, iwl_mvm_reprobe_wk);
1816 		schedule_work(&reprobe->work);
1817 	} else if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
1818 			    &mvm->status)) {
1819 		IWL_ERR(mvm, "HW restart already requested, but not started\n");
1820 	} else if (mvm->fwrt.cur_fw_img == IWL_UCODE_REGULAR &&
1821 		   mvm->hw_registered &&
1822 		   !test_bit(STATUS_TRANS_DEAD, &mvm->trans->status)) {
1823 		/* This should be first thing before trying to collect any
1824 		 * data to avoid endless loops if any HW error happens while
1825 		 * collecting debug data.
1826 		 */
1827 		set_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
1828 
1829 		if (mvm->fw->ucode_capa.error_log_size) {
1830 			u32 src_size = mvm->fw->ucode_capa.error_log_size;
1831 			u32 src_addr = mvm->fw->ucode_capa.error_log_addr;
1832 			u8 *recover_buf = kzalloc(src_size, GFP_ATOMIC);
1833 
1834 			if (recover_buf) {
1835 				mvm->error_recovery_buf = recover_buf;
1836 				iwl_trans_read_mem_bytes(mvm->trans,
1837 							 src_addr,
1838 							 recover_buf,
1839 							 src_size);
1840 			}
1841 		}
1842 
1843 		iwl_fw_error_collect(&mvm->fwrt, false);
1844 
1845 		if (fw_error && mvm->fw_restart > 0) {
1846 			mvm->fw_restart--;
1847 			ieee80211_restart_hw(mvm->hw);
1848 		} else if (mvm->fwrt.trans->dbg.restart_required) {
1849 			IWL_DEBUG_INFO(mvm, "FW restart requested after debug collection\n");
1850 			mvm->fwrt.trans->dbg.restart_required = FALSE;
1851 			ieee80211_restart_hw(mvm->hw);
1852 		} else if (mvm->trans->trans_cfg->device_family <= IWL_DEVICE_FAMILY_8000) {
1853 			ieee80211_restart_hw(mvm->hw);
1854 		}
1855 	}
1856 }
1857 
1858 static void iwl_mvm_nic_error(struct iwl_op_mode *op_mode, bool sync)
1859 {
1860 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1861 
1862 	if (!test_bit(STATUS_TRANS_DEAD, &mvm->trans->status) &&
1863 	    !test_and_clear_bit(IWL_MVM_STATUS_SUPPRESS_ERROR_LOG_ONCE,
1864 				&mvm->status))
1865 		iwl_mvm_dump_nic_error_log(mvm);
1866 
1867 	if (sync) {
1868 		iwl_fw_error_collect(&mvm->fwrt, true);
1869 		/*
1870 		 * Currently, the only case for sync=true is during
1871 		 * shutdown, so just stop in this case. If/when that
1872 		 * changes, we need to be a bit smarter here.
1873 		 */
1874 		return;
1875 	}
1876 
1877 	/*
1878 	 * If the firmware crashes while we're already considering it
1879 	 * to be dead then don't ask for a restart, that cannot do
1880 	 * anything useful anyway.
1881 	 */
1882 	if (!test_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status))
1883 		return;
1884 
1885 	iwl_mvm_nic_restart(mvm, false);
1886 }
1887 
1888 static void iwl_mvm_cmd_queue_full(struct iwl_op_mode *op_mode)
1889 {
1890 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1891 
1892 	WARN_ON(1);
1893 	iwl_mvm_nic_restart(mvm, true);
1894 }
1895 
1896 static void iwl_op_mode_mvm_time_point(struct iwl_op_mode *op_mode,
1897 				       enum iwl_fw_ini_time_point tp_id,
1898 				       union iwl_dbg_tlv_tp_data *tp_data)
1899 {
1900 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1901 
1902 	iwl_dbg_tlv_time_point(&mvm->fwrt, tp_id, tp_data);
1903 }
1904 
1905 #define IWL_MVM_COMMON_OPS					\
1906 	/* these could be differentiated */			\
1907 	.async_cb = iwl_mvm_async_cb,				\
1908 	.queue_full = iwl_mvm_stop_sw_queue,			\
1909 	.queue_not_full = iwl_mvm_wake_sw_queue,		\
1910 	.hw_rf_kill = iwl_mvm_set_hw_rfkill_state,		\
1911 	.free_skb = iwl_mvm_free_skb,				\
1912 	.nic_error = iwl_mvm_nic_error,				\
1913 	.cmd_queue_full = iwl_mvm_cmd_queue_full,		\
1914 	.nic_config = iwl_mvm_nic_config,			\
1915 	/* as we only register one, these MUST be common! */	\
1916 	.start = iwl_op_mode_mvm_start,				\
1917 	.stop = iwl_op_mode_mvm_stop,				\
1918 	.time_point = iwl_op_mode_mvm_time_point
1919 
1920 static const struct iwl_op_mode_ops iwl_mvm_ops = {
1921 	IWL_MVM_COMMON_OPS,
1922 	.rx = iwl_mvm_rx,
1923 };
1924 
1925 static void iwl_mvm_rx_mq_rss(struct iwl_op_mode *op_mode,
1926 			      struct napi_struct *napi,
1927 			      struct iwl_rx_cmd_buffer *rxb,
1928 			      unsigned int queue)
1929 {
1930 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1931 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1932 	u16 cmd = WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd);
1933 
1934 	if (unlikely(queue >= mvm->trans->num_rx_queues))
1935 		return;
1936 
1937 	if (unlikely(cmd == WIDE_ID(LEGACY_GROUP, FRAME_RELEASE)))
1938 		iwl_mvm_rx_frame_release(mvm, napi, rxb, queue);
1939 	else if (unlikely(cmd == WIDE_ID(DATA_PATH_GROUP,
1940 					 RX_QUEUES_NOTIFICATION)))
1941 		iwl_mvm_rx_queue_notif(mvm, napi, rxb, queue);
1942 	else if (likely(cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_MPDU_CMD)))
1943 		iwl_mvm_rx_mpdu_mq(mvm, napi, rxb, queue);
1944 }
1945 
1946 static const struct iwl_op_mode_ops iwl_mvm_ops_mq = {
1947 	IWL_MVM_COMMON_OPS,
1948 	.rx = iwl_mvm_rx_mq,
1949 	.rx_rss = iwl_mvm_rx_mq_rss,
1950 };
1951