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 	mutex_lock(&mvm->mutex);
690 
691 	ret = iwl_run_init_mvm_ucode(mvm);
692 
693 	if (ret && ret != -ERFKILL)
694 		iwl_fw_dbg_error_collect(&mvm->fwrt, FW_DBG_TRIGGER_DRIVER);
695 	if (!ret && iwl_mvm_is_lar_supported(mvm)) {
696 		mvm->hw->wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
697 		ret = iwl_mvm_init_mcc(mvm);
698 	}
699 
700 	if (!iwlmvm_mod_params.init_dbg || !ret)
701 		iwl_mvm_stop_device(mvm);
702 
703 	mutex_unlock(&mvm->mutex);
704 	rtnl_unlock();
705 
706 	if (ret < 0)
707 		IWL_ERR(mvm, "Failed to run INIT ucode: %d\n", ret);
708 
709 	return ret;
710 }
711 
712 static int iwl_mvm_start_post_nvm(struct iwl_mvm *mvm)
713 {
714 	int ret;
715 
716 	iwl_mvm_toggle_tx_ant(mvm, &mvm->mgmt_last_antenna_idx);
717 
718 	ret = iwl_mvm_mac_setup_register(mvm);
719 	if (ret)
720 		return ret;
721 	mvm->hw_registered = true;
722 
723 	iwl_mvm_dbgfs_register(mvm);
724 
725 	return 0;
726 }
727 
728 struct iwl_mvm_frob_txf_data {
729 	u8 *buf;
730 	size_t buflen;
731 };
732 
733 static void iwl_mvm_frob_txf_key_iter(struct ieee80211_hw *hw,
734 				      struct ieee80211_vif *vif,
735 				      struct ieee80211_sta *sta,
736 				      struct ieee80211_key_conf *key,
737 				      void *data)
738 {
739 	struct iwl_mvm_frob_txf_data *txf = data;
740 	u8 keylen, match, matchend;
741 	u8 *keydata;
742 	size_t i;
743 
744 	switch (key->cipher) {
745 	case WLAN_CIPHER_SUITE_CCMP:
746 		keydata = key->key;
747 		keylen = key->keylen;
748 		break;
749 	case WLAN_CIPHER_SUITE_WEP40:
750 	case WLAN_CIPHER_SUITE_WEP104:
751 	case WLAN_CIPHER_SUITE_TKIP:
752 		/*
753 		 * WEP has short keys which might show up in the payload,
754 		 * and then you can deduce the key, so in this case just
755 		 * remove all FIFO data.
756 		 * For TKIP, we don't know the phase 2 keys here, so same.
757 		 */
758 		memset(txf->buf, 0xBB, txf->buflen);
759 		return;
760 	default:
761 		return;
762 	}
763 
764 	/* scan for key material and clear it out */
765 	match = 0;
766 	for (i = 0; i < txf->buflen; i++) {
767 		if (txf->buf[i] != keydata[match]) {
768 			match = 0;
769 			continue;
770 		}
771 		match++;
772 		if (match == keylen) {
773 			memset(txf->buf + i - keylen, 0xAA, keylen);
774 			match = 0;
775 		}
776 	}
777 
778 	/* we're dealing with a FIFO, so check wrapped around data */
779 	matchend = match;
780 	for (i = 0; match && i < keylen - match; i++) {
781 		if (txf->buf[i] != keydata[match])
782 			break;
783 		match++;
784 		if (match == keylen) {
785 			memset(txf->buf, 0xAA, i + 1);
786 			memset(txf->buf + txf->buflen - matchend, 0xAA,
787 			       matchend);
788 			break;
789 		}
790 	}
791 }
792 
793 static void iwl_mvm_frob_txf(void *ctx, void *buf, size_t buflen)
794 {
795 	struct iwl_mvm_frob_txf_data txf = {
796 		.buf = buf,
797 		.buflen = buflen,
798 	};
799 	struct iwl_mvm *mvm = ctx;
800 
801 	/* embedded key material exists only on old API */
802 	if (iwl_mvm_has_new_tx_api(mvm))
803 		return;
804 
805 	rcu_read_lock();
806 	ieee80211_iter_keys_rcu(mvm->hw, NULL, iwl_mvm_frob_txf_key_iter, &txf);
807 	rcu_read_unlock();
808 }
809 
810 static void iwl_mvm_frob_hcmd(void *ctx, void *hcmd, size_t len)
811 {
812 	/* we only use wide headers for commands */
813 	struct iwl_cmd_header_wide *hdr = hcmd;
814 	unsigned int frob_start = sizeof(*hdr), frob_end = 0;
815 
816 	if (len < sizeof(hdr))
817 		return;
818 
819 	/* all the commands we care about are in LONG_GROUP */
820 	if (hdr->group_id != LONG_GROUP)
821 		return;
822 
823 	switch (hdr->cmd) {
824 	case WEP_KEY:
825 	case WOWLAN_TKIP_PARAM:
826 	case WOWLAN_KEK_KCK_MATERIAL:
827 	case ADD_STA_KEY:
828 		/*
829 		 * blank out everything here, easier than dealing
830 		 * with the various versions of the command
831 		 */
832 		frob_end = INT_MAX;
833 		break;
834 	case MGMT_MCAST_KEY:
835 		frob_start = offsetof(struct iwl_mvm_mgmt_mcast_key_cmd, igtk);
836 		BUILD_BUG_ON(offsetof(struct iwl_mvm_mgmt_mcast_key_cmd, igtk) !=
837 			     offsetof(struct iwl_mvm_mgmt_mcast_key_cmd_v1, igtk));
838 
839 		frob_end = offsetofend(struct iwl_mvm_mgmt_mcast_key_cmd, igtk);
840 		BUILD_BUG_ON(offsetof(struct iwl_mvm_mgmt_mcast_key_cmd, igtk) <
841 			     offsetof(struct iwl_mvm_mgmt_mcast_key_cmd_v1, igtk));
842 		break;
843 	}
844 
845 	if (frob_start >= frob_end)
846 		return;
847 
848 	if (frob_end > len)
849 		frob_end = len;
850 
851 	memset((u8 *)hcmd + frob_start, 0xAA, frob_end - frob_start);
852 }
853 
854 static void iwl_mvm_frob_mem(void *ctx, u32 mem_addr, void *mem, size_t buflen)
855 {
856 	const struct iwl_dump_exclude *excl;
857 	struct iwl_mvm *mvm = ctx;
858 	int i;
859 
860 	switch (mvm->fwrt.cur_fw_img) {
861 	case IWL_UCODE_INIT:
862 	default:
863 		/* not relevant */
864 		return;
865 	case IWL_UCODE_REGULAR:
866 	case IWL_UCODE_REGULAR_USNIFFER:
867 		excl = mvm->fw->dump_excl;
868 		break;
869 	case IWL_UCODE_WOWLAN:
870 		excl = mvm->fw->dump_excl_wowlan;
871 		break;
872 	}
873 
874 	BUILD_BUG_ON(sizeof(mvm->fw->dump_excl) !=
875 		     sizeof(mvm->fw->dump_excl_wowlan));
876 
877 	for (i = 0; i < ARRAY_SIZE(mvm->fw->dump_excl); i++) {
878 		u32 start, end;
879 
880 		if (!excl[i].addr || !excl[i].size)
881 			continue;
882 
883 		start = excl[i].addr;
884 		end = start + excl[i].size;
885 
886 		if (end <= mem_addr || start >= mem_addr + buflen)
887 			continue;
888 
889 		if (start < mem_addr)
890 			start = mem_addr;
891 
892 		if (end > mem_addr + buflen)
893 			end = mem_addr + buflen;
894 
895 		memset((u8 *)mem + start - mem_addr, 0xAA, end - start);
896 	}
897 }
898 
899 static const struct iwl_dump_sanitize_ops iwl_mvm_sanitize_ops = {
900 	.frob_txf = iwl_mvm_frob_txf,
901 	.frob_hcmd = iwl_mvm_frob_hcmd,
902 	.frob_mem = iwl_mvm_frob_mem,
903 };
904 
905 static struct iwl_op_mode *
906 iwl_op_mode_mvm_start(struct iwl_trans *trans, const struct iwl_cfg *cfg,
907 		      const struct iwl_fw *fw, struct dentry *dbgfs_dir)
908 {
909 	struct ieee80211_hw *hw;
910 	struct iwl_op_mode *op_mode;
911 	struct iwl_mvm *mvm;
912 	struct iwl_trans_config trans_cfg = {};
913 	static const u8 no_reclaim_cmds[] = {
914 		TX_CMD,
915 	};
916 	int err, scan_size;
917 	u32 min_backoff;
918 	enum iwl_amsdu_size rb_size_default;
919 
920 	/*
921 	 * We use IWL_MVM_STATION_COUNT_MAX to check the validity of the station
922 	 * index all over the driver - check that its value corresponds to the
923 	 * array size.
924 	 */
925 	BUILD_BUG_ON(ARRAY_SIZE(mvm->fw_id_to_mac_id) !=
926 		     IWL_MVM_STATION_COUNT_MAX);
927 
928 	/********************************
929 	 * 1. Allocating and configuring HW data
930 	 ********************************/
931 	hw = ieee80211_alloc_hw(sizeof(struct iwl_op_mode) +
932 				sizeof(struct iwl_mvm),
933 				&iwl_mvm_hw_ops);
934 	if (!hw)
935 		return NULL;
936 
937 	hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF;
938 
939 	if (cfg->max_tx_agg_size)
940 		hw->max_tx_aggregation_subframes = cfg->max_tx_agg_size;
941 	else
942 		hw->max_tx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF;
943 
944 	op_mode = hw->priv;
945 
946 	mvm = IWL_OP_MODE_GET_MVM(op_mode);
947 	mvm->dev = trans->dev;
948 	mvm->trans = trans;
949 	mvm->cfg = cfg;
950 	mvm->fw = fw;
951 	mvm->hw = hw;
952 
953 	iwl_fw_runtime_init(&mvm->fwrt, trans, fw, &iwl_mvm_fwrt_ops, mvm,
954 			    &iwl_mvm_sanitize_ops, mvm, dbgfs_dir);
955 
956 	iwl_mvm_get_acpi_tables(mvm);
957 
958 	mvm->init_status = 0;
959 
960 	if (iwl_mvm_has_new_rx_api(mvm)) {
961 		op_mode->ops = &iwl_mvm_ops_mq;
962 		trans->rx_mpdu_cmd_hdr_size =
963 			(trans->trans_cfg->device_family >=
964 			 IWL_DEVICE_FAMILY_AX210) ?
965 			sizeof(struct iwl_rx_mpdu_desc) :
966 			IWL_RX_DESC_SIZE_V1;
967 	} else {
968 		op_mode->ops = &iwl_mvm_ops;
969 		trans->rx_mpdu_cmd_hdr_size =
970 			sizeof(struct iwl_rx_mpdu_res_start);
971 
972 		if (WARN_ON(trans->num_rx_queues > 1))
973 			goto out_free;
974 	}
975 
976 	mvm->fw_restart = iwlwifi_mod_params.fw_restart ? -1 : 0;
977 
978 	if (iwl_mvm_has_new_tx_api(mvm)) {
979 		/*
980 		 * If we have the new TX/queue allocation API initialize them
981 		 * all to invalid numbers. We'll rewrite the ones that we need
982 		 * later, but that doesn't happen for all of them all of the
983 		 * time (e.g. P2P Device is optional), and if a dynamic queue
984 		 * ends up getting number 2 (IWL_MVM_DQA_P2P_DEVICE_QUEUE) then
985 		 * iwl_mvm_is_static_queue() erroneously returns true, and we
986 		 * might have things getting stuck.
987 		 */
988 		mvm->aux_queue = IWL_MVM_INVALID_QUEUE;
989 		mvm->snif_queue = IWL_MVM_INVALID_QUEUE;
990 		mvm->probe_queue = IWL_MVM_INVALID_QUEUE;
991 		mvm->p2p_dev_queue = IWL_MVM_INVALID_QUEUE;
992 	} else {
993 		mvm->aux_queue = IWL_MVM_DQA_AUX_QUEUE;
994 		mvm->snif_queue = IWL_MVM_DQA_INJECT_MONITOR_QUEUE;
995 		mvm->probe_queue = IWL_MVM_DQA_AP_PROBE_RESP_QUEUE;
996 		mvm->p2p_dev_queue = IWL_MVM_DQA_P2P_DEVICE_QUEUE;
997 	}
998 
999 	mvm->sf_state = SF_UNINIT;
1000 	if (iwl_mvm_has_unified_ucode(mvm))
1001 		iwl_fw_set_current_image(&mvm->fwrt, IWL_UCODE_REGULAR);
1002 	else
1003 		iwl_fw_set_current_image(&mvm->fwrt, IWL_UCODE_INIT);
1004 	mvm->drop_bcn_ap_mode = true;
1005 
1006 	mutex_init(&mvm->mutex);
1007 	spin_lock_init(&mvm->async_handlers_lock);
1008 	INIT_LIST_HEAD(&mvm->time_event_list);
1009 	INIT_LIST_HEAD(&mvm->aux_roc_te_list);
1010 	INIT_LIST_HEAD(&mvm->async_handlers_list);
1011 	spin_lock_init(&mvm->time_event_lock);
1012 	INIT_LIST_HEAD(&mvm->ftm_initiator.loc_list);
1013 	INIT_LIST_HEAD(&mvm->ftm_initiator.pasn_list);
1014 	INIT_LIST_HEAD(&mvm->resp_pasn_list);
1015 
1016 	INIT_WORK(&mvm->async_handlers_wk, iwl_mvm_async_handlers_wk);
1017 	INIT_WORK(&mvm->roc_done_wk, iwl_mvm_roc_done_wk);
1018 	INIT_DELAYED_WORK(&mvm->tdls_cs.dwork, iwl_mvm_tdls_ch_switch_work);
1019 	INIT_DELAYED_WORK(&mvm->scan_timeout_dwork, iwl_mvm_scan_timeout_wk);
1020 	INIT_WORK(&mvm->add_stream_wk, iwl_mvm_add_new_dqa_stream_wk);
1021 	INIT_LIST_HEAD(&mvm->add_stream_txqs);
1022 
1023 	init_waitqueue_head(&mvm->rx_sync_waitq);
1024 
1025 	mvm->queue_sync_state = 0;
1026 
1027 	SET_IEEE80211_DEV(mvm->hw, mvm->trans->dev);
1028 
1029 	spin_lock_init(&mvm->tcm.lock);
1030 	INIT_DELAYED_WORK(&mvm->tcm.work, iwl_mvm_tcm_work);
1031 	mvm->tcm.ts = jiffies;
1032 	mvm->tcm.ll_ts = jiffies;
1033 	mvm->tcm.uapsd_nonagg_ts = jiffies;
1034 
1035 	INIT_DELAYED_WORK(&mvm->cs_tx_unblock_dwork, iwl_mvm_tx_unblock_dwork);
1036 
1037 	mvm->cmd_ver.d0i3_resp =
1038 		iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP, D0I3_END_CMD,
1039 					0);
1040 	/* we only support version 1 */
1041 	if (WARN_ON_ONCE(mvm->cmd_ver.d0i3_resp > 1))
1042 		goto out_free;
1043 
1044 	mvm->cmd_ver.range_resp =
1045 		iwl_fw_lookup_notif_ver(mvm->fw, LOCATION_GROUP,
1046 					TOF_RANGE_RESPONSE_NOTIF, 5);
1047 	/* we only support up to version 9 */
1048 	if (WARN_ON_ONCE(mvm->cmd_ver.range_resp > 9))
1049 		goto out_free;
1050 
1051 	/*
1052 	 * Populate the state variables that the transport layer needs
1053 	 * to know about.
1054 	 */
1055 	trans_cfg.op_mode = op_mode;
1056 	trans_cfg.no_reclaim_cmds = no_reclaim_cmds;
1057 	trans_cfg.n_no_reclaim_cmds = ARRAY_SIZE(no_reclaim_cmds);
1058 
1059 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
1060 		rb_size_default = IWL_AMSDU_2K;
1061 	else
1062 		rb_size_default = IWL_AMSDU_4K;
1063 
1064 	switch (iwlwifi_mod_params.amsdu_size) {
1065 	case IWL_AMSDU_DEF:
1066 		trans_cfg.rx_buf_size = rb_size_default;
1067 		break;
1068 	case IWL_AMSDU_4K:
1069 		trans_cfg.rx_buf_size = IWL_AMSDU_4K;
1070 		break;
1071 	case IWL_AMSDU_8K:
1072 		trans_cfg.rx_buf_size = IWL_AMSDU_8K;
1073 		break;
1074 	case IWL_AMSDU_12K:
1075 		trans_cfg.rx_buf_size = IWL_AMSDU_12K;
1076 		break;
1077 	default:
1078 		pr_err("%s: Unsupported amsdu_size: %d\n", KBUILD_MODNAME,
1079 		       iwlwifi_mod_params.amsdu_size);
1080 		trans_cfg.rx_buf_size = rb_size_default;
1081 	}
1082 
1083 	trans->wide_cmd_header = true;
1084 	trans_cfg.bc_table_dword =
1085 		mvm->trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_AX210;
1086 
1087 	trans_cfg.command_groups = iwl_mvm_groups;
1088 	trans_cfg.command_groups_size = ARRAY_SIZE(iwl_mvm_groups);
1089 
1090 	trans_cfg.cmd_queue = IWL_MVM_DQA_CMD_QUEUE;
1091 	trans_cfg.cmd_fifo = IWL_MVM_TX_FIFO_CMD;
1092 	trans_cfg.scd_set_active = true;
1093 
1094 	trans_cfg.cb_data_offs = offsetof(struct ieee80211_tx_info,
1095 					  driver_data[2]);
1096 
1097 	/* Set a short watchdog for the command queue */
1098 	trans_cfg.cmd_q_wdg_timeout =
1099 		iwl_mvm_get_wd_timeout(mvm, NULL, false, true);
1100 
1101 	snprintf(mvm->hw->wiphy->fw_version,
1102 		 sizeof(mvm->hw->wiphy->fw_version),
1103 		 "%s", fw->fw_version);
1104 
1105 	trans_cfg.fw_reset_handshake = fw_has_capa(&mvm->fw->ucode_capa,
1106 						   IWL_UCODE_TLV_CAPA_FW_RESET_HANDSHAKE);
1107 
1108 	/* Configure transport layer */
1109 	iwl_trans_configure(mvm->trans, &trans_cfg);
1110 
1111 	trans->rx_mpdu_cmd = REPLY_RX_MPDU_CMD;
1112 	trans->dbg.dest_tlv = mvm->fw->dbg.dest_tlv;
1113 	trans->dbg.n_dest_reg = mvm->fw->dbg.n_dest_reg;
1114 	memcpy(trans->dbg.conf_tlv, mvm->fw->dbg.conf_tlv,
1115 	       sizeof(trans->dbg.conf_tlv));
1116 	trans->dbg.trigger_tlv = mvm->fw->dbg.trigger_tlv;
1117 
1118 	trans->iml = mvm->fw->iml;
1119 	trans->iml_len = mvm->fw->iml_len;
1120 
1121 	/* set up notification wait support */
1122 	iwl_notification_wait_init(&mvm->notif_wait);
1123 
1124 	/* Init phy db */
1125 	mvm->phy_db = iwl_phy_db_init(trans);
1126 	if (!mvm->phy_db) {
1127 		IWL_ERR(mvm, "Cannot init phy_db\n");
1128 		goto out_free;
1129 	}
1130 
1131 	IWL_INFO(mvm, "Detected %s, REV=0x%X\n",
1132 		 mvm->trans->name, mvm->trans->hw_rev);
1133 
1134 	if (iwlwifi_mod_params.nvm_file)
1135 		mvm->nvm_file_name = iwlwifi_mod_params.nvm_file;
1136 	else
1137 		IWL_DEBUG_EEPROM(mvm->trans->dev,
1138 				 "working without external nvm file\n");
1139 
1140 	err = iwl_trans_start_hw(mvm->trans);
1141 	if (err)
1142 		goto out_free;
1143 
1144 	scan_size = iwl_mvm_scan_size(mvm);
1145 
1146 	mvm->scan_cmd = kmalloc(scan_size, GFP_KERNEL);
1147 	if (!mvm->scan_cmd)
1148 		goto out_free;
1149 
1150 	/* invalidate ids to prevent accidental removal of sta_id 0 */
1151 	mvm->aux_sta.sta_id = IWL_MVM_INVALID_STA;
1152 	mvm->snif_sta.sta_id = IWL_MVM_INVALID_STA;
1153 
1154 	/* Set EBS as successful as long as not stated otherwise by the FW. */
1155 	mvm->last_ebs_successful = true;
1156 
1157 	min_backoff = iwl_mvm_min_backoff(mvm);
1158 	iwl_mvm_thermal_initialize(mvm, min_backoff);
1159 
1160 	if (!iwl_mvm_has_new_rx_stats_api(mvm))
1161 		memset(&mvm->rx_stats_v3, 0,
1162 		       sizeof(struct mvm_statistics_rx_v3));
1163 	else
1164 		memset(&mvm->rx_stats, 0, sizeof(struct mvm_statistics_rx));
1165 
1166 	mvm->debugfs_dir = dbgfs_dir;
1167 
1168 	if (iwl_mvm_start_get_nvm(mvm))
1169 		goto out_thermal_exit;
1170 
1171 	if (iwl_mvm_start_post_nvm(mvm))
1172 		goto out_thermal_exit;
1173 
1174 	return op_mode;
1175 
1176  out_thermal_exit:
1177 	iwl_mvm_thermal_exit(mvm);
1178  out_free:
1179 	iwl_fw_flush_dumps(&mvm->fwrt);
1180 	iwl_fw_runtime_free(&mvm->fwrt);
1181 
1182 	if (iwlmvm_mod_params.init_dbg)
1183 		return op_mode;
1184 	iwl_phy_db_free(mvm->phy_db);
1185 	kfree(mvm->scan_cmd);
1186 	iwl_trans_op_mode_leave(trans);
1187 
1188 	ieee80211_free_hw(mvm->hw);
1189 	return NULL;
1190 }
1191 
1192 void iwl_mvm_stop_device(struct iwl_mvm *mvm)
1193 {
1194 	lockdep_assert_held(&mvm->mutex);
1195 
1196 	iwl_fw_cancel_timestamp(&mvm->fwrt);
1197 
1198 	clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
1199 
1200 	iwl_fw_dbg_stop_sync(&mvm->fwrt);
1201 	iwl_trans_stop_device(mvm->trans);
1202 	iwl_free_fw_paging(&mvm->fwrt);
1203 	iwl_fw_dump_conf_clear(&mvm->fwrt);
1204 }
1205 
1206 static void iwl_op_mode_mvm_stop(struct iwl_op_mode *op_mode)
1207 {
1208 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1209 	int i;
1210 
1211 	iwl_mvm_leds_exit(mvm);
1212 
1213 	iwl_mvm_thermal_exit(mvm);
1214 
1215 	ieee80211_unregister_hw(mvm->hw);
1216 
1217 	kfree(mvm->scan_cmd);
1218 	kfree(mvm->mcast_filter_cmd);
1219 	mvm->mcast_filter_cmd = NULL;
1220 
1221 	kfree(mvm->error_recovery_buf);
1222 	mvm->error_recovery_buf = NULL;
1223 
1224 	iwl_trans_op_mode_leave(mvm->trans);
1225 
1226 	iwl_phy_db_free(mvm->phy_db);
1227 	mvm->phy_db = NULL;
1228 
1229 	kfree(mvm->nvm_data);
1230 	for (i = 0; i < NVM_MAX_NUM_SECTIONS; i++)
1231 		kfree(mvm->nvm_sections[i].data);
1232 
1233 	cancel_delayed_work_sync(&mvm->tcm.work);
1234 
1235 	iwl_fw_runtime_free(&mvm->fwrt);
1236 	mutex_destroy(&mvm->mutex);
1237 
1238 	ieee80211_free_hw(mvm->hw);
1239 }
1240 
1241 struct iwl_async_handler_entry {
1242 	struct list_head list;
1243 	struct iwl_rx_cmd_buffer rxb;
1244 	enum iwl_rx_handler_context context;
1245 	void (*fn)(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb);
1246 };
1247 
1248 void iwl_mvm_async_handlers_purge(struct iwl_mvm *mvm)
1249 {
1250 	struct iwl_async_handler_entry *entry, *tmp;
1251 
1252 	spin_lock_bh(&mvm->async_handlers_lock);
1253 	list_for_each_entry_safe(entry, tmp, &mvm->async_handlers_list, list) {
1254 		iwl_free_rxb(&entry->rxb);
1255 		list_del(&entry->list);
1256 		kfree(entry);
1257 	}
1258 	spin_unlock_bh(&mvm->async_handlers_lock);
1259 }
1260 
1261 static void iwl_mvm_async_handlers_wk(struct work_struct *wk)
1262 {
1263 	struct iwl_mvm *mvm =
1264 		container_of(wk, struct iwl_mvm, async_handlers_wk);
1265 	struct iwl_async_handler_entry *entry, *tmp;
1266 	LIST_HEAD(local_list);
1267 
1268 	/* Ensure that we are not in stop flow (check iwl_mvm_mac_stop) */
1269 
1270 	/*
1271 	 * Sync with Rx path with a lock. Remove all the entries from this list,
1272 	 * add them to a local one (lock free), and then handle them.
1273 	 */
1274 	spin_lock_bh(&mvm->async_handlers_lock);
1275 	list_splice_init(&mvm->async_handlers_list, &local_list);
1276 	spin_unlock_bh(&mvm->async_handlers_lock);
1277 
1278 	list_for_each_entry_safe(entry, tmp, &local_list, list) {
1279 		if (entry->context == RX_HANDLER_ASYNC_LOCKED)
1280 			mutex_lock(&mvm->mutex);
1281 		entry->fn(mvm, &entry->rxb);
1282 		iwl_free_rxb(&entry->rxb);
1283 		list_del(&entry->list);
1284 		if (entry->context == RX_HANDLER_ASYNC_LOCKED)
1285 			mutex_unlock(&mvm->mutex);
1286 		kfree(entry);
1287 	}
1288 }
1289 
1290 static inline void iwl_mvm_rx_check_trigger(struct iwl_mvm *mvm,
1291 					    struct iwl_rx_packet *pkt)
1292 {
1293 	struct iwl_fw_dbg_trigger_tlv *trig;
1294 	struct iwl_fw_dbg_trigger_cmd *cmds_trig;
1295 	int i;
1296 
1297 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, NULL,
1298 				     FW_DBG_TRIGGER_FW_NOTIF);
1299 	if (!trig)
1300 		return;
1301 
1302 	cmds_trig = (void *)trig->data;
1303 
1304 	for (i = 0; i < ARRAY_SIZE(cmds_trig->cmds); i++) {
1305 		/* don't collect on CMD 0 */
1306 		if (!cmds_trig->cmds[i].cmd_id)
1307 			break;
1308 
1309 		if (cmds_trig->cmds[i].cmd_id != pkt->hdr.cmd ||
1310 		    cmds_trig->cmds[i].group_id != pkt->hdr.group_id)
1311 			continue;
1312 
1313 		iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
1314 					"CMD 0x%02x.%02x received",
1315 					pkt->hdr.group_id, pkt->hdr.cmd);
1316 		break;
1317 	}
1318 }
1319 
1320 static void iwl_mvm_rx_common(struct iwl_mvm *mvm,
1321 			      struct iwl_rx_cmd_buffer *rxb,
1322 			      struct iwl_rx_packet *pkt)
1323 {
1324 	unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
1325 	int i;
1326 	union iwl_dbg_tlv_tp_data tp_data = { .fw_pkt = pkt };
1327 
1328 	iwl_dbg_tlv_time_point(&mvm->fwrt,
1329 			       IWL_FW_INI_TIME_POINT_FW_RSP_OR_NOTIF, &tp_data);
1330 	iwl_mvm_rx_check_trigger(mvm, pkt);
1331 
1332 	/*
1333 	 * Do the notification wait before RX handlers so
1334 	 * even if the RX handler consumes the RXB we have
1335 	 * access to it in the notification wait entry.
1336 	 */
1337 	iwl_notification_wait_notify(&mvm->notif_wait, pkt);
1338 
1339 	for (i = 0; i < ARRAY_SIZE(iwl_mvm_rx_handlers); i++) {
1340 		const struct iwl_rx_handlers *rx_h = &iwl_mvm_rx_handlers[i];
1341 		struct iwl_async_handler_entry *entry;
1342 
1343 		if (rx_h->cmd_id != WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd))
1344 			continue;
1345 
1346 		if (unlikely(pkt_len < rx_h->min_size))
1347 			return;
1348 
1349 		if (rx_h->context == RX_HANDLER_SYNC) {
1350 			rx_h->fn(mvm, rxb);
1351 			return;
1352 		}
1353 
1354 		entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
1355 		/* we can't do much... */
1356 		if (!entry)
1357 			return;
1358 
1359 		entry->rxb._page = rxb_steal_page(rxb);
1360 		entry->rxb._offset = rxb->_offset;
1361 		entry->rxb._rx_page_order = rxb->_rx_page_order;
1362 		entry->fn = rx_h->fn;
1363 		entry->context = rx_h->context;
1364 		spin_lock(&mvm->async_handlers_lock);
1365 		list_add_tail(&entry->list, &mvm->async_handlers_list);
1366 		spin_unlock(&mvm->async_handlers_lock);
1367 		schedule_work(&mvm->async_handlers_wk);
1368 		break;
1369 	}
1370 }
1371 
1372 static void iwl_mvm_rx(struct iwl_op_mode *op_mode,
1373 		       struct napi_struct *napi,
1374 		       struct iwl_rx_cmd_buffer *rxb)
1375 {
1376 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1377 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1378 	u16 cmd = WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd);
1379 
1380 	if (likely(cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_MPDU_CMD)))
1381 		iwl_mvm_rx_rx_mpdu(mvm, napi, rxb);
1382 	else if (cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_PHY_CMD))
1383 		iwl_mvm_rx_rx_phy_cmd(mvm, rxb);
1384 	else
1385 		iwl_mvm_rx_common(mvm, rxb, pkt);
1386 }
1387 
1388 void iwl_mvm_rx_mq(struct iwl_op_mode *op_mode,
1389 		   struct napi_struct *napi,
1390 		   struct iwl_rx_cmd_buffer *rxb)
1391 {
1392 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1393 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1394 	u16 cmd = WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd);
1395 
1396 	if (likely(cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_MPDU_CMD)))
1397 		iwl_mvm_rx_mpdu_mq(mvm, napi, rxb, 0);
1398 	else if (unlikely(cmd == WIDE_ID(DATA_PATH_GROUP,
1399 					 RX_QUEUES_NOTIFICATION)))
1400 		iwl_mvm_rx_queue_notif(mvm, napi, rxb, 0);
1401 	else if (cmd == WIDE_ID(LEGACY_GROUP, FRAME_RELEASE))
1402 		iwl_mvm_rx_frame_release(mvm, napi, rxb, 0);
1403 	else if (cmd == WIDE_ID(LEGACY_GROUP, BAR_FRAME_RELEASE))
1404 		iwl_mvm_rx_bar_frame_release(mvm, napi, rxb, 0);
1405 	else if (cmd == WIDE_ID(DATA_PATH_GROUP, RX_NO_DATA_NOTIF))
1406 		iwl_mvm_rx_monitor_no_data(mvm, napi, rxb, 0);
1407 	else
1408 		iwl_mvm_rx_common(mvm, rxb, pkt);
1409 }
1410 
1411 static void iwl_mvm_async_cb(struct iwl_op_mode *op_mode,
1412 			     const struct iwl_device_cmd *cmd)
1413 {
1414 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1415 
1416 	/*
1417 	 * For now, we only set the CMD_WANT_ASYNC_CALLBACK for ADD_STA
1418 	 * commands that need to block the Tx queues.
1419 	 */
1420 	iwl_trans_block_txq_ptrs(mvm->trans, false);
1421 }
1422 
1423 static int iwl_mvm_is_static_queue(struct iwl_mvm *mvm, int queue)
1424 {
1425 	return queue == mvm->aux_queue || queue == mvm->probe_queue ||
1426 		queue == mvm->p2p_dev_queue || queue == mvm->snif_queue;
1427 }
1428 
1429 static void iwl_mvm_queue_state_change(struct iwl_op_mode *op_mode,
1430 				       int hw_queue, bool start)
1431 {
1432 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1433 	struct ieee80211_sta *sta;
1434 	struct ieee80211_txq *txq;
1435 	struct iwl_mvm_txq *mvmtxq;
1436 	int i;
1437 	unsigned long tid_bitmap;
1438 	struct iwl_mvm_sta *mvmsta;
1439 	u8 sta_id;
1440 
1441 	sta_id = iwl_mvm_has_new_tx_api(mvm) ?
1442 		mvm->tvqm_info[hw_queue].sta_id :
1443 		mvm->queue_info[hw_queue].ra_sta_id;
1444 
1445 	if (WARN_ON_ONCE(sta_id >= mvm->fw->ucode_capa.num_stations))
1446 		return;
1447 
1448 	rcu_read_lock();
1449 
1450 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1451 	if (IS_ERR_OR_NULL(sta))
1452 		goto out;
1453 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
1454 
1455 	if (iwl_mvm_is_static_queue(mvm, hw_queue)) {
1456 		if (!start)
1457 			ieee80211_stop_queues(mvm->hw);
1458 		else if (mvmsta->sta_state != IEEE80211_STA_NOTEXIST)
1459 			ieee80211_wake_queues(mvm->hw);
1460 
1461 		goto out;
1462 	}
1463 
1464 	if (iwl_mvm_has_new_tx_api(mvm)) {
1465 		int tid = mvm->tvqm_info[hw_queue].txq_tid;
1466 
1467 		tid_bitmap = BIT(tid);
1468 	} else {
1469 		tid_bitmap = mvm->queue_info[hw_queue].tid_bitmap;
1470 	}
1471 
1472 	for_each_set_bit(i, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1473 		int tid = i;
1474 
1475 		if (tid == IWL_MAX_TID_COUNT)
1476 			tid = IEEE80211_NUM_TIDS;
1477 
1478 		txq = sta->txq[tid];
1479 		mvmtxq = iwl_mvm_txq_from_mac80211(txq);
1480 		mvmtxq->stopped = !start;
1481 
1482 		if (start && mvmsta->sta_state != IEEE80211_STA_NOTEXIST)
1483 			iwl_mvm_mac_itxq_xmit(mvm->hw, txq);
1484 	}
1485 
1486 out:
1487 	rcu_read_unlock();
1488 }
1489 
1490 static void iwl_mvm_stop_sw_queue(struct iwl_op_mode *op_mode, int hw_queue)
1491 {
1492 	iwl_mvm_queue_state_change(op_mode, hw_queue, false);
1493 }
1494 
1495 static void iwl_mvm_wake_sw_queue(struct iwl_op_mode *op_mode, int hw_queue)
1496 {
1497 	iwl_mvm_queue_state_change(op_mode, hw_queue, true);
1498 }
1499 
1500 static void iwl_mvm_set_rfkill_state(struct iwl_mvm *mvm)
1501 {
1502 	bool state = iwl_mvm_is_radio_killed(mvm);
1503 
1504 	if (state)
1505 		wake_up(&mvm->rx_sync_waitq);
1506 
1507 	wiphy_rfkill_set_hw_state(mvm->hw->wiphy, state);
1508 }
1509 
1510 void iwl_mvm_set_hw_ctkill_state(struct iwl_mvm *mvm, bool state)
1511 {
1512 	if (state)
1513 		set_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status);
1514 	else
1515 		clear_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status);
1516 
1517 	iwl_mvm_set_rfkill_state(mvm);
1518 }
1519 
1520 static bool iwl_mvm_set_hw_rfkill_state(struct iwl_op_mode *op_mode, bool state)
1521 {
1522 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1523 	bool rfkill_safe_init_done = READ_ONCE(mvm->rfkill_safe_init_done);
1524 	bool unified = iwl_mvm_has_unified_ucode(mvm);
1525 
1526 	if (state)
1527 		set_bit(IWL_MVM_STATUS_HW_RFKILL, &mvm->status);
1528 	else
1529 		clear_bit(IWL_MVM_STATUS_HW_RFKILL, &mvm->status);
1530 
1531 	iwl_mvm_set_rfkill_state(mvm);
1532 
1533 	 /* iwl_run_init_mvm_ucode is waiting for results, abort it. */
1534 	if (rfkill_safe_init_done)
1535 		iwl_abort_notification_waits(&mvm->notif_wait);
1536 
1537 	/*
1538 	 * Don't ask the transport to stop the firmware. We'll do it
1539 	 * after cfg80211 takes us down.
1540 	 */
1541 	if (unified)
1542 		return false;
1543 
1544 	/*
1545 	 * Stop the device if we run OPERATIONAL firmware or if we are in the
1546 	 * middle of the calibrations.
1547 	 */
1548 	return state && rfkill_safe_init_done;
1549 }
1550 
1551 static void iwl_mvm_free_skb(struct iwl_op_mode *op_mode, struct sk_buff *skb)
1552 {
1553 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1554 	struct ieee80211_tx_info *info;
1555 
1556 	info = IEEE80211_SKB_CB(skb);
1557 	iwl_trans_free_tx_cmd(mvm->trans, info->driver_data[1]);
1558 	ieee80211_free_txskb(mvm->hw, skb);
1559 }
1560 
1561 struct iwl_mvm_reprobe {
1562 	struct device *dev;
1563 	struct work_struct work;
1564 };
1565 
1566 static void iwl_mvm_reprobe_wk(struct work_struct *wk)
1567 {
1568 	struct iwl_mvm_reprobe *reprobe;
1569 
1570 	reprobe = container_of(wk, struct iwl_mvm_reprobe, work);
1571 	if (device_reprobe(reprobe->dev))
1572 		dev_err(reprobe->dev, "reprobe failed!\n");
1573 	put_device(reprobe->dev);
1574 	kfree(reprobe);
1575 	module_put(THIS_MODULE);
1576 }
1577 
1578 void iwl_mvm_nic_restart(struct iwl_mvm *mvm, bool fw_error)
1579 {
1580 	iwl_abort_notification_waits(&mvm->notif_wait);
1581 	iwl_dbg_tlv_del_timers(mvm->trans);
1582 
1583 	/*
1584 	 * This is a bit racy, but worst case we tell mac80211 about
1585 	 * a stopped/aborted scan when that was already done which
1586 	 * is not a problem. It is necessary to abort any os scan
1587 	 * here because mac80211 requires having the scan cleared
1588 	 * before restarting.
1589 	 * We'll reset the scan_status to NONE in restart cleanup in
1590 	 * the next start() call from mac80211. If restart isn't called
1591 	 * (no fw restart) scan status will stay busy.
1592 	 */
1593 	iwl_mvm_report_scan_aborted(mvm);
1594 
1595 	/*
1596 	 * If we're restarting already, don't cycle restarts.
1597 	 * If INIT fw asserted, it will likely fail again.
1598 	 * If WoWLAN fw asserted, don't restart either, mac80211
1599 	 * can't recover this since we're already half suspended.
1600 	 */
1601 	if (!mvm->fw_restart && fw_error) {
1602 		iwl_fw_error_collect(&mvm->fwrt, false);
1603 	} else if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1604 		struct iwl_mvm_reprobe *reprobe;
1605 
1606 		IWL_ERR(mvm,
1607 			"Firmware error during reconfiguration - reprobe!\n");
1608 
1609 		/*
1610 		 * get a module reference to avoid doing this while unloading
1611 		 * anyway and to avoid scheduling a work with code that's
1612 		 * being removed.
1613 		 */
1614 		if (!try_module_get(THIS_MODULE)) {
1615 			IWL_ERR(mvm, "Module is being unloaded - abort\n");
1616 			return;
1617 		}
1618 
1619 		reprobe = kzalloc(sizeof(*reprobe), GFP_ATOMIC);
1620 		if (!reprobe) {
1621 			module_put(THIS_MODULE);
1622 			return;
1623 		}
1624 		reprobe->dev = get_device(mvm->trans->dev);
1625 		INIT_WORK(&reprobe->work, iwl_mvm_reprobe_wk);
1626 		schedule_work(&reprobe->work);
1627 	} else if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
1628 			    &mvm->status)) {
1629 		IWL_ERR(mvm, "HW restart already requested, but not started\n");
1630 	} else if (mvm->fwrt.cur_fw_img == IWL_UCODE_REGULAR &&
1631 		   mvm->hw_registered &&
1632 		   !test_bit(STATUS_TRANS_DEAD, &mvm->trans->status)) {
1633 		/* This should be first thing before trying to collect any
1634 		 * data to avoid endless loops if any HW error happens while
1635 		 * collecting debug data.
1636 		 */
1637 		set_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
1638 
1639 		if (mvm->fw->ucode_capa.error_log_size) {
1640 			u32 src_size = mvm->fw->ucode_capa.error_log_size;
1641 			u32 src_addr = mvm->fw->ucode_capa.error_log_addr;
1642 			u8 *recover_buf = kzalloc(src_size, GFP_ATOMIC);
1643 
1644 			if (recover_buf) {
1645 				mvm->error_recovery_buf = recover_buf;
1646 				iwl_trans_read_mem_bytes(mvm->trans,
1647 							 src_addr,
1648 							 recover_buf,
1649 							 src_size);
1650 			}
1651 		}
1652 
1653 		iwl_fw_error_collect(&mvm->fwrt, false);
1654 
1655 		if (fw_error && mvm->fw_restart > 0)
1656 			mvm->fw_restart--;
1657 		ieee80211_restart_hw(mvm->hw);
1658 	}
1659 }
1660 
1661 static void iwl_mvm_nic_error(struct iwl_op_mode *op_mode, bool sync)
1662 {
1663 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1664 
1665 	if (!test_bit(STATUS_TRANS_DEAD, &mvm->trans->status))
1666 		iwl_mvm_dump_nic_error_log(mvm);
1667 
1668 	if (sync) {
1669 		iwl_fw_error_collect(&mvm->fwrt, true);
1670 		/*
1671 		 * Currently, the only case for sync=true is during
1672 		 * shutdown, so just stop in this case. If/when that
1673 		 * changes, we need to be a bit smarter here.
1674 		 */
1675 		return;
1676 	}
1677 
1678 	/*
1679 	 * If the firmware crashes while we're already considering it
1680 	 * to be dead then don't ask for a restart, that cannot do
1681 	 * anything useful anyway.
1682 	 */
1683 	if (!test_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status))
1684 		return;
1685 
1686 	iwl_mvm_nic_restart(mvm, true);
1687 }
1688 
1689 static void iwl_mvm_cmd_queue_full(struct iwl_op_mode *op_mode)
1690 {
1691 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1692 
1693 	WARN_ON(1);
1694 	iwl_mvm_nic_restart(mvm, true);
1695 }
1696 
1697 static void iwl_op_mode_mvm_time_point(struct iwl_op_mode *op_mode,
1698 				       enum iwl_fw_ini_time_point tp_id,
1699 				       union iwl_dbg_tlv_tp_data *tp_data)
1700 {
1701 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1702 
1703 	iwl_dbg_tlv_time_point(&mvm->fwrt, tp_id, tp_data);
1704 }
1705 
1706 #define IWL_MVM_COMMON_OPS					\
1707 	/* these could be differentiated */			\
1708 	.async_cb = iwl_mvm_async_cb,				\
1709 	.queue_full = iwl_mvm_stop_sw_queue,			\
1710 	.queue_not_full = iwl_mvm_wake_sw_queue,		\
1711 	.hw_rf_kill = iwl_mvm_set_hw_rfkill_state,		\
1712 	.free_skb = iwl_mvm_free_skb,				\
1713 	.nic_error = iwl_mvm_nic_error,				\
1714 	.cmd_queue_full = iwl_mvm_cmd_queue_full,		\
1715 	.nic_config = iwl_mvm_nic_config,			\
1716 	/* as we only register one, these MUST be common! */	\
1717 	.start = iwl_op_mode_mvm_start,				\
1718 	.stop = iwl_op_mode_mvm_stop,				\
1719 	.time_point = iwl_op_mode_mvm_time_point
1720 
1721 static const struct iwl_op_mode_ops iwl_mvm_ops = {
1722 	IWL_MVM_COMMON_OPS,
1723 	.rx = iwl_mvm_rx,
1724 };
1725 
1726 static void iwl_mvm_rx_mq_rss(struct iwl_op_mode *op_mode,
1727 			      struct napi_struct *napi,
1728 			      struct iwl_rx_cmd_buffer *rxb,
1729 			      unsigned int queue)
1730 {
1731 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1732 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1733 	u16 cmd = WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd);
1734 
1735 	if (unlikely(cmd == WIDE_ID(LEGACY_GROUP, FRAME_RELEASE)))
1736 		iwl_mvm_rx_frame_release(mvm, napi, rxb, queue);
1737 	else if (unlikely(cmd == WIDE_ID(DATA_PATH_GROUP,
1738 					 RX_QUEUES_NOTIFICATION)))
1739 		iwl_mvm_rx_queue_notif(mvm, napi, rxb, queue);
1740 	else if (likely(cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_MPDU_CMD)))
1741 		iwl_mvm_rx_mpdu_mq(mvm, napi, rxb, queue);
1742 }
1743 
1744 static const struct iwl_op_mode_ops iwl_mvm_ops_mq = {
1745 	IWL_MVM_COMMON_OPS,
1746 	.rx = iwl_mvm_rx_mq,
1747 	.rx_rss = iwl_mvm_rx_mq_rss,
1748 };
1749