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