18e99ea8dSJohannes Berg // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
28e99ea8dSJohannes Berg /*
338e72100SJohannes Berg  * Copyright (C) 2012-2014, 2018-2023 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 
22895a35ec7SGregory Greenman 	/* MAC ID 0 should be used only for the managed/IBSS vif with non-MLO
22995a35ec7SGregory Greenman 	 * FW API
23095a35ec7SGregory Greenman 	 */
23195a35ec7SGregory Greenman 	if (!mvm->mld_api_is_used) {
232e705c121SKalle Valo 		switch (vif->type) {
233e705c121SKalle Valo 		case NL80211_IFTYPE_ADHOC:
234e705c121SKalle Valo 			break;
235e705c121SKalle Valo 		case NL80211_IFTYPE_STATION:
236e705c121SKalle Valo 			if (!vif->p2p)
237e705c121SKalle Valo 				break;
2385a2abdcaSGustavo A. R. Silva 			fallthrough;
239e705c121SKalle Valo 		default:
240e705c121SKalle Valo 			__clear_bit(0, data.available_mac_ids);
241e705c121SKalle Valo 		}
24295a35ec7SGregory Greenman 	}
243e705c121SKalle Valo 
244e705c121SKalle Valo 	ieee80211_iterate_active_interfaces_atomic(
245e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
246e705c121SKalle Valo 		iwl_mvm_mac_iface_iterator, &data);
247e705c121SKalle Valo 
248e705c121SKalle Valo 	/*
249e705c121SKalle Valo 	 * In the case we're getting here during resume, it's similar to
250e705c121SKalle Valo 	 * firmware restart, and with RESUME_ALL the iterator will find
251e705c121SKalle Valo 	 * the vif being added already.
252e705c121SKalle Valo 	 * We don't want to reassign any IDs in either case since doing
253e705c121SKalle Valo 	 * so would probably assign different IDs (as interfaces aren't
254e705c121SKalle Valo 	 * necessarily added in the same order), but the old IDs were
255e705c121SKalle Valo 	 * preserved anyway, so skip ID assignment for both resume and
256e705c121SKalle Valo 	 * recovery.
257e705c121SKalle Valo 	 */
258e705c121SKalle Valo 	if (data.found_vif)
259e705c121SKalle Valo 		return 0;
260e705c121SKalle Valo 
261e705c121SKalle Valo 	/* Therefore, in recovery, we can't get here */
262e705c121SKalle Valo 	if (WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
263e705c121SKalle Valo 		return -EBUSY;
264e705c121SKalle Valo 
265e705c121SKalle Valo 	mvmvif->id = find_first_bit(data.available_mac_ids,
266e705c121SKalle Valo 				    NUM_MAC_INDEX_DRIVER);
267e705c121SKalle Valo 	if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
268e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
269e705c121SKalle Valo 		ret = -EIO;
270e705c121SKalle Valo 		goto exit_fail;
271e705c121SKalle Valo 	}
272e705c121SKalle Valo 
273e705c121SKalle Valo 	if (data.preferred_tsf != NUM_TSF_IDS)
274e705c121SKalle Valo 		mvmvif->tsf_id = data.preferred_tsf;
275e705c121SKalle Valo 	else
276e705c121SKalle Valo 		mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
277e705c121SKalle Valo 						NUM_TSF_IDS);
278e705c121SKalle Valo 	if (mvmvif->tsf_id == NUM_TSF_IDS) {
279e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
280e705c121SKalle Valo 		ret = -EIO;
281e705c121SKalle Valo 		goto exit_fail;
282e705c121SKalle Valo 	}
283e705c121SKalle Valo 
284e705c121SKalle Valo 	mvmvif->color = 0;
285e705c121SKalle Valo 
286e705c121SKalle Valo 	INIT_LIST_HEAD(&mvmvif->time_event_data.list);
287e705c121SKalle Valo 	mvmvif->time_event_data.id = TE_MAX;
288e705c121SKalle Valo 
2893f7fbc8cSJohannes Berg 	/* No need to allocate data queues to P2P Device MAC and NAN.*/
2903f7fbc8cSJohannes Berg 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
291e705c121SKalle Valo 		return 0;
292e705c121SKalle Valo 
293e705c121SKalle Valo 	/* Allocate the CAB queue for softAP and GO interfaces */
294fc07bd8cSSara Sharon 	if (vif->type == NL80211_IFTYPE_AP ||
295fc07bd8cSSara Sharon 	    vif->type == NL80211_IFTYPE_ADHOC) {
296e2af3fabSSara Sharon 		/*
297e2af3fabSSara Sharon 		 * For TVQM this will be overwritten later with the FW assigned
298e2af3fabSSara Sharon 		 * queue value (when queue is enabled).
299e2af3fabSSara Sharon 		 */
300650cadb7SGregory Greenman 		mvmvif->deflink.cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
301e705c121SKalle Valo 	}
302e705c121SKalle Valo 
303650cadb7SGregory Greenman 	mvmvif->deflink.bcast_sta.sta_id = IWL_MVM_INVALID_STA;
304650cadb7SGregory Greenman 	mvmvif->deflink.mcast_sta.sta_id = IWL_MVM_INVALID_STA;
305650cadb7SGregory Greenman 	mvmvif->deflink.ap_sta_id = IWL_MVM_INVALID_STA;
306e705c121SKalle Valo 
307e705c121SKalle Valo 	for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
308650cadb7SGregory Greenman 		mvmvif->deflink.smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
309e705c121SKalle Valo 
310e705c121SKalle Valo 	return 0;
311e705c121SKalle Valo 
312e705c121SKalle Valo exit_fail:
313e705c121SKalle Valo 	memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
314e705c121SKalle Valo 	return ret;
315e705c121SKalle Valo }
316e705c121SKalle Valo 
317e705c121SKalle Valo static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
318e705c121SKalle Valo 			      struct ieee80211_vif *vif,
31957fbcce3SJohannes Berg 			      enum nl80211_band band,
320e705c121SKalle Valo 			      u8 *cck_rates, u8 *ofdm_rates)
321e705c121SKalle Valo {
322e705c121SKalle Valo 	struct ieee80211_supported_band *sband;
323e705c121SKalle Valo 	unsigned long basic = vif->bss_conf.basic_rates;
324e705c121SKalle Valo 	int lowest_present_ofdm = 100;
325e705c121SKalle Valo 	int lowest_present_cck = 100;
326e705c121SKalle Valo 	u8 cck = 0;
327e705c121SKalle Valo 	u8 ofdm = 0;
328e705c121SKalle Valo 	int i;
329e705c121SKalle Valo 
330e705c121SKalle Valo 	sband = mvm->hw->wiphy->bands[band];
331e705c121SKalle Valo 
332e705c121SKalle Valo 	for_each_set_bit(i, &basic, BITS_PER_LONG) {
333e705c121SKalle Valo 		int hw = sband->bitrates[i].hw_value;
334e705c121SKalle Valo 		if (hw >= IWL_FIRST_OFDM_RATE) {
335e705c121SKalle Valo 			ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
336e705c121SKalle Valo 			if (lowest_present_ofdm > hw)
337e705c121SKalle Valo 				lowest_present_ofdm = hw;
338e705c121SKalle Valo 		} else {
339e705c121SKalle Valo 			BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
340e705c121SKalle Valo 
341e705c121SKalle Valo 			cck |= BIT(hw);
342e705c121SKalle Valo 			if (lowest_present_cck > hw)
343e705c121SKalle Valo 				lowest_present_cck = hw;
344e705c121SKalle Valo 		}
345e705c121SKalle Valo 	}
346e705c121SKalle Valo 
347e705c121SKalle Valo 	/*
348e705c121SKalle Valo 	 * Now we've got the basic rates as bitmaps in the ofdm and cck
349e705c121SKalle Valo 	 * variables. This isn't sufficient though, as there might not
350e705c121SKalle Valo 	 * be all the right rates in the bitmap. E.g. if the only basic
351e705c121SKalle Valo 	 * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
352e705c121SKalle Valo 	 * and 6 Mbps because the 802.11-2007 standard says in 9.6:
353e705c121SKalle Valo 	 *
354e705c121SKalle Valo 	 *    [...] a STA responding to a received frame shall transmit
355e705c121SKalle Valo 	 *    its Control Response frame [...] at the highest rate in the
356e705c121SKalle Valo 	 *    BSSBasicRateSet parameter that is less than or equal to the
357e705c121SKalle Valo 	 *    rate of the immediately previous frame in the frame exchange
358e705c121SKalle Valo 	 *    sequence ([...]) and that is of the same modulation class
359e705c121SKalle Valo 	 *    ([...]) as the received frame. If no rate contained in the
360e705c121SKalle Valo 	 *    BSSBasicRateSet parameter meets these conditions, then the
361e705c121SKalle Valo 	 *    control frame sent in response to a received frame shall be
362e705c121SKalle Valo 	 *    transmitted at the highest mandatory rate of the PHY that is
363e705c121SKalle Valo 	 *    less than or equal to the rate of the received frame, and
364e705c121SKalle Valo 	 *    that is of the same modulation class as the received frame.
365e705c121SKalle Valo 	 *
366e705c121SKalle Valo 	 * As a consequence, we need to add all mandatory rates that are
367e705c121SKalle Valo 	 * lower than all of the basic rates to these bitmaps.
368e705c121SKalle Valo 	 */
369e705c121SKalle Valo 
370e705c121SKalle Valo 	if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
371e705c121SKalle Valo 		ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
372e705c121SKalle Valo 	if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
373e705c121SKalle Valo 		ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
374e705c121SKalle Valo 	/* 6M already there or needed so always add */
375e705c121SKalle Valo 	ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
376e705c121SKalle Valo 
377e705c121SKalle Valo 	/*
378e705c121SKalle Valo 	 * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
379e705c121SKalle Valo 	 * Note, however:
380e705c121SKalle Valo 	 *  - if no CCK rates are basic, it must be ERP since there must
381e705c121SKalle Valo 	 *    be some basic rates at all, so they're OFDM => ERP PHY
382e705c121SKalle Valo 	 *    (or we're in 5 GHz, and the cck bitmap will never be used)
383e705c121SKalle Valo 	 *  - if 11M is a basic rate, it must be ERP as well, so add 5.5M
384e705c121SKalle Valo 	 *  - if 5.5M is basic, 1M and 2M are mandatory
385e705c121SKalle Valo 	 *  - if 2M is basic, 1M is mandatory
386e705c121SKalle Valo 	 *  - if 1M is basic, that's the only valid ACK rate.
387e705c121SKalle Valo 	 * As a consequence, it's not as complicated as it sounds, just add
388e705c121SKalle Valo 	 * any lower rates to the ACK rate bitmap.
389e705c121SKalle Valo 	 */
390e705c121SKalle Valo 	if (IWL_RATE_11M_INDEX < lowest_present_cck)
391e705c121SKalle Valo 		cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
392e705c121SKalle Valo 	if (IWL_RATE_5M_INDEX < lowest_present_cck)
393e705c121SKalle Valo 		cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
394e705c121SKalle Valo 	if (IWL_RATE_2M_INDEX < lowest_present_cck)
395e705c121SKalle Valo 		cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
396e705c121SKalle Valo 	/* 1M already there or needed so always add */
397e705c121SKalle Valo 	cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
398e705c121SKalle Valo 
399e705c121SKalle Valo 	*cck_rates = cck;
400e705c121SKalle Valo 	*ofdm_rates = ofdm;
401e705c121SKalle Valo }
402e705c121SKalle Valo 
40355eb1c5fSMiri Korenblit void iwl_mvm_set_fw_basic_rates(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
404cacc1d42SGregory Greenman 				struct ieee80211_bss_conf *link_conf,
405af6d168fSMiri Korenblit 				__le32 *cck_rates, __le32 *ofdm_rates)
406af6d168fSMiri Korenblit {
407af6d168fSMiri Korenblit 	struct ieee80211_chanctx_conf *chanctx;
408cacc1d42SGregory Greenman 	u8 cck_ack_rates = 0, ofdm_ack_rates = 0;
409af6d168fSMiri Korenblit 
410af6d168fSMiri Korenblit 	rcu_read_lock();
411cacc1d42SGregory Greenman 	chanctx = rcu_dereference(link_conf->chanctx_conf);
412af6d168fSMiri Korenblit 	iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
413af6d168fSMiri Korenblit 					    : NL80211_BAND_2GHZ,
414af6d168fSMiri Korenblit 			  &cck_ack_rates, &ofdm_ack_rates);
415cacc1d42SGregory Greenman 
416af6d168fSMiri Korenblit 	rcu_read_unlock();
417af6d168fSMiri Korenblit 
418af6d168fSMiri Korenblit 	*cck_rates = cpu_to_le32((u32)cck_ack_rates);
419af6d168fSMiri Korenblit 	*ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
420af6d168fSMiri Korenblit }
421af6d168fSMiri Korenblit 
42255eb1c5fSMiri Korenblit void iwl_mvm_set_fw_protection_flags(struct iwl_mvm *mvm,
423af6d168fSMiri Korenblit 				     struct ieee80211_vif *vif,
424cacc1d42SGregory Greenman 				     struct ieee80211_bss_conf *link_conf,
42555eb1c5fSMiri Korenblit 				     __le32 *protection_flags, u32 ht_flag,
426af6d168fSMiri Korenblit 				     u32 tgg_flag)
427e705c121SKalle Valo {
428e705c121SKalle Valo 	/* for both sta and ap, ht_operation_mode hold the protection_mode */
429cacc1d42SGregory Greenman 	u8 protection_mode = link_conf->ht_operation_mode &
430e705c121SKalle Valo 				 IEEE80211_HT_OP_MODE_PROTECTION;
431cacc1d42SGregory Greenman 	bool ht_enabled = !!(link_conf->ht_operation_mode &
432af6d168fSMiri Korenblit 			     IEEE80211_HT_OP_MODE_PROTECTION);
433af6d168fSMiri Korenblit 
434cacc1d42SGregory Greenman 	if (link_conf->use_cts_prot)
435af6d168fSMiri Korenblit 		*protection_flags |= cpu_to_le32(tgg_flag);
436af6d168fSMiri Korenblit 
437af6d168fSMiri Korenblit 	IWL_DEBUG_RATE(mvm, "use_cts_prot %d, ht_operation_mode %d\n",
438cacc1d42SGregory Greenman 		       link_conf->use_cts_prot,
439cacc1d42SGregory Greenman 		       link_conf->ht_operation_mode);
440af6d168fSMiri Korenblit 
441af6d168fSMiri Korenblit 	if (!ht_enabled)
442af6d168fSMiri Korenblit 		return;
443e705c121SKalle Valo 
444e705c121SKalle Valo 	IWL_DEBUG_RATE(mvm, "protection mode set to %d\n", protection_mode);
445e705c121SKalle Valo 	/*
446e705c121SKalle Valo 	 * See section 9.23.3.1 of IEEE 80211-2012.
447e705c121SKalle Valo 	 * Nongreenfield HT STAs Present is not supported.
448e705c121SKalle Valo 	 */
449e705c121SKalle Valo 	switch (protection_mode) {
450e705c121SKalle Valo 	case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
451e705c121SKalle Valo 		break;
452e705c121SKalle Valo 	case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
453e705c121SKalle Valo 	case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
454af6d168fSMiri Korenblit 		*protection_flags |= cpu_to_le32(ht_flag);
455e705c121SKalle Valo 		break;
456e705c121SKalle Valo 	case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
457e705c121SKalle Valo 		/* Protect when channel wider than 20MHz */
458cacc1d42SGregory Greenman 		if (link_conf->chandef.width > NL80211_CHAN_WIDTH_20)
459af6d168fSMiri Korenblit 			*protection_flags |= cpu_to_le32(ht_flag);
460e705c121SKalle Valo 		break;
461e705c121SKalle Valo 	default:
462e705c121SKalle Valo 		IWL_ERR(mvm, "Illegal protection mode %d\n",
463e705c121SKalle Valo 			protection_mode);
464e705c121SKalle Valo 		break;
465e705c121SKalle Valo 	}
466e705c121SKalle Valo }
467e705c121SKalle Valo 
46855eb1c5fSMiri Korenblit void iwl_mvm_set_fw_qos_params(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
469cacc1d42SGregory Greenman 			       struct ieee80211_bss_conf *link_conf,
470af6d168fSMiri Korenblit 			       struct iwl_ac_qos *ac, __le32 *qos_flags)
471af6d168fSMiri Korenblit {
472af6d168fSMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
47333acbe6aSJohannes Berg 	struct iwl_mvm_vif_link_info *mvm_link =
47433acbe6aSJohannes Berg 		mvmvif->link[link_conf->link_id];
475af6d168fSMiri Korenblit 	int i;
476af6d168fSMiri Korenblit 
47733acbe6aSJohannes Berg 	if (!mvm_link)
47833acbe6aSJohannes Berg 		return;
47933acbe6aSJohannes Berg 
480af6d168fSMiri Korenblit 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
481af6d168fSMiri Korenblit 		u8 txf = iwl_mvm_mac_ac_to_tx_fifo(mvm, i);
482af6d168fSMiri Korenblit 		u8 ucode_ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);
483af6d168fSMiri Korenblit 
484af6d168fSMiri Korenblit 		ac[ucode_ac].cw_min =
48533acbe6aSJohannes Berg 			cpu_to_le16(mvm_link->queue_params[i].cw_min);
486af6d168fSMiri Korenblit 		ac[ucode_ac].cw_max =
48733acbe6aSJohannes Berg 			cpu_to_le16(mvm_link->queue_params[i].cw_max);
488af6d168fSMiri Korenblit 		ac[ucode_ac].edca_txop =
48933acbe6aSJohannes Berg 			cpu_to_le16(mvm_link->queue_params[i].txop * 32);
49033acbe6aSJohannes Berg 		ac[ucode_ac].aifsn = mvm_link->queue_params[i].aifs;
491af6d168fSMiri Korenblit 		ac[ucode_ac].fifos_mask = BIT(txf);
492af6d168fSMiri Korenblit 	}
493af6d168fSMiri Korenblit 
494cacc1d42SGregory Greenman 	if (link_conf->qos)
495af6d168fSMiri Korenblit 		*qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
496af6d168fSMiri Korenblit 
497cacc1d42SGregory Greenman 	if (link_conf->chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
498af6d168fSMiri Korenblit 		*qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
499af6d168fSMiri Korenblit }
500af6d168fSMiri Korenblit 
5019be162a7SMiri Korenblit int iwl_mvm_get_mac_type(struct ieee80211_vif *vif)
502834f920eSMukesh Sisodiya {
503834f920eSMukesh Sisodiya 	u32 mac_type = FW_MAC_TYPE_BSS_STA;
504834f920eSMukesh Sisodiya 
505834f920eSMukesh Sisodiya 	switch (vif->type) {
506834f920eSMukesh Sisodiya 	case NL80211_IFTYPE_STATION:
507834f920eSMukesh Sisodiya 		if (vif->p2p)
508834f920eSMukesh Sisodiya 			mac_type = FW_MAC_TYPE_P2P_STA;
509834f920eSMukesh Sisodiya 		else
510834f920eSMukesh Sisodiya 			mac_type = FW_MAC_TYPE_BSS_STA;
511834f920eSMukesh Sisodiya 		break;
512834f920eSMukesh Sisodiya 	case NL80211_IFTYPE_AP:
513834f920eSMukesh Sisodiya 		mac_type = FW_MAC_TYPE_GO;
514834f920eSMukesh Sisodiya 		break;
515834f920eSMukesh Sisodiya 	case NL80211_IFTYPE_MONITOR:
516834f920eSMukesh Sisodiya 		mac_type = FW_MAC_TYPE_LISTENER;
517834f920eSMukesh Sisodiya 		break;
518834f920eSMukesh Sisodiya 	case NL80211_IFTYPE_P2P_DEVICE:
519834f920eSMukesh Sisodiya 		mac_type = FW_MAC_TYPE_P2P_DEVICE;
520834f920eSMukesh Sisodiya 		break;
521834f920eSMukesh Sisodiya 	case NL80211_IFTYPE_ADHOC:
522834f920eSMukesh Sisodiya 		mac_type = FW_MAC_TYPE_IBSS;
523834f920eSMukesh Sisodiya 		break;
524834f920eSMukesh Sisodiya 	default:
525834f920eSMukesh Sisodiya 		WARN_ON_ONCE(1);
526834f920eSMukesh Sisodiya 	}
527834f920eSMukesh Sisodiya 	return mac_type;
528834f920eSMukesh Sisodiya }
529834f920eSMukesh Sisodiya 
530e705c121SKalle Valo static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
531e705c121SKalle Valo 					struct ieee80211_vif *vif,
532e705c121SKalle Valo 					struct iwl_mac_ctx_cmd *cmd,
533e705c121SKalle Valo 					const u8 *bssid_override,
534e705c121SKalle Valo 					u32 action)
535e705c121SKalle Valo {
536e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
537e705c121SKalle Valo 	const u8 *bssid = bssid_override ?: vif->bss_conf.bssid;
538af6d168fSMiri Korenblit 	u32 ht_flag;
539e705c121SKalle Valo 
540e705c121SKalle Valo 	cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
541e705c121SKalle Valo 							    mvmvif->color));
542e705c121SKalle Valo 	cmd->action = cpu_to_le32(action);
543834f920eSMukesh Sisodiya 	cmd->mac_type = cpu_to_le32(iwl_mvm_get_mac_type(vif));
544e705c121SKalle Valo 
545e705c121SKalle Valo 	cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
546e705c121SKalle Valo 
547e705c121SKalle Valo 	memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
548e705c121SKalle Valo 
549e705c121SKalle Valo 	if (bssid)
550e705c121SKalle Valo 		memcpy(cmd->bssid_addr, bssid, ETH_ALEN);
551e705c121SKalle Valo 	else
552e705c121SKalle Valo 		eth_broadcast_addr(cmd->bssid_addr);
553e705c121SKalle Valo 
554cacc1d42SGregory Greenman 	iwl_mvm_set_fw_basic_rates(mvm, vif, &vif->bss_conf, &cmd->cck_rates,
555af6d168fSMiri Korenblit 				   &cmd->ofdm_rates);
556e705c121SKalle Valo 
557e705c121SKalle Valo 	cmd->cck_short_preamble =
558e705c121SKalle Valo 		cpu_to_le32(vif->bss_conf.use_short_preamble ?
559e705c121SKalle Valo 			    MAC_FLG_SHORT_PREAMBLE : 0);
560e705c121SKalle Valo 	cmd->short_slot =
561e705c121SKalle Valo 		cpu_to_le32(vif->bss_conf.use_short_slot ?
562e705c121SKalle Valo 			    MAC_FLG_SHORT_SLOT : 0);
563e705c121SKalle Valo 
56450f56044SIlan Peer 	cmd->filter_flags = 0;
565bd6f5bd7SAyala Beker 
56677e1f3f3SJohannes Berg 	iwl_mvm_set_fw_qos_params(mvm, vif, &vif->bss_conf, cmd->ac,
567cacc1d42SGregory Greenman 				  &cmd->qos_flags);
568e705c121SKalle Valo 
569af6d168fSMiri Korenblit 	/* The fw does not distinguish between ht and fat */
570af6d168fSMiri Korenblit 	ht_flag = MAC_PROT_FLG_HT_PROT | MAC_PROT_FLG_FAT_PROT;
571cacc1d42SGregory Greenman 	iwl_mvm_set_fw_protection_flags(mvm, vif, &vif->bss_conf,
572cacc1d42SGregory Greenman 					&cmd->protection_flags,
573af6d168fSMiri Korenblit 					ht_flag, MAC_PROT_FLG_TGG_PROTECT);
574e705c121SKalle Valo }
575e705c121SKalle Valo 
576e705c121SKalle Valo static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
577e705c121SKalle Valo 				     struct iwl_mac_ctx_cmd *cmd)
578e705c121SKalle Valo {
579e705c121SKalle Valo 	int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
580e705c121SKalle Valo 				       sizeof(*cmd), cmd);
581e705c121SKalle Valo 	if (ret)
582af6d168fSMiri Korenblit 		IWL_ERR(mvm, "Failed to send MAC_CONTEXT_CMD (action:%d): %d\n",
583e705c121SKalle Valo 			le32_to_cpu(cmd->action), ret);
584e705c121SKalle Valo 	return ret;
585e705c121SKalle Valo }
586e705c121SKalle Valo 
58755eb1c5fSMiri Korenblit void iwl_mvm_set_fw_dtim_tbtt(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
588cacc1d42SGregory Greenman 			      struct ieee80211_bss_conf *link_conf,
589af6d168fSMiri Korenblit 			      __le64 *dtim_tsf, __le32 *dtim_time,
590af6d168fSMiri Korenblit 			      __le32 *assoc_beacon_arrive_time)
591e705c121SKalle Valo {
592e705c121SKalle Valo 	u32 dtim_offs;
593e705c121SKalle Valo 
594e705c121SKalle Valo 	/*
595e705c121SKalle Valo 	 * The DTIM count counts down, so when it is N that means N
596e705c121SKalle Valo 	 * more beacon intervals happen until the DTIM TBTT. Therefore
597e705c121SKalle Valo 	 * add this to the current time. If that ends up being in the
598e705c121SKalle Valo 	 * future, the firmware will handle it.
599e705c121SKalle Valo 	 *
600e705c121SKalle Valo 	 * Also note that the system_timestamp (which we get here as
601e705c121SKalle Valo 	 * "sync_device_ts") and TSF timestamp aren't at exactly the
602e705c121SKalle Valo 	 * same offset in the frame -- the TSF is at the first symbol
603e705c121SKalle Valo 	 * of the TSF, the system timestamp is at signal acquisition
604e705c121SKalle Valo 	 * time. This means there's an offset between them of at most
605e705c121SKalle Valo 	 * a few hundred microseconds (24 * 8 bits + PLCP time gives
606e705c121SKalle Valo 	 * 384us in the longest case), this is currently not relevant
607e705c121SKalle Valo 	 * as the firmware wakes up around 2ms before the TBTT.
608e705c121SKalle Valo 	 */
609cacc1d42SGregory Greenman 	dtim_offs = link_conf->sync_dtim_count *
610cacc1d42SGregory Greenman 			link_conf->beacon_int;
611e705c121SKalle Valo 	/* convert TU to usecs */
612e705c121SKalle Valo 	dtim_offs *= 1024;
613e705c121SKalle Valo 
614af6d168fSMiri Korenblit 	*dtim_tsf =
615cacc1d42SGregory Greenman 		cpu_to_le64(link_conf->sync_tsf + dtim_offs);
616af6d168fSMiri Korenblit 	*dtim_time =
617cacc1d42SGregory Greenman 		cpu_to_le32(link_conf->sync_device_ts + dtim_offs);
618af6d168fSMiri Korenblit 	*assoc_beacon_arrive_time =
619cacc1d42SGregory Greenman 		cpu_to_le32(link_conf->sync_device_ts);
620e705c121SKalle Valo 
621e705c121SKalle Valo 	IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
622af6d168fSMiri Korenblit 		       le64_to_cpu(*dtim_tsf),
623af6d168fSMiri Korenblit 		       le32_to_cpu(*dtim_time),
624e705c121SKalle Valo 		       dtim_offs);
625af6d168fSMiri Korenblit }
626af6d168fSMiri Korenblit 
6279be162a7SMiri Korenblit __le32 iwl_mvm_mac_ctxt_cmd_p2p_sta_get_oppps_ctwin(struct iwl_mvm *mvm,
628af6d168fSMiri Korenblit 						    struct ieee80211_vif *vif)
629af6d168fSMiri Korenblit {
630af6d168fSMiri Korenblit 	struct ieee80211_p2p_noa_attr *noa =
631af6d168fSMiri Korenblit 		&vif->bss_conf.p2p_noa_attr;
632af6d168fSMiri Korenblit 
633af6d168fSMiri Korenblit 	return cpu_to_le32(noa->oppps_ctwindow &
634af6d168fSMiri Korenblit 			IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
635af6d168fSMiri Korenblit }
636af6d168fSMiri Korenblit 
6373f302269SEmmanuel Grumbach u32 iwl_mvm_mac_ctxt_cmd_sta_get_twt_policy(struct iwl_mvm *mvm,
638af6d168fSMiri Korenblit 					    struct ieee80211_vif *vif)
639af6d168fSMiri Korenblit {
6403f302269SEmmanuel Grumbach 	u32 twt_policy = 0;
641af6d168fSMiri Korenblit 
642af6d168fSMiri Korenblit 	if (vif->bss_conf.twt_requester && IWL_MVM_USE_TWT)
6433f302269SEmmanuel Grumbach 		twt_policy |= TWT_SUPPORTED;
644af6d168fSMiri Korenblit 	if (vif->bss_conf.twt_protected)
6453f302269SEmmanuel Grumbach 		twt_policy |= PROTECTED_TWT_SUPPORTED;
646af6d168fSMiri Korenblit 	if (vif->bss_conf.twt_broadcast)
6473f302269SEmmanuel Grumbach 		twt_policy |= BROADCAST_TWT_SUPPORTED;
648af6d168fSMiri Korenblit 
649af6d168fSMiri Korenblit 	return twt_policy;
650af6d168fSMiri Korenblit }
651af6d168fSMiri Korenblit 
652af6d168fSMiri Korenblit static int iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm *mvm,
653af6d168fSMiri Korenblit 				    struct ieee80211_vif *vif,
654af6d168fSMiri Korenblit 				    u32 action, bool force_assoc_off,
655af6d168fSMiri Korenblit 				    const u8 *bssid_override)
656af6d168fSMiri Korenblit {
657af6d168fSMiri Korenblit 	struct iwl_mac_ctx_cmd cmd = {};
658af6d168fSMiri Korenblit 	struct iwl_mac_data_sta *ctxt_sta;
659af6d168fSMiri Korenblit 
660af6d168fSMiri Korenblit 	WARN_ON(vif->type != NL80211_IFTYPE_STATION);
661af6d168fSMiri Korenblit 
662af6d168fSMiri Korenblit 	/* Fill the common data for all mac context types */
663af6d168fSMiri Korenblit 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, bssid_override, action);
664af6d168fSMiri Korenblit 
665af6d168fSMiri Korenblit 	/*
666af6d168fSMiri Korenblit 	 * We always want to hear MCAST frames, if we're not authorized yet,
667af6d168fSMiri Korenblit 	 * we'll drop them.
668af6d168fSMiri Korenblit 	 */
669af6d168fSMiri Korenblit 	cmd.filter_flags |= cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
670af6d168fSMiri Korenblit 
671af6d168fSMiri Korenblit 	if (vif->p2p) {
672af6d168fSMiri Korenblit 		cmd.p2p_sta.ctwin =
673af6d168fSMiri Korenblit 			iwl_mvm_mac_ctxt_cmd_p2p_sta_get_oppps_ctwin(mvm, vif);
674af6d168fSMiri Korenblit 
675af6d168fSMiri Korenblit 		ctxt_sta = &cmd.p2p_sta.sta;
676af6d168fSMiri Korenblit 	} else {
677af6d168fSMiri Korenblit 		ctxt_sta = &cmd.sta;
678af6d168fSMiri Korenblit 	}
679af6d168fSMiri Korenblit 
680af6d168fSMiri Korenblit 	/* We need the dtim_period to set the MAC as associated */
681af6d168fSMiri Korenblit 	if (vif->cfg.assoc && vif->bss_conf.dtim_period &&
682af6d168fSMiri Korenblit 	    !force_assoc_off) {
683af6d168fSMiri Korenblit 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
684af6d168fSMiri Korenblit 
685cacc1d42SGregory Greenman 		iwl_mvm_set_fw_dtim_tbtt(mvm, vif, &vif->bss_conf,
686cacc1d42SGregory Greenman 					 &ctxt_sta->dtim_tsf,
687af6d168fSMiri Korenblit 					 &ctxt_sta->dtim_time,
688af6d168fSMiri Korenblit 					 &ctxt_sta->assoc_beacon_arrive_time);
689e705c121SKalle Valo 
690e705c121SKalle Valo 		ctxt_sta->is_assoc = cpu_to_le32(1);
69150f56044SIlan Peer 
6928b75858cSJohannes Berg 		if (!mvmvif->authorized &&
6938b75858cSJohannes Berg 		    fw_has_capa(&mvm->fw->ucode_capa,
6948b75858cSJohannes Berg 				IWL_UCODE_TLV_CAPA_COEX_HIGH_PRIO))
6958b75858cSJohannes Berg 			ctxt_sta->data_policy |=
6968b75858cSJohannes Berg 				cpu_to_le32(COEX_HIGH_PRIORITY_ENABLE);
697e705c121SKalle Valo 	} else {
698e705c121SKalle Valo 		ctxt_sta->is_assoc = cpu_to_le32(0);
699e705c121SKalle Valo 
700e705c121SKalle Valo 		/* Allow beacons to pass through as long as we are not
701e705c121SKalle Valo 		 * associated, or we do not have dtim period information.
702e705c121SKalle Valo 		 */
703e705c121SKalle Valo 		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
704e705c121SKalle Valo 	}
705e705c121SKalle Valo 
706e705c121SKalle Valo 	ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
707e705c121SKalle Valo 	ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
708e705c121SKalle Valo 					      vif->bss_conf.dtim_period);
709e705c121SKalle Valo 
710e705c121SKalle Valo 	ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
711f276e20bSJohannes Berg 	ctxt_sta->assoc_id = cpu_to_le32(vif->cfg.aid);
712e705c121SKalle Valo 
713f276e20bSJohannes Berg 	if (vif->probe_req_reg && vif->cfg.assoc && vif->p2p)
714e705c121SKalle Valo 		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
715e705c121SKalle Valo 
7169394662aSLiad Kaufman 	if (vif->bss_conf.he_support && !iwlwifi_mod_params.disable_11ax) {
717514c3069SLuca Coelho 		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_11AX);
718659ac93dSShaul Triebitz 		ctxt_sta->data_policy |=
7193f302269SEmmanuel Grumbach 			cpu_to_le32(iwl_mvm_mac_ctxt_cmd_sta_get_twt_policy(mvm, vif));
720659ac93dSShaul Triebitz 	}
721d91d9b94SEmmanuel Grumbach 
722514c3069SLuca Coelho 
723e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
724e705c121SKalle Valo }
725e705c121SKalle Valo 
726e705c121SKalle Valo static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
727e705c121SKalle Valo 					 struct ieee80211_vif *vif,
728e705c121SKalle Valo 					 u32 action)
729e705c121SKalle Valo {
730e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd = {};
731b85f7ebbSJohannes Berg 	u32 tfd_queue_msk = 0;
732c8f54701SJohannes Berg 	int ret;
733e705c121SKalle Valo 
734e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
735e705c121SKalle Valo 
736e705c121SKalle Valo 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
737e705c121SKalle Valo 
738e705c121SKalle Valo 	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
739e705c121SKalle Valo 				       MAC_FILTER_IN_CONTROL_AND_MGMT |
740e705c121SKalle Valo 				       MAC_FILTER_IN_BEACON |
741e705c121SKalle Valo 				       MAC_FILTER_IN_PROBE_REQUEST |
74250f56044SIlan Peer 				       MAC_FILTER_IN_CRC32 |
74350f56044SIlan Peer 				       MAC_FILTER_ACCEPT_GRP);
744e705c121SKalle Valo 	ieee80211_hw_set(mvm->hw, RX_INCLUDES_FCS);
745e705c121SKalle Valo 
746b85f7ebbSJohannes Berg 	/*
747b85f7ebbSJohannes Berg 	 * the queue mask is only relevant for old TX API, and
748b85f7ebbSJohannes Berg 	 * mvm->snif_queue isn't set here (it's still set to
749b85f7ebbSJohannes Berg 	 * IWL_MVM_INVALID_QUEUE so the BIT() of it is UB)
750b85f7ebbSJohannes Berg 	 */
751b85f7ebbSJohannes Berg 	if (!iwl_mvm_has_new_tx_api(mvm))
752b85f7ebbSJohannes Berg 		tfd_queue_msk = BIT(mvm->snif_queue);
753b85f7ebbSJohannes Berg 
7540e39eb03SChaya Rachel Ivgi 	/* Allocate sniffer station */
7550e39eb03SChaya Rachel Ivgi 	ret = iwl_mvm_allocate_int_sta(mvm, &mvm->snif_sta, tfd_queue_msk,
756ced19f26SSara Sharon 				       vif->type, IWL_STA_GENERAL_PURPOSE);
7570e39eb03SChaya Rachel Ivgi 	if (ret)
7580e39eb03SChaya Rachel Ivgi 		return ret;
7590e39eb03SChaya Rachel Ivgi 
760e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
761e705c121SKalle Valo }
762e705c121SKalle Valo 
763e705c121SKalle Valo static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
764e705c121SKalle Valo 				     struct ieee80211_vif *vif,
765e705c121SKalle Valo 				     u32 action)
766e705c121SKalle Valo {
767e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
768e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd = {};
769e705c121SKalle Valo 
770e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
771e705c121SKalle Valo 
772e705c121SKalle Valo 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
773e705c121SKalle Valo 
774e705c121SKalle Valo 	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
77550f56044SIlan Peer 				       MAC_FILTER_IN_PROBE_REQUEST |
77650f56044SIlan Peer 				       MAC_FILTER_ACCEPT_GRP);
777e705c121SKalle Valo 
778e705c121SKalle Valo 	/* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */
779e705c121SKalle Valo 	cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
780e705c121SKalle Valo 
781e705c121SKalle Valo 	/* TODO: Assumes that the beacon id == mac context id */
782e705c121SKalle Valo 	cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
783e705c121SKalle Valo 
784e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
785e705c121SKalle Valo }
786e705c121SKalle Valo 
787e705c121SKalle Valo struct iwl_mvm_go_iterator_data {
788e705c121SKalle Valo 	bool go_active;
789e705c121SKalle Valo };
790e705c121SKalle Valo 
791e705c121SKalle Valo static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
792e705c121SKalle Valo {
793e705c121SKalle Valo 	struct iwl_mvm_go_iterator_data *data = _data;
794e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
795e705c121SKalle Valo 
796e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
797e705c121SKalle Valo 	    mvmvif->ap_ibss_active)
798e705c121SKalle Valo 		data->go_active = true;
799e705c121SKalle Valo }
800e705c121SKalle Valo 
8019be162a7SMiri Korenblit __le32 iwl_mac_ctxt_p2p_dev_has_extended_disc(struct iwl_mvm *mvm,
802af6d168fSMiri Korenblit 					      struct ieee80211_vif *vif)
803e705c121SKalle Valo {
804e705c121SKalle Valo 	struct iwl_mvm_go_iterator_data data = {};
805e705c121SKalle Valo 
806e705c121SKalle Valo 	/*
807e705c121SKalle Valo 	 * This flag should be set to true when the P2P Device is
808e705c121SKalle Valo 	 * discoverable and there is at least another active P2P GO. Settings
809e705c121SKalle Valo 	 * this flag will allow the P2P Device to be discoverable on other
810e705c121SKalle Valo 	 * channels in addition to its listen channel.
811e705c121SKalle Valo 	 * Note that this flag should not be set in other cases as it opens the
812e705c121SKalle Valo 	 * Rx filters on all MAC and increases the number of interrupts.
813e705c121SKalle Valo 	 */
814e705c121SKalle Valo 	ieee80211_iterate_active_interfaces_atomic(
815e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
816e705c121SKalle Valo 		iwl_mvm_go_iterator, &data);
817e705c121SKalle Valo 
818af6d168fSMiri Korenblit 	return cpu_to_le32(data.go_active ? 1 : 0);
819af6d168fSMiri Korenblit }
820af6d168fSMiri Korenblit 
821af6d168fSMiri Korenblit static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
822af6d168fSMiri Korenblit 					   struct ieee80211_vif *vif,
823af6d168fSMiri Korenblit 					   u32 action)
824af6d168fSMiri Korenblit {
825af6d168fSMiri Korenblit 	struct iwl_mac_ctx_cmd cmd = {};
826af6d168fSMiri Korenblit 
827af6d168fSMiri Korenblit 	WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
828af6d168fSMiri Korenblit 
829af6d168fSMiri Korenblit 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
830af6d168fSMiri Korenblit 
831af6d168fSMiri Korenblit 	cmd.p2p_dev.is_disc_extended =
832af6d168fSMiri Korenblit 		iwl_mac_ctxt_p2p_dev_has_extended_disc(mvm, vif);
833af6d168fSMiri Korenblit 
834af6d168fSMiri Korenblit 	/* Override the filter flags to accept only probe requests */
835af6d168fSMiri Korenblit 	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
836af6d168fSMiri Korenblit 
837e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
838e705c121SKalle Valo }
839e705c121SKalle Valo 
840138664a3SSara Sharon void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
8418364fbb4SSara Sharon 			      __le32 *tim_index, __le32 *tim_size,
842e705c121SKalle Valo 			      u8 *beacon, u32 frame_size)
843e705c121SKalle Valo {
844e705c121SKalle Valo 	u32 tim_idx;
845e705c121SKalle Valo 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
846e705c121SKalle Valo 
847e705c121SKalle Valo 	/* The index is relative to frame start but we start looking at the
848e705c121SKalle Valo 	 * variable-length part of the beacon. */
849e705c121SKalle Valo 	tim_idx = mgmt->u.beacon.variable - beacon;
850e705c121SKalle Valo 
851e705c121SKalle Valo 	/* Parse variable-length elements of beacon to find WLAN_EID_TIM */
852e705c121SKalle Valo 	while ((tim_idx < (frame_size - 2)) &&
853e705c121SKalle Valo 			(beacon[tim_idx] != WLAN_EID_TIM))
854e705c121SKalle Valo 		tim_idx += beacon[tim_idx+1] + 2;
855e705c121SKalle Valo 
856e705c121SKalle Valo 	/* If TIM field was found, set variables */
857e705c121SKalle Valo 	if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
8588364fbb4SSara Sharon 		*tim_index = cpu_to_le32(tim_idx);
8598364fbb4SSara Sharon 		*tim_size = cpu_to_le32((u32)beacon[tim_idx + 1]);
860e705c121SKalle Valo 	} else {
861e705c121SKalle Valo 		IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
862e705c121SKalle Valo 	}
863e705c121SKalle Valo }
864e705c121SKalle Valo 
865d3a108a4SAndrei Otcheretianski static u32 iwl_mvm_find_ie_offset(u8 *beacon, u8 eid, u32 frame_size)
866d3a108a4SAndrei Otcheretianski {
867d3a108a4SAndrei Otcheretianski 	struct ieee80211_mgmt *mgmt = (void *)beacon;
868d3a108a4SAndrei Otcheretianski 	const u8 *ie;
869d3a108a4SAndrei Otcheretianski 
870d3a108a4SAndrei Otcheretianski 	if (WARN_ON_ONCE(frame_size <= (mgmt->u.beacon.variable - beacon)))
871d3a108a4SAndrei Otcheretianski 		return 0;
872d3a108a4SAndrei Otcheretianski 
873d3a108a4SAndrei Otcheretianski 	frame_size -= mgmt->u.beacon.variable - beacon;
874d3a108a4SAndrei Otcheretianski 
875d3a108a4SAndrei Otcheretianski 	ie = cfg80211_find_ie(eid, mgmt->u.beacon.variable, frame_size);
876d3a108a4SAndrei Otcheretianski 	if (!ie)
877d3a108a4SAndrei Otcheretianski 		return 0;
878d3a108a4SAndrei Otcheretianski 
879d3a108a4SAndrei Otcheretianski 	return ie - beacon;
880d3a108a4SAndrei Otcheretianski }
881d3a108a4SAndrei Otcheretianski 
8827f11d17fSIlan Peer u8 iwl_mvm_mac_ctxt_get_lowest_rate(struct iwl_mvm *mvm,
883ef2e7a51SIlan Peer 				    struct ieee80211_tx_info *info,
8846ca33f8bSHaim Dreyfuss 				    struct ieee80211_vif *vif)
8856ca33f8bSHaim Dreyfuss {
8860af637b5SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
887ef2e7a51SIlan Peer 	struct ieee80211_supported_band *sband;
888ef2e7a51SIlan Peer 	unsigned long basic = vif->bss_conf.basic_rates;
889ef2e7a51SIlan Peer 	u16 lowest_cck = IWL_RATE_COUNT, lowest_ofdm = IWL_RATE_COUNT;
8900af637b5SJohannes Berg 	u32 link_id = u32_get_bits(info->control.flags,
8910af637b5SJohannes Berg 				   IEEE80211_TX_CTRL_MLO_LINK);
8920af637b5SJohannes Berg 	u8 band = info->band;
8936ca33f8bSHaim Dreyfuss 	u8 rate;
894ef2e7a51SIlan Peer 	u32 i;
895ef2e7a51SIlan Peer 
896*98d8a003SIlan Peer 	if (link_id == IEEE80211_LINK_UNSPECIFIED && ieee80211_vif_is_mld(vif)) {
8970af637b5SJohannes Berg 		for (i = 0; i < ARRAY_SIZE(mvmvif->link); i++) {
8980af637b5SJohannes Berg 			if (!mvmvif->link[i])
8990af637b5SJohannes Berg 				continue;
9000af637b5SJohannes Berg 			/* shouldn't do this when >1 link is active */
9010af637b5SJohannes Berg 			WARN_ON_ONCE(link_id != IEEE80211_LINK_UNSPECIFIED);
9020af637b5SJohannes Berg 			link_id = i;
9030af637b5SJohannes Berg 		}
9040af637b5SJohannes Berg 	}
9050af637b5SJohannes Berg 
9060af637b5SJohannes Berg 	if (link_id < IEEE80211_LINK_UNSPECIFIED) {
9070af637b5SJohannes Berg 		struct ieee80211_bss_conf *link_conf;
9080af637b5SJohannes Berg 
9090af637b5SJohannes Berg 		rcu_read_lock();
9100af637b5SJohannes Berg 		link_conf = rcu_dereference(vif->link_conf[link_id]);
9110af637b5SJohannes Berg 		if (link_conf) {
9120af637b5SJohannes Berg 			basic = link_conf->basic_rates;
9130af637b5SJohannes Berg 			if (link_conf->chandef.chan)
9140af637b5SJohannes Berg 				band = link_conf->chandef.chan->band;
9150af637b5SJohannes Berg 		}
9160af637b5SJohannes Berg 		rcu_read_unlock();
9170af637b5SJohannes Berg 	}
9180af637b5SJohannes Berg 
9190af637b5SJohannes Berg 	sband = mvm->hw->wiphy->bands[band];
920ef2e7a51SIlan Peer 	for_each_set_bit(i, &basic, BITS_PER_LONG) {
921ef2e7a51SIlan Peer 		u16 hw = sband->bitrates[i].hw_value;
922ef2e7a51SIlan Peer 
923ef2e7a51SIlan Peer 		if (hw >= IWL_FIRST_OFDM_RATE) {
924ef2e7a51SIlan Peer 			if (lowest_ofdm > hw)
925ef2e7a51SIlan Peer 				lowest_ofdm = hw;
926ef2e7a51SIlan Peer 		} else if (lowest_cck > hw) {
927ef2e7a51SIlan Peer 			lowest_cck = hw;
928ef2e7a51SIlan Peer 		}
929ef2e7a51SIlan Peer 	}
930ef2e7a51SIlan Peer 
9310af637b5SJohannes Berg 	if (band == NL80211_BAND_2GHZ && !vif->p2p &&
9327f11d17fSIlan Peer 	    vif->type != NL80211_IFTYPE_P2P_DEVICE &&
9337f11d17fSIlan Peer 	    !(info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)) {
934ef2e7a51SIlan Peer 		if (lowest_cck != IWL_RATE_COUNT)
935ef2e7a51SIlan Peer 			rate = lowest_cck;
936ef2e7a51SIlan Peer 		else if (lowest_ofdm != IWL_RATE_COUNT)
937ef2e7a51SIlan Peer 			rate = lowest_ofdm;
938d558b7f8STova Mussai 		else
939ef2e7a51SIlan Peer 			rate = IWL_RATE_1M_INDEX;
940ef2e7a51SIlan Peer 	} else if (lowest_ofdm != IWL_RATE_COUNT) {
941ef2e7a51SIlan Peer 		rate = lowest_ofdm;
942ef2e7a51SIlan Peer 	} else {
943ef2e7a51SIlan Peer 		rate = IWL_RATE_6M_INDEX;
944ef2e7a51SIlan Peer 	}
9456ca33f8bSHaim Dreyfuss 
9466ca33f8bSHaim Dreyfuss 	return rate;
9476ca33f8bSHaim Dreyfuss }
9486ca33f8bSHaim Dreyfuss 
949cd2c46a7SMiri Korenblit u16 iwl_mvm_mac_ctxt_get_beacon_flags(const struct iwl_fw *fw, u8 rate_idx)
950cd2c46a7SMiri Korenblit {
951cd2c46a7SMiri Korenblit 	u16 flags = iwl_mvm_mac80211_idx_to_hwrate(fw, rate_idx);
952971cbe50SJohannes Berg 	bool is_new_rate = iwl_fw_lookup_cmd_ver(fw, BEACON_TEMPLATE_CMD, 0) > 10;
953cd2c46a7SMiri Korenblit 
954cd2c46a7SMiri Korenblit 	if (rate_idx <= IWL_FIRST_CCK_RATE)
955cd2c46a7SMiri Korenblit 		flags |= is_new_rate ? IWL_MAC_BEACON_CCK
956cd2c46a7SMiri Korenblit 			  : IWL_MAC_BEACON_CCK_V1;
957cd2c46a7SMiri Korenblit 
958cd2c46a7SMiri Korenblit 	return flags;
959cd2c46a7SMiri Korenblit }
960cd2c46a7SMiri Korenblit 
961ef2e7a51SIlan Peer u8 iwl_mvm_mac_ctxt_get_beacon_rate(struct iwl_mvm *mvm,
962ef2e7a51SIlan Peer 				    struct ieee80211_tx_info *info,
963ef2e7a51SIlan Peer 				    struct ieee80211_vif *vif)
964ef2e7a51SIlan Peer {
965ef2e7a51SIlan Peer 	struct ieee80211_supported_band *sband =
966ef2e7a51SIlan Peer 		mvm->hw->wiphy->bands[info->band];
967ef2e7a51SIlan Peer 	u32 legacy = vif->bss_conf.beacon_tx_rate.control[info->band].legacy;
968ef2e7a51SIlan Peer 
969ef2e7a51SIlan Peer 	/* if beacon rate was configured try using it */
970ef2e7a51SIlan Peer 	if (hweight32(legacy) == 1) {
971ef2e7a51SIlan Peer 		u32 rate = ffs(legacy) - 1;
972ef2e7a51SIlan Peer 
973ef2e7a51SIlan Peer 		return sband->bitrates[rate].hw_value;
974ef2e7a51SIlan Peer 	}
975ef2e7a51SIlan Peer 
976ef2e7a51SIlan Peer 	return iwl_mvm_mac_ctxt_get_lowest_rate(mvm, info, vif);
977ef2e7a51SIlan Peer }
978ef2e7a51SIlan Peer 
97909856582SLuca Coelho static void iwl_mvm_mac_ctxt_set_tx(struct iwl_mvm *mvm,
980e705c121SKalle Valo 				    struct ieee80211_vif *vif,
98109856582SLuca Coelho 				    struct sk_buff *beacon,
98209856582SLuca Coelho 				    struct iwl_tx_cmd *tx)
983e705c121SKalle Valo {
984e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
985e705c121SKalle Valo 	struct ieee80211_tx_info *info;
9866ca33f8bSHaim Dreyfuss 	u8 rate;
9876ca33f8bSHaim Dreyfuss 	u32 tx_flags;
988e705c121SKalle Valo 
989e705c121SKalle Valo 	info = IEEE80211_SKB_CB(beacon);
990e705c121SKalle Valo 
991e705c121SKalle Valo 	/* Set up TX command fields */
99209856582SLuca Coelho 	tx->len = cpu_to_le16((u16)beacon->len);
993650cadb7SGregory Greenman 	tx->sta_id = mvmvif->deflink.bcast_sta.sta_id;
99409856582SLuca Coelho 	tx->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
995e705c121SKalle Valo 	tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;
996e705c121SKalle Valo 	tx_flags |=
997e705c121SKalle Valo 		iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<
998e705c121SKalle Valo 						TX_CMD_FLG_BT_PRIO_POS;
99909856582SLuca Coelho 	tx->tx_flags = cpu_to_le32(tx_flags);
1000e705c121SKalle Valo 
10011e3c3c35SEmmanuel Grumbach 	if (!fw_has_capa(&mvm->fw->ucode_capa,
1002656fca00SAvraham Stern 			 IWL_UCODE_TLV_CAPA_BEACON_ANT_SELECTION))
1003656fca00SAvraham Stern 		iwl_mvm_toggle_tx_ant(mvm, &mvm->mgmt_last_antenna_idx);
1004e705c121SKalle Valo 
100509856582SLuca Coelho 	tx->rate_n_flags =
1006e705c121SKalle Valo 		cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
1007e705c121SKalle Valo 			    RATE_MCS_ANT_POS);
1008e705c121SKalle Valo 
1009ef2e7a51SIlan Peer 	rate = iwl_mvm_mac_ctxt_get_beacon_rate(mvm, info, vif);
1010e705c121SKalle Valo 
1011d35d95ceSMiri Korenblit 	tx->rate_n_flags |=
1012d35d95ceSMiri Korenblit 		cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(mvm->fw, rate));
10136ca33f8bSHaim Dreyfuss 	if (rate == IWL_FIRST_CCK_RATE)
101448c6ebc1SMiri Korenblit 		tx->rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK_V1);
10156ca33f8bSHaim Dreyfuss 
101609856582SLuca Coelho }
1017e705c121SKalle Valo 
1018138664a3SSara Sharon int iwl_mvm_mac_ctxt_send_beacon_cmd(struct iwl_mvm *mvm,
101909856582SLuca Coelho 				     struct sk_buff *beacon,
102009856582SLuca Coelho 				     void *data, int len)
102109856582SLuca Coelho {
102209856582SLuca Coelho 	struct iwl_host_cmd cmd = {
102309856582SLuca Coelho 		.id = BEACON_TEMPLATE_CMD,
102409856582SLuca Coelho 		.flags = CMD_ASYNC,
102509856582SLuca Coelho 	};
102609856582SLuca Coelho 
102709856582SLuca Coelho 	cmd.len[0] = len;
102809856582SLuca Coelho 	cmd.data[0] = data;
1029e705c121SKalle Valo 	cmd.dataflags[0] = 0;
103009856582SLuca Coelho 	cmd.len[1] = beacon->len;
1031e705c121SKalle Valo 	cmd.data[1] = beacon->data;
1032e705c121SKalle Valo 	cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
1033e705c121SKalle Valo 
1034e705c121SKalle Valo 	return iwl_mvm_send_cmd(mvm, &cmd);
1035e705c121SKalle Valo }
1036e705c121SKalle Valo 
103709856582SLuca Coelho static int iwl_mvm_mac_ctxt_send_beacon_v6(struct iwl_mvm *mvm,
103809856582SLuca Coelho 					   struct ieee80211_vif *vif,
103909856582SLuca Coelho 					   struct sk_buff *beacon)
104009856582SLuca Coelho {
104109856582SLuca Coelho 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
104209856582SLuca Coelho 	struct iwl_mac_beacon_cmd_v6 beacon_cmd = {};
104309856582SLuca Coelho 
104409856582SLuca Coelho 	iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
104509856582SLuca Coelho 
104609856582SLuca Coelho 	beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
104709856582SLuca Coelho 
104809856582SLuca Coelho 	if (vif->type == NL80211_IFTYPE_AP)
104909856582SLuca Coelho 		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
105009856582SLuca Coelho 					 &beacon_cmd.tim_size,
105109856582SLuca Coelho 					 beacon->data, beacon->len);
105209856582SLuca Coelho 
105309856582SLuca Coelho 	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
105409856582SLuca Coelho 						sizeof(beacon_cmd));
105509856582SLuca Coelho }
105609856582SLuca Coelho 
105709856582SLuca Coelho static int iwl_mvm_mac_ctxt_send_beacon_v7(struct iwl_mvm *mvm,
105809856582SLuca Coelho 					   struct ieee80211_vif *vif,
105909856582SLuca Coelho 					   struct sk_buff *beacon)
106009856582SLuca Coelho {
106109856582SLuca Coelho 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
106209856582SLuca Coelho 	struct iwl_mac_beacon_cmd_v7 beacon_cmd = {};
106309856582SLuca Coelho 
106409856582SLuca Coelho 	iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
106509856582SLuca Coelho 
106609856582SLuca Coelho 	beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
106709856582SLuca Coelho 
106809856582SLuca Coelho 	if (vif->type == NL80211_IFTYPE_AP)
106909856582SLuca Coelho 		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
107009856582SLuca Coelho 					 &beacon_cmd.tim_size,
107109856582SLuca Coelho 					 beacon->data, beacon->len);
107209856582SLuca Coelho 
107309856582SLuca Coelho 	beacon_cmd.csa_offset =
107409856582SLuca Coelho 		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
107509856582SLuca Coelho 						   WLAN_EID_CHANNEL_SWITCH,
107609856582SLuca Coelho 						   beacon->len));
107709856582SLuca Coelho 	beacon_cmd.ecsa_offset =
107809856582SLuca Coelho 		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
107909856582SLuca Coelho 						   WLAN_EID_EXT_CHANSWITCH_ANN,
108009856582SLuca Coelho 						   beacon->len));
108109856582SLuca Coelho 
108209856582SLuca Coelho 	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
108309856582SLuca Coelho 						sizeof(beacon_cmd));
108409856582SLuca Coelho }
108509856582SLuca Coelho 
10866ca33f8bSHaim Dreyfuss static int iwl_mvm_mac_ctxt_send_beacon_v9(struct iwl_mvm *mvm,
108709856582SLuca Coelho 					   struct ieee80211_vif *vif,
108836cf5377SGregory Greenman 					   struct sk_buff *beacon,
108936cf5377SGregory Greenman 					   struct ieee80211_bss_conf *link_conf)
109009856582SLuca Coelho {
109109856582SLuca Coelho 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
10926ca33f8bSHaim Dreyfuss 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(beacon);
109309856582SLuca Coelho 	struct iwl_mac_beacon_cmd beacon_cmd = {};
1094ef2e7a51SIlan Peer 	u8 rate = iwl_mvm_mac_ctxt_get_beacon_rate(mvm, info, vif);
10956ca33f8bSHaim Dreyfuss 	u16 flags;
1096eae94cf8SLuca Coelho 	struct ieee80211_chanctx_conf *ctx;
1097eae94cf8SLuca Coelho 	int channel;
1098cd2c46a7SMiri Korenblit 	flags = iwl_mvm_mac_ctxt_get_beacon_flags(mvm->fw, rate);
10996ca33f8bSHaim Dreyfuss 
1100eae94cf8SLuca Coelho 	/* Enable FILS on PSC channels only */
1101eae94cf8SLuca Coelho 	rcu_read_lock();
110236cf5377SGregory Greenman 	ctx = rcu_dereference(link_conf->chanctx_conf);
1103eae94cf8SLuca Coelho 	channel = ieee80211_frequency_to_channel(ctx->def.chan->center_freq);
1104eae94cf8SLuca Coelho 	WARN_ON(channel == 0);
1105eae94cf8SLuca Coelho 	if (cfg80211_channel_is_psc(ctx->def.chan) &&
1106eae94cf8SLuca Coelho 	    !IWL_MVM_DISABLE_AP_FILS) {
1107971cbe50SJohannes Berg 		flags |= iwl_fw_lookup_cmd_ver(mvm->fw, BEACON_TEMPLATE_CMD,
1108cd2c46a7SMiri Korenblit 					       0) > 10 ?
1109cd2c46a7SMiri Korenblit 			IWL_MAC_BEACON_FILS :
1110cd2c46a7SMiri Korenblit 			IWL_MAC_BEACON_FILS_V1;
1111eae94cf8SLuca Coelho 		beacon_cmd.short_ssid =
1112f276e20bSJohannes Berg 			cpu_to_le32(~crc32_le(~0, vif->cfg.ssid,
1113f276e20bSJohannes Berg 					      vif->cfg.ssid_len));
1114eae94cf8SLuca Coelho 	}
1115eae94cf8SLuca Coelho 	rcu_read_unlock();
1116eae94cf8SLuca Coelho 
11176ca33f8bSHaim Dreyfuss 	beacon_cmd.flags = cpu_to_le16(flags);
111809856582SLuca Coelho 	beacon_cmd.byte_cnt = cpu_to_le16((u16)beacon->len);
111938e72100SJohannes Berg 
112038e72100SJohannes Berg 	if (WARN_ON(!mvmvif->link[link_conf->link_id]))
112138e72100SJohannes Berg 		return -EINVAL;
112238e72100SJohannes Berg 
1123d6f6b0d8SGregory Greenman 	if (iwl_fw_lookup_cmd_ver(mvm->fw, BEACON_TEMPLATE_CMD, 0) > 12)
1124d6f6b0d8SGregory Greenman 		beacon_cmd.link_id =
1125d6f6b0d8SGregory Greenman 			cpu_to_le32(mvmvif->link[link_conf->link_id]->fw_link_id);
1126d6f6b0d8SGregory Greenman 	else
112736cf5377SGregory Greenman 		beacon_cmd.link_id = cpu_to_le32((u32)mvmvif->id);
112809856582SLuca Coelho 
112909856582SLuca Coelho 	if (vif->type == NL80211_IFTYPE_AP)
11306ca33f8bSHaim Dreyfuss 		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
113109856582SLuca Coelho 					 &beacon_cmd.tim_size,
113209856582SLuca Coelho 					 beacon->data, beacon->len);
113309856582SLuca Coelho 
113409856582SLuca Coelho 	beacon_cmd.csa_offset =
113509856582SLuca Coelho 		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
113609856582SLuca Coelho 						   WLAN_EID_CHANNEL_SWITCH,
113709856582SLuca Coelho 						   beacon->len));
113809856582SLuca Coelho 	beacon_cmd.ecsa_offset =
113909856582SLuca Coelho 		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
114009856582SLuca Coelho 						   WLAN_EID_EXT_CHANSWITCH_ANN,
114109856582SLuca Coelho 						   beacon->len));
114209856582SLuca Coelho 
114309856582SLuca Coelho 	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
114409856582SLuca Coelho 						sizeof(beacon_cmd));
114509856582SLuca Coelho }
114609856582SLuca Coelho 
11477035b5baSJohannes Berg static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
114809856582SLuca Coelho 					struct ieee80211_vif *vif,
114936cf5377SGregory Greenman 					struct sk_buff *beacon,
115036cf5377SGregory Greenman 					struct ieee80211_bss_conf *link_conf)
115109856582SLuca Coelho {
115209856582SLuca Coelho 	if (WARN_ON(!beacon))
115309856582SLuca Coelho 		return -EINVAL;
115409856582SLuca Coelho 
115550386305SSara Sharon 	if (IWL_MVM_NON_TRANSMITTING_AP)
115650386305SSara Sharon 		return 0;
115750386305SSara Sharon 
115809856582SLuca Coelho 	if (!fw_has_capa(&mvm->fw->ucode_capa,
115909856582SLuca Coelho 			 IWL_UCODE_TLV_CAPA_CSA_AND_TBTT_OFFLOAD))
116009856582SLuca Coelho 		return iwl_mvm_mac_ctxt_send_beacon_v6(mvm, vif, beacon);
116109856582SLuca Coelho 
11626ca33f8bSHaim Dreyfuss 	if (fw_has_api(&mvm->fw->ucode_capa,
11636ca33f8bSHaim Dreyfuss 		       IWL_UCODE_TLV_API_NEW_BEACON_TEMPLATE))
116436cf5377SGregory Greenman 		return iwl_mvm_mac_ctxt_send_beacon_v9(mvm, vif, beacon,
116536cf5377SGregory Greenman 						       link_conf);
116609856582SLuca Coelho 
11676ca33f8bSHaim Dreyfuss 	return iwl_mvm_mac_ctxt_send_beacon_v7(mvm, vif, beacon);
116809856582SLuca Coelho }
116909856582SLuca Coelho 
1170e705c121SKalle Valo /* The beacon template for the AP/GO/IBSS has changed and needs update */
1171e705c121SKalle Valo int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
117236cf5377SGregory Greenman 				    struct ieee80211_vif *vif,
117336cf5377SGregory Greenman 				    struct ieee80211_bss_conf *link_conf)
1174e705c121SKalle Valo {
1175e705c121SKalle Valo 	struct sk_buff *beacon;
1176e705c121SKalle Valo 	int ret;
1177e705c121SKalle Valo 
1178e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_AP &&
1179e705c121SKalle Valo 		vif->type != NL80211_IFTYPE_ADHOC);
1180e705c121SKalle Valo 
118136cf5377SGregory Greenman 	beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL,
118236cf5377SGregory Greenman 					       link_conf->link_id);
1183e705c121SKalle Valo 	if (!beacon)
1184e705c121SKalle Valo 		return -ENOMEM;
1185e705c121SKalle Valo 
118690a12829SSara Sharon #ifdef CONFIG_IWLWIFI_DEBUGFS
11870f5d44acSZhang Qilong 	if (mvm->beacon_inject_active) {
11880f5d44acSZhang Qilong 		dev_kfree_skb(beacon);
118990a12829SSara Sharon 		return -EBUSY;
11900f5d44acSZhang Qilong 	}
119190a12829SSara Sharon #endif
119290a12829SSara Sharon 
119336cf5377SGregory Greenman 	ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon, link_conf);
1194e705c121SKalle Valo 	dev_kfree_skb(beacon);
1195e705c121SKalle Valo 	return ret;
1196e705c121SKalle Valo }
1197e705c121SKalle Valo 
1198e705c121SKalle Valo struct iwl_mvm_mac_ap_iterator_data {
1199e705c121SKalle Valo 	struct iwl_mvm *mvm;
1200e705c121SKalle Valo 	struct ieee80211_vif *vif;
1201e705c121SKalle Valo 	u32 beacon_device_ts;
1202e705c121SKalle Valo 	u16 beacon_int;
1203e705c121SKalle Valo };
1204e705c121SKalle Valo 
1205e705c121SKalle Valo /* Find the beacon_device_ts and beacon_int for a managed interface */
1206e705c121SKalle Valo static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
1207e705c121SKalle Valo 				    struct ieee80211_vif *vif)
1208e705c121SKalle Valo {
1209e705c121SKalle Valo 	struct iwl_mvm_mac_ap_iterator_data *data = _data;
1210e705c121SKalle Valo 
1211f276e20bSJohannes Berg 	if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc)
1212e705c121SKalle Valo 		return;
1213e705c121SKalle Valo 
1214e705c121SKalle Valo 	/* Station client has higher priority over P2P client*/
1215e705c121SKalle Valo 	if (vif->p2p && data->beacon_device_ts)
1216e705c121SKalle Valo 		return;
1217e705c121SKalle Valo 
1218e705c121SKalle Valo 	data->beacon_device_ts = vif->bss_conf.sync_device_ts;
1219e705c121SKalle Valo 	data->beacon_int = vif->bss_conf.beacon_int;
1220e705c121SKalle Valo }
1221e705c121SKalle Valo 
1222e705c121SKalle Valo /*
1223af6d168fSMiri Korenblit  * Fill the filter flags for mac context of type AP or P2P GO.
1224af6d168fSMiri Korenblit  */
12259be162a7SMiri Korenblit void iwl_mvm_mac_ctxt_cmd_ap_set_filter_flags(struct iwl_mvm *mvm,
1226af6d168fSMiri Korenblit 					      struct iwl_mvm_vif *mvmvif,
1227af6d168fSMiri Korenblit 					      __le32 *filter_flags,
1228af6d168fSMiri Korenblit 					      int accept_probe_req_flag,
1229af6d168fSMiri Korenblit 					      int accept_beacon_flag)
1230af6d168fSMiri Korenblit {
1231af6d168fSMiri Korenblit 	/*
1232af6d168fSMiri Korenblit 	 * in AP mode, pass probe requests and beacons from other APs
1233af6d168fSMiri Korenblit 	 * (needed for ht protection); when there're no any associated
1234af6d168fSMiri Korenblit 	 * station don't ask FW to pass beacons to prevent unnecessary
1235af6d168fSMiri Korenblit 	 * wake-ups.
1236af6d168fSMiri Korenblit 	 */
1237af6d168fSMiri Korenblit 	*filter_flags |= cpu_to_le32(accept_probe_req_flag);
1238af6d168fSMiri Korenblit 	if (mvmvif->ap_assoc_sta_count || !mvm->drop_bcn_ap_mode) {
1239af6d168fSMiri Korenblit 		*filter_flags |= cpu_to_le32(accept_beacon_flag);
1240af6d168fSMiri Korenblit 		IWL_DEBUG_HC(mvm, "Asking FW to pass beacons\n");
1241af6d168fSMiri Korenblit 	} else {
1242af6d168fSMiri Korenblit 		IWL_DEBUG_HC(mvm, "No need to receive beacons\n");
1243af6d168fSMiri Korenblit 	}
1244af6d168fSMiri Korenblit }
1245af6d168fSMiri Korenblit 
1246af6d168fSMiri Korenblit /*
1247e705c121SKalle Valo  * Fill the specific data for mac context of type AP of P2P GO
1248e705c121SKalle Valo  */
1249e705c121SKalle Valo static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
1250e705c121SKalle Valo 					 struct ieee80211_vif *vif,
1251186cd49aSJohannes Berg 					 struct iwl_mac_ctx_cmd *cmd,
1252e705c121SKalle Valo 					 struct iwl_mac_data_ap *ctxt_ap,
1253e705c121SKalle Valo 					 bool add)
1254e705c121SKalle Valo {
1255e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1256e705c121SKalle Valo 	struct iwl_mvm_mac_ap_iterator_data data = {
1257e705c121SKalle Valo 		.mvm = mvm,
1258e705c121SKalle Valo 		.vif = vif,
1259e705c121SKalle Valo 		.beacon_device_ts = 0
1260e705c121SKalle Valo 	};
1261e705c121SKalle Valo 
1262186cd49aSJohannes Berg 	/* in AP mode, the MCAST FIFO takes the EDCA params from VO */
1263186cd49aSJohannes Berg 	cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |= BIT(IWL_MVM_TX_FIFO_MCAST);
1264186cd49aSJohannes Berg 
1265af6d168fSMiri Korenblit 	iwl_mvm_mac_ctxt_cmd_ap_set_filter_flags(mvm, mvmvif,
1266af6d168fSMiri Korenblit 						 &cmd->filter_flags,
1267af6d168fSMiri Korenblit 						 MAC_FILTER_IN_PROBE_REQUEST,
1268af6d168fSMiri Korenblit 						 MAC_FILTER_IN_BEACON);
1269186cd49aSJohannes Berg 
1270e705c121SKalle Valo 	ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
1271e705c121SKalle Valo 	ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
1272e705c121SKalle Valo 					     vif->bss_conf.dtim_period);
1273e705c121SKalle Valo 
1274ced19f26SSara Sharon 	if (!fw_has_api(&mvm->fw->ucode_capa,
1275ced19f26SSara Sharon 			IWL_UCODE_TLV_API_STA_TYPE))
1276650cadb7SGregory Greenman 		ctxt_ap->mcast_qid = cpu_to_le32(mvmvif->deflink.cab_queue);
1277e705c121SKalle Valo 
1278e705c121SKalle Valo 	/*
1279e705c121SKalle Valo 	 * Only set the beacon time when the MAC is being added, when we
1280e705c121SKalle Valo 	 * just modify the MAC then we should keep the time -- the firmware
1281e705c121SKalle Valo 	 * can otherwise have a "jumping" TBTT.
1282e705c121SKalle Valo 	 */
1283e705c121SKalle Valo 	if (add) {
1284e705c121SKalle Valo 		/*
1285e705c121SKalle Valo 		 * If there is a station/P2P client interface which is
1286e705c121SKalle Valo 		 * associated, set the AP's TBTT far enough from the station's
1287e705c121SKalle Valo 		 * TBTT. Otherwise, set it to the current system time
1288e705c121SKalle Valo 		 */
1289e705c121SKalle Valo 		ieee80211_iterate_active_interfaces_atomic(
1290e705c121SKalle Valo 			mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1291e705c121SKalle Valo 			iwl_mvm_mac_ap_iterator, &data);
1292e705c121SKalle Valo 
1293e705c121SKalle Valo 		if (data.beacon_device_ts) {
1294e8a533cbSJason A. Donenfeld 			u32 rand = get_random_u32_inclusive(36, 63);
1295e705c121SKalle Valo 			mvmvif->ap_beacon_time = data.beacon_device_ts +
1296e705c121SKalle Valo 				ieee80211_tu_to_usec(data.beacon_int * rand /
1297e705c121SKalle Valo 						     100);
1298e705c121SKalle Valo 		} else {
1299afc1e3b4SAvraham Stern 			mvmvif->ap_beacon_time = iwl_mvm_get_systime(mvm);
1300e705c121SKalle Valo 		}
1301e705c121SKalle Valo 	}
1302e705c121SKalle Valo 
1303e705c121SKalle Valo 	ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
1304e705c121SKalle Valo 	ctxt_ap->beacon_tsf = 0; /* unused */
1305e705c121SKalle Valo 
1306e705c121SKalle Valo 	/* TODO: Assume that the beacon id == mac context id */
1307e705c121SKalle Valo 	ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
1308e705c121SKalle Valo }
1309e705c121SKalle Valo 
1310e705c121SKalle Valo static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
1311e705c121SKalle Valo 				   struct ieee80211_vif *vif,
1312e705c121SKalle Valo 				   u32 action)
1313e705c121SKalle Valo {
1314e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd = {};
1315e705c121SKalle Valo 
1316e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
1317e705c121SKalle Valo 
1318e705c121SKalle Valo 	/* Fill the common data for all mac context types */
1319e705c121SKalle Valo 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1320e705c121SKalle Valo 
1321e705c121SKalle Valo 	/* Fill the data specific for ap mode */
1322186cd49aSJohannes Berg 	iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.ap,
1323e705c121SKalle Valo 				     action == FW_CTXT_ACTION_ADD);
1324e705c121SKalle Valo 
1325e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1326e705c121SKalle Valo }
1327e705c121SKalle Valo 
1328e705c121SKalle Valo static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
1329e705c121SKalle Valo 				   struct ieee80211_vif *vif,
1330e705c121SKalle Valo 				   u32 action)
1331e705c121SKalle Valo {
1332e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd = {};
1333e705c121SKalle Valo 	struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
1334e705c121SKalle Valo 
1335e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
1336e705c121SKalle Valo 
1337e705c121SKalle Valo 	/* Fill the common data for all mac context types */
1338e705c121SKalle Valo 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1339e705c121SKalle Valo 
1340e705c121SKalle Valo 	/* Fill the data specific for GO mode */
1341186cd49aSJohannes Berg 	iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.go.ap,
1342e705c121SKalle Valo 				     action == FW_CTXT_ACTION_ADD);
1343e705c121SKalle Valo 
1344e705c121SKalle Valo 	cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
1345e705c121SKalle Valo 					IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
1346e705c121SKalle Valo 	cmd.go.opp_ps_enabled =
1347e705c121SKalle Valo 			cpu_to_le32(!!(noa->oppps_ctwindow &
1348e705c121SKalle Valo 					IEEE80211_P2P_OPPPS_ENABLE_BIT));
1349e705c121SKalle Valo 
1350e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1351e705c121SKalle Valo }
1352e705c121SKalle Valo 
1353e705c121SKalle Valo static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1354e705c121SKalle Valo 				u32 action, bool force_assoc_off,
1355e705c121SKalle Valo 				const u8 *bssid_override)
1356e705c121SKalle Valo {
1357e705c121SKalle Valo 	switch (vif->type) {
1358e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
1359e705c121SKalle Valo 		return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
1360e705c121SKalle Valo 						force_assoc_off,
1361e705c121SKalle Valo 						bssid_override);
1362e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
1363e705c121SKalle Valo 		if (!vif->p2p)
1364e705c121SKalle Valo 			return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
1365e705c121SKalle Valo 		else
1366e705c121SKalle Valo 			return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
1367e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
1368e705c121SKalle Valo 		return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
1369e705c121SKalle Valo 	case NL80211_IFTYPE_P2P_DEVICE:
1370e705c121SKalle Valo 		return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
1371e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
1372e705c121SKalle Valo 		return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
1373e705c121SKalle Valo 	default:
1374e705c121SKalle Valo 		break;
1375e705c121SKalle Valo 	}
1376e705c121SKalle Valo 
1377e705c121SKalle Valo 	return -EOPNOTSUPP;
1378e705c121SKalle Valo }
1379e705c121SKalle Valo 
1380e705c121SKalle Valo int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1381e705c121SKalle Valo {
1382e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1383e705c121SKalle Valo 	int ret;
1384e705c121SKalle Valo 
1385e705c121SKalle Valo 	if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
1386e705c121SKalle Valo 		      vif->addr, ieee80211_vif_type_p2p(vif)))
1387e705c121SKalle Valo 		return -EIO;
1388e705c121SKalle Valo 
1389e705c121SKalle Valo 	ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
1390e705c121SKalle Valo 				   true, NULL);
1391e705c121SKalle Valo 	if (ret)
1392e705c121SKalle Valo 		return ret;
1393e705c121SKalle Valo 
1394e705c121SKalle Valo 	/* will only do anything at resume from D3 time */
1395e705c121SKalle Valo 	iwl_mvm_set_last_nonqos_seq(mvm, vif);
1396e705c121SKalle Valo 
1397e705c121SKalle Valo 	mvmvif->uploaded = true;
1398e705c121SKalle Valo 	return 0;
1399e705c121SKalle Valo }
1400e705c121SKalle Valo 
1401e705c121SKalle Valo int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1402e705c121SKalle Valo 			     bool force_assoc_off, const u8 *bssid_override)
1403e705c121SKalle Valo {
1404e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1405e705c121SKalle Valo 
1406e705c121SKalle Valo 	if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
1407e705c121SKalle Valo 		      vif->addr, ieee80211_vif_type_p2p(vif)))
1408e705c121SKalle Valo 		return -EIO;
1409e705c121SKalle Valo 
1410e705c121SKalle Valo 	return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
1411e705c121SKalle Valo 				    force_assoc_off, bssid_override);
1412e705c121SKalle Valo }
1413e705c121SKalle Valo 
1414e705c121SKalle Valo int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1415e705c121SKalle Valo {
1416e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1417e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd;
1418e705c121SKalle Valo 	int ret;
1419e705c121SKalle Valo 
1420e705c121SKalle Valo 	if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1421e705c121SKalle Valo 		      vif->addr, ieee80211_vif_type_p2p(vif)))
1422e705c121SKalle Valo 		return -EIO;
1423e705c121SKalle Valo 
1424e705c121SKalle Valo 	memset(&cmd, 0, sizeof(cmd));
1425e705c121SKalle Valo 
1426e705c121SKalle Valo 	cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1427e705c121SKalle Valo 							   mvmvif->color));
1428e705c121SKalle Valo 	cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1429e705c121SKalle Valo 
1430af6d168fSMiri Korenblit 	ret = iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1431af6d168fSMiri Korenblit 	if (ret)
1432e705c121SKalle Valo 		return ret;
1433e705c121SKalle Valo 
1434e705c121SKalle Valo 	mvmvif->uploaded = false;
1435e705c121SKalle Valo 
14360e39eb03SChaya Rachel Ivgi 	if (vif->type == NL80211_IFTYPE_MONITOR) {
1437e705c121SKalle Valo 		__clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, mvm->hw->flags);
14380e39eb03SChaya Rachel Ivgi 		iwl_mvm_dealloc_snif_sta(mvm);
14390e39eb03SChaya Rachel Ivgi 	}
1440e705c121SKalle Valo 
1441e705c121SKalle Valo 	return 0;
1442e705c121SKalle Valo }
1443e705c121SKalle Valo 
1444e705c121SKalle Valo static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
1445e705c121SKalle Valo 				   struct ieee80211_vif *csa_vif, u32 gp2,
1446e705c121SKalle Valo 				   bool tx_success)
1447e705c121SKalle Valo {
1448e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif =
1449e705c121SKalle Valo 			iwl_mvm_vif_from_mac80211(csa_vif);
1450e705c121SKalle Valo 
1451e705c121SKalle Valo 	/* Don't start to countdown from a failed beacon */
1452e705c121SKalle Valo 	if (!tx_success && !mvmvif->csa_countdown)
1453e705c121SKalle Valo 		return;
1454e705c121SKalle Valo 
1455e705c121SKalle Valo 	mvmvif->csa_countdown = true;
1456e705c121SKalle Valo 
14578552a434SJohn Crispin 	if (!ieee80211_beacon_cntdwn_is_complete(csa_vif)) {
14588552a434SJohn Crispin 		int c = ieee80211_beacon_update_cntdwn(csa_vif);
1459e705c121SKalle Valo 
146036cf5377SGregory Greenman 		iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif,
146136cf5377SGregory Greenman 						&csa_vif->bss_conf);
1462e705c121SKalle Valo 		if (csa_vif->p2p &&
1463e705c121SKalle Valo 		    !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2 &&
1464e705c121SKalle Valo 		    tx_success) {
1465e705c121SKalle Valo 			u32 rel_time = (c + 1) *
1466e705c121SKalle Valo 				       csa_vif->bss_conf.beacon_int -
1467e705c121SKalle Valo 				       IWL_MVM_CHANNEL_SWITCH_TIME_GO;
1468e705c121SKalle Valo 			u32 apply_time = gp2 + rel_time * 1024;
1469e705c121SKalle Valo 
1470e705c121SKalle Valo 			iwl_mvm_schedule_csa_period(mvm, csa_vif,
1471e705c121SKalle Valo 					 IWL_MVM_CHANNEL_SWITCH_TIME_GO -
1472e705c121SKalle Valo 					 IWL_MVM_CHANNEL_SWITCH_MARGIN,
1473e705c121SKalle Valo 					 apply_time);
1474e705c121SKalle Valo 		}
1475e705c121SKalle Valo 	} else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
1476e705c121SKalle Valo 		/* we don't have CSA NoA scheduled yet, switch now */
1477e705c121SKalle Valo 		ieee80211_csa_finish(csa_vif);
1478e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
1479e705c121SKalle Valo 	}
1480e705c121SKalle Valo }
1481e705c121SKalle Valo 
1482e705c121SKalle Valo void iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1483e705c121SKalle Valo 			     struct iwl_rx_cmd_buffer *rxb)
1484e705c121SKalle Valo {
1485e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1486afc857bcSJohannes Berg 	unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
1487e705c121SKalle Valo 	struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
148815e28c78SEmmanuel Grumbach 	struct iwl_extended_beacon_notif_v5 *beacon_v5 = (void *)pkt->data;
1489e705c121SKalle Valo 	struct ieee80211_vif *csa_vif;
1490e705c121SKalle Valo 	struct ieee80211_vif *tx_blocked_vif;
149112db294cSSara Sharon 	struct agg_tx_status *agg_status;
1492e705c121SKalle Valo 	u16 status;
1493e705c121SKalle Valo 
1494e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1495e705c121SKalle Valo 
1496e705c121SKalle Valo 	mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
1497e705c121SKalle Valo 
149815e28c78SEmmanuel Grumbach 	if (!iwl_mvm_is_short_beacon_notif_supported(mvm)) {
149915e28c78SEmmanuel Grumbach 		struct iwl_mvm_tx_resp *beacon_notify_hdr =
150015e28c78SEmmanuel Grumbach 			&beacon_v5->beacon_notify_hdr;
150115e28c78SEmmanuel Grumbach 
1502afc857bcSJohannes Berg 		if (unlikely(pkt_len < sizeof(*beacon_v5)))
1503afc857bcSJohannes Berg 			return;
1504afc857bcSJohannes Berg 
150515e28c78SEmmanuel Grumbach 		mvm->ibss_manager = beacon_v5->ibss_mgr_status != 0;
150612db294cSSara Sharon 		agg_status = iwl_mvm_get_agg_status(mvm, beacon_notify_hdr);
150712db294cSSara Sharon 		status = le16_to_cpu(agg_status->status) & TX_STATUS_MSK;
1508e705c121SKalle Valo 		IWL_DEBUG_RX(mvm,
1509677837b8SJohannes Berg 			     "beacon status %#x retries:%d tsf:0x%016llX gp2:0x%X rate:%d\n",
1510e705c121SKalle Valo 			     status, beacon_notify_hdr->failure_frame,
1511e705c121SKalle Valo 			     le64_to_cpu(beacon->tsf),
1512e705c121SKalle Valo 			     mvm->ap_last_beacon_gp2,
1513e705c121SKalle Valo 			     le32_to_cpu(beacon_notify_hdr->initial_rate));
151415e28c78SEmmanuel Grumbach 	} else {
1515afc857bcSJohannes Berg 		if (unlikely(pkt_len < sizeof(*beacon)))
1516afc857bcSJohannes Berg 			return;
1517afc857bcSJohannes Berg 
151815e28c78SEmmanuel Grumbach 		mvm->ibss_manager = beacon->ibss_mgr_status != 0;
151915e28c78SEmmanuel Grumbach 		status = le32_to_cpu(beacon->status) & TX_STATUS_MSK;
152015e28c78SEmmanuel Grumbach 		IWL_DEBUG_RX(mvm,
152115e28c78SEmmanuel Grumbach 			     "beacon status %#x tsf:0x%016llX gp2:0x%X\n",
152215e28c78SEmmanuel Grumbach 			     status, le64_to_cpu(beacon->tsf),
152315e28c78SEmmanuel Grumbach 			     mvm->ap_last_beacon_gp2);
152415e28c78SEmmanuel Grumbach 	}
1525e705c121SKalle Valo 
1526e705c121SKalle Valo 	csa_vif = rcu_dereference_protected(mvm->csa_vif,
1527e705c121SKalle Valo 					    lockdep_is_held(&mvm->mutex));
1528d0a9123eSJohannes Berg 	if (unlikely(csa_vif && csa_vif->bss_conf.csa_active))
1529e705c121SKalle Valo 		iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2,
1530e705c121SKalle Valo 				       (status == TX_STATUS_SUCCESS));
1531e705c121SKalle Valo 
1532e705c121SKalle Valo 	tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
1533e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
1534e705c121SKalle Valo 	if (unlikely(tx_blocked_vif)) {
1535e705c121SKalle Valo 		struct iwl_mvm_vif *mvmvif =
1536e705c121SKalle Valo 			iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1537e705c121SKalle Valo 
1538e705c121SKalle Valo 		/*
1539e705c121SKalle Valo 		 * The channel switch is started and we have blocked the
1540e705c121SKalle Valo 		 * stations. If this is the first beacon (the timeout wasn't
1541e705c121SKalle Valo 		 * set), set the unblock timeout, otherwise countdown
1542e705c121SKalle Valo 		 */
1543e705c121SKalle Valo 		if (!mvm->csa_tx_block_bcn_timeout)
1544e705c121SKalle Valo 			mvm->csa_tx_block_bcn_timeout =
1545e705c121SKalle Valo 				IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
1546e705c121SKalle Valo 		else
1547e705c121SKalle Valo 			mvm->csa_tx_block_bcn_timeout--;
1548e705c121SKalle Valo 
1549e705c121SKalle Valo 		/* Check if the timeout is expired, and unblock tx */
1550e705c121SKalle Valo 		if (mvm->csa_tx_block_bcn_timeout == 0) {
1551e705c121SKalle Valo 			iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
1552e705c121SKalle Valo 			RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1553e705c121SKalle Valo 		}
1554e705c121SKalle Valo 	}
1555e705c121SKalle Valo }
1556e705c121SKalle Valo 
1557698478c4SSara Sharon void iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
1558698478c4SSara Sharon 				     struct iwl_rx_cmd_buffer *rxb)
1559e705c121SKalle Valo {
1560698478c4SSara Sharon 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1561698478c4SSara Sharon 	struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
1562e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_missed_bcon *bcon_trig;
1563e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trigger;
1564e705c121SKalle Valo 	u32 stop_trig_missed_bcon, stop_trig_missed_bcon_since_rx;
1565e705c121SKalle Valo 	u32 rx_missed_bcon, rx_missed_bcon_since_rx;
1566698478c4SSara Sharon 	struct ieee80211_vif *vif;
1567d464550bSYedidya Benshimol 	/* Id can be mac/link id depending on the notification version */
1568d464550bSYedidya Benshimol 	u32 id = le32_to_cpu(mb->link_id);
1569eae7550bSShahar S Matityahu 	union iwl_dbg_tlv_tp_data tp_data = { .fw_pkt = pkt };
1570834f920eSMukesh Sisodiya 	u32 mac_type;
1571d464550bSYedidya Benshimol 	u8 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
1572d464550bSYedidya Benshimol 					       MISSED_BEACONS_NOTIFICATION,
1573d464550bSYedidya Benshimol 					       0);
1574d464550bSYedidya Benshimol 
1575d464550bSYedidya Benshimol 	rcu_read_lock();
1576d464550bSYedidya Benshimol 
1577d464550bSYedidya Benshimol 	/* before version four the ID in the notification refers to mac ID */
1578d464550bSYedidya Benshimol 	if (notif_ver < 4) {
1579d464550bSYedidya Benshimol 		vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);
1580d464550bSYedidya Benshimol 	} else {
1581d464550bSYedidya Benshimol 		struct ieee80211_bss_conf *bss_conf =
1582d464550bSYedidya Benshimol 			iwl_mvm_rcu_fw_link_id_to_link_conf(mvm, id, true);
1583d464550bSYedidya Benshimol 
1584d464550bSYedidya Benshimol 		if (!bss_conf)
1585d464550bSYedidya Benshimol 			goto out;
1586d464550bSYedidya Benshimol 
1587d464550bSYedidya Benshimol 		vif = bss_conf->vif;
1588d464550bSYedidya Benshimol 	}
1589e705c121SKalle Valo 
1590698478c4SSara Sharon 	IWL_DEBUG_INFO(mvm,
1591d464550bSYedidya Benshimol 		       "missed bcn %s_id=%u, consecutive=%u (%u, %u, %u)\n",
1592d464550bSYedidya Benshimol 		       notif_ver < 4 ? "mac" : "link",
1593d464550bSYedidya Benshimol 		       id,
1594698478c4SSara Sharon 		       le32_to_cpu(mb->consec_missed_beacons),
1595698478c4SSara Sharon 		       le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
1596698478c4SSara Sharon 		       le32_to_cpu(mb->num_recvd_beacons),
1597698478c4SSara Sharon 		       le32_to_cpu(mb->num_expected_beacons));
1598e705c121SKalle Valo 
1599698478c4SSara Sharon 	if (!vif)
1600698478c4SSara Sharon 		goto out;
1601698478c4SSara Sharon 
1602834f920eSMukesh Sisodiya 	mac_type = iwl_mvm_get_mac_type(vif);
1603834f920eSMukesh Sisodiya 
1604834f920eSMukesh Sisodiya 	IWL_DEBUG_INFO(mvm, "missed beacon mac_type=%u,\n", mac_type);
1605834f920eSMukesh Sisodiya 
1606834f920eSMukesh Sisodiya 	mvm->trans->dbg.dump_file_name_ext_valid = true;
1607834f920eSMukesh Sisodiya 	snprintf(mvm->trans->dbg.dump_file_name_ext, IWL_FW_INI_MAX_NAME,
1608834f920eSMukesh Sisodiya 		 "MacId_%d_MacType_%d", id, mac_type);
1609834f920eSMukesh Sisodiya 
1610698478c4SSara Sharon 	rx_missed_bcon = le32_to_cpu(mb->consec_missed_beacons);
1611e705c121SKalle Valo 	rx_missed_bcon_since_rx =
1612698478c4SSara Sharon 		le32_to_cpu(mb->consec_missed_beacons_since_last_rx);
1613e705c121SKalle Valo 	/*
1614e705c121SKalle Valo 	 * TODO: the threshold should be adjusted based on latency conditions,
1615e705c121SKalle Valo 	 * and/or in case of a CS flow on one of the other AP vifs.
1616e705c121SKalle Valo 	 */
1617babea2d4SAndrei Otcheretianski 	if (rx_missed_bcon > IWL_MVM_MISSED_BEACONS_THRESHOLD_LONG)
1618babea2d4SAndrei Otcheretianski 		iwl_mvm_connection_loss(mvm, vif, "missed beacons");
1619babea2d4SAndrei Otcheretianski 	else if (rx_missed_bcon_since_rx > IWL_MVM_MISSED_BEACONS_THRESHOLD)
1620e705c121SKalle Valo 		ieee80211_beacon_loss(vif);
1621e705c121SKalle Valo 
1622b108d8c7SShahar S Matityahu 	iwl_dbg_tlv_time_point(&mvm->fwrt,
1623eae7550bSShahar S Matityahu 			       IWL_FW_INI_TIME_POINT_MISSED_BEACONS, &tp_data);
1624b108d8c7SShahar S Matityahu 
16256c042d75SSara Sharon 	trigger = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
16266c042d75SSara Sharon 					FW_DBG_TRIGGER_MISSED_BEACONS);
16276c042d75SSara Sharon 	if (!trigger)
1628698478c4SSara Sharon 		goto out;
1629e705c121SKalle Valo 
1630e705c121SKalle Valo 	bcon_trig = (void *)trigger->data;
1631e705c121SKalle Valo 	stop_trig_missed_bcon = le32_to_cpu(bcon_trig->stop_consec_missed_bcon);
1632e705c121SKalle Valo 	stop_trig_missed_bcon_since_rx =
1633e705c121SKalle Valo 		le32_to_cpu(bcon_trig->stop_consec_missed_bcon_since_rx);
1634e705c121SKalle Valo 
1635e705c121SKalle Valo 	/* TODO: implement start trigger */
1636e705c121SKalle Valo 
1637e705c121SKalle Valo 	if (rx_missed_bcon_since_rx >= stop_trig_missed_bcon_since_rx ||
1638e705c121SKalle Valo 	    rx_missed_bcon >= stop_trig_missed_bcon)
16397174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&mvm->fwrt, trigger, NULL);
1640da2eb669SSara Sharon 
1641698478c4SSara Sharon out:
1642698478c4SSara Sharon 	rcu_read_unlock();
1643e705c121SKalle Valo }
16440db056d3SSara Sharon 
16450db056d3SSara Sharon void iwl_mvm_rx_stored_beacon_notif(struct iwl_mvm *mvm,
16460db056d3SSara Sharon 				    struct iwl_rx_cmd_buffer *rxb)
16470db056d3SSara Sharon {
16480db056d3SSara Sharon 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1649afc857bcSJohannes Berg 	unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
1650fb3fac5fSGregory Greenman 	struct iwl_stored_beacon_notif_common *sb = (void *)pkt->data;
16510db056d3SSara Sharon 	struct ieee80211_rx_status rx_status;
16520db056d3SSara Sharon 	struct sk_buff *skb;
1653fb3fac5fSGregory Greenman 	u8 *data;
16540db056d3SSara Sharon 	u32 size = le32_to_cpu(sb->byte_count);
1655971cbe50SJohannes Berg 	int ver = iwl_fw_lookup_cmd_ver(mvm->fw,
1656971cbe50SJohannes Berg 					WIDE_ID(PROT_OFFLOAD_GROUP, STORED_BEACON_NTF),
1657971cbe50SJohannes Berg 					0);
16580db056d3SSara Sharon 
1659fb3fac5fSGregory Greenman 	if (size == 0)
16600db056d3SSara Sharon 		return;
16610db056d3SSara Sharon 
1662fb3fac5fSGregory Greenman 	/* handle per-version differences */
1663fb3fac5fSGregory Greenman 	if (ver <= 2) {
1664fb3fac5fSGregory Greenman 		struct iwl_stored_beacon_notif_v2 *sb_v2 = (void *)pkt->data;
1665fb3fac5fSGregory Greenman 
1666fb3fac5fSGregory Greenman 		if (pkt_len < struct_size(sb_v2, data, size))
1667fb3fac5fSGregory Greenman 			return;
1668fb3fac5fSGregory Greenman 
1669fb3fac5fSGregory Greenman 		data = sb_v2->data;
1670fb3fac5fSGregory Greenman 	} else {
1671fb3fac5fSGregory Greenman 		struct iwl_stored_beacon_notif_v3 *sb_v3 = (void *)pkt->data;
1672fb3fac5fSGregory Greenman 
1673fb3fac5fSGregory Greenman 		if (pkt_len < struct_size(sb_v3, data, size))
1674fb3fac5fSGregory Greenman 			return;
1675fb3fac5fSGregory Greenman 
1676fb3fac5fSGregory Greenman 		data = sb_v3->data;
1677fb3fac5fSGregory Greenman 	}
1678fb3fac5fSGregory Greenman 
16790db056d3SSara Sharon 	skb = alloc_skb(size, GFP_ATOMIC);
16800db056d3SSara Sharon 	if (!skb) {
16810db056d3SSara Sharon 		IWL_ERR(mvm, "alloc_skb failed\n");
16820db056d3SSara Sharon 		return;
16830db056d3SSara Sharon 	}
16840db056d3SSara Sharon 
16850db056d3SSara Sharon 	/* update rx_status according to the notification's metadata */
16860db056d3SSara Sharon 	memset(&rx_status, 0, sizeof(rx_status));
16870db056d3SSara Sharon 	rx_status.mactime = le64_to_cpu(sb->tsf);
168877fe7395SSara Sharon 	/* TSF as indicated by the firmware  is at INA time */
168977fe7395SSara Sharon 	rx_status.flag |= RX_FLAG_MACTIME_PLCP_START;
16900db056d3SSara Sharon 	rx_status.device_timestamp = le32_to_cpu(sb->system_time);
16910db056d3SSara Sharon 	rx_status.band =
169234118c25SSara Sharon 		(sb->band & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
169357fbcce3SJohannes Berg 				NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
16940db056d3SSara Sharon 	rx_status.freq =
16950db056d3SSara Sharon 		ieee80211_channel_to_frequency(le16_to_cpu(sb->channel),
16960db056d3SSara Sharon 					       rx_status.band);
16970db056d3SSara Sharon 
16980db056d3SSara Sharon 	/* copy the data */
1699fb3fac5fSGregory Greenman 	skb_put_data(skb, data, size);
17000db056d3SSara Sharon 	memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
17010db056d3SSara Sharon 
17020db056d3SSara Sharon 	/* pass it as regular rx to mac80211 */
1703d63b548fSJohannes Berg 	ieee80211_rx_napi(mvm->hw, NULL, skb, NULL);
17040db056d3SSara Sharon }
1705d3a108a4SAndrei Otcheretianski 
1706698478c4SSara Sharon void iwl_mvm_probe_resp_data_notif(struct iwl_mvm *mvm,
1707698478c4SSara Sharon 				   struct iwl_rx_cmd_buffer *rxb)
170886e177d8SGregory Greenman {
1709698478c4SSara Sharon 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1710698478c4SSara Sharon 	struct iwl_probe_resp_data_notif *notif = (void *)pkt->data;
171186e177d8SGregory Greenman 	struct iwl_probe_resp_data *old_data, *new_data;
1712698478c4SSara Sharon 	u32 id = le32_to_cpu(notif->mac_id);
1713698478c4SSara Sharon 	struct ieee80211_vif *vif;
1714698478c4SSara Sharon 	struct iwl_mvm_vif *mvmvif;
171586e177d8SGregory Greenman 
1716698478c4SSara Sharon 	IWL_DEBUG_INFO(mvm, "Probe response data notif: noa %d, csa %d\n",
1717698478c4SSara Sharon 		       notif->noa_active, notif->csa_counter);
1718698478c4SSara Sharon 
1719698478c4SSara Sharon 	vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, false);
1720698478c4SSara Sharon 	if (!vif)
1721698478c4SSara Sharon 		return;
1722698478c4SSara Sharon 
1723698478c4SSara Sharon 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
1724698478c4SSara Sharon 
172586e177d8SGregory Greenman 	new_data = kzalloc(sizeof(*new_data), GFP_KERNEL);
172686e177d8SGregory Greenman 	if (!new_data)
172786e177d8SGregory Greenman 		return;
172886e177d8SGregory Greenman 
172986e177d8SGregory Greenman 	memcpy(&new_data->notif, notif, sizeof(new_data->notif));
173086e177d8SGregory Greenman 
173186e177d8SGregory Greenman 	/* noa_attr contains 1 reserved byte, need to substruct it */
173286e177d8SGregory Greenman 	new_data->noa_len = sizeof(struct ieee80211_vendor_ie) +
173386e177d8SGregory Greenman 			    sizeof(new_data->notif.noa_attr) - 1;
173486e177d8SGregory Greenman 
173586e177d8SGregory Greenman 	/*
173686e177d8SGregory Greenman 	 * If it's a one time NoA, only one descriptor is needed,
173786e177d8SGregory Greenman 	 * adjust the length according to len_low.
173886e177d8SGregory Greenman 	 */
173986e177d8SGregory Greenman 	if (new_data->notif.noa_attr.len_low ==
174086e177d8SGregory Greenman 	    sizeof(struct ieee80211_p2p_noa_desc) + 2)
174186e177d8SGregory Greenman 		new_data->noa_len -= sizeof(struct ieee80211_p2p_noa_desc);
174286e177d8SGregory Greenman 
1743650cadb7SGregory Greenman 	old_data = rcu_dereference_protected(mvmvif->deflink.probe_resp_data,
174486e177d8SGregory Greenman 					     lockdep_is_held(&mvmvif->mvm->mutex));
1745650cadb7SGregory Greenman 	rcu_assign_pointer(mvmvif->deflink.probe_resp_data, new_data);
174686e177d8SGregory Greenman 
174786e177d8SGregory Greenman 	if (old_data)
174886e177d8SGregory Greenman 		kfree_rcu(old_data, rcu_head);
174986e177d8SGregory Greenman 
175086e177d8SGregory Greenman 	if (notif->csa_counter != IWL_PROBE_RESP_DATA_NO_CSA &&
175186e177d8SGregory Greenman 	    notif->csa_counter >= 1)
17528552a434SJohn Crispin 		ieee80211_beacon_set_cntdwn(vif, notif->csa_counter);
175386e177d8SGregory Greenman }
175486e177d8SGregory Greenman 
17556905eb1cSNathan Errera void iwl_mvm_channel_switch_start_notif(struct iwl_mvm *mvm,
1756d3a108a4SAndrei Otcheretianski 					struct iwl_rx_cmd_buffer *rxb)
1757d3a108a4SAndrei Otcheretianski {
1758d3a108a4SAndrei Otcheretianski 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
175974a10252SSara Sharon 	struct ieee80211_vif *csa_vif, *vif;
1760fa53608bSGregory Greenman 	struct iwl_mvm_vif *mvmvif, *csa_mvmvif;
1761fa53608bSGregory Greenman 	u32 id_n_color, csa_id;
1762fa53608bSGregory Greenman 	/* save mac_id or link_id to use later to cancel csa if needed */
1763fa53608bSGregory Greenman 	u32 id;
1764fa53608bSGregory Greenman 	u8 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
1765fa53608bSGregory Greenman 					       CHANNEL_SWITCH_START_NOTIF, 0);
1766fa53608bSGregory Greenman 
1767fa53608bSGregory Greenman 	rcu_read_lock();
1768fa53608bSGregory Greenman 
1769fa53608bSGregory Greenman 	if (notif_ver < 3) {
1770fa53608bSGregory Greenman 		struct iwl_channel_switch_start_notif_v1 *notif = (void *)pkt->data;
1771fa53608bSGregory Greenman 		u32 mac_id;
1772d3a108a4SAndrei Otcheretianski 
177374a10252SSara Sharon 		id_n_color = le32_to_cpu(notif->id_and_color);
177474a10252SSara Sharon 		mac_id = id_n_color & FW_CTXT_ID_MSK;
1775d3a108a4SAndrei Otcheretianski 
1776fa53608bSGregory Greenman 		vif = iwl_mvm_rcu_dereference_vif_id(mvm, mac_id, true);
1777fa53608bSGregory Greenman 		if (!vif)
1778fa53608bSGregory Greenman 			goto out_unlock;
177974a10252SSara Sharon 
1780fa53608bSGregory Greenman 		id = mac_id;
1781fa53608bSGregory Greenman 	} else {
1782fa53608bSGregory Greenman 		struct iwl_channel_switch_start_notif *notif = (void *)pkt->data;
1783fa53608bSGregory Greenman 		u32 link_id = le32_to_cpu(notif->link_id);
1784fa53608bSGregory Greenman 		struct ieee80211_bss_conf *bss_conf =
1785fa53608bSGregory Greenman 			iwl_mvm_rcu_fw_link_id_to_link_conf(mvm, link_id, true);
1786fa53608bSGregory Greenman 
1787fa53608bSGregory Greenman 		if (!bss_conf)
1788fa53608bSGregory Greenman 			goto out_unlock;
1789fa53608bSGregory Greenman 
1790fa53608bSGregory Greenman 		id = link_id;
1791fa53608bSGregory Greenman 		vif = bss_conf->vif;
1792fa53608bSGregory Greenman 	}
1793fa53608bSGregory Greenman 
1794f6780614SSara Sharon 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
1795fa53608bSGregory Greenman 	if (notif_ver >= 3)
1796fa53608bSGregory Greenman 		id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
179774a10252SSara Sharon 
179874a10252SSara Sharon 	switch (vif->type) {
179974a10252SSara Sharon 	case NL80211_IFTYPE_AP:
1800d3a108a4SAndrei Otcheretianski 		csa_vif = rcu_dereference(mvm->csa_vif);
1801d0a9123eSJohannes Berg 		if (WARN_ON(!csa_vif || !csa_vif->bss_conf.csa_active ||
180274a10252SSara Sharon 			    csa_vif != vif))
1803d3a108a4SAndrei Otcheretianski 			goto out_unlock;
1804d3a108a4SAndrei Otcheretianski 
1805fa53608bSGregory Greenman 		csa_mvmvif = iwl_mvm_vif_from_mac80211(csa_vif);
1806fa53608bSGregory Greenman 		csa_id = FW_CMD_ID_AND_COLOR(csa_mvmvif->id, csa_mvmvif->color);
180774a10252SSara Sharon 		if (WARN(csa_id != id_n_color,
1808d3a108a4SAndrei Otcheretianski 			 "channel switch noa notification on unexpected vif (csa_vif=%d, notif=%d)",
180974a10252SSara Sharon 			 csa_id, id_n_color))
1810d3a108a4SAndrei Otcheretianski 			goto out_unlock;
1811d3a108a4SAndrei Otcheretianski 
1812d3a108a4SAndrei Otcheretianski 		IWL_DEBUG_INFO(mvm, "Channel Switch Started Notification\n");
1813d3a108a4SAndrei Otcheretianski 
181486bbb1e1SJohannes Berg 		schedule_delayed_work(&mvm->cs_tx_unblock_dwork,
1815d3a108a4SAndrei Otcheretianski 				      msecs_to_jiffies(IWL_MVM_CS_UNBLOCK_TX_TIMEOUT *
1816d3a108a4SAndrei Otcheretianski 						       csa_vif->bss_conf.beacon_int));
1817d3a108a4SAndrei Otcheretianski 
1818d3a108a4SAndrei Otcheretianski 		ieee80211_csa_finish(csa_vif);
1819d3a108a4SAndrei Otcheretianski 
1820d3a108a4SAndrei Otcheretianski 		rcu_read_unlock();
1821d3a108a4SAndrei Otcheretianski 
1822d3a108a4SAndrei Otcheretianski 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
1823d3a108a4SAndrei Otcheretianski 		return;
182474a10252SSara Sharon 	case NL80211_IFTYPE_STATION:
1825ad12b231SNathan Errera 		/*
1826ad12b231SNathan Errera 		 * if we don't know about an ongoing channel switch,
1827ad12b231SNathan Errera 		 * make sure FW cancels it
1828ad12b231SNathan Errera 		 */
1829ad12b231SNathan Errera 		if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
1830ad12b231SNathan Errera 					    CHANNEL_SWITCH_ERROR_NOTIF,
1831d0a9123eSJohannes Berg 					    0) && !vif->bss_conf.csa_active) {
1832ad12b231SNathan Errera 			IWL_DEBUG_INFO(mvm, "Channel Switch was canceled\n");
1833fa53608bSGregory Greenman 			iwl_mvm_cancel_channel_switch(mvm, vif, id);
1834ad12b231SNathan Errera 			break;
1835ad12b231SNathan Errera 		}
1836ad12b231SNathan Errera 
183774a10252SSara Sharon 		iwl_mvm_csa_client_absent(mvm, vif);
183811af74adSAndrei Otcheretianski 		cancel_delayed_work(&mvmvif->csa_work);
183974a10252SSara Sharon 		ieee80211_chswitch_done(vif, true);
184074a10252SSara Sharon 		break;
184174a10252SSara Sharon 	default:
184274a10252SSara Sharon 		/* should never happen */
184374a10252SSara Sharon 		WARN_ON_ONCE(1);
184474a10252SSara Sharon 		break;
184574a10252SSara Sharon 	}
1846d3a108a4SAndrei Otcheretianski out_unlock:
1847d3a108a4SAndrei Otcheretianski 	rcu_read_unlock();
1848d3a108a4SAndrei Otcheretianski }
1849449a29d0SLior Cohen 
1850ad12b231SNathan Errera void iwl_mvm_channel_switch_error_notif(struct iwl_mvm *mvm,
1851ad12b231SNathan Errera 					struct iwl_rx_cmd_buffer *rxb)
1852ad12b231SNathan Errera {
1853ad12b231SNathan Errera 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1854ad12b231SNathan Errera 	struct iwl_channel_switch_error_notif *notif = (void *)pkt->data;
1855ad12b231SNathan Errera 	struct ieee80211_vif *vif;
1856fa53608bSGregory Greenman 	u32 id = le32_to_cpu(notif->link_id);
1857ad12b231SNathan Errera 	u32 csa_err_mask = le32_to_cpu(notif->csa_err_mask);
1858ad12b231SNathan Errera 
1859ad12b231SNathan Errera 	rcu_read_lock();
1860ad12b231SNathan Errera 	vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);
1861ad12b231SNathan Errera 	if (!vif) {
1862ad12b231SNathan Errera 		rcu_read_unlock();
1863ad12b231SNathan Errera 		return;
1864ad12b231SNathan Errera 	}
1865ad12b231SNathan Errera 
1866fa53608bSGregory Greenman 	IWL_DEBUG_INFO(mvm, "FW reports CSA error: id=%u, csa_err_mask=%u\n",
1867ad12b231SNathan Errera 		       id, csa_err_mask);
1868ad12b231SNathan Errera 	if (csa_err_mask & (CS_ERR_COUNT_ERROR |
1869ad12b231SNathan Errera 			    CS_ERR_LONG_DELAY_AFTER_CS |
1870ad12b231SNathan Errera 			    CS_ERR_TX_BLOCK_TIMER_EXPIRED))
1871ad12b231SNathan Errera 		ieee80211_channel_switch_disconnect(vif, true);
1872ad12b231SNathan Errera 	rcu_read_unlock();
1873ad12b231SNathan Errera }
1874ad12b231SNathan Errera 
1875449a29d0SLior Cohen void iwl_mvm_rx_missed_vap_notif(struct iwl_mvm *mvm,
1876449a29d0SLior Cohen 				 struct iwl_rx_cmd_buffer *rxb)
1877449a29d0SLior Cohen {
1878449a29d0SLior Cohen 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1879449a29d0SLior Cohen 	struct iwl_missed_vap_notif *mb = (void *)pkt->data;
1880449a29d0SLior Cohen 	struct ieee80211_vif *vif;
1881449a29d0SLior Cohen 	u32 id = le32_to_cpu(mb->mac_id);
1882449a29d0SLior Cohen 
1883449a29d0SLior Cohen 	IWL_DEBUG_INFO(mvm,
1884449a29d0SLior Cohen 		       "missed_vap notify mac_id=%u, num_beacon_intervals_elapsed=%u, profile_periodicity=%u\n",
1885449a29d0SLior Cohen 		       le32_to_cpu(mb->mac_id),
1886449a29d0SLior Cohen 		       mb->num_beacon_intervals_elapsed,
1887449a29d0SLior Cohen 		       mb->profile_periodicity);
1888449a29d0SLior Cohen 
1889449a29d0SLior Cohen 	rcu_read_lock();
1890449a29d0SLior Cohen 
1891449a29d0SLior Cohen 	vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);
1892449a29d0SLior Cohen 	if (vif)
1893449a29d0SLior Cohen 		iwl_mvm_connection_loss(mvm, vif, "missed vap beacon");
1894449a29d0SLior Cohen 
1895449a29d0SLior Cohen 	rcu_read_unlock();
1896449a29d0SLior Cohen }
1897