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_AUTHOR(DRV_AUTHOR);
33 MODULE_LICENSE("GPL");
34 
35 static const struct iwl_op_mode_ops iwl_mvm_ops;
36 static const struct iwl_op_mode_ops iwl_mvm_ops_mq;
37 
38 struct iwl_mvm_mod_params iwlmvm_mod_params = {
39 	.power_scheme = IWL_POWER_SCHEME_BPS,
40 	/* rest of fields are 0 by default */
41 };
42 
43 module_param_named(init_dbg, iwlmvm_mod_params.init_dbg, bool, 0444);
44 MODULE_PARM_DESC(init_dbg,
45 		 "set to true to debug an ASSERT in INIT fw (default: false");
46 module_param_named(power_scheme, iwlmvm_mod_params.power_scheme, int, 0444);
47 MODULE_PARM_DESC(power_scheme,
48 		 "power management scheme: 1-active, 2-balanced, 3-low power, default: 2");
49 
50 /*
51  * module init and exit functions
52  */
53 static int __init iwl_mvm_init(void)
54 {
55 	int ret;
56 
57 	ret = iwl_mvm_rate_control_register();
58 	if (ret) {
59 		pr_err("Unable to register rate control algorithm: %d\n", ret);
60 		return ret;
61 	}
62 
63 	ret = iwl_opmode_register("iwlmvm", &iwl_mvm_ops);
64 	if (ret)
65 		pr_err("Unable to register MVM op_mode: %d\n", ret);
66 
67 	return ret;
68 }
69 module_init(iwl_mvm_init);
70 
71 static void __exit iwl_mvm_exit(void)
72 {
73 	iwl_opmode_deregister("iwlmvm");
74 	iwl_mvm_rate_control_unregister();
75 }
76 module_exit(iwl_mvm_exit);
77 
78 static void iwl_mvm_nic_config(struct iwl_op_mode *op_mode)
79 {
80 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
81 	struct iwl_trans_debug *dbg = &mvm->trans->dbg;
82 	u8 radio_cfg_type, radio_cfg_step, radio_cfg_dash;
83 	u32 reg_val = 0;
84 	u32 phy_config = iwl_mvm_get_phy_config(mvm);
85 
86 	radio_cfg_type = (phy_config & FW_PHY_CFG_RADIO_TYPE) >>
87 			 FW_PHY_CFG_RADIO_TYPE_POS;
88 	radio_cfg_step = (phy_config & FW_PHY_CFG_RADIO_STEP) >>
89 			 FW_PHY_CFG_RADIO_STEP_POS;
90 	radio_cfg_dash = (phy_config & FW_PHY_CFG_RADIO_DASH) >>
91 			 FW_PHY_CFG_RADIO_DASH_POS;
92 
93 	/* SKU control */
94 	reg_val |= CSR_HW_REV_STEP(mvm->trans->hw_rev) <<
95 				CSR_HW_IF_CONFIG_REG_POS_MAC_STEP;
96 	reg_val |= CSR_HW_REV_DASH(mvm->trans->hw_rev) <<
97 				CSR_HW_IF_CONFIG_REG_POS_MAC_DASH;
98 
99 	/* radio configuration */
100 	reg_val |= radio_cfg_type << CSR_HW_IF_CONFIG_REG_POS_PHY_TYPE;
101 	reg_val |= radio_cfg_step << CSR_HW_IF_CONFIG_REG_POS_PHY_STEP;
102 	reg_val |= radio_cfg_dash << CSR_HW_IF_CONFIG_REG_POS_PHY_DASH;
103 
104 	WARN_ON((radio_cfg_type << CSR_HW_IF_CONFIG_REG_POS_PHY_TYPE) &
105 		 ~CSR_HW_IF_CONFIG_REG_MSK_PHY_TYPE);
106 
107 	/*
108 	 * TODO: Bits 7-8 of CSR in 8000 HW family and higher set the ADC
109 	 * sampling, and shouldn't be set to any non-zero value.
110 	 * The same is supposed to be true of the other HW, but unsetting
111 	 * them (such as the 7260) causes automatic tests to fail on seemingly
112 	 * unrelated errors. Need to further investigate this, but for now
113 	 * we'll separate cases.
114 	 */
115 	if (mvm->trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_8000)
116 		reg_val |= CSR_HW_IF_CONFIG_REG_BIT_RADIO_SI;
117 
118 	if (iwl_fw_dbg_is_d3_debug_enabled(&mvm->fwrt) ||
119 	    (iwl_trans_dbg_ini_valid(mvm->trans) &&
120 	     dbg->fw_mon_cfg[IWL_FW_INI_ALLOCATION_ID_INTERNAL].buf_location)
121 	    )
122 		reg_val |= CSR_HW_IF_CONFIG_REG_D3_DEBUG;
123 
124 	iwl_trans_set_bits_mask(mvm->trans, CSR_HW_IF_CONFIG_REG,
125 				CSR_HW_IF_CONFIG_REG_MSK_MAC_DASH |
126 				CSR_HW_IF_CONFIG_REG_MSK_MAC_STEP |
127 				CSR_HW_IF_CONFIG_REG_MSK_PHY_TYPE |
128 				CSR_HW_IF_CONFIG_REG_MSK_PHY_STEP |
129 				CSR_HW_IF_CONFIG_REG_MSK_PHY_DASH |
130 				CSR_HW_IF_CONFIG_REG_BIT_RADIO_SI |
131 				CSR_HW_IF_CONFIG_REG_BIT_MAC_SI   |
132 				CSR_HW_IF_CONFIG_REG_D3_DEBUG,
133 				reg_val);
134 
135 	IWL_DEBUG_INFO(mvm, "Radio type=0x%x-0x%x-0x%x\n", radio_cfg_type,
136 		       radio_cfg_step, radio_cfg_dash);
137 
138 	/*
139 	 * W/A : NIC is stuck in a reset state after Early PCIe power off
140 	 * (PCIe power is lost before PERST# is asserted), causing ME FW
141 	 * to lose ownership and not being able to obtain it back.
142 	 */
143 	if (!mvm->trans->cfg->apmg_not_supported)
144 		iwl_set_bits_mask_prph(mvm->trans, APMG_PS_CTRL_REG,
145 				       APMG_PS_CTRL_EARLY_PWR_OFF_RESET_DIS,
146 				       ~APMG_PS_CTRL_EARLY_PWR_OFF_RESET_DIS);
147 }
148 
149 /**
150  * enum iwl_rx_handler_context context for Rx handler
151  * @RX_HANDLER_SYNC : this means that it will be called in the Rx path
152  *	which can't acquire mvm->mutex.
153  * @RX_HANDLER_ASYNC_LOCKED : If the handler needs to hold mvm->mutex
154  *	(and only in this case!), it should be set as ASYNC. In that case,
155  *	it will be called from a worker with mvm->mutex held.
156  * @RX_HANDLER_ASYNC_UNLOCKED : in case the handler needs to lock the
157  *	mutex itself, it will be called from a worker without mvm->mutex held.
158  */
159 enum iwl_rx_handler_context {
160 	RX_HANDLER_SYNC,
161 	RX_HANDLER_ASYNC_LOCKED,
162 	RX_HANDLER_ASYNC_UNLOCKED,
163 };
164 
165 /**
166  * struct iwl_rx_handlers handler for FW notification
167  * @cmd_id: command id
168  * @context: see &iwl_rx_handler_context
169  * @fn: the function is called when notification is received
170  */
171 struct iwl_rx_handlers {
172 	u16 cmd_id;
173 	enum iwl_rx_handler_context context;
174 	void (*fn)(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb);
175 };
176 
177 #define RX_HANDLER(_cmd_id, _fn, _context)	\
178 	{ .cmd_id = _cmd_id, .fn = _fn, .context = _context }
179 #define RX_HANDLER_GRP(_grp, _cmd, _fn, _context)	\
180 	{ .cmd_id = WIDE_ID(_grp, _cmd), .fn = _fn, .context = _context }
181 
182 /*
183  * Handlers for fw notifications
184  * Convention: RX_HANDLER(CMD_NAME, iwl_mvm_rx_CMD_NAME
185  * This list should be in order of frequency for performance purposes.
186  *
187  * The handler can be one from three contexts, see &iwl_rx_handler_context
188  */
189 static const struct iwl_rx_handlers iwl_mvm_rx_handlers[] = {
190 	RX_HANDLER(TX_CMD, iwl_mvm_rx_tx_cmd, RX_HANDLER_SYNC),
191 	RX_HANDLER(BA_NOTIF, iwl_mvm_rx_ba_notif, RX_HANDLER_SYNC),
192 
193 	RX_HANDLER_GRP(DATA_PATH_GROUP, TLC_MNG_UPDATE_NOTIF,
194 		       iwl_mvm_tlc_update_notif, RX_HANDLER_SYNC),
195 
196 	RX_HANDLER(BT_PROFILE_NOTIFICATION, iwl_mvm_rx_bt_coex_notif,
197 		   RX_HANDLER_ASYNC_LOCKED),
198 	RX_HANDLER(BEACON_NOTIFICATION, iwl_mvm_rx_beacon_notif,
199 		   RX_HANDLER_ASYNC_LOCKED),
200 	RX_HANDLER(STATISTICS_NOTIFICATION, iwl_mvm_rx_statistics,
201 		   RX_HANDLER_ASYNC_LOCKED),
202 
203 	RX_HANDLER(BA_WINDOW_STATUS_NOTIFICATION_ID,
204 		   iwl_mvm_window_status_notif, RX_HANDLER_SYNC),
205 
206 	RX_HANDLER(TIME_EVENT_NOTIFICATION, iwl_mvm_rx_time_event_notif,
207 		   RX_HANDLER_SYNC),
208 	RX_HANDLER_GRP(MAC_CONF_GROUP, SESSION_PROTECTION_NOTIF,
209 		       iwl_mvm_rx_session_protect_notif, RX_HANDLER_SYNC),
210 	RX_HANDLER(MCC_CHUB_UPDATE_CMD, iwl_mvm_rx_chub_update_mcc,
211 		   RX_HANDLER_ASYNC_LOCKED),
212 
213 	RX_HANDLER(EOSP_NOTIFICATION, iwl_mvm_rx_eosp_notif, RX_HANDLER_SYNC),
214 
215 	RX_HANDLER(SCAN_ITERATION_COMPLETE,
216 		   iwl_mvm_rx_lmac_scan_iter_complete_notif, RX_HANDLER_SYNC),
217 	RX_HANDLER(SCAN_OFFLOAD_COMPLETE,
218 		   iwl_mvm_rx_lmac_scan_complete_notif,
219 		   RX_HANDLER_ASYNC_LOCKED),
220 	RX_HANDLER(MATCH_FOUND_NOTIFICATION, iwl_mvm_rx_scan_match_found,
221 		   RX_HANDLER_SYNC),
222 	RX_HANDLER(SCAN_COMPLETE_UMAC, iwl_mvm_rx_umac_scan_complete_notif,
223 		   RX_HANDLER_ASYNC_LOCKED),
224 	RX_HANDLER(SCAN_ITERATION_COMPLETE_UMAC,
225 		   iwl_mvm_rx_umac_scan_iter_complete_notif, RX_HANDLER_SYNC),
226 
227 	RX_HANDLER(CARD_STATE_NOTIFICATION, iwl_mvm_rx_card_state_notif,
228 		   RX_HANDLER_SYNC),
229 
230 	RX_HANDLER(MISSED_BEACONS_NOTIFICATION, iwl_mvm_rx_missed_beacons_notif,
231 		   RX_HANDLER_SYNC),
232 
233 	RX_HANDLER(REPLY_ERROR, iwl_mvm_rx_fw_error, RX_HANDLER_SYNC),
234 	RX_HANDLER(PSM_UAPSD_AP_MISBEHAVING_NOTIFICATION,
235 		   iwl_mvm_power_uapsd_misbehaving_ap_notif, RX_HANDLER_SYNC),
236 	RX_HANDLER(DTS_MEASUREMENT_NOTIFICATION, iwl_mvm_temp_notif,
237 		   RX_HANDLER_ASYNC_LOCKED),
238 	RX_HANDLER_GRP(PHY_OPS_GROUP, DTS_MEASUREMENT_NOTIF_WIDE,
239 		       iwl_mvm_temp_notif, RX_HANDLER_ASYNC_UNLOCKED),
240 	RX_HANDLER_GRP(PHY_OPS_GROUP, CT_KILL_NOTIFICATION,
241 		       iwl_mvm_ct_kill_notif, RX_HANDLER_SYNC),
242 
243 	RX_HANDLER(TDLS_CHANNEL_SWITCH_NOTIFICATION, iwl_mvm_rx_tdls_notif,
244 		   RX_HANDLER_ASYNC_LOCKED),
245 	RX_HANDLER(MFUART_LOAD_NOTIFICATION, iwl_mvm_rx_mfuart_notif,
246 		   RX_HANDLER_SYNC),
247 	RX_HANDLER_GRP(LOCATION_GROUP, TOF_RESPONDER_STATS,
248 		       iwl_mvm_ftm_responder_stats, RX_HANDLER_ASYNC_LOCKED),
249 
250 	RX_HANDLER_GRP(LOCATION_GROUP, TOF_RANGE_RESPONSE_NOTIF,
251 		       iwl_mvm_ftm_range_resp, RX_HANDLER_ASYNC_LOCKED),
252 	RX_HANDLER_GRP(LOCATION_GROUP, TOF_LC_NOTIF,
253 		       iwl_mvm_ftm_lc_notif, RX_HANDLER_ASYNC_LOCKED),
254 
255 	RX_HANDLER_GRP(DEBUG_GROUP, MFU_ASSERT_DUMP_NTF,
256 		       iwl_mvm_mfu_assert_dump_notif, RX_HANDLER_SYNC),
257 	RX_HANDLER_GRP(PROT_OFFLOAD_GROUP, STORED_BEACON_NTF,
258 		       iwl_mvm_rx_stored_beacon_notif, RX_HANDLER_SYNC),
259 	RX_HANDLER_GRP(DATA_PATH_GROUP, MU_GROUP_MGMT_NOTIF,
260 		       iwl_mvm_mu_mimo_grp_notif, RX_HANDLER_SYNC),
261 	RX_HANDLER_GRP(DATA_PATH_GROUP, STA_PM_NOTIF,
262 		       iwl_mvm_sta_pm_notif, RX_HANDLER_SYNC),
263 	RX_HANDLER_GRP(MAC_CONF_GROUP, PROBE_RESPONSE_DATA_NOTIF,
264 		       iwl_mvm_probe_resp_data_notif,
265 		       RX_HANDLER_ASYNC_LOCKED),
266 	RX_HANDLER_GRP(MAC_CONF_GROUP, CHANNEL_SWITCH_NOA_NOTIF,
267 		       iwl_mvm_channel_switch_noa_notif,
268 		       RX_HANDLER_SYNC),
269 };
270 #undef RX_HANDLER
271 #undef RX_HANDLER_GRP
272 
273 /* Please keep this array *SORTED* by hex value.
274  * Access is done through binary search
275  */
276 static const struct iwl_hcmd_names iwl_mvm_legacy_names[] = {
277 	HCMD_NAME(UCODE_ALIVE_NTFY),
278 	HCMD_NAME(REPLY_ERROR),
279 	HCMD_NAME(ECHO_CMD),
280 	HCMD_NAME(INIT_COMPLETE_NOTIF),
281 	HCMD_NAME(PHY_CONTEXT_CMD),
282 	HCMD_NAME(DBG_CFG),
283 	HCMD_NAME(SCAN_CFG_CMD),
284 	HCMD_NAME(SCAN_REQ_UMAC),
285 	HCMD_NAME(SCAN_ABORT_UMAC),
286 	HCMD_NAME(SCAN_COMPLETE_UMAC),
287 	HCMD_NAME(BA_WINDOW_STATUS_NOTIFICATION_ID),
288 	HCMD_NAME(ADD_STA_KEY),
289 	HCMD_NAME(ADD_STA),
290 	HCMD_NAME(REMOVE_STA),
291 	HCMD_NAME(FW_GET_ITEM_CMD),
292 	HCMD_NAME(TX_CMD),
293 	HCMD_NAME(SCD_QUEUE_CFG),
294 	HCMD_NAME(TXPATH_FLUSH),
295 	HCMD_NAME(MGMT_MCAST_KEY),
296 	HCMD_NAME(WEP_KEY),
297 	HCMD_NAME(SHARED_MEM_CFG),
298 	HCMD_NAME(TDLS_CHANNEL_SWITCH_CMD),
299 	HCMD_NAME(MAC_CONTEXT_CMD),
300 	HCMD_NAME(TIME_EVENT_CMD),
301 	HCMD_NAME(TIME_EVENT_NOTIFICATION),
302 	HCMD_NAME(BINDING_CONTEXT_CMD),
303 	HCMD_NAME(TIME_QUOTA_CMD),
304 	HCMD_NAME(NON_QOS_TX_COUNTER_CMD),
305 	HCMD_NAME(LEDS_CMD),
306 	HCMD_NAME(LQ_CMD),
307 	HCMD_NAME(FW_PAGING_BLOCK_CMD),
308 	HCMD_NAME(SCAN_OFFLOAD_REQUEST_CMD),
309 	HCMD_NAME(SCAN_OFFLOAD_ABORT_CMD),
310 	HCMD_NAME(HOT_SPOT_CMD),
311 	HCMD_NAME(SCAN_OFFLOAD_PROFILES_QUERY_CMD),
312 	HCMD_NAME(BT_COEX_UPDATE_REDUCED_TXP),
313 	HCMD_NAME(BT_COEX_CI),
314 	HCMD_NAME(PHY_CONFIGURATION_CMD),
315 	HCMD_NAME(CALIB_RES_NOTIF_PHY_DB),
316 	HCMD_NAME(PHY_DB_CMD),
317 	HCMD_NAME(SCAN_OFFLOAD_COMPLETE),
318 	HCMD_NAME(SCAN_OFFLOAD_UPDATE_PROFILES_CMD),
319 	HCMD_NAME(POWER_TABLE_CMD),
320 	HCMD_NAME(PSM_UAPSD_AP_MISBEHAVING_NOTIFICATION),
321 	HCMD_NAME(REPLY_THERMAL_MNG_BACKOFF),
322 	HCMD_NAME(DC2DC_CONFIG_CMD),
323 	HCMD_NAME(NVM_ACCESS_CMD),
324 	HCMD_NAME(BEACON_NOTIFICATION),
325 	HCMD_NAME(BEACON_TEMPLATE_CMD),
326 	HCMD_NAME(TX_ANT_CONFIGURATION_CMD),
327 	HCMD_NAME(BT_CONFIG),
328 	HCMD_NAME(STATISTICS_CMD),
329 	HCMD_NAME(STATISTICS_NOTIFICATION),
330 	HCMD_NAME(EOSP_NOTIFICATION),
331 	HCMD_NAME(REDUCE_TX_POWER_CMD),
332 	HCMD_NAME(CARD_STATE_NOTIFICATION),
333 	HCMD_NAME(MISSED_BEACONS_NOTIFICATION),
334 	HCMD_NAME(TDLS_CONFIG_CMD),
335 	HCMD_NAME(MAC_PM_POWER_TABLE),
336 	HCMD_NAME(TDLS_CHANNEL_SWITCH_NOTIFICATION),
337 	HCMD_NAME(MFUART_LOAD_NOTIFICATION),
338 	HCMD_NAME(RSS_CONFIG_CMD),
339 	HCMD_NAME(SCAN_ITERATION_COMPLETE_UMAC),
340 	HCMD_NAME(REPLY_RX_PHY_CMD),
341 	HCMD_NAME(REPLY_RX_MPDU_CMD),
342 	HCMD_NAME(BAR_FRAME_RELEASE),
343 	HCMD_NAME(FRAME_RELEASE),
344 	HCMD_NAME(BA_NOTIF),
345 	HCMD_NAME(MCC_UPDATE_CMD),
346 	HCMD_NAME(MCC_CHUB_UPDATE_CMD),
347 	HCMD_NAME(MARKER_CMD),
348 	HCMD_NAME(BT_PROFILE_NOTIFICATION),
349 	HCMD_NAME(BCAST_FILTER_CMD),
350 	HCMD_NAME(MCAST_FILTER_CMD),
351 	HCMD_NAME(REPLY_SF_CFG_CMD),
352 	HCMD_NAME(REPLY_BEACON_FILTERING_CMD),
353 	HCMD_NAME(D3_CONFIG_CMD),
354 	HCMD_NAME(PROT_OFFLOAD_CONFIG_CMD),
355 	HCMD_NAME(OFFLOADS_QUERY_CMD),
356 	HCMD_NAME(REMOTE_WAKE_CONFIG_CMD),
357 	HCMD_NAME(MATCH_FOUND_NOTIFICATION),
358 	HCMD_NAME(DTS_MEASUREMENT_NOTIFICATION),
359 	HCMD_NAME(WOWLAN_PATTERNS),
360 	HCMD_NAME(WOWLAN_CONFIGURATION),
361 	HCMD_NAME(WOWLAN_TSC_RSC_PARAM),
362 	HCMD_NAME(WOWLAN_TKIP_PARAM),
363 	HCMD_NAME(WOWLAN_KEK_KCK_MATERIAL),
364 	HCMD_NAME(WOWLAN_GET_STATUSES),
365 	HCMD_NAME(SCAN_ITERATION_COMPLETE),
366 	HCMD_NAME(D0I3_END_CMD),
367 	HCMD_NAME(LTR_CONFIG),
368 	HCMD_NAME(LDBG_CONFIG_CMD),
369 };
370 
371 /* Please keep this array *SORTED* by hex value.
372  * Access is done through binary search
373  */
374 static const struct iwl_hcmd_names iwl_mvm_system_names[] = {
375 	HCMD_NAME(SHARED_MEM_CFG_CMD),
376 	HCMD_NAME(INIT_EXTENDED_CFG_CMD),
377 	HCMD_NAME(FW_ERROR_RECOVERY_CMD),
378 };
379 
380 /* Please keep this array *SORTED* by hex value.
381  * Access is done through binary search
382  */
383 static const struct iwl_hcmd_names iwl_mvm_mac_conf_names[] = {
384 	HCMD_NAME(CHANNEL_SWITCH_TIME_EVENT_CMD),
385 	HCMD_NAME(SESSION_PROTECTION_CMD),
386 	HCMD_NAME(SESSION_PROTECTION_NOTIF),
387 	HCMD_NAME(CHANNEL_SWITCH_NOA_NOTIF),
388 };
389 
390 /* Please keep this array *SORTED* by hex value.
391  * Access is done through binary search
392  */
393 static const struct iwl_hcmd_names iwl_mvm_phy_names[] = {
394 	HCMD_NAME(CMD_DTS_MEASUREMENT_TRIGGER_WIDE),
395 	HCMD_NAME(CTDP_CONFIG_CMD),
396 	HCMD_NAME(TEMP_REPORTING_THRESHOLDS_CMD),
397 	HCMD_NAME(GEO_TX_POWER_LIMIT),
398 	HCMD_NAME(CT_KILL_NOTIFICATION),
399 	HCMD_NAME(DTS_MEASUREMENT_NOTIF_WIDE),
400 };
401 
402 /* Please keep this array *SORTED* by hex value.
403  * Access is done through binary search
404  */
405 static const struct iwl_hcmd_names iwl_mvm_data_path_names[] = {
406 	HCMD_NAME(DQA_ENABLE_CMD),
407 	HCMD_NAME(UPDATE_MU_GROUPS_CMD),
408 	HCMD_NAME(TRIGGER_RX_QUEUES_NOTIF_CMD),
409 	HCMD_NAME(STA_HE_CTXT_CMD),
410 	HCMD_NAME(RFH_QUEUE_CONFIG_CMD),
411 	HCMD_NAME(TLC_MNG_CONFIG_CMD),
412 	HCMD_NAME(CHEST_COLLECTOR_FILTER_CONFIG_CMD),
413 	HCMD_NAME(STA_PM_NOTIF),
414 	HCMD_NAME(MU_GROUP_MGMT_NOTIF),
415 	HCMD_NAME(RX_QUEUES_NOTIFICATION),
416 };
417 
418 /* Please keep this array *SORTED* by hex value.
419  * Access is done through binary search
420  */
421 static const struct iwl_hcmd_names iwl_mvm_location_names[] = {
422 	HCMD_NAME(TOF_RANGE_REQ_CMD),
423 	HCMD_NAME(TOF_CONFIG_CMD),
424 	HCMD_NAME(TOF_RANGE_ABORT_CMD),
425 	HCMD_NAME(TOF_RANGE_REQ_EXT_CMD),
426 	HCMD_NAME(TOF_RESPONDER_CONFIG_CMD),
427 	HCMD_NAME(TOF_RESPONDER_DYN_CONFIG_CMD),
428 	HCMD_NAME(TOF_LC_NOTIF),
429 	HCMD_NAME(TOF_RESPONDER_STATS),
430 	HCMD_NAME(TOF_MCSI_DEBUG_NOTIF),
431 	HCMD_NAME(TOF_RANGE_RESPONSE_NOTIF),
432 };
433 
434 /* Please keep this array *SORTED* by hex value.
435  * Access is done through binary search
436  */
437 static const struct iwl_hcmd_names iwl_mvm_prot_offload_names[] = {
438 	HCMD_NAME(STORED_BEACON_NTF),
439 };
440 
441 /* Please keep this array *SORTED* by hex value.
442  * Access is done through binary search
443  */
444 static const struct iwl_hcmd_names iwl_mvm_regulatory_and_nvm_names[] = {
445 	HCMD_NAME(NVM_ACCESS_COMPLETE),
446 	HCMD_NAME(NVM_GET_INFO),
447 	HCMD_NAME(TAS_CONFIG),
448 };
449 
450 static const struct iwl_hcmd_arr iwl_mvm_groups[] = {
451 	[LEGACY_GROUP] = HCMD_ARR(iwl_mvm_legacy_names),
452 	[LONG_GROUP] = HCMD_ARR(iwl_mvm_legacy_names),
453 	[SYSTEM_GROUP] = HCMD_ARR(iwl_mvm_system_names),
454 	[MAC_CONF_GROUP] = HCMD_ARR(iwl_mvm_mac_conf_names),
455 	[PHY_OPS_GROUP] = HCMD_ARR(iwl_mvm_phy_names),
456 	[DATA_PATH_GROUP] = HCMD_ARR(iwl_mvm_data_path_names),
457 	[LOCATION_GROUP] = HCMD_ARR(iwl_mvm_location_names),
458 	[PROT_OFFLOAD_GROUP] = HCMD_ARR(iwl_mvm_prot_offload_names),
459 	[REGULATORY_AND_NVM_GROUP] =
460 		HCMD_ARR(iwl_mvm_regulatory_and_nvm_names),
461 };
462 
463 /* this forward declaration can avoid to export the function */
464 static void iwl_mvm_async_handlers_wk(struct work_struct *wk);
465 
466 static u32 iwl_mvm_min_backoff(struct iwl_mvm *mvm)
467 {
468 	const struct iwl_pwr_tx_backoff *backoff = mvm->cfg->pwr_tx_backoffs;
469 	u64 dflt_pwr_limit;
470 
471 	if (!backoff)
472 		return 0;
473 
474 	dflt_pwr_limit = iwl_acpi_get_pwr_limit(mvm->dev);
475 
476 	while (backoff->pwr) {
477 		if (dflt_pwr_limit >= backoff->pwr)
478 			return backoff->backoff;
479 
480 		backoff++;
481 	}
482 
483 	return 0;
484 }
485 
486 static void iwl_mvm_tx_unblock_dwork(struct work_struct *work)
487 {
488 	struct iwl_mvm *mvm =
489 		container_of(work, struct iwl_mvm, cs_tx_unblock_dwork.work);
490 	struct ieee80211_vif *tx_blocked_vif;
491 	struct iwl_mvm_vif *mvmvif;
492 
493 	mutex_lock(&mvm->mutex);
494 
495 	tx_blocked_vif =
496 		rcu_dereference_protected(mvm->csa_tx_blocked_vif,
497 					  lockdep_is_held(&mvm->mutex));
498 
499 	if (!tx_blocked_vif)
500 		goto unlock;
501 
502 	mvmvif = iwl_mvm_vif_from_mac80211(tx_blocked_vif);
503 	iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
504 	RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
505 unlock:
506 	mutex_unlock(&mvm->mutex);
507 }
508 
509 static int iwl_mvm_fwrt_dump_start(void *ctx)
510 {
511 	struct iwl_mvm *mvm = ctx;
512 
513 	mutex_lock(&mvm->mutex);
514 
515 	return 0;
516 }
517 
518 static void iwl_mvm_fwrt_dump_end(void *ctx)
519 {
520 	struct iwl_mvm *mvm = ctx;
521 
522 	mutex_unlock(&mvm->mutex);
523 }
524 
525 static bool iwl_mvm_fwrt_fw_running(void *ctx)
526 {
527 	return iwl_mvm_firmware_running(ctx);
528 }
529 
530 static int iwl_mvm_fwrt_send_hcmd(void *ctx, struct iwl_host_cmd *host_cmd)
531 {
532 	struct iwl_mvm *mvm = (struct iwl_mvm *)ctx;
533 	int ret;
534 
535 	mutex_lock(&mvm->mutex);
536 	ret = iwl_mvm_send_cmd(mvm, host_cmd);
537 	mutex_unlock(&mvm->mutex);
538 
539 	return ret;
540 }
541 
542 static bool iwl_mvm_d3_debug_enable(void *ctx)
543 {
544 	return IWL_MVM_D3_DEBUG;
545 }
546 
547 static const struct iwl_fw_runtime_ops iwl_mvm_fwrt_ops = {
548 	.dump_start = iwl_mvm_fwrt_dump_start,
549 	.dump_end = iwl_mvm_fwrt_dump_end,
550 	.fw_running = iwl_mvm_fwrt_fw_running,
551 	.send_hcmd = iwl_mvm_fwrt_send_hcmd,
552 	.d3_debug_enable = iwl_mvm_d3_debug_enable,
553 };
554 
555 static struct iwl_op_mode *
556 iwl_op_mode_mvm_start(struct iwl_trans *trans, const struct iwl_cfg *cfg,
557 		      const struct iwl_fw *fw, struct dentry *dbgfs_dir)
558 {
559 	struct ieee80211_hw *hw;
560 	struct iwl_op_mode *op_mode;
561 	struct iwl_mvm *mvm;
562 	struct iwl_trans_config trans_cfg = {};
563 	static const u8 no_reclaim_cmds[] = {
564 		TX_CMD,
565 	};
566 	int err, scan_size;
567 	u32 min_backoff;
568 	enum iwl_amsdu_size rb_size_default;
569 
570 	/*
571 	 * We use IWL_MVM_STATION_COUNT_MAX to check the validity of the station
572 	 * index all over the driver - check that its value corresponds to the
573 	 * array size.
574 	 */
575 	BUILD_BUG_ON(ARRAY_SIZE(mvm->fw_id_to_mac_id) !=
576 		     IWL_MVM_STATION_COUNT_MAX);
577 
578 	/********************************
579 	 * 1. Allocating and configuring HW data
580 	 ********************************/
581 	hw = ieee80211_alloc_hw(sizeof(struct iwl_op_mode) +
582 				sizeof(struct iwl_mvm),
583 				&iwl_mvm_hw_ops);
584 	if (!hw)
585 		return NULL;
586 
587 	hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF;
588 
589 	if (cfg->max_tx_agg_size)
590 		hw->max_tx_aggregation_subframes = cfg->max_tx_agg_size;
591 	else
592 		hw->max_tx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF;
593 
594 	op_mode = hw->priv;
595 
596 	mvm = IWL_OP_MODE_GET_MVM(op_mode);
597 	mvm->dev = trans->dev;
598 	mvm->trans = trans;
599 	mvm->cfg = cfg;
600 	mvm->fw = fw;
601 	mvm->hw = hw;
602 
603 	iwl_fw_runtime_init(&mvm->fwrt, trans, fw, &iwl_mvm_fwrt_ops, mvm,
604 			    dbgfs_dir);
605 
606 	mvm->init_status = 0;
607 
608 	if (iwl_mvm_has_new_rx_api(mvm)) {
609 		op_mode->ops = &iwl_mvm_ops_mq;
610 		trans->rx_mpdu_cmd_hdr_size =
611 			(trans->trans_cfg->device_family >=
612 			 IWL_DEVICE_FAMILY_AX210) ?
613 			sizeof(struct iwl_rx_mpdu_desc) :
614 			IWL_RX_DESC_SIZE_V1;
615 	} else {
616 		op_mode->ops = &iwl_mvm_ops;
617 		trans->rx_mpdu_cmd_hdr_size =
618 			sizeof(struct iwl_rx_mpdu_res_start);
619 
620 		if (WARN_ON(trans->num_rx_queues > 1))
621 			goto out_free;
622 	}
623 
624 	mvm->fw_restart = iwlwifi_mod_params.fw_restart ? -1 : 0;
625 
626 	mvm->aux_queue = IWL_MVM_DQA_AUX_QUEUE;
627 	mvm->snif_queue = IWL_MVM_DQA_INJECT_MONITOR_QUEUE;
628 	mvm->probe_queue = IWL_MVM_DQA_AP_PROBE_RESP_QUEUE;
629 	mvm->p2p_dev_queue = IWL_MVM_DQA_P2P_DEVICE_QUEUE;
630 
631 	mvm->sf_state = SF_UNINIT;
632 	if (iwl_mvm_has_unified_ucode(mvm))
633 		iwl_fw_set_current_image(&mvm->fwrt, IWL_UCODE_REGULAR);
634 	else
635 		iwl_fw_set_current_image(&mvm->fwrt, IWL_UCODE_INIT);
636 	mvm->drop_bcn_ap_mode = true;
637 
638 	mutex_init(&mvm->mutex);
639 	spin_lock_init(&mvm->async_handlers_lock);
640 	INIT_LIST_HEAD(&mvm->time_event_list);
641 	INIT_LIST_HEAD(&mvm->aux_roc_te_list);
642 	INIT_LIST_HEAD(&mvm->async_handlers_list);
643 	spin_lock_init(&mvm->time_event_lock);
644 	INIT_LIST_HEAD(&mvm->ftm_initiator.loc_list);
645 	INIT_LIST_HEAD(&mvm->ftm_initiator.pasn_list);
646 	INIT_LIST_HEAD(&mvm->resp_pasn_list);
647 
648 	INIT_WORK(&mvm->async_handlers_wk, iwl_mvm_async_handlers_wk);
649 	INIT_WORK(&mvm->roc_done_wk, iwl_mvm_roc_done_wk);
650 	INIT_DELAYED_WORK(&mvm->tdls_cs.dwork, iwl_mvm_tdls_ch_switch_work);
651 	INIT_DELAYED_WORK(&mvm->scan_timeout_dwork, iwl_mvm_scan_timeout_wk);
652 	INIT_WORK(&mvm->add_stream_wk, iwl_mvm_add_new_dqa_stream_wk);
653 	INIT_LIST_HEAD(&mvm->add_stream_txqs);
654 
655 	init_waitqueue_head(&mvm->rx_sync_waitq);
656 
657 	mvm->queue_sync_state = 0;
658 
659 	SET_IEEE80211_DEV(mvm->hw, mvm->trans->dev);
660 
661 	spin_lock_init(&mvm->tcm.lock);
662 	INIT_DELAYED_WORK(&mvm->tcm.work, iwl_mvm_tcm_work);
663 	mvm->tcm.ts = jiffies;
664 	mvm->tcm.ll_ts = jiffies;
665 	mvm->tcm.uapsd_nonagg_ts = jiffies;
666 
667 	INIT_DELAYED_WORK(&mvm->cs_tx_unblock_dwork, iwl_mvm_tx_unblock_dwork);
668 
669 	mvm->cmd_ver.d0i3_resp =
670 		iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP, D0I3_END_CMD,
671 					0);
672 	/* we only support version 1 */
673 	if (WARN_ON_ONCE(mvm->cmd_ver.d0i3_resp > 1))
674 		goto out_free;
675 
676 	mvm->cmd_ver.range_resp =
677 		iwl_fw_lookup_notif_ver(mvm->fw, LOCATION_GROUP,
678 					TOF_RANGE_RESPONSE_NOTIF, 5);
679 	/* we only support up to version 8 */
680 	if (WARN_ON_ONCE(mvm->cmd_ver.range_resp > 8))
681 		goto out_free;
682 
683 	/*
684 	 * Populate the state variables that the transport layer needs
685 	 * to know about.
686 	 */
687 	trans_cfg.op_mode = op_mode;
688 	trans_cfg.no_reclaim_cmds = no_reclaim_cmds;
689 	trans_cfg.n_no_reclaim_cmds = ARRAY_SIZE(no_reclaim_cmds);
690 
691 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
692 		rb_size_default = IWL_AMSDU_2K;
693 	else
694 		rb_size_default = IWL_AMSDU_4K;
695 
696 	switch (iwlwifi_mod_params.amsdu_size) {
697 	case IWL_AMSDU_DEF:
698 		trans_cfg.rx_buf_size = rb_size_default;
699 		break;
700 	case IWL_AMSDU_4K:
701 		trans_cfg.rx_buf_size = IWL_AMSDU_4K;
702 		break;
703 	case IWL_AMSDU_8K:
704 		trans_cfg.rx_buf_size = IWL_AMSDU_8K;
705 		break;
706 	case IWL_AMSDU_12K:
707 		trans_cfg.rx_buf_size = IWL_AMSDU_12K;
708 		break;
709 	default:
710 		pr_err("%s: Unsupported amsdu_size: %d\n", KBUILD_MODNAME,
711 		       iwlwifi_mod_params.amsdu_size);
712 		trans_cfg.rx_buf_size = rb_size_default;
713 	}
714 
715 	trans->wide_cmd_header = true;
716 	trans_cfg.bc_table_dword =
717 		mvm->trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_AX210;
718 
719 	trans_cfg.command_groups = iwl_mvm_groups;
720 	trans_cfg.command_groups_size = ARRAY_SIZE(iwl_mvm_groups);
721 
722 	trans_cfg.cmd_queue = IWL_MVM_DQA_CMD_QUEUE;
723 	trans_cfg.cmd_fifo = IWL_MVM_TX_FIFO_CMD;
724 	trans_cfg.scd_set_active = true;
725 
726 	trans_cfg.cb_data_offs = offsetof(struct ieee80211_tx_info,
727 					  driver_data[2]);
728 
729 	/* Set a short watchdog for the command queue */
730 	trans_cfg.cmd_q_wdg_timeout =
731 		iwl_mvm_get_wd_timeout(mvm, NULL, false, true);
732 
733 	snprintf(mvm->hw->wiphy->fw_version,
734 		 sizeof(mvm->hw->wiphy->fw_version),
735 		 "%s", fw->fw_version);
736 
737 	trans_cfg.fw_reset_handshake = fw_has_capa(&mvm->fw->ucode_capa,
738 						   IWL_UCODE_TLV_CAPA_FW_RESET_HANDSHAKE);
739 
740 	/* Configure transport layer */
741 	iwl_trans_configure(mvm->trans, &trans_cfg);
742 
743 	trans->rx_mpdu_cmd = REPLY_RX_MPDU_CMD;
744 	trans->dbg.dest_tlv = mvm->fw->dbg.dest_tlv;
745 	trans->dbg.n_dest_reg = mvm->fw->dbg.n_dest_reg;
746 	memcpy(trans->dbg.conf_tlv, mvm->fw->dbg.conf_tlv,
747 	       sizeof(trans->dbg.conf_tlv));
748 	trans->dbg.trigger_tlv = mvm->fw->dbg.trigger_tlv;
749 
750 	trans->iml = mvm->fw->iml;
751 	trans->iml_len = mvm->fw->iml_len;
752 
753 	/* set up notification wait support */
754 	iwl_notification_wait_init(&mvm->notif_wait);
755 
756 	/* Init phy db */
757 	mvm->phy_db = iwl_phy_db_init(trans);
758 	if (!mvm->phy_db) {
759 		IWL_ERR(mvm, "Cannot init phy_db\n");
760 		goto out_free;
761 	}
762 
763 	IWL_INFO(mvm, "Detected %s, REV=0x%X\n",
764 		 mvm->trans->name, mvm->trans->hw_rev);
765 
766 	if (iwlwifi_mod_params.nvm_file)
767 		mvm->nvm_file_name = iwlwifi_mod_params.nvm_file;
768 	else
769 		IWL_DEBUG_EEPROM(mvm->trans->dev,
770 				 "working without external nvm file\n");
771 
772 	err = iwl_trans_start_hw(mvm->trans);
773 	if (err)
774 		goto out_free;
775 
776 	mutex_lock(&mvm->mutex);
777 	err = iwl_run_init_mvm_ucode(mvm);
778 	if (err && err != -ERFKILL)
779 		iwl_fw_dbg_error_collect(&mvm->fwrt, FW_DBG_TRIGGER_DRIVER);
780 	if (!iwlmvm_mod_params.init_dbg || !err)
781 		iwl_mvm_stop_device(mvm);
782 	mutex_unlock(&mvm->mutex);
783 	if (err < 0) {
784 		IWL_ERR(mvm, "Failed to run INIT ucode: %d\n", err);
785 		goto out_free;
786 	}
787 
788 	scan_size = iwl_mvm_scan_size(mvm);
789 
790 	mvm->scan_cmd = kmalloc(scan_size, GFP_KERNEL);
791 	if (!mvm->scan_cmd)
792 		goto out_free;
793 
794 	/* invalidate ids to prevent accidental removal of sta_id 0 */
795 	mvm->aux_sta.sta_id = IWL_MVM_INVALID_STA;
796 	mvm->snif_sta.sta_id = IWL_MVM_INVALID_STA;
797 
798 	/* Set EBS as successful as long as not stated otherwise by the FW. */
799 	mvm->last_ebs_successful = true;
800 
801 	err = iwl_mvm_mac_setup_register(mvm);
802 	if (err)
803 		goto out_free;
804 	mvm->hw_registered = true;
805 
806 	min_backoff = iwl_mvm_min_backoff(mvm);
807 	iwl_mvm_thermal_initialize(mvm, min_backoff);
808 
809 	iwl_mvm_dbgfs_register(mvm, dbgfs_dir);
810 
811 	if (!iwl_mvm_has_new_rx_stats_api(mvm))
812 		memset(&mvm->rx_stats_v3, 0,
813 		       sizeof(struct mvm_statistics_rx_v3));
814 	else
815 		memset(&mvm->rx_stats, 0, sizeof(struct mvm_statistics_rx));
816 
817 	iwl_mvm_toggle_tx_ant(mvm, &mvm->mgmt_last_antenna_idx);
818 
819 	return op_mode;
820 
821  out_free:
822 	iwl_fw_flush_dumps(&mvm->fwrt);
823 	iwl_fw_runtime_free(&mvm->fwrt);
824 
825 	if (iwlmvm_mod_params.init_dbg)
826 		return op_mode;
827 	iwl_phy_db_free(mvm->phy_db);
828 	kfree(mvm->scan_cmd);
829 	iwl_trans_op_mode_leave(trans);
830 
831 	ieee80211_free_hw(mvm->hw);
832 	return NULL;
833 }
834 
835 void iwl_mvm_stop_device(struct iwl_mvm *mvm)
836 {
837 	lockdep_assert_held(&mvm->mutex);
838 
839 	iwl_fw_cancel_timestamp(&mvm->fwrt);
840 
841 	clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
842 
843 	iwl_fw_dbg_stop_sync(&mvm->fwrt);
844 	iwl_trans_stop_device(mvm->trans);
845 	iwl_free_fw_paging(&mvm->fwrt);
846 	iwl_fw_dump_conf_clear(&mvm->fwrt);
847 }
848 
849 static void iwl_op_mode_mvm_stop(struct iwl_op_mode *op_mode)
850 {
851 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
852 	int i;
853 
854 	iwl_mvm_leds_exit(mvm);
855 
856 	iwl_mvm_thermal_exit(mvm);
857 
858 	ieee80211_unregister_hw(mvm->hw);
859 
860 	kfree(mvm->scan_cmd);
861 	kfree(mvm->mcast_filter_cmd);
862 	mvm->mcast_filter_cmd = NULL;
863 
864 	kfree(mvm->error_recovery_buf);
865 	mvm->error_recovery_buf = NULL;
866 
867 	iwl_trans_op_mode_leave(mvm->trans);
868 
869 	iwl_phy_db_free(mvm->phy_db);
870 	mvm->phy_db = NULL;
871 
872 	kfree(mvm->nvm_data);
873 	for (i = 0; i < NVM_MAX_NUM_SECTIONS; i++)
874 		kfree(mvm->nvm_sections[i].data);
875 
876 	cancel_delayed_work_sync(&mvm->tcm.work);
877 
878 	iwl_fw_runtime_free(&mvm->fwrt);
879 	mutex_destroy(&mvm->mutex);
880 
881 	ieee80211_free_hw(mvm->hw);
882 }
883 
884 struct iwl_async_handler_entry {
885 	struct list_head list;
886 	struct iwl_rx_cmd_buffer rxb;
887 	enum iwl_rx_handler_context context;
888 	void (*fn)(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb);
889 };
890 
891 void iwl_mvm_async_handlers_purge(struct iwl_mvm *mvm)
892 {
893 	struct iwl_async_handler_entry *entry, *tmp;
894 
895 	spin_lock_bh(&mvm->async_handlers_lock);
896 	list_for_each_entry_safe(entry, tmp, &mvm->async_handlers_list, list) {
897 		iwl_free_rxb(&entry->rxb);
898 		list_del(&entry->list);
899 		kfree(entry);
900 	}
901 	spin_unlock_bh(&mvm->async_handlers_lock);
902 }
903 
904 static void iwl_mvm_async_handlers_wk(struct work_struct *wk)
905 {
906 	struct iwl_mvm *mvm =
907 		container_of(wk, struct iwl_mvm, async_handlers_wk);
908 	struct iwl_async_handler_entry *entry, *tmp;
909 	LIST_HEAD(local_list);
910 
911 	/* Ensure that we are not in stop flow (check iwl_mvm_mac_stop) */
912 
913 	/*
914 	 * Sync with Rx path with a lock. Remove all the entries from this list,
915 	 * add them to a local one (lock free), and then handle them.
916 	 */
917 	spin_lock_bh(&mvm->async_handlers_lock);
918 	list_splice_init(&mvm->async_handlers_list, &local_list);
919 	spin_unlock_bh(&mvm->async_handlers_lock);
920 
921 	list_for_each_entry_safe(entry, tmp, &local_list, list) {
922 		if (entry->context == RX_HANDLER_ASYNC_LOCKED)
923 			mutex_lock(&mvm->mutex);
924 		entry->fn(mvm, &entry->rxb);
925 		iwl_free_rxb(&entry->rxb);
926 		list_del(&entry->list);
927 		if (entry->context == RX_HANDLER_ASYNC_LOCKED)
928 			mutex_unlock(&mvm->mutex);
929 		kfree(entry);
930 	}
931 }
932 
933 static inline void iwl_mvm_rx_check_trigger(struct iwl_mvm *mvm,
934 					    struct iwl_rx_packet *pkt)
935 {
936 	struct iwl_fw_dbg_trigger_tlv *trig;
937 	struct iwl_fw_dbg_trigger_cmd *cmds_trig;
938 	int i;
939 
940 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, NULL,
941 				     FW_DBG_TRIGGER_FW_NOTIF);
942 	if (!trig)
943 		return;
944 
945 	cmds_trig = (void *)trig->data;
946 
947 	for (i = 0; i < ARRAY_SIZE(cmds_trig->cmds); i++) {
948 		/* don't collect on CMD 0 */
949 		if (!cmds_trig->cmds[i].cmd_id)
950 			break;
951 
952 		if (cmds_trig->cmds[i].cmd_id != pkt->hdr.cmd ||
953 		    cmds_trig->cmds[i].group_id != pkt->hdr.group_id)
954 			continue;
955 
956 		iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
957 					"CMD 0x%02x.%02x received",
958 					pkt->hdr.group_id, pkt->hdr.cmd);
959 		break;
960 	}
961 }
962 
963 static void iwl_mvm_rx_common(struct iwl_mvm *mvm,
964 			      struct iwl_rx_cmd_buffer *rxb,
965 			      struct iwl_rx_packet *pkt)
966 {
967 	int i;
968 	union iwl_dbg_tlv_tp_data tp_data = { .fw_pkt = pkt };
969 
970 	iwl_dbg_tlv_time_point(&mvm->fwrt,
971 			       IWL_FW_INI_TIME_POINT_FW_RSP_OR_NOTIF, &tp_data);
972 	iwl_mvm_rx_check_trigger(mvm, pkt);
973 
974 	/*
975 	 * Do the notification wait before RX handlers so
976 	 * even if the RX handler consumes the RXB we have
977 	 * access to it in the notification wait entry.
978 	 */
979 	iwl_notification_wait_notify(&mvm->notif_wait, pkt);
980 
981 	for (i = 0; i < ARRAY_SIZE(iwl_mvm_rx_handlers); i++) {
982 		const struct iwl_rx_handlers *rx_h = &iwl_mvm_rx_handlers[i];
983 		struct iwl_async_handler_entry *entry;
984 
985 		if (rx_h->cmd_id != WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd))
986 			continue;
987 
988 		if (rx_h->context == RX_HANDLER_SYNC) {
989 			rx_h->fn(mvm, rxb);
990 			return;
991 		}
992 
993 		entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
994 		/* we can't do much... */
995 		if (!entry)
996 			return;
997 
998 		entry->rxb._page = rxb_steal_page(rxb);
999 		entry->rxb._offset = rxb->_offset;
1000 		entry->rxb._rx_page_order = rxb->_rx_page_order;
1001 		entry->fn = rx_h->fn;
1002 		entry->context = rx_h->context;
1003 		spin_lock(&mvm->async_handlers_lock);
1004 		list_add_tail(&entry->list, &mvm->async_handlers_list);
1005 		spin_unlock(&mvm->async_handlers_lock);
1006 		schedule_work(&mvm->async_handlers_wk);
1007 		break;
1008 	}
1009 }
1010 
1011 static void iwl_mvm_rx(struct iwl_op_mode *op_mode,
1012 		       struct napi_struct *napi,
1013 		       struct iwl_rx_cmd_buffer *rxb)
1014 {
1015 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1016 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1017 	u16 cmd = WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd);
1018 
1019 	if (likely(cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_MPDU_CMD)))
1020 		iwl_mvm_rx_rx_mpdu(mvm, napi, rxb);
1021 	else if (cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_PHY_CMD))
1022 		iwl_mvm_rx_rx_phy_cmd(mvm, rxb);
1023 	else
1024 		iwl_mvm_rx_common(mvm, rxb, pkt);
1025 }
1026 
1027 static void iwl_mvm_rx_mq(struct iwl_op_mode *op_mode,
1028 			  struct napi_struct *napi,
1029 			  struct iwl_rx_cmd_buffer *rxb)
1030 {
1031 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1032 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1033 	u16 cmd = WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd);
1034 
1035 	if (likely(cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_MPDU_CMD)))
1036 		iwl_mvm_rx_mpdu_mq(mvm, napi, rxb, 0);
1037 	else if (unlikely(cmd == WIDE_ID(DATA_PATH_GROUP,
1038 					 RX_QUEUES_NOTIFICATION)))
1039 		iwl_mvm_rx_queue_notif(mvm, napi, rxb, 0);
1040 	else if (cmd == WIDE_ID(LEGACY_GROUP, FRAME_RELEASE))
1041 		iwl_mvm_rx_frame_release(mvm, napi, rxb, 0);
1042 	else if (cmd == WIDE_ID(LEGACY_GROUP, BAR_FRAME_RELEASE))
1043 		iwl_mvm_rx_bar_frame_release(mvm, napi, rxb, 0);
1044 	else if (cmd == WIDE_ID(DATA_PATH_GROUP, RX_NO_DATA_NOTIF))
1045 		iwl_mvm_rx_monitor_no_data(mvm, napi, rxb, 0);
1046 	else
1047 		iwl_mvm_rx_common(mvm, rxb, pkt);
1048 }
1049 
1050 static void iwl_mvm_async_cb(struct iwl_op_mode *op_mode,
1051 			     const struct iwl_device_cmd *cmd)
1052 {
1053 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1054 
1055 	/*
1056 	 * For now, we only set the CMD_WANT_ASYNC_CALLBACK for ADD_STA
1057 	 * commands that need to block the Tx queues.
1058 	 */
1059 	iwl_trans_block_txq_ptrs(mvm->trans, false);
1060 }
1061 
1062 static int iwl_mvm_is_static_queue(struct iwl_mvm *mvm, int queue)
1063 {
1064 	return queue == mvm->aux_queue || queue == mvm->probe_queue ||
1065 		queue == mvm->p2p_dev_queue || queue == mvm->snif_queue;
1066 }
1067 
1068 static void iwl_mvm_queue_state_change(struct iwl_op_mode *op_mode,
1069 				       int hw_queue, bool start)
1070 {
1071 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1072 	struct ieee80211_sta *sta;
1073 	struct ieee80211_txq *txq;
1074 	struct iwl_mvm_txq *mvmtxq;
1075 	int i;
1076 	unsigned long tid_bitmap;
1077 	struct iwl_mvm_sta *mvmsta;
1078 	u8 sta_id;
1079 
1080 	sta_id = iwl_mvm_has_new_tx_api(mvm) ?
1081 		mvm->tvqm_info[hw_queue].sta_id :
1082 		mvm->queue_info[hw_queue].ra_sta_id;
1083 
1084 	if (WARN_ON_ONCE(sta_id >= mvm->fw->ucode_capa.num_stations))
1085 		return;
1086 
1087 	rcu_read_lock();
1088 
1089 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1090 	if (IS_ERR_OR_NULL(sta))
1091 		goto out;
1092 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
1093 
1094 	if (iwl_mvm_is_static_queue(mvm, hw_queue)) {
1095 		if (!start)
1096 			ieee80211_stop_queues(mvm->hw);
1097 		else if (mvmsta->sta_state != IEEE80211_STA_NOTEXIST)
1098 			ieee80211_wake_queues(mvm->hw);
1099 
1100 		goto out;
1101 	}
1102 
1103 	if (iwl_mvm_has_new_tx_api(mvm)) {
1104 		int tid = mvm->tvqm_info[hw_queue].txq_tid;
1105 
1106 		tid_bitmap = BIT(tid);
1107 	} else {
1108 		tid_bitmap = mvm->queue_info[hw_queue].tid_bitmap;
1109 	}
1110 
1111 	for_each_set_bit(i, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1112 		int tid = i;
1113 
1114 		if (tid == IWL_MAX_TID_COUNT)
1115 			tid = IEEE80211_NUM_TIDS;
1116 
1117 		txq = sta->txq[tid];
1118 		mvmtxq = iwl_mvm_txq_from_mac80211(txq);
1119 		mvmtxq->stopped = !start;
1120 
1121 		if (start && mvmsta->sta_state != IEEE80211_STA_NOTEXIST)
1122 			iwl_mvm_mac_itxq_xmit(mvm->hw, txq);
1123 	}
1124 
1125 out:
1126 	rcu_read_unlock();
1127 }
1128 
1129 static void iwl_mvm_stop_sw_queue(struct iwl_op_mode *op_mode, int hw_queue)
1130 {
1131 	iwl_mvm_queue_state_change(op_mode, hw_queue, false);
1132 }
1133 
1134 static void iwl_mvm_wake_sw_queue(struct iwl_op_mode *op_mode, int hw_queue)
1135 {
1136 	iwl_mvm_queue_state_change(op_mode, hw_queue, true);
1137 }
1138 
1139 static void iwl_mvm_set_rfkill_state(struct iwl_mvm *mvm)
1140 {
1141 	bool state = iwl_mvm_is_radio_killed(mvm);
1142 
1143 	if (state)
1144 		wake_up(&mvm->rx_sync_waitq);
1145 
1146 	wiphy_rfkill_set_hw_state(mvm->hw->wiphy, state);
1147 }
1148 
1149 void iwl_mvm_set_hw_ctkill_state(struct iwl_mvm *mvm, bool state)
1150 {
1151 	if (state)
1152 		set_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status);
1153 	else
1154 		clear_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status);
1155 
1156 	iwl_mvm_set_rfkill_state(mvm);
1157 }
1158 
1159 static bool iwl_mvm_set_hw_rfkill_state(struct iwl_op_mode *op_mode, bool state)
1160 {
1161 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1162 	bool rfkill_safe_init_done = READ_ONCE(mvm->rfkill_safe_init_done);
1163 	bool unified = iwl_mvm_has_unified_ucode(mvm);
1164 
1165 	if (state)
1166 		set_bit(IWL_MVM_STATUS_HW_RFKILL, &mvm->status);
1167 	else
1168 		clear_bit(IWL_MVM_STATUS_HW_RFKILL, &mvm->status);
1169 
1170 	iwl_mvm_set_rfkill_state(mvm);
1171 
1172 	 /* iwl_run_init_mvm_ucode is waiting for results, abort it. */
1173 	if (rfkill_safe_init_done)
1174 		iwl_abort_notification_waits(&mvm->notif_wait);
1175 
1176 	/*
1177 	 * Don't ask the transport to stop the firmware. We'll do it
1178 	 * after cfg80211 takes us down.
1179 	 */
1180 	if (unified)
1181 		return false;
1182 
1183 	/*
1184 	 * Stop the device if we run OPERATIONAL firmware or if we are in the
1185 	 * middle of the calibrations.
1186 	 */
1187 	return state && rfkill_safe_init_done;
1188 }
1189 
1190 static void iwl_mvm_free_skb(struct iwl_op_mode *op_mode, struct sk_buff *skb)
1191 {
1192 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1193 	struct ieee80211_tx_info *info;
1194 
1195 	info = IEEE80211_SKB_CB(skb);
1196 	iwl_trans_free_tx_cmd(mvm->trans, info->driver_data[1]);
1197 	ieee80211_free_txskb(mvm->hw, skb);
1198 }
1199 
1200 struct iwl_mvm_reprobe {
1201 	struct device *dev;
1202 	struct work_struct work;
1203 };
1204 
1205 static void iwl_mvm_reprobe_wk(struct work_struct *wk)
1206 {
1207 	struct iwl_mvm_reprobe *reprobe;
1208 
1209 	reprobe = container_of(wk, struct iwl_mvm_reprobe, work);
1210 	if (device_reprobe(reprobe->dev))
1211 		dev_err(reprobe->dev, "reprobe failed!\n");
1212 	put_device(reprobe->dev);
1213 	kfree(reprobe);
1214 	module_put(THIS_MODULE);
1215 }
1216 
1217 void iwl_mvm_nic_restart(struct iwl_mvm *mvm, bool fw_error)
1218 {
1219 	iwl_abort_notification_waits(&mvm->notif_wait);
1220 	iwl_dbg_tlv_del_timers(mvm->trans);
1221 
1222 	/*
1223 	 * This is a bit racy, but worst case we tell mac80211 about
1224 	 * a stopped/aborted scan when that was already done which
1225 	 * is not a problem. It is necessary to abort any os scan
1226 	 * here because mac80211 requires having the scan cleared
1227 	 * before restarting.
1228 	 * We'll reset the scan_status to NONE in restart cleanup in
1229 	 * the next start() call from mac80211. If restart isn't called
1230 	 * (no fw restart) scan status will stay busy.
1231 	 */
1232 	iwl_mvm_report_scan_aborted(mvm);
1233 
1234 	/*
1235 	 * If we're restarting already, don't cycle restarts.
1236 	 * If INIT fw asserted, it will likely fail again.
1237 	 * If WoWLAN fw asserted, don't restart either, mac80211
1238 	 * can't recover this since we're already half suspended.
1239 	 */
1240 	if (!mvm->fw_restart && fw_error) {
1241 		iwl_fw_error_collect(&mvm->fwrt);
1242 	} else if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1243 		struct iwl_mvm_reprobe *reprobe;
1244 
1245 		IWL_ERR(mvm,
1246 			"Firmware error during reconfiguration - reprobe!\n");
1247 
1248 		/*
1249 		 * get a module reference to avoid doing this while unloading
1250 		 * anyway and to avoid scheduling a work with code that's
1251 		 * being removed.
1252 		 */
1253 		if (!try_module_get(THIS_MODULE)) {
1254 			IWL_ERR(mvm, "Module is being unloaded - abort\n");
1255 			return;
1256 		}
1257 
1258 		reprobe = kzalloc(sizeof(*reprobe), GFP_ATOMIC);
1259 		if (!reprobe) {
1260 			module_put(THIS_MODULE);
1261 			return;
1262 		}
1263 		reprobe->dev = get_device(mvm->trans->dev);
1264 		INIT_WORK(&reprobe->work, iwl_mvm_reprobe_wk);
1265 		schedule_work(&reprobe->work);
1266 	} else if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
1267 			    &mvm->status)) {
1268 		IWL_ERR(mvm, "HW restart already requested, but not started\n");
1269 	} else if (mvm->fwrt.cur_fw_img == IWL_UCODE_REGULAR &&
1270 		   mvm->hw_registered &&
1271 		   !test_bit(STATUS_TRANS_DEAD, &mvm->trans->status)) {
1272 		/* This should be first thing before trying to collect any
1273 		 * data to avoid endless loops if any HW error happens while
1274 		 * collecting debug data.
1275 		 */
1276 		set_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
1277 
1278 		if (mvm->fw->ucode_capa.error_log_size) {
1279 			u32 src_size = mvm->fw->ucode_capa.error_log_size;
1280 			u32 src_addr = mvm->fw->ucode_capa.error_log_addr;
1281 			u8 *recover_buf = kzalloc(src_size, GFP_ATOMIC);
1282 
1283 			if (recover_buf) {
1284 				mvm->error_recovery_buf = recover_buf;
1285 				iwl_trans_read_mem_bytes(mvm->trans,
1286 							 src_addr,
1287 							 recover_buf,
1288 							 src_size);
1289 			}
1290 		}
1291 
1292 		iwl_fw_error_collect(&mvm->fwrt);
1293 
1294 		if (fw_error && mvm->fw_restart > 0)
1295 			mvm->fw_restart--;
1296 		ieee80211_restart_hw(mvm->hw);
1297 	}
1298 }
1299 
1300 static void iwl_mvm_nic_error(struct iwl_op_mode *op_mode)
1301 {
1302 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1303 
1304 	if (!test_bit(STATUS_TRANS_DEAD, &mvm->trans->status))
1305 		iwl_mvm_dump_nic_error_log(mvm);
1306 
1307 	iwl_mvm_nic_restart(mvm, true);
1308 }
1309 
1310 static void iwl_mvm_cmd_queue_full(struct iwl_op_mode *op_mode)
1311 {
1312 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1313 
1314 	WARN_ON(1);
1315 	iwl_mvm_nic_restart(mvm, true);
1316 }
1317 
1318 #define IWL_MVM_COMMON_OPS					\
1319 	/* these could be differentiated */			\
1320 	.async_cb = iwl_mvm_async_cb,				\
1321 	.queue_full = iwl_mvm_stop_sw_queue,			\
1322 	.queue_not_full = iwl_mvm_wake_sw_queue,		\
1323 	.hw_rf_kill = iwl_mvm_set_hw_rfkill_state,		\
1324 	.free_skb = iwl_mvm_free_skb,				\
1325 	.nic_error = iwl_mvm_nic_error,				\
1326 	.cmd_queue_full = iwl_mvm_cmd_queue_full,		\
1327 	.nic_config = iwl_mvm_nic_config,			\
1328 	/* as we only register one, these MUST be common! */	\
1329 	.start = iwl_op_mode_mvm_start,				\
1330 	.stop = iwl_op_mode_mvm_stop
1331 
1332 static const struct iwl_op_mode_ops iwl_mvm_ops = {
1333 	IWL_MVM_COMMON_OPS,
1334 	.rx = iwl_mvm_rx,
1335 };
1336 
1337 static void iwl_mvm_rx_mq_rss(struct iwl_op_mode *op_mode,
1338 			      struct napi_struct *napi,
1339 			      struct iwl_rx_cmd_buffer *rxb,
1340 			      unsigned int queue)
1341 {
1342 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1343 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1344 	u16 cmd = WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd);
1345 
1346 	if (unlikely(cmd == WIDE_ID(LEGACY_GROUP, FRAME_RELEASE)))
1347 		iwl_mvm_rx_frame_release(mvm, napi, rxb, queue);
1348 	else if (unlikely(cmd == WIDE_ID(DATA_PATH_GROUP,
1349 					 RX_QUEUES_NOTIFICATION)))
1350 		iwl_mvm_rx_queue_notif(mvm, napi, rxb, queue);
1351 	else if (likely(cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_MPDU_CMD)))
1352 		iwl_mvm_rx_mpdu_mq(mvm, napi, rxb, queue);
1353 }
1354 
1355 static const struct iwl_op_mode_ops iwl_mvm_ops_mq = {
1356 	IWL_MVM_COMMON_OPS,
1357 	.rx = iwl_mvm_rx_mq,
1358 	.rx_rss = iwl_mvm_rx_mq_rss,
1359 };
1360