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 	/* Set EBS as successful as long as not stated otherwise by the FW. */
795 	mvm->last_ebs_successful = true;
796 
797 	err = iwl_mvm_mac_setup_register(mvm);
798 	if (err)
799 		goto out_free;
800 	mvm->hw_registered = true;
801 
802 	min_backoff = iwl_mvm_min_backoff(mvm);
803 	iwl_mvm_thermal_initialize(mvm, min_backoff);
804 
805 	iwl_mvm_dbgfs_register(mvm, dbgfs_dir);
806 
807 	if (!iwl_mvm_has_new_rx_stats_api(mvm))
808 		memset(&mvm->rx_stats_v3, 0,
809 		       sizeof(struct mvm_statistics_rx_v3));
810 	else
811 		memset(&mvm->rx_stats, 0, sizeof(struct mvm_statistics_rx));
812 
813 	iwl_mvm_toggle_tx_ant(mvm, &mvm->mgmt_last_antenna_idx);
814 
815 	return op_mode;
816 
817  out_free:
818 	iwl_fw_flush_dumps(&mvm->fwrt);
819 	iwl_fw_runtime_free(&mvm->fwrt);
820 
821 	if (iwlmvm_mod_params.init_dbg)
822 		return op_mode;
823 	iwl_phy_db_free(mvm->phy_db);
824 	kfree(mvm->scan_cmd);
825 	iwl_trans_op_mode_leave(trans);
826 
827 	ieee80211_free_hw(mvm->hw);
828 	return NULL;
829 }
830 
831 void iwl_mvm_stop_device(struct iwl_mvm *mvm)
832 {
833 	lockdep_assert_held(&mvm->mutex);
834 
835 	iwl_fw_cancel_timestamp(&mvm->fwrt);
836 
837 	clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
838 
839 	iwl_fw_dbg_stop_sync(&mvm->fwrt);
840 	iwl_trans_stop_device(mvm->trans);
841 	iwl_free_fw_paging(&mvm->fwrt);
842 	iwl_fw_dump_conf_clear(&mvm->fwrt);
843 }
844 
845 static void iwl_op_mode_mvm_stop(struct iwl_op_mode *op_mode)
846 {
847 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
848 	int i;
849 
850 	iwl_mvm_leds_exit(mvm);
851 
852 	iwl_mvm_thermal_exit(mvm);
853 
854 	ieee80211_unregister_hw(mvm->hw);
855 
856 	kfree(mvm->scan_cmd);
857 	kfree(mvm->mcast_filter_cmd);
858 	mvm->mcast_filter_cmd = NULL;
859 
860 	kfree(mvm->error_recovery_buf);
861 	mvm->error_recovery_buf = NULL;
862 
863 	iwl_trans_op_mode_leave(mvm->trans);
864 
865 	iwl_phy_db_free(mvm->phy_db);
866 	mvm->phy_db = NULL;
867 
868 	kfree(mvm->nvm_data);
869 	for (i = 0; i < NVM_MAX_NUM_SECTIONS; i++)
870 		kfree(mvm->nvm_sections[i].data);
871 
872 	cancel_delayed_work_sync(&mvm->tcm.work);
873 
874 	iwl_fw_runtime_free(&mvm->fwrt);
875 	mutex_destroy(&mvm->mutex);
876 
877 	ieee80211_free_hw(mvm->hw);
878 }
879 
880 struct iwl_async_handler_entry {
881 	struct list_head list;
882 	struct iwl_rx_cmd_buffer rxb;
883 	enum iwl_rx_handler_context context;
884 	void (*fn)(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb);
885 };
886 
887 void iwl_mvm_async_handlers_purge(struct iwl_mvm *mvm)
888 {
889 	struct iwl_async_handler_entry *entry, *tmp;
890 
891 	spin_lock_bh(&mvm->async_handlers_lock);
892 	list_for_each_entry_safe(entry, tmp, &mvm->async_handlers_list, list) {
893 		iwl_free_rxb(&entry->rxb);
894 		list_del(&entry->list);
895 		kfree(entry);
896 	}
897 	spin_unlock_bh(&mvm->async_handlers_lock);
898 }
899 
900 static void iwl_mvm_async_handlers_wk(struct work_struct *wk)
901 {
902 	struct iwl_mvm *mvm =
903 		container_of(wk, struct iwl_mvm, async_handlers_wk);
904 	struct iwl_async_handler_entry *entry, *tmp;
905 	LIST_HEAD(local_list);
906 
907 	/* Ensure that we are not in stop flow (check iwl_mvm_mac_stop) */
908 
909 	/*
910 	 * Sync with Rx path with a lock. Remove all the entries from this list,
911 	 * add them to a local one (lock free), and then handle them.
912 	 */
913 	spin_lock_bh(&mvm->async_handlers_lock);
914 	list_splice_init(&mvm->async_handlers_list, &local_list);
915 	spin_unlock_bh(&mvm->async_handlers_lock);
916 
917 	list_for_each_entry_safe(entry, tmp, &local_list, list) {
918 		if (entry->context == RX_HANDLER_ASYNC_LOCKED)
919 			mutex_lock(&mvm->mutex);
920 		entry->fn(mvm, &entry->rxb);
921 		iwl_free_rxb(&entry->rxb);
922 		list_del(&entry->list);
923 		if (entry->context == RX_HANDLER_ASYNC_LOCKED)
924 			mutex_unlock(&mvm->mutex);
925 		kfree(entry);
926 	}
927 }
928 
929 static inline void iwl_mvm_rx_check_trigger(struct iwl_mvm *mvm,
930 					    struct iwl_rx_packet *pkt)
931 {
932 	struct iwl_fw_dbg_trigger_tlv *trig;
933 	struct iwl_fw_dbg_trigger_cmd *cmds_trig;
934 	int i;
935 
936 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, NULL,
937 				     FW_DBG_TRIGGER_FW_NOTIF);
938 	if (!trig)
939 		return;
940 
941 	cmds_trig = (void *)trig->data;
942 
943 	for (i = 0; i < ARRAY_SIZE(cmds_trig->cmds); i++) {
944 		/* don't collect on CMD 0 */
945 		if (!cmds_trig->cmds[i].cmd_id)
946 			break;
947 
948 		if (cmds_trig->cmds[i].cmd_id != pkt->hdr.cmd ||
949 		    cmds_trig->cmds[i].group_id != pkt->hdr.group_id)
950 			continue;
951 
952 		iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
953 					"CMD 0x%02x.%02x received",
954 					pkt->hdr.group_id, pkt->hdr.cmd);
955 		break;
956 	}
957 }
958 
959 static void iwl_mvm_rx_common(struct iwl_mvm *mvm,
960 			      struct iwl_rx_cmd_buffer *rxb,
961 			      struct iwl_rx_packet *pkt)
962 {
963 	int i;
964 	union iwl_dbg_tlv_tp_data tp_data = { .fw_pkt = pkt };
965 
966 	iwl_dbg_tlv_time_point(&mvm->fwrt,
967 			       IWL_FW_INI_TIME_POINT_FW_RSP_OR_NOTIF, &tp_data);
968 	iwl_mvm_rx_check_trigger(mvm, pkt);
969 
970 	/*
971 	 * Do the notification wait before RX handlers so
972 	 * even if the RX handler consumes the RXB we have
973 	 * access to it in the notification wait entry.
974 	 */
975 	iwl_notification_wait_notify(&mvm->notif_wait, pkt);
976 
977 	for (i = 0; i < ARRAY_SIZE(iwl_mvm_rx_handlers); i++) {
978 		const struct iwl_rx_handlers *rx_h = &iwl_mvm_rx_handlers[i];
979 		struct iwl_async_handler_entry *entry;
980 
981 		if (rx_h->cmd_id != WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd))
982 			continue;
983 
984 		if (rx_h->context == RX_HANDLER_SYNC) {
985 			rx_h->fn(mvm, rxb);
986 			return;
987 		}
988 
989 		entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
990 		/* we can't do much... */
991 		if (!entry)
992 			return;
993 
994 		entry->rxb._page = rxb_steal_page(rxb);
995 		entry->rxb._offset = rxb->_offset;
996 		entry->rxb._rx_page_order = rxb->_rx_page_order;
997 		entry->fn = rx_h->fn;
998 		entry->context = rx_h->context;
999 		spin_lock(&mvm->async_handlers_lock);
1000 		list_add_tail(&entry->list, &mvm->async_handlers_list);
1001 		spin_unlock(&mvm->async_handlers_lock);
1002 		schedule_work(&mvm->async_handlers_wk);
1003 		break;
1004 	}
1005 }
1006 
1007 static void iwl_mvm_rx(struct iwl_op_mode *op_mode,
1008 		       struct napi_struct *napi,
1009 		       struct iwl_rx_cmd_buffer *rxb)
1010 {
1011 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1012 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1013 	u16 cmd = WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd);
1014 
1015 	if (likely(cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_MPDU_CMD)))
1016 		iwl_mvm_rx_rx_mpdu(mvm, napi, rxb);
1017 	else if (cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_PHY_CMD))
1018 		iwl_mvm_rx_rx_phy_cmd(mvm, rxb);
1019 	else
1020 		iwl_mvm_rx_common(mvm, rxb, pkt);
1021 }
1022 
1023 static void iwl_mvm_rx_mq(struct iwl_op_mode *op_mode,
1024 			  struct napi_struct *napi,
1025 			  struct iwl_rx_cmd_buffer *rxb)
1026 {
1027 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1028 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1029 	u16 cmd = WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd);
1030 
1031 	if (likely(cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_MPDU_CMD)))
1032 		iwl_mvm_rx_mpdu_mq(mvm, napi, rxb, 0);
1033 	else if (unlikely(cmd == WIDE_ID(DATA_PATH_GROUP,
1034 					 RX_QUEUES_NOTIFICATION)))
1035 		iwl_mvm_rx_queue_notif(mvm, napi, rxb, 0);
1036 	else if (cmd == WIDE_ID(LEGACY_GROUP, FRAME_RELEASE))
1037 		iwl_mvm_rx_frame_release(mvm, napi, rxb, 0);
1038 	else if (cmd == WIDE_ID(LEGACY_GROUP, BAR_FRAME_RELEASE))
1039 		iwl_mvm_rx_bar_frame_release(mvm, napi, rxb, 0);
1040 	else if (cmd == WIDE_ID(DATA_PATH_GROUP, RX_NO_DATA_NOTIF))
1041 		iwl_mvm_rx_monitor_no_data(mvm, napi, rxb, 0);
1042 	else
1043 		iwl_mvm_rx_common(mvm, rxb, pkt);
1044 }
1045 
1046 static void iwl_mvm_async_cb(struct iwl_op_mode *op_mode,
1047 			     const struct iwl_device_cmd *cmd)
1048 {
1049 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1050 
1051 	/*
1052 	 * For now, we only set the CMD_WANT_ASYNC_CALLBACK for ADD_STA
1053 	 * commands that need to block the Tx queues.
1054 	 */
1055 	iwl_trans_block_txq_ptrs(mvm->trans, false);
1056 }
1057 
1058 static int iwl_mvm_is_static_queue(struct iwl_mvm *mvm, int queue)
1059 {
1060 	return queue == mvm->aux_queue || queue == mvm->probe_queue ||
1061 		queue == mvm->p2p_dev_queue || queue == mvm->snif_queue;
1062 }
1063 
1064 static void iwl_mvm_queue_state_change(struct iwl_op_mode *op_mode,
1065 				       int hw_queue, bool start)
1066 {
1067 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1068 	struct ieee80211_sta *sta;
1069 	struct ieee80211_txq *txq;
1070 	struct iwl_mvm_txq *mvmtxq;
1071 	int i;
1072 	unsigned long tid_bitmap;
1073 	struct iwl_mvm_sta *mvmsta;
1074 	u8 sta_id;
1075 
1076 	sta_id = iwl_mvm_has_new_tx_api(mvm) ?
1077 		mvm->tvqm_info[hw_queue].sta_id :
1078 		mvm->queue_info[hw_queue].ra_sta_id;
1079 
1080 	if (WARN_ON_ONCE(sta_id >= mvm->fw->ucode_capa.num_stations))
1081 		return;
1082 
1083 	rcu_read_lock();
1084 
1085 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1086 	if (IS_ERR_OR_NULL(sta))
1087 		goto out;
1088 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
1089 
1090 	if (iwl_mvm_is_static_queue(mvm, hw_queue)) {
1091 		if (!start)
1092 			ieee80211_stop_queues(mvm->hw);
1093 		else if (mvmsta->sta_state != IEEE80211_STA_NOTEXIST)
1094 			ieee80211_wake_queues(mvm->hw);
1095 
1096 		goto out;
1097 	}
1098 
1099 	if (iwl_mvm_has_new_tx_api(mvm)) {
1100 		int tid = mvm->tvqm_info[hw_queue].txq_tid;
1101 
1102 		tid_bitmap = BIT(tid);
1103 	} else {
1104 		tid_bitmap = mvm->queue_info[hw_queue].tid_bitmap;
1105 	}
1106 
1107 	for_each_set_bit(i, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1108 		int tid = i;
1109 
1110 		if (tid == IWL_MAX_TID_COUNT)
1111 			tid = IEEE80211_NUM_TIDS;
1112 
1113 		txq = sta->txq[tid];
1114 		mvmtxq = iwl_mvm_txq_from_mac80211(txq);
1115 		mvmtxq->stopped = !start;
1116 
1117 		if (start && mvmsta->sta_state != IEEE80211_STA_NOTEXIST)
1118 			iwl_mvm_mac_itxq_xmit(mvm->hw, txq);
1119 	}
1120 
1121 out:
1122 	rcu_read_unlock();
1123 }
1124 
1125 static void iwl_mvm_stop_sw_queue(struct iwl_op_mode *op_mode, int hw_queue)
1126 {
1127 	iwl_mvm_queue_state_change(op_mode, hw_queue, false);
1128 }
1129 
1130 static void iwl_mvm_wake_sw_queue(struct iwl_op_mode *op_mode, int hw_queue)
1131 {
1132 	iwl_mvm_queue_state_change(op_mode, hw_queue, true);
1133 }
1134 
1135 static void iwl_mvm_set_rfkill_state(struct iwl_mvm *mvm)
1136 {
1137 	bool state = iwl_mvm_is_radio_killed(mvm);
1138 
1139 	if (state)
1140 		wake_up(&mvm->rx_sync_waitq);
1141 
1142 	wiphy_rfkill_set_hw_state(mvm->hw->wiphy, state);
1143 }
1144 
1145 void iwl_mvm_set_hw_ctkill_state(struct iwl_mvm *mvm, bool state)
1146 {
1147 	if (state)
1148 		set_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status);
1149 	else
1150 		clear_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status);
1151 
1152 	iwl_mvm_set_rfkill_state(mvm);
1153 }
1154 
1155 static bool iwl_mvm_set_hw_rfkill_state(struct iwl_op_mode *op_mode, bool state)
1156 {
1157 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1158 	bool rfkill_safe_init_done = READ_ONCE(mvm->rfkill_safe_init_done);
1159 	bool unified = iwl_mvm_has_unified_ucode(mvm);
1160 
1161 	if (state)
1162 		set_bit(IWL_MVM_STATUS_HW_RFKILL, &mvm->status);
1163 	else
1164 		clear_bit(IWL_MVM_STATUS_HW_RFKILL, &mvm->status);
1165 
1166 	iwl_mvm_set_rfkill_state(mvm);
1167 
1168 	 /* iwl_run_init_mvm_ucode is waiting for results, abort it. */
1169 	if (rfkill_safe_init_done)
1170 		iwl_abort_notification_waits(&mvm->notif_wait);
1171 
1172 	/*
1173 	 * Don't ask the transport to stop the firmware. We'll do it
1174 	 * after cfg80211 takes us down.
1175 	 */
1176 	if (unified)
1177 		return false;
1178 
1179 	/*
1180 	 * Stop the device if we run OPERATIONAL firmware or if we are in the
1181 	 * middle of the calibrations.
1182 	 */
1183 	return state && rfkill_safe_init_done;
1184 }
1185 
1186 static void iwl_mvm_free_skb(struct iwl_op_mode *op_mode, struct sk_buff *skb)
1187 {
1188 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1189 	struct ieee80211_tx_info *info;
1190 
1191 	info = IEEE80211_SKB_CB(skb);
1192 	iwl_trans_free_tx_cmd(mvm->trans, info->driver_data[1]);
1193 	ieee80211_free_txskb(mvm->hw, skb);
1194 }
1195 
1196 struct iwl_mvm_reprobe {
1197 	struct device *dev;
1198 	struct work_struct work;
1199 };
1200 
1201 static void iwl_mvm_reprobe_wk(struct work_struct *wk)
1202 {
1203 	struct iwl_mvm_reprobe *reprobe;
1204 
1205 	reprobe = container_of(wk, struct iwl_mvm_reprobe, work);
1206 	if (device_reprobe(reprobe->dev))
1207 		dev_err(reprobe->dev, "reprobe failed!\n");
1208 	kfree(reprobe);
1209 	module_put(THIS_MODULE);
1210 }
1211 
1212 void iwl_mvm_nic_restart(struct iwl_mvm *mvm, bool fw_error)
1213 {
1214 	iwl_abort_notification_waits(&mvm->notif_wait);
1215 	iwl_dbg_tlv_del_timers(mvm->trans);
1216 
1217 	/*
1218 	 * This is a bit racy, but worst case we tell mac80211 about
1219 	 * a stopped/aborted scan when that was already done which
1220 	 * is not a problem. It is necessary to abort any os scan
1221 	 * here because mac80211 requires having the scan cleared
1222 	 * before restarting.
1223 	 * We'll reset the scan_status to NONE in restart cleanup in
1224 	 * the next start() call from mac80211. If restart isn't called
1225 	 * (no fw restart) scan status will stay busy.
1226 	 */
1227 	iwl_mvm_report_scan_aborted(mvm);
1228 
1229 	/*
1230 	 * If we're restarting already, don't cycle restarts.
1231 	 * If INIT fw asserted, it will likely fail again.
1232 	 * If WoWLAN fw asserted, don't restart either, mac80211
1233 	 * can't recover this since we're already half suspended.
1234 	 */
1235 	if (!mvm->fw_restart && fw_error) {
1236 		iwl_fw_error_collect(&mvm->fwrt);
1237 	} else if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1238 		struct iwl_mvm_reprobe *reprobe;
1239 
1240 		IWL_ERR(mvm,
1241 			"Firmware error during reconfiguration - reprobe!\n");
1242 
1243 		/*
1244 		 * get a module reference to avoid doing this while unloading
1245 		 * anyway and to avoid scheduling a work with code that's
1246 		 * being removed.
1247 		 */
1248 		if (!try_module_get(THIS_MODULE)) {
1249 			IWL_ERR(mvm, "Module is being unloaded - abort\n");
1250 			return;
1251 		}
1252 
1253 		reprobe = kzalloc(sizeof(*reprobe), GFP_ATOMIC);
1254 		if (!reprobe) {
1255 			module_put(THIS_MODULE);
1256 			return;
1257 		}
1258 		reprobe->dev = mvm->trans->dev;
1259 		INIT_WORK(&reprobe->work, iwl_mvm_reprobe_wk);
1260 		schedule_work(&reprobe->work);
1261 	} else if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
1262 			    &mvm->status)) {
1263 		IWL_ERR(mvm, "HW restart already requested, but not started\n");
1264 	} else if (mvm->fwrt.cur_fw_img == IWL_UCODE_REGULAR &&
1265 		   mvm->hw_registered &&
1266 		   !test_bit(STATUS_TRANS_DEAD, &mvm->trans->status)) {
1267 		/* This should be first thing before trying to collect any
1268 		 * data to avoid endless loops if any HW error happens while
1269 		 * collecting debug data.
1270 		 */
1271 		set_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
1272 
1273 		if (mvm->fw->ucode_capa.error_log_size) {
1274 			u32 src_size = mvm->fw->ucode_capa.error_log_size;
1275 			u32 src_addr = mvm->fw->ucode_capa.error_log_addr;
1276 			u8 *recover_buf = kzalloc(src_size, GFP_ATOMIC);
1277 
1278 			if (recover_buf) {
1279 				mvm->error_recovery_buf = recover_buf;
1280 				iwl_trans_read_mem_bytes(mvm->trans,
1281 							 src_addr,
1282 							 recover_buf,
1283 							 src_size);
1284 			}
1285 		}
1286 
1287 		iwl_fw_error_collect(&mvm->fwrt);
1288 
1289 		if (fw_error && mvm->fw_restart > 0)
1290 			mvm->fw_restart--;
1291 		ieee80211_restart_hw(mvm->hw);
1292 	}
1293 }
1294 
1295 static void iwl_mvm_nic_error(struct iwl_op_mode *op_mode)
1296 {
1297 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1298 
1299 	if (!test_bit(STATUS_TRANS_DEAD, &mvm->trans->status))
1300 		iwl_mvm_dump_nic_error_log(mvm);
1301 
1302 	iwl_mvm_nic_restart(mvm, true);
1303 }
1304 
1305 static void iwl_mvm_cmd_queue_full(struct iwl_op_mode *op_mode)
1306 {
1307 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1308 
1309 	WARN_ON(1);
1310 	iwl_mvm_nic_restart(mvm, true);
1311 }
1312 
1313 #define IWL_MVM_COMMON_OPS					\
1314 	/* these could be differentiated */			\
1315 	.async_cb = iwl_mvm_async_cb,				\
1316 	.queue_full = iwl_mvm_stop_sw_queue,			\
1317 	.queue_not_full = iwl_mvm_wake_sw_queue,		\
1318 	.hw_rf_kill = iwl_mvm_set_hw_rfkill_state,		\
1319 	.free_skb = iwl_mvm_free_skb,				\
1320 	.nic_error = iwl_mvm_nic_error,				\
1321 	.cmd_queue_full = iwl_mvm_cmd_queue_full,		\
1322 	.nic_config = iwl_mvm_nic_config,			\
1323 	/* as we only register one, these MUST be common! */	\
1324 	.start = iwl_op_mode_mvm_start,				\
1325 	.stop = iwl_op_mode_mvm_stop
1326 
1327 static const struct iwl_op_mode_ops iwl_mvm_ops = {
1328 	IWL_MVM_COMMON_OPS,
1329 	.rx = iwl_mvm_rx,
1330 };
1331 
1332 static void iwl_mvm_rx_mq_rss(struct iwl_op_mode *op_mode,
1333 			      struct napi_struct *napi,
1334 			      struct iwl_rx_cmd_buffer *rxb,
1335 			      unsigned int queue)
1336 {
1337 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1338 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1339 	u16 cmd = WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd);
1340 
1341 	if (unlikely(cmd == WIDE_ID(LEGACY_GROUP, FRAME_RELEASE)))
1342 		iwl_mvm_rx_frame_release(mvm, napi, rxb, queue);
1343 	else if (unlikely(cmd == WIDE_ID(DATA_PATH_GROUP,
1344 					 RX_QUEUES_NOTIFICATION)))
1345 		iwl_mvm_rx_queue_notif(mvm, napi, rxb, queue);
1346 	else if (likely(cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_MPDU_CMD)))
1347 		iwl_mvm_rx_mpdu_mq(mvm, napi, rxb, queue);
1348 }
1349 
1350 static const struct iwl_op_mode_ops iwl_mvm_ops_mq = {
1351 	IWL_MVM_COMMON_OPS,
1352 	.rx = iwl_mvm_rx_mq,
1353 	.rx_rss = iwl_mvm_rx_mq_rss,
1354 };
1355