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