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