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