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