18e99ea8dSJohannes Berg // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
28e99ea8dSJohannes Berg /*
31269ba1cSShaul Triebitz  * Copyright (C) 2012-2014, 2018-2021 Intel Corporation
48e99ea8dSJohannes Berg  * Copyright (C) 2013-2014 Intel Mobile Communications GmbH
58e99ea8dSJohannes Berg  * Copyright (C) 2015-2017 Intel Deutschland GmbH
68e99ea8dSJohannes Berg  */
7e705c121SKalle Valo #include <linux/etherdevice.h>
8eae94cf8SLuca Coelho #include <linux/crc32.h>
9e705c121SKalle Valo #include <net/mac80211.h>
10e705c121SKalle Valo #include "iwl-io.h"
11e705c121SKalle Valo #include "iwl-prph.h"
12e705c121SKalle Valo #include "fw-api.h"
13e705c121SKalle Valo #include "mvm.h"
14e705c121SKalle Valo #include "time-event.h"
15e705c121SKalle Valo 
16e705c121SKalle Valo const u8 iwl_mvm_ac_to_tx_fifo[] = {
17e705c121SKalle Valo 	IWL_MVM_TX_FIFO_VO,
18e705c121SKalle Valo 	IWL_MVM_TX_FIFO_VI,
19e705c121SKalle Valo 	IWL_MVM_TX_FIFO_BE,
20e705c121SKalle Valo 	IWL_MVM_TX_FIFO_BK,
21e705c121SKalle Valo };
22e705c121SKalle Valo 
23cf6c6ea3SEmmanuel Grumbach const u8 iwl_mvm_ac_to_gen2_tx_fifo[] = {
24cf6c6ea3SEmmanuel Grumbach 	IWL_GEN2_EDCA_TX_FIFO_VO,
25cf6c6ea3SEmmanuel Grumbach 	IWL_GEN2_EDCA_TX_FIFO_VI,
26cf6c6ea3SEmmanuel Grumbach 	IWL_GEN2_EDCA_TX_FIFO_BE,
27cf6c6ea3SEmmanuel Grumbach 	IWL_GEN2_EDCA_TX_FIFO_BK,
287126b6f2SSara Sharon 	IWL_GEN2_TRIG_TX_FIFO_VO,
297126b6f2SSara Sharon 	IWL_GEN2_TRIG_TX_FIFO_VI,
307126b6f2SSara Sharon 	IWL_GEN2_TRIG_TX_FIFO_BE,
317126b6f2SSara Sharon 	IWL_GEN2_TRIG_TX_FIFO_BK,
32cf6c6ea3SEmmanuel Grumbach };
33cf6c6ea3SEmmanuel Grumbach 
34e705c121SKalle Valo struct iwl_mvm_mac_iface_iterator_data {
35e705c121SKalle Valo 	struct iwl_mvm *mvm;
36e705c121SKalle Valo 	struct ieee80211_vif *vif;
37e705c121SKalle Valo 	unsigned long available_mac_ids[BITS_TO_LONGS(NUM_MAC_INDEX_DRIVER)];
38e705c121SKalle Valo 	unsigned long available_tsf_ids[BITS_TO_LONGS(NUM_TSF_IDS)];
39e705c121SKalle Valo 	enum iwl_tsf_id preferred_tsf;
40e705c121SKalle Valo 	bool found_vif;
41e705c121SKalle Valo };
42e705c121SKalle Valo 
43e705c121SKalle Valo static void iwl_mvm_mac_tsf_id_iter(void *_data, u8 *mac,
44e705c121SKalle Valo 				    struct ieee80211_vif *vif)
45e705c121SKalle Valo {
46e705c121SKalle Valo 	struct iwl_mvm_mac_iface_iterator_data *data = _data;
47e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
48e705c121SKalle Valo 	u16 min_bi;
49e705c121SKalle Valo 
50e705c121SKalle Valo 	/* Skip the interface for which we are trying to assign a tsf_id  */
51e705c121SKalle Valo 	if (vif == data->vif)
52e705c121SKalle Valo 		return;
53e705c121SKalle Valo 
54e705c121SKalle Valo 	/*
55e705c121SKalle Valo 	 * The TSF is a hardware/firmware resource, there are 4 and
56e705c121SKalle Valo 	 * the driver should assign and free them as needed. However,
57e705c121SKalle Valo 	 * there are cases where 2 MACs should share the same TSF ID
58e705c121SKalle Valo 	 * for the purpose of clock sync, an optimization to avoid
59e705c121SKalle Valo 	 * clock drift causing overlapping TBTTs/DTIMs for a GO and
60e705c121SKalle Valo 	 * client in the system.
61e705c121SKalle Valo 	 *
62e705c121SKalle Valo 	 * The firmware will decide according to the MAC type which
63cdaba917SEmmanuel Grumbach 	 * will be the leader and follower. Clients that need to sync
64cdaba917SEmmanuel Grumbach 	 * with a remote station will be the leader, and an AP or GO
65cdaba917SEmmanuel Grumbach 	 * will be the follower.
66e705c121SKalle Valo 	 *
67cdaba917SEmmanuel Grumbach 	 * Depending on the new interface type it can be following
68cdaba917SEmmanuel Grumbach 	 * or become the leader of an existing interface.
69e705c121SKalle Valo 	 */
70e705c121SKalle Valo 	switch (data->vif->type) {
71e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
72e705c121SKalle Valo 		/*
73e705c121SKalle Valo 		 * The new interface is a client, so if the one we're iterating
74e705c121SKalle Valo 		 * is an AP, and the beacon interval of the AP is a multiple or
75e705c121SKalle Valo 		 * divisor of the beacon interval of the client, the same TSF
76e705c121SKalle Valo 		 * should be used to avoid drift between the new client and
77e705c121SKalle Valo 		 * existing AP. The existing AP will get drift updates from the
78e705c121SKalle Valo 		 * new client context in this case.
79e705c121SKalle Valo 		 */
80e705c121SKalle Valo 		if (vif->type != NL80211_IFTYPE_AP ||
81e705c121SKalle Valo 		    data->preferred_tsf != NUM_TSF_IDS ||
82e705c121SKalle Valo 		    !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
83e705c121SKalle Valo 			break;
84e705c121SKalle Valo 
85e705c121SKalle Valo 		min_bi = min(data->vif->bss_conf.beacon_int,
86e705c121SKalle Valo 			     vif->bss_conf.beacon_int);
87e705c121SKalle Valo 
88e705c121SKalle Valo 		if (!min_bi)
89e705c121SKalle Valo 			break;
90e705c121SKalle Valo 
91e705c121SKalle Valo 		if ((data->vif->bss_conf.beacon_int -
92e705c121SKalle Valo 		     vif->bss_conf.beacon_int) % min_bi == 0) {
93e705c121SKalle Valo 			data->preferred_tsf = mvmvif->tsf_id;
94e705c121SKalle Valo 			return;
95e705c121SKalle Valo 		}
96e705c121SKalle Valo 		break;
97e705c121SKalle Valo 
98e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
99e705c121SKalle Valo 		/*
100e705c121SKalle Valo 		 * The new interface is AP/GO, so if its beacon interval is a
101e705c121SKalle Valo 		 * multiple or a divisor of the beacon interval of an existing
102e705c121SKalle Valo 		 * interface, it should get drift updates from an existing
103e705c121SKalle Valo 		 * client or use the same TSF as an existing GO. There's no
104e705c121SKalle Valo 		 * drift between TSFs internally but if they used different
105e705c121SKalle Valo 		 * TSFs then a new client MAC could update one of them and
106e705c121SKalle Valo 		 * cause drift that way.
107e705c121SKalle Valo 		 */
108e705c121SKalle Valo 		if ((vif->type != NL80211_IFTYPE_AP &&
109e705c121SKalle Valo 		     vif->type != NL80211_IFTYPE_STATION) ||
110e705c121SKalle Valo 		    data->preferred_tsf != NUM_TSF_IDS ||
111e705c121SKalle Valo 		    !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
112e705c121SKalle Valo 			break;
113e705c121SKalle Valo 
114e705c121SKalle Valo 		min_bi = min(data->vif->bss_conf.beacon_int,
115e705c121SKalle Valo 			     vif->bss_conf.beacon_int);
116e705c121SKalle Valo 
117e705c121SKalle Valo 		if (!min_bi)
118e705c121SKalle Valo 			break;
119e705c121SKalle Valo 
120e705c121SKalle Valo 		if ((data->vif->bss_conf.beacon_int -
121e705c121SKalle Valo 		     vif->bss_conf.beacon_int) % min_bi == 0) {
122e705c121SKalle Valo 			data->preferred_tsf = mvmvif->tsf_id;
123e705c121SKalle Valo 			return;
124e705c121SKalle Valo 		}
125e705c121SKalle Valo 		break;
126e705c121SKalle Valo 	default:
127e705c121SKalle Valo 		/*
128e705c121SKalle Valo 		 * For all other interface types there's no need to
129e705c121SKalle Valo 		 * take drift into account. Either they're exclusive
130e705c121SKalle Valo 		 * like IBSS and monitor, or we don't care much about
131e705c121SKalle Valo 		 * their TSF (like P2P Device), but we won't be able
132e705c121SKalle Valo 		 * to share the TSF resource.
133e705c121SKalle Valo 		 */
134e705c121SKalle Valo 		break;
135e705c121SKalle Valo 	}
136e705c121SKalle Valo 
137e705c121SKalle Valo 	/*
138e705c121SKalle Valo 	 * Unless we exited above, we can't share the TSF resource
139e705c121SKalle Valo 	 * that the virtual interface we're iterating over is using
140e705c121SKalle Valo 	 * with the new one, so clear the available bit and if this
141e705c121SKalle Valo 	 * was the preferred one, reset that as well.
142e705c121SKalle Valo 	 */
143e705c121SKalle Valo 	__clear_bit(mvmvif->tsf_id, data->available_tsf_ids);
144e705c121SKalle Valo 
145e705c121SKalle Valo 	if (data->preferred_tsf == mvmvif->tsf_id)
146e705c121SKalle Valo 		data->preferred_tsf = NUM_TSF_IDS;
147e705c121SKalle Valo }
148e705c121SKalle Valo 
149e705c121SKalle Valo static void iwl_mvm_mac_iface_iterator(void *_data, u8 *mac,
150e705c121SKalle Valo 				       struct ieee80211_vif *vif)
151e705c121SKalle Valo {
152e705c121SKalle Valo 	struct iwl_mvm_mac_iface_iterator_data *data = _data;
153e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
154e705c121SKalle Valo 
155e705c121SKalle Valo 	/* Iterator may already find the interface being added -- skip it */
156e705c121SKalle Valo 	if (vif == data->vif) {
157e705c121SKalle Valo 		data->found_vif = true;
158e705c121SKalle Valo 		return;
159e705c121SKalle Valo 	}
160e705c121SKalle Valo 
161e705c121SKalle Valo 	/* Mark MAC IDs as used by clearing the available bit, and
162e705c121SKalle Valo 	 * (below) mark TSFs as used if their existing use is not
163e705c121SKalle Valo 	 * compatible with the new interface type.
164e705c121SKalle Valo 	 * No locking or atomic bit operations are needed since the
165e705c121SKalle Valo 	 * data is on the stack of the caller function.
166e705c121SKalle Valo 	 */
167e705c121SKalle Valo 	__clear_bit(mvmvif->id, data->available_mac_ids);
168e705c121SKalle Valo 
169e705c121SKalle Valo 	/* find a suitable tsf_id */
170e705c121SKalle Valo 	iwl_mvm_mac_tsf_id_iter(_data, mac, vif);
171e705c121SKalle Valo }
172e705c121SKalle Valo 
173e705c121SKalle Valo void iwl_mvm_mac_ctxt_recalc_tsf_id(struct iwl_mvm *mvm,
174e705c121SKalle Valo 				    struct ieee80211_vif *vif)
175e705c121SKalle Valo {
176e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
177e705c121SKalle Valo 	struct iwl_mvm_mac_iface_iterator_data data = {
178e705c121SKalle Valo 		.mvm = mvm,
179e705c121SKalle Valo 		.vif = vif,
180e705c121SKalle Valo 		.available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
181e705c121SKalle Valo 		/* no preference yet */
182e705c121SKalle Valo 		.preferred_tsf = NUM_TSF_IDS,
183e705c121SKalle Valo 	};
184e705c121SKalle Valo 
185e705c121SKalle Valo 	ieee80211_iterate_active_interfaces_atomic(
186e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
187e705c121SKalle Valo 		iwl_mvm_mac_tsf_id_iter, &data);
188e705c121SKalle Valo 
189e705c121SKalle Valo 	if (data.preferred_tsf != NUM_TSF_IDS)
190e705c121SKalle Valo 		mvmvif->tsf_id = data.preferred_tsf;
191e705c121SKalle Valo 	else if (!test_bit(mvmvif->tsf_id, data.available_tsf_ids))
192e705c121SKalle Valo 		mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
193e705c121SKalle Valo 						NUM_TSF_IDS);
194e705c121SKalle Valo }
195e705c121SKalle Valo 
196c8f54701SJohannes Berg int iwl_mvm_mac_ctxt_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
197e705c121SKalle Valo {
198e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
199e705c121SKalle Valo 	struct iwl_mvm_mac_iface_iterator_data data = {
200e705c121SKalle Valo 		.mvm = mvm,
201e705c121SKalle Valo 		.vif = vif,
202e705c121SKalle Valo 		.available_mac_ids = { (1 << NUM_MAC_INDEX_DRIVER) - 1 },
203e705c121SKalle Valo 		.available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
204e705c121SKalle Valo 		/* no preference yet */
205e705c121SKalle Valo 		.preferred_tsf = NUM_TSF_IDS,
206e705c121SKalle Valo 		.found_vif = false,
207e705c121SKalle Valo 	};
2083f7fbc8cSJohannes Berg 	int ret, i;
209e705c121SKalle Valo 
210c8f54701SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
211c8f54701SJohannes Berg 
212e705c121SKalle Valo 	/*
213e705c121SKalle Valo 	 * Allocate a MAC ID and a TSF for this MAC, along with the queues
214e705c121SKalle Valo 	 * and other resources.
215e705c121SKalle Valo 	 */
216e705c121SKalle Valo 
217e705c121SKalle Valo 	/*
218e705c121SKalle Valo 	 * Before the iterator, we start with all MAC IDs and TSFs available.
219e705c121SKalle Valo 	 *
220e705c121SKalle Valo 	 * During iteration, all MAC IDs are cleared that are in use by other
221e705c121SKalle Valo 	 * virtual interfaces, and all TSF IDs are cleared that can't be used
222e705c121SKalle Valo 	 * by this new virtual interface because they're used by an interface
223e705c121SKalle Valo 	 * that can't share it with the new one.
224e705c121SKalle Valo 	 * At the same time, we check if there's a preferred TSF in the case
225e705c121SKalle Valo 	 * that we should share it with another interface.
226e705c121SKalle Valo 	 */
227e705c121SKalle Valo 
228e705c121SKalle Valo 	/* Currently, MAC ID 0 should be used only for the managed/IBSS vif */
229e705c121SKalle Valo 	switch (vif->type) {
230e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
231e705c121SKalle Valo 		break;
232e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
233e705c121SKalle Valo 		if (!vif->p2p)
234e705c121SKalle Valo 			break;
2355a2abdcaSGustavo A. R. Silva 		fallthrough;
236e705c121SKalle Valo 	default:
237e705c121SKalle Valo 		__clear_bit(0, data.available_mac_ids);
238e705c121SKalle Valo 	}
239e705c121SKalle Valo 
240e705c121SKalle Valo 	ieee80211_iterate_active_interfaces_atomic(
241e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
242e705c121SKalle Valo 		iwl_mvm_mac_iface_iterator, &data);
243e705c121SKalle Valo 
244e705c121SKalle Valo 	/*
245e705c121SKalle Valo 	 * In the case we're getting here during resume, it's similar to
246e705c121SKalle Valo 	 * firmware restart, and with RESUME_ALL the iterator will find
247e705c121SKalle Valo 	 * the vif being added already.
248e705c121SKalle Valo 	 * We don't want to reassign any IDs in either case since doing
249e705c121SKalle Valo 	 * so would probably assign different IDs (as interfaces aren't
250e705c121SKalle Valo 	 * necessarily added in the same order), but the old IDs were
251e705c121SKalle Valo 	 * preserved anyway, so skip ID assignment for both resume and
252e705c121SKalle Valo 	 * recovery.
253e705c121SKalle Valo 	 */
254e705c121SKalle Valo 	if (data.found_vif)
255e705c121SKalle Valo 		return 0;
256e705c121SKalle Valo 
257e705c121SKalle Valo 	/* Therefore, in recovery, we can't get here */
258e705c121SKalle Valo 	if (WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
259e705c121SKalle Valo 		return -EBUSY;
260e705c121SKalle Valo 
261e705c121SKalle Valo 	mvmvif->id = find_first_bit(data.available_mac_ids,
262e705c121SKalle Valo 				    NUM_MAC_INDEX_DRIVER);
263e705c121SKalle Valo 	if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
264e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
265e705c121SKalle Valo 		ret = -EIO;
266e705c121SKalle Valo 		goto exit_fail;
267e705c121SKalle Valo 	}
268e705c121SKalle Valo 
269e705c121SKalle Valo 	if (data.preferred_tsf != NUM_TSF_IDS)
270e705c121SKalle Valo 		mvmvif->tsf_id = data.preferred_tsf;
271e705c121SKalle Valo 	else
272e705c121SKalle Valo 		mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
273e705c121SKalle Valo 						NUM_TSF_IDS);
274e705c121SKalle Valo 	if (mvmvif->tsf_id == NUM_TSF_IDS) {
275e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
276e705c121SKalle Valo 		ret = -EIO;
277e705c121SKalle Valo 		goto exit_fail;
278e705c121SKalle Valo 	}
279e705c121SKalle Valo 
280e705c121SKalle Valo 	mvmvif->color = 0;
281e705c121SKalle Valo 
282e705c121SKalle Valo 	INIT_LIST_HEAD(&mvmvif->time_event_data.list);
283e705c121SKalle Valo 	mvmvif->time_event_data.id = TE_MAX;
284e705c121SKalle Valo 
2853f7fbc8cSJohannes Berg 	/* No need to allocate data queues to P2P Device MAC and NAN.*/
2863f7fbc8cSJohannes Berg 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
287e705c121SKalle Valo 		return 0;
288e705c121SKalle Valo 
289e705c121SKalle Valo 	/* Allocate the CAB queue for softAP and GO interfaces */
290fc07bd8cSSara Sharon 	if (vif->type == NL80211_IFTYPE_AP ||
291fc07bd8cSSara Sharon 	    vif->type == NL80211_IFTYPE_ADHOC) {
292e2af3fabSSara Sharon 		/*
293e2af3fabSSara Sharon 		 * For TVQM this will be overwritten later with the FW assigned
294e2af3fabSSara Sharon 		 * queue value (when queue is enabled).
295e2af3fabSSara Sharon 		 */
296c8f54701SJohannes Berg 		mvmvif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
297e705c121SKalle Valo 	}
298e705c121SKalle Valo 
2990ae98812SSara Sharon 	mvmvif->bcast_sta.sta_id = IWL_MVM_INVALID_STA;
3000ae98812SSara Sharon 	mvmvif->mcast_sta.sta_id = IWL_MVM_INVALID_STA;
3010ae98812SSara Sharon 	mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
302e705c121SKalle Valo 
303e705c121SKalle Valo 	for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
304e705c121SKalle Valo 		mvmvif->smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
305e705c121SKalle Valo 
306e705c121SKalle Valo 	return 0;
307e705c121SKalle Valo 
308e705c121SKalle Valo exit_fail:
309e705c121SKalle Valo 	memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
310e705c121SKalle Valo 	return ret;
311e705c121SKalle Valo }
312e705c121SKalle Valo 
313e705c121SKalle Valo static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
314e705c121SKalle Valo 			      struct ieee80211_vif *vif,
31557fbcce3SJohannes Berg 			      enum nl80211_band band,
316e705c121SKalle Valo 			      u8 *cck_rates, u8 *ofdm_rates)
317e705c121SKalle Valo {
318e705c121SKalle Valo 	struct ieee80211_supported_band *sband;
319e705c121SKalle Valo 	unsigned long basic = vif->bss_conf.basic_rates;
320e705c121SKalle Valo 	int lowest_present_ofdm = 100;
321e705c121SKalle Valo 	int lowest_present_cck = 100;
322e705c121SKalle Valo 	u8 cck = 0;
323e705c121SKalle Valo 	u8 ofdm = 0;
324e705c121SKalle Valo 	int i;
325e705c121SKalle Valo 
326e705c121SKalle Valo 	sband = mvm->hw->wiphy->bands[band];
327e705c121SKalle Valo 
328e705c121SKalle Valo 	for_each_set_bit(i, &basic, BITS_PER_LONG) {
329e705c121SKalle Valo 		int hw = sband->bitrates[i].hw_value;
330e705c121SKalle Valo 		if (hw >= IWL_FIRST_OFDM_RATE) {
331e705c121SKalle Valo 			ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
332e705c121SKalle Valo 			if (lowest_present_ofdm > hw)
333e705c121SKalle Valo 				lowest_present_ofdm = hw;
334e705c121SKalle Valo 		} else {
335e705c121SKalle Valo 			BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
336e705c121SKalle Valo 
337e705c121SKalle Valo 			cck |= BIT(hw);
338e705c121SKalle Valo 			if (lowest_present_cck > hw)
339e705c121SKalle Valo 				lowest_present_cck = hw;
340e705c121SKalle Valo 		}
341e705c121SKalle Valo 	}
342e705c121SKalle Valo 
343e705c121SKalle Valo 	/*
344e705c121SKalle Valo 	 * Now we've got the basic rates as bitmaps in the ofdm and cck
345e705c121SKalle Valo 	 * variables. This isn't sufficient though, as there might not
346e705c121SKalle Valo 	 * be all the right rates in the bitmap. E.g. if the only basic
347e705c121SKalle Valo 	 * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
348e705c121SKalle Valo 	 * and 6 Mbps because the 802.11-2007 standard says in 9.6:
349e705c121SKalle Valo 	 *
350e705c121SKalle Valo 	 *    [...] a STA responding to a received frame shall transmit
351e705c121SKalle Valo 	 *    its Control Response frame [...] at the highest rate in the
352e705c121SKalle Valo 	 *    BSSBasicRateSet parameter that is less than or equal to the
353e705c121SKalle Valo 	 *    rate of the immediately previous frame in the frame exchange
354e705c121SKalle Valo 	 *    sequence ([...]) and that is of the same modulation class
355e705c121SKalle Valo 	 *    ([...]) as the received frame. If no rate contained in the
356e705c121SKalle Valo 	 *    BSSBasicRateSet parameter meets these conditions, then the
357e705c121SKalle Valo 	 *    control frame sent in response to a received frame shall be
358e705c121SKalle Valo 	 *    transmitted at the highest mandatory rate of the PHY that is
359e705c121SKalle Valo 	 *    less than or equal to the rate of the received frame, and
360e705c121SKalle Valo 	 *    that is of the same modulation class as the received frame.
361e705c121SKalle Valo 	 *
362e705c121SKalle Valo 	 * As a consequence, we need to add all mandatory rates that are
363e705c121SKalle Valo 	 * lower than all of the basic rates to these bitmaps.
364e705c121SKalle Valo 	 */
365e705c121SKalle Valo 
366e705c121SKalle Valo 	if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
367e705c121SKalle Valo 		ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
368e705c121SKalle Valo 	if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
369e705c121SKalle Valo 		ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
370e705c121SKalle Valo 	/* 6M already there or needed so always add */
371e705c121SKalle Valo 	ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
372e705c121SKalle Valo 
373e705c121SKalle Valo 	/*
374e705c121SKalle Valo 	 * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
375e705c121SKalle Valo 	 * Note, however:
376e705c121SKalle Valo 	 *  - if no CCK rates are basic, it must be ERP since there must
377e705c121SKalle Valo 	 *    be some basic rates at all, so they're OFDM => ERP PHY
378e705c121SKalle Valo 	 *    (or we're in 5 GHz, and the cck bitmap will never be used)
379e705c121SKalle Valo 	 *  - if 11M is a basic rate, it must be ERP as well, so add 5.5M
380e705c121SKalle Valo 	 *  - if 5.5M is basic, 1M and 2M are mandatory
381e705c121SKalle Valo 	 *  - if 2M is basic, 1M is mandatory
382e705c121SKalle Valo 	 *  - if 1M is basic, that's the only valid ACK rate.
383e705c121SKalle Valo 	 * As a consequence, it's not as complicated as it sounds, just add
384e705c121SKalle Valo 	 * any lower rates to the ACK rate bitmap.
385e705c121SKalle Valo 	 */
386e705c121SKalle Valo 	if (IWL_RATE_11M_INDEX < lowest_present_cck)
387e705c121SKalle Valo 		cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
388e705c121SKalle Valo 	if (IWL_RATE_5M_INDEX < lowest_present_cck)
389e705c121SKalle Valo 		cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
390e705c121SKalle Valo 	if (IWL_RATE_2M_INDEX < lowest_present_cck)
391e705c121SKalle Valo 		cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
392e705c121SKalle Valo 	/* 1M already there or needed so always add */
393e705c121SKalle Valo 	cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
394e705c121SKalle Valo 
395e705c121SKalle Valo 	*cck_rates = cck;
396e705c121SKalle Valo 	*ofdm_rates = ofdm;
397e705c121SKalle Valo }
398e705c121SKalle Valo 
399e705c121SKalle Valo static void iwl_mvm_mac_ctxt_set_ht_flags(struct iwl_mvm *mvm,
400e705c121SKalle Valo 					 struct ieee80211_vif *vif,
401e705c121SKalle Valo 					 struct iwl_mac_ctx_cmd *cmd)
402e705c121SKalle Valo {
403e705c121SKalle Valo 	/* for both sta and ap, ht_operation_mode hold the protection_mode */
404e705c121SKalle Valo 	u8 protection_mode = vif->bss_conf.ht_operation_mode &
405e705c121SKalle Valo 				 IEEE80211_HT_OP_MODE_PROTECTION;
406e705c121SKalle Valo 	/* The fw does not distinguish between ht and fat */
407e705c121SKalle Valo 	u32 ht_flag = MAC_PROT_FLG_HT_PROT | MAC_PROT_FLG_FAT_PROT;
408e705c121SKalle Valo 
409e705c121SKalle Valo 	IWL_DEBUG_RATE(mvm, "protection mode set to %d\n", protection_mode);
410e705c121SKalle Valo 	/*
411e705c121SKalle Valo 	 * See section 9.23.3.1 of IEEE 80211-2012.
412e705c121SKalle Valo 	 * Nongreenfield HT STAs Present is not supported.
413e705c121SKalle Valo 	 */
414e705c121SKalle Valo 	switch (protection_mode) {
415e705c121SKalle Valo 	case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
416e705c121SKalle Valo 		break;
417e705c121SKalle Valo 	case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
418e705c121SKalle Valo 	case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
419e705c121SKalle Valo 		cmd->protection_flags |= cpu_to_le32(ht_flag);
420e705c121SKalle Valo 		break;
421e705c121SKalle Valo 	case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
422e705c121SKalle Valo 		/* Protect when channel wider than 20MHz */
423e705c121SKalle Valo 		if (vif->bss_conf.chandef.width > NL80211_CHAN_WIDTH_20)
424e705c121SKalle Valo 			cmd->protection_flags |= cpu_to_le32(ht_flag);
425e705c121SKalle Valo 		break;
426e705c121SKalle Valo 	default:
427e705c121SKalle Valo 		IWL_ERR(mvm, "Illegal protection mode %d\n",
428e705c121SKalle Valo 			protection_mode);
429e705c121SKalle Valo 		break;
430e705c121SKalle Valo 	}
431e705c121SKalle Valo }
432e705c121SKalle Valo 
433e705c121SKalle Valo static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
434e705c121SKalle Valo 					struct ieee80211_vif *vif,
435e705c121SKalle Valo 					struct iwl_mac_ctx_cmd *cmd,
436e705c121SKalle Valo 					const u8 *bssid_override,
437e705c121SKalle Valo 					u32 action)
438e705c121SKalle Valo {
439e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
440e705c121SKalle Valo 	struct ieee80211_chanctx_conf *chanctx;
441e705c121SKalle Valo 	bool ht_enabled = !!(vif->bss_conf.ht_operation_mode &
442e705c121SKalle Valo 			     IEEE80211_HT_OP_MODE_PROTECTION);
443e705c121SKalle Valo 	u8 cck_ack_rates, ofdm_ack_rates;
444e705c121SKalle Valo 	const u8 *bssid = bssid_override ?: vif->bss_conf.bssid;
445e705c121SKalle Valo 	int i;
446e705c121SKalle Valo 
447e705c121SKalle Valo 	cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
448e705c121SKalle Valo 							    mvmvif->color));
449e705c121SKalle Valo 	cmd->action = cpu_to_le32(action);
450e705c121SKalle Valo 
451e705c121SKalle Valo 	switch (vif->type) {
452e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
453e705c121SKalle Valo 		if (vif->p2p)
454e705c121SKalle Valo 			cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_STA);
455e705c121SKalle Valo 		else
456e705c121SKalle Valo 			cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_BSS_STA);
457e705c121SKalle Valo 		break;
458e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
459e705c121SKalle Valo 		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_GO);
460e705c121SKalle Valo 		break;
461e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
462e705c121SKalle Valo 		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_LISTENER);
463e705c121SKalle Valo 		break;
464e705c121SKalle Valo 	case NL80211_IFTYPE_P2P_DEVICE:
465e705c121SKalle Valo 		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_DEVICE);
466e705c121SKalle Valo 		break;
467e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
468e705c121SKalle Valo 		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_IBSS);
469e705c121SKalle Valo 		break;
470e705c121SKalle Valo 	default:
471e705c121SKalle Valo 		WARN_ON_ONCE(1);
472e705c121SKalle Valo 	}
473e705c121SKalle Valo 
474e705c121SKalle Valo 	cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
475e705c121SKalle Valo 
476e705c121SKalle Valo 	memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
477e705c121SKalle Valo 
478e705c121SKalle Valo 	if (bssid)
479e705c121SKalle Valo 		memcpy(cmd->bssid_addr, bssid, ETH_ALEN);
480e705c121SKalle Valo 	else
481e705c121SKalle Valo 		eth_broadcast_addr(cmd->bssid_addr);
482e705c121SKalle Valo 
483e705c121SKalle Valo 	rcu_read_lock();
484e705c121SKalle Valo 	chanctx = rcu_dereference(vif->chanctx_conf);
485e705c121SKalle Valo 	iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
48657fbcce3SJohannes Berg 					    : NL80211_BAND_2GHZ,
487e705c121SKalle Valo 			  &cck_ack_rates, &ofdm_ack_rates);
488e705c121SKalle Valo 	rcu_read_unlock();
489e705c121SKalle Valo 
490e705c121SKalle Valo 	cmd->cck_rates = cpu_to_le32((u32)cck_ack_rates);
491e705c121SKalle Valo 	cmd->ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
492e705c121SKalle Valo 
493e705c121SKalle Valo 	cmd->cck_short_preamble =
494e705c121SKalle Valo 		cpu_to_le32(vif->bss_conf.use_short_preamble ?
495e705c121SKalle Valo 			    MAC_FLG_SHORT_PREAMBLE : 0);
496e705c121SKalle Valo 	cmd->short_slot =
497e705c121SKalle Valo 		cpu_to_le32(vif->bss_conf.use_short_slot ?
498e705c121SKalle Valo 			    MAC_FLG_SHORT_SLOT : 0);
499e705c121SKalle Valo 
50050f56044SIlan Peer 	cmd->filter_flags = 0;
501bd6f5bd7SAyala Beker 
502e705c121SKalle Valo 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
503cf6c6ea3SEmmanuel Grumbach 		u8 txf = iwl_mvm_mac_ac_to_tx_fifo(mvm, i);
504b5e2fe35SNaftali Goldstein 		u8 ucode_ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);
505e705c121SKalle Valo 
506b5e2fe35SNaftali Goldstein 		cmd->ac[ucode_ac].cw_min =
507e705c121SKalle Valo 			cpu_to_le16(mvmvif->queue_params[i].cw_min);
508b5e2fe35SNaftali Goldstein 		cmd->ac[ucode_ac].cw_max =
509e705c121SKalle Valo 			cpu_to_le16(mvmvif->queue_params[i].cw_max);
510b5e2fe35SNaftali Goldstein 		cmd->ac[ucode_ac].edca_txop =
511e705c121SKalle Valo 			cpu_to_le16(mvmvif->queue_params[i].txop * 32);
512b5e2fe35SNaftali Goldstein 		cmd->ac[ucode_ac].aifsn = mvmvif->queue_params[i].aifs;
513b5e2fe35SNaftali Goldstein 		cmd->ac[ucode_ac].fifos_mask = BIT(txf);
514e705c121SKalle Valo 	}
515e705c121SKalle Valo 
516e705c121SKalle Valo 	if (vif->bss_conf.qos)
517e705c121SKalle Valo 		cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
518e705c121SKalle Valo 
519e705c121SKalle Valo 	if (vif->bss_conf.use_cts_prot)
520e705c121SKalle Valo 		cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
521e705c121SKalle Valo 
522e705c121SKalle Valo 	IWL_DEBUG_RATE(mvm, "use_cts_prot %d, ht_operation_mode %d\n",
523e705c121SKalle Valo 		       vif->bss_conf.use_cts_prot,
524e705c121SKalle Valo 		       vif->bss_conf.ht_operation_mode);
525e705c121SKalle Valo 	if (vif->bss_conf.chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
526e705c121SKalle Valo 		cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
527e705c121SKalle Valo 	if (ht_enabled)
528e705c121SKalle Valo 		iwl_mvm_mac_ctxt_set_ht_flags(mvm, vif, cmd);
529e705c121SKalle Valo }
530e705c121SKalle Valo 
531e705c121SKalle Valo static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
532e705c121SKalle Valo 				     struct iwl_mac_ctx_cmd *cmd)
533e705c121SKalle Valo {
534e705c121SKalle Valo 	int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
535e705c121SKalle Valo 				       sizeof(*cmd), cmd);
536e705c121SKalle Valo 	if (ret)
537e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send MAC context (action:%d): %d\n",
538e705c121SKalle Valo 			le32_to_cpu(cmd->action), ret);
539e705c121SKalle Valo 	return ret;
540e705c121SKalle Valo }
541e705c121SKalle Valo 
542e705c121SKalle Valo static int iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm *mvm,
543e705c121SKalle Valo 				    struct ieee80211_vif *vif,
544e705c121SKalle Valo 				    u32 action, bool force_assoc_off,
545e705c121SKalle Valo 				    const u8 *bssid_override)
546e705c121SKalle Valo {
547e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd = {};
548e705c121SKalle Valo 	struct iwl_mac_data_sta *ctxt_sta;
549e705c121SKalle Valo 
550e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_STATION);
551e705c121SKalle Valo 
552e705c121SKalle Valo 	/* Fill the common data for all mac context types */
553e705c121SKalle Valo 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, bssid_override, action);
554e705c121SKalle Valo 
555e705c121SKalle Valo 	if (vif->p2p) {
556e705c121SKalle Valo 		struct ieee80211_p2p_noa_attr *noa =
557e705c121SKalle Valo 			&vif->bss_conf.p2p_noa_attr;
558e705c121SKalle Valo 
559e705c121SKalle Valo 		cmd.p2p_sta.ctwin = cpu_to_le32(noa->oppps_ctwindow &
560e705c121SKalle Valo 					IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
561e705c121SKalle Valo 		ctxt_sta = &cmd.p2p_sta.sta;
562e705c121SKalle Valo 	} else {
563e705c121SKalle Valo 		ctxt_sta = &cmd.sta;
564e705c121SKalle Valo 	}
565e705c121SKalle Valo 
566e705c121SKalle Valo 	/* We need the dtim_period to set the MAC as associated */
567e705c121SKalle Valo 	if (vif->bss_conf.assoc && vif->bss_conf.dtim_period &&
568e705c121SKalle Valo 	    !force_assoc_off) {
56950f56044SIlan Peer 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
57050f56044SIlan Peer 		u8 ap_sta_id = mvmvif->ap_sta_id;
571e705c121SKalle Valo 		u32 dtim_offs;
572e705c121SKalle Valo 
573e705c121SKalle Valo 		/*
574e705c121SKalle Valo 		 * The DTIM count counts down, so when it is N that means N
575e705c121SKalle Valo 		 * more beacon intervals happen until the DTIM TBTT. Therefore
576e705c121SKalle Valo 		 * add this to the current time. If that ends up being in the
577e705c121SKalle Valo 		 * future, the firmware will handle it.
578e705c121SKalle Valo 		 *
579e705c121SKalle Valo 		 * Also note that the system_timestamp (which we get here as
580e705c121SKalle Valo 		 * "sync_device_ts") and TSF timestamp aren't at exactly the
581e705c121SKalle Valo 		 * same offset in the frame -- the TSF is at the first symbol
582e705c121SKalle Valo 		 * of the TSF, the system timestamp is at signal acquisition
583e705c121SKalle Valo 		 * time. This means there's an offset between them of at most
584e705c121SKalle Valo 		 * a few hundred microseconds (24 * 8 bits + PLCP time gives
585e705c121SKalle Valo 		 * 384us in the longest case), this is currently not relevant
586e705c121SKalle Valo 		 * as the firmware wakes up around 2ms before the TBTT.
587e705c121SKalle Valo 		 */
588e705c121SKalle Valo 		dtim_offs = vif->bss_conf.sync_dtim_count *
589e705c121SKalle Valo 				vif->bss_conf.beacon_int;
590e705c121SKalle Valo 		/* convert TU to usecs */
591e705c121SKalle Valo 		dtim_offs *= 1024;
592e705c121SKalle Valo 
593e705c121SKalle Valo 		ctxt_sta->dtim_tsf =
594e705c121SKalle Valo 			cpu_to_le64(vif->bss_conf.sync_tsf + dtim_offs);
595e705c121SKalle Valo 		ctxt_sta->dtim_time =
596e705c121SKalle Valo 			cpu_to_le32(vif->bss_conf.sync_device_ts + dtim_offs);
597b5669727SAvraham Stern 		ctxt_sta->assoc_beacon_arrive_time =
598b5669727SAvraham Stern 			cpu_to_le32(vif->bss_conf.sync_device_ts);
599e705c121SKalle Valo 
600e705c121SKalle Valo 		IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
601e705c121SKalle Valo 			       le64_to_cpu(ctxt_sta->dtim_tsf),
602e705c121SKalle Valo 			       le32_to_cpu(ctxt_sta->dtim_time),
603e705c121SKalle Valo 			       dtim_offs);
604e705c121SKalle Valo 
605e705c121SKalle Valo 		ctxt_sta->is_assoc = cpu_to_le32(1);
60650f56044SIlan Peer 
6078b75858cSJohannes Berg 		if (!mvmvif->authorized &&
6088b75858cSJohannes Berg 		    fw_has_capa(&mvm->fw->ucode_capa,
6098b75858cSJohannes Berg 				IWL_UCODE_TLV_CAPA_COEX_HIGH_PRIO))
6108b75858cSJohannes Berg 			ctxt_sta->data_policy |=
6118b75858cSJohannes Berg 				cpu_to_le32(COEX_HIGH_PRIORITY_ENABLE);
6128b75858cSJohannes Berg 
61350f56044SIlan Peer 		/*
61450f56044SIlan Peer 		 * allow multicast data frames only as long as the station is
61550f56044SIlan Peer 		 * authorized, i.e., GTK keys are already installed (if needed)
61650f56044SIlan Peer 		 */
617be9ae34eSNathan Errera 		if (ap_sta_id < mvm->fw->ucode_capa.num_stations) {
61850f56044SIlan Peer 			struct ieee80211_sta *sta;
61950f56044SIlan Peer 
62050f56044SIlan Peer 			rcu_read_lock();
62150f56044SIlan Peer 
62250f56044SIlan Peer 			sta = rcu_dereference(mvm->fw_id_to_mac_id[ap_sta_id]);
62350f56044SIlan Peer 			if (!IS_ERR_OR_NULL(sta)) {
62450f56044SIlan Peer 				struct iwl_mvm_sta *mvmsta =
62550f56044SIlan Peer 					iwl_mvm_sta_from_mac80211(sta);
62650f56044SIlan Peer 
62750f56044SIlan Peer 				if (mvmsta->sta_state ==
62850f56044SIlan Peer 				    IEEE80211_STA_AUTHORIZED)
62950f56044SIlan Peer 					cmd.filter_flags |=
63050f56044SIlan Peer 						cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
63150f56044SIlan Peer 			}
63250f56044SIlan Peer 
63350f56044SIlan Peer 			rcu_read_unlock();
63450f56044SIlan Peer 		}
635e705c121SKalle Valo 	} else {
636e705c121SKalle Valo 		ctxt_sta->is_assoc = cpu_to_le32(0);
637e705c121SKalle Valo 
638e705c121SKalle Valo 		/* Allow beacons to pass through as long as we are not
639e705c121SKalle Valo 		 * associated, or we do not have dtim period information.
640e705c121SKalle Valo 		 */
641e705c121SKalle Valo 		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
642e705c121SKalle Valo 	}
643e705c121SKalle Valo 
644e705c121SKalle Valo 	ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
645e705c121SKalle Valo 	ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
646e705c121SKalle Valo 					      vif->bss_conf.dtim_period);
647e705c121SKalle Valo 
648e705c121SKalle Valo 	ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
649e705c121SKalle Valo 	ctxt_sta->assoc_id = cpu_to_le32(vif->bss_conf.aid);
650e705c121SKalle Valo 
651e705c121SKalle Valo 	if (vif->probe_req_reg && vif->bss_conf.assoc && vif->p2p)
652e705c121SKalle Valo 		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
653e705c121SKalle Valo 
6549394662aSLiad Kaufman 	if (vif->bss_conf.he_support && !iwlwifi_mod_params.disable_11ax) {
655514c3069SLuca Coelho 		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_11AX);
656cde5dbaaSShaul Triebitz 		if (vif->bss_conf.twt_requester && IWL_MVM_USE_TWT)
657866a6a85SEmmanuel Grumbach 			ctxt_sta->data_policy |= cpu_to_le32(TWT_SUPPORTED);
658659ac93dSShaul Triebitz 		if (vif->bss_conf.twt_protected)
659659ac93dSShaul Triebitz 			ctxt_sta->data_policy |=
660659ac93dSShaul Triebitz 				cpu_to_le32(PROTECTED_TWT_SUPPORTED);
6611269ba1cSShaul Triebitz 		if (vif->bss_conf.twt_broadcast)
6621269ba1cSShaul Triebitz 			ctxt_sta->data_policy |=
6631269ba1cSShaul Triebitz 				cpu_to_le32(BROADCAST_TWT_SUPPORTED);
664659ac93dSShaul Triebitz 	}
665d91d9b94SEmmanuel Grumbach 
666514c3069SLuca Coelho 
667e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
668e705c121SKalle Valo }
669e705c121SKalle Valo 
670e705c121SKalle Valo static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
671e705c121SKalle Valo 					 struct ieee80211_vif *vif,
672e705c121SKalle Valo 					 u32 action)
673e705c121SKalle Valo {
674e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd = {};
675b13f43a4SEmmanuel Grumbach 	u32 tfd_queue_msk = BIT(mvm->snif_queue);
676c8f54701SJohannes Berg 	int ret;
677e705c121SKalle Valo 
678e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
679e705c121SKalle Valo 
680e705c121SKalle Valo 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
681e705c121SKalle Valo 
682e705c121SKalle Valo 	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
683e705c121SKalle Valo 				       MAC_FILTER_IN_CONTROL_AND_MGMT |
684e705c121SKalle Valo 				       MAC_FILTER_IN_BEACON |
685e705c121SKalle Valo 				       MAC_FILTER_IN_PROBE_REQUEST |
68650f56044SIlan Peer 				       MAC_FILTER_IN_CRC32 |
68750f56044SIlan Peer 				       MAC_FILTER_ACCEPT_GRP);
688e705c121SKalle Valo 	ieee80211_hw_set(mvm->hw, RX_INCLUDES_FCS);
689e705c121SKalle Valo 
6900e39eb03SChaya Rachel Ivgi 	/* Allocate sniffer station */
6910e39eb03SChaya Rachel Ivgi 	ret = iwl_mvm_allocate_int_sta(mvm, &mvm->snif_sta, tfd_queue_msk,
692ced19f26SSara Sharon 				       vif->type, IWL_STA_GENERAL_PURPOSE);
6930e39eb03SChaya Rachel Ivgi 	if (ret)
6940e39eb03SChaya Rachel Ivgi 		return ret;
6950e39eb03SChaya Rachel Ivgi 
696e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
697e705c121SKalle Valo }
698e705c121SKalle Valo 
699e705c121SKalle Valo static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
700e705c121SKalle Valo 				     struct ieee80211_vif *vif,
701e705c121SKalle Valo 				     u32 action)
702e705c121SKalle Valo {
703e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
704e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd = {};
705e705c121SKalle Valo 
706e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
707e705c121SKalle Valo 
708e705c121SKalle Valo 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
709e705c121SKalle Valo 
710e705c121SKalle Valo 	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
71150f56044SIlan Peer 				       MAC_FILTER_IN_PROBE_REQUEST |
71250f56044SIlan Peer 				       MAC_FILTER_ACCEPT_GRP);
713e705c121SKalle Valo 
714e705c121SKalle Valo 	/* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */
715e705c121SKalle Valo 	cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
716e705c121SKalle Valo 
717e705c121SKalle Valo 	/* TODO: Assumes that the beacon id == mac context id */
718e705c121SKalle Valo 	cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
719e705c121SKalle Valo 
720e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
721e705c121SKalle Valo }
722e705c121SKalle Valo 
723e705c121SKalle Valo struct iwl_mvm_go_iterator_data {
724e705c121SKalle Valo 	bool go_active;
725e705c121SKalle Valo };
726e705c121SKalle Valo 
727e705c121SKalle Valo static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
728e705c121SKalle Valo {
729e705c121SKalle Valo 	struct iwl_mvm_go_iterator_data *data = _data;
730e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
731e705c121SKalle Valo 
732e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
733e705c121SKalle Valo 	    mvmvif->ap_ibss_active)
734e705c121SKalle Valo 		data->go_active = true;
735e705c121SKalle Valo }
736e705c121SKalle Valo 
737e705c121SKalle Valo static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
738e705c121SKalle Valo 					   struct ieee80211_vif *vif,
739e705c121SKalle Valo 					   u32 action)
740e705c121SKalle Valo {
741e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd = {};
742e705c121SKalle Valo 	struct iwl_mvm_go_iterator_data data = {};
743e705c121SKalle Valo 
744e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
745e705c121SKalle Valo 
746e705c121SKalle Valo 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
747e705c121SKalle Valo 
748e705c121SKalle Valo 	/* Override the filter flags to accept only probe requests */
749e705c121SKalle Valo 	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
750e705c121SKalle Valo 
751e705c121SKalle Valo 	/*
752e705c121SKalle Valo 	 * This flag should be set to true when the P2P Device is
753e705c121SKalle Valo 	 * discoverable and there is at least another active P2P GO. Settings
754e705c121SKalle Valo 	 * this flag will allow the P2P Device to be discoverable on other
755e705c121SKalle Valo 	 * channels in addition to its listen channel.
756e705c121SKalle Valo 	 * Note that this flag should not be set in other cases as it opens the
757e705c121SKalle Valo 	 * Rx filters on all MAC and increases the number of interrupts.
758e705c121SKalle Valo 	 */
759e705c121SKalle Valo 	ieee80211_iterate_active_interfaces_atomic(
760e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
761e705c121SKalle Valo 		iwl_mvm_go_iterator, &data);
762e705c121SKalle Valo 
763e705c121SKalle Valo 	cmd.p2p_dev.is_disc_extended = cpu_to_le32(data.go_active ? 1 : 0);
764e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
765e705c121SKalle Valo }
766e705c121SKalle Valo 
767138664a3SSara Sharon void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
7688364fbb4SSara Sharon 			      __le32 *tim_index, __le32 *tim_size,
769e705c121SKalle Valo 			      u8 *beacon, u32 frame_size)
770e705c121SKalle Valo {
771e705c121SKalle Valo 	u32 tim_idx;
772e705c121SKalle Valo 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
773e705c121SKalle Valo 
774e705c121SKalle Valo 	/* The index is relative to frame start but we start looking at the
775e705c121SKalle Valo 	 * variable-length part of the beacon. */
776e705c121SKalle Valo 	tim_idx = mgmt->u.beacon.variable - beacon;
777e705c121SKalle Valo 
778e705c121SKalle Valo 	/* Parse variable-length elements of beacon to find WLAN_EID_TIM */
779e705c121SKalle Valo 	while ((tim_idx < (frame_size - 2)) &&
780e705c121SKalle Valo 			(beacon[tim_idx] != WLAN_EID_TIM))
781e705c121SKalle Valo 		tim_idx += beacon[tim_idx+1] + 2;
782e705c121SKalle Valo 
783e705c121SKalle Valo 	/* If TIM field was found, set variables */
784e705c121SKalle Valo 	if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
7858364fbb4SSara Sharon 		*tim_index = cpu_to_le32(tim_idx);
7868364fbb4SSara Sharon 		*tim_size = cpu_to_le32((u32)beacon[tim_idx + 1]);
787e705c121SKalle Valo 	} else {
788e705c121SKalle Valo 		IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
789e705c121SKalle Valo 	}
790e705c121SKalle Valo }
791e705c121SKalle Valo 
792d3a108a4SAndrei Otcheretianski static u32 iwl_mvm_find_ie_offset(u8 *beacon, u8 eid, u32 frame_size)
793d3a108a4SAndrei Otcheretianski {
794d3a108a4SAndrei Otcheretianski 	struct ieee80211_mgmt *mgmt = (void *)beacon;
795d3a108a4SAndrei Otcheretianski 	const u8 *ie;
796d3a108a4SAndrei Otcheretianski 
797d3a108a4SAndrei Otcheretianski 	if (WARN_ON_ONCE(frame_size <= (mgmt->u.beacon.variable - beacon)))
798d3a108a4SAndrei Otcheretianski 		return 0;
799d3a108a4SAndrei Otcheretianski 
800d3a108a4SAndrei Otcheretianski 	frame_size -= mgmt->u.beacon.variable - beacon;
801d3a108a4SAndrei Otcheretianski 
802d3a108a4SAndrei Otcheretianski 	ie = cfg80211_find_ie(eid, mgmt->u.beacon.variable, frame_size);
803d3a108a4SAndrei Otcheretianski 	if (!ie)
804d3a108a4SAndrei Otcheretianski 		return 0;
805d3a108a4SAndrei Otcheretianski 
806d3a108a4SAndrei Otcheretianski 	return ie - beacon;
807d3a108a4SAndrei Otcheretianski }
808d3a108a4SAndrei Otcheretianski 
809138664a3SSara Sharon u8 iwl_mvm_mac_ctxt_get_lowest_rate(struct ieee80211_tx_info *info,
8106ca33f8bSHaim Dreyfuss 				    struct ieee80211_vif *vif)
8116ca33f8bSHaim Dreyfuss {
8126ca33f8bSHaim Dreyfuss 	u8 rate;
813d558b7f8STova Mussai 	if (info->band == NL80211_BAND_2GHZ && !vif->p2p)
8146ca33f8bSHaim Dreyfuss 		rate = IWL_FIRST_CCK_RATE;
815d558b7f8STova Mussai 	else
816d558b7f8STova Mussai 		rate = IWL_FIRST_OFDM_RATE;
8176ca33f8bSHaim Dreyfuss 
8186ca33f8bSHaim Dreyfuss 	return rate;
8196ca33f8bSHaim Dreyfuss }
8206ca33f8bSHaim Dreyfuss 
821cd2c46a7SMiri Korenblit u16 iwl_mvm_mac_ctxt_get_beacon_flags(const struct iwl_fw *fw, u8 rate_idx)
822cd2c46a7SMiri Korenblit {
823cd2c46a7SMiri Korenblit 	u16 flags = iwl_mvm_mac80211_idx_to_hwrate(fw, rate_idx);
824cd2c46a7SMiri Korenblit 	bool is_new_rate = iwl_fw_lookup_cmd_ver(fw,
825cd2c46a7SMiri Korenblit 						 LONG_GROUP,
826cd2c46a7SMiri Korenblit 						 BEACON_TEMPLATE_CMD,
827cd2c46a7SMiri Korenblit 						 0) > 10;
828cd2c46a7SMiri Korenblit 
829cd2c46a7SMiri Korenblit 	if (rate_idx <= IWL_FIRST_CCK_RATE)
830cd2c46a7SMiri Korenblit 		flags |= is_new_rate ? IWL_MAC_BEACON_CCK
831cd2c46a7SMiri Korenblit 			  : IWL_MAC_BEACON_CCK_V1;
832cd2c46a7SMiri Korenblit 
833cd2c46a7SMiri Korenblit 	return flags;
834cd2c46a7SMiri Korenblit }
835cd2c46a7SMiri Korenblit 
83609856582SLuca Coelho static void iwl_mvm_mac_ctxt_set_tx(struct iwl_mvm *mvm,
837e705c121SKalle Valo 				    struct ieee80211_vif *vif,
83809856582SLuca Coelho 				    struct sk_buff *beacon,
83909856582SLuca Coelho 				    struct iwl_tx_cmd *tx)
840e705c121SKalle Valo {
841e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
842e705c121SKalle Valo 	struct ieee80211_tx_info *info;
8436ca33f8bSHaim Dreyfuss 	u8 rate;
8446ca33f8bSHaim Dreyfuss 	u32 tx_flags;
845e705c121SKalle Valo 
846e705c121SKalle Valo 	info = IEEE80211_SKB_CB(beacon);
847e705c121SKalle Valo 
848e705c121SKalle Valo 	/* Set up TX command fields */
84909856582SLuca Coelho 	tx->len = cpu_to_le16((u16)beacon->len);
85009856582SLuca Coelho 	tx->sta_id = mvmvif->bcast_sta.sta_id;
85109856582SLuca Coelho 	tx->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
852e705c121SKalle Valo 	tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;
853e705c121SKalle Valo 	tx_flags |=
854e705c121SKalle Valo 		iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<
855e705c121SKalle Valo 						TX_CMD_FLG_BT_PRIO_POS;
85609856582SLuca Coelho 	tx->tx_flags = cpu_to_le32(tx_flags);
857e705c121SKalle Valo 
8581e3c3c35SEmmanuel Grumbach 	if (!fw_has_capa(&mvm->fw->ucode_capa,
859656fca00SAvraham Stern 			 IWL_UCODE_TLV_CAPA_BEACON_ANT_SELECTION))
860656fca00SAvraham Stern 		iwl_mvm_toggle_tx_ant(mvm, &mvm->mgmt_last_antenna_idx);
861e705c121SKalle Valo 
86209856582SLuca Coelho 	tx->rate_n_flags =
863e705c121SKalle Valo 		cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
864e705c121SKalle Valo 			    RATE_MCS_ANT_POS);
865e705c121SKalle Valo 
8666ca33f8bSHaim Dreyfuss 	rate = iwl_mvm_mac_ctxt_get_lowest_rate(info, vif);
867e705c121SKalle Valo 
868d35d95ceSMiri Korenblit 	tx->rate_n_flags |=
869d35d95ceSMiri Korenblit 		cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(mvm->fw, rate));
8706ca33f8bSHaim Dreyfuss 	if (rate == IWL_FIRST_CCK_RATE)
87148c6ebc1SMiri Korenblit 		tx->rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK_V1);
8726ca33f8bSHaim Dreyfuss 
87309856582SLuca Coelho }
874e705c121SKalle Valo 
875138664a3SSara Sharon int iwl_mvm_mac_ctxt_send_beacon_cmd(struct iwl_mvm *mvm,
87609856582SLuca Coelho 				     struct sk_buff *beacon,
87709856582SLuca Coelho 				     void *data, int len)
87809856582SLuca Coelho {
87909856582SLuca Coelho 	struct iwl_host_cmd cmd = {
88009856582SLuca Coelho 		.id = BEACON_TEMPLATE_CMD,
88109856582SLuca Coelho 		.flags = CMD_ASYNC,
88209856582SLuca Coelho 	};
88309856582SLuca Coelho 
88409856582SLuca Coelho 	cmd.len[0] = len;
88509856582SLuca Coelho 	cmd.data[0] = data;
886e705c121SKalle Valo 	cmd.dataflags[0] = 0;
88709856582SLuca Coelho 	cmd.len[1] = beacon->len;
888e705c121SKalle Valo 	cmd.data[1] = beacon->data;
889e705c121SKalle Valo 	cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
890e705c121SKalle Valo 
891e705c121SKalle Valo 	return iwl_mvm_send_cmd(mvm, &cmd);
892e705c121SKalle Valo }
893e705c121SKalle Valo 
89409856582SLuca Coelho static int iwl_mvm_mac_ctxt_send_beacon_v6(struct iwl_mvm *mvm,
89509856582SLuca Coelho 					   struct ieee80211_vif *vif,
89609856582SLuca Coelho 					   struct sk_buff *beacon)
89709856582SLuca Coelho {
89809856582SLuca Coelho 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
89909856582SLuca Coelho 	struct iwl_mac_beacon_cmd_v6 beacon_cmd = {};
90009856582SLuca Coelho 
90109856582SLuca Coelho 	iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
90209856582SLuca Coelho 
90309856582SLuca Coelho 	beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
90409856582SLuca Coelho 
90509856582SLuca Coelho 	if (vif->type == NL80211_IFTYPE_AP)
90609856582SLuca Coelho 		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
90709856582SLuca Coelho 					 &beacon_cmd.tim_size,
90809856582SLuca Coelho 					 beacon->data, beacon->len);
90909856582SLuca Coelho 
91009856582SLuca Coelho 	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
91109856582SLuca Coelho 						sizeof(beacon_cmd));
91209856582SLuca Coelho }
91309856582SLuca Coelho 
91409856582SLuca Coelho static int iwl_mvm_mac_ctxt_send_beacon_v7(struct iwl_mvm *mvm,
91509856582SLuca Coelho 					   struct ieee80211_vif *vif,
91609856582SLuca Coelho 					   struct sk_buff *beacon)
91709856582SLuca Coelho {
91809856582SLuca Coelho 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
91909856582SLuca Coelho 	struct iwl_mac_beacon_cmd_v7 beacon_cmd = {};
92009856582SLuca Coelho 
92109856582SLuca Coelho 	iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
92209856582SLuca Coelho 
92309856582SLuca Coelho 	beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
92409856582SLuca Coelho 
92509856582SLuca Coelho 	if (vif->type == NL80211_IFTYPE_AP)
92609856582SLuca Coelho 		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
92709856582SLuca Coelho 					 &beacon_cmd.tim_size,
92809856582SLuca Coelho 					 beacon->data, beacon->len);
92909856582SLuca Coelho 
93009856582SLuca Coelho 	beacon_cmd.csa_offset =
93109856582SLuca Coelho 		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
93209856582SLuca Coelho 						   WLAN_EID_CHANNEL_SWITCH,
93309856582SLuca Coelho 						   beacon->len));
93409856582SLuca Coelho 	beacon_cmd.ecsa_offset =
93509856582SLuca Coelho 		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
93609856582SLuca Coelho 						   WLAN_EID_EXT_CHANSWITCH_ANN,
93709856582SLuca Coelho 						   beacon->len));
93809856582SLuca Coelho 
93909856582SLuca Coelho 	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
94009856582SLuca Coelho 						sizeof(beacon_cmd));
94109856582SLuca Coelho }
94209856582SLuca Coelho 
9436ca33f8bSHaim Dreyfuss static int iwl_mvm_mac_ctxt_send_beacon_v9(struct iwl_mvm *mvm,
94409856582SLuca Coelho 					   struct ieee80211_vif *vif,
94509856582SLuca Coelho 					   struct sk_buff *beacon)
94609856582SLuca Coelho {
94709856582SLuca Coelho 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
9486ca33f8bSHaim Dreyfuss 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(beacon);
94909856582SLuca Coelho 	struct iwl_mac_beacon_cmd beacon_cmd = {};
9506ca33f8bSHaim Dreyfuss 	u8 rate = iwl_mvm_mac_ctxt_get_lowest_rate(info, vif);
9516ca33f8bSHaim Dreyfuss 	u16 flags;
952eae94cf8SLuca Coelho 	struct ieee80211_chanctx_conf *ctx;
953eae94cf8SLuca Coelho 	int channel;
954cd2c46a7SMiri Korenblit 	flags = iwl_mvm_mac_ctxt_get_beacon_flags(mvm->fw, rate);
9556ca33f8bSHaim Dreyfuss 
956eae94cf8SLuca Coelho 	/* Enable FILS on PSC channels only */
957eae94cf8SLuca Coelho 	rcu_read_lock();
958eae94cf8SLuca Coelho 	ctx = rcu_dereference(vif->chanctx_conf);
959eae94cf8SLuca Coelho 	channel = ieee80211_frequency_to_channel(ctx->def.chan->center_freq);
960eae94cf8SLuca Coelho 	WARN_ON(channel == 0);
961eae94cf8SLuca Coelho 	if (cfg80211_channel_is_psc(ctx->def.chan) &&
962eae94cf8SLuca Coelho 	    !IWL_MVM_DISABLE_AP_FILS) {
963cd2c46a7SMiri Korenblit 		flags |= iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP,
964cd2c46a7SMiri Korenblit 					       BEACON_TEMPLATE_CMD,
965cd2c46a7SMiri Korenblit 					       0) > 10 ?
966cd2c46a7SMiri Korenblit 			IWL_MAC_BEACON_FILS :
967cd2c46a7SMiri Korenblit 			IWL_MAC_BEACON_FILS_V1;
968eae94cf8SLuca Coelho 		beacon_cmd.short_ssid =
969eae94cf8SLuca Coelho 			cpu_to_le32(~crc32_le(~0, vif->bss_conf.ssid,
970eae94cf8SLuca Coelho 					      vif->bss_conf.ssid_len));
971eae94cf8SLuca Coelho 	}
972eae94cf8SLuca Coelho 	rcu_read_unlock();
973eae94cf8SLuca Coelho 
9746ca33f8bSHaim Dreyfuss 	beacon_cmd.flags = cpu_to_le16(flags);
97509856582SLuca Coelho 	beacon_cmd.byte_cnt = cpu_to_le16((u16)beacon->len);
97609856582SLuca Coelho 	beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
97709856582SLuca Coelho 
97809856582SLuca Coelho 	if (vif->type == NL80211_IFTYPE_AP)
9796ca33f8bSHaim Dreyfuss 		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
98009856582SLuca Coelho 					 &beacon_cmd.tim_size,
98109856582SLuca Coelho 					 beacon->data, beacon->len);
98209856582SLuca Coelho 
98309856582SLuca Coelho 	beacon_cmd.csa_offset =
98409856582SLuca Coelho 		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
98509856582SLuca Coelho 						   WLAN_EID_CHANNEL_SWITCH,
98609856582SLuca Coelho 						   beacon->len));
98709856582SLuca Coelho 	beacon_cmd.ecsa_offset =
98809856582SLuca Coelho 		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
98909856582SLuca Coelho 						   WLAN_EID_EXT_CHANSWITCH_ANN,
99009856582SLuca Coelho 						   beacon->len));
99109856582SLuca Coelho 
99209856582SLuca Coelho 	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
99309856582SLuca Coelho 						sizeof(beacon_cmd));
99409856582SLuca Coelho }
99509856582SLuca Coelho 
996138664a3SSara Sharon int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
99709856582SLuca Coelho 				 struct ieee80211_vif *vif,
99809856582SLuca Coelho 				 struct sk_buff *beacon)
99909856582SLuca Coelho {
100009856582SLuca Coelho 	if (WARN_ON(!beacon))
100109856582SLuca Coelho 		return -EINVAL;
100209856582SLuca Coelho 
100350386305SSara Sharon 	if (IWL_MVM_NON_TRANSMITTING_AP)
100450386305SSara Sharon 		return 0;
100550386305SSara Sharon 
100609856582SLuca Coelho 	if (!fw_has_capa(&mvm->fw->ucode_capa,
100709856582SLuca Coelho 			 IWL_UCODE_TLV_CAPA_CSA_AND_TBTT_OFFLOAD))
100809856582SLuca Coelho 		return iwl_mvm_mac_ctxt_send_beacon_v6(mvm, vif, beacon);
100909856582SLuca Coelho 
10106ca33f8bSHaim Dreyfuss 	if (fw_has_api(&mvm->fw->ucode_capa,
10116ca33f8bSHaim Dreyfuss 		       IWL_UCODE_TLV_API_NEW_BEACON_TEMPLATE))
10126ca33f8bSHaim Dreyfuss 		return iwl_mvm_mac_ctxt_send_beacon_v9(mvm, vif, beacon);
101309856582SLuca Coelho 
10146ca33f8bSHaim Dreyfuss 	return iwl_mvm_mac_ctxt_send_beacon_v7(mvm, vif, beacon);
101509856582SLuca Coelho }
101609856582SLuca Coelho 
1017e705c121SKalle Valo /* The beacon template for the AP/GO/IBSS has changed and needs update */
1018e705c121SKalle Valo int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
1019e705c121SKalle Valo 				    struct ieee80211_vif *vif)
1020e705c121SKalle Valo {
1021e705c121SKalle Valo 	struct sk_buff *beacon;
1022e705c121SKalle Valo 	int ret;
1023e705c121SKalle Valo 
1024e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_AP &&
1025e705c121SKalle Valo 		vif->type != NL80211_IFTYPE_ADHOC);
1026e705c121SKalle Valo 
1027e705c121SKalle Valo 	beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL);
1028e705c121SKalle Valo 	if (!beacon)
1029e705c121SKalle Valo 		return -ENOMEM;
1030e705c121SKalle Valo 
103190a12829SSara Sharon #ifdef CONFIG_IWLWIFI_DEBUGFS
10320f5d44acSZhang Qilong 	if (mvm->beacon_inject_active) {
10330f5d44acSZhang Qilong 		dev_kfree_skb(beacon);
103490a12829SSara Sharon 		return -EBUSY;
10350f5d44acSZhang Qilong 	}
103690a12829SSara Sharon #endif
103790a12829SSara Sharon 
1038e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
1039e705c121SKalle Valo 	dev_kfree_skb(beacon);
1040e705c121SKalle Valo 	return ret;
1041e705c121SKalle Valo }
1042e705c121SKalle Valo 
1043e705c121SKalle Valo struct iwl_mvm_mac_ap_iterator_data {
1044e705c121SKalle Valo 	struct iwl_mvm *mvm;
1045e705c121SKalle Valo 	struct ieee80211_vif *vif;
1046e705c121SKalle Valo 	u32 beacon_device_ts;
1047e705c121SKalle Valo 	u16 beacon_int;
1048e705c121SKalle Valo };
1049e705c121SKalle Valo 
1050e705c121SKalle Valo /* Find the beacon_device_ts and beacon_int for a managed interface */
1051e705c121SKalle Valo static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
1052e705c121SKalle Valo 				    struct ieee80211_vif *vif)
1053e705c121SKalle Valo {
1054e705c121SKalle Valo 	struct iwl_mvm_mac_ap_iterator_data *data = _data;
1055e705c121SKalle Valo 
1056e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc)
1057e705c121SKalle Valo 		return;
1058e705c121SKalle Valo 
1059e705c121SKalle Valo 	/* Station client has higher priority over P2P client*/
1060e705c121SKalle Valo 	if (vif->p2p && data->beacon_device_ts)
1061e705c121SKalle Valo 		return;
1062e705c121SKalle Valo 
1063e705c121SKalle Valo 	data->beacon_device_ts = vif->bss_conf.sync_device_ts;
1064e705c121SKalle Valo 	data->beacon_int = vif->bss_conf.beacon_int;
1065e705c121SKalle Valo }
1066e705c121SKalle Valo 
1067e705c121SKalle Valo /*
1068e705c121SKalle Valo  * Fill the specific data for mac context of type AP of P2P GO
1069e705c121SKalle Valo  */
1070e705c121SKalle Valo static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
1071e705c121SKalle Valo 					 struct ieee80211_vif *vif,
1072186cd49aSJohannes Berg 					 struct iwl_mac_ctx_cmd *cmd,
1073e705c121SKalle Valo 					 struct iwl_mac_data_ap *ctxt_ap,
1074e705c121SKalle Valo 					 bool add)
1075e705c121SKalle Valo {
1076e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1077e705c121SKalle Valo 	struct iwl_mvm_mac_ap_iterator_data data = {
1078e705c121SKalle Valo 		.mvm = mvm,
1079e705c121SKalle Valo 		.vif = vif,
1080e705c121SKalle Valo 		.beacon_device_ts = 0
1081e705c121SKalle Valo 	};
1082e705c121SKalle Valo 
1083186cd49aSJohannes Berg 	/* in AP mode, the MCAST FIFO takes the EDCA params from VO */
1084186cd49aSJohannes Berg 	cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |= BIT(IWL_MVM_TX_FIFO_MCAST);
1085186cd49aSJohannes Berg 
1086186cd49aSJohannes Berg 	/*
1087186cd49aSJohannes Berg 	 * in AP mode, pass probe requests and beacons from other APs
1088186cd49aSJohannes Berg 	 * (needed for ht protection); when there're no any associated
1089186cd49aSJohannes Berg 	 * station don't ask FW to pass beacons to prevent unnecessary
1090186cd49aSJohannes Berg 	 * wake-ups.
1091186cd49aSJohannes Berg 	 */
1092186cd49aSJohannes Berg 	cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
1093186cd49aSJohannes Berg 	if (mvmvif->ap_assoc_sta_count || !mvm->drop_bcn_ap_mode) {
1094186cd49aSJohannes Berg 		cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
1095186cd49aSJohannes Berg 		IWL_DEBUG_HC(mvm, "Asking FW to pass beacons\n");
1096186cd49aSJohannes Berg 	} else {
1097186cd49aSJohannes Berg 		IWL_DEBUG_HC(mvm, "No need to receive beacons\n");
1098186cd49aSJohannes Berg 	}
1099186cd49aSJohannes Berg 
1100e705c121SKalle Valo 	ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
1101e705c121SKalle Valo 	ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
1102e705c121SKalle Valo 					     vif->bss_conf.dtim_period);
1103e705c121SKalle Valo 
1104ced19f26SSara Sharon 	if (!fw_has_api(&mvm->fw->ucode_capa,
1105ced19f26SSara Sharon 			IWL_UCODE_TLV_API_STA_TYPE))
1106cfbc6c4cSSara Sharon 		ctxt_ap->mcast_qid = cpu_to_le32(mvmvif->cab_queue);
1107e705c121SKalle Valo 
1108e705c121SKalle Valo 	/*
1109e705c121SKalle Valo 	 * Only set the beacon time when the MAC is being added, when we
1110e705c121SKalle Valo 	 * just modify the MAC then we should keep the time -- the firmware
1111e705c121SKalle Valo 	 * can otherwise have a "jumping" TBTT.
1112e705c121SKalle Valo 	 */
1113e705c121SKalle Valo 	if (add) {
1114e705c121SKalle Valo 		/*
1115e705c121SKalle Valo 		 * If there is a station/P2P client interface which is
1116e705c121SKalle Valo 		 * associated, set the AP's TBTT far enough from the station's
1117e705c121SKalle Valo 		 * TBTT. Otherwise, set it to the current system time
1118e705c121SKalle Valo 		 */
1119e705c121SKalle Valo 		ieee80211_iterate_active_interfaces_atomic(
1120e705c121SKalle Valo 			mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1121e705c121SKalle Valo 			iwl_mvm_mac_ap_iterator, &data);
1122e705c121SKalle Valo 
1123e705c121SKalle Valo 		if (data.beacon_device_ts) {
1124e705c121SKalle Valo 			u32 rand = (prandom_u32() % (64 - 36)) + 36;
1125e705c121SKalle Valo 			mvmvif->ap_beacon_time = data.beacon_device_ts +
1126e705c121SKalle Valo 				ieee80211_tu_to_usec(data.beacon_int * rand /
1127e705c121SKalle Valo 						     100);
1128e705c121SKalle Valo 		} else {
1129afc1e3b4SAvraham Stern 			mvmvif->ap_beacon_time = iwl_mvm_get_systime(mvm);
1130e705c121SKalle Valo 		}
1131e705c121SKalle Valo 	}
1132e705c121SKalle Valo 
1133e705c121SKalle Valo 	ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
1134e705c121SKalle Valo 	ctxt_ap->beacon_tsf = 0; /* unused */
1135e705c121SKalle Valo 
1136e705c121SKalle Valo 	/* TODO: Assume that the beacon id == mac context id */
1137e705c121SKalle Valo 	ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
1138e705c121SKalle Valo }
1139e705c121SKalle Valo 
1140e705c121SKalle Valo static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
1141e705c121SKalle Valo 				   struct ieee80211_vif *vif,
1142e705c121SKalle Valo 				   u32 action)
1143e705c121SKalle Valo {
1144e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd = {};
1145e705c121SKalle Valo 
1146e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
1147e705c121SKalle Valo 
1148e705c121SKalle Valo 	/* Fill the common data for all mac context types */
1149e705c121SKalle Valo 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1150e705c121SKalle Valo 
1151e705c121SKalle Valo 	/* Fill the data specific for ap mode */
1152186cd49aSJohannes Berg 	iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.ap,
1153e705c121SKalle Valo 				     action == FW_CTXT_ACTION_ADD);
1154e705c121SKalle Valo 
1155e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1156e705c121SKalle Valo }
1157e705c121SKalle Valo 
1158e705c121SKalle Valo static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
1159e705c121SKalle Valo 				   struct ieee80211_vif *vif,
1160e705c121SKalle Valo 				   u32 action)
1161e705c121SKalle Valo {
1162e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd = {};
1163e705c121SKalle Valo 	struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
1164e705c121SKalle Valo 
1165e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
1166e705c121SKalle Valo 
1167e705c121SKalle Valo 	/* Fill the common data for all mac context types */
1168e705c121SKalle Valo 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1169e705c121SKalle Valo 
1170e705c121SKalle Valo 	/* Fill the data specific for GO mode */
1171186cd49aSJohannes Berg 	iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.go.ap,
1172e705c121SKalle Valo 				     action == FW_CTXT_ACTION_ADD);
1173e705c121SKalle Valo 
1174e705c121SKalle Valo 	cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
1175e705c121SKalle Valo 					IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
1176e705c121SKalle Valo 	cmd.go.opp_ps_enabled =
1177e705c121SKalle Valo 			cpu_to_le32(!!(noa->oppps_ctwindow &
1178e705c121SKalle Valo 					IEEE80211_P2P_OPPPS_ENABLE_BIT));
1179e705c121SKalle Valo 
1180e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1181e705c121SKalle Valo }
1182e705c121SKalle Valo 
1183e705c121SKalle Valo static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1184e705c121SKalle Valo 				u32 action, bool force_assoc_off,
1185e705c121SKalle Valo 				const u8 *bssid_override)
1186e705c121SKalle Valo {
1187e705c121SKalle Valo 	switch (vif->type) {
1188e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
1189e705c121SKalle Valo 		return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
1190e705c121SKalle Valo 						force_assoc_off,
1191e705c121SKalle Valo 						bssid_override);
1192e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
1193e705c121SKalle Valo 		if (!vif->p2p)
1194e705c121SKalle Valo 			return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
1195e705c121SKalle Valo 		else
1196e705c121SKalle Valo 			return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
1197e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
1198e705c121SKalle Valo 		return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
1199e705c121SKalle Valo 	case NL80211_IFTYPE_P2P_DEVICE:
1200e705c121SKalle Valo 		return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
1201e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
1202e705c121SKalle Valo 		return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
1203e705c121SKalle Valo 	default:
1204e705c121SKalle Valo 		break;
1205e705c121SKalle Valo 	}
1206e705c121SKalle Valo 
1207e705c121SKalle Valo 	return -EOPNOTSUPP;
1208e705c121SKalle Valo }
1209e705c121SKalle Valo 
1210e705c121SKalle Valo int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1211e705c121SKalle Valo {
1212e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1213e705c121SKalle Valo 	int ret;
1214e705c121SKalle Valo 
1215e705c121SKalle Valo 	if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
1216e705c121SKalle Valo 		      vif->addr, ieee80211_vif_type_p2p(vif)))
1217e705c121SKalle Valo 		return -EIO;
1218e705c121SKalle Valo 
1219e705c121SKalle Valo 	ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
1220e705c121SKalle Valo 				   true, NULL);
1221e705c121SKalle Valo 	if (ret)
1222e705c121SKalle Valo 		return ret;
1223e705c121SKalle Valo 
1224e705c121SKalle Valo 	/* will only do anything at resume from D3 time */
1225e705c121SKalle Valo 	iwl_mvm_set_last_nonqos_seq(mvm, vif);
1226e705c121SKalle Valo 
1227e705c121SKalle Valo 	mvmvif->uploaded = true;
1228e705c121SKalle Valo 	return 0;
1229e705c121SKalle Valo }
1230e705c121SKalle Valo 
1231e705c121SKalle Valo int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1232e705c121SKalle Valo 			     bool force_assoc_off, const u8 *bssid_override)
1233e705c121SKalle Valo {
1234e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1235e705c121SKalle Valo 
1236e705c121SKalle Valo 	if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
1237e705c121SKalle Valo 		      vif->addr, ieee80211_vif_type_p2p(vif)))
1238e705c121SKalle Valo 		return -EIO;
1239e705c121SKalle Valo 
1240e705c121SKalle Valo 	return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
1241e705c121SKalle Valo 				    force_assoc_off, bssid_override);
1242e705c121SKalle Valo }
1243e705c121SKalle Valo 
1244e705c121SKalle Valo int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1245e705c121SKalle Valo {
1246e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1247e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd;
1248e705c121SKalle Valo 	int ret;
1249e705c121SKalle Valo 
1250e705c121SKalle Valo 	if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1251e705c121SKalle Valo 		      vif->addr, ieee80211_vif_type_p2p(vif)))
1252e705c121SKalle Valo 		return -EIO;
1253e705c121SKalle Valo 
1254e705c121SKalle Valo 	memset(&cmd, 0, sizeof(cmd));
1255e705c121SKalle Valo 
1256e705c121SKalle Valo 	cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1257e705c121SKalle Valo 							   mvmvif->color));
1258e705c121SKalle Valo 	cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1259e705c121SKalle Valo 
1260e705c121SKalle Valo 	ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
1261e705c121SKalle Valo 				   sizeof(cmd), &cmd);
1262e705c121SKalle Valo 	if (ret) {
1263e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
1264e705c121SKalle Valo 		return ret;
1265e705c121SKalle Valo 	}
1266e705c121SKalle Valo 
1267e705c121SKalle Valo 	mvmvif->uploaded = false;
1268e705c121SKalle Valo 
12690e39eb03SChaya Rachel Ivgi 	if (vif->type == NL80211_IFTYPE_MONITOR) {
1270e705c121SKalle Valo 		__clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, mvm->hw->flags);
12710e39eb03SChaya Rachel Ivgi 		iwl_mvm_dealloc_snif_sta(mvm);
12720e39eb03SChaya Rachel Ivgi 	}
1273e705c121SKalle Valo 
1274e705c121SKalle Valo 	return 0;
1275e705c121SKalle Valo }
1276e705c121SKalle Valo 
1277e705c121SKalle Valo static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
1278e705c121SKalle Valo 				   struct ieee80211_vif *csa_vif, u32 gp2,
1279e705c121SKalle Valo 				   bool tx_success)
1280e705c121SKalle Valo {
1281e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif =
1282e705c121SKalle Valo 			iwl_mvm_vif_from_mac80211(csa_vif);
1283e705c121SKalle Valo 
1284e705c121SKalle Valo 	/* Don't start to countdown from a failed beacon */
1285e705c121SKalle Valo 	if (!tx_success && !mvmvif->csa_countdown)
1286e705c121SKalle Valo 		return;
1287e705c121SKalle Valo 
1288e705c121SKalle Valo 	mvmvif->csa_countdown = true;
1289e705c121SKalle Valo 
12908552a434SJohn Crispin 	if (!ieee80211_beacon_cntdwn_is_complete(csa_vif)) {
12918552a434SJohn Crispin 		int c = ieee80211_beacon_update_cntdwn(csa_vif);
1292e705c121SKalle Valo 
1293e705c121SKalle Valo 		iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif);
1294e705c121SKalle Valo 		if (csa_vif->p2p &&
1295e705c121SKalle Valo 		    !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2 &&
1296e705c121SKalle Valo 		    tx_success) {
1297e705c121SKalle Valo 			u32 rel_time = (c + 1) *
1298e705c121SKalle Valo 				       csa_vif->bss_conf.beacon_int -
1299e705c121SKalle Valo 				       IWL_MVM_CHANNEL_SWITCH_TIME_GO;
1300e705c121SKalle Valo 			u32 apply_time = gp2 + rel_time * 1024;
1301e705c121SKalle Valo 
1302e705c121SKalle Valo 			iwl_mvm_schedule_csa_period(mvm, csa_vif,
1303e705c121SKalle Valo 					 IWL_MVM_CHANNEL_SWITCH_TIME_GO -
1304e705c121SKalle Valo 					 IWL_MVM_CHANNEL_SWITCH_MARGIN,
1305e705c121SKalle Valo 					 apply_time);
1306e705c121SKalle Valo 		}
1307e705c121SKalle Valo 	} else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
1308e705c121SKalle Valo 		/* we don't have CSA NoA scheduled yet, switch now */
1309e705c121SKalle Valo 		ieee80211_csa_finish(csa_vif);
1310e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
1311e705c121SKalle Valo 	}
1312e705c121SKalle Valo }
1313e705c121SKalle Valo 
1314e705c121SKalle Valo void iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1315e705c121SKalle Valo 			     struct iwl_rx_cmd_buffer *rxb)
1316e705c121SKalle Valo {
1317e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1318afc857bcSJohannes Berg 	unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
1319e705c121SKalle Valo 	struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
132015e28c78SEmmanuel Grumbach 	struct iwl_extended_beacon_notif_v5 *beacon_v5 = (void *)pkt->data;
1321e705c121SKalle Valo 	struct ieee80211_vif *csa_vif;
1322e705c121SKalle Valo 	struct ieee80211_vif *tx_blocked_vif;
132312db294cSSara Sharon 	struct agg_tx_status *agg_status;
1324e705c121SKalle Valo 	u16 status;
1325e705c121SKalle Valo 
1326e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1327e705c121SKalle Valo 
1328e705c121SKalle Valo 	mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
1329e705c121SKalle Valo 
133015e28c78SEmmanuel Grumbach 	if (!iwl_mvm_is_short_beacon_notif_supported(mvm)) {
133115e28c78SEmmanuel Grumbach 		struct iwl_mvm_tx_resp *beacon_notify_hdr =
133215e28c78SEmmanuel Grumbach 			&beacon_v5->beacon_notify_hdr;
133315e28c78SEmmanuel Grumbach 
1334afc857bcSJohannes Berg 		if (unlikely(pkt_len < sizeof(*beacon_v5)))
1335afc857bcSJohannes Berg 			return;
1336afc857bcSJohannes Berg 
133715e28c78SEmmanuel Grumbach 		mvm->ibss_manager = beacon_v5->ibss_mgr_status != 0;
133812db294cSSara Sharon 		agg_status = iwl_mvm_get_agg_status(mvm, beacon_notify_hdr);
133912db294cSSara Sharon 		status = le16_to_cpu(agg_status->status) & TX_STATUS_MSK;
1340e705c121SKalle Valo 		IWL_DEBUG_RX(mvm,
1341677837b8SJohannes Berg 			     "beacon status %#x retries:%d tsf:0x%016llX gp2:0x%X rate:%d\n",
1342e705c121SKalle Valo 			     status, beacon_notify_hdr->failure_frame,
1343e705c121SKalle Valo 			     le64_to_cpu(beacon->tsf),
1344e705c121SKalle Valo 			     mvm->ap_last_beacon_gp2,
1345e705c121SKalle Valo 			     le32_to_cpu(beacon_notify_hdr->initial_rate));
134615e28c78SEmmanuel Grumbach 	} else {
1347afc857bcSJohannes Berg 		if (unlikely(pkt_len < sizeof(*beacon)))
1348afc857bcSJohannes Berg 			return;
1349afc857bcSJohannes Berg 
135015e28c78SEmmanuel Grumbach 		mvm->ibss_manager = beacon->ibss_mgr_status != 0;
135115e28c78SEmmanuel Grumbach 		status = le32_to_cpu(beacon->status) & TX_STATUS_MSK;
135215e28c78SEmmanuel Grumbach 		IWL_DEBUG_RX(mvm,
135315e28c78SEmmanuel Grumbach 			     "beacon status %#x tsf:0x%016llX gp2:0x%X\n",
135415e28c78SEmmanuel Grumbach 			     status, le64_to_cpu(beacon->tsf),
135515e28c78SEmmanuel Grumbach 			     mvm->ap_last_beacon_gp2);
135615e28c78SEmmanuel Grumbach 	}
1357e705c121SKalle Valo 
1358e705c121SKalle Valo 	csa_vif = rcu_dereference_protected(mvm->csa_vif,
1359e705c121SKalle Valo 					    lockdep_is_held(&mvm->mutex));
1360e705c121SKalle Valo 	if (unlikely(csa_vif && csa_vif->csa_active))
1361e705c121SKalle Valo 		iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2,
1362e705c121SKalle Valo 				       (status == TX_STATUS_SUCCESS));
1363e705c121SKalle Valo 
1364e705c121SKalle Valo 	tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
1365e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
1366e705c121SKalle Valo 	if (unlikely(tx_blocked_vif)) {
1367e705c121SKalle Valo 		struct iwl_mvm_vif *mvmvif =
1368e705c121SKalle Valo 			iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1369e705c121SKalle Valo 
1370e705c121SKalle Valo 		/*
1371e705c121SKalle Valo 		 * The channel switch is started and we have blocked the
1372e705c121SKalle Valo 		 * stations. If this is the first beacon (the timeout wasn't
1373e705c121SKalle Valo 		 * set), set the unblock timeout, otherwise countdown
1374e705c121SKalle Valo 		 */
1375e705c121SKalle Valo 		if (!mvm->csa_tx_block_bcn_timeout)
1376e705c121SKalle Valo 			mvm->csa_tx_block_bcn_timeout =
1377e705c121SKalle Valo 				IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
1378e705c121SKalle Valo 		else
1379e705c121SKalle Valo 			mvm->csa_tx_block_bcn_timeout--;
1380e705c121SKalle Valo 
1381e705c121SKalle Valo 		/* Check if the timeout is expired, and unblock tx */
1382e705c121SKalle Valo 		if (mvm->csa_tx_block_bcn_timeout == 0) {
1383e705c121SKalle Valo 			iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
1384e705c121SKalle Valo 			RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1385e705c121SKalle Valo 		}
1386e705c121SKalle Valo 	}
1387e705c121SKalle Valo }
1388e705c121SKalle Valo 
1389698478c4SSara Sharon void iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
1390698478c4SSara Sharon 				     struct iwl_rx_cmd_buffer *rxb)
1391e705c121SKalle Valo {
1392698478c4SSara Sharon 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1393698478c4SSara Sharon 	struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
1394e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_missed_bcon *bcon_trig;
1395e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trigger;
1396e705c121SKalle Valo 	u32 stop_trig_missed_bcon, stop_trig_missed_bcon_since_rx;
1397e705c121SKalle Valo 	u32 rx_missed_bcon, rx_missed_bcon_since_rx;
1398698478c4SSara Sharon 	struct ieee80211_vif *vif;
1399698478c4SSara Sharon 	u32 id = le32_to_cpu(mb->mac_id);
1400eae7550bSShahar S Matityahu 	union iwl_dbg_tlv_tp_data tp_data = { .fw_pkt = pkt };
1401e705c121SKalle Valo 
1402698478c4SSara Sharon 	IWL_DEBUG_INFO(mvm,
1403698478c4SSara Sharon 		       "missed bcn mac_id=%u, consecutive=%u (%u, %u, %u)\n",
1404698478c4SSara Sharon 		       le32_to_cpu(mb->mac_id),
1405698478c4SSara Sharon 		       le32_to_cpu(mb->consec_missed_beacons),
1406698478c4SSara Sharon 		       le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
1407698478c4SSara Sharon 		       le32_to_cpu(mb->num_recvd_beacons),
1408698478c4SSara Sharon 		       le32_to_cpu(mb->num_expected_beacons));
1409e705c121SKalle Valo 
1410698478c4SSara Sharon 	rcu_read_lock();
1411698478c4SSara Sharon 
1412698478c4SSara Sharon 	vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);
1413698478c4SSara Sharon 	if (!vif)
1414698478c4SSara Sharon 		goto out;
1415698478c4SSara Sharon 
1416698478c4SSara Sharon 	rx_missed_bcon = le32_to_cpu(mb->consec_missed_beacons);
1417e705c121SKalle Valo 	rx_missed_bcon_since_rx =
1418698478c4SSara Sharon 		le32_to_cpu(mb->consec_missed_beacons_since_last_rx);
1419e705c121SKalle Valo 	/*
1420e705c121SKalle Valo 	 * TODO: the threshold should be adjusted based on latency conditions,
1421e705c121SKalle Valo 	 * and/or in case of a CS flow on one of the other AP vifs.
1422e705c121SKalle Valo 	 */
1423babea2d4SAndrei Otcheretianski 	if (rx_missed_bcon > IWL_MVM_MISSED_BEACONS_THRESHOLD_LONG)
1424babea2d4SAndrei Otcheretianski 		iwl_mvm_connection_loss(mvm, vif, "missed beacons");
1425babea2d4SAndrei Otcheretianski 	else if (rx_missed_bcon_since_rx > IWL_MVM_MISSED_BEACONS_THRESHOLD)
1426e705c121SKalle Valo 		ieee80211_beacon_loss(vif);
1427e705c121SKalle Valo 
1428b108d8c7SShahar S Matityahu 	iwl_dbg_tlv_time_point(&mvm->fwrt,
1429eae7550bSShahar S Matityahu 			       IWL_FW_INI_TIME_POINT_MISSED_BEACONS, &tp_data);
1430b108d8c7SShahar S Matityahu 
14316c042d75SSara Sharon 	trigger = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
14326c042d75SSara Sharon 					FW_DBG_TRIGGER_MISSED_BEACONS);
14336c042d75SSara Sharon 	if (!trigger)
1434698478c4SSara Sharon 		goto out;
1435e705c121SKalle Valo 
1436e705c121SKalle Valo 	bcon_trig = (void *)trigger->data;
1437e705c121SKalle Valo 	stop_trig_missed_bcon = le32_to_cpu(bcon_trig->stop_consec_missed_bcon);
1438e705c121SKalle Valo 	stop_trig_missed_bcon_since_rx =
1439e705c121SKalle Valo 		le32_to_cpu(bcon_trig->stop_consec_missed_bcon_since_rx);
1440e705c121SKalle Valo 
1441e705c121SKalle Valo 	/* TODO: implement start trigger */
1442e705c121SKalle Valo 
1443e705c121SKalle Valo 	if (rx_missed_bcon_since_rx >= stop_trig_missed_bcon_since_rx ||
1444e705c121SKalle Valo 	    rx_missed_bcon >= stop_trig_missed_bcon)
14457174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&mvm->fwrt, trigger, NULL);
1446da2eb669SSara Sharon 
1447698478c4SSara Sharon out:
1448698478c4SSara Sharon 	rcu_read_unlock();
1449e705c121SKalle Valo }
14500db056d3SSara Sharon 
14510db056d3SSara Sharon void iwl_mvm_rx_stored_beacon_notif(struct iwl_mvm *mvm,
14520db056d3SSara Sharon 				    struct iwl_rx_cmd_buffer *rxb)
14530db056d3SSara Sharon {
14540db056d3SSara Sharon 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1455afc857bcSJohannes Berg 	unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
1456fb3fac5fSGregory Greenman 	struct iwl_stored_beacon_notif_common *sb = (void *)pkt->data;
14570db056d3SSara Sharon 	struct ieee80211_rx_status rx_status;
14580db056d3SSara Sharon 	struct sk_buff *skb;
1459fb3fac5fSGregory Greenman 	u8 *data;
14600db056d3SSara Sharon 	u32 size = le32_to_cpu(sb->byte_count);
1461fb3fac5fSGregory Greenman 	int ver = iwl_fw_lookup_cmd_ver(mvm->fw, PROT_OFFLOAD_GROUP,
1462fb3fac5fSGregory Greenman 					STORED_BEACON_NTF, 0);
14630db056d3SSara Sharon 
1464fb3fac5fSGregory Greenman 	if (size == 0)
14650db056d3SSara Sharon 		return;
14660db056d3SSara Sharon 
1467fb3fac5fSGregory Greenman 	/* handle per-version differences */
1468fb3fac5fSGregory Greenman 	if (ver <= 2) {
1469fb3fac5fSGregory Greenman 		struct iwl_stored_beacon_notif_v2 *sb_v2 = (void *)pkt->data;
1470fb3fac5fSGregory Greenman 
1471fb3fac5fSGregory Greenman 		if (pkt_len < struct_size(sb_v2, data, size))
1472fb3fac5fSGregory Greenman 			return;
1473fb3fac5fSGregory Greenman 
1474fb3fac5fSGregory Greenman 		data = sb_v2->data;
1475fb3fac5fSGregory Greenman 	} else {
1476fb3fac5fSGregory Greenman 		struct iwl_stored_beacon_notif_v3 *sb_v3 = (void *)pkt->data;
1477fb3fac5fSGregory Greenman 
1478fb3fac5fSGregory Greenman 		if (pkt_len < struct_size(sb_v3, data, size))
1479fb3fac5fSGregory Greenman 			return;
1480fb3fac5fSGregory Greenman 
1481fb3fac5fSGregory Greenman 		data = sb_v3->data;
1482fb3fac5fSGregory Greenman 	}
1483fb3fac5fSGregory Greenman 
14840db056d3SSara Sharon 	skb = alloc_skb(size, GFP_ATOMIC);
14850db056d3SSara Sharon 	if (!skb) {
14860db056d3SSara Sharon 		IWL_ERR(mvm, "alloc_skb failed\n");
14870db056d3SSara Sharon 		return;
14880db056d3SSara Sharon 	}
14890db056d3SSara Sharon 
14900db056d3SSara Sharon 	/* update rx_status according to the notification's metadata */
14910db056d3SSara Sharon 	memset(&rx_status, 0, sizeof(rx_status));
14920db056d3SSara Sharon 	rx_status.mactime = le64_to_cpu(sb->tsf);
149377fe7395SSara Sharon 	/* TSF as indicated by the firmware  is at INA time */
149477fe7395SSara Sharon 	rx_status.flag |= RX_FLAG_MACTIME_PLCP_START;
14950db056d3SSara Sharon 	rx_status.device_timestamp = le32_to_cpu(sb->system_time);
14960db056d3SSara Sharon 	rx_status.band =
149734118c25SSara Sharon 		(sb->band & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
149857fbcce3SJohannes Berg 				NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
14990db056d3SSara Sharon 	rx_status.freq =
15000db056d3SSara Sharon 		ieee80211_channel_to_frequency(le16_to_cpu(sb->channel),
15010db056d3SSara Sharon 					       rx_status.band);
15020db056d3SSara Sharon 
15030db056d3SSara Sharon 	/* copy the data */
1504fb3fac5fSGregory Greenman 	skb_put_data(skb, data, size);
15050db056d3SSara Sharon 	memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
15060db056d3SSara Sharon 
15070db056d3SSara Sharon 	/* pass it as regular rx to mac80211 */
1508d63b548fSJohannes Berg 	ieee80211_rx_napi(mvm->hw, NULL, skb, NULL);
15090db056d3SSara Sharon }
1510d3a108a4SAndrei Otcheretianski 
1511698478c4SSara Sharon void iwl_mvm_probe_resp_data_notif(struct iwl_mvm *mvm,
1512698478c4SSara Sharon 				   struct iwl_rx_cmd_buffer *rxb)
151386e177d8SGregory Greenman {
1514698478c4SSara Sharon 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1515698478c4SSara Sharon 	struct iwl_probe_resp_data_notif *notif = (void *)pkt->data;
151686e177d8SGregory Greenman 	struct iwl_probe_resp_data *old_data, *new_data;
1517698478c4SSara Sharon 	u32 id = le32_to_cpu(notif->mac_id);
1518698478c4SSara Sharon 	struct ieee80211_vif *vif;
1519698478c4SSara Sharon 	struct iwl_mvm_vif *mvmvif;
152086e177d8SGregory Greenman 
1521698478c4SSara Sharon 	IWL_DEBUG_INFO(mvm, "Probe response data notif: noa %d, csa %d\n",
1522698478c4SSara Sharon 		       notif->noa_active, notif->csa_counter);
1523698478c4SSara Sharon 
1524698478c4SSara Sharon 	vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, false);
1525698478c4SSara Sharon 	if (!vif)
1526698478c4SSara Sharon 		return;
1527698478c4SSara Sharon 
1528698478c4SSara Sharon 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
1529698478c4SSara Sharon 
153086e177d8SGregory Greenman 	new_data = kzalloc(sizeof(*new_data), GFP_KERNEL);
153186e177d8SGregory Greenman 	if (!new_data)
153286e177d8SGregory Greenman 		return;
153386e177d8SGregory Greenman 
153486e177d8SGregory Greenman 	memcpy(&new_data->notif, notif, sizeof(new_data->notif));
153586e177d8SGregory Greenman 
153686e177d8SGregory Greenman 	/* noa_attr contains 1 reserved byte, need to substruct it */
153786e177d8SGregory Greenman 	new_data->noa_len = sizeof(struct ieee80211_vendor_ie) +
153886e177d8SGregory Greenman 			    sizeof(new_data->notif.noa_attr) - 1;
153986e177d8SGregory Greenman 
154086e177d8SGregory Greenman 	/*
154186e177d8SGregory Greenman 	 * If it's a one time NoA, only one descriptor is needed,
154286e177d8SGregory Greenman 	 * adjust the length according to len_low.
154386e177d8SGregory Greenman 	 */
154486e177d8SGregory Greenman 	if (new_data->notif.noa_attr.len_low ==
154586e177d8SGregory Greenman 	    sizeof(struct ieee80211_p2p_noa_desc) + 2)
154686e177d8SGregory Greenman 		new_data->noa_len -= sizeof(struct ieee80211_p2p_noa_desc);
154786e177d8SGregory Greenman 
154886e177d8SGregory Greenman 	old_data = rcu_dereference_protected(mvmvif->probe_resp_data,
154986e177d8SGregory Greenman 					lockdep_is_held(&mvmvif->mvm->mutex));
155086e177d8SGregory Greenman 	rcu_assign_pointer(mvmvif->probe_resp_data, new_data);
155186e177d8SGregory Greenman 
155286e177d8SGregory Greenman 	if (old_data)
155386e177d8SGregory Greenman 		kfree_rcu(old_data, rcu_head);
155486e177d8SGregory Greenman 
155586e177d8SGregory Greenman 	if (notif->csa_counter != IWL_PROBE_RESP_DATA_NO_CSA &&
155686e177d8SGregory Greenman 	    notif->csa_counter >= 1)
15578552a434SJohn Crispin 		ieee80211_beacon_set_cntdwn(vif, notif->csa_counter);
155886e177d8SGregory Greenman }
155986e177d8SGregory Greenman 
1560*6905eb1cSNathan Errera void iwl_mvm_channel_switch_start_notif(struct iwl_mvm *mvm,
1561d3a108a4SAndrei Otcheretianski 					struct iwl_rx_cmd_buffer *rxb)
1562d3a108a4SAndrei Otcheretianski {
1563d3a108a4SAndrei Otcheretianski 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1564*6905eb1cSNathan Errera 	struct iwl_channel_switch_start_notif *notif = (void *)pkt->data;
156574a10252SSara Sharon 	struct ieee80211_vif *csa_vif, *vif;
1566d3a108a4SAndrei Otcheretianski 	struct iwl_mvm_vif *mvmvif;
156774a10252SSara Sharon 	u32 id_n_color, csa_id, mac_id;
1568d3a108a4SAndrei Otcheretianski 
156974a10252SSara Sharon 	id_n_color = le32_to_cpu(notif->id_and_color);
157074a10252SSara Sharon 	mac_id = id_n_color & FW_CTXT_ID_MSK;
1571d3a108a4SAndrei Otcheretianski 
157274a10252SSara Sharon 	if (WARN_ON_ONCE(mac_id >= NUM_MAC_INDEX_DRIVER))
157374a10252SSara Sharon 		return;
157474a10252SSara Sharon 
157574a10252SSara Sharon 	rcu_read_lock();
157674a10252SSara Sharon 	vif = rcu_dereference(mvm->vif_id_to_mac[mac_id]);
1577f6780614SSara Sharon 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
157874a10252SSara Sharon 
157974a10252SSara Sharon 	switch (vif->type) {
158074a10252SSara Sharon 	case NL80211_IFTYPE_AP:
1581d3a108a4SAndrei Otcheretianski 		csa_vif = rcu_dereference(mvm->csa_vif);
158274a10252SSara Sharon 		if (WARN_ON(!csa_vif || !csa_vif->csa_active ||
158374a10252SSara Sharon 			    csa_vif != vif))
1584d3a108a4SAndrei Otcheretianski 			goto out_unlock;
1585d3a108a4SAndrei Otcheretianski 
158674a10252SSara Sharon 		csa_id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
158774a10252SSara Sharon 		if (WARN(csa_id != id_n_color,
1588d3a108a4SAndrei Otcheretianski 			 "channel switch noa notification on unexpected vif (csa_vif=%d, notif=%d)",
158974a10252SSara Sharon 			 csa_id, id_n_color))
1590d3a108a4SAndrei Otcheretianski 			goto out_unlock;
1591d3a108a4SAndrei Otcheretianski 
1592d3a108a4SAndrei Otcheretianski 		IWL_DEBUG_INFO(mvm, "Channel Switch Started Notification\n");
1593d3a108a4SAndrei Otcheretianski 
159486bbb1e1SJohannes Berg 		schedule_delayed_work(&mvm->cs_tx_unblock_dwork,
1595d3a108a4SAndrei Otcheretianski 				      msecs_to_jiffies(IWL_MVM_CS_UNBLOCK_TX_TIMEOUT *
1596d3a108a4SAndrei Otcheretianski 						       csa_vif->bss_conf.beacon_int));
1597d3a108a4SAndrei Otcheretianski 
1598d3a108a4SAndrei Otcheretianski 		ieee80211_csa_finish(csa_vif);
1599d3a108a4SAndrei Otcheretianski 
1600d3a108a4SAndrei Otcheretianski 		rcu_read_unlock();
1601d3a108a4SAndrei Otcheretianski 
1602d3a108a4SAndrei Otcheretianski 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
1603d3a108a4SAndrei Otcheretianski 		return;
160474a10252SSara Sharon 	case NL80211_IFTYPE_STATION:
160574a10252SSara Sharon 		iwl_mvm_csa_client_absent(mvm, vif);
160611af74adSAndrei Otcheretianski 		cancel_delayed_work(&mvmvif->csa_work);
160774a10252SSara Sharon 		ieee80211_chswitch_done(vif, true);
160874a10252SSara Sharon 		break;
160974a10252SSara Sharon 	default:
161074a10252SSara Sharon 		/* should never happen */
161174a10252SSara Sharon 		WARN_ON_ONCE(1);
161274a10252SSara Sharon 		break;
161374a10252SSara Sharon 	}
1614d3a108a4SAndrei Otcheretianski out_unlock:
1615d3a108a4SAndrei Otcheretianski 	rcu_read_unlock();
1616d3a108a4SAndrei Otcheretianski }
1617449a29d0SLior Cohen 
1618449a29d0SLior Cohen void iwl_mvm_rx_missed_vap_notif(struct iwl_mvm *mvm,
1619449a29d0SLior Cohen 				 struct iwl_rx_cmd_buffer *rxb)
1620449a29d0SLior Cohen {
1621449a29d0SLior Cohen 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1622449a29d0SLior Cohen 	struct iwl_missed_vap_notif *mb = (void *)pkt->data;
1623449a29d0SLior Cohen 	struct ieee80211_vif *vif;
1624449a29d0SLior Cohen 	u32 id = le32_to_cpu(mb->mac_id);
1625449a29d0SLior Cohen 
1626449a29d0SLior Cohen 	IWL_DEBUG_INFO(mvm,
1627449a29d0SLior Cohen 		       "missed_vap notify mac_id=%u, num_beacon_intervals_elapsed=%u, profile_periodicity=%u\n",
1628449a29d0SLior Cohen 		       le32_to_cpu(mb->mac_id),
1629449a29d0SLior Cohen 		       mb->num_beacon_intervals_elapsed,
1630449a29d0SLior Cohen 		       mb->profile_periodicity);
1631449a29d0SLior Cohen 
1632449a29d0SLior Cohen 	rcu_read_lock();
1633449a29d0SLior Cohen 
1634449a29d0SLior Cohen 	vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);
1635449a29d0SLior Cohen 	if (vif)
1636449a29d0SLior Cohen 		iwl_mvm_connection_loss(mvm, vif, "missed vap beacon");
1637449a29d0SLior Cohen 
1638449a29d0SLior Cohen 	rcu_read_unlock();
1639449a29d0SLior Cohen }
1640