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