18e99ea8dSJohannes Berg // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
28e99ea8dSJohannes Berg /*
351e073c2SJohannes Berg  * Copyright (C) 2012-2014, 2018-2022 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);
473af6d168fSMiri Korenblit 	int i;
474af6d168fSMiri Korenblit 
475af6d168fSMiri Korenblit 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
476af6d168fSMiri Korenblit 		u8 txf = iwl_mvm_mac_ac_to_tx_fifo(mvm, i);
477af6d168fSMiri Korenblit 		u8 ucode_ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);
478af6d168fSMiri Korenblit 
479af6d168fSMiri Korenblit 		ac[ucode_ac].cw_min =
480650cadb7SGregory Greenman 			cpu_to_le16(mvmvif->deflink.queue_params[i].cw_min);
481af6d168fSMiri Korenblit 		ac[ucode_ac].cw_max =
482650cadb7SGregory Greenman 			cpu_to_le16(mvmvif->deflink.queue_params[i].cw_max);
483af6d168fSMiri Korenblit 		ac[ucode_ac].edca_txop =
484650cadb7SGregory Greenman 			cpu_to_le16(mvmvif->deflink.queue_params[i].txop * 32);
485650cadb7SGregory Greenman 		ac[ucode_ac].aifsn = mvmvif->deflink.queue_params[i].aifs;
486af6d168fSMiri Korenblit 		ac[ucode_ac].fifos_mask = BIT(txf);
487af6d168fSMiri Korenblit 	}
488af6d168fSMiri Korenblit 
489cacc1d42SGregory Greenman 	if (link_conf->qos)
490af6d168fSMiri Korenblit 		*qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
491af6d168fSMiri Korenblit 
492cacc1d42SGregory Greenman 	if (link_conf->chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
493af6d168fSMiri Korenblit 		*qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
494af6d168fSMiri Korenblit }
495af6d168fSMiri Korenblit 
4969be162a7SMiri Korenblit int iwl_mvm_get_mac_type(struct ieee80211_vif *vif)
497834f920eSMukesh Sisodiya {
498834f920eSMukesh Sisodiya 	u32 mac_type = FW_MAC_TYPE_BSS_STA;
499834f920eSMukesh Sisodiya 
500834f920eSMukesh Sisodiya 	switch (vif->type) {
501834f920eSMukesh Sisodiya 	case NL80211_IFTYPE_STATION:
502834f920eSMukesh Sisodiya 		if (vif->p2p)
503834f920eSMukesh Sisodiya 			mac_type = FW_MAC_TYPE_P2P_STA;
504834f920eSMukesh Sisodiya 		else
505834f920eSMukesh Sisodiya 			mac_type = FW_MAC_TYPE_BSS_STA;
506834f920eSMukesh Sisodiya 		break;
507834f920eSMukesh Sisodiya 	case NL80211_IFTYPE_AP:
508834f920eSMukesh Sisodiya 		mac_type = FW_MAC_TYPE_GO;
509834f920eSMukesh Sisodiya 		break;
510834f920eSMukesh Sisodiya 	case NL80211_IFTYPE_MONITOR:
511834f920eSMukesh Sisodiya 		mac_type = FW_MAC_TYPE_LISTENER;
512834f920eSMukesh Sisodiya 		break;
513834f920eSMukesh Sisodiya 	case NL80211_IFTYPE_P2P_DEVICE:
514834f920eSMukesh Sisodiya 		mac_type = FW_MAC_TYPE_P2P_DEVICE;
515834f920eSMukesh Sisodiya 		break;
516834f920eSMukesh Sisodiya 	case NL80211_IFTYPE_ADHOC:
517834f920eSMukesh Sisodiya 		mac_type = FW_MAC_TYPE_IBSS;
518834f920eSMukesh Sisodiya 		break;
519834f920eSMukesh Sisodiya 	default:
520834f920eSMukesh Sisodiya 		WARN_ON_ONCE(1);
521834f920eSMukesh Sisodiya 	}
522834f920eSMukesh Sisodiya 	return mac_type;
523834f920eSMukesh Sisodiya }
524834f920eSMukesh Sisodiya 
525e705c121SKalle Valo static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
526e705c121SKalle Valo 					struct ieee80211_vif *vif,
527e705c121SKalle Valo 					struct iwl_mac_ctx_cmd *cmd,
528e705c121SKalle Valo 					const u8 *bssid_override,
529e705c121SKalle Valo 					u32 action)
530e705c121SKalle Valo {
531e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
532e705c121SKalle Valo 	const u8 *bssid = bssid_override ?: vif->bss_conf.bssid;
533af6d168fSMiri Korenblit 	u32 ht_flag;
534e705c121SKalle Valo 
535e705c121SKalle Valo 	cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
536e705c121SKalle Valo 							    mvmvif->color));
537e705c121SKalle Valo 	cmd->action = cpu_to_le32(action);
538834f920eSMukesh Sisodiya 	cmd->mac_type = cpu_to_le32(iwl_mvm_get_mac_type(vif));
539e705c121SKalle Valo 
540e705c121SKalle Valo 	cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
541e705c121SKalle Valo 
542e705c121SKalle Valo 	memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
543e705c121SKalle Valo 
544e705c121SKalle Valo 	if (bssid)
545e705c121SKalle Valo 		memcpy(cmd->bssid_addr, bssid, ETH_ALEN);
546e705c121SKalle Valo 	else
547e705c121SKalle Valo 		eth_broadcast_addr(cmd->bssid_addr);
548e705c121SKalle Valo 
549cacc1d42SGregory Greenman 	iwl_mvm_set_fw_basic_rates(mvm, vif, &vif->bss_conf, &cmd->cck_rates,
550af6d168fSMiri Korenblit 				   &cmd->ofdm_rates);
551e705c121SKalle Valo 
552e705c121SKalle Valo 	cmd->cck_short_preamble =
553e705c121SKalle Valo 		cpu_to_le32(vif->bss_conf.use_short_preamble ?
554e705c121SKalle Valo 			    MAC_FLG_SHORT_PREAMBLE : 0);
555e705c121SKalle Valo 	cmd->short_slot =
556e705c121SKalle Valo 		cpu_to_le32(vif->bss_conf.use_short_slot ?
557e705c121SKalle Valo 			    MAC_FLG_SHORT_SLOT : 0);
558e705c121SKalle Valo 
55950f56044SIlan Peer 	cmd->filter_flags = 0;
560bd6f5bd7SAyala Beker 
561cacc1d42SGregory Greenman 	iwl_mvm_set_fw_qos_params(mvm, vif, &vif->bss_conf, &cmd->ac[0],
562cacc1d42SGregory Greenman 				  &cmd->qos_flags);
563e705c121SKalle Valo 
564af6d168fSMiri Korenblit 	/* The fw does not distinguish between ht and fat */
565af6d168fSMiri Korenblit 	ht_flag = MAC_PROT_FLG_HT_PROT | MAC_PROT_FLG_FAT_PROT;
566cacc1d42SGregory Greenman 	iwl_mvm_set_fw_protection_flags(mvm, vif, &vif->bss_conf,
567cacc1d42SGregory Greenman 					&cmd->protection_flags,
568af6d168fSMiri Korenblit 					ht_flag, MAC_PROT_FLG_TGG_PROTECT);
569e705c121SKalle Valo }
570e705c121SKalle Valo 
571e705c121SKalle Valo static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
572e705c121SKalle Valo 				     struct iwl_mac_ctx_cmd *cmd)
573e705c121SKalle Valo {
574e705c121SKalle Valo 	int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
575e705c121SKalle Valo 				       sizeof(*cmd), cmd);
576e705c121SKalle Valo 	if (ret)
577af6d168fSMiri Korenblit 		IWL_ERR(mvm, "Failed to send MAC_CONTEXT_CMD (action:%d): %d\n",
578e705c121SKalle Valo 			le32_to_cpu(cmd->action), ret);
579e705c121SKalle Valo 	return ret;
580e705c121SKalle Valo }
581e705c121SKalle Valo 
58255eb1c5fSMiri Korenblit void iwl_mvm_set_fw_dtim_tbtt(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
583cacc1d42SGregory Greenman 			      struct ieee80211_bss_conf *link_conf,
584af6d168fSMiri Korenblit 			      __le64 *dtim_tsf, __le32 *dtim_time,
585af6d168fSMiri Korenblit 			      __le32 *assoc_beacon_arrive_time)
586e705c121SKalle Valo {
587e705c121SKalle Valo 	u32 dtim_offs;
588e705c121SKalle Valo 
589e705c121SKalle Valo 	/*
590e705c121SKalle Valo 	 * The DTIM count counts down, so when it is N that means N
591e705c121SKalle Valo 	 * more beacon intervals happen until the DTIM TBTT. Therefore
592e705c121SKalle Valo 	 * add this to the current time. If that ends up being in the
593e705c121SKalle Valo 	 * future, the firmware will handle it.
594e705c121SKalle Valo 	 *
595e705c121SKalle Valo 	 * Also note that the system_timestamp (which we get here as
596e705c121SKalle Valo 	 * "sync_device_ts") and TSF timestamp aren't at exactly the
597e705c121SKalle Valo 	 * same offset in the frame -- the TSF is at the first symbol
598e705c121SKalle Valo 	 * of the TSF, the system timestamp is at signal acquisition
599e705c121SKalle Valo 	 * time. This means there's an offset between them of at most
600e705c121SKalle Valo 	 * a few hundred microseconds (24 * 8 bits + PLCP time gives
601e705c121SKalle Valo 	 * 384us in the longest case), this is currently not relevant
602e705c121SKalle Valo 	 * as the firmware wakes up around 2ms before the TBTT.
603e705c121SKalle Valo 	 */
604cacc1d42SGregory Greenman 	dtim_offs = link_conf->sync_dtim_count *
605cacc1d42SGregory Greenman 			link_conf->beacon_int;
606e705c121SKalle Valo 	/* convert TU to usecs */
607e705c121SKalle Valo 	dtim_offs *= 1024;
608e705c121SKalle Valo 
609af6d168fSMiri Korenblit 	*dtim_tsf =
610cacc1d42SGregory Greenman 		cpu_to_le64(link_conf->sync_tsf + dtim_offs);
611af6d168fSMiri Korenblit 	*dtim_time =
612cacc1d42SGregory Greenman 		cpu_to_le32(link_conf->sync_device_ts + dtim_offs);
613af6d168fSMiri Korenblit 	*assoc_beacon_arrive_time =
614cacc1d42SGregory Greenman 		cpu_to_le32(link_conf->sync_device_ts);
615e705c121SKalle Valo 
616e705c121SKalle Valo 	IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
617af6d168fSMiri Korenblit 		       le64_to_cpu(*dtim_tsf),
618af6d168fSMiri Korenblit 		       le32_to_cpu(*dtim_time),
619e705c121SKalle Valo 		       dtim_offs);
620af6d168fSMiri Korenblit }
621af6d168fSMiri Korenblit 
6229be162a7SMiri Korenblit __le32 iwl_mvm_mac_ctxt_cmd_p2p_sta_get_oppps_ctwin(struct iwl_mvm *mvm,
623af6d168fSMiri Korenblit 						    struct ieee80211_vif *vif)
624af6d168fSMiri Korenblit {
625af6d168fSMiri Korenblit 	struct ieee80211_p2p_noa_attr *noa =
626af6d168fSMiri Korenblit 		&vif->bss_conf.p2p_noa_attr;
627af6d168fSMiri Korenblit 
628af6d168fSMiri Korenblit 	return cpu_to_le32(noa->oppps_ctwindow &
629af6d168fSMiri Korenblit 			IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
630af6d168fSMiri Korenblit }
631af6d168fSMiri Korenblit 
6329be162a7SMiri Korenblit __le32 iwl_mvm_mac_ctxt_cmd_sta_get_twt_policy(struct iwl_mvm *mvm,
633af6d168fSMiri Korenblit 					       struct ieee80211_vif *vif)
634af6d168fSMiri Korenblit {
635af6d168fSMiri Korenblit 	__le32 twt_policy = cpu_to_le32(0);
636af6d168fSMiri Korenblit 
637af6d168fSMiri Korenblit 	if (vif->bss_conf.twt_requester && IWL_MVM_USE_TWT)
638af6d168fSMiri Korenblit 		twt_policy |= cpu_to_le32(TWT_SUPPORTED);
639af6d168fSMiri Korenblit 	if (vif->bss_conf.twt_protected)
640af6d168fSMiri Korenblit 		twt_policy |= cpu_to_le32(PROTECTED_TWT_SUPPORTED);
641af6d168fSMiri Korenblit 	if (vif->bss_conf.twt_broadcast)
642af6d168fSMiri Korenblit 		twt_policy |= cpu_to_le32(BROADCAST_TWT_SUPPORTED);
643af6d168fSMiri Korenblit 
644af6d168fSMiri Korenblit 	return twt_policy;
645af6d168fSMiri Korenblit }
646af6d168fSMiri Korenblit 
647af6d168fSMiri Korenblit static int iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm *mvm,
648af6d168fSMiri Korenblit 				    struct ieee80211_vif *vif,
649af6d168fSMiri Korenblit 				    u32 action, bool force_assoc_off,
650af6d168fSMiri Korenblit 				    const u8 *bssid_override)
651af6d168fSMiri Korenblit {
652af6d168fSMiri Korenblit 	struct iwl_mac_ctx_cmd cmd = {};
653af6d168fSMiri Korenblit 	struct iwl_mac_data_sta *ctxt_sta;
654af6d168fSMiri Korenblit 
655af6d168fSMiri Korenblit 	WARN_ON(vif->type != NL80211_IFTYPE_STATION);
656af6d168fSMiri Korenblit 
657af6d168fSMiri Korenblit 	/* Fill the common data for all mac context types */
658af6d168fSMiri Korenblit 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, bssid_override, action);
659af6d168fSMiri Korenblit 
660af6d168fSMiri Korenblit 	/*
661af6d168fSMiri Korenblit 	 * We always want to hear MCAST frames, if we're not authorized yet,
662af6d168fSMiri Korenblit 	 * we'll drop them.
663af6d168fSMiri Korenblit 	 */
664af6d168fSMiri Korenblit 	cmd.filter_flags |= cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
665af6d168fSMiri Korenblit 
666af6d168fSMiri Korenblit 	if (vif->p2p) {
667af6d168fSMiri Korenblit 		cmd.p2p_sta.ctwin =
668af6d168fSMiri Korenblit 			iwl_mvm_mac_ctxt_cmd_p2p_sta_get_oppps_ctwin(mvm, vif);
669af6d168fSMiri Korenblit 
670af6d168fSMiri Korenblit 		ctxt_sta = &cmd.p2p_sta.sta;
671af6d168fSMiri Korenblit 	} else {
672af6d168fSMiri Korenblit 		ctxt_sta = &cmd.sta;
673af6d168fSMiri Korenblit 	}
674af6d168fSMiri Korenblit 
675af6d168fSMiri Korenblit 	/* We need the dtim_period to set the MAC as associated */
676af6d168fSMiri Korenblit 	if (vif->cfg.assoc && vif->bss_conf.dtim_period &&
677af6d168fSMiri Korenblit 	    !force_assoc_off) {
678af6d168fSMiri Korenblit 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
679af6d168fSMiri Korenblit 
680cacc1d42SGregory Greenman 		iwl_mvm_set_fw_dtim_tbtt(mvm, vif, &vif->bss_conf,
681cacc1d42SGregory Greenman 					 &ctxt_sta->dtim_tsf,
682af6d168fSMiri Korenblit 					 &ctxt_sta->dtim_time,
683af6d168fSMiri Korenblit 					 &ctxt_sta->assoc_beacon_arrive_time);
684e705c121SKalle Valo 
685e705c121SKalle Valo 		ctxt_sta->is_assoc = cpu_to_le32(1);
68650f56044SIlan Peer 
6878b75858cSJohannes Berg 		if (!mvmvif->authorized &&
6888b75858cSJohannes Berg 		    fw_has_capa(&mvm->fw->ucode_capa,
6898b75858cSJohannes Berg 				IWL_UCODE_TLV_CAPA_COEX_HIGH_PRIO))
6908b75858cSJohannes Berg 			ctxt_sta->data_policy |=
6918b75858cSJohannes Berg 				cpu_to_le32(COEX_HIGH_PRIORITY_ENABLE);
692e705c121SKalle Valo 	} else {
693e705c121SKalle Valo 		ctxt_sta->is_assoc = cpu_to_le32(0);
694e705c121SKalle Valo 
695e705c121SKalle Valo 		/* Allow beacons to pass through as long as we are not
696e705c121SKalle Valo 		 * associated, or we do not have dtim period information.
697e705c121SKalle Valo 		 */
698e705c121SKalle Valo 		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
699e705c121SKalle Valo 	}
700e705c121SKalle Valo 
701e705c121SKalle Valo 	ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
702e705c121SKalle Valo 	ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
703e705c121SKalle Valo 					      vif->bss_conf.dtim_period);
704e705c121SKalle Valo 
705e705c121SKalle Valo 	ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
706f276e20bSJohannes Berg 	ctxt_sta->assoc_id = cpu_to_le32(vif->cfg.aid);
707e705c121SKalle Valo 
708f276e20bSJohannes Berg 	if (vif->probe_req_reg && vif->cfg.assoc && vif->p2p)
709e705c121SKalle Valo 		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
710e705c121SKalle Valo 
7119394662aSLiad Kaufman 	if (vif->bss_conf.he_support && !iwlwifi_mod_params.disable_11ax) {
712514c3069SLuca Coelho 		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_11AX);
713659ac93dSShaul Triebitz 		ctxt_sta->data_policy |=
714af6d168fSMiri Korenblit 			iwl_mvm_mac_ctxt_cmd_sta_get_twt_policy(mvm, vif);
715659ac93dSShaul Triebitz 	}
716d91d9b94SEmmanuel Grumbach 
717514c3069SLuca Coelho 
718e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
719e705c121SKalle Valo }
720e705c121SKalle Valo 
721e705c121SKalle Valo static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
722e705c121SKalle Valo 					 struct ieee80211_vif *vif,
723e705c121SKalle Valo 					 u32 action)
724e705c121SKalle Valo {
725e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd = {};
726b85f7ebbSJohannes Berg 	u32 tfd_queue_msk = 0;
727c8f54701SJohannes Berg 	int ret;
728e705c121SKalle Valo 
729e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
730e705c121SKalle Valo 
731e705c121SKalle Valo 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
732e705c121SKalle Valo 
733e705c121SKalle Valo 	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
734e705c121SKalle Valo 				       MAC_FILTER_IN_CONTROL_AND_MGMT |
735e705c121SKalle Valo 				       MAC_FILTER_IN_BEACON |
736e705c121SKalle Valo 				       MAC_FILTER_IN_PROBE_REQUEST |
73750f56044SIlan Peer 				       MAC_FILTER_IN_CRC32 |
73850f56044SIlan Peer 				       MAC_FILTER_ACCEPT_GRP);
739e705c121SKalle Valo 	ieee80211_hw_set(mvm->hw, RX_INCLUDES_FCS);
740e705c121SKalle Valo 
741b85f7ebbSJohannes Berg 	/*
742b85f7ebbSJohannes Berg 	 * the queue mask is only relevant for old TX API, and
743b85f7ebbSJohannes Berg 	 * mvm->snif_queue isn't set here (it's still set to
744b85f7ebbSJohannes Berg 	 * IWL_MVM_INVALID_QUEUE so the BIT() of it is UB)
745b85f7ebbSJohannes Berg 	 */
746b85f7ebbSJohannes Berg 	if (!iwl_mvm_has_new_tx_api(mvm))
747b85f7ebbSJohannes Berg 		tfd_queue_msk = BIT(mvm->snif_queue);
748b85f7ebbSJohannes Berg 
7490e39eb03SChaya Rachel Ivgi 	/* Allocate sniffer station */
7500e39eb03SChaya Rachel Ivgi 	ret = iwl_mvm_allocate_int_sta(mvm, &mvm->snif_sta, tfd_queue_msk,
751ced19f26SSara Sharon 				       vif->type, IWL_STA_GENERAL_PURPOSE);
7520e39eb03SChaya Rachel Ivgi 	if (ret)
7530e39eb03SChaya Rachel Ivgi 		return ret;
7540e39eb03SChaya Rachel Ivgi 
755e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
756e705c121SKalle Valo }
757e705c121SKalle Valo 
758e705c121SKalle Valo static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
759e705c121SKalle Valo 				     struct ieee80211_vif *vif,
760e705c121SKalle Valo 				     u32 action)
761e705c121SKalle Valo {
762e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
763e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd = {};
764e705c121SKalle Valo 
765e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
766e705c121SKalle Valo 
767e705c121SKalle Valo 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
768e705c121SKalle Valo 
769e705c121SKalle Valo 	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
77050f56044SIlan Peer 				       MAC_FILTER_IN_PROBE_REQUEST |
77150f56044SIlan Peer 				       MAC_FILTER_ACCEPT_GRP);
772e705c121SKalle Valo 
773e705c121SKalle Valo 	/* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */
774e705c121SKalle Valo 	cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
775e705c121SKalle Valo 
776e705c121SKalle Valo 	/* TODO: Assumes that the beacon id == mac context id */
777e705c121SKalle Valo 	cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
778e705c121SKalle Valo 
779e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
780e705c121SKalle Valo }
781e705c121SKalle Valo 
782e705c121SKalle Valo struct iwl_mvm_go_iterator_data {
783e705c121SKalle Valo 	bool go_active;
784e705c121SKalle Valo };
785e705c121SKalle Valo 
786e705c121SKalle Valo static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
787e705c121SKalle Valo {
788e705c121SKalle Valo 	struct iwl_mvm_go_iterator_data *data = _data;
789e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
790e705c121SKalle Valo 
791e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
792e705c121SKalle Valo 	    mvmvif->ap_ibss_active)
793e705c121SKalle Valo 		data->go_active = true;
794e705c121SKalle Valo }
795e705c121SKalle Valo 
7969be162a7SMiri Korenblit __le32 iwl_mac_ctxt_p2p_dev_has_extended_disc(struct iwl_mvm *mvm,
797af6d168fSMiri Korenblit 					      struct ieee80211_vif *vif)
798e705c121SKalle Valo {
799e705c121SKalle Valo 	struct iwl_mvm_go_iterator_data data = {};
800e705c121SKalle Valo 
801e705c121SKalle Valo 	/*
802e705c121SKalle Valo 	 * This flag should be set to true when the P2P Device is
803e705c121SKalle Valo 	 * discoverable and there is at least another active P2P GO. Settings
804e705c121SKalle Valo 	 * this flag will allow the P2P Device to be discoverable on other
805e705c121SKalle Valo 	 * channels in addition to its listen channel.
806e705c121SKalle Valo 	 * Note that this flag should not be set in other cases as it opens the
807e705c121SKalle Valo 	 * Rx filters on all MAC and increases the number of interrupts.
808e705c121SKalle Valo 	 */
809e705c121SKalle Valo 	ieee80211_iterate_active_interfaces_atomic(
810e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
811e705c121SKalle Valo 		iwl_mvm_go_iterator, &data);
812e705c121SKalle Valo 
813af6d168fSMiri Korenblit 	return cpu_to_le32(data.go_active ? 1 : 0);
814af6d168fSMiri Korenblit }
815af6d168fSMiri Korenblit 
816af6d168fSMiri Korenblit static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
817af6d168fSMiri Korenblit 					   struct ieee80211_vif *vif,
818af6d168fSMiri Korenblit 					   u32 action)
819af6d168fSMiri Korenblit {
820af6d168fSMiri Korenblit 	struct iwl_mac_ctx_cmd cmd = {};
821af6d168fSMiri Korenblit 
822af6d168fSMiri Korenblit 	WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
823af6d168fSMiri Korenblit 
824af6d168fSMiri Korenblit 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
825af6d168fSMiri Korenblit 
826af6d168fSMiri Korenblit 	cmd.p2p_dev.is_disc_extended =
827af6d168fSMiri Korenblit 		iwl_mac_ctxt_p2p_dev_has_extended_disc(mvm, vif);
828af6d168fSMiri Korenblit 
829af6d168fSMiri Korenblit 	/* Override the filter flags to accept only probe requests */
830af6d168fSMiri Korenblit 	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
831af6d168fSMiri Korenblit 
832e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
833e705c121SKalle Valo }
834e705c121SKalle Valo 
835138664a3SSara Sharon void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
8368364fbb4SSara Sharon 			      __le32 *tim_index, __le32 *tim_size,
837e705c121SKalle Valo 			      u8 *beacon, u32 frame_size)
838e705c121SKalle Valo {
839e705c121SKalle Valo 	u32 tim_idx;
840e705c121SKalle Valo 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
841e705c121SKalle Valo 
842e705c121SKalle Valo 	/* The index is relative to frame start but we start looking at the
843e705c121SKalle Valo 	 * variable-length part of the beacon. */
844e705c121SKalle Valo 	tim_idx = mgmt->u.beacon.variable - beacon;
845e705c121SKalle Valo 
846e705c121SKalle Valo 	/* Parse variable-length elements of beacon to find WLAN_EID_TIM */
847e705c121SKalle Valo 	while ((tim_idx < (frame_size - 2)) &&
848e705c121SKalle Valo 			(beacon[tim_idx] != WLAN_EID_TIM))
849e705c121SKalle Valo 		tim_idx += beacon[tim_idx+1] + 2;
850e705c121SKalle Valo 
851e705c121SKalle Valo 	/* If TIM field was found, set variables */
852e705c121SKalle Valo 	if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
8538364fbb4SSara Sharon 		*tim_index = cpu_to_le32(tim_idx);
8548364fbb4SSara Sharon 		*tim_size = cpu_to_le32((u32)beacon[tim_idx + 1]);
855e705c121SKalle Valo 	} else {
856e705c121SKalle Valo 		IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
857e705c121SKalle Valo 	}
858e705c121SKalle Valo }
859e705c121SKalle Valo 
860d3a108a4SAndrei Otcheretianski static u32 iwl_mvm_find_ie_offset(u8 *beacon, u8 eid, u32 frame_size)
861d3a108a4SAndrei Otcheretianski {
862d3a108a4SAndrei Otcheretianski 	struct ieee80211_mgmt *mgmt = (void *)beacon;
863d3a108a4SAndrei Otcheretianski 	const u8 *ie;
864d3a108a4SAndrei Otcheretianski 
865d3a108a4SAndrei Otcheretianski 	if (WARN_ON_ONCE(frame_size <= (mgmt->u.beacon.variable - beacon)))
866d3a108a4SAndrei Otcheretianski 		return 0;
867d3a108a4SAndrei Otcheretianski 
868d3a108a4SAndrei Otcheretianski 	frame_size -= mgmt->u.beacon.variable - beacon;
869d3a108a4SAndrei Otcheretianski 
870d3a108a4SAndrei Otcheretianski 	ie = cfg80211_find_ie(eid, mgmt->u.beacon.variable, frame_size);
871d3a108a4SAndrei Otcheretianski 	if (!ie)
872d3a108a4SAndrei Otcheretianski 		return 0;
873d3a108a4SAndrei Otcheretianski 
874d3a108a4SAndrei Otcheretianski 	return ie - beacon;
875d3a108a4SAndrei Otcheretianski }
876d3a108a4SAndrei Otcheretianski 
8777f11d17fSIlan Peer u8 iwl_mvm_mac_ctxt_get_lowest_rate(struct iwl_mvm *mvm,
878ef2e7a51SIlan Peer 				    struct ieee80211_tx_info *info,
8796ca33f8bSHaim Dreyfuss 				    struct ieee80211_vif *vif)
8806ca33f8bSHaim Dreyfuss {
8810af637b5SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
882ef2e7a51SIlan Peer 	struct ieee80211_supported_band *sband;
883ef2e7a51SIlan Peer 	unsigned long basic = vif->bss_conf.basic_rates;
884ef2e7a51SIlan Peer 	u16 lowest_cck = IWL_RATE_COUNT, lowest_ofdm = IWL_RATE_COUNT;
8850af637b5SJohannes Berg 	u32 link_id = u32_get_bits(info->control.flags,
8860af637b5SJohannes Berg 				   IEEE80211_TX_CTRL_MLO_LINK);
8870af637b5SJohannes Berg 	u8 band = info->band;
8886ca33f8bSHaim Dreyfuss 	u8 rate;
889ef2e7a51SIlan Peer 	u32 i;
890ef2e7a51SIlan Peer 
8910af637b5SJohannes Berg 	if (link_id == IEEE80211_LINK_UNSPECIFIED && vif->valid_links) {
8920af637b5SJohannes Berg 		for (i = 0; i < ARRAY_SIZE(mvmvif->link); i++) {
8930af637b5SJohannes Berg 			if (!mvmvif->link[i])
8940af637b5SJohannes Berg 				continue;
8950af637b5SJohannes Berg 			/* shouldn't do this when >1 link is active */
8960af637b5SJohannes Berg 			WARN_ON_ONCE(link_id != IEEE80211_LINK_UNSPECIFIED);
8970af637b5SJohannes Berg 			link_id = i;
8980af637b5SJohannes Berg 		}
8990af637b5SJohannes Berg 	}
9000af637b5SJohannes Berg 
9010af637b5SJohannes Berg 	if (link_id < IEEE80211_LINK_UNSPECIFIED) {
9020af637b5SJohannes Berg 		struct ieee80211_bss_conf *link_conf;
9030af637b5SJohannes Berg 
9040af637b5SJohannes Berg 		rcu_read_lock();
9050af637b5SJohannes Berg 		link_conf = rcu_dereference(vif->link_conf[link_id]);
9060af637b5SJohannes Berg 		if (link_conf) {
9070af637b5SJohannes Berg 			basic = link_conf->basic_rates;
9080af637b5SJohannes Berg 			if (link_conf->chandef.chan)
9090af637b5SJohannes Berg 				band = link_conf->chandef.chan->band;
9100af637b5SJohannes Berg 		}
9110af637b5SJohannes Berg 		rcu_read_unlock();
9120af637b5SJohannes Berg 	}
9130af637b5SJohannes Berg 
9140af637b5SJohannes Berg 	sband = mvm->hw->wiphy->bands[band];
915ef2e7a51SIlan Peer 	for_each_set_bit(i, &basic, BITS_PER_LONG) {
916ef2e7a51SIlan Peer 		u16 hw = sband->bitrates[i].hw_value;
917ef2e7a51SIlan Peer 
918ef2e7a51SIlan Peer 		if (hw >= IWL_FIRST_OFDM_RATE) {
919ef2e7a51SIlan Peer 			if (lowest_ofdm > hw)
920ef2e7a51SIlan Peer 				lowest_ofdm = hw;
921ef2e7a51SIlan Peer 		} else if (lowest_cck > hw) {
922ef2e7a51SIlan Peer 			lowest_cck = hw;
923ef2e7a51SIlan Peer 		}
924ef2e7a51SIlan Peer 	}
925ef2e7a51SIlan Peer 
9260af637b5SJohannes Berg 	if (band == NL80211_BAND_2GHZ && !vif->p2p &&
9277f11d17fSIlan Peer 	    vif->type != NL80211_IFTYPE_P2P_DEVICE &&
9287f11d17fSIlan Peer 	    !(info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)) {
929ef2e7a51SIlan Peer 		if (lowest_cck != IWL_RATE_COUNT)
930ef2e7a51SIlan Peer 			rate = lowest_cck;
931ef2e7a51SIlan Peer 		else if (lowest_ofdm != IWL_RATE_COUNT)
932ef2e7a51SIlan Peer 			rate = lowest_ofdm;
933d558b7f8STova Mussai 		else
934ef2e7a51SIlan Peer 			rate = IWL_RATE_1M_INDEX;
935ef2e7a51SIlan Peer 	} else if (lowest_ofdm != IWL_RATE_COUNT) {
936ef2e7a51SIlan Peer 		rate = lowest_ofdm;
937ef2e7a51SIlan Peer 	} else {
938ef2e7a51SIlan Peer 		rate = IWL_RATE_6M_INDEX;
939ef2e7a51SIlan Peer 	}
9406ca33f8bSHaim Dreyfuss 
9416ca33f8bSHaim Dreyfuss 	return rate;
9426ca33f8bSHaim Dreyfuss }
9436ca33f8bSHaim Dreyfuss 
944cd2c46a7SMiri Korenblit u16 iwl_mvm_mac_ctxt_get_beacon_flags(const struct iwl_fw *fw, u8 rate_idx)
945cd2c46a7SMiri Korenblit {
946cd2c46a7SMiri Korenblit 	u16 flags = iwl_mvm_mac80211_idx_to_hwrate(fw, rate_idx);
947971cbe50SJohannes Berg 	bool is_new_rate = iwl_fw_lookup_cmd_ver(fw, BEACON_TEMPLATE_CMD, 0) > 10;
948cd2c46a7SMiri Korenblit 
949cd2c46a7SMiri Korenblit 	if (rate_idx <= IWL_FIRST_CCK_RATE)
950cd2c46a7SMiri Korenblit 		flags |= is_new_rate ? IWL_MAC_BEACON_CCK
951cd2c46a7SMiri Korenblit 			  : IWL_MAC_BEACON_CCK_V1;
952cd2c46a7SMiri Korenblit 
953cd2c46a7SMiri Korenblit 	return flags;
954cd2c46a7SMiri Korenblit }
955cd2c46a7SMiri Korenblit 
956ef2e7a51SIlan Peer u8 iwl_mvm_mac_ctxt_get_beacon_rate(struct iwl_mvm *mvm,
957ef2e7a51SIlan Peer 				    struct ieee80211_tx_info *info,
958ef2e7a51SIlan Peer 				    struct ieee80211_vif *vif)
959ef2e7a51SIlan Peer {
960ef2e7a51SIlan Peer 	struct ieee80211_supported_band *sband =
961ef2e7a51SIlan Peer 		mvm->hw->wiphy->bands[info->band];
962ef2e7a51SIlan Peer 	u32 legacy = vif->bss_conf.beacon_tx_rate.control[info->band].legacy;
963ef2e7a51SIlan Peer 
964ef2e7a51SIlan Peer 	/* if beacon rate was configured try using it */
965ef2e7a51SIlan Peer 	if (hweight32(legacy) == 1) {
966ef2e7a51SIlan Peer 		u32 rate = ffs(legacy) - 1;
967ef2e7a51SIlan Peer 
968ef2e7a51SIlan Peer 		return sband->bitrates[rate].hw_value;
969ef2e7a51SIlan Peer 	}
970ef2e7a51SIlan Peer 
971ef2e7a51SIlan Peer 	return iwl_mvm_mac_ctxt_get_lowest_rate(mvm, info, vif);
972ef2e7a51SIlan Peer }
973ef2e7a51SIlan Peer 
97409856582SLuca Coelho static void iwl_mvm_mac_ctxt_set_tx(struct iwl_mvm *mvm,
975e705c121SKalle Valo 				    struct ieee80211_vif *vif,
97609856582SLuca Coelho 				    struct sk_buff *beacon,
97709856582SLuca Coelho 				    struct iwl_tx_cmd *tx)
978e705c121SKalle Valo {
979e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
980e705c121SKalle Valo 	struct ieee80211_tx_info *info;
9816ca33f8bSHaim Dreyfuss 	u8 rate;
9826ca33f8bSHaim Dreyfuss 	u32 tx_flags;
983e705c121SKalle Valo 
984e705c121SKalle Valo 	info = IEEE80211_SKB_CB(beacon);
985e705c121SKalle Valo 
986e705c121SKalle Valo 	/* Set up TX command fields */
98709856582SLuca Coelho 	tx->len = cpu_to_le16((u16)beacon->len);
988650cadb7SGregory Greenman 	tx->sta_id = mvmvif->deflink.bcast_sta.sta_id;
98909856582SLuca Coelho 	tx->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
990e705c121SKalle Valo 	tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;
991e705c121SKalle Valo 	tx_flags |=
992e705c121SKalle Valo 		iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<
993e705c121SKalle Valo 						TX_CMD_FLG_BT_PRIO_POS;
99409856582SLuca Coelho 	tx->tx_flags = cpu_to_le32(tx_flags);
995e705c121SKalle Valo 
9961e3c3c35SEmmanuel Grumbach 	if (!fw_has_capa(&mvm->fw->ucode_capa,
997656fca00SAvraham Stern 			 IWL_UCODE_TLV_CAPA_BEACON_ANT_SELECTION))
998656fca00SAvraham Stern 		iwl_mvm_toggle_tx_ant(mvm, &mvm->mgmt_last_antenna_idx);
999e705c121SKalle Valo 
100009856582SLuca Coelho 	tx->rate_n_flags =
1001e705c121SKalle Valo 		cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
1002e705c121SKalle Valo 			    RATE_MCS_ANT_POS);
1003e705c121SKalle Valo 
1004ef2e7a51SIlan Peer 	rate = iwl_mvm_mac_ctxt_get_beacon_rate(mvm, info, vif);
1005e705c121SKalle Valo 
1006d35d95ceSMiri Korenblit 	tx->rate_n_flags |=
1007d35d95ceSMiri Korenblit 		cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(mvm->fw, rate));
10086ca33f8bSHaim Dreyfuss 	if (rate == IWL_FIRST_CCK_RATE)
100948c6ebc1SMiri Korenblit 		tx->rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK_V1);
10106ca33f8bSHaim Dreyfuss 
101109856582SLuca Coelho }
1012e705c121SKalle Valo 
1013138664a3SSara Sharon int iwl_mvm_mac_ctxt_send_beacon_cmd(struct iwl_mvm *mvm,
101409856582SLuca Coelho 				     struct sk_buff *beacon,
101509856582SLuca Coelho 				     void *data, int len)
101609856582SLuca Coelho {
101709856582SLuca Coelho 	struct iwl_host_cmd cmd = {
101809856582SLuca Coelho 		.id = BEACON_TEMPLATE_CMD,
101909856582SLuca Coelho 		.flags = CMD_ASYNC,
102009856582SLuca Coelho 	};
102109856582SLuca Coelho 
102209856582SLuca Coelho 	cmd.len[0] = len;
102309856582SLuca Coelho 	cmd.data[0] = data;
1024e705c121SKalle Valo 	cmd.dataflags[0] = 0;
102509856582SLuca Coelho 	cmd.len[1] = beacon->len;
1026e705c121SKalle Valo 	cmd.data[1] = beacon->data;
1027e705c121SKalle Valo 	cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
1028e705c121SKalle Valo 
1029e705c121SKalle Valo 	return iwl_mvm_send_cmd(mvm, &cmd);
1030e705c121SKalle Valo }
1031e705c121SKalle Valo 
103209856582SLuca Coelho static int iwl_mvm_mac_ctxt_send_beacon_v6(struct iwl_mvm *mvm,
103309856582SLuca Coelho 					   struct ieee80211_vif *vif,
103409856582SLuca Coelho 					   struct sk_buff *beacon)
103509856582SLuca Coelho {
103609856582SLuca Coelho 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
103709856582SLuca Coelho 	struct iwl_mac_beacon_cmd_v6 beacon_cmd = {};
103809856582SLuca Coelho 
103909856582SLuca Coelho 	iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
104009856582SLuca Coelho 
104109856582SLuca Coelho 	beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
104209856582SLuca Coelho 
104309856582SLuca Coelho 	if (vif->type == NL80211_IFTYPE_AP)
104409856582SLuca Coelho 		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
104509856582SLuca Coelho 					 &beacon_cmd.tim_size,
104609856582SLuca Coelho 					 beacon->data, beacon->len);
104709856582SLuca Coelho 
104809856582SLuca Coelho 	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
104909856582SLuca Coelho 						sizeof(beacon_cmd));
105009856582SLuca Coelho }
105109856582SLuca Coelho 
105209856582SLuca Coelho static int iwl_mvm_mac_ctxt_send_beacon_v7(struct iwl_mvm *mvm,
105309856582SLuca Coelho 					   struct ieee80211_vif *vif,
105409856582SLuca Coelho 					   struct sk_buff *beacon)
105509856582SLuca Coelho {
105609856582SLuca Coelho 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
105709856582SLuca Coelho 	struct iwl_mac_beacon_cmd_v7 beacon_cmd = {};
105809856582SLuca Coelho 
105909856582SLuca Coelho 	iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
106009856582SLuca Coelho 
106109856582SLuca Coelho 	beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
106209856582SLuca Coelho 
106309856582SLuca Coelho 	if (vif->type == NL80211_IFTYPE_AP)
106409856582SLuca Coelho 		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
106509856582SLuca Coelho 					 &beacon_cmd.tim_size,
106609856582SLuca Coelho 					 beacon->data, beacon->len);
106709856582SLuca Coelho 
106809856582SLuca Coelho 	beacon_cmd.csa_offset =
106909856582SLuca Coelho 		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
107009856582SLuca Coelho 						   WLAN_EID_CHANNEL_SWITCH,
107109856582SLuca Coelho 						   beacon->len));
107209856582SLuca Coelho 	beacon_cmd.ecsa_offset =
107309856582SLuca Coelho 		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
107409856582SLuca Coelho 						   WLAN_EID_EXT_CHANSWITCH_ANN,
107509856582SLuca Coelho 						   beacon->len));
107609856582SLuca Coelho 
107709856582SLuca Coelho 	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
107809856582SLuca Coelho 						sizeof(beacon_cmd));
107909856582SLuca Coelho }
108009856582SLuca Coelho 
10816ca33f8bSHaim Dreyfuss static int iwl_mvm_mac_ctxt_send_beacon_v9(struct iwl_mvm *mvm,
108209856582SLuca Coelho 					   struct ieee80211_vif *vif,
108336cf5377SGregory Greenman 					   struct sk_buff *beacon,
108436cf5377SGregory Greenman 					   struct ieee80211_bss_conf *link_conf)
108509856582SLuca Coelho {
108609856582SLuca Coelho 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
10876ca33f8bSHaim Dreyfuss 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(beacon);
108809856582SLuca Coelho 	struct iwl_mac_beacon_cmd beacon_cmd = {};
1089ef2e7a51SIlan Peer 	u8 rate = iwl_mvm_mac_ctxt_get_beacon_rate(mvm, info, vif);
10906ca33f8bSHaim Dreyfuss 	u16 flags;
1091eae94cf8SLuca Coelho 	struct ieee80211_chanctx_conf *ctx;
1092eae94cf8SLuca Coelho 	int channel;
1093cd2c46a7SMiri Korenblit 	flags = iwl_mvm_mac_ctxt_get_beacon_flags(mvm->fw, rate);
10946ca33f8bSHaim Dreyfuss 
1095eae94cf8SLuca Coelho 	/* Enable FILS on PSC channels only */
1096eae94cf8SLuca Coelho 	rcu_read_lock();
109736cf5377SGregory Greenman 	ctx = rcu_dereference(link_conf->chanctx_conf);
1098eae94cf8SLuca Coelho 	channel = ieee80211_frequency_to_channel(ctx->def.chan->center_freq);
1099eae94cf8SLuca Coelho 	WARN_ON(channel == 0);
1100eae94cf8SLuca Coelho 	if (cfg80211_channel_is_psc(ctx->def.chan) &&
1101eae94cf8SLuca Coelho 	    !IWL_MVM_DISABLE_AP_FILS) {
1102971cbe50SJohannes Berg 		flags |= iwl_fw_lookup_cmd_ver(mvm->fw, BEACON_TEMPLATE_CMD,
1103cd2c46a7SMiri Korenblit 					       0) > 10 ?
1104cd2c46a7SMiri Korenblit 			IWL_MAC_BEACON_FILS :
1105cd2c46a7SMiri Korenblit 			IWL_MAC_BEACON_FILS_V1;
1106eae94cf8SLuca Coelho 		beacon_cmd.short_ssid =
1107f276e20bSJohannes Berg 			cpu_to_le32(~crc32_le(~0, vif->cfg.ssid,
1108f276e20bSJohannes Berg 					      vif->cfg.ssid_len));
1109eae94cf8SLuca Coelho 	}
1110eae94cf8SLuca Coelho 	rcu_read_unlock();
1111eae94cf8SLuca Coelho 
11126ca33f8bSHaim Dreyfuss 	beacon_cmd.flags = cpu_to_le16(flags);
111309856582SLuca Coelho 	beacon_cmd.byte_cnt = cpu_to_le16((u16)beacon->len);
1114d6f6b0d8SGregory Greenman 	if (iwl_fw_lookup_cmd_ver(mvm->fw, BEACON_TEMPLATE_CMD, 0) > 12)
1115d6f6b0d8SGregory Greenman 		beacon_cmd.link_id =
1116d6f6b0d8SGregory Greenman 			cpu_to_le32(mvmvif->link[link_conf->link_id]->fw_link_id);
1117d6f6b0d8SGregory Greenman 	else
111836cf5377SGregory Greenman 		beacon_cmd.link_id = cpu_to_le32((u32)mvmvif->id);
111909856582SLuca Coelho 
112009856582SLuca Coelho 	if (vif->type == NL80211_IFTYPE_AP)
11216ca33f8bSHaim Dreyfuss 		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
112209856582SLuca Coelho 					 &beacon_cmd.tim_size,
112309856582SLuca Coelho 					 beacon->data, beacon->len);
112409856582SLuca Coelho 
112509856582SLuca Coelho 	beacon_cmd.csa_offset =
112609856582SLuca Coelho 		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
112709856582SLuca Coelho 						   WLAN_EID_CHANNEL_SWITCH,
112809856582SLuca Coelho 						   beacon->len));
112909856582SLuca Coelho 	beacon_cmd.ecsa_offset =
113009856582SLuca Coelho 		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
113109856582SLuca Coelho 						   WLAN_EID_EXT_CHANSWITCH_ANN,
113209856582SLuca Coelho 						   beacon->len));
113309856582SLuca Coelho 
113409856582SLuca Coelho 	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
113509856582SLuca Coelho 						sizeof(beacon_cmd));
113609856582SLuca Coelho }
113709856582SLuca Coelho 
11387035b5baSJohannes Berg static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
113909856582SLuca Coelho 					struct ieee80211_vif *vif,
114036cf5377SGregory Greenman 					struct sk_buff *beacon,
114136cf5377SGregory Greenman 					struct ieee80211_bss_conf *link_conf)
114209856582SLuca Coelho {
114309856582SLuca Coelho 	if (WARN_ON(!beacon))
114409856582SLuca Coelho 		return -EINVAL;
114509856582SLuca Coelho 
114650386305SSara Sharon 	if (IWL_MVM_NON_TRANSMITTING_AP)
114750386305SSara Sharon 		return 0;
114850386305SSara Sharon 
114909856582SLuca Coelho 	if (!fw_has_capa(&mvm->fw->ucode_capa,
115009856582SLuca Coelho 			 IWL_UCODE_TLV_CAPA_CSA_AND_TBTT_OFFLOAD))
115109856582SLuca Coelho 		return iwl_mvm_mac_ctxt_send_beacon_v6(mvm, vif, beacon);
115209856582SLuca Coelho 
11536ca33f8bSHaim Dreyfuss 	if (fw_has_api(&mvm->fw->ucode_capa,
11546ca33f8bSHaim Dreyfuss 		       IWL_UCODE_TLV_API_NEW_BEACON_TEMPLATE))
115536cf5377SGregory Greenman 		return iwl_mvm_mac_ctxt_send_beacon_v9(mvm, vif, beacon,
115636cf5377SGregory Greenman 						       link_conf);
115709856582SLuca Coelho 
11586ca33f8bSHaim Dreyfuss 	return iwl_mvm_mac_ctxt_send_beacon_v7(mvm, vif, beacon);
115909856582SLuca Coelho }
116009856582SLuca Coelho 
1161e705c121SKalle Valo /* The beacon template for the AP/GO/IBSS has changed and needs update */
1162e705c121SKalle Valo int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
116336cf5377SGregory Greenman 				    struct ieee80211_vif *vif,
116436cf5377SGregory Greenman 				    struct ieee80211_bss_conf *link_conf)
1165e705c121SKalle Valo {
1166e705c121SKalle Valo 	struct sk_buff *beacon;
1167e705c121SKalle Valo 	int ret;
1168e705c121SKalle Valo 
1169e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_AP &&
1170e705c121SKalle Valo 		vif->type != NL80211_IFTYPE_ADHOC);
1171e705c121SKalle Valo 
117236cf5377SGregory Greenman 	beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL,
117336cf5377SGregory Greenman 					       link_conf->link_id);
1174e705c121SKalle Valo 	if (!beacon)
1175e705c121SKalle Valo 		return -ENOMEM;
1176e705c121SKalle Valo 
117790a12829SSara Sharon #ifdef CONFIG_IWLWIFI_DEBUGFS
11780f5d44acSZhang Qilong 	if (mvm->beacon_inject_active) {
11790f5d44acSZhang Qilong 		dev_kfree_skb(beacon);
118090a12829SSara Sharon 		return -EBUSY;
11810f5d44acSZhang Qilong 	}
118290a12829SSara Sharon #endif
118390a12829SSara Sharon 
118436cf5377SGregory Greenman 	ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon, link_conf);
1185e705c121SKalle Valo 	dev_kfree_skb(beacon);
1186e705c121SKalle Valo 	return ret;
1187e705c121SKalle Valo }
1188e705c121SKalle Valo 
1189e705c121SKalle Valo struct iwl_mvm_mac_ap_iterator_data {
1190e705c121SKalle Valo 	struct iwl_mvm *mvm;
1191e705c121SKalle Valo 	struct ieee80211_vif *vif;
1192e705c121SKalle Valo 	u32 beacon_device_ts;
1193e705c121SKalle Valo 	u16 beacon_int;
1194e705c121SKalle Valo };
1195e705c121SKalle Valo 
1196e705c121SKalle Valo /* Find the beacon_device_ts and beacon_int for a managed interface */
1197e705c121SKalle Valo static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
1198e705c121SKalle Valo 				    struct ieee80211_vif *vif)
1199e705c121SKalle Valo {
1200e705c121SKalle Valo 	struct iwl_mvm_mac_ap_iterator_data *data = _data;
1201e705c121SKalle Valo 
1202f276e20bSJohannes Berg 	if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc)
1203e705c121SKalle Valo 		return;
1204e705c121SKalle Valo 
1205e705c121SKalle Valo 	/* Station client has higher priority over P2P client*/
1206e705c121SKalle Valo 	if (vif->p2p && data->beacon_device_ts)
1207e705c121SKalle Valo 		return;
1208e705c121SKalle Valo 
1209e705c121SKalle Valo 	data->beacon_device_ts = vif->bss_conf.sync_device_ts;
1210e705c121SKalle Valo 	data->beacon_int = vif->bss_conf.beacon_int;
1211e705c121SKalle Valo }
1212e705c121SKalle Valo 
1213e705c121SKalle Valo /*
1214af6d168fSMiri Korenblit  * Fill the filter flags for mac context of type AP or P2P GO.
1215af6d168fSMiri Korenblit  */
12169be162a7SMiri Korenblit void iwl_mvm_mac_ctxt_cmd_ap_set_filter_flags(struct iwl_mvm *mvm,
1217af6d168fSMiri Korenblit 					      struct iwl_mvm_vif *mvmvif,
1218af6d168fSMiri Korenblit 					      __le32 *filter_flags,
1219af6d168fSMiri Korenblit 					      int accept_probe_req_flag,
1220af6d168fSMiri Korenblit 					      int accept_beacon_flag)
1221af6d168fSMiri Korenblit {
1222af6d168fSMiri Korenblit 	/*
1223af6d168fSMiri Korenblit 	 * in AP mode, pass probe requests and beacons from other APs
1224af6d168fSMiri Korenblit 	 * (needed for ht protection); when there're no any associated
1225af6d168fSMiri Korenblit 	 * station don't ask FW to pass beacons to prevent unnecessary
1226af6d168fSMiri Korenblit 	 * wake-ups.
1227af6d168fSMiri Korenblit 	 */
1228af6d168fSMiri Korenblit 	*filter_flags |= cpu_to_le32(accept_probe_req_flag);
1229af6d168fSMiri Korenblit 	if (mvmvif->ap_assoc_sta_count || !mvm->drop_bcn_ap_mode) {
1230af6d168fSMiri Korenblit 		*filter_flags |= cpu_to_le32(accept_beacon_flag);
1231af6d168fSMiri Korenblit 		IWL_DEBUG_HC(mvm, "Asking FW to pass beacons\n");
1232af6d168fSMiri Korenblit 	} else {
1233af6d168fSMiri Korenblit 		IWL_DEBUG_HC(mvm, "No need to receive beacons\n");
1234af6d168fSMiri Korenblit 	}
1235af6d168fSMiri Korenblit }
1236af6d168fSMiri Korenblit 
1237af6d168fSMiri Korenblit /*
1238e705c121SKalle Valo  * Fill the specific data for mac context of type AP of P2P GO
1239e705c121SKalle Valo  */
1240e705c121SKalle Valo static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
1241e705c121SKalle Valo 					 struct ieee80211_vif *vif,
1242186cd49aSJohannes Berg 					 struct iwl_mac_ctx_cmd *cmd,
1243e705c121SKalle Valo 					 struct iwl_mac_data_ap *ctxt_ap,
1244e705c121SKalle Valo 					 bool add)
1245e705c121SKalle Valo {
1246e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1247e705c121SKalle Valo 	struct iwl_mvm_mac_ap_iterator_data data = {
1248e705c121SKalle Valo 		.mvm = mvm,
1249e705c121SKalle Valo 		.vif = vif,
1250e705c121SKalle Valo 		.beacon_device_ts = 0
1251e705c121SKalle Valo 	};
1252e705c121SKalle Valo 
1253186cd49aSJohannes Berg 	/* in AP mode, the MCAST FIFO takes the EDCA params from VO */
1254186cd49aSJohannes Berg 	cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |= BIT(IWL_MVM_TX_FIFO_MCAST);
1255186cd49aSJohannes Berg 
1256af6d168fSMiri Korenblit 	iwl_mvm_mac_ctxt_cmd_ap_set_filter_flags(mvm, mvmvif,
1257af6d168fSMiri Korenblit 						 &cmd->filter_flags,
1258af6d168fSMiri Korenblit 						 MAC_FILTER_IN_PROBE_REQUEST,
1259af6d168fSMiri Korenblit 						 MAC_FILTER_IN_BEACON);
1260186cd49aSJohannes Berg 
1261e705c121SKalle Valo 	ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
1262e705c121SKalle Valo 	ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
1263e705c121SKalle Valo 					     vif->bss_conf.dtim_period);
1264e705c121SKalle Valo 
1265ced19f26SSara Sharon 	if (!fw_has_api(&mvm->fw->ucode_capa,
1266ced19f26SSara Sharon 			IWL_UCODE_TLV_API_STA_TYPE))
1267650cadb7SGregory Greenman 		ctxt_ap->mcast_qid = cpu_to_le32(mvmvif->deflink.cab_queue);
1268e705c121SKalle Valo 
1269e705c121SKalle Valo 	/*
1270e705c121SKalle Valo 	 * Only set the beacon time when the MAC is being added, when we
1271e705c121SKalle Valo 	 * just modify the MAC then we should keep the time -- the firmware
1272e705c121SKalle Valo 	 * can otherwise have a "jumping" TBTT.
1273e705c121SKalle Valo 	 */
1274e705c121SKalle Valo 	if (add) {
1275e705c121SKalle Valo 		/*
1276e705c121SKalle Valo 		 * If there is a station/P2P client interface which is
1277e705c121SKalle Valo 		 * associated, set the AP's TBTT far enough from the station's
1278e705c121SKalle Valo 		 * TBTT. Otherwise, set it to the current system time
1279e705c121SKalle Valo 		 */
1280e705c121SKalle Valo 		ieee80211_iterate_active_interfaces_atomic(
1281e705c121SKalle Valo 			mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1282e705c121SKalle Valo 			iwl_mvm_mac_ap_iterator, &data);
1283e705c121SKalle Valo 
1284e705c121SKalle Valo 		if (data.beacon_device_ts) {
1285e8a533cbSJason A. Donenfeld 			u32 rand = get_random_u32_inclusive(36, 63);
1286e705c121SKalle Valo 			mvmvif->ap_beacon_time = data.beacon_device_ts +
1287e705c121SKalle Valo 				ieee80211_tu_to_usec(data.beacon_int * rand /
1288e705c121SKalle Valo 						     100);
1289e705c121SKalle Valo 		} else {
1290afc1e3b4SAvraham Stern 			mvmvif->ap_beacon_time = iwl_mvm_get_systime(mvm);
1291e705c121SKalle Valo 		}
1292e705c121SKalle Valo 	}
1293e705c121SKalle Valo 
1294e705c121SKalle Valo 	ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
1295e705c121SKalle Valo 	ctxt_ap->beacon_tsf = 0; /* unused */
1296e705c121SKalle Valo 
1297e705c121SKalle Valo 	/* TODO: Assume that the beacon id == mac context id */
1298e705c121SKalle Valo 	ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
1299e705c121SKalle Valo }
1300e705c121SKalle Valo 
1301e705c121SKalle Valo static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
1302e705c121SKalle Valo 				   struct ieee80211_vif *vif,
1303e705c121SKalle Valo 				   u32 action)
1304e705c121SKalle Valo {
1305e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd = {};
1306e705c121SKalle Valo 
1307e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
1308e705c121SKalle Valo 
1309e705c121SKalle Valo 	/* Fill the common data for all mac context types */
1310e705c121SKalle Valo 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1311e705c121SKalle Valo 
1312e705c121SKalle Valo 	/* Fill the data specific for ap mode */
1313186cd49aSJohannes Berg 	iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.ap,
1314e705c121SKalle Valo 				     action == FW_CTXT_ACTION_ADD);
1315e705c121SKalle Valo 
1316e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1317e705c121SKalle Valo }
1318e705c121SKalle Valo 
1319e705c121SKalle Valo static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
1320e705c121SKalle Valo 				   struct ieee80211_vif *vif,
1321e705c121SKalle Valo 				   u32 action)
1322e705c121SKalle Valo {
1323e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd = {};
1324e705c121SKalle Valo 	struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
1325e705c121SKalle Valo 
1326e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
1327e705c121SKalle Valo 
1328e705c121SKalle Valo 	/* Fill the common data for all mac context types */
1329e705c121SKalle Valo 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1330e705c121SKalle Valo 
1331e705c121SKalle Valo 	/* Fill the data specific for GO mode */
1332186cd49aSJohannes Berg 	iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.go.ap,
1333e705c121SKalle Valo 				     action == FW_CTXT_ACTION_ADD);
1334e705c121SKalle Valo 
1335e705c121SKalle Valo 	cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
1336e705c121SKalle Valo 					IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
1337e705c121SKalle Valo 	cmd.go.opp_ps_enabled =
1338e705c121SKalle Valo 			cpu_to_le32(!!(noa->oppps_ctwindow &
1339e705c121SKalle Valo 					IEEE80211_P2P_OPPPS_ENABLE_BIT));
1340e705c121SKalle Valo 
1341e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1342e705c121SKalle Valo }
1343e705c121SKalle Valo 
1344e705c121SKalle Valo static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1345e705c121SKalle Valo 				u32 action, bool force_assoc_off,
1346e705c121SKalle Valo 				const u8 *bssid_override)
1347e705c121SKalle Valo {
1348e705c121SKalle Valo 	switch (vif->type) {
1349e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
1350e705c121SKalle Valo 		return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
1351e705c121SKalle Valo 						force_assoc_off,
1352e705c121SKalle Valo 						bssid_override);
1353e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
1354e705c121SKalle Valo 		if (!vif->p2p)
1355e705c121SKalle Valo 			return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
1356e705c121SKalle Valo 		else
1357e705c121SKalle Valo 			return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
1358e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
1359e705c121SKalle Valo 		return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
1360e705c121SKalle Valo 	case NL80211_IFTYPE_P2P_DEVICE:
1361e705c121SKalle Valo 		return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
1362e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
1363e705c121SKalle Valo 		return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
1364e705c121SKalle Valo 	default:
1365e705c121SKalle Valo 		break;
1366e705c121SKalle Valo 	}
1367e705c121SKalle Valo 
1368e705c121SKalle Valo 	return -EOPNOTSUPP;
1369e705c121SKalle Valo }
1370e705c121SKalle Valo 
1371e705c121SKalle Valo int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1372e705c121SKalle Valo {
1373e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1374e705c121SKalle Valo 	int ret;
1375e705c121SKalle Valo 
1376e705c121SKalle Valo 	if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
1377e705c121SKalle Valo 		      vif->addr, ieee80211_vif_type_p2p(vif)))
1378e705c121SKalle Valo 		return -EIO;
1379e705c121SKalle Valo 
1380e705c121SKalle Valo 	ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
1381e705c121SKalle Valo 				   true, NULL);
1382e705c121SKalle Valo 	if (ret)
1383e705c121SKalle Valo 		return ret;
1384e705c121SKalle Valo 
1385e705c121SKalle Valo 	/* will only do anything at resume from D3 time */
1386e705c121SKalle Valo 	iwl_mvm_set_last_nonqos_seq(mvm, vif);
1387e705c121SKalle Valo 
1388e705c121SKalle Valo 	mvmvif->uploaded = true;
1389e705c121SKalle Valo 	return 0;
1390e705c121SKalle Valo }
1391e705c121SKalle Valo 
1392e705c121SKalle Valo int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1393e705c121SKalle Valo 			     bool force_assoc_off, const u8 *bssid_override)
1394e705c121SKalle Valo {
1395e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1396e705c121SKalle Valo 
1397e705c121SKalle Valo 	if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
1398e705c121SKalle Valo 		      vif->addr, ieee80211_vif_type_p2p(vif)))
1399e705c121SKalle Valo 		return -EIO;
1400e705c121SKalle Valo 
1401e705c121SKalle Valo 	return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
1402e705c121SKalle Valo 				    force_assoc_off, bssid_override);
1403e705c121SKalle Valo }
1404e705c121SKalle Valo 
1405e705c121SKalle Valo int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1406e705c121SKalle Valo {
1407e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1408e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd;
1409e705c121SKalle Valo 	int ret;
1410e705c121SKalle Valo 
1411e705c121SKalle Valo 	if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1412e705c121SKalle Valo 		      vif->addr, ieee80211_vif_type_p2p(vif)))
1413e705c121SKalle Valo 		return -EIO;
1414e705c121SKalle Valo 
1415e705c121SKalle Valo 	memset(&cmd, 0, sizeof(cmd));
1416e705c121SKalle Valo 
1417e705c121SKalle Valo 	cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1418e705c121SKalle Valo 							   mvmvif->color));
1419e705c121SKalle Valo 	cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1420e705c121SKalle Valo 
1421af6d168fSMiri Korenblit 	ret = iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1422af6d168fSMiri Korenblit 	if (ret)
1423e705c121SKalle Valo 		return ret;
1424e705c121SKalle Valo 
1425e705c121SKalle Valo 	mvmvif->uploaded = false;
1426e705c121SKalle Valo 
14270e39eb03SChaya Rachel Ivgi 	if (vif->type == NL80211_IFTYPE_MONITOR) {
1428e705c121SKalle Valo 		__clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, mvm->hw->flags);
14290e39eb03SChaya Rachel Ivgi 		iwl_mvm_dealloc_snif_sta(mvm);
14300e39eb03SChaya Rachel Ivgi 	}
1431e705c121SKalle Valo 
1432e705c121SKalle Valo 	return 0;
1433e705c121SKalle Valo }
1434e705c121SKalle Valo 
1435e705c121SKalle Valo static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
1436e705c121SKalle Valo 				   struct ieee80211_vif *csa_vif, u32 gp2,
1437e705c121SKalle Valo 				   bool tx_success)
1438e705c121SKalle Valo {
1439e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif =
1440e705c121SKalle Valo 			iwl_mvm_vif_from_mac80211(csa_vif);
1441e705c121SKalle Valo 
1442e705c121SKalle Valo 	/* Don't start to countdown from a failed beacon */
1443e705c121SKalle Valo 	if (!tx_success && !mvmvif->csa_countdown)
1444e705c121SKalle Valo 		return;
1445e705c121SKalle Valo 
1446e705c121SKalle Valo 	mvmvif->csa_countdown = true;
1447e705c121SKalle Valo 
14488552a434SJohn Crispin 	if (!ieee80211_beacon_cntdwn_is_complete(csa_vif)) {
14498552a434SJohn Crispin 		int c = ieee80211_beacon_update_cntdwn(csa_vif);
1450e705c121SKalle Valo 
145136cf5377SGregory Greenman 		iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif,
145236cf5377SGregory Greenman 						&csa_vif->bss_conf);
1453e705c121SKalle Valo 		if (csa_vif->p2p &&
1454e705c121SKalle Valo 		    !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2 &&
1455e705c121SKalle Valo 		    tx_success) {
1456e705c121SKalle Valo 			u32 rel_time = (c + 1) *
1457e705c121SKalle Valo 				       csa_vif->bss_conf.beacon_int -
1458e705c121SKalle Valo 				       IWL_MVM_CHANNEL_SWITCH_TIME_GO;
1459e705c121SKalle Valo 			u32 apply_time = gp2 + rel_time * 1024;
1460e705c121SKalle Valo 
1461e705c121SKalle Valo 			iwl_mvm_schedule_csa_period(mvm, csa_vif,
1462e705c121SKalle Valo 					 IWL_MVM_CHANNEL_SWITCH_TIME_GO -
1463e705c121SKalle Valo 					 IWL_MVM_CHANNEL_SWITCH_MARGIN,
1464e705c121SKalle Valo 					 apply_time);
1465e705c121SKalle Valo 		}
1466e705c121SKalle Valo 	} else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
1467e705c121SKalle Valo 		/* we don't have CSA NoA scheduled yet, switch now */
1468e705c121SKalle Valo 		ieee80211_csa_finish(csa_vif);
1469e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
1470e705c121SKalle Valo 	}
1471e705c121SKalle Valo }
1472e705c121SKalle Valo 
1473e705c121SKalle Valo void iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1474e705c121SKalle Valo 			     struct iwl_rx_cmd_buffer *rxb)
1475e705c121SKalle Valo {
1476e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1477afc857bcSJohannes Berg 	unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
1478e705c121SKalle Valo 	struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
147915e28c78SEmmanuel Grumbach 	struct iwl_extended_beacon_notif_v5 *beacon_v5 = (void *)pkt->data;
1480e705c121SKalle Valo 	struct ieee80211_vif *csa_vif;
1481e705c121SKalle Valo 	struct ieee80211_vif *tx_blocked_vif;
148212db294cSSara Sharon 	struct agg_tx_status *agg_status;
1483e705c121SKalle Valo 	u16 status;
1484e705c121SKalle Valo 
1485e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1486e705c121SKalle Valo 
1487e705c121SKalle Valo 	mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
1488e705c121SKalle Valo 
148915e28c78SEmmanuel Grumbach 	if (!iwl_mvm_is_short_beacon_notif_supported(mvm)) {
149015e28c78SEmmanuel Grumbach 		struct iwl_mvm_tx_resp *beacon_notify_hdr =
149115e28c78SEmmanuel Grumbach 			&beacon_v5->beacon_notify_hdr;
149215e28c78SEmmanuel Grumbach 
1493afc857bcSJohannes Berg 		if (unlikely(pkt_len < sizeof(*beacon_v5)))
1494afc857bcSJohannes Berg 			return;
1495afc857bcSJohannes Berg 
149615e28c78SEmmanuel Grumbach 		mvm->ibss_manager = beacon_v5->ibss_mgr_status != 0;
149712db294cSSara Sharon 		agg_status = iwl_mvm_get_agg_status(mvm, beacon_notify_hdr);
149812db294cSSara Sharon 		status = le16_to_cpu(agg_status->status) & TX_STATUS_MSK;
1499e705c121SKalle Valo 		IWL_DEBUG_RX(mvm,
1500677837b8SJohannes Berg 			     "beacon status %#x retries:%d tsf:0x%016llX gp2:0x%X rate:%d\n",
1501e705c121SKalle Valo 			     status, beacon_notify_hdr->failure_frame,
1502e705c121SKalle Valo 			     le64_to_cpu(beacon->tsf),
1503e705c121SKalle Valo 			     mvm->ap_last_beacon_gp2,
1504e705c121SKalle Valo 			     le32_to_cpu(beacon_notify_hdr->initial_rate));
150515e28c78SEmmanuel Grumbach 	} else {
1506afc857bcSJohannes Berg 		if (unlikely(pkt_len < sizeof(*beacon)))
1507afc857bcSJohannes Berg 			return;
1508afc857bcSJohannes Berg 
150915e28c78SEmmanuel Grumbach 		mvm->ibss_manager = beacon->ibss_mgr_status != 0;
151015e28c78SEmmanuel Grumbach 		status = le32_to_cpu(beacon->status) & TX_STATUS_MSK;
151115e28c78SEmmanuel Grumbach 		IWL_DEBUG_RX(mvm,
151215e28c78SEmmanuel Grumbach 			     "beacon status %#x tsf:0x%016llX gp2:0x%X\n",
151315e28c78SEmmanuel Grumbach 			     status, le64_to_cpu(beacon->tsf),
151415e28c78SEmmanuel Grumbach 			     mvm->ap_last_beacon_gp2);
151515e28c78SEmmanuel Grumbach 	}
1516e705c121SKalle Valo 
1517e705c121SKalle Valo 	csa_vif = rcu_dereference_protected(mvm->csa_vif,
1518e705c121SKalle Valo 					    lockdep_is_held(&mvm->mutex));
1519d0a9123eSJohannes Berg 	if (unlikely(csa_vif && csa_vif->bss_conf.csa_active))
1520e705c121SKalle Valo 		iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2,
1521e705c121SKalle Valo 				       (status == TX_STATUS_SUCCESS));
1522e705c121SKalle Valo 
1523e705c121SKalle Valo 	tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
1524e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
1525e705c121SKalle Valo 	if (unlikely(tx_blocked_vif)) {
1526e705c121SKalle Valo 		struct iwl_mvm_vif *mvmvif =
1527e705c121SKalle Valo 			iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1528e705c121SKalle Valo 
1529e705c121SKalle Valo 		/*
1530e705c121SKalle Valo 		 * The channel switch is started and we have blocked the
1531e705c121SKalle Valo 		 * stations. If this is the first beacon (the timeout wasn't
1532e705c121SKalle Valo 		 * set), set the unblock timeout, otherwise countdown
1533e705c121SKalle Valo 		 */
1534e705c121SKalle Valo 		if (!mvm->csa_tx_block_bcn_timeout)
1535e705c121SKalle Valo 			mvm->csa_tx_block_bcn_timeout =
1536e705c121SKalle Valo 				IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
1537e705c121SKalle Valo 		else
1538e705c121SKalle Valo 			mvm->csa_tx_block_bcn_timeout--;
1539e705c121SKalle Valo 
1540e705c121SKalle Valo 		/* Check if the timeout is expired, and unblock tx */
1541e705c121SKalle Valo 		if (mvm->csa_tx_block_bcn_timeout == 0) {
1542e705c121SKalle Valo 			iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
1543e705c121SKalle Valo 			RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1544e705c121SKalle Valo 		}
1545e705c121SKalle Valo 	}
1546e705c121SKalle Valo }
1547e705c121SKalle Valo 
1548698478c4SSara Sharon void iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
1549698478c4SSara Sharon 				     struct iwl_rx_cmd_buffer *rxb)
1550e705c121SKalle Valo {
1551698478c4SSara Sharon 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1552698478c4SSara Sharon 	struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
1553e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_missed_bcon *bcon_trig;
1554e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trigger;
1555e705c121SKalle Valo 	u32 stop_trig_missed_bcon, stop_trig_missed_bcon_since_rx;
1556e705c121SKalle Valo 	u32 rx_missed_bcon, rx_missed_bcon_since_rx;
1557698478c4SSara Sharon 	struct ieee80211_vif *vif;
1558*d464550bSYedidya Benshimol 	/* Id can be mac/link id depending on the notification version */
1559*d464550bSYedidya Benshimol 	u32 id = le32_to_cpu(mb->link_id);
1560eae7550bSShahar S Matityahu 	union iwl_dbg_tlv_tp_data tp_data = { .fw_pkt = pkt };
1561834f920eSMukesh Sisodiya 	u32 mac_type;
1562*d464550bSYedidya Benshimol 	u8 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
1563*d464550bSYedidya Benshimol 					       MISSED_BEACONS_NOTIFICATION,
1564*d464550bSYedidya Benshimol 					       0);
1565*d464550bSYedidya Benshimol 
1566*d464550bSYedidya Benshimol 	rcu_read_lock();
1567*d464550bSYedidya Benshimol 
1568*d464550bSYedidya Benshimol 	/* before version four the ID in the notification refers to mac ID */
1569*d464550bSYedidya Benshimol 	if (notif_ver < 4) {
1570*d464550bSYedidya Benshimol 		vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);
1571*d464550bSYedidya Benshimol 	} else {
1572*d464550bSYedidya Benshimol 		struct ieee80211_bss_conf *bss_conf =
1573*d464550bSYedidya Benshimol 			iwl_mvm_rcu_fw_link_id_to_link_conf(mvm, id, true);
1574*d464550bSYedidya Benshimol 
1575*d464550bSYedidya Benshimol 		if (!bss_conf)
1576*d464550bSYedidya Benshimol 			goto out;
1577*d464550bSYedidya Benshimol 
1578*d464550bSYedidya Benshimol 		vif = bss_conf->vif;
1579*d464550bSYedidya Benshimol 	}
1580e705c121SKalle Valo 
1581698478c4SSara Sharon 	IWL_DEBUG_INFO(mvm,
1582*d464550bSYedidya Benshimol 		       "missed bcn %s_id=%u, consecutive=%u (%u, %u, %u)\n",
1583*d464550bSYedidya Benshimol 		       notif_ver < 4 ? "mac" : "link",
1584*d464550bSYedidya Benshimol 		       id,
1585698478c4SSara Sharon 		       le32_to_cpu(mb->consec_missed_beacons),
1586698478c4SSara Sharon 		       le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
1587698478c4SSara Sharon 		       le32_to_cpu(mb->num_recvd_beacons),
1588698478c4SSara Sharon 		       le32_to_cpu(mb->num_expected_beacons));
1589e705c121SKalle Valo 
1590698478c4SSara Sharon 	if (!vif)
1591698478c4SSara Sharon 		goto out;
1592698478c4SSara Sharon 
1593834f920eSMukesh Sisodiya 	mac_type = iwl_mvm_get_mac_type(vif);
1594834f920eSMukesh Sisodiya 
1595834f920eSMukesh Sisodiya 	IWL_DEBUG_INFO(mvm, "missed beacon mac_type=%u,\n", mac_type);
1596834f920eSMukesh Sisodiya 
1597834f920eSMukesh Sisodiya 	mvm->trans->dbg.dump_file_name_ext_valid = true;
1598834f920eSMukesh Sisodiya 	snprintf(mvm->trans->dbg.dump_file_name_ext, IWL_FW_INI_MAX_NAME,
1599834f920eSMukesh Sisodiya 		 "MacId_%d_MacType_%d", id, mac_type);
1600834f920eSMukesh Sisodiya 
1601698478c4SSara Sharon 	rx_missed_bcon = le32_to_cpu(mb->consec_missed_beacons);
1602e705c121SKalle Valo 	rx_missed_bcon_since_rx =
1603698478c4SSara Sharon 		le32_to_cpu(mb->consec_missed_beacons_since_last_rx);
1604e705c121SKalle Valo 	/*
1605e705c121SKalle Valo 	 * TODO: the threshold should be adjusted based on latency conditions,
1606e705c121SKalle Valo 	 * and/or in case of a CS flow on one of the other AP vifs.
1607e705c121SKalle Valo 	 */
1608babea2d4SAndrei Otcheretianski 	if (rx_missed_bcon > IWL_MVM_MISSED_BEACONS_THRESHOLD_LONG)
1609babea2d4SAndrei Otcheretianski 		iwl_mvm_connection_loss(mvm, vif, "missed beacons");
1610babea2d4SAndrei Otcheretianski 	else if (rx_missed_bcon_since_rx > IWL_MVM_MISSED_BEACONS_THRESHOLD)
1611e705c121SKalle Valo 		ieee80211_beacon_loss(vif);
1612e705c121SKalle Valo 
1613b108d8c7SShahar S Matityahu 	iwl_dbg_tlv_time_point(&mvm->fwrt,
1614eae7550bSShahar S Matityahu 			       IWL_FW_INI_TIME_POINT_MISSED_BEACONS, &tp_data);
1615b108d8c7SShahar S Matityahu 
16166c042d75SSara Sharon 	trigger = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
16176c042d75SSara Sharon 					FW_DBG_TRIGGER_MISSED_BEACONS);
16186c042d75SSara Sharon 	if (!trigger)
1619698478c4SSara Sharon 		goto out;
1620e705c121SKalle Valo 
1621e705c121SKalle Valo 	bcon_trig = (void *)trigger->data;
1622e705c121SKalle Valo 	stop_trig_missed_bcon = le32_to_cpu(bcon_trig->stop_consec_missed_bcon);
1623e705c121SKalle Valo 	stop_trig_missed_bcon_since_rx =
1624e705c121SKalle Valo 		le32_to_cpu(bcon_trig->stop_consec_missed_bcon_since_rx);
1625e705c121SKalle Valo 
1626e705c121SKalle Valo 	/* TODO: implement start trigger */
1627e705c121SKalle Valo 
1628e705c121SKalle Valo 	if (rx_missed_bcon_since_rx >= stop_trig_missed_bcon_since_rx ||
1629e705c121SKalle Valo 	    rx_missed_bcon >= stop_trig_missed_bcon)
16307174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&mvm->fwrt, trigger, NULL);
1631da2eb669SSara Sharon 
1632698478c4SSara Sharon out:
1633698478c4SSara Sharon 	rcu_read_unlock();
1634e705c121SKalle Valo }
16350db056d3SSara Sharon 
16360db056d3SSara Sharon void iwl_mvm_rx_stored_beacon_notif(struct iwl_mvm *mvm,
16370db056d3SSara Sharon 				    struct iwl_rx_cmd_buffer *rxb)
16380db056d3SSara Sharon {
16390db056d3SSara Sharon 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1640afc857bcSJohannes Berg 	unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
1641fb3fac5fSGregory Greenman 	struct iwl_stored_beacon_notif_common *sb = (void *)pkt->data;
16420db056d3SSara Sharon 	struct ieee80211_rx_status rx_status;
16430db056d3SSara Sharon 	struct sk_buff *skb;
1644fb3fac5fSGregory Greenman 	u8 *data;
16450db056d3SSara Sharon 	u32 size = le32_to_cpu(sb->byte_count);
1646971cbe50SJohannes Berg 	int ver = iwl_fw_lookup_cmd_ver(mvm->fw,
1647971cbe50SJohannes Berg 					WIDE_ID(PROT_OFFLOAD_GROUP, STORED_BEACON_NTF),
1648971cbe50SJohannes Berg 					0);
16490db056d3SSara Sharon 
1650fb3fac5fSGregory Greenman 	if (size == 0)
16510db056d3SSara Sharon 		return;
16520db056d3SSara Sharon 
1653fb3fac5fSGregory Greenman 	/* handle per-version differences */
1654fb3fac5fSGregory Greenman 	if (ver <= 2) {
1655fb3fac5fSGregory Greenman 		struct iwl_stored_beacon_notif_v2 *sb_v2 = (void *)pkt->data;
1656fb3fac5fSGregory Greenman 
1657fb3fac5fSGregory Greenman 		if (pkt_len < struct_size(sb_v2, data, size))
1658fb3fac5fSGregory Greenman 			return;
1659fb3fac5fSGregory Greenman 
1660fb3fac5fSGregory Greenman 		data = sb_v2->data;
1661fb3fac5fSGregory Greenman 	} else {
1662fb3fac5fSGregory Greenman 		struct iwl_stored_beacon_notif_v3 *sb_v3 = (void *)pkt->data;
1663fb3fac5fSGregory Greenman 
1664fb3fac5fSGregory Greenman 		if (pkt_len < struct_size(sb_v3, data, size))
1665fb3fac5fSGregory Greenman 			return;
1666fb3fac5fSGregory Greenman 
1667fb3fac5fSGregory Greenman 		data = sb_v3->data;
1668fb3fac5fSGregory Greenman 	}
1669fb3fac5fSGregory Greenman 
16700db056d3SSara Sharon 	skb = alloc_skb(size, GFP_ATOMIC);
16710db056d3SSara Sharon 	if (!skb) {
16720db056d3SSara Sharon 		IWL_ERR(mvm, "alloc_skb failed\n");
16730db056d3SSara Sharon 		return;
16740db056d3SSara Sharon 	}
16750db056d3SSara Sharon 
16760db056d3SSara Sharon 	/* update rx_status according to the notification's metadata */
16770db056d3SSara Sharon 	memset(&rx_status, 0, sizeof(rx_status));
16780db056d3SSara Sharon 	rx_status.mactime = le64_to_cpu(sb->tsf);
167977fe7395SSara Sharon 	/* TSF as indicated by the firmware  is at INA time */
168077fe7395SSara Sharon 	rx_status.flag |= RX_FLAG_MACTIME_PLCP_START;
16810db056d3SSara Sharon 	rx_status.device_timestamp = le32_to_cpu(sb->system_time);
16820db056d3SSara Sharon 	rx_status.band =
168334118c25SSara Sharon 		(sb->band & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
168457fbcce3SJohannes Berg 				NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
16850db056d3SSara Sharon 	rx_status.freq =
16860db056d3SSara Sharon 		ieee80211_channel_to_frequency(le16_to_cpu(sb->channel),
16870db056d3SSara Sharon 					       rx_status.band);
16880db056d3SSara Sharon 
16890db056d3SSara Sharon 	/* copy the data */
1690fb3fac5fSGregory Greenman 	skb_put_data(skb, data, size);
16910db056d3SSara Sharon 	memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
16920db056d3SSara Sharon 
16930db056d3SSara Sharon 	/* pass it as regular rx to mac80211 */
1694d63b548fSJohannes Berg 	ieee80211_rx_napi(mvm->hw, NULL, skb, NULL);
16950db056d3SSara Sharon }
1696d3a108a4SAndrei Otcheretianski 
1697698478c4SSara Sharon void iwl_mvm_probe_resp_data_notif(struct iwl_mvm *mvm,
1698698478c4SSara Sharon 				   struct iwl_rx_cmd_buffer *rxb)
169986e177d8SGregory Greenman {
1700698478c4SSara Sharon 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1701698478c4SSara Sharon 	struct iwl_probe_resp_data_notif *notif = (void *)pkt->data;
170286e177d8SGregory Greenman 	struct iwl_probe_resp_data *old_data, *new_data;
1703698478c4SSara Sharon 	u32 id = le32_to_cpu(notif->mac_id);
1704698478c4SSara Sharon 	struct ieee80211_vif *vif;
1705698478c4SSara Sharon 	struct iwl_mvm_vif *mvmvif;
170686e177d8SGregory Greenman 
1707698478c4SSara Sharon 	IWL_DEBUG_INFO(mvm, "Probe response data notif: noa %d, csa %d\n",
1708698478c4SSara Sharon 		       notif->noa_active, notif->csa_counter);
1709698478c4SSara Sharon 
1710698478c4SSara Sharon 	vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, false);
1711698478c4SSara Sharon 	if (!vif)
1712698478c4SSara Sharon 		return;
1713698478c4SSara Sharon 
1714698478c4SSara Sharon 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
1715698478c4SSara Sharon 
171686e177d8SGregory Greenman 	new_data = kzalloc(sizeof(*new_data), GFP_KERNEL);
171786e177d8SGregory Greenman 	if (!new_data)
171886e177d8SGregory Greenman 		return;
171986e177d8SGregory Greenman 
172086e177d8SGregory Greenman 	memcpy(&new_data->notif, notif, sizeof(new_data->notif));
172186e177d8SGregory Greenman 
172286e177d8SGregory Greenman 	/* noa_attr contains 1 reserved byte, need to substruct it */
172386e177d8SGregory Greenman 	new_data->noa_len = sizeof(struct ieee80211_vendor_ie) +
172486e177d8SGregory Greenman 			    sizeof(new_data->notif.noa_attr) - 1;
172586e177d8SGregory Greenman 
172686e177d8SGregory Greenman 	/*
172786e177d8SGregory Greenman 	 * If it's a one time NoA, only one descriptor is needed,
172886e177d8SGregory Greenman 	 * adjust the length according to len_low.
172986e177d8SGregory Greenman 	 */
173086e177d8SGregory Greenman 	if (new_data->notif.noa_attr.len_low ==
173186e177d8SGregory Greenman 	    sizeof(struct ieee80211_p2p_noa_desc) + 2)
173286e177d8SGregory Greenman 		new_data->noa_len -= sizeof(struct ieee80211_p2p_noa_desc);
173386e177d8SGregory Greenman 
1734650cadb7SGregory Greenman 	old_data = rcu_dereference_protected(mvmvif->deflink.probe_resp_data,
173586e177d8SGregory Greenman 					     lockdep_is_held(&mvmvif->mvm->mutex));
1736650cadb7SGregory Greenman 	rcu_assign_pointer(mvmvif->deflink.probe_resp_data, new_data);
173786e177d8SGregory Greenman 
173886e177d8SGregory Greenman 	if (old_data)
173986e177d8SGregory Greenman 		kfree_rcu(old_data, rcu_head);
174086e177d8SGregory Greenman 
174186e177d8SGregory Greenman 	if (notif->csa_counter != IWL_PROBE_RESP_DATA_NO_CSA &&
174286e177d8SGregory Greenman 	    notif->csa_counter >= 1)
17438552a434SJohn Crispin 		ieee80211_beacon_set_cntdwn(vif, notif->csa_counter);
174486e177d8SGregory Greenman }
174586e177d8SGregory Greenman 
17466905eb1cSNathan Errera void iwl_mvm_channel_switch_start_notif(struct iwl_mvm *mvm,
1747d3a108a4SAndrei Otcheretianski 					struct iwl_rx_cmd_buffer *rxb)
1748d3a108a4SAndrei Otcheretianski {
1749d3a108a4SAndrei Otcheretianski 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
17506905eb1cSNathan Errera 	struct iwl_channel_switch_start_notif *notif = (void *)pkt->data;
175174a10252SSara Sharon 	struct ieee80211_vif *csa_vif, *vif;
1752d3a108a4SAndrei Otcheretianski 	struct iwl_mvm_vif *mvmvif;
175374a10252SSara Sharon 	u32 id_n_color, csa_id, mac_id;
1754d3a108a4SAndrei Otcheretianski 
175574a10252SSara Sharon 	id_n_color = le32_to_cpu(notif->id_and_color);
175674a10252SSara Sharon 	mac_id = id_n_color & FW_CTXT_ID_MSK;
1757d3a108a4SAndrei Otcheretianski 
175874a10252SSara Sharon 	if (WARN_ON_ONCE(mac_id >= NUM_MAC_INDEX_DRIVER))
175974a10252SSara Sharon 		return;
176074a10252SSara Sharon 
176174a10252SSara Sharon 	rcu_read_lock();
176274a10252SSara Sharon 	vif = rcu_dereference(mvm->vif_id_to_mac[mac_id]);
1763f6780614SSara Sharon 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
176474a10252SSara Sharon 
176574a10252SSara Sharon 	switch (vif->type) {
176674a10252SSara Sharon 	case NL80211_IFTYPE_AP:
1767d3a108a4SAndrei Otcheretianski 		csa_vif = rcu_dereference(mvm->csa_vif);
1768d0a9123eSJohannes Berg 		if (WARN_ON(!csa_vif || !csa_vif->bss_conf.csa_active ||
176974a10252SSara Sharon 			    csa_vif != vif))
1770d3a108a4SAndrei Otcheretianski 			goto out_unlock;
1771d3a108a4SAndrei Otcheretianski 
177274a10252SSara Sharon 		csa_id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
177374a10252SSara Sharon 		if (WARN(csa_id != id_n_color,
1774d3a108a4SAndrei Otcheretianski 			 "channel switch noa notification on unexpected vif (csa_vif=%d, notif=%d)",
177574a10252SSara Sharon 			 csa_id, id_n_color))
1776d3a108a4SAndrei Otcheretianski 			goto out_unlock;
1777d3a108a4SAndrei Otcheretianski 
1778d3a108a4SAndrei Otcheretianski 		IWL_DEBUG_INFO(mvm, "Channel Switch Started Notification\n");
1779d3a108a4SAndrei Otcheretianski 
178086bbb1e1SJohannes Berg 		schedule_delayed_work(&mvm->cs_tx_unblock_dwork,
1781d3a108a4SAndrei Otcheretianski 				      msecs_to_jiffies(IWL_MVM_CS_UNBLOCK_TX_TIMEOUT *
1782d3a108a4SAndrei Otcheretianski 						       csa_vif->bss_conf.beacon_int));
1783d3a108a4SAndrei Otcheretianski 
1784d3a108a4SAndrei Otcheretianski 		ieee80211_csa_finish(csa_vif);
1785d3a108a4SAndrei Otcheretianski 
1786d3a108a4SAndrei Otcheretianski 		rcu_read_unlock();
1787d3a108a4SAndrei Otcheretianski 
1788d3a108a4SAndrei Otcheretianski 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
1789d3a108a4SAndrei Otcheretianski 		return;
179074a10252SSara Sharon 	case NL80211_IFTYPE_STATION:
1791ad12b231SNathan Errera 		/*
1792ad12b231SNathan Errera 		 * if we don't know about an ongoing channel switch,
1793ad12b231SNathan Errera 		 * make sure FW cancels it
1794ad12b231SNathan Errera 		 */
1795ad12b231SNathan Errera 		if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
1796ad12b231SNathan Errera 					    CHANNEL_SWITCH_ERROR_NOTIF,
1797d0a9123eSJohannes Berg 					    0) && !vif->bss_conf.csa_active) {
1798ad12b231SNathan Errera 			IWL_DEBUG_INFO(mvm, "Channel Switch was canceled\n");
1799ad12b231SNathan Errera 			iwl_mvm_cancel_channel_switch(mvm, vif, mac_id);
1800ad12b231SNathan Errera 			break;
1801ad12b231SNathan Errera 		}
1802ad12b231SNathan Errera 
180374a10252SSara Sharon 		iwl_mvm_csa_client_absent(mvm, vif);
180411af74adSAndrei Otcheretianski 		cancel_delayed_work(&mvmvif->csa_work);
180574a10252SSara Sharon 		ieee80211_chswitch_done(vif, true);
180674a10252SSara Sharon 		break;
180774a10252SSara Sharon 	default:
180874a10252SSara Sharon 		/* should never happen */
180974a10252SSara Sharon 		WARN_ON_ONCE(1);
181074a10252SSara Sharon 		break;
181174a10252SSara Sharon 	}
1812d3a108a4SAndrei Otcheretianski out_unlock:
1813d3a108a4SAndrei Otcheretianski 	rcu_read_unlock();
1814d3a108a4SAndrei Otcheretianski }
1815449a29d0SLior Cohen 
1816ad12b231SNathan Errera void iwl_mvm_channel_switch_error_notif(struct iwl_mvm *mvm,
1817ad12b231SNathan Errera 					struct iwl_rx_cmd_buffer *rxb)
1818ad12b231SNathan Errera {
1819ad12b231SNathan Errera 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1820ad12b231SNathan Errera 	struct iwl_channel_switch_error_notif *notif = (void *)pkt->data;
1821ad12b231SNathan Errera 	struct ieee80211_vif *vif;
1822ad12b231SNathan Errera 	u32 id = le32_to_cpu(notif->mac_id);
1823ad12b231SNathan Errera 	u32 csa_err_mask = le32_to_cpu(notif->csa_err_mask);
1824ad12b231SNathan Errera 
1825ad12b231SNathan Errera 	rcu_read_lock();
1826ad12b231SNathan Errera 	vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);
1827ad12b231SNathan Errera 	if (!vif) {
1828ad12b231SNathan Errera 		rcu_read_unlock();
1829ad12b231SNathan Errera 		return;
1830ad12b231SNathan Errera 	}
1831ad12b231SNathan Errera 
1832ad12b231SNathan Errera 	IWL_DEBUG_INFO(mvm, "FW reports CSA error: mac_id=%u, csa_err_mask=%u\n",
1833ad12b231SNathan Errera 		       id, csa_err_mask);
1834ad12b231SNathan Errera 	if (csa_err_mask & (CS_ERR_COUNT_ERROR |
1835ad12b231SNathan Errera 			    CS_ERR_LONG_DELAY_AFTER_CS |
1836ad12b231SNathan Errera 			    CS_ERR_TX_BLOCK_TIMER_EXPIRED))
1837ad12b231SNathan Errera 		ieee80211_channel_switch_disconnect(vif, true);
1838ad12b231SNathan Errera 	rcu_read_unlock();
1839ad12b231SNathan Errera }
1840ad12b231SNathan Errera 
1841449a29d0SLior Cohen void iwl_mvm_rx_missed_vap_notif(struct iwl_mvm *mvm,
1842449a29d0SLior Cohen 				 struct iwl_rx_cmd_buffer *rxb)
1843449a29d0SLior Cohen {
1844449a29d0SLior Cohen 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1845449a29d0SLior Cohen 	struct iwl_missed_vap_notif *mb = (void *)pkt->data;
1846449a29d0SLior Cohen 	struct ieee80211_vif *vif;
1847449a29d0SLior Cohen 	u32 id = le32_to_cpu(mb->mac_id);
1848449a29d0SLior Cohen 
1849449a29d0SLior Cohen 	IWL_DEBUG_INFO(mvm,
1850449a29d0SLior Cohen 		       "missed_vap notify mac_id=%u, num_beacon_intervals_elapsed=%u, profile_periodicity=%u\n",
1851449a29d0SLior Cohen 		       le32_to_cpu(mb->mac_id),
1852449a29d0SLior Cohen 		       mb->num_beacon_intervals_elapsed,
1853449a29d0SLior Cohen 		       mb->profile_periodicity);
1854449a29d0SLior Cohen 
1855449a29d0SLior Cohen 	rcu_read_lock();
1856449a29d0SLior Cohen 
1857449a29d0SLior Cohen 	vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);
1858449a29d0SLior Cohen 	if (vif)
1859449a29d0SLior Cohen 		iwl_mvm_connection_loss(mvm, vif, "missed vap beacon");
1860449a29d0SLior Cohen 
1861449a29d0SLior Cohen 	rcu_read_unlock();
1862449a29d0SLior Cohen }
1863