xref: /freebsd/sys/contrib/dev/iwlwifi/mvm/mac-ctxt.c (revision 9af1bba4)
1bfcc09ddSBjoern A. Zeeb // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2bfcc09ddSBjoern A. Zeeb /*
39af1bba4SBjoern A. Zeeb  * Copyright (C) 2012-2014, 2018-2023 Intel Corporation
4bfcc09ddSBjoern A. Zeeb  * Copyright (C) 2013-2014 Intel Mobile Communications GmbH
5bfcc09ddSBjoern A. Zeeb  * Copyright (C) 2015-2017 Intel Deutschland GmbH
6bfcc09ddSBjoern A. Zeeb  */
7bfcc09ddSBjoern A. Zeeb #include <linux/etherdevice.h>
8bfcc09ddSBjoern A. Zeeb #include <linux/crc32.h>
9bfcc09ddSBjoern A. Zeeb #include <net/mac80211.h>
10bfcc09ddSBjoern A. Zeeb #include "iwl-io.h"
11bfcc09ddSBjoern A. Zeeb #include "iwl-prph.h"
12bfcc09ddSBjoern A. Zeeb #include "fw-api.h"
13bfcc09ddSBjoern A. Zeeb #include "mvm.h"
14bfcc09ddSBjoern A. Zeeb #include "time-event.h"
15bfcc09ddSBjoern A. Zeeb 
16bfcc09ddSBjoern A. Zeeb const u8 iwl_mvm_ac_to_tx_fifo[] = {
17bfcc09ddSBjoern A. Zeeb 	IWL_MVM_TX_FIFO_VO,
18bfcc09ddSBjoern A. Zeeb 	IWL_MVM_TX_FIFO_VI,
19bfcc09ddSBjoern A. Zeeb 	IWL_MVM_TX_FIFO_BE,
20bfcc09ddSBjoern A. Zeeb 	IWL_MVM_TX_FIFO_BK,
21bfcc09ddSBjoern A. Zeeb };
22bfcc09ddSBjoern A. Zeeb 
23bfcc09ddSBjoern A. Zeeb const u8 iwl_mvm_ac_to_gen2_tx_fifo[] = {
24bfcc09ddSBjoern A. Zeeb 	IWL_GEN2_EDCA_TX_FIFO_VO,
25bfcc09ddSBjoern A. Zeeb 	IWL_GEN2_EDCA_TX_FIFO_VI,
26bfcc09ddSBjoern A. Zeeb 	IWL_GEN2_EDCA_TX_FIFO_BE,
27bfcc09ddSBjoern A. Zeeb 	IWL_GEN2_EDCA_TX_FIFO_BK,
28bfcc09ddSBjoern A. Zeeb 	IWL_GEN2_TRIG_TX_FIFO_VO,
29bfcc09ddSBjoern A. Zeeb 	IWL_GEN2_TRIG_TX_FIFO_VI,
30bfcc09ddSBjoern A. Zeeb 	IWL_GEN2_TRIG_TX_FIFO_BE,
31bfcc09ddSBjoern A. Zeeb 	IWL_GEN2_TRIG_TX_FIFO_BK,
32bfcc09ddSBjoern A. Zeeb };
33bfcc09ddSBjoern A. Zeeb 
34bfcc09ddSBjoern A. Zeeb struct iwl_mvm_mac_iface_iterator_data {
35bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm *mvm;
36bfcc09ddSBjoern A. Zeeb 	struct ieee80211_vif *vif;
37bfcc09ddSBjoern A. Zeeb 	unsigned long available_mac_ids[BITS_TO_LONGS(NUM_MAC_INDEX_DRIVER)];
38bfcc09ddSBjoern A. Zeeb 	unsigned long available_tsf_ids[BITS_TO_LONGS(NUM_TSF_IDS)];
39bfcc09ddSBjoern A. Zeeb 	enum iwl_tsf_id preferred_tsf;
40bfcc09ddSBjoern A. Zeeb 	bool found_vif;
41bfcc09ddSBjoern A. Zeeb };
42bfcc09ddSBjoern A. Zeeb 
iwl_mvm_mac_tsf_id_iter(void * _data,u8 * mac,struct ieee80211_vif * vif)43bfcc09ddSBjoern A. Zeeb static void iwl_mvm_mac_tsf_id_iter(void *_data, u8 *mac,
44bfcc09ddSBjoern A. Zeeb 				    struct ieee80211_vif *vif)
45bfcc09ddSBjoern A. Zeeb {
46bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_mac_iface_iterator_data *data = _data;
47bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
48bfcc09ddSBjoern A. Zeeb 	u16 min_bi;
49bfcc09ddSBjoern A. Zeeb 
50bfcc09ddSBjoern A. Zeeb 	/* Skip the interface for which we are trying to assign a tsf_id  */
51bfcc09ddSBjoern A. Zeeb 	if (vif == data->vif)
52bfcc09ddSBjoern A. Zeeb 		return;
53bfcc09ddSBjoern A. Zeeb 
54bfcc09ddSBjoern A. Zeeb 	/*
55bfcc09ddSBjoern A. Zeeb 	 * The TSF is a hardware/firmware resource, there are 4 and
56bfcc09ddSBjoern A. Zeeb 	 * the driver should assign and free them as needed. However,
57bfcc09ddSBjoern A. Zeeb 	 * there are cases where 2 MACs should share the same TSF ID
58bfcc09ddSBjoern A. Zeeb 	 * for the purpose of clock sync, an optimization to avoid
59bfcc09ddSBjoern A. Zeeb 	 * clock drift causing overlapping TBTTs/DTIMs for a GO and
60bfcc09ddSBjoern A. Zeeb 	 * client in the system.
61bfcc09ddSBjoern A. Zeeb 	 *
62bfcc09ddSBjoern A. Zeeb 	 * The firmware will decide according to the MAC type which
63bfcc09ddSBjoern A. Zeeb 	 * will be the leader and follower. Clients that need to sync
64bfcc09ddSBjoern A. Zeeb 	 * with a remote station will be the leader, and an AP or GO
65bfcc09ddSBjoern A. Zeeb 	 * will be the follower.
66bfcc09ddSBjoern A. Zeeb 	 *
67bfcc09ddSBjoern A. Zeeb 	 * Depending on the new interface type it can be following
68bfcc09ddSBjoern A. Zeeb 	 * or become the leader of an existing interface.
69bfcc09ddSBjoern A. Zeeb 	 */
70bfcc09ddSBjoern A. Zeeb 	switch (data->vif->type) {
71bfcc09ddSBjoern A. Zeeb 	case NL80211_IFTYPE_STATION:
72bfcc09ddSBjoern A. Zeeb 		/*
73bfcc09ddSBjoern A. Zeeb 		 * The new interface is a client, so if the one we're iterating
74bfcc09ddSBjoern A. Zeeb 		 * is an AP, and the beacon interval of the AP is a multiple or
75bfcc09ddSBjoern A. Zeeb 		 * divisor of the beacon interval of the client, the same TSF
76bfcc09ddSBjoern A. Zeeb 		 * should be used to avoid drift between the new client and
77bfcc09ddSBjoern A. Zeeb 		 * existing AP. The existing AP will get drift updates from the
78bfcc09ddSBjoern A. Zeeb 		 * new client context in this case.
79bfcc09ddSBjoern A. Zeeb 		 */
80bfcc09ddSBjoern A. Zeeb 		if (vif->type != NL80211_IFTYPE_AP ||
81bfcc09ddSBjoern A. Zeeb 		    data->preferred_tsf != NUM_TSF_IDS ||
82bfcc09ddSBjoern A. Zeeb 		    !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
83bfcc09ddSBjoern A. Zeeb 			break;
84bfcc09ddSBjoern A. Zeeb 
85bfcc09ddSBjoern A. Zeeb 		min_bi = min(data->vif->bss_conf.beacon_int,
86bfcc09ddSBjoern A. Zeeb 			     vif->bss_conf.beacon_int);
87bfcc09ddSBjoern A. Zeeb 
88bfcc09ddSBjoern A. Zeeb 		if (!min_bi)
89bfcc09ddSBjoern A. Zeeb 			break;
90bfcc09ddSBjoern A. Zeeb 
91bfcc09ddSBjoern A. Zeeb 		if ((data->vif->bss_conf.beacon_int -
92bfcc09ddSBjoern A. Zeeb 		     vif->bss_conf.beacon_int) % min_bi == 0) {
93bfcc09ddSBjoern A. Zeeb 			data->preferred_tsf = mvmvif->tsf_id;
94bfcc09ddSBjoern A. Zeeb 			return;
95bfcc09ddSBjoern A. Zeeb 		}
96bfcc09ddSBjoern A. Zeeb 		break;
97bfcc09ddSBjoern A. Zeeb 
98bfcc09ddSBjoern A. Zeeb 	case NL80211_IFTYPE_AP:
99bfcc09ddSBjoern A. Zeeb 		/*
100bfcc09ddSBjoern A. Zeeb 		 * The new interface is AP/GO, so if its beacon interval is a
101bfcc09ddSBjoern A. Zeeb 		 * multiple or a divisor of the beacon interval of an existing
102bfcc09ddSBjoern A. Zeeb 		 * interface, it should get drift updates from an existing
103bfcc09ddSBjoern A. Zeeb 		 * client or use the same TSF as an existing GO. There's no
104bfcc09ddSBjoern A. Zeeb 		 * drift between TSFs internally but if they used different
105bfcc09ddSBjoern A. Zeeb 		 * TSFs then a new client MAC could update one of them and
106bfcc09ddSBjoern A. Zeeb 		 * cause drift that way.
107bfcc09ddSBjoern A. Zeeb 		 */
108bfcc09ddSBjoern A. Zeeb 		if ((vif->type != NL80211_IFTYPE_AP &&
109bfcc09ddSBjoern A. Zeeb 		     vif->type != NL80211_IFTYPE_STATION) ||
110bfcc09ddSBjoern A. Zeeb 		    data->preferred_tsf != NUM_TSF_IDS ||
111bfcc09ddSBjoern A. Zeeb 		    !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
112bfcc09ddSBjoern A. Zeeb 			break;
113bfcc09ddSBjoern A. Zeeb 
114bfcc09ddSBjoern A. Zeeb 		min_bi = min(data->vif->bss_conf.beacon_int,
115bfcc09ddSBjoern A. Zeeb 			     vif->bss_conf.beacon_int);
116bfcc09ddSBjoern A. Zeeb 
117bfcc09ddSBjoern A. Zeeb 		if (!min_bi)
118bfcc09ddSBjoern A. Zeeb 			break;
119bfcc09ddSBjoern A. Zeeb 
120bfcc09ddSBjoern A. Zeeb 		if ((data->vif->bss_conf.beacon_int -
121bfcc09ddSBjoern A. Zeeb 		     vif->bss_conf.beacon_int) % min_bi == 0) {
122bfcc09ddSBjoern A. Zeeb 			data->preferred_tsf = mvmvif->tsf_id;
123bfcc09ddSBjoern A. Zeeb 			return;
124bfcc09ddSBjoern A. Zeeb 		}
125bfcc09ddSBjoern A. Zeeb 		break;
126bfcc09ddSBjoern A. Zeeb 	default:
127bfcc09ddSBjoern A. Zeeb 		/*
128bfcc09ddSBjoern A. Zeeb 		 * For all other interface types there's no need to
129bfcc09ddSBjoern A. Zeeb 		 * take drift into account. Either they're exclusive
130bfcc09ddSBjoern A. Zeeb 		 * like IBSS and monitor, or we don't care much about
131bfcc09ddSBjoern A. Zeeb 		 * their TSF (like P2P Device), but we won't be able
132bfcc09ddSBjoern A. Zeeb 		 * to share the TSF resource.
133bfcc09ddSBjoern A. Zeeb 		 */
134bfcc09ddSBjoern A. Zeeb 		break;
135bfcc09ddSBjoern A. Zeeb 	}
136bfcc09ddSBjoern A. Zeeb 
137bfcc09ddSBjoern A. Zeeb 	/*
138bfcc09ddSBjoern A. Zeeb 	 * Unless we exited above, we can't share the TSF resource
139bfcc09ddSBjoern A. Zeeb 	 * that the virtual interface we're iterating over is using
140bfcc09ddSBjoern A. Zeeb 	 * with the new one, so clear the available bit and if this
141bfcc09ddSBjoern A. Zeeb 	 * was the preferred one, reset that as well.
142bfcc09ddSBjoern A. Zeeb 	 */
143bfcc09ddSBjoern A. Zeeb 	__clear_bit(mvmvif->tsf_id, data->available_tsf_ids);
144bfcc09ddSBjoern A. Zeeb 
145bfcc09ddSBjoern A. Zeeb 	if (data->preferred_tsf == mvmvif->tsf_id)
146bfcc09ddSBjoern A. Zeeb 		data->preferred_tsf = NUM_TSF_IDS;
147bfcc09ddSBjoern A. Zeeb }
148bfcc09ddSBjoern A. Zeeb 
iwl_mvm_mac_iface_iterator(void * _data,u8 * mac,struct ieee80211_vif * vif)149bfcc09ddSBjoern A. Zeeb static void iwl_mvm_mac_iface_iterator(void *_data, u8 *mac,
150bfcc09ddSBjoern A. Zeeb 				       struct ieee80211_vif *vif)
151bfcc09ddSBjoern A. Zeeb {
152bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_mac_iface_iterator_data *data = _data;
153bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
154bfcc09ddSBjoern A. Zeeb 
155bfcc09ddSBjoern A. Zeeb 	/* Iterator may already find the interface being added -- skip it */
156bfcc09ddSBjoern A. Zeeb 	if (vif == data->vif) {
157bfcc09ddSBjoern A. Zeeb 		data->found_vif = true;
158bfcc09ddSBjoern A. Zeeb 		return;
159bfcc09ddSBjoern A. Zeeb 	}
160bfcc09ddSBjoern A. Zeeb 
161bfcc09ddSBjoern A. Zeeb 	/* Mark MAC IDs as used by clearing the available bit, and
162bfcc09ddSBjoern A. Zeeb 	 * (below) mark TSFs as used if their existing use is not
163bfcc09ddSBjoern A. Zeeb 	 * compatible with the new interface type.
164bfcc09ddSBjoern A. Zeeb 	 * No locking or atomic bit operations are needed since the
165bfcc09ddSBjoern A. Zeeb 	 * data is on the stack of the caller function.
166bfcc09ddSBjoern A. Zeeb 	 */
167bfcc09ddSBjoern A. Zeeb 	__clear_bit(mvmvif->id, data->available_mac_ids);
168bfcc09ddSBjoern A. Zeeb 
169bfcc09ddSBjoern A. Zeeb 	/* find a suitable tsf_id */
170bfcc09ddSBjoern A. Zeeb 	iwl_mvm_mac_tsf_id_iter(_data, mac, vif);
171bfcc09ddSBjoern A. Zeeb }
172bfcc09ddSBjoern A. Zeeb 
iwl_mvm_mac_ctxt_recalc_tsf_id(struct iwl_mvm * mvm,struct ieee80211_vif * vif)173bfcc09ddSBjoern A. Zeeb void iwl_mvm_mac_ctxt_recalc_tsf_id(struct iwl_mvm *mvm,
174bfcc09ddSBjoern A. Zeeb 				    struct ieee80211_vif *vif)
175bfcc09ddSBjoern A. Zeeb {
176bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
177bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_mac_iface_iterator_data data = {
178bfcc09ddSBjoern A. Zeeb 		.mvm = mvm,
179bfcc09ddSBjoern A. Zeeb 		.vif = vif,
180bfcc09ddSBjoern A. Zeeb 		.available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
181bfcc09ddSBjoern A. Zeeb 		/* no preference yet */
182bfcc09ddSBjoern A. Zeeb 		.preferred_tsf = NUM_TSF_IDS,
183bfcc09ddSBjoern A. Zeeb 	};
184bfcc09ddSBjoern A. Zeeb 
185bfcc09ddSBjoern A. Zeeb 	ieee80211_iterate_active_interfaces_atomic(
186bfcc09ddSBjoern A. Zeeb 		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
187bfcc09ddSBjoern A. Zeeb 		iwl_mvm_mac_tsf_id_iter, &data);
188bfcc09ddSBjoern A. Zeeb 
189bfcc09ddSBjoern A. Zeeb 	if (data.preferred_tsf != NUM_TSF_IDS)
190bfcc09ddSBjoern A. Zeeb 		mvmvif->tsf_id = data.preferred_tsf;
191bfcc09ddSBjoern A. Zeeb 	else if (!test_bit(mvmvif->tsf_id, data.available_tsf_ids))
192bfcc09ddSBjoern A. Zeeb 		mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
193bfcc09ddSBjoern A. Zeeb 						NUM_TSF_IDS);
194bfcc09ddSBjoern A. Zeeb }
195bfcc09ddSBjoern A. Zeeb 
iwl_mvm_mac_ctxt_init(struct iwl_mvm * mvm,struct ieee80211_vif * vif)196bfcc09ddSBjoern A. Zeeb int iwl_mvm_mac_ctxt_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
197bfcc09ddSBjoern A. Zeeb {
198bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
199bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_mac_iface_iterator_data data = {
200bfcc09ddSBjoern A. Zeeb 		.mvm = mvm,
201bfcc09ddSBjoern A. Zeeb 		.vif = vif,
202bfcc09ddSBjoern A. Zeeb 		.available_mac_ids = { (1 << NUM_MAC_INDEX_DRIVER) - 1 },
203bfcc09ddSBjoern A. Zeeb 		.available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
204bfcc09ddSBjoern A. Zeeb 		/* no preference yet */
205bfcc09ddSBjoern A. Zeeb 		.preferred_tsf = NUM_TSF_IDS,
206bfcc09ddSBjoern A. Zeeb 		.found_vif = false,
207bfcc09ddSBjoern A. Zeeb 	};
208bfcc09ddSBjoern A. Zeeb 	int ret, i;
209bfcc09ddSBjoern A. Zeeb 
210bfcc09ddSBjoern A. Zeeb 	lockdep_assert_held(&mvm->mutex);
211bfcc09ddSBjoern A. Zeeb 
212bfcc09ddSBjoern A. Zeeb 	/*
213bfcc09ddSBjoern A. Zeeb 	 * Allocate a MAC ID and a TSF for this MAC, along with the queues
214bfcc09ddSBjoern A. Zeeb 	 * and other resources.
215bfcc09ddSBjoern A. Zeeb 	 */
216bfcc09ddSBjoern A. Zeeb 
217bfcc09ddSBjoern A. Zeeb 	/*
218bfcc09ddSBjoern A. Zeeb 	 * Before the iterator, we start with all MAC IDs and TSFs available.
219bfcc09ddSBjoern A. Zeeb 	 *
220bfcc09ddSBjoern A. Zeeb 	 * During iteration, all MAC IDs are cleared that are in use by other
221bfcc09ddSBjoern A. Zeeb 	 * virtual interfaces, and all TSF IDs are cleared that can't be used
222bfcc09ddSBjoern A. Zeeb 	 * by this new virtual interface because they're used by an interface
223bfcc09ddSBjoern A. Zeeb 	 * that can't share it with the new one.
224bfcc09ddSBjoern A. Zeeb 	 * At the same time, we check if there's a preferred TSF in the case
225bfcc09ddSBjoern A. Zeeb 	 * that we should share it with another interface.
226bfcc09ddSBjoern A. Zeeb 	 */
227bfcc09ddSBjoern A. Zeeb 
2289af1bba4SBjoern A. Zeeb 	/* MAC ID 0 should be used only for the managed/IBSS vif with non-MLO
2299af1bba4SBjoern A. Zeeb 	 * FW API
2309af1bba4SBjoern A. Zeeb 	 */
2319af1bba4SBjoern A. Zeeb 	if (!mvm->mld_api_is_used) {
232bfcc09ddSBjoern A. Zeeb 		switch (vif->type) {
233bfcc09ddSBjoern A. Zeeb 		case NL80211_IFTYPE_ADHOC:
234bfcc09ddSBjoern A. Zeeb 			break;
235bfcc09ddSBjoern A. Zeeb 		case NL80211_IFTYPE_STATION:
236bfcc09ddSBjoern A. Zeeb 			if (!vif->p2p)
237bfcc09ddSBjoern A. Zeeb 				break;
238bfcc09ddSBjoern A. Zeeb 			fallthrough;
239bfcc09ddSBjoern A. Zeeb 		default:
240bfcc09ddSBjoern A. Zeeb 			__clear_bit(0, data.available_mac_ids);
241bfcc09ddSBjoern A. Zeeb 		}
2429af1bba4SBjoern A. Zeeb 	}
243bfcc09ddSBjoern A. Zeeb 
244bfcc09ddSBjoern A. Zeeb 	ieee80211_iterate_active_interfaces_atomic(
245bfcc09ddSBjoern A. Zeeb 		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
246bfcc09ddSBjoern A. Zeeb 		iwl_mvm_mac_iface_iterator, &data);
247bfcc09ddSBjoern A. Zeeb 
248bfcc09ddSBjoern A. Zeeb 	/*
249bfcc09ddSBjoern A. Zeeb 	 * In the case we're getting here during resume, it's similar to
250bfcc09ddSBjoern A. Zeeb 	 * firmware restart, and with RESUME_ALL the iterator will find
251bfcc09ddSBjoern A. Zeeb 	 * the vif being added already.
252bfcc09ddSBjoern A. Zeeb 	 * We don't want to reassign any IDs in either case since doing
253bfcc09ddSBjoern A. Zeeb 	 * so would probably assign different IDs (as interfaces aren't
254bfcc09ddSBjoern A. Zeeb 	 * necessarily added in the same order), but the old IDs were
255bfcc09ddSBjoern A. Zeeb 	 * preserved anyway, so skip ID assignment for both resume and
256bfcc09ddSBjoern A. Zeeb 	 * recovery.
257bfcc09ddSBjoern A. Zeeb 	 */
258bfcc09ddSBjoern A. Zeeb 	if (data.found_vif)
259bfcc09ddSBjoern A. Zeeb 		return 0;
260bfcc09ddSBjoern A. Zeeb 
261bfcc09ddSBjoern A. Zeeb 	/* Therefore, in recovery, we can't get here */
262bfcc09ddSBjoern A. Zeeb 	if (WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
263bfcc09ddSBjoern A. Zeeb 		return -EBUSY;
264bfcc09ddSBjoern A. Zeeb 
265bfcc09ddSBjoern A. Zeeb 	mvmvif->id = find_first_bit(data.available_mac_ids,
266bfcc09ddSBjoern A. Zeeb 				    NUM_MAC_INDEX_DRIVER);
267bfcc09ddSBjoern A. Zeeb 	if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
268bfcc09ddSBjoern A. Zeeb 		IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
269bfcc09ddSBjoern A. Zeeb 		ret = -EIO;
270bfcc09ddSBjoern A. Zeeb 		goto exit_fail;
271bfcc09ddSBjoern A. Zeeb 	}
272bfcc09ddSBjoern A. Zeeb 
273bfcc09ddSBjoern A. Zeeb 	if (data.preferred_tsf != NUM_TSF_IDS)
274bfcc09ddSBjoern A. Zeeb 		mvmvif->tsf_id = data.preferred_tsf;
275bfcc09ddSBjoern A. Zeeb 	else
276bfcc09ddSBjoern A. Zeeb 		mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
277bfcc09ddSBjoern A. Zeeb 						NUM_TSF_IDS);
278bfcc09ddSBjoern A. Zeeb 	if (mvmvif->tsf_id == NUM_TSF_IDS) {
279bfcc09ddSBjoern A. Zeeb 		IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
280bfcc09ddSBjoern A. Zeeb 		ret = -EIO;
281bfcc09ddSBjoern A. Zeeb 		goto exit_fail;
282bfcc09ddSBjoern A. Zeeb 	}
283bfcc09ddSBjoern A. Zeeb 
284bfcc09ddSBjoern A. Zeeb 	mvmvif->color = 0;
285bfcc09ddSBjoern A. Zeeb 
286bfcc09ddSBjoern A. Zeeb 	INIT_LIST_HEAD(&mvmvif->time_event_data.list);
287bfcc09ddSBjoern A. Zeeb 	mvmvif->time_event_data.id = TE_MAX;
288bfcc09ddSBjoern A. Zeeb 
289bfcc09ddSBjoern A. Zeeb 	/* No need to allocate data queues to P2P Device MAC and NAN.*/
290bfcc09ddSBjoern A. Zeeb 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
291bfcc09ddSBjoern A. Zeeb 		return 0;
292bfcc09ddSBjoern A. Zeeb 
293bfcc09ddSBjoern A. Zeeb 	/* Allocate the CAB queue for softAP and GO interfaces */
294bfcc09ddSBjoern A. Zeeb 	if (vif->type == NL80211_IFTYPE_AP ||
295bfcc09ddSBjoern A. Zeeb 	    vif->type == NL80211_IFTYPE_ADHOC) {
296bfcc09ddSBjoern A. Zeeb 		/*
297bfcc09ddSBjoern A. Zeeb 		 * For TVQM this will be overwritten later with the FW assigned
298bfcc09ddSBjoern A. Zeeb 		 * queue value (when queue is enabled).
299bfcc09ddSBjoern A. Zeeb 		 */
3009af1bba4SBjoern A. Zeeb 		mvmvif->deflink.cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
301bfcc09ddSBjoern A. Zeeb 	}
302bfcc09ddSBjoern A. Zeeb 
3039af1bba4SBjoern A. Zeeb 	mvmvif->deflink.bcast_sta.sta_id = IWL_MVM_INVALID_STA;
3049af1bba4SBjoern A. Zeeb 	mvmvif->deflink.mcast_sta.sta_id = IWL_MVM_INVALID_STA;
3059af1bba4SBjoern A. Zeeb 	mvmvif->deflink.ap_sta_id = IWL_MVM_INVALID_STA;
306bfcc09ddSBjoern A. Zeeb 
307bfcc09ddSBjoern A. Zeeb 	for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
3089af1bba4SBjoern A. Zeeb 		mvmvif->deflink.smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
309bfcc09ddSBjoern A. Zeeb 
310bfcc09ddSBjoern A. Zeeb 	return 0;
311bfcc09ddSBjoern A. Zeeb 
312bfcc09ddSBjoern A. Zeeb exit_fail:
313bfcc09ddSBjoern A. Zeeb 	memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
314bfcc09ddSBjoern A. Zeeb 	return ret;
315bfcc09ddSBjoern A. Zeeb }
316bfcc09ddSBjoern A. Zeeb 
iwl_mvm_ack_rates(struct iwl_mvm * mvm,struct ieee80211_vif * vif,enum nl80211_band band,u8 * cck_rates,u8 * ofdm_rates)317bfcc09ddSBjoern A. Zeeb static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
318bfcc09ddSBjoern A. Zeeb 			      struct ieee80211_vif *vif,
319bfcc09ddSBjoern A. Zeeb 			      enum nl80211_band band,
320bfcc09ddSBjoern A. Zeeb 			      u8 *cck_rates, u8 *ofdm_rates)
321bfcc09ddSBjoern A. Zeeb {
322bfcc09ddSBjoern A. Zeeb 	struct ieee80211_supported_band *sband;
323bfcc09ddSBjoern A. Zeeb 	unsigned long basic = vif->bss_conf.basic_rates;
324bfcc09ddSBjoern A. Zeeb 	int lowest_present_ofdm = 100;
325bfcc09ddSBjoern A. Zeeb 	int lowest_present_cck = 100;
326bfcc09ddSBjoern A. Zeeb 	u8 cck = 0;
327bfcc09ddSBjoern A. Zeeb 	u8 ofdm = 0;
328bfcc09ddSBjoern A. Zeeb 	int i;
329bfcc09ddSBjoern A. Zeeb 
330bfcc09ddSBjoern A. Zeeb 	sband = mvm->hw->wiphy->bands[band];
331bfcc09ddSBjoern A. Zeeb 
332bfcc09ddSBjoern A. Zeeb 	for_each_set_bit(i, &basic, BITS_PER_LONG) {
333bfcc09ddSBjoern A. Zeeb 		int hw = sband->bitrates[i].hw_value;
334bfcc09ddSBjoern A. Zeeb 		if (hw >= IWL_FIRST_OFDM_RATE) {
335bfcc09ddSBjoern A. Zeeb 			ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
336bfcc09ddSBjoern A. Zeeb 			if (lowest_present_ofdm > hw)
337bfcc09ddSBjoern A. Zeeb 				lowest_present_ofdm = hw;
338bfcc09ddSBjoern A. Zeeb 		} else {
339bfcc09ddSBjoern A. Zeeb 			BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
340bfcc09ddSBjoern A. Zeeb 
341bfcc09ddSBjoern A. Zeeb 			cck |= BIT(hw);
342bfcc09ddSBjoern A. Zeeb 			if (lowest_present_cck > hw)
343bfcc09ddSBjoern A. Zeeb 				lowest_present_cck = hw;
344bfcc09ddSBjoern A. Zeeb 		}
345bfcc09ddSBjoern A. Zeeb 	}
346bfcc09ddSBjoern A. Zeeb 
347bfcc09ddSBjoern A. Zeeb 	/*
348bfcc09ddSBjoern A. Zeeb 	 * Now we've got the basic rates as bitmaps in the ofdm and cck
349bfcc09ddSBjoern A. Zeeb 	 * variables. This isn't sufficient though, as there might not
350bfcc09ddSBjoern A. Zeeb 	 * be all the right rates in the bitmap. E.g. if the only basic
351bfcc09ddSBjoern A. Zeeb 	 * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
352bfcc09ddSBjoern A. Zeeb 	 * and 6 Mbps because the 802.11-2007 standard says in 9.6:
353bfcc09ddSBjoern A. Zeeb 	 *
354bfcc09ddSBjoern A. Zeeb 	 *    [...] a STA responding to a received frame shall transmit
355bfcc09ddSBjoern A. Zeeb 	 *    its Control Response frame [...] at the highest rate in the
356bfcc09ddSBjoern A. Zeeb 	 *    BSSBasicRateSet parameter that is less than or equal to the
357bfcc09ddSBjoern A. Zeeb 	 *    rate of the immediately previous frame in the frame exchange
358bfcc09ddSBjoern A. Zeeb 	 *    sequence ([...]) and that is of the same modulation class
359bfcc09ddSBjoern A. Zeeb 	 *    ([...]) as the received frame. If no rate contained in the
360bfcc09ddSBjoern A. Zeeb 	 *    BSSBasicRateSet parameter meets these conditions, then the
361bfcc09ddSBjoern A. Zeeb 	 *    control frame sent in response to a received frame shall be
362bfcc09ddSBjoern A. Zeeb 	 *    transmitted at the highest mandatory rate of the PHY that is
363bfcc09ddSBjoern A. Zeeb 	 *    less than or equal to the rate of the received frame, and
364bfcc09ddSBjoern A. Zeeb 	 *    that is of the same modulation class as the received frame.
365bfcc09ddSBjoern A. Zeeb 	 *
366bfcc09ddSBjoern A. Zeeb 	 * As a consequence, we need to add all mandatory rates that are
367bfcc09ddSBjoern A. Zeeb 	 * lower than all of the basic rates to these bitmaps.
368bfcc09ddSBjoern A. Zeeb 	 */
369bfcc09ddSBjoern A. Zeeb 
370bfcc09ddSBjoern A. Zeeb 	if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
371bfcc09ddSBjoern A. Zeeb 		ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
372bfcc09ddSBjoern A. Zeeb 	if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
373bfcc09ddSBjoern A. Zeeb 		ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
374bfcc09ddSBjoern A. Zeeb 	/* 6M already there or needed so always add */
375bfcc09ddSBjoern A. Zeeb 	ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
376bfcc09ddSBjoern A. Zeeb 
377bfcc09ddSBjoern A. Zeeb 	/*
378bfcc09ddSBjoern A. Zeeb 	 * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
379bfcc09ddSBjoern A. Zeeb 	 * Note, however:
380bfcc09ddSBjoern A. Zeeb 	 *  - if no CCK rates are basic, it must be ERP since there must
381bfcc09ddSBjoern A. Zeeb 	 *    be some basic rates at all, so they're OFDM => ERP PHY
382bfcc09ddSBjoern A. Zeeb 	 *    (or we're in 5 GHz, and the cck bitmap will never be used)
383bfcc09ddSBjoern A. Zeeb 	 *  - if 11M is a basic rate, it must be ERP as well, so add 5.5M
384bfcc09ddSBjoern A. Zeeb 	 *  - if 5.5M is basic, 1M and 2M are mandatory
385bfcc09ddSBjoern A. Zeeb 	 *  - if 2M is basic, 1M is mandatory
386bfcc09ddSBjoern A. Zeeb 	 *  - if 1M is basic, that's the only valid ACK rate.
387bfcc09ddSBjoern A. Zeeb 	 * As a consequence, it's not as complicated as it sounds, just add
388bfcc09ddSBjoern A. Zeeb 	 * any lower rates to the ACK rate bitmap.
389bfcc09ddSBjoern A. Zeeb 	 */
390bfcc09ddSBjoern A. Zeeb 	if (IWL_RATE_11M_INDEX < lowest_present_cck)
391bfcc09ddSBjoern A. Zeeb 		cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
392bfcc09ddSBjoern A. Zeeb 	if (IWL_RATE_5M_INDEX < lowest_present_cck)
393bfcc09ddSBjoern A. Zeeb 		cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
394bfcc09ddSBjoern A. Zeeb 	if (IWL_RATE_2M_INDEX < lowest_present_cck)
395bfcc09ddSBjoern A. Zeeb 		cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
396bfcc09ddSBjoern A. Zeeb 	/* 1M already there or needed so always add */
397bfcc09ddSBjoern A. Zeeb 	cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
398bfcc09ddSBjoern A. Zeeb 
399bfcc09ddSBjoern A. Zeeb 	*cck_rates = cck;
400bfcc09ddSBjoern A. Zeeb 	*ofdm_rates = ofdm;
401bfcc09ddSBjoern A. Zeeb }
402bfcc09ddSBjoern A. Zeeb 
iwl_mvm_set_fw_basic_rates(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf,__le32 * cck_rates,__le32 * ofdm_rates)4039af1bba4SBjoern A. Zeeb void iwl_mvm_set_fw_basic_rates(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
4049af1bba4SBjoern A. Zeeb 				struct ieee80211_bss_conf *link_conf,
4059af1bba4SBjoern A. Zeeb 				__le32 *cck_rates, __le32 *ofdm_rates)
4069af1bba4SBjoern A. Zeeb {
4079af1bba4SBjoern A. Zeeb 	struct ieee80211_chanctx_conf *chanctx;
4089af1bba4SBjoern A. Zeeb 	u8 cck_ack_rates = 0, ofdm_ack_rates = 0;
4099af1bba4SBjoern A. Zeeb 
4109af1bba4SBjoern A. Zeeb 	rcu_read_lock();
4119af1bba4SBjoern A. Zeeb 	chanctx = rcu_dereference(link_conf->chanctx_conf);
4129af1bba4SBjoern A. Zeeb 	iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
4139af1bba4SBjoern A. Zeeb 					    : NL80211_BAND_2GHZ,
4149af1bba4SBjoern A. Zeeb 			  &cck_ack_rates, &ofdm_ack_rates);
4159af1bba4SBjoern A. Zeeb 
4169af1bba4SBjoern A. Zeeb 	rcu_read_unlock();
4179af1bba4SBjoern A. Zeeb 
4189af1bba4SBjoern A. Zeeb 	*cck_rates = cpu_to_le32((u32)cck_ack_rates);
4199af1bba4SBjoern A. Zeeb 	*ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
4209af1bba4SBjoern A. Zeeb }
4219af1bba4SBjoern A. Zeeb 
iwl_mvm_set_fw_protection_flags(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf,__le32 * protection_flags,u32 ht_flag,u32 tgg_flag)4229af1bba4SBjoern A. Zeeb void iwl_mvm_set_fw_protection_flags(struct iwl_mvm *mvm,
423bfcc09ddSBjoern A. Zeeb 				     struct ieee80211_vif *vif,
4249af1bba4SBjoern A. Zeeb 				     struct ieee80211_bss_conf *link_conf,
4259af1bba4SBjoern A. Zeeb 				     __le32 *protection_flags, u32 ht_flag,
4269af1bba4SBjoern A. Zeeb 				     u32 tgg_flag)
427bfcc09ddSBjoern A. Zeeb {
428bfcc09ddSBjoern A. Zeeb 	/* for both sta and ap, ht_operation_mode hold the protection_mode */
4299af1bba4SBjoern A. Zeeb 	u8 protection_mode = link_conf->ht_operation_mode &
430bfcc09ddSBjoern A. Zeeb 				 IEEE80211_HT_OP_MODE_PROTECTION;
4319af1bba4SBjoern A. Zeeb 	bool ht_enabled = !!(link_conf->ht_operation_mode &
4329af1bba4SBjoern A. Zeeb 			     IEEE80211_HT_OP_MODE_PROTECTION);
4339af1bba4SBjoern A. Zeeb 
4349af1bba4SBjoern A. Zeeb 	if (link_conf->use_cts_prot)
4359af1bba4SBjoern A. Zeeb 		*protection_flags |= cpu_to_le32(tgg_flag);
4369af1bba4SBjoern A. Zeeb 
4379af1bba4SBjoern A. Zeeb 	IWL_DEBUG_RATE(mvm, "use_cts_prot %d, ht_operation_mode %d\n",
4389af1bba4SBjoern A. Zeeb 		       link_conf->use_cts_prot,
4399af1bba4SBjoern A. Zeeb 		       link_conf->ht_operation_mode);
4409af1bba4SBjoern A. Zeeb 
4419af1bba4SBjoern A. Zeeb 	if (!ht_enabled)
4429af1bba4SBjoern A. Zeeb 		return;
443bfcc09ddSBjoern A. Zeeb 
444bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_RATE(mvm, "protection mode set to %d\n", protection_mode);
445bfcc09ddSBjoern A. Zeeb 	/*
446bfcc09ddSBjoern A. Zeeb 	 * See section 9.23.3.1 of IEEE 80211-2012.
447bfcc09ddSBjoern A. Zeeb 	 * Nongreenfield HT STAs Present is not supported.
448bfcc09ddSBjoern A. Zeeb 	 */
449bfcc09ddSBjoern A. Zeeb 	switch (protection_mode) {
450bfcc09ddSBjoern A. Zeeb 	case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
451bfcc09ddSBjoern A. Zeeb 		break;
452bfcc09ddSBjoern A. Zeeb 	case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
453bfcc09ddSBjoern A. Zeeb 	case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
4549af1bba4SBjoern A. Zeeb 		*protection_flags |= cpu_to_le32(ht_flag);
455bfcc09ddSBjoern A. Zeeb 		break;
456bfcc09ddSBjoern A. Zeeb 	case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
457bfcc09ddSBjoern A. Zeeb 		/* Protect when channel wider than 20MHz */
4589af1bba4SBjoern A. Zeeb 		if (link_conf->chandef.width > NL80211_CHAN_WIDTH_20)
4599af1bba4SBjoern A. Zeeb 			*protection_flags |= cpu_to_le32(ht_flag);
460bfcc09ddSBjoern A. Zeeb 		break;
461bfcc09ddSBjoern A. Zeeb 	default:
462bfcc09ddSBjoern A. Zeeb 		IWL_ERR(mvm, "Illegal protection mode %d\n",
463bfcc09ddSBjoern A. Zeeb 			protection_mode);
464bfcc09ddSBjoern A. Zeeb 		break;
465bfcc09ddSBjoern A. Zeeb 	}
466bfcc09ddSBjoern A. Zeeb }
467bfcc09ddSBjoern A. Zeeb 
iwl_mvm_set_fw_qos_params(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf,struct iwl_ac_qos * ac,__le32 * qos_flags)4689af1bba4SBjoern A. Zeeb void iwl_mvm_set_fw_qos_params(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
4699af1bba4SBjoern A. Zeeb 			       struct ieee80211_bss_conf *link_conf,
4709af1bba4SBjoern A. Zeeb 			       struct iwl_ac_qos *ac, __le32 *qos_flags)
4719af1bba4SBjoern A. Zeeb {
4729af1bba4SBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4739af1bba4SBjoern A. Zeeb 	struct iwl_mvm_vif_link_info *mvm_link =
4749af1bba4SBjoern A. Zeeb 		mvmvif->link[link_conf->link_id];
4759af1bba4SBjoern A. Zeeb 	int i;
4769af1bba4SBjoern A. Zeeb 
4779af1bba4SBjoern A. Zeeb 	if (!mvm_link)
4789af1bba4SBjoern A. Zeeb 		return;
4799af1bba4SBjoern A. Zeeb 
4809af1bba4SBjoern A. Zeeb 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
4819af1bba4SBjoern A. Zeeb 		u8 txf = iwl_mvm_mac_ac_to_tx_fifo(mvm, i);
4829af1bba4SBjoern A. Zeeb 		u8 ucode_ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);
4839af1bba4SBjoern A. Zeeb 
4849af1bba4SBjoern A. Zeeb 		ac[ucode_ac].cw_min =
4859af1bba4SBjoern A. Zeeb 			cpu_to_le16(mvm_link->queue_params[i].cw_min);
4869af1bba4SBjoern A. Zeeb 		ac[ucode_ac].cw_max =
4879af1bba4SBjoern A. Zeeb 			cpu_to_le16(mvm_link->queue_params[i].cw_max);
4889af1bba4SBjoern A. Zeeb 		ac[ucode_ac].edca_txop =
4899af1bba4SBjoern A. Zeeb 			cpu_to_le16(mvm_link->queue_params[i].txop * 32);
4909af1bba4SBjoern A. Zeeb 		ac[ucode_ac].aifsn = mvm_link->queue_params[i].aifs;
4919af1bba4SBjoern A. Zeeb 		ac[ucode_ac].fifos_mask = BIT(txf);
4929af1bba4SBjoern A. Zeeb 	}
4939af1bba4SBjoern A. Zeeb 
4949af1bba4SBjoern A. Zeeb 	if (link_conf->qos)
4959af1bba4SBjoern A. Zeeb 		*qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
4969af1bba4SBjoern A. Zeeb 
4979af1bba4SBjoern A. Zeeb 	if (link_conf->chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
4989af1bba4SBjoern A. Zeeb 		*qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
4999af1bba4SBjoern A. Zeeb }
5009af1bba4SBjoern A. Zeeb 
iwl_mvm_get_mac_type(struct ieee80211_vif * vif)5019af1bba4SBjoern A. Zeeb int iwl_mvm_get_mac_type(struct ieee80211_vif *vif)
5029af1bba4SBjoern A. Zeeb {
5039af1bba4SBjoern A. Zeeb 	u32 mac_type = FW_MAC_TYPE_BSS_STA;
5049af1bba4SBjoern A. Zeeb 
5059af1bba4SBjoern A. Zeeb 	switch (vif->type) {
5069af1bba4SBjoern A. Zeeb 	case NL80211_IFTYPE_STATION:
5079af1bba4SBjoern A. Zeeb 		if (vif->p2p)
5089af1bba4SBjoern A. Zeeb 			mac_type = FW_MAC_TYPE_P2P_STA;
5099af1bba4SBjoern A. Zeeb 		else
5109af1bba4SBjoern A. Zeeb 			mac_type = FW_MAC_TYPE_BSS_STA;
5119af1bba4SBjoern A. Zeeb 		break;
5129af1bba4SBjoern A. Zeeb 	case NL80211_IFTYPE_AP:
5139af1bba4SBjoern A. Zeeb 		mac_type = FW_MAC_TYPE_GO;
5149af1bba4SBjoern A. Zeeb 		break;
5159af1bba4SBjoern A. Zeeb 	case NL80211_IFTYPE_MONITOR:
5169af1bba4SBjoern A. Zeeb 		mac_type = FW_MAC_TYPE_LISTENER;
5179af1bba4SBjoern A. Zeeb 		break;
5189af1bba4SBjoern A. Zeeb 	case NL80211_IFTYPE_P2P_DEVICE:
5199af1bba4SBjoern A. Zeeb 		mac_type = FW_MAC_TYPE_P2P_DEVICE;
5209af1bba4SBjoern A. Zeeb 		break;
5219af1bba4SBjoern A. Zeeb 	case NL80211_IFTYPE_ADHOC:
5229af1bba4SBjoern A. Zeeb 		mac_type = FW_MAC_TYPE_IBSS;
5239af1bba4SBjoern A. Zeeb 		break;
5249af1bba4SBjoern A. Zeeb 	default:
5259af1bba4SBjoern A. Zeeb 		WARN_ON_ONCE(1);
5269af1bba4SBjoern A. Zeeb 	}
5279af1bba4SBjoern A. Zeeb 	return mac_type;
5289af1bba4SBjoern A. Zeeb }
5299af1bba4SBjoern A. Zeeb 
iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_mac_ctx_cmd * cmd,const u8 * bssid_override,u32 action)530bfcc09ddSBjoern A. Zeeb static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
531bfcc09ddSBjoern A. Zeeb 					struct ieee80211_vif *vif,
532bfcc09ddSBjoern A. Zeeb 					struct iwl_mac_ctx_cmd *cmd,
533bfcc09ddSBjoern A. Zeeb 					const u8 *bssid_override,
534bfcc09ddSBjoern A. Zeeb 					u32 action)
535bfcc09ddSBjoern A. Zeeb {
536bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
537bfcc09ddSBjoern A. Zeeb 	const u8 *bssid = bssid_override ?: vif->bss_conf.bssid;
5389af1bba4SBjoern A. Zeeb 	u32 ht_flag;
539bfcc09ddSBjoern A. Zeeb 
540bfcc09ddSBjoern A. Zeeb 	cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
541bfcc09ddSBjoern A. Zeeb 							    mvmvif->color));
542bfcc09ddSBjoern A. Zeeb 	cmd->action = cpu_to_le32(action);
5439af1bba4SBjoern A. Zeeb 	cmd->mac_type = cpu_to_le32(iwl_mvm_get_mac_type(vif));
544bfcc09ddSBjoern A. Zeeb 
545bfcc09ddSBjoern A. Zeeb 	cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
546bfcc09ddSBjoern A. Zeeb 
547bfcc09ddSBjoern A. Zeeb 	memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
548bfcc09ddSBjoern A. Zeeb 
549bfcc09ddSBjoern A. Zeeb 	if (bssid)
550bfcc09ddSBjoern A. Zeeb 		memcpy(cmd->bssid_addr, bssid, ETH_ALEN);
551bfcc09ddSBjoern A. Zeeb 	else
552bfcc09ddSBjoern A. Zeeb 		eth_broadcast_addr(cmd->bssid_addr);
553bfcc09ddSBjoern A. Zeeb 
5549af1bba4SBjoern A. Zeeb 	iwl_mvm_set_fw_basic_rates(mvm, vif, &vif->bss_conf, &cmd->cck_rates,
5559af1bba4SBjoern A. Zeeb 				   &cmd->ofdm_rates);
556bfcc09ddSBjoern A. Zeeb 
557bfcc09ddSBjoern A. Zeeb 	cmd->cck_short_preamble =
558bfcc09ddSBjoern A. Zeeb 		cpu_to_le32(vif->bss_conf.use_short_preamble ?
559bfcc09ddSBjoern A. Zeeb 			    MAC_FLG_SHORT_PREAMBLE : 0);
560bfcc09ddSBjoern A. Zeeb 	cmd->short_slot =
561bfcc09ddSBjoern A. Zeeb 		cpu_to_le32(vif->bss_conf.use_short_slot ?
562bfcc09ddSBjoern A. Zeeb 			    MAC_FLG_SHORT_SLOT : 0);
563bfcc09ddSBjoern A. Zeeb 
564bfcc09ddSBjoern A. Zeeb 	cmd->filter_flags = 0;
565bfcc09ddSBjoern A. Zeeb 
5669af1bba4SBjoern A. Zeeb 	iwl_mvm_set_fw_qos_params(mvm, vif, &vif->bss_conf, cmd->ac,
5679af1bba4SBjoern A. Zeeb 				  &cmd->qos_flags);
568bfcc09ddSBjoern A. Zeeb 
5699af1bba4SBjoern A. Zeeb 	/* The fw does not distinguish between ht and fat */
5709af1bba4SBjoern A. Zeeb 	ht_flag = MAC_PROT_FLG_HT_PROT | MAC_PROT_FLG_FAT_PROT;
5719af1bba4SBjoern A. Zeeb 	iwl_mvm_set_fw_protection_flags(mvm, vif, &vif->bss_conf,
5729af1bba4SBjoern A. Zeeb 					&cmd->protection_flags,
5739af1bba4SBjoern A. Zeeb 					ht_flag, MAC_PROT_FLG_TGG_PROTECT);
574bfcc09ddSBjoern A. Zeeb }
575bfcc09ddSBjoern A. Zeeb 
iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm * mvm,struct iwl_mac_ctx_cmd * cmd)576bfcc09ddSBjoern A. Zeeb static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
577bfcc09ddSBjoern A. Zeeb 				     struct iwl_mac_ctx_cmd *cmd)
578bfcc09ddSBjoern A. Zeeb {
579bfcc09ddSBjoern A. Zeeb 	int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
580bfcc09ddSBjoern A. Zeeb 				       sizeof(*cmd), cmd);
581bfcc09ddSBjoern A. Zeeb 	if (ret)
5829af1bba4SBjoern A. Zeeb 		IWL_ERR(mvm, "Failed to send MAC_CONTEXT_CMD (action:%d): %d\n",
583bfcc09ddSBjoern A. Zeeb 			le32_to_cpu(cmd->action), ret);
584bfcc09ddSBjoern A. Zeeb 	return ret;
585bfcc09ddSBjoern A. Zeeb }
586bfcc09ddSBjoern A. Zeeb 
iwl_mvm_set_fw_dtim_tbtt(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf,__le64 * dtim_tsf,__le32 * dtim_time,__le32 * assoc_beacon_arrive_time)5879af1bba4SBjoern A. Zeeb void iwl_mvm_set_fw_dtim_tbtt(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
5889af1bba4SBjoern A. Zeeb 			      struct ieee80211_bss_conf *link_conf,
5899af1bba4SBjoern A. Zeeb 			      __le64 *dtim_tsf, __le32 *dtim_time,
5909af1bba4SBjoern A. Zeeb 			      __le32 *assoc_beacon_arrive_time)
591bfcc09ddSBjoern A. Zeeb {
592bfcc09ddSBjoern A. Zeeb 	u32 dtim_offs;
593bfcc09ddSBjoern A. Zeeb 
594bfcc09ddSBjoern A. Zeeb 	/*
595bfcc09ddSBjoern A. Zeeb 	 * The DTIM count counts down, so when it is N that means N
596bfcc09ddSBjoern A. Zeeb 	 * more beacon intervals happen until the DTIM TBTT. Therefore
597bfcc09ddSBjoern A. Zeeb 	 * add this to the current time. If that ends up being in the
598bfcc09ddSBjoern A. Zeeb 	 * future, the firmware will handle it.
599bfcc09ddSBjoern A. Zeeb 	 *
600bfcc09ddSBjoern A. Zeeb 	 * Also note that the system_timestamp (which we get here as
601bfcc09ddSBjoern A. Zeeb 	 * "sync_device_ts") and TSF timestamp aren't at exactly the
602bfcc09ddSBjoern A. Zeeb 	 * same offset in the frame -- the TSF is at the first symbol
603bfcc09ddSBjoern A. Zeeb 	 * of the TSF, the system timestamp is at signal acquisition
604bfcc09ddSBjoern A. Zeeb 	 * time. This means there's an offset between them of at most
605bfcc09ddSBjoern A. Zeeb 	 * a few hundred microseconds (24 * 8 bits + PLCP time gives
606bfcc09ddSBjoern A. Zeeb 	 * 384us in the longest case), this is currently not relevant
607bfcc09ddSBjoern A. Zeeb 	 * as the firmware wakes up around 2ms before the TBTT.
608bfcc09ddSBjoern A. Zeeb 	 */
6099af1bba4SBjoern A. Zeeb 	dtim_offs = link_conf->sync_dtim_count *
6109af1bba4SBjoern A. Zeeb 			link_conf->beacon_int;
611bfcc09ddSBjoern A. Zeeb 	/* convert TU to usecs */
612bfcc09ddSBjoern A. Zeeb 	dtim_offs *= 1024;
613bfcc09ddSBjoern A. Zeeb 
6149af1bba4SBjoern A. Zeeb 	*dtim_tsf =
6159af1bba4SBjoern A. Zeeb 		cpu_to_le64(link_conf->sync_tsf + dtim_offs);
6169af1bba4SBjoern A. Zeeb 	*dtim_time =
6179af1bba4SBjoern A. Zeeb 		cpu_to_le32(link_conf->sync_device_ts + dtim_offs);
6189af1bba4SBjoern A. Zeeb 	*assoc_beacon_arrive_time =
6199af1bba4SBjoern A. Zeeb 		cpu_to_le32(link_conf->sync_device_ts);
620bfcc09ddSBjoern A. Zeeb 
621bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
6229af1bba4SBjoern A. Zeeb 		       le64_to_cpu(*dtim_tsf),
6239af1bba4SBjoern A. Zeeb 		       le32_to_cpu(*dtim_time),
624bfcc09ddSBjoern A. Zeeb 		       dtim_offs);
6259af1bba4SBjoern A. Zeeb }
6269af1bba4SBjoern A. Zeeb 
iwl_mvm_mac_ctxt_cmd_p2p_sta_get_oppps_ctwin(struct iwl_mvm * mvm,struct ieee80211_vif * vif)6279af1bba4SBjoern A. Zeeb __le32 iwl_mvm_mac_ctxt_cmd_p2p_sta_get_oppps_ctwin(struct iwl_mvm *mvm,
6289af1bba4SBjoern A. Zeeb 						    struct ieee80211_vif *vif)
6299af1bba4SBjoern A. Zeeb {
6309af1bba4SBjoern A. Zeeb 	struct ieee80211_p2p_noa_attr *noa =
6319af1bba4SBjoern A. Zeeb 		&vif->bss_conf.p2p_noa_attr;
6329af1bba4SBjoern A. Zeeb 
6339af1bba4SBjoern A. Zeeb 	return cpu_to_le32(noa->oppps_ctwindow &
6349af1bba4SBjoern A. Zeeb 			IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
6359af1bba4SBjoern A. Zeeb }
6369af1bba4SBjoern A. Zeeb 
iwl_mvm_mac_ctxt_cmd_sta_get_twt_policy(struct iwl_mvm * mvm,struct ieee80211_vif * vif)6379af1bba4SBjoern A. Zeeb u32 iwl_mvm_mac_ctxt_cmd_sta_get_twt_policy(struct iwl_mvm *mvm,
6389af1bba4SBjoern A. Zeeb 					    struct ieee80211_vif *vif)
6399af1bba4SBjoern A. Zeeb {
6409af1bba4SBjoern A. Zeeb 	u32 twt_policy = 0;
6419af1bba4SBjoern A. Zeeb 
6429af1bba4SBjoern A. Zeeb 	if (vif->bss_conf.twt_requester && IWL_MVM_USE_TWT)
6439af1bba4SBjoern A. Zeeb 		twt_policy |= TWT_SUPPORTED;
6449af1bba4SBjoern A. Zeeb 	if (vif->bss_conf.twt_protected)
6459af1bba4SBjoern A. Zeeb 		twt_policy |= PROTECTED_TWT_SUPPORTED;
6469af1bba4SBjoern A. Zeeb 	if (vif->bss_conf.twt_broadcast)
6479af1bba4SBjoern A. Zeeb 		twt_policy |= BROADCAST_TWT_SUPPORTED;
6489af1bba4SBjoern A. Zeeb 
6499af1bba4SBjoern A. Zeeb 	return twt_policy;
6509af1bba4SBjoern A. Zeeb }
6519af1bba4SBjoern A. Zeeb 
iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm * mvm,struct ieee80211_vif * vif,u32 action,bool force_assoc_off,const u8 * bssid_override)6529af1bba4SBjoern A. Zeeb static int iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm *mvm,
6539af1bba4SBjoern A. Zeeb 				    struct ieee80211_vif *vif,
6549af1bba4SBjoern A. Zeeb 				    u32 action, bool force_assoc_off,
6559af1bba4SBjoern A. Zeeb 				    const u8 *bssid_override)
6569af1bba4SBjoern A. Zeeb {
6579af1bba4SBjoern A. Zeeb 	struct iwl_mac_ctx_cmd cmd = {};
6589af1bba4SBjoern A. Zeeb 	struct iwl_mac_data_sta *ctxt_sta;
6599af1bba4SBjoern A. Zeeb 
6609af1bba4SBjoern A. Zeeb 	WARN_ON(vif->type != NL80211_IFTYPE_STATION);
6619af1bba4SBjoern A. Zeeb 
6629af1bba4SBjoern A. Zeeb 	/* Fill the common data for all mac context types */
6639af1bba4SBjoern A. Zeeb 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, bssid_override, action);
6649af1bba4SBjoern A. Zeeb 
6659af1bba4SBjoern A. Zeeb 	/*
6669af1bba4SBjoern A. Zeeb 	 * We always want to hear MCAST frames, if we're not authorized yet,
6679af1bba4SBjoern A. Zeeb 	 * we'll drop them.
6689af1bba4SBjoern A. Zeeb 	 */
6699af1bba4SBjoern A. Zeeb 	cmd.filter_flags |= cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
6709af1bba4SBjoern A. Zeeb 
6719af1bba4SBjoern A. Zeeb 	if (vif->p2p) {
6729af1bba4SBjoern A. Zeeb 		cmd.p2p_sta.ctwin =
6739af1bba4SBjoern A. Zeeb 			iwl_mvm_mac_ctxt_cmd_p2p_sta_get_oppps_ctwin(mvm, vif);
6749af1bba4SBjoern A. Zeeb 
6759af1bba4SBjoern A. Zeeb 		ctxt_sta = &cmd.p2p_sta.sta;
6769af1bba4SBjoern A. Zeeb 	} else {
6779af1bba4SBjoern A. Zeeb 		ctxt_sta = &cmd.sta;
6789af1bba4SBjoern A. Zeeb 	}
6799af1bba4SBjoern A. Zeeb 
6809af1bba4SBjoern A. Zeeb 	/* We need the dtim_period to set the MAC as associated */
6819af1bba4SBjoern A. Zeeb 	if (vif->cfg.assoc && vif->bss_conf.dtim_period &&
6829af1bba4SBjoern A. Zeeb 	    !force_assoc_off) {
6839af1bba4SBjoern A. Zeeb 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
6849af1bba4SBjoern A. Zeeb 
6859af1bba4SBjoern A. Zeeb 		iwl_mvm_set_fw_dtim_tbtt(mvm, vif, &vif->bss_conf,
6869af1bba4SBjoern A. Zeeb 					 &ctxt_sta->dtim_tsf,
6879af1bba4SBjoern A. Zeeb 					 &ctxt_sta->dtim_time,
6889af1bba4SBjoern A. Zeeb 					 &ctxt_sta->assoc_beacon_arrive_time);
689bfcc09ddSBjoern A. Zeeb 
690bfcc09ddSBjoern A. Zeeb 		ctxt_sta->is_assoc = cpu_to_le32(1);
691bfcc09ddSBjoern A. Zeeb 
692bfcc09ddSBjoern A. Zeeb 		if (!mvmvif->authorized &&
693bfcc09ddSBjoern A. Zeeb 		    fw_has_capa(&mvm->fw->ucode_capa,
694bfcc09ddSBjoern A. Zeeb 				IWL_UCODE_TLV_CAPA_COEX_HIGH_PRIO))
695bfcc09ddSBjoern A. Zeeb 			ctxt_sta->data_policy |=
696bfcc09ddSBjoern A. Zeeb 				cpu_to_le32(COEX_HIGH_PRIORITY_ENABLE);
697bfcc09ddSBjoern A. Zeeb 	} else {
698bfcc09ddSBjoern A. Zeeb 		ctxt_sta->is_assoc = cpu_to_le32(0);
699bfcc09ddSBjoern A. Zeeb 
700bfcc09ddSBjoern A. Zeeb 		/* Allow beacons to pass through as long as we are not
701bfcc09ddSBjoern A. Zeeb 		 * associated, or we do not have dtim period information.
702bfcc09ddSBjoern A. Zeeb 		 */
703bfcc09ddSBjoern A. Zeeb 		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
704bfcc09ddSBjoern A. Zeeb 	}
705bfcc09ddSBjoern A. Zeeb 
706bfcc09ddSBjoern A. Zeeb 	ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
707bfcc09ddSBjoern A. Zeeb 	ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
708bfcc09ddSBjoern A. Zeeb 					      vif->bss_conf.dtim_period);
709bfcc09ddSBjoern A. Zeeb 
710bfcc09ddSBjoern A. Zeeb 	ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
7119af1bba4SBjoern A. Zeeb 	ctxt_sta->assoc_id = cpu_to_le32(vif->cfg.aid);
712bfcc09ddSBjoern A. Zeeb 
7139af1bba4SBjoern A. Zeeb 	if (vif->probe_req_reg && vif->cfg.assoc && vif->p2p)
714bfcc09ddSBjoern A. Zeeb 		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
715bfcc09ddSBjoern A. Zeeb 
716bfcc09ddSBjoern A. Zeeb 	if (vif->bss_conf.he_support && !iwlwifi_mod_params.disable_11ax) {
717bfcc09ddSBjoern A. Zeeb 		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_11AX);
718bfcc09ddSBjoern A. Zeeb 		ctxt_sta->data_policy |=
7199af1bba4SBjoern A. Zeeb 			cpu_to_le32(iwl_mvm_mac_ctxt_cmd_sta_get_twt_policy(mvm, vif));
720bfcc09ddSBjoern A. Zeeb 	}
721bfcc09ddSBjoern A. Zeeb 
722bfcc09ddSBjoern A. Zeeb 
723bfcc09ddSBjoern A. Zeeb 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
724bfcc09ddSBjoern A. Zeeb }
725bfcc09ddSBjoern A. Zeeb 
iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm * mvm,struct ieee80211_vif * vif,u32 action)726bfcc09ddSBjoern A. Zeeb static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
727bfcc09ddSBjoern A. Zeeb 					 struct ieee80211_vif *vif,
728bfcc09ddSBjoern A. Zeeb 					 u32 action)
729bfcc09ddSBjoern A. Zeeb {
730bfcc09ddSBjoern A. Zeeb 	struct iwl_mac_ctx_cmd cmd = {};
7319af1bba4SBjoern A. Zeeb 	u32 tfd_queue_msk = 0;
732bfcc09ddSBjoern A. Zeeb 	int ret;
733bfcc09ddSBjoern A. Zeeb 
734bfcc09ddSBjoern A. Zeeb 	WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
735bfcc09ddSBjoern A. Zeeb 
736bfcc09ddSBjoern A. Zeeb 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
737bfcc09ddSBjoern A. Zeeb 
738bfcc09ddSBjoern A. Zeeb 	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
739bfcc09ddSBjoern A. Zeeb 				       MAC_FILTER_IN_CONTROL_AND_MGMT |
740bfcc09ddSBjoern A. Zeeb 				       MAC_FILTER_IN_BEACON |
741bfcc09ddSBjoern A. Zeeb 				       MAC_FILTER_IN_PROBE_REQUEST |
742bfcc09ddSBjoern A. Zeeb 				       MAC_FILTER_IN_CRC32 |
743bfcc09ddSBjoern A. Zeeb 				       MAC_FILTER_ACCEPT_GRP);
744bfcc09ddSBjoern A. Zeeb 	ieee80211_hw_set(mvm->hw, RX_INCLUDES_FCS);
745bfcc09ddSBjoern A. Zeeb 
7469af1bba4SBjoern A. Zeeb 	/*
7479af1bba4SBjoern A. Zeeb 	 * the queue mask is only relevant for old TX API, and
7489af1bba4SBjoern A. Zeeb 	 * mvm->snif_queue isn't set here (it's still set to
7499af1bba4SBjoern A. Zeeb 	 * IWL_MVM_INVALID_QUEUE so the BIT() of it is UB)
7509af1bba4SBjoern A. Zeeb 	 */
7519af1bba4SBjoern A. Zeeb 	if (!iwl_mvm_has_new_tx_api(mvm))
7529af1bba4SBjoern A. Zeeb 		tfd_queue_msk = BIT(mvm->snif_queue);
7539af1bba4SBjoern A. Zeeb 
754bfcc09ddSBjoern A. Zeeb 	/* Allocate sniffer station */
755bfcc09ddSBjoern A. Zeeb 	ret = iwl_mvm_allocate_int_sta(mvm, &mvm->snif_sta, tfd_queue_msk,
756bfcc09ddSBjoern A. Zeeb 				       vif->type, IWL_STA_GENERAL_PURPOSE);
757bfcc09ddSBjoern A. Zeeb 	if (ret)
758bfcc09ddSBjoern A. Zeeb 		return ret;
759bfcc09ddSBjoern A. Zeeb 
760bfcc09ddSBjoern A. Zeeb 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
761bfcc09ddSBjoern A. Zeeb }
762bfcc09ddSBjoern A. Zeeb 
iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm * mvm,struct ieee80211_vif * vif,u32 action)763bfcc09ddSBjoern A. Zeeb static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
764bfcc09ddSBjoern A. Zeeb 				     struct ieee80211_vif *vif,
765bfcc09ddSBjoern A. Zeeb 				     u32 action)
766bfcc09ddSBjoern A. Zeeb {
767bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
768bfcc09ddSBjoern A. Zeeb 	struct iwl_mac_ctx_cmd cmd = {};
769bfcc09ddSBjoern A. Zeeb 
770bfcc09ddSBjoern A. Zeeb 	WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
771bfcc09ddSBjoern A. Zeeb 
772bfcc09ddSBjoern A. Zeeb 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
773bfcc09ddSBjoern A. Zeeb 
774bfcc09ddSBjoern A. Zeeb 	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
775bfcc09ddSBjoern A. Zeeb 				       MAC_FILTER_IN_PROBE_REQUEST |
776bfcc09ddSBjoern A. Zeeb 				       MAC_FILTER_ACCEPT_GRP);
777bfcc09ddSBjoern A. Zeeb 
778bfcc09ddSBjoern A. Zeeb 	/* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */
779bfcc09ddSBjoern A. Zeeb 	cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
780bfcc09ddSBjoern A. Zeeb 
781bfcc09ddSBjoern A. Zeeb 	/* TODO: Assumes that the beacon id == mac context id */
782bfcc09ddSBjoern A. Zeeb 	cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
783bfcc09ddSBjoern A. Zeeb 
784bfcc09ddSBjoern A. Zeeb 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
785bfcc09ddSBjoern A. Zeeb }
786bfcc09ddSBjoern A. Zeeb 
787bfcc09ddSBjoern A. Zeeb struct iwl_mvm_go_iterator_data {
788bfcc09ddSBjoern A. Zeeb 	bool go_active;
789bfcc09ddSBjoern A. Zeeb };
790bfcc09ddSBjoern A. Zeeb 
iwl_mvm_go_iterator(void * _data,u8 * mac,struct ieee80211_vif * vif)791bfcc09ddSBjoern A. Zeeb static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
792bfcc09ddSBjoern A. Zeeb {
793bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_go_iterator_data *data = _data;
794bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
795bfcc09ddSBjoern A. Zeeb 
796bfcc09ddSBjoern A. Zeeb 	if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
797bfcc09ddSBjoern A. Zeeb 	    mvmvif->ap_ibss_active)
798bfcc09ddSBjoern A. Zeeb 		data->go_active = true;
799bfcc09ddSBjoern A. Zeeb }
800bfcc09ddSBjoern A. Zeeb 
iwl_mac_ctxt_p2p_dev_has_extended_disc(struct iwl_mvm * mvm,struct ieee80211_vif * vif)8019af1bba4SBjoern A. Zeeb __le32 iwl_mac_ctxt_p2p_dev_has_extended_disc(struct iwl_mvm *mvm,
8029af1bba4SBjoern A. Zeeb 					      struct ieee80211_vif *vif)
803bfcc09ddSBjoern A. Zeeb {
804bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_go_iterator_data data = {};
805bfcc09ddSBjoern A. Zeeb 
806bfcc09ddSBjoern A. Zeeb 	/*
807bfcc09ddSBjoern A. Zeeb 	 * This flag should be set to true when the P2P Device is
808bfcc09ddSBjoern A. Zeeb 	 * discoverable and there is at least another active P2P GO. Settings
809bfcc09ddSBjoern A. Zeeb 	 * this flag will allow the P2P Device to be discoverable on other
810bfcc09ddSBjoern A. Zeeb 	 * channels in addition to its listen channel.
811bfcc09ddSBjoern A. Zeeb 	 * Note that this flag should not be set in other cases as it opens the
812bfcc09ddSBjoern A. Zeeb 	 * Rx filters on all MAC and increases the number of interrupts.
813bfcc09ddSBjoern A. Zeeb 	 */
814bfcc09ddSBjoern A. Zeeb 	ieee80211_iterate_active_interfaces_atomic(
815bfcc09ddSBjoern A. Zeeb 		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
816bfcc09ddSBjoern A. Zeeb 		iwl_mvm_go_iterator, &data);
817bfcc09ddSBjoern A. Zeeb 
8189af1bba4SBjoern A. Zeeb 	return cpu_to_le32(data.go_active ? 1 : 0);
8199af1bba4SBjoern A. Zeeb }
8209af1bba4SBjoern A. Zeeb 
iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm * mvm,struct ieee80211_vif * vif,u32 action)8219af1bba4SBjoern A. Zeeb static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
8229af1bba4SBjoern A. Zeeb 					   struct ieee80211_vif *vif,
8239af1bba4SBjoern A. Zeeb 					   u32 action)
8249af1bba4SBjoern A. Zeeb {
8259af1bba4SBjoern A. Zeeb 	struct iwl_mac_ctx_cmd cmd = {};
8269af1bba4SBjoern A. Zeeb 
8279af1bba4SBjoern A. Zeeb 	WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
8289af1bba4SBjoern A. Zeeb 
8299af1bba4SBjoern A. Zeeb 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
8309af1bba4SBjoern A. Zeeb 
8319af1bba4SBjoern A. Zeeb 	cmd.p2p_dev.is_disc_extended =
8329af1bba4SBjoern A. Zeeb 		iwl_mac_ctxt_p2p_dev_has_extended_disc(mvm, vif);
8339af1bba4SBjoern A. Zeeb 
8349af1bba4SBjoern A. Zeeb 	/* Override the filter flags to accept only probe requests */
8359af1bba4SBjoern A. Zeeb 	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
8369af1bba4SBjoern A. Zeeb 
837bfcc09ddSBjoern A. Zeeb 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
838bfcc09ddSBjoern A. Zeeb }
839bfcc09ddSBjoern A. Zeeb 
iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm * mvm,__le32 * tim_index,__le32 * tim_size,u8 * beacon,u32 frame_size)840bfcc09ddSBjoern A. Zeeb void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
841bfcc09ddSBjoern A. Zeeb 			      __le32 *tim_index, __le32 *tim_size,
842bfcc09ddSBjoern A. Zeeb 			      u8 *beacon, u32 frame_size)
843bfcc09ddSBjoern A. Zeeb {
844bfcc09ddSBjoern A. Zeeb 	u32 tim_idx;
845bfcc09ddSBjoern A. Zeeb 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
846bfcc09ddSBjoern A. Zeeb 
847bfcc09ddSBjoern A. Zeeb 	/* The index is relative to frame start but we start looking at the
848bfcc09ddSBjoern A. Zeeb 	 * variable-length part of the beacon. */
849bfcc09ddSBjoern A. Zeeb 	tim_idx = mgmt->u.beacon.variable - beacon;
850bfcc09ddSBjoern A. Zeeb 
851bfcc09ddSBjoern A. Zeeb 	/* Parse variable-length elements of beacon to find WLAN_EID_TIM */
852bfcc09ddSBjoern A. Zeeb 	while ((tim_idx < (frame_size - 2)) &&
853bfcc09ddSBjoern A. Zeeb 			(beacon[tim_idx] != WLAN_EID_TIM))
854bfcc09ddSBjoern A. Zeeb 		tim_idx += beacon[tim_idx+1] + 2;
855bfcc09ddSBjoern A. Zeeb 
856bfcc09ddSBjoern A. Zeeb 	/* If TIM field was found, set variables */
857bfcc09ddSBjoern A. Zeeb 	if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
858bfcc09ddSBjoern A. Zeeb 		*tim_index = cpu_to_le32(tim_idx);
859bfcc09ddSBjoern A. Zeeb 		*tim_size = cpu_to_le32((u32)beacon[tim_idx + 1]);
860bfcc09ddSBjoern A. Zeeb 	} else {
861bfcc09ddSBjoern A. Zeeb 		IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
862bfcc09ddSBjoern A. Zeeb 	}
863bfcc09ddSBjoern A. Zeeb }
864bfcc09ddSBjoern A. Zeeb 
iwl_mvm_find_ie_offset(u8 * beacon,u8 eid,u32 frame_size)865bfcc09ddSBjoern A. Zeeb static u32 iwl_mvm_find_ie_offset(u8 *beacon, u8 eid, u32 frame_size)
866bfcc09ddSBjoern A. Zeeb {
867bfcc09ddSBjoern A. Zeeb 	struct ieee80211_mgmt *mgmt = (void *)beacon;
868bfcc09ddSBjoern A. Zeeb 	const u8 *ie;
869bfcc09ddSBjoern A. Zeeb 
870bfcc09ddSBjoern A. Zeeb 	if (WARN_ON_ONCE(frame_size <= (mgmt->u.beacon.variable - beacon)))
871bfcc09ddSBjoern A. Zeeb 		return 0;
872bfcc09ddSBjoern A. Zeeb 
873bfcc09ddSBjoern A. Zeeb 	frame_size -= mgmt->u.beacon.variable - beacon;
874bfcc09ddSBjoern A. Zeeb 
875bfcc09ddSBjoern A. Zeeb 	ie = cfg80211_find_ie(eid, mgmt->u.beacon.variable, frame_size);
876bfcc09ddSBjoern A. Zeeb 	if (!ie)
877bfcc09ddSBjoern A. Zeeb 		return 0;
878bfcc09ddSBjoern A. Zeeb 
879bfcc09ddSBjoern A. Zeeb 	return ie - beacon;
880bfcc09ddSBjoern A. Zeeb }
881bfcc09ddSBjoern A. Zeeb 
iwl_mvm_mac_ctxt_get_lowest_rate(struct iwl_mvm * mvm,struct ieee80211_tx_info * info,struct ieee80211_vif * vif)8829af1bba4SBjoern A. Zeeb u8 iwl_mvm_mac_ctxt_get_lowest_rate(struct iwl_mvm *mvm,
8839af1bba4SBjoern A. Zeeb 				    struct ieee80211_tx_info *info,
884bfcc09ddSBjoern A. Zeeb 				    struct ieee80211_vif *vif)
885bfcc09ddSBjoern A. Zeeb {
8869af1bba4SBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
8879af1bba4SBjoern A. Zeeb 	struct ieee80211_supported_band *sband;
8889af1bba4SBjoern A. Zeeb 	unsigned long basic = vif->bss_conf.basic_rates;
8899af1bba4SBjoern A. Zeeb 	u16 lowest_cck = IWL_RATE_COUNT, lowest_ofdm = IWL_RATE_COUNT;
8909af1bba4SBjoern A. Zeeb 	u32 link_id = u32_get_bits(info->control.flags,
8919af1bba4SBjoern A. Zeeb 				   IEEE80211_TX_CTRL_MLO_LINK);
8929af1bba4SBjoern A. Zeeb 	u8 band = info->band;
893bfcc09ddSBjoern A. Zeeb 	u8 rate;
8949af1bba4SBjoern A. Zeeb 	u32 i;
8959af1bba4SBjoern A. Zeeb 
8969af1bba4SBjoern A. Zeeb 	if (link_id == IEEE80211_LINK_UNSPECIFIED && ieee80211_vif_is_mld(vif)) {
8979af1bba4SBjoern A. Zeeb 		for (i = 0; i < ARRAY_SIZE(mvmvif->link); i++) {
8989af1bba4SBjoern A. Zeeb 			if (!mvmvif->link[i])
8999af1bba4SBjoern A. Zeeb 				continue;
9009af1bba4SBjoern A. Zeeb 			/* shouldn't do this when >1 link is active */
9019af1bba4SBjoern A. Zeeb 			WARN_ON_ONCE(link_id != IEEE80211_LINK_UNSPECIFIED);
9029af1bba4SBjoern A. Zeeb 			link_id = i;
9039af1bba4SBjoern A. Zeeb 		}
9049af1bba4SBjoern A. Zeeb 	}
9059af1bba4SBjoern A. Zeeb 
9069af1bba4SBjoern A. Zeeb 	if (link_id < IEEE80211_LINK_UNSPECIFIED) {
9079af1bba4SBjoern A. Zeeb 		struct ieee80211_bss_conf *link_conf;
9089af1bba4SBjoern A. Zeeb 
9099af1bba4SBjoern A. Zeeb 		rcu_read_lock();
9109af1bba4SBjoern A. Zeeb 		link_conf = rcu_dereference(vif->link_conf[link_id]);
9119af1bba4SBjoern A. Zeeb 		if (link_conf) {
9129af1bba4SBjoern A. Zeeb 			basic = link_conf->basic_rates;
9139af1bba4SBjoern A. Zeeb 			if (link_conf->chandef.chan)
9149af1bba4SBjoern A. Zeeb 				band = link_conf->chandef.chan->band;
9159af1bba4SBjoern A. Zeeb 		}
9169af1bba4SBjoern A. Zeeb 		rcu_read_unlock();
9179af1bba4SBjoern A. Zeeb 	}
9189af1bba4SBjoern A. Zeeb 
9199af1bba4SBjoern A. Zeeb 	sband = mvm->hw->wiphy->bands[band];
9209af1bba4SBjoern A. Zeeb 	for_each_set_bit(i, &basic, BITS_PER_LONG) {
9219af1bba4SBjoern A. Zeeb 		u16 hw = sband->bitrates[i].hw_value;
9229af1bba4SBjoern A. Zeeb 
9239af1bba4SBjoern A. Zeeb 		if (hw >= IWL_FIRST_OFDM_RATE) {
9249af1bba4SBjoern A. Zeeb 			if (lowest_ofdm > hw)
9259af1bba4SBjoern A. Zeeb 				lowest_ofdm = hw;
9269af1bba4SBjoern A. Zeeb 		} else if (lowest_cck > hw) {
9279af1bba4SBjoern A. Zeeb 			lowest_cck = hw;
9289af1bba4SBjoern A. Zeeb 		}
9299af1bba4SBjoern A. Zeeb 	}
9309af1bba4SBjoern A. Zeeb 
9319af1bba4SBjoern A. Zeeb 	if (band == NL80211_BAND_2GHZ && !vif->p2p &&
9329af1bba4SBjoern A. Zeeb 	    vif->type != NL80211_IFTYPE_P2P_DEVICE &&
9339af1bba4SBjoern A. Zeeb 	    !(info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)) {
9349af1bba4SBjoern A. Zeeb 		if (lowest_cck != IWL_RATE_COUNT)
9359af1bba4SBjoern A. Zeeb 			rate = lowest_cck;
9369af1bba4SBjoern A. Zeeb 		else if (lowest_ofdm != IWL_RATE_COUNT)
9379af1bba4SBjoern A. Zeeb 			rate = lowest_ofdm;
938bfcc09ddSBjoern A. Zeeb 		else
9399af1bba4SBjoern A. Zeeb 			rate = IWL_RATE_1M_INDEX;
9409af1bba4SBjoern A. Zeeb 	} else if (lowest_ofdm != IWL_RATE_COUNT) {
9419af1bba4SBjoern A. Zeeb 		rate = lowest_ofdm;
9429af1bba4SBjoern A. Zeeb 	} else {
9439af1bba4SBjoern A. Zeeb 		rate = IWL_RATE_6M_INDEX;
9449af1bba4SBjoern A. Zeeb 	}
945bfcc09ddSBjoern A. Zeeb 
946bfcc09ddSBjoern A. Zeeb 	return rate;
947bfcc09ddSBjoern A. Zeeb }
948bfcc09ddSBjoern A. Zeeb 
iwl_mvm_mac_ctxt_get_beacon_flags(const struct iwl_fw * fw,u8 rate_idx)949bfcc09ddSBjoern A. Zeeb u16 iwl_mvm_mac_ctxt_get_beacon_flags(const struct iwl_fw *fw, u8 rate_idx)
950bfcc09ddSBjoern A. Zeeb {
951bfcc09ddSBjoern A. Zeeb 	u16 flags = iwl_mvm_mac80211_idx_to_hwrate(fw, rate_idx);
952d9836fb4SBjoern A. Zeeb 	bool is_new_rate = iwl_fw_lookup_cmd_ver(fw, BEACON_TEMPLATE_CMD, 0) > 10;
953bfcc09ddSBjoern A. Zeeb 
954bfcc09ddSBjoern A. Zeeb 	if (rate_idx <= IWL_FIRST_CCK_RATE)
955bfcc09ddSBjoern A. Zeeb 		flags |= is_new_rate ? IWL_MAC_BEACON_CCK
956bfcc09ddSBjoern A. Zeeb 			  : IWL_MAC_BEACON_CCK_V1;
957bfcc09ddSBjoern A. Zeeb 
958bfcc09ddSBjoern A. Zeeb 	return flags;
959bfcc09ddSBjoern A. Zeeb }
960bfcc09ddSBjoern A. Zeeb 
iwl_mvm_mac_ctxt_get_beacon_rate(struct iwl_mvm * mvm,struct ieee80211_tx_info * info,struct ieee80211_vif * vif)9619af1bba4SBjoern A. Zeeb u8 iwl_mvm_mac_ctxt_get_beacon_rate(struct iwl_mvm *mvm,
9629af1bba4SBjoern A. Zeeb 				    struct ieee80211_tx_info *info,
9639af1bba4SBjoern A. Zeeb 				    struct ieee80211_vif *vif)
9649af1bba4SBjoern A. Zeeb {
9659af1bba4SBjoern A. Zeeb 	struct ieee80211_supported_band *sband =
9669af1bba4SBjoern A. Zeeb 		mvm->hw->wiphy->bands[info->band];
9679af1bba4SBjoern A. Zeeb 	u32 legacy = vif->bss_conf.beacon_tx_rate.control[info->band].legacy;
9689af1bba4SBjoern A. Zeeb 
9699af1bba4SBjoern A. Zeeb 	/* if beacon rate was configured try using it */
9709af1bba4SBjoern A. Zeeb 	if (hweight32(legacy) == 1) {
9719af1bba4SBjoern A. Zeeb 		u32 rate = ffs(legacy) - 1;
9729af1bba4SBjoern A. Zeeb 
9739af1bba4SBjoern A. Zeeb 		return sband->bitrates[rate].hw_value;
9749af1bba4SBjoern A. Zeeb 	}
9759af1bba4SBjoern A. Zeeb 
9769af1bba4SBjoern A. Zeeb 	return iwl_mvm_mac_ctxt_get_lowest_rate(mvm, info, vif);
9779af1bba4SBjoern A. Zeeb }
9789af1bba4SBjoern A. Zeeb 
iwl_mvm_mac_ctxt_set_tx(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct sk_buff * beacon,struct iwl_tx_cmd * tx)979bfcc09ddSBjoern A. Zeeb static void iwl_mvm_mac_ctxt_set_tx(struct iwl_mvm *mvm,
980bfcc09ddSBjoern A. Zeeb 				    struct ieee80211_vif *vif,
981bfcc09ddSBjoern A. Zeeb 				    struct sk_buff *beacon,
982bfcc09ddSBjoern A. Zeeb 				    struct iwl_tx_cmd *tx)
983bfcc09ddSBjoern A. Zeeb {
984bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
985bfcc09ddSBjoern A. Zeeb 	struct ieee80211_tx_info *info;
986bfcc09ddSBjoern A. Zeeb 	u8 rate;
987bfcc09ddSBjoern A. Zeeb 	u32 tx_flags;
988bfcc09ddSBjoern A. Zeeb 
989bfcc09ddSBjoern A. Zeeb 	info = IEEE80211_SKB_CB(beacon);
990bfcc09ddSBjoern A. Zeeb 
991bfcc09ddSBjoern A. Zeeb 	/* Set up TX command fields */
992bfcc09ddSBjoern A. Zeeb 	tx->len = cpu_to_le16((u16)beacon->len);
9939af1bba4SBjoern A. Zeeb 	tx->sta_id = mvmvif->deflink.bcast_sta.sta_id;
994bfcc09ddSBjoern A. Zeeb 	tx->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
995bfcc09ddSBjoern A. Zeeb 	tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;
996bfcc09ddSBjoern A. Zeeb 	tx_flags |=
997bfcc09ddSBjoern A. Zeeb 		iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<
998bfcc09ddSBjoern A. Zeeb 						TX_CMD_FLG_BT_PRIO_POS;
999bfcc09ddSBjoern A. Zeeb 	tx->tx_flags = cpu_to_le32(tx_flags);
1000bfcc09ddSBjoern A. Zeeb 
1001bfcc09ddSBjoern A. Zeeb 	if (!fw_has_capa(&mvm->fw->ucode_capa,
1002bfcc09ddSBjoern A. Zeeb 			 IWL_UCODE_TLV_CAPA_BEACON_ANT_SELECTION))
1003bfcc09ddSBjoern A. Zeeb 		iwl_mvm_toggle_tx_ant(mvm, &mvm->mgmt_last_antenna_idx);
1004bfcc09ddSBjoern A. Zeeb 
1005bfcc09ddSBjoern A. Zeeb 	tx->rate_n_flags =
1006bfcc09ddSBjoern A. Zeeb 		cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
1007bfcc09ddSBjoern A. Zeeb 			    RATE_MCS_ANT_POS);
1008bfcc09ddSBjoern A. Zeeb 
10099af1bba4SBjoern A. Zeeb 	rate = iwl_mvm_mac_ctxt_get_beacon_rate(mvm, info, vif);
1010bfcc09ddSBjoern A. Zeeb 
1011bfcc09ddSBjoern A. Zeeb 	tx->rate_n_flags |=
1012bfcc09ddSBjoern A. Zeeb 		cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(mvm->fw, rate));
1013bfcc09ddSBjoern A. Zeeb 	if (rate == IWL_FIRST_CCK_RATE)
1014bfcc09ddSBjoern A. Zeeb 		tx->rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK_V1);
1015bfcc09ddSBjoern A. Zeeb 
1016bfcc09ddSBjoern A. Zeeb }
1017bfcc09ddSBjoern A. Zeeb 
iwl_mvm_mac_ctxt_send_beacon_cmd(struct iwl_mvm * mvm,struct sk_buff * beacon,void * data,int len)1018bfcc09ddSBjoern A. Zeeb int iwl_mvm_mac_ctxt_send_beacon_cmd(struct iwl_mvm *mvm,
1019bfcc09ddSBjoern A. Zeeb 				     struct sk_buff *beacon,
1020bfcc09ddSBjoern A. Zeeb 				     void *data, int len)
1021bfcc09ddSBjoern A. Zeeb {
1022bfcc09ddSBjoern A. Zeeb 	struct iwl_host_cmd cmd = {
1023bfcc09ddSBjoern A. Zeeb 		.id = BEACON_TEMPLATE_CMD,
1024bfcc09ddSBjoern A. Zeeb 		.flags = CMD_ASYNC,
1025bfcc09ddSBjoern A. Zeeb 	};
1026bfcc09ddSBjoern A. Zeeb 
1027bfcc09ddSBjoern A. Zeeb 	cmd.len[0] = len;
1028bfcc09ddSBjoern A. Zeeb 	cmd.data[0] = data;
1029bfcc09ddSBjoern A. Zeeb 	cmd.dataflags[0] = 0;
1030bfcc09ddSBjoern A. Zeeb 	cmd.len[1] = beacon->len;
1031bfcc09ddSBjoern A. Zeeb 	cmd.data[1] = beacon->data;
1032bfcc09ddSBjoern A. Zeeb 	cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
1033bfcc09ddSBjoern A. Zeeb 
1034bfcc09ddSBjoern A. Zeeb 	return iwl_mvm_send_cmd(mvm, &cmd);
1035bfcc09ddSBjoern A. Zeeb }
1036bfcc09ddSBjoern A. Zeeb 
iwl_mvm_mac_ctxt_send_beacon_v6(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct sk_buff * beacon)1037bfcc09ddSBjoern A. Zeeb static int iwl_mvm_mac_ctxt_send_beacon_v6(struct iwl_mvm *mvm,
1038bfcc09ddSBjoern A. Zeeb 					   struct ieee80211_vif *vif,
1039bfcc09ddSBjoern A. Zeeb 					   struct sk_buff *beacon)
1040bfcc09ddSBjoern A. Zeeb {
1041bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1042bfcc09ddSBjoern A. Zeeb 	struct iwl_mac_beacon_cmd_v6 beacon_cmd = {};
1043bfcc09ddSBjoern A. Zeeb 
1044bfcc09ddSBjoern A. Zeeb 	iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
1045bfcc09ddSBjoern A. Zeeb 
1046bfcc09ddSBjoern A. Zeeb 	beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
1047bfcc09ddSBjoern A. Zeeb 
1048bfcc09ddSBjoern A. Zeeb 	if (vif->type == NL80211_IFTYPE_AP)
1049bfcc09ddSBjoern A. Zeeb 		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
1050bfcc09ddSBjoern A. Zeeb 					 &beacon_cmd.tim_size,
1051bfcc09ddSBjoern A. Zeeb 					 beacon->data, beacon->len);
1052bfcc09ddSBjoern A. Zeeb 
1053bfcc09ddSBjoern A. Zeeb 	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
1054bfcc09ddSBjoern A. Zeeb 						sizeof(beacon_cmd));
1055bfcc09ddSBjoern A. Zeeb }
1056bfcc09ddSBjoern A. Zeeb 
iwl_mvm_mac_ctxt_send_beacon_v7(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct sk_buff * beacon)1057bfcc09ddSBjoern A. Zeeb static int iwl_mvm_mac_ctxt_send_beacon_v7(struct iwl_mvm *mvm,
1058bfcc09ddSBjoern A. Zeeb 					   struct ieee80211_vif *vif,
1059bfcc09ddSBjoern A. Zeeb 					   struct sk_buff *beacon)
1060bfcc09ddSBjoern A. Zeeb {
1061bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1062bfcc09ddSBjoern A. Zeeb 	struct iwl_mac_beacon_cmd_v7 beacon_cmd = {};
1063bfcc09ddSBjoern A. Zeeb 
1064bfcc09ddSBjoern A. Zeeb 	iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
1065bfcc09ddSBjoern A. Zeeb 
1066bfcc09ddSBjoern A. Zeeb 	beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
1067bfcc09ddSBjoern A. Zeeb 
1068bfcc09ddSBjoern A. Zeeb 	if (vif->type == NL80211_IFTYPE_AP)
1069bfcc09ddSBjoern A. Zeeb 		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
1070bfcc09ddSBjoern A. Zeeb 					 &beacon_cmd.tim_size,
1071bfcc09ddSBjoern A. Zeeb 					 beacon->data, beacon->len);
1072bfcc09ddSBjoern A. Zeeb 
1073bfcc09ddSBjoern A. Zeeb 	beacon_cmd.csa_offset =
1074bfcc09ddSBjoern A. Zeeb 		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1075bfcc09ddSBjoern A. Zeeb 						   WLAN_EID_CHANNEL_SWITCH,
1076bfcc09ddSBjoern A. Zeeb 						   beacon->len));
1077bfcc09ddSBjoern A. Zeeb 	beacon_cmd.ecsa_offset =
1078bfcc09ddSBjoern A. Zeeb 		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1079bfcc09ddSBjoern A. Zeeb 						   WLAN_EID_EXT_CHANSWITCH_ANN,
1080bfcc09ddSBjoern A. Zeeb 						   beacon->len));
1081bfcc09ddSBjoern A. Zeeb 
1082bfcc09ddSBjoern A. Zeeb 	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
1083bfcc09ddSBjoern A. Zeeb 						sizeof(beacon_cmd));
1084bfcc09ddSBjoern A. Zeeb }
1085bfcc09ddSBjoern A. Zeeb 
iwl_mvm_mac_ctxt_send_beacon_v9(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct sk_buff * beacon,struct ieee80211_bss_conf * link_conf)1086bfcc09ddSBjoern A. Zeeb static int iwl_mvm_mac_ctxt_send_beacon_v9(struct iwl_mvm *mvm,
1087bfcc09ddSBjoern A. Zeeb 					   struct ieee80211_vif *vif,
10889af1bba4SBjoern A. Zeeb 					   struct sk_buff *beacon,
10899af1bba4SBjoern A. Zeeb 					   struct ieee80211_bss_conf *link_conf)
1090bfcc09ddSBjoern A. Zeeb {
1091bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1092bfcc09ddSBjoern A. Zeeb 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(beacon);
1093bfcc09ddSBjoern A. Zeeb 	struct iwl_mac_beacon_cmd beacon_cmd = {};
10949af1bba4SBjoern A. Zeeb 	u8 rate = iwl_mvm_mac_ctxt_get_beacon_rate(mvm, info, vif);
1095bfcc09ddSBjoern A. Zeeb 	u16 flags;
1096bfcc09ddSBjoern A. Zeeb 	struct ieee80211_chanctx_conf *ctx;
1097bfcc09ddSBjoern A. Zeeb 	int channel;
1098bfcc09ddSBjoern A. Zeeb 	flags = iwl_mvm_mac_ctxt_get_beacon_flags(mvm->fw, rate);
1099bfcc09ddSBjoern A. Zeeb 
1100bfcc09ddSBjoern A. Zeeb 	/* Enable FILS on PSC channels only */
1101bfcc09ddSBjoern A. Zeeb 	rcu_read_lock();
11029af1bba4SBjoern A. Zeeb 	ctx = rcu_dereference(link_conf->chanctx_conf);
1103bfcc09ddSBjoern A. Zeeb 	channel = ieee80211_frequency_to_channel(ctx->def.chan->center_freq);
1104bfcc09ddSBjoern A. Zeeb 	WARN_ON(channel == 0);
1105bfcc09ddSBjoern A. Zeeb 	if (cfg80211_channel_is_psc(ctx->def.chan) &&
1106bfcc09ddSBjoern A. Zeeb 	    !IWL_MVM_DISABLE_AP_FILS) {
1107d9836fb4SBjoern A. Zeeb 		flags |= iwl_fw_lookup_cmd_ver(mvm->fw, BEACON_TEMPLATE_CMD,
1108bfcc09ddSBjoern A. Zeeb 					       0) > 10 ?
1109bfcc09ddSBjoern A. Zeeb 			IWL_MAC_BEACON_FILS :
1110bfcc09ddSBjoern A. Zeeb 			IWL_MAC_BEACON_FILS_V1;
1111bfcc09ddSBjoern A. Zeeb 		beacon_cmd.short_ssid =
11129af1bba4SBjoern A. Zeeb 			cpu_to_le32(~crc32_le(~0, vif->cfg.ssid,
11139af1bba4SBjoern A. Zeeb 					      vif->cfg.ssid_len));
1114bfcc09ddSBjoern A. Zeeb 	}
1115bfcc09ddSBjoern A. Zeeb 	rcu_read_unlock();
1116bfcc09ddSBjoern A. Zeeb 
1117bfcc09ddSBjoern A. Zeeb 	beacon_cmd.flags = cpu_to_le16(flags);
1118bfcc09ddSBjoern A. Zeeb 	beacon_cmd.byte_cnt = cpu_to_le16((u16)beacon->len);
11199af1bba4SBjoern A. Zeeb 
11209af1bba4SBjoern A. Zeeb 	if (WARN_ON(!mvmvif->link[link_conf->link_id]))
11219af1bba4SBjoern A. Zeeb 		return -EINVAL;
11229af1bba4SBjoern A. Zeeb 
11239af1bba4SBjoern A. Zeeb 	if (iwl_fw_lookup_cmd_ver(mvm->fw, BEACON_TEMPLATE_CMD, 0) > 12)
11249af1bba4SBjoern A. Zeeb 		beacon_cmd.link_id =
11259af1bba4SBjoern A. Zeeb 			cpu_to_le32(mvmvif->link[link_conf->link_id]->fw_link_id);
11269af1bba4SBjoern A. Zeeb 	else
11279af1bba4SBjoern A. Zeeb 		beacon_cmd.link_id = cpu_to_le32((u32)mvmvif->id);
1128bfcc09ddSBjoern A. Zeeb 
1129bfcc09ddSBjoern A. Zeeb 	if (vif->type == NL80211_IFTYPE_AP)
1130bfcc09ddSBjoern A. Zeeb 		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
1131bfcc09ddSBjoern A. Zeeb 					 &beacon_cmd.tim_size,
1132bfcc09ddSBjoern A. Zeeb 					 beacon->data, beacon->len);
1133bfcc09ddSBjoern A. Zeeb 
1134bfcc09ddSBjoern A. Zeeb 	beacon_cmd.csa_offset =
1135bfcc09ddSBjoern A. Zeeb 		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1136bfcc09ddSBjoern A. Zeeb 						   WLAN_EID_CHANNEL_SWITCH,
1137bfcc09ddSBjoern A. Zeeb 						   beacon->len));
1138bfcc09ddSBjoern A. Zeeb 	beacon_cmd.ecsa_offset =
1139bfcc09ddSBjoern A. Zeeb 		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1140bfcc09ddSBjoern A. Zeeb 						   WLAN_EID_EXT_CHANSWITCH_ANN,
1141bfcc09ddSBjoern A. Zeeb 						   beacon->len));
1142bfcc09ddSBjoern A. Zeeb 
1143bfcc09ddSBjoern A. Zeeb 	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
1144bfcc09ddSBjoern A. Zeeb 						sizeof(beacon_cmd));
1145bfcc09ddSBjoern A. Zeeb }
1146bfcc09ddSBjoern A. Zeeb 
iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct sk_buff * beacon,struct ieee80211_bss_conf * link_conf)11479af1bba4SBjoern A. Zeeb static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
1148bfcc09ddSBjoern A. Zeeb 					struct ieee80211_vif *vif,
11499af1bba4SBjoern A. Zeeb 					struct sk_buff *beacon,
11509af1bba4SBjoern A. Zeeb 					struct ieee80211_bss_conf *link_conf)
1151bfcc09ddSBjoern A. Zeeb {
1152bfcc09ddSBjoern A. Zeeb 	if (WARN_ON(!beacon))
1153bfcc09ddSBjoern A. Zeeb 		return -EINVAL;
1154bfcc09ddSBjoern A. Zeeb 
1155bfcc09ddSBjoern A. Zeeb 	if (IWL_MVM_NON_TRANSMITTING_AP)
1156bfcc09ddSBjoern A. Zeeb 		return 0;
1157bfcc09ddSBjoern A. Zeeb 
1158bfcc09ddSBjoern A. Zeeb 	if (!fw_has_capa(&mvm->fw->ucode_capa,
1159bfcc09ddSBjoern A. Zeeb 			 IWL_UCODE_TLV_CAPA_CSA_AND_TBTT_OFFLOAD))
1160bfcc09ddSBjoern A. Zeeb 		return iwl_mvm_mac_ctxt_send_beacon_v6(mvm, vif, beacon);
1161bfcc09ddSBjoern A. Zeeb 
1162bfcc09ddSBjoern A. Zeeb 	if (fw_has_api(&mvm->fw->ucode_capa,
1163bfcc09ddSBjoern A. Zeeb 		       IWL_UCODE_TLV_API_NEW_BEACON_TEMPLATE))
11649af1bba4SBjoern A. Zeeb 		return iwl_mvm_mac_ctxt_send_beacon_v9(mvm, vif, beacon,
11659af1bba4SBjoern A. Zeeb 						       link_conf);
1166bfcc09ddSBjoern A. Zeeb 
1167bfcc09ddSBjoern A. Zeeb 	return iwl_mvm_mac_ctxt_send_beacon_v7(mvm, vif, beacon);
1168bfcc09ddSBjoern A. Zeeb }
1169bfcc09ddSBjoern A. Zeeb 
1170bfcc09ddSBjoern A. Zeeb /* The beacon template for the AP/GO/IBSS has changed and needs update */
iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf)1171bfcc09ddSBjoern A. Zeeb int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
11729af1bba4SBjoern A. Zeeb 				    struct ieee80211_vif *vif,
11739af1bba4SBjoern A. Zeeb 				    struct ieee80211_bss_conf *link_conf)
1174bfcc09ddSBjoern A. Zeeb {
1175bfcc09ddSBjoern A. Zeeb 	struct sk_buff *beacon;
1176bfcc09ddSBjoern A. Zeeb 	int ret;
1177bfcc09ddSBjoern A. Zeeb 
1178bfcc09ddSBjoern A. Zeeb 	WARN_ON(vif->type != NL80211_IFTYPE_AP &&
1179bfcc09ddSBjoern A. Zeeb 		vif->type != NL80211_IFTYPE_ADHOC);
1180bfcc09ddSBjoern A. Zeeb 
11819af1bba4SBjoern A. Zeeb 	beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL,
11829af1bba4SBjoern A. Zeeb 					       link_conf->link_id);
1183bfcc09ddSBjoern A. Zeeb 	if (!beacon)
1184bfcc09ddSBjoern A. Zeeb 		return -ENOMEM;
1185bfcc09ddSBjoern A. Zeeb 
1186bfcc09ddSBjoern A. Zeeb #ifdef CONFIG_IWLWIFI_DEBUGFS
1187bfcc09ddSBjoern A. Zeeb 	if (mvm->beacon_inject_active) {
1188bfcc09ddSBjoern A. Zeeb 		dev_kfree_skb(beacon);
1189bfcc09ddSBjoern A. Zeeb 		return -EBUSY;
1190bfcc09ddSBjoern A. Zeeb 	}
1191bfcc09ddSBjoern A. Zeeb #endif
1192bfcc09ddSBjoern A. Zeeb 
11939af1bba4SBjoern A. Zeeb 	ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon, link_conf);
1194bfcc09ddSBjoern A. Zeeb 	dev_kfree_skb(beacon);
1195bfcc09ddSBjoern A. Zeeb 	return ret;
1196bfcc09ddSBjoern A. Zeeb }
1197bfcc09ddSBjoern A. Zeeb 
1198bfcc09ddSBjoern A. Zeeb struct iwl_mvm_mac_ap_iterator_data {
1199bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm *mvm;
1200bfcc09ddSBjoern A. Zeeb 	struct ieee80211_vif *vif;
1201bfcc09ddSBjoern A. Zeeb 	u32 beacon_device_ts;
1202bfcc09ddSBjoern A. Zeeb 	u16 beacon_int;
1203bfcc09ddSBjoern A. Zeeb };
1204bfcc09ddSBjoern A. Zeeb 
1205bfcc09ddSBjoern A. Zeeb /* Find the beacon_device_ts and beacon_int for a managed interface */
iwl_mvm_mac_ap_iterator(void * _data,u8 * mac,struct ieee80211_vif * vif)1206bfcc09ddSBjoern A. Zeeb static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
1207bfcc09ddSBjoern A. Zeeb 				    struct ieee80211_vif *vif)
1208bfcc09ddSBjoern A. Zeeb {
1209bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_mac_ap_iterator_data *data = _data;
1210bfcc09ddSBjoern A. Zeeb 
12119af1bba4SBjoern A. Zeeb 	if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc)
1212bfcc09ddSBjoern A. Zeeb 		return;
1213bfcc09ddSBjoern A. Zeeb 
1214bfcc09ddSBjoern A. Zeeb 	/* Station client has higher priority over P2P client*/
1215bfcc09ddSBjoern A. Zeeb 	if (vif->p2p && data->beacon_device_ts)
1216bfcc09ddSBjoern A. Zeeb 		return;
1217bfcc09ddSBjoern A. Zeeb 
1218bfcc09ddSBjoern A. Zeeb 	data->beacon_device_ts = vif->bss_conf.sync_device_ts;
1219bfcc09ddSBjoern A. Zeeb 	data->beacon_int = vif->bss_conf.beacon_int;
1220bfcc09ddSBjoern A. Zeeb }
1221bfcc09ddSBjoern A. Zeeb 
1222bfcc09ddSBjoern A. Zeeb /*
12239af1bba4SBjoern A. Zeeb  * Fill the filter flags for mac context of type AP or P2P GO.
12249af1bba4SBjoern A. Zeeb  */
iwl_mvm_mac_ctxt_cmd_ap_set_filter_flags(struct iwl_mvm * mvm,struct iwl_mvm_vif * mvmvif,__le32 * filter_flags,int accept_probe_req_flag,int accept_beacon_flag)12259af1bba4SBjoern A. Zeeb void iwl_mvm_mac_ctxt_cmd_ap_set_filter_flags(struct iwl_mvm *mvm,
12269af1bba4SBjoern A. Zeeb 					      struct iwl_mvm_vif *mvmvif,
12279af1bba4SBjoern A. Zeeb 					      __le32 *filter_flags,
12289af1bba4SBjoern A. Zeeb 					      int accept_probe_req_flag,
12299af1bba4SBjoern A. Zeeb 					      int accept_beacon_flag)
12309af1bba4SBjoern A. Zeeb {
12319af1bba4SBjoern A. Zeeb 	/*
12329af1bba4SBjoern A. Zeeb 	 * in AP mode, pass probe requests and beacons from other APs
12339af1bba4SBjoern A. Zeeb 	 * (needed for ht protection); when there're no any associated
12349af1bba4SBjoern A. Zeeb 	 * station don't ask FW to pass beacons to prevent unnecessary
12359af1bba4SBjoern A. Zeeb 	 * wake-ups.
12369af1bba4SBjoern A. Zeeb 	 */
12379af1bba4SBjoern A. Zeeb 	*filter_flags |= cpu_to_le32(accept_probe_req_flag);
12389af1bba4SBjoern A. Zeeb 	if (mvmvif->ap_assoc_sta_count || !mvm->drop_bcn_ap_mode) {
12399af1bba4SBjoern A. Zeeb 		*filter_flags |= cpu_to_le32(accept_beacon_flag);
12409af1bba4SBjoern A. Zeeb 		IWL_DEBUG_HC(mvm, "Asking FW to pass beacons\n");
12419af1bba4SBjoern A. Zeeb 	} else {
12429af1bba4SBjoern A. Zeeb 		IWL_DEBUG_HC(mvm, "No need to receive beacons\n");
12439af1bba4SBjoern A. Zeeb 	}
12449af1bba4SBjoern A. Zeeb }
12459af1bba4SBjoern A. Zeeb 
12469af1bba4SBjoern A. Zeeb /*
1247bfcc09ddSBjoern A. Zeeb  * Fill the specific data for mac context of type AP of P2P GO
1248bfcc09ddSBjoern A. Zeeb  */
iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_mac_ctx_cmd * cmd,struct iwl_mac_data_ap * ctxt_ap,bool add)1249bfcc09ddSBjoern A. Zeeb static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
1250bfcc09ddSBjoern A. Zeeb 					 struct ieee80211_vif *vif,
1251bfcc09ddSBjoern A. Zeeb 					 struct iwl_mac_ctx_cmd *cmd,
1252bfcc09ddSBjoern A. Zeeb 					 struct iwl_mac_data_ap *ctxt_ap,
1253bfcc09ddSBjoern A. Zeeb 					 bool add)
1254bfcc09ddSBjoern A. Zeeb {
1255bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1256bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_mac_ap_iterator_data data = {
1257bfcc09ddSBjoern A. Zeeb 		.mvm = mvm,
1258bfcc09ddSBjoern A. Zeeb 		.vif = vif,
1259bfcc09ddSBjoern A. Zeeb 		.beacon_device_ts = 0
1260bfcc09ddSBjoern A. Zeeb 	};
1261bfcc09ddSBjoern A. Zeeb 
1262bfcc09ddSBjoern A. Zeeb 	/* in AP mode, the MCAST FIFO takes the EDCA params from VO */
1263bfcc09ddSBjoern A. Zeeb 	cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |= BIT(IWL_MVM_TX_FIFO_MCAST);
1264bfcc09ddSBjoern A. Zeeb 
12659af1bba4SBjoern A. Zeeb 	iwl_mvm_mac_ctxt_cmd_ap_set_filter_flags(mvm, mvmvif,
12669af1bba4SBjoern A. Zeeb 						 &cmd->filter_flags,
12679af1bba4SBjoern A. Zeeb 						 MAC_FILTER_IN_PROBE_REQUEST,
12689af1bba4SBjoern A. Zeeb 						 MAC_FILTER_IN_BEACON);
1269bfcc09ddSBjoern A. Zeeb 
1270bfcc09ddSBjoern A. Zeeb 	ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
1271bfcc09ddSBjoern A. Zeeb 	ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
1272bfcc09ddSBjoern A. Zeeb 					     vif->bss_conf.dtim_period);
1273bfcc09ddSBjoern A. Zeeb 
1274bfcc09ddSBjoern A. Zeeb 	if (!fw_has_api(&mvm->fw->ucode_capa,
1275bfcc09ddSBjoern A. Zeeb 			IWL_UCODE_TLV_API_STA_TYPE))
12769af1bba4SBjoern A. Zeeb 		ctxt_ap->mcast_qid = cpu_to_le32(mvmvif->deflink.cab_queue);
1277bfcc09ddSBjoern A. Zeeb 
1278bfcc09ddSBjoern A. Zeeb 	/*
1279bfcc09ddSBjoern A. Zeeb 	 * Only set the beacon time when the MAC is being added, when we
1280bfcc09ddSBjoern A. Zeeb 	 * just modify the MAC then we should keep the time -- the firmware
1281bfcc09ddSBjoern A. Zeeb 	 * can otherwise have a "jumping" TBTT.
1282bfcc09ddSBjoern A. Zeeb 	 */
1283bfcc09ddSBjoern A. Zeeb 	if (add) {
1284bfcc09ddSBjoern A. Zeeb 		/*
1285bfcc09ddSBjoern A. Zeeb 		 * If there is a station/P2P client interface which is
1286bfcc09ddSBjoern A. Zeeb 		 * associated, set the AP's TBTT far enough from the station's
1287bfcc09ddSBjoern A. Zeeb 		 * TBTT. Otherwise, set it to the current system time
1288bfcc09ddSBjoern A. Zeeb 		 */
1289bfcc09ddSBjoern A. Zeeb 		ieee80211_iterate_active_interfaces_atomic(
1290bfcc09ddSBjoern A. Zeeb 			mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1291bfcc09ddSBjoern A. Zeeb 			iwl_mvm_mac_ap_iterator, &data);
1292bfcc09ddSBjoern A. Zeeb 
1293bfcc09ddSBjoern A. Zeeb 		if (data.beacon_device_ts) {
12949af1bba4SBjoern A. Zeeb 			u32 rand = get_random_u32_inclusive(36, 63);
1295bfcc09ddSBjoern A. Zeeb 			mvmvif->ap_beacon_time = data.beacon_device_ts +
1296bfcc09ddSBjoern A. Zeeb 				ieee80211_tu_to_usec(data.beacon_int * rand /
1297bfcc09ddSBjoern A. Zeeb 						     100);
1298bfcc09ddSBjoern A. Zeeb 		} else {
1299bfcc09ddSBjoern A. Zeeb 			mvmvif->ap_beacon_time = iwl_mvm_get_systime(mvm);
1300bfcc09ddSBjoern A. Zeeb 		}
1301bfcc09ddSBjoern A. Zeeb 	}
1302bfcc09ddSBjoern A. Zeeb 
1303bfcc09ddSBjoern A. Zeeb 	ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
1304bfcc09ddSBjoern A. Zeeb 	ctxt_ap->beacon_tsf = 0; /* unused */
1305bfcc09ddSBjoern A. Zeeb 
1306bfcc09ddSBjoern A. Zeeb 	/* TODO: Assume that the beacon id == mac context id */
1307bfcc09ddSBjoern A. Zeeb 	ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
1308bfcc09ddSBjoern A. Zeeb }
1309bfcc09ddSBjoern A. Zeeb 
iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm * mvm,struct ieee80211_vif * vif,u32 action)1310bfcc09ddSBjoern A. Zeeb static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
1311bfcc09ddSBjoern A. Zeeb 				   struct ieee80211_vif *vif,
1312bfcc09ddSBjoern A. Zeeb 				   u32 action)
1313bfcc09ddSBjoern A. Zeeb {
1314bfcc09ddSBjoern A. Zeeb 	struct iwl_mac_ctx_cmd cmd = {};
1315bfcc09ddSBjoern A. Zeeb 
1316bfcc09ddSBjoern A. Zeeb 	WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
1317bfcc09ddSBjoern A. Zeeb 
1318bfcc09ddSBjoern A. Zeeb 	/* Fill the common data for all mac context types */
1319bfcc09ddSBjoern A. Zeeb 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1320bfcc09ddSBjoern A. Zeeb 
1321bfcc09ddSBjoern A. Zeeb 	/* Fill the data specific for ap mode */
1322bfcc09ddSBjoern A. Zeeb 	iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.ap,
1323bfcc09ddSBjoern A. Zeeb 				     action == FW_CTXT_ACTION_ADD);
1324bfcc09ddSBjoern A. Zeeb 
1325bfcc09ddSBjoern A. Zeeb 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1326bfcc09ddSBjoern A. Zeeb }
1327bfcc09ddSBjoern A. Zeeb 
iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm * mvm,struct ieee80211_vif * vif,u32 action)1328bfcc09ddSBjoern A. Zeeb static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
1329bfcc09ddSBjoern A. Zeeb 				   struct ieee80211_vif *vif,
1330bfcc09ddSBjoern A. Zeeb 				   u32 action)
1331bfcc09ddSBjoern A. Zeeb {
1332bfcc09ddSBjoern A. Zeeb 	struct iwl_mac_ctx_cmd cmd = {};
1333bfcc09ddSBjoern A. Zeeb 	struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
1334bfcc09ddSBjoern A. Zeeb 
1335bfcc09ddSBjoern A. Zeeb 	WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
1336bfcc09ddSBjoern A. Zeeb 
1337bfcc09ddSBjoern A. Zeeb 	/* Fill the common data for all mac context types */
1338bfcc09ddSBjoern A. Zeeb 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1339bfcc09ddSBjoern A. Zeeb 
1340bfcc09ddSBjoern A. Zeeb 	/* Fill the data specific for GO mode */
1341bfcc09ddSBjoern A. Zeeb 	iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.go.ap,
1342bfcc09ddSBjoern A. Zeeb 				     action == FW_CTXT_ACTION_ADD);
1343bfcc09ddSBjoern A. Zeeb 
1344bfcc09ddSBjoern A. Zeeb 	cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
1345bfcc09ddSBjoern A. Zeeb 					IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
1346bfcc09ddSBjoern A. Zeeb 	cmd.go.opp_ps_enabled =
1347bfcc09ddSBjoern A. Zeeb 			cpu_to_le32(!!(noa->oppps_ctwindow &
1348bfcc09ddSBjoern A. Zeeb 					IEEE80211_P2P_OPPPS_ENABLE_BIT));
1349bfcc09ddSBjoern A. Zeeb 
1350bfcc09ddSBjoern A. Zeeb 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1351bfcc09ddSBjoern A. Zeeb }
1352bfcc09ddSBjoern A. Zeeb 
iwl_mvm_mac_ctx_send(struct iwl_mvm * mvm,struct ieee80211_vif * vif,u32 action,bool force_assoc_off,const u8 * bssid_override)1353bfcc09ddSBjoern A. Zeeb static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1354bfcc09ddSBjoern A. Zeeb 				u32 action, bool force_assoc_off,
1355bfcc09ddSBjoern A. Zeeb 				const u8 *bssid_override)
1356bfcc09ddSBjoern A. Zeeb {
1357bfcc09ddSBjoern A. Zeeb 	switch (vif->type) {
1358bfcc09ddSBjoern A. Zeeb 	case NL80211_IFTYPE_STATION:
1359bfcc09ddSBjoern A. Zeeb 		return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
1360bfcc09ddSBjoern A. Zeeb 						force_assoc_off,
1361bfcc09ddSBjoern A. Zeeb 						bssid_override);
1362bfcc09ddSBjoern A. Zeeb 	case NL80211_IFTYPE_AP:
1363bfcc09ddSBjoern A. Zeeb 		if (!vif->p2p)
1364bfcc09ddSBjoern A. Zeeb 			return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
1365bfcc09ddSBjoern A. Zeeb 		else
1366bfcc09ddSBjoern A. Zeeb 			return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
1367bfcc09ddSBjoern A. Zeeb 	case NL80211_IFTYPE_MONITOR:
1368bfcc09ddSBjoern A. Zeeb 		return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
1369bfcc09ddSBjoern A. Zeeb 	case NL80211_IFTYPE_P2P_DEVICE:
1370bfcc09ddSBjoern A. Zeeb 		return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
1371bfcc09ddSBjoern A. Zeeb 	case NL80211_IFTYPE_ADHOC:
1372bfcc09ddSBjoern A. Zeeb 		return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
1373bfcc09ddSBjoern A. Zeeb 	default:
1374bfcc09ddSBjoern A. Zeeb 		break;
1375bfcc09ddSBjoern A. Zeeb 	}
1376bfcc09ddSBjoern A. Zeeb 
1377bfcc09ddSBjoern A. Zeeb 	return -EOPNOTSUPP;
1378bfcc09ddSBjoern A. Zeeb }
1379bfcc09ddSBjoern A. Zeeb 
iwl_mvm_mac_ctxt_add(struct iwl_mvm * mvm,struct ieee80211_vif * vif)1380bfcc09ddSBjoern A. Zeeb int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1381bfcc09ddSBjoern A. Zeeb {
1382bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1383bfcc09ddSBjoern A. Zeeb 	int ret;
1384bfcc09ddSBjoern A. Zeeb 
1385bfcc09ddSBjoern A. Zeeb 	if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
1386bfcc09ddSBjoern A. Zeeb 		      vif->addr, ieee80211_vif_type_p2p(vif)))
1387bfcc09ddSBjoern A. Zeeb 		return -EIO;
1388bfcc09ddSBjoern A. Zeeb 
1389bfcc09ddSBjoern A. Zeeb 	ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
1390bfcc09ddSBjoern A. Zeeb 				   true, NULL);
1391bfcc09ddSBjoern A. Zeeb 	if (ret)
1392bfcc09ddSBjoern A. Zeeb 		return ret;
1393bfcc09ddSBjoern A. Zeeb 
1394bfcc09ddSBjoern A. Zeeb 	/* will only do anything at resume from D3 time */
1395bfcc09ddSBjoern A. Zeeb 	iwl_mvm_set_last_nonqos_seq(mvm, vif);
1396bfcc09ddSBjoern A. Zeeb 
1397bfcc09ddSBjoern A. Zeeb 	mvmvif->uploaded = true;
1398bfcc09ddSBjoern A. Zeeb 	return 0;
1399bfcc09ddSBjoern A. Zeeb }
1400bfcc09ddSBjoern A. Zeeb 
iwl_mvm_mac_ctxt_changed(struct iwl_mvm * mvm,struct ieee80211_vif * vif,bool force_assoc_off,const u8 * bssid_override)1401bfcc09ddSBjoern A. Zeeb int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1402bfcc09ddSBjoern A. Zeeb 			     bool force_assoc_off, const u8 *bssid_override)
1403bfcc09ddSBjoern A. Zeeb {
1404bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1405bfcc09ddSBjoern A. Zeeb 
1406bfcc09ddSBjoern A. Zeeb 	if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
1407bfcc09ddSBjoern A. Zeeb 		      vif->addr, ieee80211_vif_type_p2p(vif)))
1408bfcc09ddSBjoern A. Zeeb 		return -EIO;
1409bfcc09ddSBjoern A. Zeeb 
1410bfcc09ddSBjoern A. Zeeb 	return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
1411bfcc09ddSBjoern A. Zeeb 				    force_assoc_off, bssid_override);
1412bfcc09ddSBjoern A. Zeeb }
1413bfcc09ddSBjoern A. Zeeb 
iwl_mvm_mac_ctxt_remove(struct iwl_mvm * mvm,struct ieee80211_vif * vif)1414bfcc09ddSBjoern A. Zeeb int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1415bfcc09ddSBjoern A. Zeeb {
1416bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1417bfcc09ddSBjoern A. Zeeb 	struct iwl_mac_ctx_cmd cmd;
1418bfcc09ddSBjoern A. Zeeb 	int ret;
1419bfcc09ddSBjoern A. Zeeb 
1420bfcc09ddSBjoern A. Zeeb 	if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1421bfcc09ddSBjoern A. Zeeb 		      vif->addr, ieee80211_vif_type_p2p(vif)))
1422bfcc09ddSBjoern A. Zeeb 		return -EIO;
1423bfcc09ddSBjoern A. Zeeb 
1424bfcc09ddSBjoern A. Zeeb 	memset(&cmd, 0, sizeof(cmd));
1425bfcc09ddSBjoern A. Zeeb 
1426bfcc09ddSBjoern A. Zeeb 	cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1427bfcc09ddSBjoern A. Zeeb 							   mvmvif->color));
1428bfcc09ddSBjoern A. Zeeb 	cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1429bfcc09ddSBjoern A. Zeeb 
14309af1bba4SBjoern A. Zeeb 	ret = iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
14319af1bba4SBjoern A. Zeeb 	if (ret)
1432bfcc09ddSBjoern A. Zeeb 		return ret;
1433bfcc09ddSBjoern A. Zeeb 
1434bfcc09ddSBjoern A. Zeeb 	mvmvif->uploaded = false;
1435bfcc09ddSBjoern A. Zeeb 
1436bfcc09ddSBjoern A. Zeeb 	if (vif->type == NL80211_IFTYPE_MONITOR) {
1437bfcc09ddSBjoern A. Zeeb 		__clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, mvm->hw->flags);
1438bfcc09ddSBjoern A. Zeeb 		iwl_mvm_dealloc_snif_sta(mvm);
1439bfcc09ddSBjoern A. Zeeb 	}
1440bfcc09ddSBjoern A. Zeeb 
1441bfcc09ddSBjoern A. Zeeb 	return 0;
1442bfcc09ddSBjoern A. Zeeb }
1443bfcc09ddSBjoern A. Zeeb 
iwl_mvm_csa_count_down(struct iwl_mvm * mvm,struct ieee80211_vif * csa_vif,u32 gp2,bool tx_success)1444bfcc09ddSBjoern A. Zeeb static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
1445bfcc09ddSBjoern A. Zeeb 				   struct ieee80211_vif *csa_vif, u32 gp2,
1446bfcc09ddSBjoern A. Zeeb 				   bool tx_success)
1447bfcc09ddSBjoern A. Zeeb {
1448bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif =
1449bfcc09ddSBjoern A. Zeeb 			iwl_mvm_vif_from_mac80211(csa_vif);
1450bfcc09ddSBjoern A. Zeeb 
1451bfcc09ddSBjoern A. Zeeb 	/* Don't start to countdown from a failed beacon */
1452bfcc09ddSBjoern A. Zeeb 	if (!tx_success && !mvmvif->csa_countdown)
1453bfcc09ddSBjoern A. Zeeb 		return;
1454bfcc09ddSBjoern A. Zeeb 
1455bfcc09ddSBjoern A. Zeeb 	mvmvif->csa_countdown = true;
1456bfcc09ddSBjoern A. Zeeb 
1457bfcc09ddSBjoern A. Zeeb 	if (!ieee80211_beacon_cntdwn_is_complete(csa_vif)) {
1458bfcc09ddSBjoern A. Zeeb 		int c = ieee80211_beacon_update_cntdwn(csa_vif);
1459bfcc09ddSBjoern A. Zeeb 
14609af1bba4SBjoern A. Zeeb 		iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif,
14619af1bba4SBjoern A. Zeeb 						&csa_vif->bss_conf);
1462bfcc09ddSBjoern A. Zeeb 		if (csa_vif->p2p &&
1463bfcc09ddSBjoern A. Zeeb 		    !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2 &&
1464bfcc09ddSBjoern A. Zeeb 		    tx_success) {
1465bfcc09ddSBjoern A. Zeeb 			u32 rel_time = (c + 1) *
1466bfcc09ddSBjoern A. Zeeb 				       csa_vif->bss_conf.beacon_int -
1467bfcc09ddSBjoern A. Zeeb 				       IWL_MVM_CHANNEL_SWITCH_TIME_GO;
1468bfcc09ddSBjoern A. Zeeb 			u32 apply_time = gp2 + rel_time * 1024;
1469bfcc09ddSBjoern A. Zeeb 
1470bfcc09ddSBjoern A. Zeeb 			iwl_mvm_schedule_csa_period(mvm, csa_vif,
1471bfcc09ddSBjoern A. Zeeb 					 IWL_MVM_CHANNEL_SWITCH_TIME_GO -
1472bfcc09ddSBjoern A. Zeeb 					 IWL_MVM_CHANNEL_SWITCH_MARGIN,
1473bfcc09ddSBjoern A. Zeeb 					 apply_time);
1474bfcc09ddSBjoern A. Zeeb 		}
1475bfcc09ddSBjoern A. Zeeb 	} else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
1476bfcc09ddSBjoern A. Zeeb 		/* we don't have CSA NoA scheduled yet, switch now */
1477bfcc09ddSBjoern A. Zeeb 		ieee80211_csa_finish(csa_vif);
1478bfcc09ddSBjoern A. Zeeb 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
1479bfcc09ddSBjoern A. Zeeb 	}
1480bfcc09ddSBjoern A. Zeeb }
1481bfcc09ddSBjoern A. Zeeb 
iwl_mvm_rx_beacon_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)1482bfcc09ddSBjoern A. Zeeb void iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1483bfcc09ddSBjoern A. Zeeb 			     struct iwl_rx_cmd_buffer *rxb)
1484bfcc09ddSBjoern A. Zeeb {
1485bfcc09ddSBjoern A. Zeeb 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1486bfcc09ddSBjoern A. Zeeb 	unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
1487bfcc09ddSBjoern A. Zeeb 	struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
1488bfcc09ddSBjoern A. Zeeb 	struct iwl_extended_beacon_notif_v5 *beacon_v5 = (void *)pkt->data;
1489bfcc09ddSBjoern A. Zeeb 	struct ieee80211_vif *csa_vif;
1490bfcc09ddSBjoern A. Zeeb 	struct ieee80211_vif *tx_blocked_vif;
1491bfcc09ddSBjoern A. Zeeb 	struct agg_tx_status *agg_status;
1492bfcc09ddSBjoern A. Zeeb 	u16 status;
1493bfcc09ddSBjoern A. Zeeb 
1494bfcc09ddSBjoern A. Zeeb 	lockdep_assert_held(&mvm->mutex);
1495bfcc09ddSBjoern A. Zeeb 
1496bfcc09ddSBjoern A. Zeeb 	mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
1497bfcc09ddSBjoern A. Zeeb 
1498bfcc09ddSBjoern A. Zeeb 	if (!iwl_mvm_is_short_beacon_notif_supported(mvm)) {
1499bfcc09ddSBjoern A. Zeeb 		struct iwl_mvm_tx_resp *beacon_notify_hdr =
1500bfcc09ddSBjoern A. Zeeb 			&beacon_v5->beacon_notify_hdr;
1501bfcc09ddSBjoern A. Zeeb 
1502bfcc09ddSBjoern A. Zeeb 		if (unlikely(pkt_len < sizeof(*beacon_v5)))
1503bfcc09ddSBjoern A. Zeeb 			return;
1504bfcc09ddSBjoern A. Zeeb 
1505bfcc09ddSBjoern A. Zeeb 		mvm->ibss_manager = beacon_v5->ibss_mgr_status != 0;
1506bfcc09ddSBjoern A. Zeeb 		agg_status = iwl_mvm_get_agg_status(mvm, beacon_notify_hdr);
1507bfcc09ddSBjoern A. Zeeb 		status = le16_to_cpu(agg_status->status) & TX_STATUS_MSK;
1508bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_RX(mvm,
1509bfcc09ddSBjoern A. Zeeb 			     "beacon status %#x retries:%d tsf:0x%016llX gp2:0x%X rate:%d\n",
1510bfcc09ddSBjoern A. Zeeb 			     status, beacon_notify_hdr->failure_frame,
1511bfcc09ddSBjoern A. Zeeb 			     le64_to_cpu(beacon->tsf),
1512bfcc09ddSBjoern A. Zeeb 			     mvm->ap_last_beacon_gp2,
1513bfcc09ddSBjoern A. Zeeb 			     le32_to_cpu(beacon_notify_hdr->initial_rate));
1514bfcc09ddSBjoern A. Zeeb 	} else {
1515bfcc09ddSBjoern A. Zeeb 		if (unlikely(pkt_len < sizeof(*beacon)))
1516bfcc09ddSBjoern A. Zeeb 			return;
1517bfcc09ddSBjoern A. Zeeb 
1518bfcc09ddSBjoern A. Zeeb 		mvm->ibss_manager = beacon->ibss_mgr_status != 0;
1519bfcc09ddSBjoern A. Zeeb 		status = le32_to_cpu(beacon->status) & TX_STATUS_MSK;
1520bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_RX(mvm,
1521bfcc09ddSBjoern A. Zeeb 			     "beacon status %#x tsf:0x%016llX gp2:0x%X\n",
1522bfcc09ddSBjoern A. Zeeb 			     status, le64_to_cpu(beacon->tsf),
1523bfcc09ddSBjoern A. Zeeb 			     mvm->ap_last_beacon_gp2);
1524bfcc09ddSBjoern A. Zeeb 	}
1525bfcc09ddSBjoern A. Zeeb 
1526bfcc09ddSBjoern A. Zeeb 	csa_vif = rcu_dereference_protected(mvm->csa_vif,
1527bfcc09ddSBjoern A. Zeeb 					    lockdep_is_held(&mvm->mutex));
15289af1bba4SBjoern A. Zeeb 	if (unlikely(csa_vif && csa_vif->bss_conf.csa_active))
1529bfcc09ddSBjoern A. Zeeb 		iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2,
1530bfcc09ddSBjoern A. Zeeb 				       (status == TX_STATUS_SUCCESS));
1531bfcc09ddSBjoern A. Zeeb 
1532bfcc09ddSBjoern A. Zeeb 	tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
1533bfcc09ddSBjoern A. Zeeb 						lockdep_is_held(&mvm->mutex));
1534bfcc09ddSBjoern A. Zeeb 	if (unlikely(tx_blocked_vif)) {
1535bfcc09ddSBjoern A. Zeeb 		struct iwl_mvm_vif *mvmvif =
1536bfcc09ddSBjoern A. Zeeb 			iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1537bfcc09ddSBjoern A. Zeeb 
1538bfcc09ddSBjoern A. Zeeb 		/*
1539bfcc09ddSBjoern A. Zeeb 		 * The channel switch is started and we have blocked the
1540bfcc09ddSBjoern A. Zeeb 		 * stations. If this is the first beacon (the timeout wasn't
1541bfcc09ddSBjoern A. Zeeb 		 * set), set the unblock timeout, otherwise countdown
1542bfcc09ddSBjoern A. Zeeb 		 */
1543bfcc09ddSBjoern A. Zeeb 		if (!mvm->csa_tx_block_bcn_timeout)
1544bfcc09ddSBjoern A. Zeeb 			mvm->csa_tx_block_bcn_timeout =
1545bfcc09ddSBjoern A. Zeeb 				IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
1546bfcc09ddSBjoern A. Zeeb 		else
1547bfcc09ddSBjoern A. Zeeb 			mvm->csa_tx_block_bcn_timeout--;
1548bfcc09ddSBjoern A. Zeeb 
1549bfcc09ddSBjoern A. Zeeb 		/* Check if the timeout is expired, and unblock tx */
1550bfcc09ddSBjoern A. Zeeb 		if (mvm->csa_tx_block_bcn_timeout == 0) {
1551bfcc09ddSBjoern A. Zeeb 			iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
1552bfcc09ddSBjoern A. Zeeb 			RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1553bfcc09ddSBjoern A. Zeeb 		}
1554bfcc09ddSBjoern A. Zeeb 	}
1555bfcc09ddSBjoern A. Zeeb }
1556bfcc09ddSBjoern A. Zeeb 
iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)1557bfcc09ddSBjoern A. Zeeb void iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
1558bfcc09ddSBjoern A. Zeeb 				     struct iwl_rx_cmd_buffer *rxb)
1559bfcc09ddSBjoern A. Zeeb {
1560bfcc09ddSBjoern A. Zeeb 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1561bfcc09ddSBjoern A. Zeeb 	struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
1562bfcc09ddSBjoern A. Zeeb 	struct iwl_fw_dbg_trigger_missed_bcon *bcon_trig;
1563bfcc09ddSBjoern A. Zeeb 	struct iwl_fw_dbg_trigger_tlv *trigger;
1564bfcc09ddSBjoern A. Zeeb 	u32 stop_trig_missed_bcon, stop_trig_missed_bcon_since_rx;
1565bfcc09ddSBjoern A. Zeeb 	u32 rx_missed_bcon, rx_missed_bcon_since_rx;
1566bfcc09ddSBjoern A. Zeeb 	struct ieee80211_vif *vif;
15679af1bba4SBjoern A. Zeeb 	/* Id can be mac/link id depending on the notification version */
15689af1bba4SBjoern A. Zeeb 	u32 id = le32_to_cpu(mb->link_id);
1569bfcc09ddSBjoern A. Zeeb 	union iwl_dbg_tlv_tp_data tp_data = { .fw_pkt = pkt };
15709af1bba4SBjoern A. Zeeb 	u32 mac_type;
15719af1bba4SBjoern A. Zeeb 	u8 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
15729af1bba4SBjoern A. Zeeb 					       MISSED_BEACONS_NOTIFICATION,
15739af1bba4SBjoern A. Zeeb 					       0);
15749af1bba4SBjoern A. Zeeb 
15759af1bba4SBjoern A. Zeeb 	rcu_read_lock();
15769af1bba4SBjoern A. Zeeb 
15779af1bba4SBjoern A. Zeeb 	/* before version four the ID in the notification refers to mac ID */
15789af1bba4SBjoern A. Zeeb 	if (notif_ver < 4) {
15799af1bba4SBjoern A. Zeeb 		vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);
15809af1bba4SBjoern A. Zeeb 	} else {
15819af1bba4SBjoern A. Zeeb 		struct ieee80211_bss_conf *bss_conf =
15829af1bba4SBjoern A. Zeeb 			iwl_mvm_rcu_fw_link_id_to_link_conf(mvm, id, true);
15839af1bba4SBjoern A. Zeeb 
15849af1bba4SBjoern A. Zeeb 		if (!bss_conf)
15859af1bba4SBjoern A. Zeeb 			goto out;
15869af1bba4SBjoern A. Zeeb 
15879af1bba4SBjoern A. Zeeb 		vif = bss_conf->vif;
15889af1bba4SBjoern A. Zeeb 	}
1589bfcc09ddSBjoern A. Zeeb 
1590bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_INFO(mvm,
15919af1bba4SBjoern A. Zeeb 		       "missed bcn %s_id=%u, consecutive=%u (%u, %u, %u)\n",
15929af1bba4SBjoern A. Zeeb 		       notif_ver < 4 ? "mac" : "link",
15939af1bba4SBjoern A. Zeeb 		       id,
1594bfcc09ddSBjoern A. Zeeb 		       le32_to_cpu(mb->consec_missed_beacons),
1595bfcc09ddSBjoern A. Zeeb 		       le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
1596bfcc09ddSBjoern A. Zeeb 		       le32_to_cpu(mb->num_recvd_beacons),
1597bfcc09ddSBjoern A. Zeeb 		       le32_to_cpu(mb->num_expected_beacons));
1598bfcc09ddSBjoern A. Zeeb 
1599bfcc09ddSBjoern A. Zeeb 	if (!vif)
1600bfcc09ddSBjoern A. Zeeb 		goto out;
1601bfcc09ddSBjoern A. Zeeb 
16029af1bba4SBjoern A. Zeeb 	mac_type = iwl_mvm_get_mac_type(vif);
16039af1bba4SBjoern A. Zeeb 
16049af1bba4SBjoern A. Zeeb 	IWL_DEBUG_INFO(mvm, "missed beacon mac_type=%u,\n", mac_type);
16059af1bba4SBjoern A. Zeeb 
16069af1bba4SBjoern A. Zeeb 	mvm->trans->dbg.dump_file_name_ext_valid = true;
16079af1bba4SBjoern A. Zeeb 	snprintf(mvm->trans->dbg.dump_file_name_ext, IWL_FW_INI_MAX_NAME,
16089af1bba4SBjoern A. Zeeb 		 "MacId_%d_MacType_%d", id, mac_type);
16099af1bba4SBjoern A. Zeeb 
1610bfcc09ddSBjoern A. Zeeb 	rx_missed_bcon = le32_to_cpu(mb->consec_missed_beacons);
1611bfcc09ddSBjoern A. Zeeb 	rx_missed_bcon_since_rx =
1612bfcc09ddSBjoern A. Zeeb 		le32_to_cpu(mb->consec_missed_beacons_since_last_rx);
1613bfcc09ddSBjoern A. Zeeb 	/*
1614bfcc09ddSBjoern A. Zeeb 	 * TODO: the threshold should be adjusted based on latency conditions,
1615bfcc09ddSBjoern A. Zeeb 	 * and/or in case of a CS flow on one of the other AP vifs.
1616bfcc09ddSBjoern A. Zeeb 	 */
1617bfcc09ddSBjoern A. Zeeb 	if (rx_missed_bcon > IWL_MVM_MISSED_BEACONS_THRESHOLD_LONG)
1618bfcc09ddSBjoern A. Zeeb 		iwl_mvm_connection_loss(mvm, vif, "missed beacons");
1619bfcc09ddSBjoern A. Zeeb 	else if (rx_missed_bcon_since_rx > IWL_MVM_MISSED_BEACONS_THRESHOLD)
1620bfcc09ddSBjoern A. Zeeb 		ieee80211_beacon_loss(vif);
1621bfcc09ddSBjoern A. Zeeb 
1622bfcc09ddSBjoern A. Zeeb 	iwl_dbg_tlv_time_point(&mvm->fwrt,
1623bfcc09ddSBjoern A. Zeeb 			       IWL_FW_INI_TIME_POINT_MISSED_BEACONS, &tp_data);
1624bfcc09ddSBjoern A. Zeeb 
1625bfcc09ddSBjoern A. Zeeb 	trigger = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
1626bfcc09ddSBjoern A. Zeeb 					FW_DBG_TRIGGER_MISSED_BEACONS);
1627bfcc09ddSBjoern A. Zeeb 	if (!trigger)
1628bfcc09ddSBjoern A. Zeeb 		goto out;
1629bfcc09ddSBjoern A. Zeeb 
1630bfcc09ddSBjoern A. Zeeb 	bcon_trig = (void *)trigger->data;
1631bfcc09ddSBjoern A. Zeeb 	stop_trig_missed_bcon = le32_to_cpu(bcon_trig->stop_consec_missed_bcon);
1632bfcc09ddSBjoern A. Zeeb 	stop_trig_missed_bcon_since_rx =
1633bfcc09ddSBjoern A. Zeeb 		le32_to_cpu(bcon_trig->stop_consec_missed_bcon_since_rx);
1634bfcc09ddSBjoern A. Zeeb 
1635bfcc09ddSBjoern A. Zeeb 	/* TODO: implement start trigger */
1636bfcc09ddSBjoern A. Zeeb 
1637bfcc09ddSBjoern A. Zeeb 	if (rx_missed_bcon_since_rx >= stop_trig_missed_bcon_since_rx ||
1638bfcc09ddSBjoern A. Zeeb 	    rx_missed_bcon >= stop_trig_missed_bcon)
1639f621b087SBjoern A. Zeeb #if defined(__linux__)
1640bfcc09ddSBjoern A. Zeeb 		iwl_fw_dbg_collect_trig(&mvm->fwrt, trigger, NULL);
1641f621b087SBjoern A. Zeeb #elif defined(__FreeBSD__)
1642f621b087SBjoern A. Zeeb 		iwl_fw_dbg_collect_trig(&mvm->fwrt, trigger, "");
1643f621b087SBjoern A. Zeeb #endif
1644bfcc09ddSBjoern A. Zeeb 
1645bfcc09ddSBjoern A. Zeeb out:
1646bfcc09ddSBjoern A. Zeeb 	rcu_read_unlock();
1647bfcc09ddSBjoern A. Zeeb }
1648bfcc09ddSBjoern A. Zeeb 
iwl_mvm_rx_stored_beacon_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)1649bfcc09ddSBjoern A. Zeeb void iwl_mvm_rx_stored_beacon_notif(struct iwl_mvm *mvm,
1650bfcc09ddSBjoern A. Zeeb 				    struct iwl_rx_cmd_buffer *rxb)
1651bfcc09ddSBjoern A. Zeeb {
1652bfcc09ddSBjoern A. Zeeb 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1653bfcc09ddSBjoern A. Zeeb 	unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
1654bfcc09ddSBjoern A. Zeeb 	struct iwl_stored_beacon_notif_common *sb = (void *)pkt->data;
1655bfcc09ddSBjoern A. Zeeb 	struct ieee80211_rx_status rx_status;
1656bfcc09ddSBjoern A. Zeeb 	struct sk_buff *skb;
1657bfcc09ddSBjoern A. Zeeb 	u8 *data;
1658bfcc09ddSBjoern A. Zeeb 	u32 size = le32_to_cpu(sb->byte_count);
1659d9836fb4SBjoern A. Zeeb 	int ver = iwl_fw_lookup_cmd_ver(mvm->fw,
1660d9836fb4SBjoern A. Zeeb 					WIDE_ID(PROT_OFFLOAD_GROUP, STORED_BEACON_NTF),
1661d9836fb4SBjoern A. Zeeb 					0);
1662bfcc09ddSBjoern A. Zeeb 
1663bfcc09ddSBjoern A. Zeeb 	if (size == 0)
1664bfcc09ddSBjoern A. Zeeb 		return;
1665bfcc09ddSBjoern A. Zeeb 
1666bfcc09ddSBjoern A. Zeeb 	/* handle per-version differences */
1667bfcc09ddSBjoern A. Zeeb 	if (ver <= 2) {
1668bfcc09ddSBjoern A. Zeeb 		struct iwl_stored_beacon_notif_v2 *sb_v2 = (void *)pkt->data;
1669bfcc09ddSBjoern A. Zeeb 
1670bfcc09ddSBjoern A. Zeeb 		if (pkt_len < struct_size(sb_v2, data, size))
1671bfcc09ddSBjoern A. Zeeb 			return;
1672bfcc09ddSBjoern A. Zeeb 
1673bfcc09ddSBjoern A. Zeeb 		data = sb_v2->data;
1674bfcc09ddSBjoern A. Zeeb 	} else {
1675bfcc09ddSBjoern A. Zeeb 		struct iwl_stored_beacon_notif_v3 *sb_v3 = (void *)pkt->data;
1676bfcc09ddSBjoern A. Zeeb 
1677bfcc09ddSBjoern A. Zeeb 		if (pkt_len < struct_size(sb_v3, data, size))
1678bfcc09ddSBjoern A. Zeeb 			return;
1679bfcc09ddSBjoern A. Zeeb 
1680bfcc09ddSBjoern A. Zeeb 		data = sb_v3->data;
1681bfcc09ddSBjoern A. Zeeb 	}
1682bfcc09ddSBjoern A. Zeeb 
1683bfcc09ddSBjoern A. Zeeb 	skb = alloc_skb(size, GFP_ATOMIC);
1684bfcc09ddSBjoern A. Zeeb 	if (!skb) {
1685bfcc09ddSBjoern A. Zeeb 		IWL_ERR(mvm, "alloc_skb failed\n");
1686bfcc09ddSBjoern A. Zeeb 		return;
1687bfcc09ddSBjoern A. Zeeb 	}
1688bfcc09ddSBjoern A. Zeeb 
1689bfcc09ddSBjoern A. Zeeb 	/* update rx_status according to the notification's metadata */
1690bfcc09ddSBjoern A. Zeeb 	memset(&rx_status, 0, sizeof(rx_status));
1691bfcc09ddSBjoern A. Zeeb 	rx_status.mactime = le64_to_cpu(sb->tsf);
1692bfcc09ddSBjoern A. Zeeb 	/* TSF as indicated by the firmware  is at INA time */
1693bfcc09ddSBjoern A. Zeeb 	rx_status.flag |= RX_FLAG_MACTIME_PLCP_START;
1694bfcc09ddSBjoern A. Zeeb 	rx_status.device_timestamp = le32_to_cpu(sb->system_time);
1695bfcc09ddSBjoern A. Zeeb 	rx_status.band =
1696bfcc09ddSBjoern A. Zeeb 		(sb->band & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
1697bfcc09ddSBjoern A. Zeeb 				NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
1698bfcc09ddSBjoern A. Zeeb 	rx_status.freq =
1699bfcc09ddSBjoern A. Zeeb 		ieee80211_channel_to_frequency(le16_to_cpu(sb->channel),
1700bfcc09ddSBjoern A. Zeeb 					       rx_status.band);
1701bfcc09ddSBjoern A. Zeeb 
1702bfcc09ddSBjoern A. Zeeb 	/* copy the data */
1703bfcc09ddSBjoern A. Zeeb 	skb_put_data(skb, data, size);
1704bfcc09ddSBjoern A. Zeeb 	memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
1705bfcc09ddSBjoern A. Zeeb 
1706bfcc09ddSBjoern A. Zeeb 	/* pass it as regular rx to mac80211 */
1707bfcc09ddSBjoern A. Zeeb 	ieee80211_rx_napi(mvm->hw, NULL, skb, NULL);
1708bfcc09ddSBjoern A. Zeeb }
1709bfcc09ddSBjoern A. Zeeb 
iwl_mvm_probe_resp_data_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)1710bfcc09ddSBjoern A. Zeeb void iwl_mvm_probe_resp_data_notif(struct iwl_mvm *mvm,
1711bfcc09ddSBjoern A. Zeeb 				   struct iwl_rx_cmd_buffer *rxb)
1712bfcc09ddSBjoern A. Zeeb {
1713bfcc09ddSBjoern A. Zeeb 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1714bfcc09ddSBjoern A. Zeeb 	struct iwl_probe_resp_data_notif *notif = (void *)pkt->data;
1715bfcc09ddSBjoern A. Zeeb 	struct iwl_probe_resp_data *old_data, *new_data;
1716bfcc09ddSBjoern A. Zeeb 	u32 id = le32_to_cpu(notif->mac_id);
1717bfcc09ddSBjoern A. Zeeb 	struct ieee80211_vif *vif;
1718bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif;
1719bfcc09ddSBjoern A. Zeeb 
1720bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_INFO(mvm, "Probe response data notif: noa %d, csa %d\n",
1721bfcc09ddSBjoern A. Zeeb 		       notif->noa_active, notif->csa_counter);
1722bfcc09ddSBjoern A. Zeeb 
1723bfcc09ddSBjoern A. Zeeb 	vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, false);
1724bfcc09ddSBjoern A. Zeeb 	if (!vif)
1725bfcc09ddSBjoern A. Zeeb 		return;
1726bfcc09ddSBjoern A. Zeeb 
1727bfcc09ddSBjoern A. Zeeb 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
1728bfcc09ddSBjoern A. Zeeb 
1729bfcc09ddSBjoern A. Zeeb 	new_data = kzalloc(sizeof(*new_data), GFP_KERNEL);
1730bfcc09ddSBjoern A. Zeeb 	if (!new_data)
1731bfcc09ddSBjoern A. Zeeb 		return;
1732bfcc09ddSBjoern A. Zeeb 
1733bfcc09ddSBjoern A. Zeeb 	memcpy(&new_data->notif, notif, sizeof(new_data->notif));
1734bfcc09ddSBjoern A. Zeeb 
1735bfcc09ddSBjoern A. Zeeb 	/* noa_attr contains 1 reserved byte, need to substruct it */
1736bfcc09ddSBjoern A. Zeeb 	new_data->noa_len = sizeof(struct ieee80211_vendor_ie) +
1737bfcc09ddSBjoern A. Zeeb 			    sizeof(new_data->notif.noa_attr) - 1;
1738bfcc09ddSBjoern A. Zeeb 
1739bfcc09ddSBjoern A. Zeeb 	/*
1740bfcc09ddSBjoern A. Zeeb 	 * If it's a one time NoA, only one descriptor is needed,
1741bfcc09ddSBjoern A. Zeeb 	 * adjust the length according to len_low.
1742bfcc09ddSBjoern A. Zeeb 	 */
1743bfcc09ddSBjoern A. Zeeb 	if (new_data->notif.noa_attr.len_low ==
1744bfcc09ddSBjoern A. Zeeb 	    sizeof(struct ieee80211_p2p_noa_desc) + 2)
1745bfcc09ddSBjoern A. Zeeb 		new_data->noa_len -= sizeof(struct ieee80211_p2p_noa_desc);
1746bfcc09ddSBjoern A. Zeeb 
17479af1bba4SBjoern A. Zeeb 	old_data = rcu_dereference_protected(mvmvif->deflink.probe_resp_data,
1748bfcc09ddSBjoern A. Zeeb 					     lockdep_is_held(&mvmvif->mvm->mutex));
17499af1bba4SBjoern A. Zeeb 	rcu_assign_pointer(mvmvif->deflink.probe_resp_data, new_data);
1750bfcc09ddSBjoern A. Zeeb 
1751bfcc09ddSBjoern A. Zeeb 	if (old_data)
1752bfcc09ddSBjoern A. Zeeb 		kfree_rcu(old_data, rcu_head);
1753bfcc09ddSBjoern A. Zeeb 
1754bfcc09ddSBjoern A. Zeeb 	if (notif->csa_counter != IWL_PROBE_RESP_DATA_NO_CSA &&
1755bfcc09ddSBjoern A. Zeeb 	    notif->csa_counter >= 1)
1756bfcc09ddSBjoern A. Zeeb 		ieee80211_beacon_set_cntdwn(vif, notif->csa_counter);
1757bfcc09ddSBjoern A. Zeeb }
1758bfcc09ddSBjoern A. Zeeb 
iwl_mvm_channel_switch_start_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)1759bfcc09ddSBjoern A. Zeeb void iwl_mvm_channel_switch_start_notif(struct iwl_mvm *mvm,
1760bfcc09ddSBjoern A. Zeeb 					struct iwl_rx_cmd_buffer *rxb)
1761bfcc09ddSBjoern A. Zeeb {
1762bfcc09ddSBjoern A. Zeeb 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1763bfcc09ddSBjoern A. Zeeb 	struct ieee80211_vif *csa_vif, *vif;
17649af1bba4SBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif, *csa_mvmvif;
17659af1bba4SBjoern A. Zeeb 	u32 id_n_color, csa_id;
17669af1bba4SBjoern A. Zeeb 	/* save mac_id or link_id to use later to cancel csa if needed */
17679af1bba4SBjoern A. Zeeb 	u32 id;
17689af1bba4SBjoern A. Zeeb 	u8 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
17699af1bba4SBjoern A. Zeeb 					       CHANNEL_SWITCH_START_NOTIF, 0);
17709af1bba4SBjoern A. Zeeb 	bool csa_active;
17719af1bba4SBjoern A. Zeeb 
17729af1bba4SBjoern A. Zeeb 	rcu_read_lock();
17739af1bba4SBjoern A. Zeeb 
17749af1bba4SBjoern A. Zeeb 	if (notif_ver < 3) {
17759af1bba4SBjoern A. Zeeb 		struct iwl_channel_switch_start_notif_v1 *notif = (void *)pkt->data;
17769af1bba4SBjoern A. Zeeb 		u32 mac_id;
1777bfcc09ddSBjoern A. Zeeb 
1778bfcc09ddSBjoern A. Zeeb 		id_n_color = le32_to_cpu(notif->id_and_color);
1779bfcc09ddSBjoern A. Zeeb 		mac_id = id_n_color & FW_CTXT_ID_MSK;
1780bfcc09ddSBjoern A. Zeeb 
17819af1bba4SBjoern A. Zeeb 		vif = iwl_mvm_rcu_dereference_vif_id(mvm, mac_id, true);
17829af1bba4SBjoern A. Zeeb 		if (!vif)
17839af1bba4SBjoern A. Zeeb 			goto out_unlock;
1784bfcc09ddSBjoern A. Zeeb 
17859af1bba4SBjoern A. Zeeb 		id = mac_id;
17869af1bba4SBjoern A. Zeeb 		csa_active = vif->bss_conf.csa_active;
17879af1bba4SBjoern A. Zeeb 	} else {
17889af1bba4SBjoern A. Zeeb 		struct iwl_channel_switch_start_notif *notif = (void *)pkt->data;
17899af1bba4SBjoern A. Zeeb 		u32 link_id = le32_to_cpu(notif->link_id);
17909af1bba4SBjoern A. Zeeb 		struct ieee80211_bss_conf *bss_conf =
17919af1bba4SBjoern A. Zeeb 			iwl_mvm_rcu_fw_link_id_to_link_conf(mvm, link_id, true);
17929af1bba4SBjoern A. Zeeb 
17939af1bba4SBjoern A. Zeeb 		if (!bss_conf)
17949af1bba4SBjoern A. Zeeb 			goto out_unlock;
17959af1bba4SBjoern A. Zeeb 
17969af1bba4SBjoern A. Zeeb 		id = link_id;
17979af1bba4SBjoern A. Zeeb 		vif = bss_conf->vif;
17989af1bba4SBjoern A. Zeeb 		csa_active = bss_conf->csa_active;
17999af1bba4SBjoern A. Zeeb 	}
18009af1bba4SBjoern A. Zeeb 
1801bfcc09ddSBjoern A. Zeeb 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
18029af1bba4SBjoern A. Zeeb 	if (notif_ver >= 3)
18039af1bba4SBjoern A. Zeeb 		id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
1804bfcc09ddSBjoern A. Zeeb 
1805bfcc09ddSBjoern A. Zeeb 	switch (vif->type) {
1806bfcc09ddSBjoern A. Zeeb 	case NL80211_IFTYPE_AP:
1807bfcc09ddSBjoern A. Zeeb 		csa_vif = rcu_dereference(mvm->csa_vif);
18089af1bba4SBjoern A. Zeeb 		if (WARN_ON(!csa_vif || !csa_vif->bss_conf.csa_active ||
1809bfcc09ddSBjoern A. Zeeb 			    csa_vif != vif))
1810bfcc09ddSBjoern A. Zeeb 			goto out_unlock;
1811bfcc09ddSBjoern A. Zeeb 
18129af1bba4SBjoern A. Zeeb 		csa_mvmvif = iwl_mvm_vif_from_mac80211(csa_vif);
18139af1bba4SBjoern A. Zeeb 		csa_id = FW_CMD_ID_AND_COLOR(csa_mvmvif->id, csa_mvmvif->color);
1814bfcc09ddSBjoern A. Zeeb 		if (WARN(csa_id != id_n_color,
1815bfcc09ddSBjoern A. Zeeb 			 "channel switch noa notification on unexpected vif (csa_vif=%d, notif=%d)",
1816bfcc09ddSBjoern A. Zeeb 			 csa_id, id_n_color))
1817bfcc09ddSBjoern A. Zeeb 			goto out_unlock;
1818bfcc09ddSBjoern A. Zeeb 
1819bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_INFO(mvm, "Channel Switch Started Notification\n");
1820bfcc09ddSBjoern A. Zeeb 
1821bfcc09ddSBjoern A. Zeeb 		schedule_delayed_work(&mvm->cs_tx_unblock_dwork,
1822bfcc09ddSBjoern A. Zeeb 				      msecs_to_jiffies(IWL_MVM_CS_UNBLOCK_TX_TIMEOUT *
1823bfcc09ddSBjoern A. Zeeb 						       csa_vif->bss_conf.beacon_int));
1824bfcc09ddSBjoern A. Zeeb 
1825bfcc09ddSBjoern A. Zeeb 		ieee80211_csa_finish(csa_vif);
1826bfcc09ddSBjoern A. Zeeb 
1827bfcc09ddSBjoern A. Zeeb 		rcu_read_unlock();
1828bfcc09ddSBjoern A. Zeeb 
1829bfcc09ddSBjoern A. Zeeb 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
1830bfcc09ddSBjoern A. Zeeb 		return;
1831bfcc09ddSBjoern A. Zeeb 	case NL80211_IFTYPE_STATION:
1832d9836fb4SBjoern A. Zeeb 		/*
1833d9836fb4SBjoern A. Zeeb 		 * if we don't know about an ongoing channel switch,
1834d9836fb4SBjoern A. Zeeb 		 * make sure FW cancels it
1835d9836fb4SBjoern A. Zeeb 		 */
1836d9836fb4SBjoern A. Zeeb 		if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
1837d9836fb4SBjoern A. Zeeb 					    CHANNEL_SWITCH_ERROR_NOTIF,
18389af1bba4SBjoern A. Zeeb 					    0) && !csa_active) {
1839d9836fb4SBjoern A. Zeeb 			IWL_DEBUG_INFO(mvm, "Channel Switch was canceled\n");
18409af1bba4SBjoern A. Zeeb 			iwl_mvm_cancel_channel_switch(mvm, vif, id);
1841d9836fb4SBjoern A. Zeeb 			break;
1842d9836fb4SBjoern A. Zeeb 		}
1843d9836fb4SBjoern A. Zeeb 
1844bfcc09ddSBjoern A. Zeeb 		iwl_mvm_csa_client_absent(mvm, vif);
1845bfcc09ddSBjoern A. Zeeb 		cancel_delayed_work(&mvmvif->csa_work);
1846bfcc09ddSBjoern A. Zeeb 		ieee80211_chswitch_done(vif, true);
1847bfcc09ddSBjoern A. Zeeb 		break;
1848bfcc09ddSBjoern A. Zeeb 	default:
1849bfcc09ddSBjoern A. Zeeb 		/* should never happen */
1850bfcc09ddSBjoern A. Zeeb 		WARN_ON_ONCE(1);
1851bfcc09ddSBjoern A. Zeeb 		break;
1852bfcc09ddSBjoern A. Zeeb 	}
1853bfcc09ddSBjoern A. Zeeb out_unlock:
1854bfcc09ddSBjoern A. Zeeb 	rcu_read_unlock();
1855bfcc09ddSBjoern A. Zeeb }
1856bfcc09ddSBjoern A. Zeeb 
iwl_mvm_channel_switch_error_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)1857d9836fb4SBjoern A. Zeeb void iwl_mvm_channel_switch_error_notif(struct iwl_mvm *mvm,
1858d9836fb4SBjoern A. Zeeb 					struct iwl_rx_cmd_buffer *rxb)
1859d9836fb4SBjoern A. Zeeb {
1860d9836fb4SBjoern A. Zeeb 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1861d9836fb4SBjoern A. Zeeb 	struct iwl_channel_switch_error_notif *notif = (void *)pkt->data;
1862d9836fb4SBjoern A. Zeeb 	struct ieee80211_vif *vif;
18639af1bba4SBjoern A. Zeeb 	u32 id = le32_to_cpu(notif->link_id);
1864d9836fb4SBjoern A. Zeeb 	u32 csa_err_mask = le32_to_cpu(notif->csa_err_mask);
1865d9836fb4SBjoern A. Zeeb 
1866d9836fb4SBjoern A. Zeeb 	rcu_read_lock();
1867d9836fb4SBjoern A. Zeeb 	vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);
1868d9836fb4SBjoern A. Zeeb 	if (!vif) {
1869d9836fb4SBjoern A. Zeeb 		rcu_read_unlock();
1870d9836fb4SBjoern A. Zeeb 		return;
1871d9836fb4SBjoern A. Zeeb 	}
1872d9836fb4SBjoern A. Zeeb 
18739af1bba4SBjoern A. Zeeb 	IWL_DEBUG_INFO(mvm, "FW reports CSA error: id=%u, csa_err_mask=%u\n",
1874d9836fb4SBjoern A. Zeeb 		       id, csa_err_mask);
1875d9836fb4SBjoern A. Zeeb 	if (csa_err_mask & (CS_ERR_COUNT_ERROR |
1876d9836fb4SBjoern A. Zeeb 			    CS_ERR_LONG_DELAY_AFTER_CS |
1877d9836fb4SBjoern A. Zeeb 			    CS_ERR_TX_BLOCK_TIMER_EXPIRED))
1878d9836fb4SBjoern A. Zeeb 		ieee80211_channel_switch_disconnect(vif, true);
1879d9836fb4SBjoern A. Zeeb 	rcu_read_unlock();
1880d9836fb4SBjoern A. Zeeb }
1881d9836fb4SBjoern A. Zeeb 
iwl_mvm_rx_missed_vap_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)1882bfcc09ddSBjoern A. Zeeb void iwl_mvm_rx_missed_vap_notif(struct iwl_mvm *mvm,
1883bfcc09ddSBjoern A. Zeeb 				 struct iwl_rx_cmd_buffer *rxb)
1884bfcc09ddSBjoern A. Zeeb {
1885bfcc09ddSBjoern A. Zeeb 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1886bfcc09ddSBjoern A. Zeeb 	struct iwl_missed_vap_notif *mb = (void *)pkt->data;
1887bfcc09ddSBjoern A. Zeeb 	struct ieee80211_vif *vif;
1888bfcc09ddSBjoern A. Zeeb 	u32 id = le32_to_cpu(mb->mac_id);
1889bfcc09ddSBjoern A. Zeeb 
1890bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_INFO(mvm,
1891bfcc09ddSBjoern A. Zeeb 		       "missed_vap notify mac_id=%u, num_beacon_intervals_elapsed=%u, profile_periodicity=%u\n",
1892bfcc09ddSBjoern A. Zeeb 		       le32_to_cpu(mb->mac_id),
1893bfcc09ddSBjoern A. Zeeb 		       mb->num_beacon_intervals_elapsed,
1894bfcc09ddSBjoern A. Zeeb 		       mb->profile_periodicity);
1895bfcc09ddSBjoern A. Zeeb 
1896bfcc09ddSBjoern A. Zeeb 	rcu_read_lock();
1897bfcc09ddSBjoern A. Zeeb 
1898bfcc09ddSBjoern A. Zeeb 	vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);
1899bfcc09ddSBjoern A. Zeeb 	if (vif)
1900bfcc09ddSBjoern A. Zeeb 		iwl_mvm_connection_loss(mvm, vif, "missed vap beacon");
1901bfcc09ddSBjoern A. Zeeb 
1902bfcc09ddSBjoern A. Zeeb 	rcu_read_unlock();
1903bfcc09ddSBjoern A. Zeeb }
1904