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