1e705c121SKalle Valo /******************************************************************************
2e705c121SKalle Valo  *
3e705c121SKalle Valo  * This file is provided under a dual BSD/GPLv2 license.  When using or
4e705c121SKalle Valo  * redistributing this file, you may do so under either license.
5e705c121SKalle Valo  *
6e705c121SKalle Valo  * GPL LICENSE SUMMARY
7e705c121SKalle Valo  *
8e705c121SKalle Valo  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9e705c121SKalle Valo  * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
1009856582SLuca Coelho  * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
11f6780614SSara Sharon  * Copyright(c) 2018 - 2019 Intel Corporation
12e705c121SKalle Valo  *
13e705c121SKalle Valo  * This program is free software; you can redistribute it and/or modify
14e705c121SKalle Valo  * it under the terms of version 2 of the GNU General Public License as
15e705c121SKalle Valo  * published by the Free Software Foundation.
16e705c121SKalle Valo  *
17e705c121SKalle Valo  * This program is distributed in the hope that it will be useful, but
18e705c121SKalle Valo  * WITHOUT ANY WARRANTY; without even the implied warranty of
19e705c121SKalle Valo  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20e705c121SKalle Valo  * General Public License for more details.
21e705c121SKalle Valo  *
22e705c121SKalle Valo  * The full GNU General Public License is included in this distribution
23e705c121SKalle Valo  * in the file called COPYING.
24e705c121SKalle Valo  *
25e705c121SKalle Valo  * Contact Information:
26cb2f8277SEmmanuel Grumbach  *  Intel Linux Wireless <linuxwifi@intel.com>
27e705c121SKalle Valo  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28e705c121SKalle Valo  *
29e705c121SKalle Valo  * BSD LICENSE
30e705c121SKalle Valo  *
31e705c121SKalle Valo  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
32e705c121SKalle Valo  * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
3309856582SLuca Coelho  * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
34f6780614SSara Sharon  * Copyright(c) 2018 - 2019 Intel Corporation
35e705c121SKalle Valo  * All rights reserved.
36e705c121SKalle Valo  *
37e705c121SKalle Valo  * Redistribution and use in source and binary forms, with or without
38e705c121SKalle Valo  * modification, are permitted provided that the following conditions
39e705c121SKalle Valo  * are met:
40e705c121SKalle Valo  *
41e705c121SKalle Valo  *  * Redistributions of source code must retain the above copyright
42e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer.
43e705c121SKalle Valo  *  * Redistributions in binary form must reproduce the above copyright
44e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer in
45e705c121SKalle Valo  *    the documentation and/or other materials provided with the
46e705c121SKalle Valo  *    distribution.
47e705c121SKalle Valo  *  * Neither the name Intel Corporation nor the names of its
48e705c121SKalle Valo  *    contributors may be used to endorse or promote products derived
49e705c121SKalle Valo  *    from this software without specific prior written permission.
50e705c121SKalle Valo  *
51e705c121SKalle Valo  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52e705c121SKalle Valo  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53e705c121SKalle Valo  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54e705c121SKalle Valo  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55e705c121SKalle Valo  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56e705c121SKalle Valo  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
57e705c121SKalle Valo  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58e705c121SKalle Valo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59e705c121SKalle Valo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60e705c121SKalle Valo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61e705c121SKalle Valo  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62e705c121SKalle Valo  *
63e705c121SKalle Valo  *****************************************************************************/
64e705c121SKalle Valo 
65e705c121SKalle Valo #include <linux/etherdevice.h>
66e705c121SKalle Valo #include <net/mac80211.h>
67e705c121SKalle Valo #include "iwl-io.h"
68e705c121SKalle Valo #include "iwl-prph.h"
69e705c121SKalle Valo #include "fw-api.h"
70e705c121SKalle Valo #include "mvm.h"
71e705c121SKalle Valo #include "time-event.h"
72e705c121SKalle Valo 
73e705c121SKalle Valo const u8 iwl_mvm_ac_to_tx_fifo[] = {
74e705c121SKalle Valo 	IWL_MVM_TX_FIFO_VO,
75e705c121SKalle Valo 	IWL_MVM_TX_FIFO_VI,
76e705c121SKalle Valo 	IWL_MVM_TX_FIFO_BE,
77e705c121SKalle Valo 	IWL_MVM_TX_FIFO_BK,
78e705c121SKalle Valo };
79e705c121SKalle Valo 
80cf6c6ea3SEmmanuel Grumbach const u8 iwl_mvm_ac_to_gen2_tx_fifo[] = {
81cf6c6ea3SEmmanuel Grumbach 	IWL_GEN2_EDCA_TX_FIFO_VO,
82cf6c6ea3SEmmanuel Grumbach 	IWL_GEN2_EDCA_TX_FIFO_VI,
83cf6c6ea3SEmmanuel Grumbach 	IWL_GEN2_EDCA_TX_FIFO_BE,
84cf6c6ea3SEmmanuel Grumbach 	IWL_GEN2_EDCA_TX_FIFO_BK,
857126b6f2SSara Sharon 	IWL_GEN2_TRIG_TX_FIFO_VO,
867126b6f2SSara Sharon 	IWL_GEN2_TRIG_TX_FIFO_VI,
877126b6f2SSara Sharon 	IWL_GEN2_TRIG_TX_FIFO_BE,
887126b6f2SSara Sharon 	IWL_GEN2_TRIG_TX_FIFO_BK,
89cf6c6ea3SEmmanuel Grumbach };
90cf6c6ea3SEmmanuel Grumbach 
91e705c121SKalle Valo struct iwl_mvm_mac_iface_iterator_data {
92e705c121SKalle Valo 	struct iwl_mvm *mvm;
93e705c121SKalle Valo 	struct ieee80211_vif *vif;
94e705c121SKalle Valo 	unsigned long available_mac_ids[BITS_TO_LONGS(NUM_MAC_INDEX_DRIVER)];
95e705c121SKalle Valo 	unsigned long available_tsf_ids[BITS_TO_LONGS(NUM_TSF_IDS)];
96e705c121SKalle Valo 	enum iwl_tsf_id preferred_tsf;
97e705c121SKalle Valo 	bool found_vif;
98e705c121SKalle Valo };
99e705c121SKalle Valo 
100e705c121SKalle Valo static void iwl_mvm_mac_tsf_id_iter(void *_data, u8 *mac,
101e705c121SKalle Valo 				    struct ieee80211_vif *vif)
102e705c121SKalle Valo {
103e705c121SKalle Valo 	struct iwl_mvm_mac_iface_iterator_data *data = _data;
104e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
105e705c121SKalle Valo 	u16 min_bi;
106e705c121SKalle Valo 
107e705c121SKalle Valo 	/* Skip the interface for which we are trying to assign a tsf_id  */
108e705c121SKalle Valo 	if (vif == data->vif)
109e705c121SKalle Valo 		return;
110e705c121SKalle Valo 
111e705c121SKalle Valo 	/*
112e705c121SKalle Valo 	 * The TSF is a hardware/firmware resource, there are 4 and
113e705c121SKalle Valo 	 * the driver should assign and free them as needed. However,
114e705c121SKalle Valo 	 * there are cases where 2 MACs should share the same TSF ID
115e705c121SKalle Valo 	 * for the purpose of clock sync, an optimization to avoid
116e705c121SKalle Valo 	 * clock drift causing overlapping TBTTs/DTIMs for a GO and
117e705c121SKalle Valo 	 * client in the system.
118e705c121SKalle Valo 	 *
119e705c121SKalle Valo 	 * The firmware will decide according to the MAC type which
120e705c121SKalle Valo 	 * will be the master and slave. Clients that need to sync
121e705c121SKalle Valo 	 * with a remote station will be the master, and an AP or GO
122e705c121SKalle Valo 	 * will be the slave.
123e705c121SKalle Valo 	 *
124e705c121SKalle Valo 	 * Depending on the new interface type it can be slaved to
125e705c121SKalle Valo 	 * or become the master of an existing interface.
126e705c121SKalle Valo 	 */
127e705c121SKalle Valo 	switch (data->vif->type) {
128e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
129e705c121SKalle Valo 		/*
130e705c121SKalle Valo 		 * The new interface is a client, so if the one we're iterating
131e705c121SKalle Valo 		 * is an AP, and the beacon interval of the AP is a multiple or
132e705c121SKalle Valo 		 * divisor of the beacon interval of the client, the same TSF
133e705c121SKalle Valo 		 * should be used to avoid drift between the new client and
134e705c121SKalle Valo 		 * existing AP. The existing AP will get drift updates from the
135e705c121SKalle Valo 		 * new client context in this case.
136e705c121SKalle Valo 		 */
137e705c121SKalle Valo 		if (vif->type != NL80211_IFTYPE_AP ||
138e705c121SKalle Valo 		    data->preferred_tsf != NUM_TSF_IDS ||
139e705c121SKalle Valo 		    !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
140e705c121SKalle Valo 			break;
141e705c121SKalle Valo 
142e705c121SKalle Valo 		min_bi = min(data->vif->bss_conf.beacon_int,
143e705c121SKalle Valo 			     vif->bss_conf.beacon_int);
144e705c121SKalle Valo 
145e705c121SKalle Valo 		if (!min_bi)
146e705c121SKalle Valo 			break;
147e705c121SKalle Valo 
148e705c121SKalle Valo 		if ((data->vif->bss_conf.beacon_int -
149e705c121SKalle Valo 		     vif->bss_conf.beacon_int) % min_bi == 0) {
150e705c121SKalle Valo 			data->preferred_tsf = mvmvif->tsf_id;
151e705c121SKalle Valo 			return;
152e705c121SKalle Valo 		}
153e705c121SKalle Valo 		break;
154e705c121SKalle Valo 
155e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
156e705c121SKalle Valo 		/*
157e705c121SKalle Valo 		 * The new interface is AP/GO, so if its beacon interval is a
158e705c121SKalle Valo 		 * multiple or a divisor of the beacon interval of an existing
159e705c121SKalle Valo 		 * interface, it should get drift updates from an existing
160e705c121SKalle Valo 		 * client or use the same TSF as an existing GO. There's no
161e705c121SKalle Valo 		 * drift between TSFs internally but if they used different
162e705c121SKalle Valo 		 * TSFs then a new client MAC could update one of them and
163e705c121SKalle Valo 		 * cause drift that way.
164e705c121SKalle Valo 		 */
165e705c121SKalle Valo 		if ((vif->type != NL80211_IFTYPE_AP &&
166e705c121SKalle Valo 		     vif->type != NL80211_IFTYPE_STATION) ||
167e705c121SKalle Valo 		    data->preferred_tsf != NUM_TSF_IDS ||
168e705c121SKalle Valo 		    !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
169e705c121SKalle Valo 			break;
170e705c121SKalle Valo 
171e705c121SKalle Valo 		min_bi = min(data->vif->bss_conf.beacon_int,
172e705c121SKalle Valo 			     vif->bss_conf.beacon_int);
173e705c121SKalle Valo 
174e705c121SKalle Valo 		if (!min_bi)
175e705c121SKalle Valo 			break;
176e705c121SKalle Valo 
177e705c121SKalle Valo 		if ((data->vif->bss_conf.beacon_int -
178e705c121SKalle Valo 		     vif->bss_conf.beacon_int) % min_bi == 0) {
179e705c121SKalle Valo 			data->preferred_tsf = mvmvif->tsf_id;
180e705c121SKalle Valo 			return;
181e705c121SKalle Valo 		}
182e705c121SKalle Valo 		break;
183e705c121SKalle Valo 	default:
184e705c121SKalle Valo 		/*
185e705c121SKalle Valo 		 * For all other interface types there's no need to
186e705c121SKalle Valo 		 * take drift into account. Either they're exclusive
187e705c121SKalle Valo 		 * like IBSS and monitor, or we don't care much about
188e705c121SKalle Valo 		 * their TSF (like P2P Device), but we won't be able
189e705c121SKalle Valo 		 * to share the TSF resource.
190e705c121SKalle Valo 		 */
191e705c121SKalle Valo 		break;
192e705c121SKalle Valo 	}
193e705c121SKalle Valo 
194e705c121SKalle Valo 	/*
195e705c121SKalle Valo 	 * Unless we exited above, we can't share the TSF resource
196e705c121SKalle Valo 	 * that the virtual interface we're iterating over is using
197e705c121SKalle Valo 	 * with the new one, so clear the available bit and if this
198e705c121SKalle Valo 	 * was the preferred one, reset that as well.
199e705c121SKalle Valo 	 */
200e705c121SKalle Valo 	__clear_bit(mvmvif->tsf_id, data->available_tsf_ids);
201e705c121SKalle Valo 
202e705c121SKalle Valo 	if (data->preferred_tsf == mvmvif->tsf_id)
203e705c121SKalle Valo 		data->preferred_tsf = NUM_TSF_IDS;
204e705c121SKalle Valo }
205e705c121SKalle Valo 
206e705c121SKalle Valo static void iwl_mvm_mac_iface_iterator(void *_data, u8 *mac,
207e705c121SKalle Valo 				       struct ieee80211_vif *vif)
208e705c121SKalle Valo {
209e705c121SKalle Valo 	struct iwl_mvm_mac_iface_iterator_data *data = _data;
210e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
211e705c121SKalle Valo 
212e705c121SKalle Valo 	/* Iterator may already find the interface being added -- skip it */
213e705c121SKalle Valo 	if (vif == data->vif) {
214e705c121SKalle Valo 		data->found_vif = true;
215e705c121SKalle Valo 		return;
216e705c121SKalle Valo 	}
217e705c121SKalle Valo 
218e705c121SKalle Valo 	/* Mark MAC IDs as used by clearing the available bit, and
219e705c121SKalle Valo 	 * (below) mark TSFs as used if their existing use is not
220e705c121SKalle Valo 	 * compatible with the new interface type.
221e705c121SKalle Valo 	 * No locking or atomic bit operations are needed since the
222e705c121SKalle Valo 	 * data is on the stack of the caller function.
223e705c121SKalle Valo 	 */
224e705c121SKalle Valo 	__clear_bit(mvmvif->id, data->available_mac_ids);
225e705c121SKalle Valo 
226e705c121SKalle Valo 	/* find a suitable tsf_id */
227e705c121SKalle Valo 	iwl_mvm_mac_tsf_id_iter(_data, mac, vif);
228e705c121SKalle Valo }
229e705c121SKalle Valo 
230e705c121SKalle Valo void iwl_mvm_mac_ctxt_recalc_tsf_id(struct iwl_mvm *mvm,
231e705c121SKalle Valo 				    struct ieee80211_vif *vif)
232e705c121SKalle Valo {
233e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
234e705c121SKalle Valo 	struct iwl_mvm_mac_iface_iterator_data data = {
235e705c121SKalle Valo 		.mvm = mvm,
236e705c121SKalle Valo 		.vif = vif,
237e705c121SKalle Valo 		.available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
238e705c121SKalle Valo 		/* no preference yet */
239e705c121SKalle Valo 		.preferred_tsf = NUM_TSF_IDS,
240e705c121SKalle Valo 	};
241e705c121SKalle Valo 
242e705c121SKalle Valo 	ieee80211_iterate_active_interfaces_atomic(
243e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
244e705c121SKalle Valo 		iwl_mvm_mac_tsf_id_iter, &data);
245e705c121SKalle Valo 
246e705c121SKalle Valo 	if (data.preferred_tsf != NUM_TSF_IDS)
247e705c121SKalle Valo 		mvmvif->tsf_id = data.preferred_tsf;
248e705c121SKalle Valo 	else if (!test_bit(mvmvif->tsf_id, data.available_tsf_ids))
249e705c121SKalle Valo 		mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
250e705c121SKalle Valo 						NUM_TSF_IDS);
251e705c121SKalle Valo }
252e705c121SKalle Valo 
253c8f54701SJohannes Berg int iwl_mvm_mac_ctxt_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
254e705c121SKalle Valo {
255e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
256e705c121SKalle Valo 	struct iwl_mvm_mac_iface_iterator_data data = {
257e705c121SKalle Valo 		.mvm = mvm,
258e705c121SKalle Valo 		.vif = vif,
259e705c121SKalle Valo 		.available_mac_ids = { (1 << NUM_MAC_INDEX_DRIVER) - 1 },
260e705c121SKalle Valo 		.available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
261e705c121SKalle Valo 		/* no preference yet */
262e705c121SKalle Valo 		.preferred_tsf = NUM_TSF_IDS,
263e705c121SKalle Valo 		.found_vif = false,
264e705c121SKalle Valo 	};
2653f7fbc8cSJohannes Berg 	int ret, i;
266e705c121SKalle Valo 
267c8f54701SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
268c8f54701SJohannes Berg 
269e705c121SKalle Valo 	/*
270e705c121SKalle Valo 	 * Allocate a MAC ID and a TSF for this MAC, along with the queues
271e705c121SKalle Valo 	 * and other resources.
272e705c121SKalle Valo 	 */
273e705c121SKalle Valo 
274e705c121SKalle Valo 	/*
275e705c121SKalle Valo 	 * Before the iterator, we start with all MAC IDs and TSFs available.
276e705c121SKalle Valo 	 *
277e705c121SKalle Valo 	 * During iteration, all MAC IDs are cleared that are in use by other
278e705c121SKalle Valo 	 * virtual interfaces, and all TSF IDs are cleared that can't be used
279e705c121SKalle Valo 	 * by this new virtual interface because they're used by an interface
280e705c121SKalle Valo 	 * that can't share it with the new one.
281e705c121SKalle Valo 	 * At the same time, we check if there's a preferred TSF in the case
282e705c121SKalle Valo 	 * that we should share it with another interface.
283e705c121SKalle Valo 	 */
284e705c121SKalle Valo 
285e705c121SKalle Valo 	/* Currently, MAC ID 0 should be used only for the managed/IBSS vif */
286e705c121SKalle Valo 	switch (vif->type) {
287e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
288e705c121SKalle Valo 		break;
289e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
290e705c121SKalle Valo 		if (!vif->p2p)
291e705c121SKalle Valo 			break;
292e705c121SKalle Valo 		/* fall through */
293e705c121SKalle Valo 	default:
294e705c121SKalle Valo 		__clear_bit(0, data.available_mac_ids);
295e705c121SKalle Valo 	}
296e705c121SKalle Valo 
297e705c121SKalle Valo 	ieee80211_iterate_active_interfaces_atomic(
298e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
299e705c121SKalle Valo 		iwl_mvm_mac_iface_iterator, &data);
300e705c121SKalle Valo 
301e705c121SKalle Valo 	/*
302e705c121SKalle Valo 	 * In the case we're getting here during resume, it's similar to
303e705c121SKalle Valo 	 * firmware restart, and with RESUME_ALL the iterator will find
304e705c121SKalle Valo 	 * the vif being added already.
305e705c121SKalle Valo 	 * We don't want to reassign any IDs in either case since doing
306e705c121SKalle Valo 	 * so would probably assign different IDs (as interfaces aren't
307e705c121SKalle Valo 	 * necessarily added in the same order), but the old IDs were
308e705c121SKalle Valo 	 * preserved anyway, so skip ID assignment for both resume and
309e705c121SKalle Valo 	 * recovery.
310e705c121SKalle Valo 	 */
311e705c121SKalle Valo 	if (data.found_vif)
312e705c121SKalle Valo 		return 0;
313e705c121SKalle Valo 
314e705c121SKalle Valo 	/* Therefore, in recovery, we can't get here */
315e705c121SKalle Valo 	if (WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
316e705c121SKalle Valo 		return -EBUSY;
317e705c121SKalle Valo 
318e705c121SKalle Valo 	mvmvif->id = find_first_bit(data.available_mac_ids,
319e705c121SKalle Valo 				    NUM_MAC_INDEX_DRIVER);
320e705c121SKalle Valo 	if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
321e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
322e705c121SKalle Valo 		ret = -EIO;
323e705c121SKalle Valo 		goto exit_fail;
324e705c121SKalle Valo 	}
325e705c121SKalle Valo 
326e705c121SKalle Valo 	if (data.preferred_tsf != NUM_TSF_IDS)
327e705c121SKalle Valo 		mvmvif->tsf_id = data.preferred_tsf;
328e705c121SKalle Valo 	else
329e705c121SKalle Valo 		mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
330e705c121SKalle Valo 						NUM_TSF_IDS);
331e705c121SKalle Valo 	if (mvmvif->tsf_id == NUM_TSF_IDS) {
332e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
333e705c121SKalle Valo 		ret = -EIO;
334e705c121SKalle Valo 		goto exit_fail;
335e705c121SKalle Valo 	}
336e705c121SKalle Valo 
337e705c121SKalle Valo 	mvmvif->color = 0;
338e705c121SKalle Valo 
339e705c121SKalle Valo 	INIT_LIST_HEAD(&mvmvif->time_event_data.list);
340e705c121SKalle Valo 	mvmvif->time_event_data.id = TE_MAX;
341e705c121SKalle Valo 
3423f7fbc8cSJohannes Berg 	/* No need to allocate data queues to P2P Device MAC and NAN.*/
3433f7fbc8cSJohannes Berg 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
344e705c121SKalle Valo 		return 0;
345e705c121SKalle Valo 
346e705c121SKalle Valo 	/* Allocate the CAB queue for softAP and GO interfaces */
347fc07bd8cSSara Sharon 	if (vif->type == NL80211_IFTYPE_AP ||
348fc07bd8cSSara Sharon 	    vif->type == NL80211_IFTYPE_ADHOC) {
349e2af3fabSSara Sharon 		/*
350e2af3fabSSara Sharon 		 * For TVQM this will be overwritten later with the FW assigned
351e2af3fabSSara Sharon 		 * queue value (when queue is enabled).
352e2af3fabSSara Sharon 		 */
353c8f54701SJohannes Berg 		mvmvif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
354e705c121SKalle Valo 	}
355e705c121SKalle Valo 
3560ae98812SSara Sharon 	mvmvif->bcast_sta.sta_id = IWL_MVM_INVALID_STA;
3570ae98812SSara Sharon 	mvmvif->mcast_sta.sta_id = IWL_MVM_INVALID_STA;
3580ae98812SSara Sharon 	mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
359e705c121SKalle Valo 
360e705c121SKalle Valo 	for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
361e705c121SKalle Valo 		mvmvif->smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
362e705c121SKalle Valo 
363e705c121SKalle Valo 	return 0;
364e705c121SKalle Valo 
365e705c121SKalle Valo exit_fail:
366e705c121SKalle Valo 	memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
367e705c121SKalle Valo 	return ret;
368e705c121SKalle Valo }
369e705c121SKalle Valo 
370e705c121SKalle Valo static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
371e705c121SKalle Valo 			      struct ieee80211_vif *vif,
37257fbcce3SJohannes Berg 			      enum nl80211_band band,
373e705c121SKalle Valo 			      u8 *cck_rates, u8 *ofdm_rates)
374e705c121SKalle Valo {
375e705c121SKalle Valo 	struct ieee80211_supported_band *sband;
376e705c121SKalle Valo 	unsigned long basic = vif->bss_conf.basic_rates;
377e705c121SKalle Valo 	int lowest_present_ofdm = 100;
378e705c121SKalle Valo 	int lowest_present_cck = 100;
379e705c121SKalle Valo 	u8 cck = 0;
380e705c121SKalle Valo 	u8 ofdm = 0;
381e705c121SKalle Valo 	int i;
382e705c121SKalle Valo 
383e705c121SKalle Valo 	sband = mvm->hw->wiphy->bands[band];
384e705c121SKalle Valo 
385e705c121SKalle Valo 	for_each_set_bit(i, &basic, BITS_PER_LONG) {
386e705c121SKalle Valo 		int hw = sband->bitrates[i].hw_value;
387e705c121SKalle Valo 		if (hw >= IWL_FIRST_OFDM_RATE) {
388e705c121SKalle Valo 			ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
389e705c121SKalle Valo 			if (lowest_present_ofdm > hw)
390e705c121SKalle Valo 				lowest_present_ofdm = hw;
391e705c121SKalle Valo 		} else {
392e705c121SKalle Valo 			BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
393e705c121SKalle Valo 
394e705c121SKalle Valo 			cck |= BIT(hw);
395e705c121SKalle Valo 			if (lowest_present_cck > hw)
396e705c121SKalle Valo 				lowest_present_cck = hw;
397e705c121SKalle Valo 		}
398e705c121SKalle Valo 	}
399e705c121SKalle Valo 
400e705c121SKalle Valo 	/*
401e705c121SKalle Valo 	 * Now we've got the basic rates as bitmaps in the ofdm and cck
402e705c121SKalle Valo 	 * variables. This isn't sufficient though, as there might not
403e705c121SKalle Valo 	 * be all the right rates in the bitmap. E.g. if the only basic
404e705c121SKalle Valo 	 * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
405e705c121SKalle Valo 	 * and 6 Mbps because the 802.11-2007 standard says in 9.6:
406e705c121SKalle Valo 	 *
407e705c121SKalle Valo 	 *    [...] a STA responding to a received frame shall transmit
408e705c121SKalle Valo 	 *    its Control Response frame [...] at the highest rate in the
409e705c121SKalle Valo 	 *    BSSBasicRateSet parameter that is less than or equal to the
410e705c121SKalle Valo 	 *    rate of the immediately previous frame in the frame exchange
411e705c121SKalle Valo 	 *    sequence ([...]) and that is of the same modulation class
412e705c121SKalle Valo 	 *    ([...]) as the received frame. If no rate contained in the
413e705c121SKalle Valo 	 *    BSSBasicRateSet parameter meets these conditions, then the
414e705c121SKalle Valo 	 *    control frame sent in response to a received frame shall be
415e705c121SKalle Valo 	 *    transmitted at the highest mandatory rate of the PHY that is
416e705c121SKalle Valo 	 *    less than or equal to the rate of the received frame, and
417e705c121SKalle Valo 	 *    that is of the same modulation class as the received frame.
418e705c121SKalle Valo 	 *
419e705c121SKalle Valo 	 * As a consequence, we need to add all mandatory rates that are
420e705c121SKalle Valo 	 * lower than all of the basic rates to these bitmaps.
421e705c121SKalle Valo 	 */
422e705c121SKalle Valo 
423e705c121SKalle Valo 	if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
424e705c121SKalle Valo 		ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
425e705c121SKalle Valo 	if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
426e705c121SKalle Valo 		ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
427e705c121SKalle Valo 	/* 6M already there or needed so always add */
428e705c121SKalle Valo 	ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
429e705c121SKalle Valo 
430e705c121SKalle Valo 	/*
431e705c121SKalle Valo 	 * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
432e705c121SKalle Valo 	 * Note, however:
433e705c121SKalle Valo 	 *  - if no CCK rates are basic, it must be ERP since there must
434e705c121SKalle Valo 	 *    be some basic rates at all, so they're OFDM => ERP PHY
435e705c121SKalle Valo 	 *    (or we're in 5 GHz, and the cck bitmap will never be used)
436e705c121SKalle Valo 	 *  - if 11M is a basic rate, it must be ERP as well, so add 5.5M
437e705c121SKalle Valo 	 *  - if 5.5M is basic, 1M and 2M are mandatory
438e705c121SKalle Valo 	 *  - if 2M is basic, 1M is mandatory
439e705c121SKalle Valo 	 *  - if 1M is basic, that's the only valid ACK rate.
440e705c121SKalle Valo 	 * As a consequence, it's not as complicated as it sounds, just add
441e705c121SKalle Valo 	 * any lower rates to the ACK rate bitmap.
442e705c121SKalle Valo 	 */
443e705c121SKalle Valo 	if (IWL_RATE_11M_INDEX < lowest_present_cck)
444e705c121SKalle Valo 		cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
445e705c121SKalle Valo 	if (IWL_RATE_5M_INDEX < lowest_present_cck)
446e705c121SKalle Valo 		cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
447e705c121SKalle Valo 	if (IWL_RATE_2M_INDEX < lowest_present_cck)
448e705c121SKalle Valo 		cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
449e705c121SKalle Valo 	/* 1M already there or needed so always add */
450e705c121SKalle Valo 	cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
451e705c121SKalle Valo 
452e705c121SKalle Valo 	*cck_rates = cck;
453e705c121SKalle Valo 	*ofdm_rates = ofdm;
454e705c121SKalle Valo }
455e705c121SKalle Valo 
456e705c121SKalle Valo static void iwl_mvm_mac_ctxt_set_ht_flags(struct iwl_mvm *mvm,
457e705c121SKalle Valo 					 struct ieee80211_vif *vif,
458e705c121SKalle Valo 					 struct iwl_mac_ctx_cmd *cmd)
459e705c121SKalle Valo {
460e705c121SKalle Valo 	/* for both sta and ap, ht_operation_mode hold the protection_mode */
461e705c121SKalle Valo 	u8 protection_mode = vif->bss_conf.ht_operation_mode &
462e705c121SKalle Valo 				 IEEE80211_HT_OP_MODE_PROTECTION;
463e705c121SKalle Valo 	/* The fw does not distinguish between ht and fat */
464e705c121SKalle Valo 	u32 ht_flag = MAC_PROT_FLG_HT_PROT | MAC_PROT_FLG_FAT_PROT;
465e705c121SKalle Valo 
466e705c121SKalle Valo 	IWL_DEBUG_RATE(mvm, "protection mode set to %d\n", protection_mode);
467e705c121SKalle Valo 	/*
468e705c121SKalle Valo 	 * See section 9.23.3.1 of IEEE 80211-2012.
469e705c121SKalle Valo 	 * Nongreenfield HT STAs Present is not supported.
470e705c121SKalle Valo 	 */
471e705c121SKalle Valo 	switch (protection_mode) {
472e705c121SKalle Valo 	case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
473e705c121SKalle Valo 		break;
474e705c121SKalle Valo 	case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
475e705c121SKalle Valo 	case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
476e705c121SKalle Valo 		cmd->protection_flags |= cpu_to_le32(ht_flag);
477e705c121SKalle Valo 		break;
478e705c121SKalle Valo 	case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
479e705c121SKalle Valo 		/* Protect when channel wider than 20MHz */
480e705c121SKalle Valo 		if (vif->bss_conf.chandef.width > NL80211_CHAN_WIDTH_20)
481e705c121SKalle Valo 			cmd->protection_flags |= cpu_to_le32(ht_flag);
482e705c121SKalle Valo 		break;
483e705c121SKalle Valo 	default:
484e705c121SKalle Valo 		IWL_ERR(mvm, "Illegal protection mode %d\n",
485e705c121SKalle Valo 			protection_mode);
486e705c121SKalle Valo 		break;
487e705c121SKalle Valo 	}
488e705c121SKalle Valo }
489e705c121SKalle Valo 
490e705c121SKalle Valo static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
491e705c121SKalle Valo 					struct ieee80211_vif *vif,
492e705c121SKalle Valo 					struct iwl_mac_ctx_cmd *cmd,
493e705c121SKalle Valo 					const u8 *bssid_override,
494e705c121SKalle Valo 					u32 action)
495e705c121SKalle Valo {
496e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
497e705c121SKalle Valo 	struct ieee80211_chanctx_conf *chanctx;
498e705c121SKalle Valo 	bool ht_enabled = !!(vif->bss_conf.ht_operation_mode &
499e705c121SKalle Valo 			     IEEE80211_HT_OP_MODE_PROTECTION);
500e705c121SKalle Valo 	u8 cck_ack_rates, ofdm_ack_rates;
501e705c121SKalle Valo 	const u8 *bssid = bssid_override ?: vif->bss_conf.bssid;
502e705c121SKalle Valo 	int i;
503e705c121SKalle Valo 
504e705c121SKalle Valo 	cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
505e705c121SKalle Valo 							    mvmvif->color));
506e705c121SKalle Valo 	cmd->action = cpu_to_le32(action);
507e705c121SKalle Valo 
508e705c121SKalle Valo 	switch (vif->type) {
509e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
510e705c121SKalle Valo 		if (vif->p2p)
511e705c121SKalle Valo 			cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_STA);
512e705c121SKalle Valo 		else
513e705c121SKalle Valo 			cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_BSS_STA);
514e705c121SKalle Valo 		break;
515e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
516e705c121SKalle Valo 		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_GO);
517e705c121SKalle Valo 		break;
518e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
519e705c121SKalle Valo 		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_LISTENER);
520e705c121SKalle Valo 		break;
521e705c121SKalle Valo 	case NL80211_IFTYPE_P2P_DEVICE:
522e705c121SKalle Valo 		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_DEVICE);
523e705c121SKalle Valo 		break;
524e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
525e705c121SKalle Valo 		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_IBSS);
526e705c121SKalle Valo 		break;
527e705c121SKalle Valo 	default:
528e705c121SKalle Valo 		WARN_ON_ONCE(1);
529e705c121SKalle Valo 	}
530e705c121SKalle Valo 
531e705c121SKalle Valo 	cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
532e705c121SKalle Valo 
533e705c121SKalle Valo 	memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
534e705c121SKalle Valo 
535e705c121SKalle Valo 	if (bssid)
536e705c121SKalle Valo 		memcpy(cmd->bssid_addr, bssid, ETH_ALEN);
537e705c121SKalle Valo 	else
538e705c121SKalle Valo 		eth_broadcast_addr(cmd->bssid_addr);
539e705c121SKalle Valo 
540e705c121SKalle Valo 	rcu_read_lock();
541e705c121SKalle Valo 	chanctx = rcu_dereference(vif->chanctx_conf);
542e705c121SKalle Valo 	iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
54357fbcce3SJohannes Berg 					    : NL80211_BAND_2GHZ,
544e705c121SKalle Valo 			  &cck_ack_rates, &ofdm_ack_rates);
545e705c121SKalle Valo 	rcu_read_unlock();
546e705c121SKalle Valo 
547e705c121SKalle Valo 	cmd->cck_rates = cpu_to_le32((u32)cck_ack_rates);
548e705c121SKalle Valo 	cmd->ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
549e705c121SKalle Valo 
550e705c121SKalle Valo 	cmd->cck_short_preamble =
551e705c121SKalle Valo 		cpu_to_le32(vif->bss_conf.use_short_preamble ?
552e705c121SKalle Valo 			    MAC_FLG_SHORT_PREAMBLE : 0);
553e705c121SKalle Valo 	cmd->short_slot =
554e705c121SKalle Valo 		cpu_to_le32(vif->bss_conf.use_short_slot ?
555e705c121SKalle Valo 			    MAC_FLG_SHORT_SLOT : 0);
556e705c121SKalle Valo 
557bd6f5bd7SAyala Beker 	cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
558bd6f5bd7SAyala Beker 
559e705c121SKalle Valo 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
560cf6c6ea3SEmmanuel Grumbach 		u8 txf = iwl_mvm_mac_ac_to_tx_fifo(mvm, i);
561b5e2fe35SNaftali Goldstein 		u8 ucode_ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);
562e705c121SKalle Valo 
563b5e2fe35SNaftali Goldstein 		cmd->ac[ucode_ac].cw_min =
564e705c121SKalle Valo 			cpu_to_le16(mvmvif->queue_params[i].cw_min);
565b5e2fe35SNaftali Goldstein 		cmd->ac[ucode_ac].cw_max =
566e705c121SKalle Valo 			cpu_to_le16(mvmvif->queue_params[i].cw_max);
567b5e2fe35SNaftali Goldstein 		cmd->ac[ucode_ac].edca_txop =
568e705c121SKalle Valo 			cpu_to_le16(mvmvif->queue_params[i].txop * 32);
569b5e2fe35SNaftali Goldstein 		cmd->ac[ucode_ac].aifsn = mvmvif->queue_params[i].aifs;
570b5e2fe35SNaftali Goldstein 		cmd->ac[ucode_ac].fifos_mask = BIT(txf);
571e705c121SKalle Valo 	}
572e705c121SKalle Valo 
573e705c121SKalle Valo 	if (vif->bss_conf.qos)
574e705c121SKalle Valo 		cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
575e705c121SKalle Valo 
576e705c121SKalle Valo 	if (vif->bss_conf.use_cts_prot)
577e705c121SKalle Valo 		cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
578e705c121SKalle Valo 
579e705c121SKalle Valo 	IWL_DEBUG_RATE(mvm, "use_cts_prot %d, ht_operation_mode %d\n",
580e705c121SKalle Valo 		       vif->bss_conf.use_cts_prot,
581e705c121SKalle Valo 		       vif->bss_conf.ht_operation_mode);
582e705c121SKalle Valo 	if (vif->bss_conf.chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
583e705c121SKalle Valo 		cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
584e705c121SKalle Valo 	if (ht_enabled)
585e705c121SKalle Valo 		iwl_mvm_mac_ctxt_set_ht_flags(mvm, vif, cmd);
586e705c121SKalle Valo }
587e705c121SKalle Valo 
588e705c121SKalle Valo static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
589e705c121SKalle Valo 				     struct iwl_mac_ctx_cmd *cmd)
590e705c121SKalle Valo {
591e705c121SKalle Valo 	int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
592e705c121SKalle Valo 				       sizeof(*cmd), cmd);
593e705c121SKalle Valo 	if (ret)
594e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send MAC context (action:%d): %d\n",
595e705c121SKalle Valo 			le32_to_cpu(cmd->action), ret);
596e705c121SKalle Valo 	return ret;
597e705c121SKalle Valo }
598e705c121SKalle Valo 
599e705c121SKalle Valo static int iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm *mvm,
600e705c121SKalle Valo 				    struct ieee80211_vif *vif,
601e705c121SKalle Valo 				    u32 action, bool force_assoc_off,
602e705c121SKalle Valo 				    const u8 *bssid_override)
603e705c121SKalle Valo {
604e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd = {};
605e705c121SKalle Valo 	struct iwl_mac_data_sta *ctxt_sta;
606e705c121SKalle Valo 
607e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_STATION);
608e705c121SKalle Valo 
609e705c121SKalle Valo 	/* Fill the common data for all mac context types */
610e705c121SKalle Valo 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, bssid_override, action);
611e705c121SKalle Valo 
612e705c121SKalle Valo 	if (vif->p2p) {
613e705c121SKalle Valo 		struct ieee80211_p2p_noa_attr *noa =
614e705c121SKalle Valo 			&vif->bss_conf.p2p_noa_attr;
615e705c121SKalle Valo 
616e705c121SKalle Valo 		cmd.p2p_sta.ctwin = cpu_to_le32(noa->oppps_ctwindow &
617e705c121SKalle Valo 					IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
618e705c121SKalle Valo 		ctxt_sta = &cmd.p2p_sta.sta;
619e705c121SKalle Valo 	} else {
620e705c121SKalle Valo 		ctxt_sta = &cmd.sta;
621e705c121SKalle Valo 	}
622e705c121SKalle Valo 
623e705c121SKalle Valo 	/* We need the dtim_period to set the MAC as associated */
624e705c121SKalle Valo 	if (vif->bss_conf.assoc && vif->bss_conf.dtim_period &&
625e705c121SKalle Valo 	    !force_assoc_off) {
626e705c121SKalle Valo 		u32 dtim_offs;
627e705c121SKalle Valo 
628e705c121SKalle Valo 		/*
629e705c121SKalle Valo 		 * The DTIM count counts down, so when it is N that means N
630e705c121SKalle Valo 		 * more beacon intervals happen until the DTIM TBTT. Therefore
631e705c121SKalle Valo 		 * add this to the current time. If that ends up being in the
632e705c121SKalle Valo 		 * future, the firmware will handle it.
633e705c121SKalle Valo 		 *
634e705c121SKalle Valo 		 * Also note that the system_timestamp (which we get here as
635e705c121SKalle Valo 		 * "sync_device_ts") and TSF timestamp aren't at exactly the
636e705c121SKalle Valo 		 * same offset in the frame -- the TSF is at the first symbol
637e705c121SKalle Valo 		 * of the TSF, the system timestamp is at signal acquisition
638e705c121SKalle Valo 		 * time. This means there's an offset between them of at most
639e705c121SKalle Valo 		 * a few hundred microseconds (24 * 8 bits + PLCP time gives
640e705c121SKalle Valo 		 * 384us in the longest case), this is currently not relevant
641e705c121SKalle Valo 		 * as the firmware wakes up around 2ms before the TBTT.
642e705c121SKalle Valo 		 */
643e705c121SKalle Valo 		dtim_offs = vif->bss_conf.sync_dtim_count *
644e705c121SKalle Valo 				vif->bss_conf.beacon_int;
645e705c121SKalle Valo 		/* convert TU to usecs */
646e705c121SKalle Valo 		dtim_offs *= 1024;
647e705c121SKalle Valo 
648e705c121SKalle Valo 		ctxt_sta->dtim_tsf =
649e705c121SKalle Valo 			cpu_to_le64(vif->bss_conf.sync_tsf + dtim_offs);
650e705c121SKalle Valo 		ctxt_sta->dtim_time =
651e705c121SKalle Valo 			cpu_to_le32(vif->bss_conf.sync_device_ts + dtim_offs);
652b5669727SAvraham Stern 		ctxt_sta->assoc_beacon_arrive_time =
653b5669727SAvraham Stern 			cpu_to_le32(vif->bss_conf.sync_device_ts);
654e705c121SKalle Valo 
655e705c121SKalle Valo 		IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
656e705c121SKalle Valo 			       le64_to_cpu(ctxt_sta->dtim_tsf),
657e705c121SKalle Valo 			       le32_to_cpu(ctxt_sta->dtim_time),
658e705c121SKalle Valo 			       dtim_offs);
659e705c121SKalle Valo 
660e705c121SKalle Valo 		ctxt_sta->is_assoc = cpu_to_le32(1);
661e705c121SKalle Valo 	} else {
662e705c121SKalle Valo 		ctxt_sta->is_assoc = cpu_to_le32(0);
663e705c121SKalle Valo 
664e705c121SKalle Valo 		/* Allow beacons to pass through as long as we are not
665e705c121SKalle Valo 		 * associated, or we do not have dtim period information.
666e705c121SKalle Valo 		 */
667e705c121SKalle Valo 		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
668e705c121SKalle Valo 	}
669e705c121SKalle Valo 
670e705c121SKalle Valo 	ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
671e705c121SKalle Valo 	ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
672e705c121SKalle Valo 					      vif->bss_conf.dtim_period);
673e705c121SKalle Valo 
674e705c121SKalle Valo 	ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
675e705c121SKalle Valo 	ctxt_sta->assoc_id = cpu_to_le32(vif->bss_conf.aid);
676e705c121SKalle Valo 
677e705c121SKalle Valo 	if (vif->probe_req_reg && vif->bss_conf.assoc && vif->p2p)
678e705c121SKalle Valo 		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
679e705c121SKalle Valo 
6809394662aSLiad Kaufman 	if (vif->bss_conf.he_support && !iwlwifi_mod_params.disable_11ax) {
681514c3069SLuca Coelho 		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_11AX);
682d374f315SEmmanuel Grumbach 		if (vif->bss_conf.twt_requester && IWL_MVM_USE_TWT)
683866a6a85SEmmanuel Grumbach 			ctxt_sta->data_policy |= cpu_to_le32(TWT_SUPPORTED);
684d91d9b94SEmmanuel Grumbach 	}
685d91d9b94SEmmanuel Grumbach 
686514c3069SLuca Coelho 
687e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
688e705c121SKalle Valo }
689e705c121SKalle Valo 
690e705c121SKalle Valo static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
691e705c121SKalle Valo 					 struct ieee80211_vif *vif,
692e705c121SKalle Valo 					 u32 action)
693e705c121SKalle Valo {
694e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd = {};
695b13f43a4SEmmanuel Grumbach 	u32 tfd_queue_msk = BIT(mvm->snif_queue);
696c8f54701SJohannes Berg 	int ret;
697e705c121SKalle Valo 
698e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
699e705c121SKalle Valo 
700e705c121SKalle Valo 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
701e705c121SKalle Valo 
702e705c121SKalle Valo 	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
703e705c121SKalle Valo 				       MAC_FILTER_IN_CONTROL_AND_MGMT |
704e705c121SKalle Valo 				       MAC_FILTER_IN_BEACON |
705e705c121SKalle Valo 				       MAC_FILTER_IN_PROBE_REQUEST |
706e705c121SKalle Valo 				       MAC_FILTER_IN_CRC32);
707e705c121SKalle Valo 	ieee80211_hw_set(mvm->hw, RX_INCLUDES_FCS);
708e705c121SKalle Valo 
7090e39eb03SChaya Rachel Ivgi 	/* Allocate sniffer station */
7100e39eb03SChaya Rachel Ivgi 	ret = iwl_mvm_allocate_int_sta(mvm, &mvm->snif_sta, tfd_queue_msk,
711ced19f26SSara Sharon 				       vif->type, IWL_STA_GENERAL_PURPOSE);
7120e39eb03SChaya Rachel Ivgi 	if (ret)
7130e39eb03SChaya Rachel Ivgi 		return ret;
7140e39eb03SChaya Rachel Ivgi 
715e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
716e705c121SKalle Valo }
717e705c121SKalle Valo 
718e705c121SKalle Valo static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
719e705c121SKalle Valo 				     struct ieee80211_vif *vif,
720e705c121SKalle Valo 				     u32 action)
721e705c121SKalle Valo {
722e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
723e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd = {};
724e705c121SKalle Valo 
725e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
726e705c121SKalle Valo 
727e705c121SKalle Valo 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
728e705c121SKalle Valo 
729e705c121SKalle Valo 	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
730e705c121SKalle Valo 				       MAC_FILTER_IN_PROBE_REQUEST);
731e705c121SKalle Valo 
732e705c121SKalle Valo 	/* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */
733e705c121SKalle Valo 	cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
734e705c121SKalle Valo 
735e705c121SKalle Valo 	/* TODO: Assumes that the beacon id == mac context id */
736e705c121SKalle Valo 	cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
737e705c121SKalle Valo 
738e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
739e705c121SKalle Valo }
740e705c121SKalle Valo 
741e705c121SKalle Valo struct iwl_mvm_go_iterator_data {
742e705c121SKalle Valo 	bool go_active;
743e705c121SKalle Valo };
744e705c121SKalle Valo 
745e705c121SKalle Valo static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
746e705c121SKalle Valo {
747e705c121SKalle Valo 	struct iwl_mvm_go_iterator_data *data = _data;
748e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
749e705c121SKalle Valo 
750e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
751e705c121SKalle Valo 	    mvmvif->ap_ibss_active)
752e705c121SKalle Valo 		data->go_active = true;
753e705c121SKalle Valo }
754e705c121SKalle Valo 
755e705c121SKalle Valo static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
756e705c121SKalle Valo 					   struct ieee80211_vif *vif,
757e705c121SKalle Valo 					   u32 action)
758e705c121SKalle Valo {
759e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd = {};
760e705c121SKalle Valo 	struct iwl_mvm_go_iterator_data data = {};
761e705c121SKalle Valo 
762e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
763e705c121SKalle Valo 
764e705c121SKalle Valo 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
765e705c121SKalle Valo 
766e705c121SKalle Valo 	/* Override the filter flags to accept only probe requests */
767e705c121SKalle Valo 	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
768e705c121SKalle Valo 
769e705c121SKalle Valo 	/*
770e705c121SKalle Valo 	 * This flag should be set to true when the P2P Device is
771e705c121SKalle Valo 	 * discoverable and there is at least another active P2P GO. Settings
772e705c121SKalle Valo 	 * this flag will allow the P2P Device to be discoverable on other
773e705c121SKalle Valo 	 * channels in addition to its listen channel.
774e705c121SKalle Valo 	 * Note that this flag should not be set in other cases as it opens the
775e705c121SKalle Valo 	 * Rx filters on all MAC and increases the number of interrupts.
776e705c121SKalle Valo 	 */
777e705c121SKalle Valo 	ieee80211_iterate_active_interfaces_atomic(
778e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
779e705c121SKalle Valo 		iwl_mvm_go_iterator, &data);
780e705c121SKalle Valo 
781e705c121SKalle Valo 	cmd.p2p_dev.is_disc_extended = cpu_to_le32(data.go_active ? 1 : 0);
782e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
783e705c121SKalle Valo }
784e705c121SKalle Valo 
785138664a3SSara Sharon void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
7868364fbb4SSara Sharon 			      __le32 *tim_index, __le32 *tim_size,
787e705c121SKalle Valo 			      u8 *beacon, u32 frame_size)
788e705c121SKalle Valo {
789e705c121SKalle Valo 	u32 tim_idx;
790e705c121SKalle Valo 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
791e705c121SKalle Valo 
792e705c121SKalle Valo 	/* The index is relative to frame start but we start looking at the
793e705c121SKalle Valo 	 * variable-length part of the beacon. */
794e705c121SKalle Valo 	tim_idx = mgmt->u.beacon.variable - beacon;
795e705c121SKalle Valo 
796e705c121SKalle Valo 	/* Parse variable-length elements of beacon to find WLAN_EID_TIM */
797e705c121SKalle Valo 	while ((tim_idx < (frame_size - 2)) &&
798e705c121SKalle Valo 			(beacon[tim_idx] != WLAN_EID_TIM))
799e705c121SKalle Valo 		tim_idx += beacon[tim_idx+1] + 2;
800e705c121SKalle Valo 
801e705c121SKalle Valo 	/* If TIM field was found, set variables */
802e705c121SKalle Valo 	if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
8038364fbb4SSara Sharon 		*tim_index = cpu_to_le32(tim_idx);
8048364fbb4SSara Sharon 		*tim_size = cpu_to_le32((u32)beacon[tim_idx + 1]);
805e705c121SKalle Valo 	} else {
806e705c121SKalle Valo 		IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
807e705c121SKalle Valo 	}
808e705c121SKalle Valo }
809e705c121SKalle Valo 
810d3a108a4SAndrei Otcheretianski static u32 iwl_mvm_find_ie_offset(u8 *beacon, u8 eid, u32 frame_size)
811d3a108a4SAndrei Otcheretianski {
812d3a108a4SAndrei Otcheretianski 	struct ieee80211_mgmt *mgmt = (void *)beacon;
813d3a108a4SAndrei Otcheretianski 	const u8 *ie;
814d3a108a4SAndrei Otcheretianski 
815d3a108a4SAndrei Otcheretianski 	if (WARN_ON_ONCE(frame_size <= (mgmt->u.beacon.variable - beacon)))
816d3a108a4SAndrei Otcheretianski 		return 0;
817d3a108a4SAndrei Otcheretianski 
818d3a108a4SAndrei Otcheretianski 	frame_size -= mgmt->u.beacon.variable - beacon;
819d3a108a4SAndrei Otcheretianski 
820d3a108a4SAndrei Otcheretianski 	ie = cfg80211_find_ie(eid, mgmt->u.beacon.variable, frame_size);
821d3a108a4SAndrei Otcheretianski 	if (!ie)
822d3a108a4SAndrei Otcheretianski 		return 0;
823d3a108a4SAndrei Otcheretianski 
824d3a108a4SAndrei Otcheretianski 	return ie - beacon;
825d3a108a4SAndrei Otcheretianski }
826d3a108a4SAndrei Otcheretianski 
827138664a3SSara Sharon u8 iwl_mvm_mac_ctxt_get_lowest_rate(struct ieee80211_tx_info *info,
8286ca33f8bSHaim Dreyfuss 				    struct ieee80211_vif *vif)
8296ca33f8bSHaim Dreyfuss {
8306ca33f8bSHaim Dreyfuss 	u8 rate;
8316ca33f8bSHaim Dreyfuss 
8326ca33f8bSHaim Dreyfuss 	if (info->band == NL80211_BAND_5GHZ || vif->p2p)
8336ca33f8bSHaim Dreyfuss 		rate = IWL_FIRST_OFDM_RATE;
8346ca33f8bSHaim Dreyfuss 	else
8356ca33f8bSHaim Dreyfuss 		rate = IWL_FIRST_CCK_RATE;
8366ca33f8bSHaim Dreyfuss 
8376ca33f8bSHaim Dreyfuss 	return rate;
8386ca33f8bSHaim Dreyfuss }
8396ca33f8bSHaim Dreyfuss 
84009856582SLuca Coelho static void iwl_mvm_mac_ctxt_set_tx(struct iwl_mvm *mvm,
841e705c121SKalle Valo 				    struct ieee80211_vif *vif,
84209856582SLuca Coelho 				    struct sk_buff *beacon,
84309856582SLuca Coelho 				    struct iwl_tx_cmd *tx)
844e705c121SKalle Valo {
845e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
846e705c121SKalle Valo 	struct ieee80211_tx_info *info;
8476ca33f8bSHaim Dreyfuss 	u8 rate;
8486ca33f8bSHaim Dreyfuss 	u32 tx_flags;
849e705c121SKalle Valo 
850e705c121SKalle Valo 	info = IEEE80211_SKB_CB(beacon);
851e705c121SKalle Valo 
852e705c121SKalle Valo 	/* Set up TX command fields */
85309856582SLuca Coelho 	tx->len = cpu_to_le16((u16)beacon->len);
85409856582SLuca Coelho 	tx->sta_id = mvmvif->bcast_sta.sta_id;
85509856582SLuca Coelho 	tx->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
856e705c121SKalle Valo 	tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;
857e705c121SKalle Valo 	tx_flags |=
858e705c121SKalle Valo 		iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<
859e705c121SKalle Valo 						TX_CMD_FLG_BT_PRIO_POS;
86009856582SLuca Coelho 	tx->tx_flags = cpu_to_le32(tx_flags);
861e705c121SKalle Valo 
8621e3c3c35SEmmanuel Grumbach 	if (!fw_has_capa(&mvm->fw->ucode_capa,
863656fca00SAvraham Stern 			 IWL_UCODE_TLV_CAPA_BEACON_ANT_SELECTION))
864656fca00SAvraham Stern 		iwl_mvm_toggle_tx_ant(mvm, &mvm->mgmt_last_antenna_idx);
865e705c121SKalle Valo 
86609856582SLuca Coelho 	tx->rate_n_flags =
867e705c121SKalle Valo 		cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
868e705c121SKalle Valo 			    RATE_MCS_ANT_POS);
869e705c121SKalle Valo 
8706ca33f8bSHaim Dreyfuss 	rate = iwl_mvm_mac_ctxt_get_lowest_rate(info, vif);
871e705c121SKalle Valo 
87209856582SLuca Coelho 	tx->rate_n_flags |= cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(rate));
8736ca33f8bSHaim Dreyfuss 	if (rate == IWL_FIRST_CCK_RATE)
8746ca33f8bSHaim Dreyfuss 		tx->rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK);
8756ca33f8bSHaim Dreyfuss 
87609856582SLuca Coelho }
877e705c121SKalle Valo 
878138664a3SSara Sharon int iwl_mvm_mac_ctxt_send_beacon_cmd(struct iwl_mvm *mvm,
87909856582SLuca Coelho 				     struct sk_buff *beacon,
88009856582SLuca Coelho 				     void *data, int len)
88109856582SLuca Coelho {
88209856582SLuca Coelho 	struct iwl_host_cmd cmd = {
88309856582SLuca Coelho 		.id = BEACON_TEMPLATE_CMD,
88409856582SLuca Coelho 		.flags = CMD_ASYNC,
88509856582SLuca Coelho 	};
88609856582SLuca Coelho 
88709856582SLuca Coelho 	cmd.len[0] = len;
88809856582SLuca Coelho 	cmd.data[0] = data;
889e705c121SKalle Valo 	cmd.dataflags[0] = 0;
89009856582SLuca Coelho 	cmd.len[1] = beacon->len;
891e705c121SKalle Valo 	cmd.data[1] = beacon->data;
892e705c121SKalle Valo 	cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
893e705c121SKalle Valo 
894e705c121SKalle Valo 	return iwl_mvm_send_cmd(mvm, &cmd);
895e705c121SKalle Valo }
896e705c121SKalle Valo 
89709856582SLuca Coelho static int iwl_mvm_mac_ctxt_send_beacon_v6(struct iwl_mvm *mvm,
89809856582SLuca Coelho 					   struct ieee80211_vif *vif,
89909856582SLuca Coelho 					   struct sk_buff *beacon)
90009856582SLuca Coelho {
90109856582SLuca Coelho 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
90209856582SLuca Coelho 	struct iwl_mac_beacon_cmd_v6 beacon_cmd = {};
90309856582SLuca Coelho 
90409856582SLuca Coelho 	iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
90509856582SLuca Coelho 
90609856582SLuca Coelho 	beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
90709856582SLuca Coelho 
90809856582SLuca Coelho 	if (vif->type == NL80211_IFTYPE_AP)
90909856582SLuca Coelho 		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
91009856582SLuca Coelho 					 &beacon_cmd.tim_size,
91109856582SLuca Coelho 					 beacon->data, beacon->len);
91209856582SLuca Coelho 
91309856582SLuca Coelho 	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
91409856582SLuca Coelho 						sizeof(beacon_cmd));
91509856582SLuca Coelho }
91609856582SLuca Coelho 
91709856582SLuca Coelho static int iwl_mvm_mac_ctxt_send_beacon_v7(struct iwl_mvm *mvm,
91809856582SLuca Coelho 					   struct ieee80211_vif *vif,
91909856582SLuca Coelho 					   struct sk_buff *beacon)
92009856582SLuca Coelho {
92109856582SLuca Coelho 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
92209856582SLuca Coelho 	struct iwl_mac_beacon_cmd_v7 beacon_cmd = {};
92309856582SLuca Coelho 
92409856582SLuca Coelho 	iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
92509856582SLuca Coelho 
92609856582SLuca Coelho 	beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
92709856582SLuca Coelho 
92809856582SLuca Coelho 	if (vif->type == NL80211_IFTYPE_AP)
92909856582SLuca Coelho 		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
93009856582SLuca Coelho 					 &beacon_cmd.tim_size,
93109856582SLuca Coelho 					 beacon->data, beacon->len);
93209856582SLuca Coelho 
93309856582SLuca Coelho 	beacon_cmd.csa_offset =
93409856582SLuca Coelho 		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
93509856582SLuca Coelho 						   WLAN_EID_CHANNEL_SWITCH,
93609856582SLuca Coelho 						   beacon->len));
93709856582SLuca Coelho 	beacon_cmd.ecsa_offset =
93809856582SLuca Coelho 		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
93909856582SLuca Coelho 						   WLAN_EID_EXT_CHANSWITCH_ANN,
94009856582SLuca Coelho 						   beacon->len));
94109856582SLuca Coelho 
94209856582SLuca Coelho 	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
94309856582SLuca Coelho 						sizeof(beacon_cmd));
94409856582SLuca Coelho }
94509856582SLuca Coelho 
9466ca33f8bSHaim Dreyfuss static int iwl_mvm_mac_ctxt_send_beacon_v9(struct iwl_mvm *mvm,
94709856582SLuca Coelho 					   struct ieee80211_vif *vif,
94809856582SLuca Coelho 					   struct sk_buff *beacon)
94909856582SLuca Coelho {
95009856582SLuca Coelho 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
9516ca33f8bSHaim Dreyfuss 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(beacon);
95209856582SLuca Coelho 	struct iwl_mac_beacon_cmd beacon_cmd = {};
9536ca33f8bSHaim Dreyfuss 	u8 rate = iwl_mvm_mac_ctxt_get_lowest_rate(info, vif);
9546ca33f8bSHaim Dreyfuss 	u16 flags;
95509856582SLuca Coelho 
9566ca33f8bSHaim Dreyfuss 	flags = iwl_mvm_mac80211_idx_to_hwrate(rate);
9576ca33f8bSHaim Dreyfuss 
9586ca33f8bSHaim Dreyfuss 	if (rate == IWL_FIRST_CCK_RATE)
9596ca33f8bSHaim Dreyfuss 		flags |= IWL_MAC_BEACON_CCK;
9606ca33f8bSHaim Dreyfuss 
9616ca33f8bSHaim Dreyfuss 	beacon_cmd.flags = cpu_to_le16(flags);
96209856582SLuca Coelho 	beacon_cmd.byte_cnt = cpu_to_le16((u16)beacon->len);
96309856582SLuca Coelho 	beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
96409856582SLuca Coelho 
96509856582SLuca Coelho 	if (vif->type == NL80211_IFTYPE_AP)
9666ca33f8bSHaim Dreyfuss 		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
96709856582SLuca Coelho 					 &beacon_cmd.tim_size,
96809856582SLuca Coelho 					 beacon->data, beacon->len);
96909856582SLuca Coelho 
97009856582SLuca Coelho 	beacon_cmd.csa_offset =
97109856582SLuca Coelho 		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
97209856582SLuca Coelho 						   WLAN_EID_CHANNEL_SWITCH,
97309856582SLuca Coelho 						   beacon->len));
97409856582SLuca Coelho 	beacon_cmd.ecsa_offset =
97509856582SLuca Coelho 		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
97609856582SLuca Coelho 						   WLAN_EID_EXT_CHANSWITCH_ANN,
97709856582SLuca Coelho 						   beacon->len));
97809856582SLuca Coelho 
97909856582SLuca Coelho 	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
98009856582SLuca Coelho 						sizeof(beacon_cmd));
98109856582SLuca Coelho }
98209856582SLuca Coelho 
983138664a3SSara Sharon int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
98409856582SLuca Coelho 				 struct ieee80211_vif *vif,
98509856582SLuca Coelho 				 struct sk_buff *beacon)
98609856582SLuca Coelho {
98709856582SLuca Coelho 	if (WARN_ON(!beacon))
98809856582SLuca Coelho 		return -EINVAL;
98909856582SLuca Coelho 
99050386305SSara Sharon 	if (IWL_MVM_NON_TRANSMITTING_AP)
99150386305SSara Sharon 		return 0;
99250386305SSara Sharon 
99309856582SLuca Coelho 	if (!fw_has_capa(&mvm->fw->ucode_capa,
99409856582SLuca Coelho 			 IWL_UCODE_TLV_CAPA_CSA_AND_TBTT_OFFLOAD))
99509856582SLuca Coelho 		return iwl_mvm_mac_ctxt_send_beacon_v6(mvm, vif, beacon);
99609856582SLuca Coelho 
9976ca33f8bSHaim Dreyfuss 	if (fw_has_api(&mvm->fw->ucode_capa,
9986ca33f8bSHaim Dreyfuss 		       IWL_UCODE_TLV_API_NEW_BEACON_TEMPLATE))
9996ca33f8bSHaim Dreyfuss 		return iwl_mvm_mac_ctxt_send_beacon_v9(mvm, vif, beacon);
100009856582SLuca Coelho 
10016ca33f8bSHaim Dreyfuss 	return iwl_mvm_mac_ctxt_send_beacon_v7(mvm, vif, beacon);
100209856582SLuca Coelho }
100309856582SLuca Coelho 
1004e705c121SKalle Valo /* The beacon template for the AP/GO/IBSS has changed and needs update */
1005e705c121SKalle Valo int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
1006e705c121SKalle Valo 				    struct ieee80211_vif *vif)
1007e705c121SKalle Valo {
1008e705c121SKalle Valo 	struct sk_buff *beacon;
1009e705c121SKalle Valo 	int ret;
1010e705c121SKalle Valo 
1011e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_AP &&
1012e705c121SKalle Valo 		vif->type != NL80211_IFTYPE_ADHOC);
1013e705c121SKalle Valo 
1014e705c121SKalle Valo 	beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL);
1015e705c121SKalle Valo 	if (!beacon)
1016e705c121SKalle Valo 		return -ENOMEM;
1017e705c121SKalle Valo 
101890a12829SSara Sharon #ifdef CONFIG_IWLWIFI_DEBUGFS
101990a12829SSara Sharon 	if (mvm->beacon_inject_active)
102090a12829SSara Sharon 		return -EBUSY;
102190a12829SSara Sharon #endif
102290a12829SSara Sharon 
1023e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
1024e705c121SKalle Valo 	dev_kfree_skb(beacon);
1025e705c121SKalle Valo 	return ret;
1026e705c121SKalle Valo }
1027e705c121SKalle Valo 
1028e705c121SKalle Valo struct iwl_mvm_mac_ap_iterator_data {
1029e705c121SKalle Valo 	struct iwl_mvm *mvm;
1030e705c121SKalle Valo 	struct ieee80211_vif *vif;
1031e705c121SKalle Valo 	u32 beacon_device_ts;
1032e705c121SKalle Valo 	u16 beacon_int;
1033e705c121SKalle Valo };
1034e705c121SKalle Valo 
1035e705c121SKalle Valo /* Find the beacon_device_ts and beacon_int for a managed interface */
1036e705c121SKalle Valo static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
1037e705c121SKalle Valo 				    struct ieee80211_vif *vif)
1038e705c121SKalle Valo {
1039e705c121SKalle Valo 	struct iwl_mvm_mac_ap_iterator_data *data = _data;
1040e705c121SKalle Valo 
1041e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc)
1042e705c121SKalle Valo 		return;
1043e705c121SKalle Valo 
1044e705c121SKalle Valo 	/* Station client has higher priority over P2P client*/
1045e705c121SKalle Valo 	if (vif->p2p && data->beacon_device_ts)
1046e705c121SKalle Valo 		return;
1047e705c121SKalle Valo 
1048e705c121SKalle Valo 	data->beacon_device_ts = vif->bss_conf.sync_device_ts;
1049e705c121SKalle Valo 	data->beacon_int = vif->bss_conf.beacon_int;
1050e705c121SKalle Valo }
1051e705c121SKalle Valo 
1052e705c121SKalle Valo /*
1053e705c121SKalle Valo  * Fill the specific data for mac context of type AP of P2P GO
1054e705c121SKalle Valo  */
1055e705c121SKalle Valo static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
1056e705c121SKalle Valo 					 struct ieee80211_vif *vif,
1057186cd49aSJohannes Berg 					 struct iwl_mac_ctx_cmd *cmd,
1058e705c121SKalle Valo 					 struct iwl_mac_data_ap *ctxt_ap,
1059e705c121SKalle Valo 					 bool add)
1060e705c121SKalle Valo {
1061e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1062e705c121SKalle Valo 	struct iwl_mvm_mac_ap_iterator_data data = {
1063e705c121SKalle Valo 		.mvm = mvm,
1064e705c121SKalle Valo 		.vif = vif,
1065e705c121SKalle Valo 		.beacon_device_ts = 0
1066e705c121SKalle Valo 	};
1067e705c121SKalle Valo 
1068186cd49aSJohannes Berg 	/* in AP mode, the MCAST FIFO takes the EDCA params from VO */
1069186cd49aSJohannes Berg 	cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |= BIT(IWL_MVM_TX_FIFO_MCAST);
1070186cd49aSJohannes Berg 
1071186cd49aSJohannes Berg 	/*
1072186cd49aSJohannes Berg 	 * in AP mode, pass probe requests and beacons from other APs
1073186cd49aSJohannes Berg 	 * (needed for ht protection); when there're no any associated
1074186cd49aSJohannes Berg 	 * station don't ask FW to pass beacons to prevent unnecessary
1075186cd49aSJohannes Berg 	 * wake-ups.
1076186cd49aSJohannes Berg 	 */
1077186cd49aSJohannes Berg 	cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
1078186cd49aSJohannes Berg 	if (mvmvif->ap_assoc_sta_count || !mvm->drop_bcn_ap_mode) {
1079186cd49aSJohannes Berg 		cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
1080186cd49aSJohannes Berg 		IWL_DEBUG_HC(mvm, "Asking FW to pass beacons\n");
1081186cd49aSJohannes Berg 	} else {
1082186cd49aSJohannes Berg 		IWL_DEBUG_HC(mvm, "No need to receive beacons\n");
1083186cd49aSJohannes Berg 	}
1084186cd49aSJohannes Berg 
10853b5ee8ddSShaul Triebitz 	if (vif->bss_conf.he_support && !iwlwifi_mod_params.disable_11ax)
10863b5ee8ddSShaul Triebitz 		cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_11AX);
10873b5ee8ddSShaul Triebitz 
1088e705c121SKalle Valo 	ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
1089e705c121SKalle Valo 	ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
1090e705c121SKalle Valo 					     vif->bss_conf.dtim_period);
1091e705c121SKalle Valo 
1092ced19f26SSara Sharon 	if (!fw_has_api(&mvm->fw->ucode_capa,
1093ced19f26SSara Sharon 			IWL_UCODE_TLV_API_STA_TYPE))
1094cfbc6c4cSSara Sharon 		ctxt_ap->mcast_qid = cpu_to_le32(mvmvif->cab_queue);
1095e705c121SKalle Valo 
1096e705c121SKalle Valo 	/*
1097e705c121SKalle Valo 	 * Only set the beacon time when the MAC is being added, when we
1098e705c121SKalle Valo 	 * just modify the MAC then we should keep the time -- the firmware
1099e705c121SKalle Valo 	 * can otherwise have a "jumping" TBTT.
1100e705c121SKalle Valo 	 */
1101e705c121SKalle Valo 	if (add) {
1102e705c121SKalle Valo 		/*
1103e705c121SKalle Valo 		 * If there is a station/P2P client interface which is
1104e705c121SKalle Valo 		 * associated, set the AP's TBTT far enough from the station's
1105e705c121SKalle Valo 		 * TBTT. Otherwise, set it to the current system time
1106e705c121SKalle Valo 		 */
1107e705c121SKalle Valo 		ieee80211_iterate_active_interfaces_atomic(
1108e705c121SKalle Valo 			mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1109e705c121SKalle Valo 			iwl_mvm_mac_ap_iterator, &data);
1110e705c121SKalle Valo 
1111e705c121SKalle Valo 		if (data.beacon_device_ts) {
1112e705c121SKalle Valo 			u32 rand = (prandom_u32() % (64 - 36)) + 36;
1113e705c121SKalle Valo 			mvmvif->ap_beacon_time = data.beacon_device_ts +
1114e705c121SKalle Valo 				ieee80211_tu_to_usec(data.beacon_int * rand /
1115e705c121SKalle Valo 						     100);
1116e705c121SKalle Valo 		} else {
1117afc1e3b4SAvraham Stern 			mvmvif->ap_beacon_time = iwl_mvm_get_systime(mvm);
1118e705c121SKalle Valo 		}
1119e705c121SKalle Valo 	}
1120e705c121SKalle Valo 
1121e705c121SKalle Valo 	ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
1122e705c121SKalle Valo 	ctxt_ap->beacon_tsf = 0; /* unused */
1123e705c121SKalle Valo 
1124e705c121SKalle Valo 	/* TODO: Assume that the beacon id == mac context id */
1125e705c121SKalle Valo 	ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
1126e705c121SKalle Valo }
1127e705c121SKalle Valo 
1128e705c121SKalle Valo static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
1129e705c121SKalle Valo 				   struct ieee80211_vif *vif,
1130e705c121SKalle Valo 				   u32 action)
1131e705c121SKalle Valo {
1132e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd = {};
1133e705c121SKalle Valo 
1134e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
1135e705c121SKalle Valo 
1136e705c121SKalle Valo 	/* Fill the common data for all mac context types */
1137e705c121SKalle Valo 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1138e705c121SKalle Valo 
1139e705c121SKalle Valo 	/* Fill the data specific for ap mode */
1140186cd49aSJohannes Berg 	iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.ap,
1141e705c121SKalle Valo 				     action == FW_CTXT_ACTION_ADD);
1142e705c121SKalle Valo 
1143e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1144e705c121SKalle Valo }
1145e705c121SKalle Valo 
1146e705c121SKalle Valo static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
1147e705c121SKalle Valo 				   struct ieee80211_vif *vif,
1148e705c121SKalle Valo 				   u32 action)
1149e705c121SKalle Valo {
1150e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd = {};
1151e705c121SKalle Valo 	struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
1152e705c121SKalle Valo 
1153e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
1154e705c121SKalle Valo 
1155e705c121SKalle Valo 	/* Fill the common data for all mac context types */
1156e705c121SKalle Valo 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1157e705c121SKalle Valo 
1158e705c121SKalle Valo 	/* Fill the data specific for GO mode */
1159186cd49aSJohannes Berg 	iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.go.ap,
1160e705c121SKalle Valo 				     action == FW_CTXT_ACTION_ADD);
1161e705c121SKalle Valo 
1162e705c121SKalle Valo 	cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
1163e705c121SKalle Valo 					IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
1164e705c121SKalle Valo 	cmd.go.opp_ps_enabled =
1165e705c121SKalle Valo 			cpu_to_le32(!!(noa->oppps_ctwindow &
1166e705c121SKalle Valo 					IEEE80211_P2P_OPPPS_ENABLE_BIT));
1167e705c121SKalle Valo 
1168e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1169e705c121SKalle Valo }
1170e705c121SKalle Valo 
1171e705c121SKalle Valo static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1172e705c121SKalle Valo 				u32 action, bool force_assoc_off,
1173e705c121SKalle Valo 				const u8 *bssid_override)
1174e705c121SKalle Valo {
1175e705c121SKalle Valo 	switch (vif->type) {
1176e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
1177e705c121SKalle Valo 		return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
1178e705c121SKalle Valo 						force_assoc_off,
1179e705c121SKalle Valo 						bssid_override);
1180e705c121SKalle Valo 		break;
1181e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
1182e705c121SKalle Valo 		if (!vif->p2p)
1183e705c121SKalle Valo 			return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
1184e705c121SKalle Valo 		else
1185e705c121SKalle Valo 			return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
1186e705c121SKalle Valo 		break;
1187e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
1188e705c121SKalle Valo 		return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
1189e705c121SKalle Valo 	case NL80211_IFTYPE_P2P_DEVICE:
1190e705c121SKalle Valo 		return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
1191e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
1192e705c121SKalle Valo 		return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
1193e705c121SKalle Valo 	default:
1194e705c121SKalle Valo 		break;
1195e705c121SKalle Valo 	}
1196e705c121SKalle Valo 
1197e705c121SKalle Valo 	return -EOPNOTSUPP;
1198e705c121SKalle Valo }
1199e705c121SKalle Valo 
1200e705c121SKalle Valo int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1201e705c121SKalle Valo {
1202e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1203e705c121SKalle Valo 	int ret;
1204e705c121SKalle Valo 
1205e705c121SKalle Valo 	if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
1206e705c121SKalle Valo 		      vif->addr, ieee80211_vif_type_p2p(vif)))
1207e705c121SKalle Valo 		return -EIO;
1208e705c121SKalle Valo 
1209e705c121SKalle Valo 	ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
1210e705c121SKalle Valo 				   true, NULL);
1211e705c121SKalle Valo 	if (ret)
1212e705c121SKalle Valo 		return ret;
1213e705c121SKalle Valo 
1214e705c121SKalle Valo 	/* will only do anything at resume from D3 time */
1215e705c121SKalle Valo 	iwl_mvm_set_last_nonqos_seq(mvm, vif);
1216e705c121SKalle Valo 
1217e705c121SKalle Valo 	mvmvif->uploaded = true;
1218e705c121SKalle Valo 	return 0;
1219e705c121SKalle Valo }
1220e705c121SKalle Valo 
1221e705c121SKalle Valo int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1222e705c121SKalle Valo 			     bool force_assoc_off, const u8 *bssid_override)
1223e705c121SKalle Valo {
1224e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1225e705c121SKalle Valo 
1226e705c121SKalle Valo 	if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
1227e705c121SKalle Valo 		      vif->addr, ieee80211_vif_type_p2p(vif)))
1228e705c121SKalle Valo 		return -EIO;
1229e705c121SKalle Valo 
1230e705c121SKalle Valo 	return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
1231e705c121SKalle Valo 				    force_assoc_off, bssid_override);
1232e705c121SKalle Valo }
1233e705c121SKalle Valo 
1234e705c121SKalle Valo int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1235e705c121SKalle Valo {
1236e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1237e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd;
1238e705c121SKalle Valo 	int ret;
1239e705c121SKalle Valo 
1240e705c121SKalle Valo 	if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1241e705c121SKalle Valo 		      vif->addr, ieee80211_vif_type_p2p(vif)))
1242e705c121SKalle Valo 		return -EIO;
1243e705c121SKalle Valo 
1244e705c121SKalle Valo 	memset(&cmd, 0, sizeof(cmd));
1245e705c121SKalle Valo 
1246e705c121SKalle Valo 	cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1247e705c121SKalle Valo 							   mvmvif->color));
1248e705c121SKalle Valo 	cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1249e705c121SKalle Valo 
1250e705c121SKalle Valo 	ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
1251e705c121SKalle Valo 				   sizeof(cmd), &cmd);
1252e705c121SKalle Valo 	if (ret) {
1253e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
1254e705c121SKalle Valo 		return ret;
1255e705c121SKalle Valo 	}
1256e705c121SKalle Valo 
1257e705c121SKalle Valo 	mvmvif->uploaded = false;
1258e705c121SKalle Valo 
12590e39eb03SChaya Rachel Ivgi 	if (vif->type == NL80211_IFTYPE_MONITOR) {
1260e705c121SKalle Valo 		__clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, mvm->hw->flags);
12610e39eb03SChaya Rachel Ivgi 		iwl_mvm_dealloc_snif_sta(mvm);
12620e39eb03SChaya Rachel Ivgi 	}
1263e705c121SKalle Valo 
1264e705c121SKalle Valo 	return 0;
1265e705c121SKalle Valo }
1266e705c121SKalle Valo 
1267e705c121SKalle Valo static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
1268e705c121SKalle Valo 				   struct ieee80211_vif *csa_vif, u32 gp2,
1269e705c121SKalle Valo 				   bool tx_success)
1270e705c121SKalle Valo {
1271e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif =
1272e705c121SKalle Valo 			iwl_mvm_vif_from_mac80211(csa_vif);
1273e705c121SKalle Valo 
1274e705c121SKalle Valo 	/* Don't start to countdown from a failed beacon */
1275e705c121SKalle Valo 	if (!tx_success && !mvmvif->csa_countdown)
1276e705c121SKalle Valo 		return;
1277e705c121SKalle Valo 
1278e705c121SKalle Valo 	mvmvif->csa_countdown = true;
1279e705c121SKalle Valo 
1280e705c121SKalle Valo 	if (!ieee80211_csa_is_complete(csa_vif)) {
1281e705c121SKalle Valo 		int c = ieee80211_csa_update_counter(csa_vif);
1282e705c121SKalle Valo 
1283e705c121SKalle Valo 		iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif);
1284e705c121SKalle Valo 		if (csa_vif->p2p &&
1285e705c121SKalle Valo 		    !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2 &&
1286e705c121SKalle Valo 		    tx_success) {
1287e705c121SKalle Valo 			u32 rel_time = (c + 1) *
1288e705c121SKalle Valo 				       csa_vif->bss_conf.beacon_int -
1289e705c121SKalle Valo 				       IWL_MVM_CHANNEL_SWITCH_TIME_GO;
1290e705c121SKalle Valo 			u32 apply_time = gp2 + rel_time * 1024;
1291e705c121SKalle Valo 
1292e705c121SKalle Valo 			iwl_mvm_schedule_csa_period(mvm, csa_vif,
1293e705c121SKalle Valo 					 IWL_MVM_CHANNEL_SWITCH_TIME_GO -
1294e705c121SKalle Valo 					 IWL_MVM_CHANNEL_SWITCH_MARGIN,
1295e705c121SKalle Valo 					 apply_time);
1296e705c121SKalle Valo 		}
1297e705c121SKalle Valo 	} else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
1298e705c121SKalle Valo 		/* we don't have CSA NoA scheduled yet, switch now */
1299e705c121SKalle Valo 		ieee80211_csa_finish(csa_vif);
1300e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
1301e705c121SKalle Valo 	}
1302e705c121SKalle Valo }
1303e705c121SKalle Valo 
1304e705c121SKalle Valo void iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1305e705c121SKalle Valo 			     struct iwl_rx_cmd_buffer *rxb)
1306e705c121SKalle Valo {
1307e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1308e705c121SKalle Valo 	struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
130915e28c78SEmmanuel Grumbach 	struct iwl_extended_beacon_notif_v5 *beacon_v5 = (void *)pkt->data;
1310e705c121SKalle Valo 	struct ieee80211_vif *csa_vif;
1311e705c121SKalle Valo 	struct ieee80211_vif *tx_blocked_vif;
131212db294cSSara Sharon 	struct agg_tx_status *agg_status;
1313e705c121SKalle Valo 	u16 status;
1314e705c121SKalle Valo 
1315e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1316e705c121SKalle Valo 
1317e705c121SKalle Valo 	mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
1318e705c121SKalle Valo 
131915e28c78SEmmanuel Grumbach 	if (!iwl_mvm_is_short_beacon_notif_supported(mvm)) {
132015e28c78SEmmanuel Grumbach 		struct iwl_mvm_tx_resp *beacon_notify_hdr =
132115e28c78SEmmanuel Grumbach 			&beacon_v5->beacon_notify_hdr;
132215e28c78SEmmanuel Grumbach 
132315e28c78SEmmanuel Grumbach 		mvm->ibss_manager = beacon_v5->ibss_mgr_status != 0;
132412db294cSSara Sharon 		agg_status = iwl_mvm_get_agg_status(mvm, beacon_notify_hdr);
132512db294cSSara Sharon 		status = le16_to_cpu(agg_status->status) & TX_STATUS_MSK;
1326e705c121SKalle Valo 		IWL_DEBUG_RX(mvm,
1327677837b8SJohannes Berg 			     "beacon status %#x retries:%d tsf:0x%016llX gp2:0x%X rate:%d\n",
1328e705c121SKalle Valo 			     status, beacon_notify_hdr->failure_frame,
1329e705c121SKalle Valo 			     le64_to_cpu(beacon->tsf),
1330e705c121SKalle Valo 			     mvm->ap_last_beacon_gp2,
1331e705c121SKalle Valo 			     le32_to_cpu(beacon_notify_hdr->initial_rate));
133215e28c78SEmmanuel Grumbach 	} else {
133315e28c78SEmmanuel Grumbach 		mvm->ibss_manager = beacon->ibss_mgr_status != 0;
133415e28c78SEmmanuel Grumbach 		status = le32_to_cpu(beacon->status) & TX_STATUS_MSK;
133515e28c78SEmmanuel Grumbach 		IWL_DEBUG_RX(mvm,
133615e28c78SEmmanuel Grumbach 			     "beacon status %#x tsf:0x%016llX gp2:0x%X\n",
133715e28c78SEmmanuel Grumbach 			     status, le64_to_cpu(beacon->tsf),
133815e28c78SEmmanuel Grumbach 			     mvm->ap_last_beacon_gp2);
133915e28c78SEmmanuel Grumbach 	}
1340e705c121SKalle Valo 
1341e705c121SKalle Valo 	csa_vif = rcu_dereference_protected(mvm->csa_vif,
1342e705c121SKalle Valo 					    lockdep_is_held(&mvm->mutex));
1343e705c121SKalle Valo 	if (unlikely(csa_vif && csa_vif->csa_active))
1344e705c121SKalle Valo 		iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2,
1345e705c121SKalle Valo 				       (status == TX_STATUS_SUCCESS));
1346e705c121SKalle Valo 
1347e705c121SKalle Valo 	tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
1348e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
1349e705c121SKalle Valo 	if (unlikely(tx_blocked_vif)) {
1350e705c121SKalle Valo 		struct iwl_mvm_vif *mvmvif =
1351e705c121SKalle Valo 			iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1352e705c121SKalle Valo 
1353e705c121SKalle Valo 		/*
1354e705c121SKalle Valo 		 * The channel switch is started and we have blocked the
1355e705c121SKalle Valo 		 * stations. If this is the first beacon (the timeout wasn't
1356e705c121SKalle Valo 		 * set), set the unblock timeout, otherwise countdown
1357e705c121SKalle Valo 		 */
1358e705c121SKalle Valo 		if (!mvm->csa_tx_block_bcn_timeout)
1359e705c121SKalle Valo 			mvm->csa_tx_block_bcn_timeout =
1360e705c121SKalle Valo 				IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
1361e705c121SKalle Valo 		else
1362e705c121SKalle Valo 			mvm->csa_tx_block_bcn_timeout--;
1363e705c121SKalle Valo 
1364e705c121SKalle Valo 		/* Check if the timeout is expired, and unblock tx */
1365e705c121SKalle Valo 		if (mvm->csa_tx_block_bcn_timeout == 0) {
1366e705c121SKalle Valo 			iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
1367e705c121SKalle Valo 			RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1368e705c121SKalle Valo 		}
1369e705c121SKalle Valo 	}
1370e705c121SKalle Valo }
1371e705c121SKalle Valo 
1372698478c4SSara Sharon void iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
1373698478c4SSara Sharon 				     struct iwl_rx_cmd_buffer *rxb)
1374e705c121SKalle Valo {
1375698478c4SSara Sharon 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1376698478c4SSara Sharon 	struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
1377e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_missed_bcon *bcon_trig;
1378e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trigger;
1379e705c121SKalle Valo 	u32 stop_trig_missed_bcon, stop_trig_missed_bcon_since_rx;
1380e705c121SKalle Valo 	u32 rx_missed_bcon, rx_missed_bcon_since_rx;
1381698478c4SSara Sharon 	struct ieee80211_vif *vif;
1382698478c4SSara Sharon 	u32 id = le32_to_cpu(mb->mac_id);
1383e705c121SKalle Valo 
1384698478c4SSara Sharon 	IWL_DEBUG_INFO(mvm,
1385698478c4SSara Sharon 		       "missed bcn mac_id=%u, consecutive=%u (%u, %u, %u)\n",
1386698478c4SSara Sharon 		       le32_to_cpu(mb->mac_id),
1387698478c4SSara Sharon 		       le32_to_cpu(mb->consec_missed_beacons),
1388698478c4SSara Sharon 		       le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
1389698478c4SSara Sharon 		       le32_to_cpu(mb->num_recvd_beacons),
1390698478c4SSara Sharon 		       le32_to_cpu(mb->num_expected_beacons));
1391e705c121SKalle Valo 
1392698478c4SSara Sharon 	rcu_read_lock();
1393698478c4SSara Sharon 
1394698478c4SSara Sharon 	vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);
1395698478c4SSara Sharon 	if (!vif)
1396698478c4SSara Sharon 		goto out;
1397698478c4SSara Sharon 
1398698478c4SSara Sharon 	rx_missed_bcon = le32_to_cpu(mb->consec_missed_beacons);
1399e705c121SKalle Valo 	rx_missed_bcon_since_rx =
1400698478c4SSara Sharon 		le32_to_cpu(mb->consec_missed_beacons_since_last_rx);
1401e705c121SKalle Valo 	/*
1402e705c121SKalle Valo 	 * TODO: the threshold should be adjusted based on latency conditions,
1403e705c121SKalle Valo 	 * and/or in case of a CS flow on one of the other AP vifs.
1404e705c121SKalle Valo 	 */
1405babea2d4SAndrei Otcheretianski 	if (rx_missed_bcon > IWL_MVM_MISSED_BEACONS_THRESHOLD_LONG)
1406babea2d4SAndrei Otcheretianski 		iwl_mvm_connection_loss(mvm, vif, "missed beacons");
1407babea2d4SAndrei Otcheretianski 	else if (rx_missed_bcon_since_rx > IWL_MVM_MISSED_BEACONS_THRESHOLD)
1408e705c121SKalle Valo 		ieee80211_beacon_loss(vif);
1409e705c121SKalle Valo 
14106c042d75SSara Sharon 	trigger = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
14116c042d75SSara Sharon 					FW_DBG_TRIGGER_MISSED_BEACONS);
14126c042d75SSara Sharon 	if (!trigger)
1413698478c4SSara Sharon 		goto out;
1414e705c121SKalle Valo 
1415e705c121SKalle Valo 	bcon_trig = (void *)trigger->data;
1416e705c121SKalle Valo 	stop_trig_missed_bcon = le32_to_cpu(bcon_trig->stop_consec_missed_bcon);
1417e705c121SKalle Valo 	stop_trig_missed_bcon_since_rx =
1418e705c121SKalle Valo 		le32_to_cpu(bcon_trig->stop_consec_missed_bcon_since_rx);
1419e705c121SKalle Valo 
1420e705c121SKalle Valo 	/* TODO: implement start trigger */
1421e705c121SKalle Valo 
1422e705c121SKalle Valo 	if (rx_missed_bcon_since_rx >= stop_trig_missed_bcon_since_rx ||
1423e705c121SKalle Valo 	    rx_missed_bcon >= stop_trig_missed_bcon)
14247174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&mvm->fwrt, trigger, NULL);
1425da2eb669SSara Sharon 
1426da2eb669SSara Sharon 	iwl_fw_dbg_apply_point(&mvm->fwrt, IWL_FW_INI_APPLY_MISSED_BEACONS);
1427698478c4SSara Sharon 
1428698478c4SSara Sharon out:
1429698478c4SSara Sharon 	rcu_read_unlock();
1430e705c121SKalle Valo }
14310db056d3SSara Sharon 
14320db056d3SSara Sharon void iwl_mvm_rx_stored_beacon_notif(struct iwl_mvm *mvm,
14330db056d3SSara Sharon 				    struct iwl_rx_cmd_buffer *rxb)
14340db056d3SSara Sharon {
14350db056d3SSara Sharon 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
14360db056d3SSara Sharon 	struct iwl_stored_beacon_notif *sb = (void *)pkt->data;
14370db056d3SSara Sharon 	struct ieee80211_rx_status rx_status;
14380db056d3SSara Sharon 	struct sk_buff *skb;
14390db056d3SSara Sharon 	u32 size = le32_to_cpu(sb->byte_count);
14400db056d3SSara Sharon 
14410db056d3SSara Sharon 	if (size == 0)
14420db056d3SSara Sharon 		return;
14430db056d3SSara Sharon 
14440db056d3SSara Sharon 	skb = alloc_skb(size, GFP_ATOMIC);
14450db056d3SSara Sharon 	if (!skb) {
14460db056d3SSara Sharon 		IWL_ERR(mvm, "alloc_skb failed\n");
14470db056d3SSara Sharon 		return;
14480db056d3SSara Sharon 	}
14490db056d3SSara Sharon 
14500db056d3SSara Sharon 	/* update rx_status according to the notification's metadata */
14510db056d3SSara Sharon 	memset(&rx_status, 0, sizeof(rx_status));
14520db056d3SSara Sharon 	rx_status.mactime = le64_to_cpu(sb->tsf);
145377fe7395SSara Sharon 	/* TSF as indicated by the firmware  is at INA time */
145477fe7395SSara Sharon 	rx_status.flag |= RX_FLAG_MACTIME_PLCP_START;
14550db056d3SSara Sharon 	rx_status.device_timestamp = le32_to_cpu(sb->system_time);
14560db056d3SSara Sharon 	rx_status.band =
145734118c25SSara Sharon 		(sb->band & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
145857fbcce3SJohannes Berg 				NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
14590db056d3SSara Sharon 	rx_status.freq =
14600db056d3SSara Sharon 		ieee80211_channel_to_frequency(le16_to_cpu(sb->channel),
14610db056d3SSara Sharon 					       rx_status.band);
14620db056d3SSara Sharon 
14630db056d3SSara Sharon 	/* copy the data */
146459ae1d12SJohannes Berg 	skb_put_data(skb, sb->data, size);
14650db056d3SSara Sharon 	memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
14660db056d3SSara Sharon 
14670db056d3SSara Sharon 	/* pass it as regular rx to mac80211 */
1468d63b548fSJohannes Berg 	ieee80211_rx_napi(mvm->hw, NULL, skb, NULL);
14690db056d3SSara Sharon }
1470d3a108a4SAndrei Otcheretianski 
1471698478c4SSara Sharon void iwl_mvm_probe_resp_data_notif(struct iwl_mvm *mvm,
1472698478c4SSara Sharon 				   struct iwl_rx_cmd_buffer *rxb)
147386e177d8SGregory Greenman {
1474698478c4SSara Sharon 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1475698478c4SSara Sharon 	struct iwl_probe_resp_data_notif *notif = (void *)pkt->data;
147686e177d8SGregory Greenman 	struct iwl_probe_resp_data *old_data, *new_data;
1477698478c4SSara Sharon 	int len = iwl_rx_packet_payload_len(pkt);
1478698478c4SSara Sharon 	u32 id = le32_to_cpu(notif->mac_id);
1479698478c4SSara Sharon 	struct ieee80211_vif *vif;
1480698478c4SSara Sharon 	struct iwl_mvm_vif *mvmvif;
148186e177d8SGregory Greenman 
1482698478c4SSara Sharon 	if (WARN_ON_ONCE(len < sizeof(*notif)))
148386e177d8SGregory Greenman 		return;
148486e177d8SGregory Greenman 
1485698478c4SSara Sharon 	IWL_DEBUG_INFO(mvm, "Probe response data notif: noa %d, csa %d\n",
1486698478c4SSara Sharon 		       notif->noa_active, notif->csa_counter);
1487698478c4SSara Sharon 
1488698478c4SSara Sharon 	vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, false);
1489698478c4SSara Sharon 	if (!vif)
1490698478c4SSara Sharon 		return;
1491698478c4SSara Sharon 
1492698478c4SSara Sharon 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
1493698478c4SSara Sharon 
149486e177d8SGregory Greenman 	new_data = kzalloc(sizeof(*new_data), GFP_KERNEL);
149586e177d8SGregory Greenman 	if (!new_data)
149686e177d8SGregory Greenman 		return;
149786e177d8SGregory Greenman 
149886e177d8SGregory Greenman 	memcpy(&new_data->notif, notif, sizeof(new_data->notif));
149986e177d8SGregory Greenman 
150086e177d8SGregory Greenman 	/* noa_attr contains 1 reserved byte, need to substruct it */
150186e177d8SGregory Greenman 	new_data->noa_len = sizeof(struct ieee80211_vendor_ie) +
150286e177d8SGregory Greenman 			    sizeof(new_data->notif.noa_attr) - 1;
150386e177d8SGregory Greenman 
150486e177d8SGregory Greenman 	/*
150586e177d8SGregory Greenman 	 * If it's a one time NoA, only one descriptor is needed,
150686e177d8SGregory Greenman 	 * adjust the length according to len_low.
150786e177d8SGregory Greenman 	 */
150886e177d8SGregory Greenman 	if (new_data->notif.noa_attr.len_low ==
150986e177d8SGregory Greenman 	    sizeof(struct ieee80211_p2p_noa_desc) + 2)
151086e177d8SGregory Greenman 		new_data->noa_len -= sizeof(struct ieee80211_p2p_noa_desc);
151186e177d8SGregory Greenman 
151286e177d8SGregory Greenman 	old_data = rcu_dereference_protected(mvmvif->probe_resp_data,
151386e177d8SGregory Greenman 					lockdep_is_held(&mvmvif->mvm->mutex));
151486e177d8SGregory Greenman 	rcu_assign_pointer(mvmvif->probe_resp_data, new_data);
151586e177d8SGregory Greenman 
151686e177d8SGregory Greenman 	if (old_data)
151786e177d8SGregory Greenman 		kfree_rcu(old_data, rcu_head);
151886e177d8SGregory Greenman 
151986e177d8SGregory Greenman 	if (notif->csa_counter != IWL_PROBE_RESP_DATA_NO_CSA &&
152086e177d8SGregory Greenman 	    notif->csa_counter >= 1)
152186e177d8SGregory Greenman 		ieee80211_csa_set_counter(vif, notif->csa_counter);
152286e177d8SGregory Greenman }
152386e177d8SGregory Greenman 
1524d3a108a4SAndrei Otcheretianski void iwl_mvm_channel_switch_noa_notif(struct iwl_mvm *mvm,
1525d3a108a4SAndrei Otcheretianski 				      struct iwl_rx_cmd_buffer *rxb)
1526d3a108a4SAndrei Otcheretianski {
1527d3a108a4SAndrei Otcheretianski 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1528d3a108a4SAndrei Otcheretianski 	struct iwl_channel_switch_noa_notif *notif = (void *)pkt->data;
152974a10252SSara Sharon 	struct ieee80211_vif *csa_vif, *vif;
1530d3a108a4SAndrei Otcheretianski 	struct iwl_mvm_vif *mvmvif;
1531d3a108a4SAndrei Otcheretianski 	int len = iwl_rx_packet_payload_len(pkt);
153274a10252SSara Sharon 	u32 id_n_color, csa_id, mac_id;
1533d3a108a4SAndrei Otcheretianski 
1534d3a108a4SAndrei Otcheretianski 	if (WARN_ON_ONCE(len < sizeof(*notif)))
1535d3a108a4SAndrei Otcheretianski 		return;
1536d3a108a4SAndrei Otcheretianski 
153774a10252SSara Sharon 	id_n_color = le32_to_cpu(notif->id_and_color);
153874a10252SSara Sharon 	mac_id = id_n_color & FW_CTXT_ID_MSK;
1539d3a108a4SAndrei Otcheretianski 
154074a10252SSara Sharon 	if (WARN_ON_ONCE(mac_id >= NUM_MAC_INDEX_DRIVER))
154174a10252SSara Sharon 		return;
154274a10252SSara Sharon 
154374a10252SSara Sharon 	rcu_read_lock();
154474a10252SSara Sharon 	vif = rcu_dereference(mvm->vif_id_to_mac[mac_id]);
1545f6780614SSara Sharon 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
154674a10252SSara Sharon 
154774a10252SSara Sharon 	switch (vif->type) {
154874a10252SSara Sharon 	case NL80211_IFTYPE_AP:
1549d3a108a4SAndrei Otcheretianski 		csa_vif = rcu_dereference(mvm->csa_vif);
155074a10252SSara Sharon 		if (WARN_ON(!csa_vif || !csa_vif->csa_active ||
155174a10252SSara Sharon 			    csa_vif != vif))
1552d3a108a4SAndrei Otcheretianski 			goto out_unlock;
1553d3a108a4SAndrei Otcheretianski 
155474a10252SSara Sharon 		csa_id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
155574a10252SSara Sharon 		if (WARN(csa_id != id_n_color,
1556d3a108a4SAndrei Otcheretianski 			 "channel switch noa notification on unexpected vif (csa_vif=%d, notif=%d)",
155774a10252SSara Sharon 			 csa_id, id_n_color))
1558d3a108a4SAndrei Otcheretianski 			goto out_unlock;
1559d3a108a4SAndrei Otcheretianski 
1560d3a108a4SAndrei Otcheretianski 		IWL_DEBUG_INFO(mvm, "Channel Switch Started Notification\n");
1561d3a108a4SAndrei Otcheretianski 
156286bbb1e1SJohannes Berg 		schedule_delayed_work(&mvm->cs_tx_unblock_dwork,
1563d3a108a4SAndrei Otcheretianski 				      msecs_to_jiffies(IWL_MVM_CS_UNBLOCK_TX_TIMEOUT *
1564d3a108a4SAndrei Otcheretianski 						       csa_vif->bss_conf.beacon_int));
1565d3a108a4SAndrei Otcheretianski 
1566d3a108a4SAndrei Otcheretianski 		ieee80211_csa_finish(csa_vif);
1567d3a108a4SAndrei Otcheretianski 
1568d3a108a4SAndrei Otcheretianski 		rcu_read_unlock();
1569d3a108a4SAndrei Otcheretianski 
1570d3a108a4SAndrei Otcheretianski 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
1571d3a108a4SAndrei Otcheretianski 		return;
157274a10252SSara Sharon 	case NL80211_IFTYPE_STATION:
157374a10252SSara Sharon 		iwl_mvm_csa_client_absent(mvm, vif);
157411af74adSAndrei Otcheretianski 		cancel_delayed_work(&mvmvif->csa_work);
157574a10252SSara Sharon 		ieee80211_chswitch_done(vif, true);
157674a10252SSara Sharon 		break;
157774a10252SSara Sharon 	default:
157874a10252SSara Sharon 		/* should never happen */
157974a10252SSara Sharon 		WARN_ON_ONCE(1);
158074a10252SSara Sharon 		break;
158174a10252SSara Sharon 	}
1582d3a108a4SAndrei Otcheretianski out_unlock:
1583d3a108a4SAndrei Otcheretianski 	rcu_read_unlock();
1584d3a108a4SAndrei Otcheretianski }
1585