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