1e705c121SKalle Valo /******************************************************************************
2e705c121SKalle Valo  *
3e705c121SKalle Valo  * This file is provided under a dual BSD/GPLv2 license.  When using or
4e705c121SKalle Valo  * redistributing this file, you may do so under either license.
5e705c121SKalle Valo  *
6e705c121SKalle Valo  * GPL LICENSE SUMMARY
7e705c121SKalle Valo  *
8e705c121SKalle Valo  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9e705c121SKalle Valo  * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
100db056d3SSara Sharon  * Copyright(c) 2015 - 2016 Intel Deutschland GmbH
11e705c121SKalle Valo  *
12e705c121SKalle Valo  * This program is free software; you can redistribute it and/or modify
13e705c121SKalle Valo  * it under the terms of version 2 of the GNU General Public License as
14e705c121SKalle Valo  * published by the Free Software Foundation.
15e705c121SKalle Valo  *
16e705c121SKalle Valo  * This program is distributed in the hope that it will be useful, but
17e705c121SKalle Valo  * WITHOUT ANY WARRANTY; without even the implied warranty of
18e705c121SKalle Valo  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19e705c121SKalle Valo  * General Public License for more details.
20e705c121SKalle Valo  *
21e705c121SKalle Valo  * You should have received a copy of the GNU General Public License
22e705c121SKalle Valo  * along with this program; if not, write to the Free Software
23e705c121SKalle Valo  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
24e705c121SKalle Valo  * USA
25e705c121SKalle Valo  *
26e705c121SKalle Valo  * The full GNU General Public License is included in this distribution
27e705c121SKalle Valo  * in the file called COPYING.
28e705c121SKalle Valo  *
29e705c121SKalle Valo  * Contact Information:
30cb2f8277SEmmanuel Grumbach  *  Intel Linux Wireless <linuxwifi@intel.com>
31e705c121SKalle Valo  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
32e705c121SKalle Valo  *
33e705c121SKalle Valo  * BSD LICENSE
34e705c121SKalle Valo  *
35e705c121SKalle Valo  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
36e705c121SKalle Valo  * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
370db056d3SSara Sharon  * Copyright(c) 2015 - 2016 Intel Deutschland GmbH
38e705c121SKalle Valo  * All rights reserved.
39e705c121SKalle Valo  *
40e705c121SKalle Valo  * Redistribution and use in source and binary forms, with or without
41e705c121SKalle Valo  * modification, are permitted provided that the following conditions
42e705c121SKalle Valo  * are met:
43e705c121SKalle Valo  *
44e705c121SKalle Valo  *  * Redistributions of source code must retain the above copyright
45e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer.
46e705c121SKalle Valo  *  * Redistributions in binary form must reproduce the above copyright
47e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer in
48e705c121SKalle Valo  *    the documentation and/or other materials provided with the
49e705c121SKalle Valo  *    distribution.
50e705c121SKalle Valo  *  * Neither the name Intel Corporation nor the names of its
51e705c121SKalle Valo  *    contributors may be used to endorse or promote products derived
52e705c121SKalle Valo  *    from this software without specific prior written permission.
53e705c121SKalle Valo  *
54e705c121SKalle Valo  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55e705c121SKalle Valo  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56e705c121SKalle Valo  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57e705c121SKalle Valo  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58e705c121SKalle Valo  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59e705c121SKalle Valo  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60e705c121SKalle Valo  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
61e705c121SKalle Valo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
62e705c121SKalle Valo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63e705c121SKalle Valo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
64e705c121SKalle Valo  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65e705c121SKalle Valo  *
66e705c121SKalle Valo  *****************************************************************************/
67e705c121SKalle Valo 
68e705c121SKalle Valo #include <linux/etherdevice.h>
69e705c121SKalle Valo #include <net/mac80211.h>
70e705c121SKalle Valo #include "iwl-io.h"
71e705c121SKalle Valo #include "iwl-prph.h"
72e705c121SKalle Valo #include "fw-api.h"
73e705c121SKalle Valo #include "mvm.h"
74e705c121SKalle Valo #include "time-event.h"
752f89a5d7SGolan Ben-Ami #include "fw-dbg.h"
76e705c121SKalle Valo 
77e705c121SKalle Valo const u8 iwl_mvm_ac_to_tx_fifo[] = {
78e705c121SKalle Valo 	IWL_MVM_TX_FIFO_VO,
79e705c121SKalle Valo 	IWL_MVM_TX_FIFO_VI,
80e705c121SKalle Valo 	IWL_MVM_TX_FIFO_BE,
81e705c121SKalle Valo 	IWL_MVM_TX_FIFO_BK,
82e705c121SKalle Valo };
83e705c121SKalle Valo 
84e705c121SKalle Valo struct iwl_mvm_mac_iface_iterator_data {
85e705c121SKalle Valo 	struct iwl_mvm *mvm;
86e705c121SKalle Valo 	struct ieee80211_vif *vif;
87e705c121SKalle Valo 	unsigned long available_mac_ids[BITS_TO_LONGS(NUM_MAC_INDEX_DRIVER)];
88e705c121SKalle Valo 	unsigned long available_tsf_ids[BITS_TO_LONGS(NUM_TSF_IDS)];
89e705c121SKalle Valo 	enum iwl_tsf_id preferred_tsf;
90e705c121SKalle Valo 	bool found_vif;
91e705c121SKalle Valo };
92e705c121SKalle Valo 
93e705c121SKalle Valo struct iwl_mvm_hw_queues_iface_iterator_data {
94e705c121SKalle Valo 	struct ieee80211_vif *exclude_vif;
95e705c121SKalle Valo 	unsigned long used_hw_queues;
96e705c121SKalle Valo };
97e705c121SKalle Valo 
98e705c121SKalle Valo static void iwl_mvm_mac_tsf_id_iter(void *_data, u8 *mac,
99e705c121SKalle Valo 				    struct ieee80211_vif *vif)
100e705c121SKalle Valo {
101e705c121SKalle Valo 	struct iwl_mvm_mac_iface_iterator_data *data = _data;
102e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
103e705c121SKalle Valo 	u16 min_bi;
104e705c121SKalle Valo 
105e705c121SKalle Valo 	/* Skip the interface for which we are trying to assign a tsf_id  */
106e705c121SKalle Valo 	if (vif == data->vif)
107e705c121SKalle Valo 		return;
108e705c121SKalle Valo 
109e705c121SKalle Valo 	/*
110e705c121SKalle Valo 	 * The TSF is a hardware/firmware resource, there are 4 and
111e705c121SKalle Valo 	 * the driver should assign and free them as needed. However,
112e705c121SKalle Valo 	 * there are cases where 2 MACs should share the same TSF ID
113e705c121SKalle Valo 	 * for the purpose of clock sync, an optimization to avoid
114e705c121SKalle Valo 	 * clock drift causing overlapping TBTTs/DTIMs for a GO and
115e705c121SKalle Valo 	 * client in the system.
116e705c121SKalle Valo 	 *
117e705c121SKalle Valo 	 * The firmware will decide according to the MAC type which
118e705c121SKalle Valo 	 * will be the master and slave. Clients that need to sync
119e705c121SKalle Valo 	 * with a remote station will be the master, and an AP or GO
120e705c121SKalle Valo 	 * will be the slave.
121e705c121SKalle Valo 	 *
122e705c121SKalle Valo 	 * Depending on the new interface type it can be slaved to
123e705c121SKalle Valo 	 * or become the master of an existing interface.
124e705c121SKalle Valo 	 */
125e705c121SKalle Valo 	switch (data->vif->type) {
126e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
127e705c121SKalle Valo 		/*
128e705c121SKalle Valo 		 * The new interface is a client, so if the one we're iterating
129e705c121SKalle Valo 		 * is an AP, and the beacon interval of the AP is a multiple or
130e705c121SKalle Valo 		 * divisor of the beacon interval of the client, the same TSF
131e705c121SKalle Valo 		 * should be used to avoid drift between the new client and
132e705c121SKalle Valo 		 * existing AP. The existing AP will get drift updates from the
133e705c121SKalle Valo 		 * new client context in this case.
134e705c121SKalle Valo 		 */
135e705c121SKalle Valo 		if (vif->type != NL80211_IFTYPE_AP ||
136e705c121SKalle Valo 		    data->preferred_tsf != NUM_TSF_IDS ||
137e705c121SKalle Valo 		    !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
138e705c121SKalle Valo 			break;
139e705c121SKalle Valo 
140e705c121SKalle Valo 		min_bi = min(data->vif->bss_conf.beacon_int,
141e705c121SKalle Valo 			     vif->bss_conf.beacon_int);
142e705c121SKalle Valo 
143e705c121SKalle Valo 		if (!min_bi)
144e705c121SKalle Valo 			break;
145e705c121SKalle Valo 
146e705c121SKalle Valo 		if ((data->vif->bss_conf.beacon_int -
147e705c121SKalle Valo 		     vif->bss_conf.beacon_int) % min_bi == 0) {
148e705c121SKalle Valo 			data->preferred_tsf = mvmvif->tsf_id;
149e705c121SKalle Valo 			return;
150e705c121SKalle Valo 		}
151e705c121SKalle Valo 		break;
152e705c121SKalle Valo 
153e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
154e705c121SKalle Valo 		/*
155e705c121SKalle Valo 		 * The new interface is AP/GO, so if its beacon interval is a
156e705c121SKalle Valo 		 * multiple or a divisor of the beacon interval of an existing
157e705c121SKalle Valo 		 * interface, it should get drift updates from an existing
158e705c121SKalle Valo 		 * client or use the same TSF as an existing GO. There's no
159e705c121SKalle Valo 		 * drift between TSFs internally but if they used different
160e705c121SKalle Valo 		 * TSFs then a new client MAC could update one of them and
161e705c121SKalle Valo 		 * cause drift that way.
162e705c121SKalle Valo 		 */
163e705c121SKalle Valo 		if ((vif->type != NL80211_IFTYPE_AP &&
164e705c121SKalle Valo 		     vif->type != NL80211_IFTYPE_STATION) ||
165e705c121SKalle Valo 		    data->preferred_tsf != NUM_TSF_IDS ||
166e705c121SKalle Valo 		    !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
167e705c121SKalle Valo 			break;
168e705c121SKalle Valo 
169e705c121SKalle Valo 		min_bi = min(data->vif->bss_conf.beacon_int,
170e705c121SKalle Valo 			     vif->bss_conf.beacon_int);
171e705c121SKalle Valo 
172e705c121SKalle Valo 		if (!min_bi)
173e705c121SKalle Valo 			break;
174e705c121SKalle Valo 
175e705c121SKalle Valo 		if ((data->vif->bss_conf.beacon_int -
176e705c121SKalle Valo 		     vif->bss_conf.beacon_int) % min_bi == 0) {
177e705c121SKalle Valo 			data->preferred_tsf = mvmvif->tsf_id;
178e705c121SKalle Valo 			return;
179e705c121SKalle Valo 		}
180e705c121SKalle Valo 		break;
181e705c121SKalle Valo 	default:
182e705c121SKalle Valo 		/*
183e705c121SKalle Valo 		 * For all other interface types there's no need to
184e705c121SKalle Valo 		 * take drift into account. Either they're exclusive
185e705c121SKalle Valo 		 * like IBSS and monitor, or we don't care much about
186e705c121SKalle Valo 		 * their TSF (like P2P Device), but we won't be able
187e705c121SKalle Valo 		 * to share the TSF resource.
188e705c121SKalle Valo 		 */
189e705c121SKalle Valo 		break;
190e705c121SKalle Valo 	}
191e705c121SKalle Valo 
192e705c121SKalle Valo 	/*
193e705c121SKalle Valo 	 * Unless we exited above, we can't share the TSF resource
194e705c121SKalle Valo 	 * that the virtual interface we're iterating over is using
195e705c121SKalle Valo 	 * with the new one, so clear the available bit and if this
196e705c121SKalle Valo 	 * was the preferred one, reset that as well.
197e705c121SKalle Valo 	 */
198e705c121SKalle Valo 	__clear_bit(mvmvif->tsf_id, data->available_tsf_ids);
199e705c121SKalle Valo 
200e705c121SKalle Valo 	if (data->preferred_tsf == mvmvif->tsf_id)
201e705c121SKalle Valo 		data->preferred_tsf = NUM_TSF_IDS;
202e705c121SKalle Valo }
203e705c121SKalle Valo 
204e705c121SKalle Valo /*
205e705c121SKalle Valo  * Get the mask of the queues used by the vif
206e705c121SKalle Valo  */
207e705c121SKalle Valo u32 iwl_mvm_mac_get_queues_mask(struct ieee80211_vif *vif)
208e705c121SKalle Valo {
209e705c121SKalle Valo 	u32 qmask = 0, ac;
210e705c121SKalle Valo 
211e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
212e705c121SKalle Valo 		return BIT(IWL_MVM_OFFCHANNEL_QUEUE);
213e705c121SKalle Valo 
214e705c121SKalle Valo 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
215e705c121SKalle Valo 		if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
216e705c121SKalle Valo 			qmask |= BIT(vif->hw_queue[ac]);
217e705c121SKalle Valo 	}
218e705c121SKalle Valo 
219e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP)
220e705c121SKalle Valo 		qmask |= BIT(vif->cab_queue);
221e705c121SKalle Valo 
222e705c121SKalle Valo 	return qmask;
223e705c121SKalle Valo }
224e705c121SKalle Valo 
225e705c121SKalle Valo static void iwl_mvm_iface_hw_queues_iter(void *_data, u8 *mac,
226e705c121SKalle Valo 					 struct ieee80211_vif *vif)
227e705c121SKalle Valo {
228e705c121SKalle Valo 	struct iwl_mvm_hw_queues_iface_iterator_data *data = _data;
229e705c121SKalle Valo 
230e705c121SKalle Valo 	/* exclude the given vif */
231e705c121SKalle Valo 	if (vif == data->exclude_vif)
232e705c121SKalle Valo 		return;
233e705c121SKalle Valo 
234e705c121SKalle Valo 	data->used_hw_queues |= iwl_mvm_mac_get_queues_mask(vif);
235e705c121SKalle Valo }
236e705c121SKalle Valo 
237e705c121SKalle Valo static void iwl_mvm_mac_sta_hw_queues_iter(void *_data,
238e705c121SKalle Valo 					   struct ieee80211_sta *sta)
239e705c121SKalle Valo {
240e705c121SKalle Valo 	struct iwl_mvm_hw_queues_iface_iterator_data *data = _data;
241e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
242e705c121SKalle Valo 
243e705c121SKalle Valo 	/* Mark the queues used by the sta */
244e705c121SKalle Valo 	data->used_hw_queues |= mvmsta->tfd_queue_msk;
245e705c121SKalle Valo }
246e705c121SKalle Valo 
247e705c121SKalle Valo unsigned long iwl_mvm_get_used_hw_queues(struct iwl_mvm *mvm,
248e705c121SKalle Valo 					 struct ieee80211_vif *exclude_vif)
249e705c121SKalle Valo {
250e705c121SKalle Valo 	u8 sta_id;
251e705c121SKalle Valo 	struct iwl_mvm_hw_queues_iface_iterator_data data = {
252e705c121SKalle Valo 		.exclude_vif = exclude_vif,
253e705c121SKalle Valo 		.used_hw_queues =
254e705c121SKalle Valo 			BIT(IWL_MVM_OFFCHANNEL_QUEUE) |
255097129c9SLiad Kaufman 			BIT(mvm->aux_queue),
256e705c121SKalle Valo 	};
257e705c121SKalle Valo 
258097129c9SLiad Kaufman 	if (iwl_mvm_is_dqa_supported(mvm))
259097129c9SLiad Kaufman 		data.used_hw_queues |= BIT(IWL_MVM_DQA_CMD_QUEUE);
260097129c9SLiad Kaufman 	else
261097129c9SLiad Kaufman 		data.used_hw_queues |= BIT(IWL_MVM_CMD_QUEUE);
262097129c9SLiad Kaufman 
263e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
264e705c121SKalle Valo 
265e705c121SKalle Valo 	/* mark all VIF used hw queues */
266e705c121SKalle Valo 	ieee80211_iterate_active_interfaces_atomic(
267e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
268e705c121SKalle Valo 		iwl_mvm_iface_hw_queues_iter, &data);
269e705c121SKalle Valo 
270e705c121SKalle Valo 	/* don't assign the same hw queues as TDLS stations */
271e705c121SKalle Valo 	ieee80211_iterate_stations_atomic(mvm->hw,
272e705c121SKalle Valo 					  iwl_mvm_mac_sta_hw_queues_iter,
273e705c121SKalle Valo 					  &data);
274e705c121SKalle Valo 
275e705c121SKalle Valo 	/*
276e705c121SKalle Valo 	 * Some TDLS stations may be removed but are in the process of being
277e705c121SKalle Valo 	 * drained. Don't touch their queues.
278e705c121SKalle Valo 	 */
279e705c121SKalle Valo 	for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT)
280e705c121SKalle Valo 		data.used_hw_queues |= mvm->tfd_drained[sta_id];
281e705c121SKalle Valo 
282e705c121SKalle Valo 	return data.used_hw_queues;
283e705c121SKalle Valo }
284e705c121SKalle Valo 
285e705c121SKalle Valo static void iwl_mvm_mac_iface_iterator(void *_data, u8 *mac,
286e705c121SKalle Valo 				       struct ieee80211_vif *vif)
287e705c121SKalle Valo {
288e705c121SKalle Valo 	struct iwl_mvm_mac_iface_iterator_data *data = _data;
289e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
290e705c121SKalle Valo 
291e705c121SKalle Valo 	/* Iterator may already find the interface being added -- skip it */
292e705c121SKalle Valo 	if (vif == data->vif) {
293e705c121SKalle Valo 		data->found_vif = true;
294e705c121SKalle Valo 		return;
295e705c121SKalle Valo 	}
296e705c121SKalle Valo 
297e705c121SKalle Valo 	/* Mark MAC IDs as used by clearing the available bit, and
298e705c121SKalle Valo 	 * (below) mark TSFs as used if their existing use is not
299e705c121SKalle Valo 	 * compatible with the new interface type.
300e705c121SKalle Valo 	 * No locking or atomic bit operations are needed since the
301e705c121SKalle Valo 	 * data is on the stack of the caller function.
302e705c121SKalle Valo 	 */
303e705c121SKalle Valo 	__clear_bit(mvmvif->id, data->available_mac_ids);
304e705c121SKalle Valo 
305e705c121SKalle Valo 	/* find a suitable tsf_id */
306e705c121SKalle Valo 	iwl_mvm_mac_tsf_id_iter(_data, mac, vif);
307e705c121SKalle Valo }
308e705c121SKalle Valo 
309e705c121SKalle Valo void iwl_mvm_mac_ctxt_recalc_tsf_id(struct iwl_mvm *mvm,
310e705c121SKalle Valo 				    struct ieee80211_vif *vif)
311e705c121SKalle Valo {
312e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
313e705c121SKalle Valo 	struct iwl_mvm_mac_iface_iterator_data data = {
314e705c121SKalle Valo 		.mvm = mvm,
315e705c121SKalle Valo 		.vif = vif,
316e705c121SKalle Valo 		.available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
317e705c121SKalle Valo 		/* no preference yet */
318e705c121SKalle Valo 		.preferred_tsf = NUM_TSF_IDS,
319e705c121SKalle Valo 	};
320e705c121SKalle Valo 
321e705c121SKalle Valo 	ieee80211_iterate_active_interfaces_atomic(
322e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
323e705c121SKalle Valo 		iwl_mvm_mac_tsf_id_iter, &data);
324e705c121SKalle Valo 
325e705c121SKalle Valo 	if (data.preferred_tsf != NUM_TSF_IDS)
326e705c121SKalle Valo 		mvmvif->tsf_id = data.preferred_tsf;
327e705c121SKalle Valo 	else if (!test_bit(mvmvif->tsf_id, data.available_tsf_ids))
328e705c121SKalle Valo 		mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
329e705c121SKalle Valo 						NUM_TSF_IDS);
330e705c121SKalle Valo }
331e705c121SKalle Valo 
332e705c121SKalle Valo static int iwl_mvm_mac_ctxt_allocate_resources(struct iwl_mvm *mvm,
333e705c121SKalle Valo 					       struct ieee80211_vif *vif)
334e705c121SKalle Valo {
335e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
336e705c121SKalle Valo 	struct iwl_mvm_mac_iface_iterator_data data = {
337e705c121SKalle Valo 		.mvm = mvm,
338e705c121SKalle Valo 		.vif = vif,
339e705c121SKalle Valo 		.available_mac_ids = { (1 << NUM_MAC_INDEX_DRIVER) - 1 },
340e705c121SKalle Valo 		.available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
341e705c121SKalle Valo 		/* no preference yet */
342e705c121SKalle Valo 		.preferred_tsf = NUM_TSF_IDS,
343e705c121SKalle Valo 		.found_vif = false,
344e705c121SKalle Valo 	};
345e705c121SKalle Valo 	u32 ac;
346e705c121SKalle Valo 	int ret, i;
347e705c121SKalle Valo 	unsigned long used_hw_queues;
348e705c121SKalle Valo 
349e705c121SKalle Valo 	/*
350e705c121SKalle Valo 	 * Allocate a MAC ID and a TSF for this MAC, along with the queues
351e705c121SKalle Valo 	 * and other resources.
352e705c121SKalle Valo 	 */
353e705c121SKalle Valo 
354e705c121SKalle Valo 	/*
355e705c121SKalle Valo 	 * Before the iterator, we start with all MAC IDs and TSFs available.
356e705c121SKalle Valo 	 *
357e705c121SKalle Valo 	 * During iteration, all MAC IDs are cleared that are in use by other
358e705c121SKalle Valo 	 * virtual interfaces, and all TSF IDs are cleared that can't be used
359e705c121SKalle Valo 	 * by this new virtual interface because they're used by an interface
360e705c121SKalle Valo 	 * that can't share it with the new one.
361e705c121SKalle Valo 	 * At the same time, we check if there's a preferred TSF in the case
362e705c121SKalle Valo 	 * that we should share it with another interface.
363e705c121SKalle Valo 	 */
364e705c121SKalle Valo 
365e705c121SKalle Valo 	/* Currently, MAC ID 0 should be used only for the managed/IBSS vif */
366e705c121SKalle Valo 	switch (vif->type) {
367e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
368e705c121SKalle Valo 		break;
369e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
370e705c121SKalle Valo 		if (!vif->p2p)
371e705c121SKalle Valo 			break;
372e705c121SKalle Valo 		/* fall through */
373e705c121SKalle Valo 	default:
374e705c121SKalle Valo 		__clear_bit(0, data.available_mac_ids);
375e705c121SKalle Valo 	}
376e705c121SKalle Valo 
377e705c121SKalle Valo 	ieee80211_iterate_active_interfaces_atomic(
378e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
379e705c121SKalle Valo 		iwl_mvm_mac_iface_iterator, &data);
380e705c121SKalle Valo 
381e705c121SKalle Valo 	used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, vif);
382e705c121SKalle Valo 
383e705c121SKalle Valo 	/*
384e705c121SKalle Valo 	 * In the case we're getting here during resume, it's similar to
385e705c121SKalle Valo 	 * firmware restart, and with RESUME_ALL the iterator will find
386e705c121SKalle Valo 	 * the vif being added already.
387e705c121SKalle Valo 	 * We don't want to reassign any IDs in either case since doing
388e705c121SKalle Valo 	 * so would probably assign different IDs (as interfaces aren't
389e705c121SKalle Valo 	 * necessarily added in the same order), but the old IDs were
390e705c121SKalle Valo 	 * preserved anyway, so skip ID assignment for both resume and
391e705c121SKalle Valo 	 * recovery.
392e705c121SKalle Valo 	 */
393e705c121SKalle Valo 	if (data.found_vif)
394e705c121SKalle Valo 		return 0;
395e705c121SKalle Valo 
396e705c121SKalle Valo 	/* Therefore, in recovery, we can't get here */
397e705c121SKalle Valo 	if (WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
398e705c121SKalle Valo 		return -EBUSY;
399e705c121SKalle Valo 
400e705c121SKalle Valo 	mvmvif->id = find_first_bit(data.available_mac_ids,
401e705c121SKalle Valo 				    NUM_MAC_INDEX_DRIVER);
402e705c121SKalle Valo 	if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
403e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
404e705c121SKalle Valo 		ret = -EIO;
405e705c121SKalle Valo 		goto exit_fail;
406e705c121SKalle Valo 	}
407e705c121SKalle Valo 
408e705c121SKalle Valo 	if (data.preferred_tsf != NUM_TSF_IDS)
409e705c121SKalle Valo 		mvmvif->tsf_id = data.preferred_tsf;
410e705c121SKalle Valo 	else
411e705c121SKalle Valo 		mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
412e705c121SKalle Valo 						NUM_TSF_IDS);
413e705c121SKalle Valo 	if (mvmvif->tsf_id == NUM_TSF_IDS) {
414e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
415e705c121SKalle Valo 		ret = -EIO;
416e705c121SKalle Valo 		goto exit_fail;
417e705c121SKalle Valo 	}
418e705c121SKalle Valo 
419e705c121SKalle Valo 	mvmvif->color = 0;
420e705c121SKalle Valo 
421e705c121SKalle Valo 	INIT_LIST_HEAD(&mvmvif->time_event_data.list);
422e705c121SKalle Valo 	mvmvif->time_event_data.id = TE_MAX;
423e705c121SKalle Valo 
424e705c121SKalle Valo 	/* No need to allocate data queues to P2P Device MAC.*/
425e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
426e705c121SKalle Valo 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
427e705c121SKalle Valo 			vif->hw_queue[ac] = IEEE80211_INVAL_HW_QUEUE;
428e705c121SKalle Valo 
429e705c121SKalle Valo 		return 0;
430e705c121SKalle Valo 	}
431e705c121SKalle Valo 
43224afba76SLiad Kaufman 	/*
43324afba76SLiad Kaufman 	 * Find available queues, and allocate them to the ACs. When in
43424afba76SLiad Kaufman 	 * DQA-mode they aren't really used, and this is done only so the
43524afba76SLiad Kaufman 	 * mac80211 ieee80211_check_queues() function won't fail
43624afba76SLiad Kaufman 	 */
437e705c121SKalle Valo 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
438e705c121SKalle Valo 		u8 queue = find_first_zero_bit(&used_hw_queues,
439e705c121SKalle Valo 					       mvm->first_agg_queue);
440e705c121SKalle Valo 
44124afba76SLiad Kaufman 		if (!iwl_mvm_is_dqa_supported(mvm) &&
44224afba76SLiad Kaufman 		    queue >= mvm->first_agg_queue) {
443e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to allocate queue\n");
444e705c121SKalle Valo 			ret = -EIO;
445e705c121SKalle Valo 			goto exit_fail;
446e705c121SKalle Valo 		}
447e705c121SKalle Valo 
448e705c121SKalle Valo 		__set_bit(queue, &used_hw_queues);
449e705c121SKalle Valo 		vif->hw_queue[ac] = queue;
450e705c121SKalle Valo 	}
451e705c121SKalle Valo 
452e705c121SKalle Valo 	/* Allocate the CAB queue for softAP and GO interfaces */
453e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP) {
4540e0e4420SLiad Kaufman 		u8 queue;
4550e0e4420SLiad Kaufman 
4560e0e4420SLiad Kaufman 		if (!iwl_mvm_is_dqa_supported(mvm)) {
4570e0e4420SLiad Kaufman 			queue = find_first_zero_bit(&used_hw_queues,
458e705c121SKalle Valo 						    mvm->first_agg_queue);
459e705c121SKalle Valo 
460e705c121SKalle Valo 			if (queue >= mvm->first_agg_queue) {
461e705c121SKalle Valo 				IWL_ERR(mvm, "Failed to allocate cab queue\n");
462e705c121SKalle Valo 				ret = -EIO;
463e705c121SKalle Valo 				goto exit_fail;
464e705c121SKalle Valo 			}
4650e0e4420SLiad Kaufman 		} else {
4660e0e4420SLiad Kaufman 			queue = IWL_MVM_DQA_GCAST_QUEUE;
4670e0e4420SLiad Kaufman 		}
468e705c121SKalle Valo 
469e705c121SKalle Valo 		vif->cab_queue = queue;
470e705c121SKalle Valo 	} else {
471e705c121SKalle Valo 		vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
472e705c121SKalle Valo 	}
473e705c121SKalle Valo 
474e705c121SKalle Valo 	mvmvif->bcast_sta.sta_id = IWL_MVM_STATION_COUNT;
475e705c121SKalle Valo 	mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
476e705c121SKalle Valo 
477e705c121SKalle Valo 	for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
478e705c121SKalle Valo 		mvmvif->smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
479e705c121SKalle Valo 
480e705c121SKalle Valo 	return 0;
481e705c121SKalle Valo 
482e705c121SKalle Valo exit_fail:
483e705c121SKalle Valo 	memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
484e705c121SKalle Valo 	memset(vif->hw_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(vif->hw_queue));
485e705c121SKalle Valo 	vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
486e705c121SKalle Valo 	return ret;
487e705c121SKalle Valo }
488e705c121SKalle Valo 
489e705c121SKalle Valo int iwl_mvm_mac_ctxt_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
490e705c121SKalle Valo {
491e705c121SKalle Valo 	unsigned int wdg_timeout =
492e705c121SKalle Valo 		iwl_mvm_get_wd_timeout(mvm, vif, false, false);
493e705c121SKalle Valo 	u32 ac;
494e705c121SKalle Valo 	int ret;
495e705c121SKalle Valo 
496e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
497e705c121SKalle Valo 
498e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_allocate_resources(mvm, vif);
499e705c121SKalle Valo 	if (ret)
500e705c121SKalle Valo 		return ret;
501e705c121SKalle Valo 
502e705c121SKalle Valo 	switch (vif->type) {
503e705c121SKalle Valo 	case NL80211_IFTYPE_P2P_DEVICE:
504e705c121SKalle Valo 		iwl_mvm_enable_ac_txq(mvm, IWL_MVM_OFFCHANNEL_QUEUE,
505e705c121SKalle Valo 				      IWL_MVM_OFFCHANNEL_QUEUE,
506e705c121SKalle Valo 				      IWL_MVM_TX_FIFO_VO, 0, wdg_timeout);
507e705c121SKalle Valo 		break;
508e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
509e705c121SKalle Valo 		iwl_mvm_enable_ac_txq(mvm, vif->cab_queue, vif->cab_queue,
510e705c121SKalle Valo 				      IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
511e705c121SKalle Valo 		/* fall through */
512e705c121SKalle Valo 	default:
51324afba76SLiad Kaufman 		/* If DQA is supported - queues will be enabled when needed */
51424afba76SLiad Kaufman 		if (iwl_mvm_is_dqa_supported(mvm))
51524afba76SLiad Kaufman 			break;
51624afba76SLiad Kaufman 
517e705c121SKalle Valo 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
518e705c121SKalle Valo 			iwl_mvm_enable_ac_txq(mvm, vif->hw_queue[ac],
519e705c121SKalle Valo 					      vif->hw_queue[ac],
520e705c121SKalle Valo 					      iwl_mvm_ac_to_tx_fifo[ac], 0,
521e705c121SKalle Valo 					      wdg_timeout);
522e705c121SKalle Valo 		break;
523e705c121SKalle Valo 	}
524e705c121SKalle Valo 
525e705c121SKalle Valo 	return 0;
526e705c121SKalle Valo }
527e705c121SKalle Valo 
528e705c121SKalle Valo void iwl_mvm_mac_ctxt_release(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
529e705c121SKalle Valo {
530e705c121SKalle Valo 	int ac;
531e705c121SKalle Valo 
532e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
533e705c121SKalle Valo 
534e705c121SKalle Valo 	switch (vif->type) {
535e705c121SKalle Valo 	case NL80211_IFTYPE_P2P_DEVICE:
536e705c121SKalle Valo 		iwl_mvm_disable_txq(mvm, IWL_MVM_OFFCHANNEL_QUEUE,
537e705c121SKalle Valo 				    IWL_MVM_OFFCHANNEL_QUEUE, IWL_MAX_TID_COUNT,
538e705c121SKalle Valo 				    0);
539e705c121SKalle Valo 		break;
540e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
541e705c121SKalle Valo 		iwl_mvm_disable_txq(mvm, vif->cab_queue, vif->cab_queue,
542e705c121SKalle Valo 				    IWL_MAX_TID_COUNT, 0);
543e705c121SKalle Valo 		/* fall through */
544e705c121SKalle Valo 	default:
54524afba76SLiad Kaufman 		/*
54624afba76SLiad Kaufman 		 * If DQA is supported - queues were already disabled, since in
54724afba76SLiad Kaufman 		 * DQA-mode the queues are a property of the STA and not of the
54824afba76SLiad Kaufman 		 * vif, and at this point the STA was already deleted
54924afba76SLiad Kaufman 		 */
55024afba76SLiad Kaufman 		if (iwl_mvm_is_dqa_supported(mvm))
55124afba76SLiad Kaufman 			break;
55224afba76SLiad Kaufman 
553e705c121SKalle Valo 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
554e705c121SKalle Valo 			iwl_mvm_disable_txq(mvm, vif->hw_queue[ac],
555e705c121SKalle Valo 					    vif->hw_queue[ac],
556e705c121SKalle Valo 					    IWL_MAX_TID_COUNT, 0);
557e705c121SKalle Valo 	}
558e705c121SKalle Valo }
559e705c121SKalle Valo 
560e705c121SKalle Valo static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
561e705c121SKalle Valo 			      struct ieee80211_vif *vif,
56257fbcce3SJohannes Berg 			      enum nl80211_band band,
563e705c121SKalle Valo 			      u8 *cck_rates, u8 *ofdm_rates)
564e705c121SKalle Valo {
565e705c121SKalle Valo 	struct ieee80211_supported_band *sband;
566e705c121SKalle Valo 	unsigned long basic = vif->bss_conf.basic_rates;
567e705c121SKalle Valo 	int lowest_present_ofdm = 100;
568e705c121SKalle Valo 	int lowest_present_cck = 100;
569e705c121SKalle Valo 	u8 cck = 0;
570e705c121SKalle Valo 	u8 ofdm = 0;
571e705c121SKalle Valo 	int i;
572e705c121SKalle Valo 
573e705c121SKalle Valo 	sband = mvm->hw->wiphy->bands[band];
574e705c121SKalle Valo 
575e705c121SKalle Valo 	for_each_set_bit(i, &basic, BITS_PER_LONG) {
576e705c121SKalle Valo 		int hw = sband->bitrates[i].hw_value;
577e705c121SKalle Valo 		if (hw >= IWL_FIRST_OFDM_RATE) {
578e705c121SKalle Valo 			ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
579e705c121SKalle Valo 			if (lowest_present_ofdm > hw)
580e705c121SKalle Valo 				lowest_present_ofdm = hw;
581e705c121SKalle Valo 		} else {
582e705c121SKalle Valo 			BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
583e705c121SKalle Valo 
584e705c121SKalle Valo 			cck |= BIT(hw);
585e705c121SKalle Valo 			if (lowest_present_cck > hw)
586e705c121SKalle Valo 				lowest_present_cck = hw;
587e705c121SKalle Valo 		}
588e705c121SKalle Valo 	}
589e705c121SKalle Valo 
590e705c121SKalle Valo 	/*
591e705c121SKalle Valo 	 * Now we've got the basic rates as bitmaps in the ofdm and cck
592e705c121SKalle Valo 	 * variables. This isn't sufficient though, as there might not
593e705c121SKalle Valo 	 * be all the right rates in the bitmap. E.g. if the only basic
594e705c121SKalle Valo 	 * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
595e705c121SKalle Valo 	 * and 6 Mbps because the 802.11-2007 standard says in 9.6:
596e705c121SKalle Valo 	 *
597e705c121SKalle Valo 	 *    [...] a STA responding to a received frame shall transmit
598e705c121SKalle Valo 	 *    its Control Response frame [...] at the highest rate in the
599e705c121SKalle Valo 	 *    BSSBasicRateSet parameter that is less than or equal to the
600e705c121SKalle Valo 	 *    rate of the immediately previous frame in the frame exchange
601e705c121SKalle Valo 	 *    sequence ([...]) and that is of the same modulation class
602e705c121SKalle Valo 	 *    ([...]) as the received frame. If no rate contained in the
603e705c121SKalle Valo 	 *    BSSBasicRateSet parameter meets these conditions, then the
604e705c121SKalle Valo 	 *    control frame sent in response to a received frame shall be
605e705c121SKalle Valo 	 *    transmitted at the highest mandatory rate of the PHY that is
606e705c121SKalle Valo 	 *    less than or equal to the rate of the received frame, and
607e705c121SKalle Valo 	 *    that is of the same modulation class as the received frame.
608e705c121SKalle Valo 	 *
609e705c121SKalle Valo 	 * As a consequence, we need to add all mandatory rates that are
610e705c121SKalle Valo 	 * lower than all of the basic rates to these bitmaps.
611e705c121SKalle Valo 	 */
612e705c121SKalle Valo 
613e705c121SKalle Valo 	if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
614e705c121SKalle Valo 		ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
615e705c121SKalle Valo 	if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
616e705c121SKalle Valo 		ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
617e705c121SKalle Valo 	/* 6M already there or needed so always add */
618e705c121SKalle Valo 	ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
619e705c121SKalle Valo 
620e705c121SKalle Valo 	/*
621e705c121SKalle Valo 	 * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
622e705c121SKalle Valo 	 * Note, however:
623e705c121SKalle Valo 	 *  - if no CCK rates are basic, it must be ERP since there must
624e705c121SKalle Valo 	 *    be some basic rates at all, so they're OFDM => ERP PHY
625e705c121SKalle Valo 	 *    (or we're in 5 GHz, and the cck bitmap will never be used)
626e705c121SKalle Valo 	 *  - if 11M is a basic rate, it must be ERP as well, so add 5.5M
627e705c121SKalle Valo 	 *  - if 5.5M is basic, 1M and 2M are mandatory
628e705c121SKalle Valo 	 *  - if 2M is basic, 1M is mandatory
629e705c121SKalle Valo 	 *  - if 1M is basic, that's the only valid ACK rate.
630e705c121SKalle Valo 	 * As a consequence, it's not as complicated as it sounds, just add
631e705c121SKalle Valo 	 * any lower rates to the ACK rate bitmap.
632e705c121SKalle Valo 	 */
633e705c121SKalle Valo 	if (IWL_RATE_11M_INDEX < lowest_present_cck)
634e705c121SKalle Valo 		cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
635e705c121SKalle Valo 	if (IWL_RATE_5M_INDEX < lowest_present_cck)
636e705c121SKalle Valo 		cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
637e705c121SKalle Valo 	if (IWL_RATE_2M_INDEX < lowest_present_cck)
638e705c121SKalle Valo 		cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
639e705c121SKalle Valo 	/* 1M already there or needed so always add */
640e705c121SKalle Valo 	cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
641e705c121SKalle Valo 
642e705c121SKalle Valo 	*cck_rates = cck;
643e705c121SKalle Valo 	*ofdm_rates = ofdm;
644e705c121SKalle Valo }
645e705c121SKalle Valo 
646e705c121SKalle Valo static void iwl_mvm_mac_ctxt_set_ht_flags(struct iwl_mvm *mvm,
647e705c121SKalle Valo 					 struct ieee80211_vif *vif,
648e705c121SKalle Valo 					 struct iwl_mac_ctx_cmd *cmd)
649e705c121SKalle Valo {
650e705c121SKalle Valo 	/* for both sta and ap, ht_operation_mode hold the protection_mode */
651e705c121SKalle Valo 	u8 protection_mode = vif->bss_conf.ht_operation_mode &
652e705c121SKalle Valo 				 IEEE80211_HT_OP_MODE_PROTECTION;
653e705c121SKalle Valo 	/* The fw does not distinguish between ht and fat */
654e705c121SKalle Valo 	u32 ht_flag = MAC_PROT_FLG_HT_PROT | MAC_PROT_FLG_FAT_PROT;
655e705c121SKalle Valo 
656e705c121SKalle Valo 	IWL_DEBUG_RATE(mvm, "protection mode set to %d\n", protection_mode);
657e705c121SKalle Valo 	/*
658e705c121SKalle Valo 	 * See section 9.23.3.1 of IEEE 80211-2012.
659e705c121SKalle Valo 	 * Nongreenfield HT STAs Present is not supported.
660e705c121SKalle Valo 	 */
661e705c121SKalle Valo 	switch (protection_mode) {
662e705c121SKalle Valo 	case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
663e705c121SKalle Valo 		break;
664e705c121SKalle Valo 	case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
665e705c121SKalle Valo 	case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
666e705c121SKalle Valo 		cmd->protection_flags |= cpu_to_le32(ht_flag);
667e705c121SKalle Valo 		break;
668e705c121SKalle Valo 	case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
669e705c121SKalle Valo 		/* Protect when channel wider than 20MHz */
670e705c121SKalle Valo 		if (vif->bss_conf.chandef.width > NL80211_CHAN_WIDTH_20)
671e705c121SKalle Valo 			cmd->protection_flags |= cpu_to_le32(ht_flag);
672e705c121SKalle Valo 		break;
673e705c121SKalle Valo 	default:
674e705c121SKalle Valo 		IWL_ERR(mvm, "Illegal protection mode %d\n",
675e705c121SKalle Valo 			protection_mode);
676e705c121SKalle Valo 		break;
677e705c121SKalle Valo 	}
678e705c121SKalle Valo }
679e705c121SKalle Valo 
680e705c121SKalle Valo static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
681e705c121SKalle Valo 					struct ieee80211_vif *vif,
682e705c121SKalle Valo 					struct iwl_mac_ctx_cmd *cmd,
683e705c121SKalle Valo 					const u8 *bssid_override,
684e705c121SKalle Valo 					u32 action)
685e705c121SKalle Valo {
686e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
687e705c121SKalle Valo 	struct ieee80211_chanctx_conf *chanctx;
688e705c121SKalle Valo 	bool ht_enabled = !!(vif->bss_conf.ht_operation_mode &
689e705c121SKalle Valo 			     IEEE80211_HT_OP_MODE_PROTECTION);
690e705c121SKalle Valo 	u8 cck_ack_rates, ofdm_ack_rates;
691e705c121SKalle Valo 	const u8 *bssid = bssid_override ?: vif->bss_conf.bssid;
692e705c121SKalle Valo 	int i;
693e705c121SKalle Valo 
694e705c121SKalle Valo 	cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
695e705c121SKalle Valo 							    mvmvif->color));
696e705c121SKalle Valo 	cmd->action = cpu_to_le32(action);
697e705c121SKalle Valo 
698e705c121SKalle Valo 	switch (vif->type) {
699e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
700e705c121SKalle Valo 		if (vif->p2p)
701e705c121SKalle Valo 			cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_STA);
702e705c121SKalle Valo 		else
703e705c121SKalle Valo 			cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_BSS_STA);
704e705c121SKalle Valo 		break;
705e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
706e705c121SKalle Valo 		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_GO);
707e705c121SKalle Valo 		break;
708e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
709e705c121SKalle Valo 		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_LISTENER);
710e705c121SKalle Valo 		break;
711e705c121SKalle Valo 	case NL80211_IFTYPE_P2P_DEVICE:
712e705c121SKalle Valo 		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_DEVICE);
713e705c121SKalle Valo 		break;
714e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
715e705c121SKalle Valo 		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_IBSS);
716e705c121SKalle Valo 		break;
717e705c121SKalle Valo 	default:
718e705c121SKalle Valo 		WARN_ON_ONCE(1);
719e705c121SKalle Valo 	}
720e705c121SKalle Valo 
721e705c121SKalle Valo 	cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
722e705c121SKalle Valo 
723e705c121SKalle Valo 	memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
724e705c121SKalle Valo 
725e705c121SKalle Valo 	if (bssid)
726e705c121SKalle Valo 		memcpy(cmd->bssid_addr, bssid, ETH_ALEN);
727e705c121SKalle Valo 	else
728e705c121SKalle Valo 		eth_broadcast_addr(cmd->bssid_addr);
729e705c121SKalle Valo 
730e705c121SKalle Valo 	rcu_read_lock();
731e705c121SKalle Valo 	chanctx = rcu_dereference(vif->chanctx_conf);
732e705c121SKalle Valo 	iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
73357fbcce3SJohannes Berg 					    : NL80211_BAND_2GHZ,
734e705c121SKalle Valo 			  &cck_ack_rates, &ofdm_ack_rates);
735e705c121SKalle Valo 	rcu_read_unlock();
736e705c121SKalle Valo 
737e705c121SKalle Valo 	cmd->cck_rates = cpu_to_le32((u32)cck_ack_rates);
738e705c121SKalle Valo 	cmd->ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
739e705c121SKalle Valo 
740e705c121SKalle Valo 	cmd->cck_short_preamble =
741e705c121SKalle Valo 		cpu_to_le32(vif->bss_conf.use_short_preamble ?
742e705c121SKalle Valo 			    MAC_FLG_SHORT_PREAMBLE : 0);
743e705c121SKalle Valo 	cmd->short_slot =
744e705c121SKalle Valo 		cpu_to_le32(vif->bss_conf.use_short_slot ?
745e705c121SKalle Valo 			    MAC_FLG_SHORT_SLOT : 0);
746e705c121SKalle Valo 
747bd6f5bd7SAyala Beker 	cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
748bd6f5bd7SAyala Beker 
749e705c121SKalle Valo 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
750e705c121SKalle Valo 		u8 txf = iwl_mvm_ac_to_tx_fifo[i];
751e705c121SKalle Valo 
752e705c121SKalle Valo 		cmd->ac[txf].cw_min =
753e705c121SKalle Valo 			cpu_to_le16(mvmvif->queue_params[i].cw_min);
754e705c121SKalle Valo 		cmd->ac[txf].cw_max =
755e705c121SKalle Valo 			cpu_to_le16(mvmvif->queue_params[i].cw_max);
756e705c121SKalle Valo 		cmd->ac[txf].edca_txop =
757e705c121SKalle Valo 			cpu_to_le16(mvmvif->queue_params[i].txop * 32);
758e705c121SKalle Valo 		cmd->ac[txf].aifsn = mvmvif->queue_params[i].aifs;
759e705c121SKalle Valo 		cmd->ac[txf].fifos_mask = BIT(txf);
760e705c121SKalle Valo 	}
761e705c121SKalle Valo 
762bd6f5bd7SAyala Beker 	if (vif->type == NL80211_IFTYPE_AP) {
763e705c121SKalle Valo 		/* in AP mode, the MCAST FIFO takes the EDCA params from VO */
764e705c121SKalle Valo 		cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |=
765e705c121SKalle Valo 			BIT(IWL_MVM_TX_FIFO_MCAST);
766e705c121SKalle Valo 
767bd6f5bd7SAyala Beker 		/*
768bd6f5bd7SAyala Beker 		 * in AP mode, pass probe requests and beacons from other APs
769bd6f5bd7SAyala Beker 		 * (needed for ht protection); when there're no any associated
770bd6f5bd7SAyala Beker 		 * station don't ask FW to pass beacons to prevent unnecessary
771bd6f5bd7SAyala Beker 		 * wake-ups.
772bd6f5bd7SAyala Beker 		 */
773bd6f5bd7SAyala Beker 		cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
774c89e333dSAndrei Otcheretianski 		if (mvmvif->ap_assoc_sta_count || !mvm->drop_bcn_ap_mode) {
775bd6f5bd7SAyala Beker 			cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
776bd6f5bd7SAyala Beker 			IWL_DEBUG_HC(mvm, "Asking FW to pass beacons\n");
777bd6f5bd7SAyala Beker 		} else {
778bd6f5bd7SAyala Beker 			IWL_DEBUG_HC(mvm, "No need to receive beacons\n");
779bd6f5bd7SAyala Beker 		}
780bd6f5bd7SAyala Beker 	}
781bd6f5bd7SAyala Beker 
782e705c121SKalle Valo 	if (vif->bss_conf.qos)
783e705c121SKalle Valo 		cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
784e705c121SKalle Valo 
785e705c121SKalle Valo 	if (vif->bss_conf.use_cts_prot)
786e705c121SKalle Valo 		cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
787e705c121SKalle Valo 
788e705c121SKalle Valo 	IWL_DEBUG_RATE(mvm, "use_cts_prot %d, ht_operation_mode %d\n",
789e705c121SKalle Valo 		       vif->bss_conf.use_cts_prot,
790e705c121SKalle Valo 		       vif->bss_conf.ht_operation_mode);
791e705c121SKalle Valo 	if (vif->bss_conf.chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
792e705c121SKalle Valo 		cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
793e705c121SKalle Valo 	if (ht_enabled)
794e705c121SKalle Valo 		iwl_mvm_mac_ctxt_set_ht_flags(mvm, vif, cmd);
795e705c121SKalle Valo }
796e705c121SKalle Valo 
797e705c121SKalle Valo static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
798e705c121SKalle Valo 				     struct iwl_mac_ctx_cmd *cmd)
799e705c121SKalle Valo {
800e705c121SKalle Valo 	int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
801e705c121SKalle Valo 				       sizeof(*cmd), cmd);
802e705c121SKalle Valo 	if (ret)
803e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send MAC context (action:%d): %d\n",
804e705c121SKalle Valo 			le32_to_cpu(cmd->action), ret);
805e705c121SKalle Valo 	return ret;
806e705c121SKalle Valo }
807e705c121SKalle Valo 
808e705c121SKalle Valo static int iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm *mvm,
809e705c121SKalle Valo 				    struct ieee80211_vif *vif,
810e705c121SKalle Valo 				    u32 action, bool force_assoc_off,
811e705c121SKalle Valo 				    const u8 *bssid_override)
812e705c121SKalle Valo {
813e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd = {};
814e705c121SKalle Valo 	struct iwl_mac_data_sta *ctxt_sta;
815e705c121SKalle Valo 
816e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_STATION);
817e705c121SKalle Valo 
818e705c121SKalle Valo 	/* Fill the common data for all mac context types */
819e705c121SKalle Valo 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, bssid_override, action);
820e705c121SKalle Valo 
821e705c121SKalle Valo 	if (vif->p2p) {
822e705c121SKalle Valo 		struct ieee80211_p2p_noa_attr *noa =
823e705c121SKalle Valo 			&vif->bss_conf.p2p_noa_attr;
824e705c121SKalle Valo 
825e705c121SKalle Valo 		cmd.p2p_sta.ctwin = cpu_to_le32(noa->oppps_ctwindow &
826e705c121SKalle Valo 					IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
827e705c121SKalle Valo 		ctxt_sta = &cmd.p2p_sta.sta;
828e705c121SKalle Valo 	} else {
829e705c121SKalle Valo 		ctxt_sta = &cmd.sta;
830e705c121SKalle Valo 	}
831e705c121SKalle Valo 
832e705c121SKalle Valo 	/* We need the dtim_period to set the MAC as associated */
833e705c121SKalle Valo 	if (vif->bss_conf.assoc && vif->bss_conf.dtim_period &&
834e705c121SKalle Valo 	    !force_assoc_off) {
835e705c121SKalle Valo 		u32 dtim_offs;
836e705c121SKalle Valo 
837e705c121SKalle Valo 		/*
838e705c121SKalle Valo 		 * The DTIM count counts down, so when it is N that means N
839e705c121SKalle Valo 		 * more beacon intervals happen until the DTIM TBTT. Therefore
840e705c121SKalle Valo 		 * add this to the current time. If that ends up being in the
841e705c121SKalle Valo 		 * future, the firmware will handle it.
842e705c121SKalle Valo 		 *
843e705c121SKalle Valo 		 * Also note that the system_timestamp (which we get here as
844e705c121SKalle Valo 		 * "sync_device_ts") and TSF timestamp aren't at exactly the
845e705c121SKalle Valo 		 * same offset in the frame -- the TSF is at the first symbol
846e705c121SKalle Valo 		 * of the TSF, the system timestamp is at signal acquisition
847e705c121SKalle Valo 		 * time. This means there's an offset between them of at most
848e705c121SKalle Valo 		 * a few hundred microseconds (24 * 8 bits + PLCP time gives
849e705c121SKalle Valo 		 * 384us in the longest case), this is currently not relevant
850e705c121SKalle Valo 		 * as the firmware wakes up around 2ms before the TBTT.
851e705c121SKalle Valo 		 */
852e705c121SKalle Valo 		dtim_offs = vif->bss_conf.sync_dtim_count *
853e705c121SKalle Valo 				vif->bss_conf.beacon_int;
854e705c121SKalle Valo 		/* convert TU to usecs */
855e705c121SKalle Valo 		dtim_offs *= 1024;
856e705c121SKalle Valo 
857e705c121SKalle Valo 		ctxt_sta->dtim_tsf =
858e705c121SKalle Valo 			cpu_to_le64(vif->bss_conf.sync_tsf + dtim_offs);
859e705c121SKalle Valo 		ctxt_sta->dtim_time =
860e705c121SKalle Valo 			cpu_to_le32(vif->bss_conf.sync_device_ts + dtim_offs);
861e705c121SKalle Valo 
862e705c121SKalle Valo 		IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
863e705c121SKalle Valo 			       le64_to_cpu(ctxt_sta->dtim_tsf),
864e705c121SKalle Valo 			       le32_to_cpu(ctxt_sta->dtim_time),
865e705c121SKalle Valo 			       dtim_offs);
866e705c121SKalle Valo 
867e705c121SKalle Valo 		ctxt_sta->is_assoc = cpu_to_le32(1);
868e705c121SKalle Valo 	} else {
869e705c121SKalle Valo 		ctxt_sta->is_assoc = cpu_to_le32(0);
870e705c121SKalle Valo 
871e705c121SKalle Valo 		/* Allow beacons to pass through as long as we are not
872e705c121SKalle Valo 		 * associated, or we do not have dtim period information.
873e705c121SKalle Valo 		 */
874e705c121SKalle Valo 		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
875e705c121SKalle Valo 	}
876e705c121SKalle Valo 
877e705c121SKalle Valo 	ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
878e705c121SKalle Valo 	ctxt_sta->bi_reciprocal =
879e705c121SKalle Valo 		cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
880e705c121SKalle Valo 	ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
881e705c121SKalle Valo 					      vif->bss_conf.dtim_period);
882e705c121SKalle Valo 	ctxt_sta->dtim_reciprocal =
883e705c121SKalle Valo 		cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
884e705c121SKalle Valo 					       vif->bss_conf.dtim_period));
885e705c121SKalle Valo 
886e705c121SKalle Valo 	ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
887e705c121SKalle Valo 	ctxt_sta->assoc_id = cpu_to_le32(vif->bss_conf.aid);
888e705c121SKalle Valo 
889e705c121SKalle Valo 	if (vif->probe_req_reg && vif->bss_conf.assoc && vif->p2p)
890e705c121SKalle Valo 		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
891e705c121SKalle Valo 
892e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
893e705c121SKalle Valo }
894e705c121SKalle Valo 
895e705c121SKalle Valo static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
896e705c121SKalle Valo 					 struct ieee80211_vif *vif,
897e705c121SKalle Valo 					 u32 action)
898e705c121SKalle Valo {
899e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd = {};
9000e39eb03SChaya Rachel Ivgi 	u32 tfd_queue_msk = 0;
9010e39eb03SChaya Rachel Ivgi 	int ret, i;
902e705c121SKalle Valo 
903e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
904e705c121SKalle Valo 
905e705c121SKalle Valo 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
906e705c121SKalle Valo 
9070e39eb03SChaya Rachel Ivgi 	for (i = 0; i < IEEE80211_NUM_ACS; i++)
9080e39eb03SChaya Rachel Ivgi 		if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
9090e39eb03SChaya Rachel Ivgi 			tfd_queue_msk |= BIT(vif->hw_queue[i]);
9100e39eb03SChaya Rachel Ivgi 
911e705c121SKalle Valo 	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
912e705c121SKalle Valo 				       MAC_FILTER_IN_CONTROL_AND_MGMT |
913e705c121SKalle Valo 				       MAC_FILTER_IN_BEACON |
914e705c121SKalle Valo 				       MAC_FILTER_IN_PROBE_REQUEST |
915e705c121SKalle Valo 				       MAC_FILTER_IN_CRC32);
916e705c121SKalle Valo 	ieee80211_hw_set(mvm->hw, RX_INCLUDES_FCS);
917e705c121SKalle Valo 
9180e39eb03SChaya Rachel Ivgi 	/* Allocate sniffer station */
9190e39eb03SChaya Rachel Ivgi 	ret = iwl_mvm_allocate_int_sta(mvm, &mvm->snif_sta, tfd_queue_msk,
9200e39eb03SChaya Rachel Ivgi 				       vif->type);
9210e39eb03SChaya Rachel Ivgi 	if (ret)
9220e39eb03SChaya Rachel Ivgi 		return ret;
9230e39eb03SChaya Rachel Ivgi 
924e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
925e705c121SKalle Valo }
926e705c121SKalle Valo 
927e705c121SKalle Valo static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
928e705c121SKalle Valo 				     struct ieee80211_vif *vif,
929e705c121SKalle Valo 				     u32 action)
930e705c121SKalle Valo {
931e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
932e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd = {};
933e705c121SKalle Valo 
934e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
935e705c121SKalle Valo 
936e705c121SKalle Valo 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
937e705c121SKalle Valo 
938e705c121SKalle Valo 	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
939e705c121SKalle Valo 				       MAC_FILTER_IN_PROBE_REQUEST);
940e705c121SKalle Valo 
941e705c121SKalle Valo 	/* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */
942e705c121SKalle Valo 	cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
943e705c121SKalle Valo 	cmd.ibss.bi_reciprocal =
944e705c121SKalle Valo 		cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
945e705c121SKalle Valo 
946e705c121SKalle Valo 	/* TODO: Assumes that the beacon id == mac context id */
947e705c121SKalle Valo 	cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
948e705c121SKalle Valo 
949e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
950e705c121SKalle Valo }
951e705c121SKalle Valo 
952e705c121SKalle Valo struct iwl_mvm_go_iterator_data {
953e705c121SKalle Valo 	bool go_active;
954e705c121SKalle Valo };
955e705c121SKalle Valo 
956e705c121SKalle Valo static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
957e705c121SKalle Valo {
958e705c121SKalle Valo 	struct iwl_mvm_go_iterator_data *data = _data;
959e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
960e705c121SKalle Valo 
961e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
962e705c121SKalle Valo 	    mvmvif->ap_ibss_active)
963e705c121SKalle Valo 		data->go_active = true;
964e705c121SKalle Valo }
965e705c121SKalle Valo 
966e705c121SKalle Valo static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
967e705c121SKalle Valo 					   struct ieee80211_vif *vif,
968e705c121SKalle Valo 					   u32 action)
969e705c121SKalle Valo {
970e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd = {};
971e705c121SKalle Valo 	struct iwl_mvm_go_iterator_data data = {};
972e705c121SKalle Valo 
973e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
974e705c121SKalle Valo 
975e705c121SKalle Valo 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
976e705c121SKalle Valo 
977e705c121SKalle Valo 	cmd.protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
978e705c121SKalle Valo 
979e705c121SKalle Valo 	/* Override the filter flags to accept only probe requests */
980e705c121SKalle Valo 	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
981e705c121SKalle Valo 
982e705c121SKalle Valo 	/*
983e705c121SKalle Valo 	 * This flag should be set to true when the P2P Device is
984e705c121SKalle Valo 	 * discoverable and there is at least another active P2P GO. Settings
985e705c121SKalle Valo 	 * this flag will allow the P2P Device to be discoverable on other
986e705c121SKalle Valo 	 * channels in addition to its listen channel.
987e705c121SKalle Valo 	 * Note that this flag should not be set in other cases as it opens the
988e705c121SKalle Valo 	 * Rx filters on all MAC and increases the number of interrupts.
989e705c121SKalle Valo 	 */
990e705c121SKalle Valo 	ieee80211_iterate_active_interfaces_atomic(
991e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
992e705c121SKalle Valo 		iwl_mvm_go_iterator, &data);
993e705c121SKalle Valo 
994e705c121SKalle Valo 	cmd.p2p_dev.is_disc_extended = cpu_to_le32(data.go_active ? 1 : 0);
995e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
996e705c121SKalle Valo }
997e705c121SKalle Valo 
998e705c121SKalle Valo static void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
999e705c121SKalle Valo 				     struct iwl_mac_beacon_cmd *beacon_cmd,
1000e705c121SKalle Valo 				     u8 *beacon, u32 frame_size)
1001e705c121SKalle Valo {
1002e705c121SKalle Valo 	u32 tim_idx;
1003e705c121SKalle Valo 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
1004e705c121SKalle Valo 
1005e705c121SKalle Valo 	/* The index is relative to frame start but we start looking at the
1006e705c121SKalle Valo 	 * variable-length part of the beacon. */
1007e705c121SKalle Valo 	tim_idx = mgmt->u.beacon.variable - beacon;
1008e705c121SKalle Valo 
1009e705c121SKalle Valo 	/* Parse variable-length elements of beacon to find WLAN_EID_TIM */
1010e705c121SKalle Valo 	while ((tim_idx < (frame_size - 2)) &&
1011e705c121SKalle Valo 			(beacon[tim_idx] != WLAN_EID_TIM))
1012e705c121SKalle Valo 		tim_idx += beacon[tim_idx+1] + 2;
1013e705c121SKalle Valo 
1014e705c121SKalle Valo 	/* If TIM field was found, set variables */
1015e705c121SKalle Valo 	if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
1016e705c121SKalle Valo 		beacon_cmd->tim_idx = cpu_to_le32(tim_idx);
1017e705c121SKalle Valo 		beacon_cmd->tim_size = cpu_to_le32((u32)beacon[tim_idx+1]);
1018e705c121SKalle Valo 	} else {
1019e705c121SKalle Valo 		IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
1020e705c121SKalle Valo 	}
1021e705c121SKalle Valo }
1022e705c121SKalle Valo 
1023e705c121SKalle Valo static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
1024e705c121SKalle Valo 					struct ieee80211_vif *vif,
1025e705c121SKalle Valo 					struct sk_buff *beacon)
1026e705c121SKalle Valo {
1027e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1028e705c121SKalle Valo 	struct iwl_host_cmd cmd = {
1029e705c121SKalle Valo 		.id = BEACON_TEMPLATE_CMD,
1030e705c121SKalle Valo 		.flags = CMD_ASYNC,
1031e705c121SKalle Valo 	};
1032e705c121SKalle Valo 	struct iwl_mac_beacon_cmd beacon_cmd = {};
1033e705c121SKalle Valo 	struct ieee80211_tx_info *info;
1034e705c121SKalle Valo 	u32 beacon_skb_len;
1035e705c121SKalle Valo 	u32 rate, tx_flags;
1036e705c121SKalle Valo 
1037e705c121SKalle Valo 	if (WARN_ON(!beacon))
1038e705c121SKalle Valo 		return -EINVAL;
1039e705c121SKalle Valo 
1040e705c121SKalle Valo 	beacon_skb_len = beacon->len;
1041e705c121SKalle Valo 
1042e705c121SKalle Valo 	/* TODO: for now the beacon template id is set to be the mac context id.
1043e705c121SKalle Valo 	 * Might be better to handle it as another resource ... */
1044e705c121SKalle Valo 	beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
1045e705c121SKalle Valo 	info = IEEE80211_SKB_CB(beacon);
1046e705c121SKalle Valo 
1047e705c121SKalle Valo 	/* Set up TX command fields */
1048e705c121SKalle Valo 	beacon_cmd.tx.len = cpu_to_le16((u16)beacon_skb_len);
1049e705c121SKalle Valo 	beacon_cmd.tx.sta_id = mvmvif->bcast_sta.sta_id;
1050e705c121SKalle Valo 	beacon_cmd.tx.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
1051e705c121SKalle Valo 	tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;
1052e705c121SKalle Valo 	tx_flags |=
1053e705c121SKalle Valo 		iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<
1054e705c121SKalle Valo 						TX_CMD_FLG_BT_PRIO_POS;
1055e705c121SKalle Valo 	beacon_cmd.tx.tx_flags = cpu_to_le32(tx_flags);
1056e705c121SKalle Valo 
10571e3c3c35SEmmanuel Grumbach 	if (!fw_has_capa(&mvm->fw->ucode_capa,
10581e3c3c35SEmmanuel Grumbach 			 IWL_UCODE_TLV_CAPA_BEACON_ANT_SELECTION)) {
1059e705c121SKalle Valo 		mvm->mgmt_last_antenna_idx =
1060e705c121SKalle Valo 			iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
1061e705c121SKalle Valo 					     mvm->mgmt_last_antenna_idx);
10621e3c3c35SEmmanuel Grumbach 	}
1063e705c121SKalle Valo 
1064e705c121SKalle Valo 	beacon_cmd.tx.rate_n_flags =
1065e705c121SKalle Valo 		cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
1066e705c121SKalle Valo 			    RATE_MCS_ANT_POS);
1067e705c121SKalle Valo 
106857fbcce3SJohannes Berg 	if (info->band == NL80211_BAND_5GHZ || vif->p2p) {
1069e705c121SKalle Valo 		rate = IWL_FIRST_OFDM_RATE;
1070e705c121SKalle Valo 	} else {
1071e705c121SKalle Valo 		rate = IWL_FIRST_CCK_RATE;
1072e705c121SKalle Valo 		beacon_cmd.tx.rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK);
1073e705c121SKalle Valo 	}
1074e705c121SKalle Valo 	beacon_cmd.tx.rate_n_flags |=
1075e705c121SKalle Valo 		cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(rate));
1076e705c121SKalle Valo 
1077e705c121SKalle Valo 	/* Set up TX beacon command fields */
1078e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP)
1079e705c121SKalle Valo 		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd,
1080e705c121SKalle Valo 					 beacon->data,
1081e705c121SKalle Valo 					 beacon_skb_len);
1082e705c121SKalle Valo 
1083e705c121SKalle Valo 	/* Submit command */
1084e705c121SKalle Valo 	cmd.len[0] = sizeof(beacon_cmd);
1085e705c121SKalle Valo 	cmd.data[0] = &beacon_cmd;
1086e705c121SKalle Valo 	cmd.dataflags[0] = 0;
1087e705c121SKalle Valo 	cmd.len[1] = beacon_skb_len;
1088e705c121SKalle Valo 	cmd.data[1] = beacon->data;
1089e705c121SKalle Valo 	cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
1090e705c121SKalle Valo 
1091e705c121SKalle Valo 	return iwl_mvm_send_cmd(mvm, &cmd);
1092e705c121SKalle Valo }
1093e705c121SKalle Valo 
1094e705c121SKalle Valo /* The beacon template for the AP/GO/IBSS has changed and needs update */
1095e705c121SKalle Valo int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
1096e705c121SKalle Valo 				    struct ieee80211_vif *vif)
1097e705c121SKalle Valo {
1098e705c121SKalle Valo 	struct sk_buff *beacon;
1099e705c121SKalle Valo 	int ret;
1100e705c121SKalle Valo 
1101e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_AP &&
1102e705c121SKalle Valo 		vif->type != NL80211_IFTYPE_ADHOC);
1103e705c121SKalle Valo 
1104e705c121SKalle Valo 	beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL);
1105e705c121SKalle Valo 	if (!beacon)
1106e705c121SKalle Valo 		return -ENOMEM;
1107e705c121SKalle Valo 
1108e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
1109e705c121SKalle Valo 	dev_kfree_skb(beacon);
1110e705c121SKalle Valo 	return ret;
1111e705c121SKalle Valo }
1112e705c121SKalle Valo 
1113e705c121SKalle Valo struct iwl_mvm_mac_ap_iterator_data {
1114e705c121SKalle Valo 	struct iwl_mvm *mvm;
1115e705c121SKalle Valo 	struct ieee80211_vif *vif;
1116e705c121SKalle Valo 	u32 beacon_device_ts;
1117e705c121SKalle Valo 	u16 beacon_int;
1118e705c121SKalle Valo };
1119e705c121SKalle Valo 
1120e705c121SKalle Valo /* Find the beacon_device_ts and beacon_int for a managed interface */
1121e705c121SKalle Valo static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
1122e705c121SKalle Valo 				    struct ieee80211_vif *vif)
1123e705c121SKalle Valo {
1124e705c121SKalle Valo 	struct iwl_mvm_mac_ap_iterator_data *data = _data;
1125e705c121SKalle Valo 
1126e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc)
1127e705c121SKalle Valo 		return;
1128e705c121SKalle Valo 
1129e705c121SKalle Valo 	/* Station client has higher priority over P2P client*/
1130e705c121SKalle Valo 	if (vif->p2p && data->beacon_device_ts)
1131e705c121SKalle Valo 		return;
1132e705c121SKalle Valo 
1133e705c121SKalle Valo 	data->beacon_device_ts = vif->bss_conf.sync_device_ts;
1134e705c121SKalle Valo 	data->beacon_int = vif->bss_conf.beacon_int;
1135e705c121SKalle Valo }
1136e705c121SKalle Valo 
1137e705c121SKalle Valo /*
1138e705c121SKalle Valo  * Fill the specific data for mac context of type AP of P2P GO
1139e705c121SKalle Valo  */
1140e705c121SKalle Valo static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
1141e705c121SKalle Valo 					 struct ieee80211_vif *vif,
1142e705c121SKalle Valo 					 struct iwl_mac_data_ap *ctxt_ap,
1143e705c121SKalle Valo 					 bool add)
1144e705c121SKalle Valo {
1145e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1146e705c121SKalle Valo 	struct iwl_mvm_mac_ap_iterator_data data = {
1147e705c121SKalle Valo 		.mvm = mvm,
1148e705c121SKalle Valo 		.vif = vif,
1149e705c121SKalle Valo 		.beacon_device_ts = 0
1150e705c121SKalle Valo 	};
1151e705c121SKalle Valo 
1152e705c121SKalle Valo 	ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
1153e705c121SKalle Valo 	ctxt_ap->bi_reciprocal =
1154e705c121SKalle Valo 		cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
1155e705c121SKalle Valo 	ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
1156e705c121SKalle Valo 					     vif->bss_conf.dtim_period);
1157e705c121SKalle Valo 	ctxt_ap->dtim_reciprocal =
1158e705c121SKalle Valo 		cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
1159e705c121SKalle Valo 					       vif->bss_conf.dtim_period));
1160e705c121SKalle Valo 
1161e705c121SKalle Valo 	ctxt_ap->mcast_qid = cpu_to_le32(vif->cab_queue);
1162e705c121SKalle Valo 
1163e705c121SKalle Valo 	/*
1164e705c121SKalle Valo 	 * Only set the beacon time when the MAC is being added, when we
1165e705c121SKalle Valo 	 * just modify the MAC then we should keep the time -- the firmware
1166e705c121SKalle Valo 	 * can otherwise have a "jumping" TBTT.
1167e705c121SKalle Valo 	 */
1168e705c121SKalle Valo 	if (add) {
1169e705c121SKalle Valo 		/*
1170e705c121SKalle Valo 		 * If there is a station/P2P client interface which is
1171e705c121SKalle Valo 		 * associated, set the AP's TBTT far enough from the station's
1172e705c121SKalle Valo 		 * TBTT. Otherwise, set it to the current system time
1173e705c121SKalle Valo 		 */
1174e705c121SKalle Valo 		ieee80211_iterate_active_interfaces_atomic(
1175e705c121SKalle Valo 			mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1176e705c121SKalle Valo 			iwl_mvm_mac_ap_iterator, &data);
1177e705c121SKalle Valo 
1178e705c121SKalle Valo 		if (data.beacon_device_ts) {
1179e705c121SKalle Valo 			u32 rand = (prandom_u32() % (64 - 36)) + 36;
1180e705c121SKalle Valo 			mvmvif->ap_beacon_time = data.beacon_device_ts +
1181e705c121SKalle Valo 				ieee80211_tu_to_usec(data.beacon_int * rand /
1182e705c121SKalle Valo 						     100);
1183e705c121SKalle Valo 		} else {
1184e705c121SKalle Valo 			mvmvif->ap_beacon_time =
1185e705c121SKalle Valo 				iwl_read_prph(mvm->trans,
1186e705c121SKalle Valo 					      DEVICE_SYSTEM_TIME_REG);
1187e705c121SKalle Valo 		}
1188e705c121SKalle Valo 	}
1189e705c121SKalle Valo 
1190e705c121SKalle Valo 	ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
1191e705c121SKalle Valo 	ctxt_ap->beacon_tsf = 0; /* unused */
1192e705c121SKalle Valo 
1193e705c121SKalle Valo 	/* TODO: Assume that the beacon id == mac context id */
1194e705c121SKalle Valo 	ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
1195e705c121SKalle Valo }
1196e705c121SKalle Valo 
1197e705c121SKalle Valo static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
1198e705c121SKalle Valo 				   struct ieee80211_vif *vif,
1199e705c121SKalle Valo 				   u32 action)
1200e705c121SKalle Valo {
1201e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd = {};
1202e705c121SKalle Valo 
1203e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
1204e705c121SKalle Valo 
1205e705c121SKalle Valo 	/* Fill the common data for all mac context types */
1206e705c121SKalle Valo 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1207e705c121SKalle Valo 
1208e705c121SKalle Valo 	/* Fill the data specific for ap mode */
1209e705c121SKalle Valo 	iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.ap,
1210e705c121SKalle Valo 				     action == FW_CTXT_ACTION_ADD);
1211e705c121SKalle Valo 
1212e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1213e705c121SKalle Valo }
1214e705c121SKalle Valo 
1215e705c121SKalle Valo static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
1216e705c121SKalle Valo 				   struct ieee80211_vif *vif,
1217e705c121SKalle Valo 				   u32 action)
1218e705c121SKalle Valo {
1219e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd = {};
1220e705c121SKalle Valo 	struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
1221e705c121SKalle Valo 
1222e705c121SKalle Valo 	WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
1223e705c121SKalle Valo 
1224e705c121SKalle Valo 	/* Fill the common data for all mac context types */
1225e705c121SKalle Valo 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1226e705c121SKalle Valo 
1227e705c121SKalle Valo 	/* Fill the data specific for GO mode */
1228e705c121SKalle Valo 	iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.go.ap,
1229e705c121SKalle Valo 				     action == FW_CTXT_ACTION_ADD);
1230e705c121SKalle Valo 
1231e705c121SKalle Valo 	cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
1232e705c121SKalle Valo 					IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
1233e705c121SKalle Valo 	cmd.go.opp_ps_enabled =
1234e705c121SKalle Valo 			cpu_to_le32(!!(noa->oppps_ctwindow &
1235e705c121SKalle Valo 					IEEE80211_P2P_OPPPS_ENABLE_BIT));
1236e705c121SKalle Valo 
1237e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1238e705c121SKalle Valo }
1239e705c121SKalle Valo 
1240e705c121SKalle Valo static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1241e705c121SKalle Valo 				u32 action, bool force_assoc_off,
1242e705c121SKalle Valo 				const u8 *bssid_override)
1243e705c121SKalle Valo {
1244e705c121SKalle Valo 	switch (vif->type) {
1245e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
1246e705c121SKalle Valo 		return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
1247e705c121SKalle Valo 						force_assoc_off,
1248e705c121SKalle Valo 						bssid_override);
1249e705c121SKalle Valo 		break;
1250e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
1251e705c121SKalle Valo 		if (!vif->p2p)
1252e705c121SKalle Valo 			return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
1253e705c121SKalle Valo 		else
1254e705c121SKalle Valo 			return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
1255e705c121SKalle Valo 		break;
1256e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
1257e705c121SKalle Valo 		return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
1258e705c121SKalle Valo 	case NL80211_IFTYPE_P2P_DEVICE:
1259e705c121SKalle Valo 		return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
1260e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
1261e705c121SKalle Valo 		return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
1262e705c121SKalle Valo 	default:
1263e705c121SKalle Valo 		break;
1264e705c121SKalle Valo 	}
1265e705c121SKalle Valo 
1266e705c121SKalle Valo 	return -EOPNOTSUPP;
1267e705c121SKalle Valo }
1268e705c121SKalle Valo 
1269e705c121SKalle Valo int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1270e705c121SKalle Valo {
1271e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1272e705c121SKalle Valo 	int ret;
1273e705c121SKalle Valo 
1274e705c121SKalle Valo 	if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
1275e705c121SKalle Valo 		      vif->addr, ieee80211_vif_type_p2p(vif)))
1276e705c121SKalle Valo 		return -EIO;
1277e705c121SKalle Valo 
1278e705c121SKalle Valo 	ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
1279e705c121SKalle Valo 				   true, NULL);
1280e705c121SKalle Valo 	if (ret)
1281e705c121SKalle Valo 		return ret;
1282e705c121SKalle Valo 
1283e705c121SKalle Valo 	/* will only do anything at resume from D3 time */
1284e705c121SKalle Valo 	iwl_mvm_set_last_nonqos_seq(mvm, vif);
1285e705c121SKalle Valo 
1286e705c121SKalle Valo 	mvmvif->uploaded = true;
1287e705c121SKalle Valo 	return 0;
1288e705c121SKalle Valo }
1289e705c121SKalle Valo 
1290e705c121SKalle Valo int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1291e705c121SKalle Valo 			     bool force_assoc_off, const u8 *bssid_override)
1292e705c121SKalle Valo {
1293e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1294e705c121SKalle Valo 
1295e705c121SKalle Valo 	if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
1296e705c121SKalle Valo 		      vif->addr, ieee80211_vif_type_p2p(vif)))
1297e705c121SKalle Valo 		return -EIO;
1298e705c121SKalle Valo 
1299e705c121SKalle Valo 	return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
1300e705c121SKalle Valo 				    force_assoc_off, bssid_override);
1301e705c121SKalle Valo }
1302e705c121SKalle Valo 
1303e705c121SKalle Valo int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1304e705c121SKalle Valo {
1305e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1306e705c121SKalle Valo 	struct iwl_mac_ctx_cmd cmd;
1307e705c121SKalle Valo 	int ret;
1308e705c121SKalle Valo 
1309e705c121SKalle Valo 	if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1310e705c121SKalle Valo 		      vif->addr, ieee80211_vif_type_p2p(vif)))
1311e705c121SKalle Valo 		return -EIO;
1312e705c121SKalle Valo 
1313e705c121SKalle Valo 	memset(&cmd, 0, sizeof(cmd));
1314e705c121SKalle Valo 
1315e705c121SKalle Valo 	cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1316e705c121SKalle Valo 							   mvmvif->color));
1317e705c121SKalle Valo 	cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1318e705c121SKalle Valo 
1319e705c121SKalle Valo 	ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
1320e705c121SKalle Valo 				   sizeof(cmd), &cmd);
1321e705c121SKalle Valo 	if (ret) {
1322e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
1323e705c121SKalle Valo 		return ret;
1324e705c121SKalle Valo 	}
1325e705c121SKalle Valo 
1326e705c121SKalle Valo 	mvmvif->uploaded = false;
1327e705c121SKalle Valo 
13280e39eb03SChaya Rachel Ivgi 	if (vif->type == NL80211_IFTYPE_MONITOR) {
1329e705c121SKalle Valo 		__clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, mvm->hw->flags);
13300e39eb03SChaya Rachel Ivgi 		iwl_mvm_dealloc_snif_sta(mvm);
13310e39eb03SChaya Rachel Ivgi 	}
1332e705c121SKalle Valo 
1333e705c121SKalle Valo 	return 0;
1334e705c121SKalle Valo }
1335e705c121SKalle Valo 
1336e705c121SKalle Valo static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
1337e705c121SKalle Valo 				   struct ieee80211_vif *csa_vif, u32 gp2,
1338e705c121SKalle Valo 				   bool tx_success)
1339e705c121SKalle Valo {
1340e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif =
1341e705c121SKalle Valo 			iwl_mvm_vif_from_mac80211(csa_vif);
1342e705c121SKalle Valo 
1343e705c121SKalle Valo 	/* Don't start to countdown from a failed beacon */
1344e705c121SKalle Valo 	if (!tx_success && !mvmvif->csa_countdown)
1345e705c121SKalle Valo 		return;
1346e705c121SKalle Valo 
1347e705c121SKalle Valo 	mvmvif->csa_countdown = true;
1348e705c121SKalle Valo 
1349e705c121SKalle Valo 	if (!ieee80211_csa_is_complete(csa_vif)) {
1350e705c121SKalle Valo 		int c = ieee80211_csa_update_counter(csa_vif);
1351e705c121SKalle Valo 
1352e705c121SKalle Valo 		iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif);
1353e705c121SKalle Valo 		if (csa_vif->p2p &&
1354e705c121SKalle Valo 		    !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2 &&
1355e705c121SKalle Valo 		    tx_success) {
1356e705c121SKalle Valo 			u32 rel_time = (c + 1) *
1357e705c121SKalle Valo 				       csa_vif->bss_conf.beacon_int -
1358e705c121SKalle Valo 				       IWL_MVM_CHANNEL_SWITCH_TIME_GO;
1359e705c121SKalle Valo 			u32 apply_time = gp2 + rel_time * 1024;
1360e705c121SKalle Valo 
1361e705c121SKalle Valo 			iwl_mvm_schedule_csa_period(mvm, csa_vif,
1362e705c121SKalle Valo 					 IWL_MVM_CHANNEL_SWITCH_TIME_GO -
1363e705c121SKalle Valo 					 IWL_MVM_CHANNEL_SWITCH_MARGIN,
1364e705c121SKalle Valo 					 apply_time);
1365e705c121SKalle Valo 		}
1366e705c121SKalle Valo 	} else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
1367e705c121SKalle Valo 		/* we don't have CSA NoA scheduled yet, switch now */
1368e705c121SKalle Valo 		ieee80211_csa_finish(csa_vif);
1369e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
1370e705c121SKalle Valo 	}
1371e705c121SKalle Valo }
1372e705c121SKalle Valo 
1373e705c121SKalle Valo void iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1374e705c121SKalle Valo 			     struct iwl_rx_cmd_buffer *rxb)
1375e705c121SKalle Valo {
1376e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1377e705c121SKalle Valo 	struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
1378e705c121SKalle Valo 	struct iwl_mvm_tx_resp *beacon_notify_hdr;
1379e705c121SKalle Valo 	struct ieee80211_vif *csa_vif;
1380e705c121SKalle Valo 	struct ieee80211_vif *tx_blocked_vif;
1381e705c121SKalle Valo 	u16 status;
1382e705c121SKalle Valo 
1383e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1384e705c121SKalle Valo 
1385e705c121SKalle Valo 	beacon_notify_hdr = &beacon->beacon_notify_hdr;
1386e705c121SKalle Valo 	mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
1387e705c121SKalle Valo 
1388e705c121SKalle Valo 	status = le16_to_cpu(beacon_notify_hdr->status.status) & TX_STATUS_MSK;
1389e705c121SKalle Valo 	IWL_DEBUG_RX(mvm,
1390e705c121SKalle Valo 		     "beacon status %#x retries:%d tsf:0x%16llX gp2:0x%X rate:%d\n",
1391e705c121SKalle Valo 		     status, beacon_notify_hdr->failure_frame,
1392e705c121SKalle Valo 		     le64_to_cpu(beacon->tsf),
1393e705c121SKalle Valo 		     mvm->ap_last_beacon_gp2,
1394e705c121SKalle Valo 		     le32_to_cpu(beacon_notify_hdr->initial_rate));
1395e705c121SKalle Valo 
1396e705c121SKalle Valo 	csa_vif = rcu_dereference_protected(mvm->csa_vif,
1397e705c121SKalle Valo 					    lockdep_is_held(&mvm->mutex));
1398e705c121SKalle Valo 	if (unlikely(csa_vif && csa_vif->csa_active))
1399e705c121SKalle Valo 		iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2,
1400e705c121SKalle Valo 				       (status == TX_STATUS_SUCCESS));
1401e705c121SKalle Valo 
1402e705c121SKalle Valo 	tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
1403e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
1404e705c121SKalle Valo 	if (unlikely(tx_blocked_vif)) {
1405e705c121SKalle Valo 		struct iwl_mvm_vif *mvmvif =
1406e705c121SKalle Valo 			iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1407e705c121SKalle Valo 
1408e705c121SKalle Valo 		/*
1409e705c121SKalle Valo 		 * The channel switch is started and we have blocked the
1410e705c121SKalle Valo 		 * stations. If this is the first beacon (the timeout wasn't
1411e705c121SKalle Valo 		 * set), set the unblock timeout, otherwise countdown
1412e705c121SKalle Valo 		 */
1413e705c121SKalle Valo 		if (!mvm->csa_tx_block_bcn_timeout)
1414e705c121SKalle Valo 			mvm->csa_tx_block_bcn_timeout =
1415e705c121SKalle Valo 				IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
1416e705c121SKalle Valo 		else
1417e705c121SKalle Valo 			mvm->csa_tx_block_bcn_timeout--;
1418e705c121SKalle Valo 
1419e705c121SKalle Valo 		/* Check if the timeout is expired, and unblock tx */
1420e705c121SKalle Valo 		if (mvm->csa_tx_block_bcn_timeout == 0) {
1421e705c121SKalle Valo 			iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
1422e705c121SKalle Valo 			RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1423e705c121SKalle Valo 		}
1424e705c121SKalle Valo 	}
1425e705c121SKalle Valo }
1426e705c121SKalle Valo 
1427e705c121SKalle Valo static void iwl_mvm_beacon_loss_iterator(void *_data, u8 *mac,
1428e705c121SKalle Valo 					 struct ieee80211_vif *vif)
1429e705c121SKalle Valo {
1430e705c121SKalle Valo 	struct iwl_missed_beacons_notif *missed_beacons = _data;
1431e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1432e705c121SKalle Valo 	struct iwl_mvm *mvm = mvmvif->mvm;
1433e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_missed_bcon *bcon_trig;
1434e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trigger;
1435e705c121SKalle Valo 	u32 stop_trig_missed_bcon, stop_trig_missed_bcon_since_rx;
1436e705c121SKalle Valo 	u32 rx_missed_bcon, rx_missed_bcon_since_rx;
1437e705c121SKalle Valo 
1438e705c121SKalle Valo 	if (mvmvif->id != (u16)le32_to_cpu(missed_beacons->mac_id))
1439e705c121SKalle Valo 		return;
1440e705c121SKalle Valo 
1441e705c121SKalle Valo 	rx_missed_bcon = le32_to_cpu(missed_beacons->consec_missed_beacons);
1442e705c121SKalle Valo 	rx_missed_bcon_since_rx =
1443e705c121SKalle Valo 		le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx);
1444e705c121SKalle Valo 	/*
1445e705c121SKalle Valo 	 * TODO: the threshold should be adjusted based on latency conditions,
1446e705c121SKalle Valo 	 * and/or in case of a CS flow on one of the other AP vifs.
1447e705c121SKalle Valo 	 */
1448e705c121SKalle Valo 	if (le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx) >
1449e705c121SKalle Valo 	     IWL_MVM_MISSED_BEACONS_THRESHOLD)
1450e705c121SKalle Valo 		ieee80211_beacon_loss(vif);
1451e705c121SKalle Valo 
1452e705c121SKalle Valo 	if (!iwl_fw_dbg_trigger_enabled(mvm->fw,
1453e705c121SKalle Valo 					FW_DBG_TRIGGER_MISSED_BEACONS))
1454e705c121SKalle Valo 		return;
1455e705c121SKalle Valo 
1456e705c121SKalle Valo 	trigger = iwl_fw_dbg_get_trigger(mvm->fw,
1457e705c121SKalle Valo 					 FW_DBG_TRIGGER_MISSED_BEACONS);
1458e705c121SKalle Valo 	bcon_trig = (void *)trigger->data;
1459e705c121SKalle Valo 	stop_trig_missed_bcon = le32_to_cpu(bcon_trig->stop_consec_missed_bcon);
1460e705c121SKalle Valo 	stop_trig_missed_bcon_since_rx =
1461e705c121SKalle Valo 		le32_to_cpu(bcon_trig->stop_consec_missed_bcon_since_rx);
1462e705c121SKalle Valo 
1463e705c121SKalle Valo 	/* TODO: implement start trigger */
1464e705c121SKalle Valo 
1465e705c121SKalle Valo 	if (!iwl_fw_dbg_trigger_check_stop(mvm, vif, trigger))
1466e705c121SKalle Valo 		return;
1467e705c121SKalle Valo 
1468e705c121SKalle Valo 	if (rx_missed_bcon_since_rx >= stop_trig_missed_bcon_since_rx ||
1469e705c121SKalle Valo 	    rx_missed_bcon >= stop_trig_missed_bcon)
1470e705c121SKalle Valo 		iwl_mvm_fw_dbg_collect_trig(mvm, trigger, NULL);
1471e705c121SKalle Valo }
1472e705c121SKalle Valo 
1473e705c121SKalle Valo void iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
1474e705c121SKalle Valo 				     struct iwl_rx_cmd_buffer *rxb)
1475e705c121SKalle Valo {
1476e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1477e705c121SKalle Valo 	struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
1478e705c121SKalle Valo 
1479e705c121SKalle Valo 	IWL_DEBUG_INFO(mvm,
1480e705c121SKalle Valo 		       "missed bcn mac_id=%u, consecutive=%u (%u, %u, %u)\n",
1481e705c121SKalle Valo 		       le32_to_cpu(mb->mac_id),
1482e705c121SKalle Valo 		       le32_to_cpu(mb->consec_missed_beacons),
1483e705c121SKalle Valo 		       le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
1484e705c121SKalle Valo 		       le32_to_cpu(mb->num_recvd_beacons),
1485e705c121SKalle Valo 		       le32_to_cpu(mb->num_expected_beacons));
1486e705c121SKalle Valo 
1487e705c121SKalle Valo 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
1488e705c121SKalle Valo 						   IEEE80211_IFACE_ITER_NORMAL,
1489e705c121SKalle Valo 						   iwl_mvm_beacon_loss_iterator,
1490e705c121SKalle Valo 						   mb);
1491e705c121SKalle Valo }
14920db056d3SSara Sharon 
14930db056d3SSara Sharon void iwl_mvm_rx_stored_beacon_notif(struct iwl_mvm *mvm,
14940db056d3SSara Sharon 				    struct iwl_rx_cmd_buffer *rxb)
14950db056d3SSara Sharon {
14960db056d3SSara Sharon 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
14970db056d3SSara Sharon 	struct iwl_stored_beacon_notif *sb = (void *)pkt->data;
14980db056d3SSara Sharon 	struct ieee80211_rx_status rx_status;
14990db056d3SSara Sharon 	struct sk_buff *skb;
15000db056d3SSara Sharon 	u32 size = le32_to_cpu(sb->byte_count);
15010db056d3SSara Sharon 
15020db056d3SSara Sharon 	if (size == 0)
15030db056d3SSara Sharon 		return;
15040db056d3SSara Sharon 
15050db056d3SSara Sharon 	skb = alloc_skb(size, GFP_ATOMIC);
15060db056d3SSara Sharon 	if (!skb) {
15070db056d3SSara Sharon 		IWL_ERR(mvm, "alloc_skb failed\n");
15080db056d3SSara Sharon 		return;
15090db056d3SSara Sharon 	}
15100db056d3SSara Sharon 
15110db056d3SSara Sharon 	/* update rx_status according to the notification's metadata */
15120db056d3SSara Sharon 	memset(&rx_status, 0, sizeof(rx_status));
15130db056d3SSara Sharon 	rx_status.mactime = le64_to_cpu(sb->tsf);
151477fe7395SSara Sharon 	/* TSF as indicated by the firmware  is at INA time */
151577fe7395SSara Sharon 	rx_status.flag |= RX_FLAG_MACTIME_PLCP_START;
15160db056d3SSara Sharon 	rx_status.device_timestamp = le32_to_cpu(sb->system_time);
15170db056d3SSara Sharon 	rx_status.band =
15180db056d3SSara Sharon 		(sb->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
151957fbcce3SJohannes Berg 				NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
15200db056d3SSara Sharon 	rx_status.freq =
15210db056d3SSara Sharon 		ieee80211_channel_to_frequency(le16_to_cpu(sb->channel),
15220db056d3SSara Sharon 					       rx_status.band);
15230db056d3SSara Sharon 
15240db056d3SSara Sharon 	/* copy the data */
15250db056d3SSara Sharon 	memcpy(skb_put(skb, size), sb->data, size);
15260db056d3SSara Sharon 	memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
15270db056d3SSara Sharon 
15280db056d3SSara Sharon 	/* pass it as regular rx to mac80211 */
1529d63b548fSJohannes Berg 	ieee80211_rx_napi(mvm->hw, NULL, skb, NULL);
15300db056d3SSara Sharon }
1531