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 	/* No need to allocate data queues to P2P Device MAC and NAN.*/
290 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
291 		return 0;
292 
293 	/* Allocate the CAB queue for softAP and GO interfaces */
294 	if (vif->type == NL80211_IFTYPE_AP ||
295 	    vif->type == NL80211_IFTYPE_ADHOC) {
296 		/*
297 		 * For TVQM this will be overwritten later with the FW assigned
298 		 * queue value (when queue is enabled).
299 		 */
300 		mvmvif->deflink.cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
301 	}
302 
303 	mvmvif->deflink.bcast_sta.sta_id = IWL_MVM_INVALID_STA;
304 	mvmvif->deflink.mcast_sta.sta_id = IWL_MVM_INVALID_STA;
305 	mvmvif->deflink.ap_sta_id = IWL_MVM_INVALID_STA;
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 static int iwl_mvm_mac_ctxt_send_beacon_v9(struct iwl_mvm *mvm,
1087 					   struct ieee80211_vif *vif,
1088 					   struct sk_buff *beacon,
1089 					   struct ieee80211_bss_conf *link_conf)
1090 {
1091 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1092 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(beacon);
1093 	struct iwl_mac_beacon_cmd beacon_cmd = {};
1094 	u8 rate = iwl_mvm_mac_ctxt_get_beacon_rate(mvm, info, vif);
1095 	u16 flags;
1096 	struct ieee80211_chanctx_conf *ctx;
1097 	int channel;
1098 	flags = iwl_mvm_mac_ctxt_get_beacon_flags(mvm->fw, rate);
1099 
1100 	/* Enable FILS on PSC channels only */
1101 	rcu_read_lock();
1102 	ctx = rcu_dereference(link_conf->chanctx_conf);
1103 	channel = ieee80211_frequency_to_channel(ctx->def.chan->center_freq);
1104 	WARN_ON(channel == 0);
1105 	if (cfg80211_channel_is_psc(ctx->def.chan) &&
1106 	    !IWL_MVM_DISABLE_AP_FILS) {
1107 		flags |= iwl_fw_lookup_cmd_ver(mvm->fw, BEACON_TEMPLATE_CMD,
1108 					       0) > 10 ?
1109 			IWL_MAC_BEACON_FILS :
1110 			IWL_MAC_BEACON_FILS_V1;
1111 		beacon_cmd.short_ssid =
1112 			cpu_to_le32(~crc32_le(~0, vif->cfg.ssid,
1113 					      vif->cfg.ssid_len));
1114 	}
1115 	rcu_read_unlock();
1116 
1117 	beacon_cmd.flags = cpu_to_le16(flags);
1118 	beacon_cmd.byte_cnt = cpu_to_le16((u16)beacon->len);
1119 
1120 	if (WARN_ON(!mvmvif->link[link_conf->link_id]))
1121 		return -EINVAL;
1122 
1123 	if (iwl_fw_lookup_cmd_ver(mvm->fw, BEACON_TEMPLATE_CMD, 0) > 12)
1124 		beacon_cmd.link_id =
1125 			cpu_to_le32(mvmvif->link[link_conf->link_id]->fw_link_id);
1126 	else
1127 		beacon_cmd.link_id = cpu_to_le32((u32)mvmvif->id);
1128 
1129 	if (vif->type == NL80211_IFTYPE_AP)
1130 		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
1131 					 &beacon_cmd.tim_size,
1132 					 beacon->data, beacon->len);
1133 
1134 	beacon_cmd.csa_offset =
1135 		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1136 						   WLAN_EID_CHANNEL_SWITCH,
1137 						   beacon->len));
1138 	beacon_cmd.ecsa_offset =
1139 		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1140 						   WLAN_EID_EXT_CHANSWITCH_ANN,
1141 						   beacon->len));
1142 
1143 	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
1144 						sizeof(beacon_cmd));
1145 }
1146 
1147 static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
1148 					struct ieee80211_vif *vif,
1149 					struct sk_buff *beacon,
1150 					struct ieee80211_bss_conf *link_conf)
1151 {
1152 	if (WARN_ON(!beacon))
1153 		return -EINVAL;
1154 
1155 	if (IWL_MVM_NON_TRANSMITTING_AP)
1156 		return 0;
1157 
1158 	if (!fw_has_capa(&mvm->fw->ucode_capa,
1159 			 IWL_UCODE_TLV_CAPA_CSA_AND_TBTT_OFFLOAD))
1160 		return iwl_mvm_mac_ctxt_send_beacon_v6(mvm, vif, beacon);
1161 
1162 	if (fw_has_api(&mvm->fw->ucode_capa,
1163 		       IWL_UCODE_TLV_API_NEW_BEACON_TEMPLATE))
1164 		return iwl_mvm_mac_ctxt_send_beacon_v9(mvm, vif, beacon,
1165 						       link_conf);
1166 
1167 	return iwl_mvm_mac_ctxt_send_beacon_v7(mvm, vif, beacon);
1168 }
1169 
1170 /* The beacon template for the AP/GO/IBSS has changed and needs update */
1171 int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
1172 				    struct ieee80211_vif *vif,
1173 				    struct ieee80211_bss_conf *link_conf)
1174 {
1175 	struct sk_buff *beacon;
1176 	int ret;
1177 
1178 	WARN_ON(vif->type != NL80211_IFTYPE_AP &&
1179 		vif->type != NL80211_IFTYPE_ADHOC);
1180 
1181 	beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL,
1182 					       link_conf->link_id);
1183 	if (!beacon)
1184 		return -ENOMEM;
1185 
1186 #ifdef CONFIG_IWLWIFI_DEBUGFS
1187 	if (mvm->beacon_inject_active) {
1188 		dev_kfree_skb(beacon);
1189 		return -EBUSY;
1190 	}
1191 #endif
1192 
1193 	ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon, link_conf);
1194 	dev_kfree_skb(beacon);
1195 	return ret;
1196 }
1197 
1198 struct iwl_mvm_mac_ap_iterator_data {
1199 	struct iwl_mvm *mvm;
1200 	struct ieee80211_vif *vif;
1201 	u32 beacon_device_ts;
1202 	u16 beacon_int;
1203 };
1204 
1205 /* Find the beacon_device_ts and beacon_int for a managed interface */
1206 static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
1207 				    struct ieee80211_vif *vif)
1208 {
1209 	struct iwl_mvm_mac_ap_iterator_data *data = _data;
1210 
1211 	if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc)
1212 		return;
1213 
1214 	/* Station client has higher priority over P2P client*/
1215 	if (vif->p2p && data->beacon_device_ts)
1216 		return;
1217 
1218 	data->beacon_device_ts = vif->bss_conf.sync_device_ts;
1219 	data->beacon_int = vif->bss_conf.beacon_int;
1220 }
1221 
1222 /*
1223  * Fill the filter flags for mac context of type AP or P2P GO.
1224  */
1225 void iwl_mvm_mac_ctxt_cmd_ap_set_filter_flags(struct iwl_mvm *mvm,
1226 					      struct iwl_mvm_vif *mvmvif,
1227 					      __le32 *filter_flags,
1228 					      int accept_probe_req_flag,
1229 					      int accept_beacon_flag)
1230 {
1231 	/*
1232 	 * in AP mode, pass probe requests and beacons from other APs
1233 	 * (needed for ht protection); when there're no any associated
1234 	 * station don't ask FW to pass beacons to prevent unnecessary
1235 	 * wake-ups.
1236 	 */
1237 	*filter_flags |= cpu_to_le32(accept_probe_req_flag);
1238 	if (mvmvif->ap_assoc_sta_count || !mvm->drop_bcn_ap_mode) {
1239 		*filter_flags |= cpu_to_le32(accept_beacon_flag);
1240 		IWL_DEBUG_HC(mvm, "Asking FW to pass beacons\n");
1241 	} else {
1242 		IWL_DEBUG_HC(mvm, "No need to receive beacons\n");
1243 	}
1244 }
1245 
1246 /*
1247  * Fill the specific data for mac context of type AP of P2P GO
1248  */
1249 static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
1250 					 struct ieee80211_vif *vif,
1251 					 struct iwl_mac_ctx_cmd *cmd,
1252 					 struct iwl_mac_data_ap *ctxt_ap,
1253 					 bool add)
1254 {
1255 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1256 	struct iwl_mvm_mac_ap_iterator_data data = {
1257 		.mvm = mvm,
1258 		.vif = vif,
1259 		.beacon_device_ts = 0
1260 	};
1261 
1262 	/* in AP mode, the MCAST FIFO takes the EDCA params from VO */
1263 	cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |= BIT(IWL_MVM_TX_FIFO_MCAST);
1264 
1265 	iwl_mvm_mac_ctxt_cmd_ap_set_filter_flags(mvm, mvmvif,
1266 						 &cmd->filter_flags,
1267 						 MAC_FILTER_IN_PROBE_REQUEST,
1268 						 MAC_FILTER_IN_BEACON);
1269 
1270 	ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
1271 	ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
1272 					     vif->bss_conf.dtim_period);
1273 
1274 	if (!fw_has_api(&mvm->fw->ucode_capa,
1275 			IWL_UCODE_TLV_API_STA_TYPE))
1276 		ctxt_ap->mcast_qid = cpu_to_le32(mvmvif->deflink.cab_queue);
1277 
1278 	/*
1279 	 * Only set the beacon time when the MAC is being added, when we
1280 	 * just modify the MAC then we should keep the time -- the firmware
1281 	 * can otherwise have a "jumping" TBTT.
1282 	 */
1283 	if (add) {
1284 		/*
1285 		 * If there is a station/P2P client interface which is
1286 		 * associated, set the AP's TBTT far enough from the station's
1287 		 * TBTT. Otherwise, set it to the current system time
1288 		 */
1289 		ieee80211_iterate_active_interfaces_atomic(
1290 			mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1291 			iwl_mvm_mac_ap_iterator, &data);
1292 
1293 		if (data.beacon_device_ts) {
1294 			u32 rand = get_random_u32_inclusive(36, 63);
1295 			mvmvif->ap_beacon_time = data.beacon_device_ts +
1296 				ieee80211_tu_to_usec(data.beacon_int * rand /
1297 						     100);
1298 		} else {
1299 			mvmvif->ap_beacon_time = iwl_mvm_get_systime(mvm);
1300 		}
1301 	}
1302 
1303 	ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
1304 	ctxt_ap->beacon_tsf = 0; /* unused */
1305 
1306 	/* TODO: Assume that the beacon id == mac context id */
1307 	ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
1308 }
1309 
1310 static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
1311 				   struct ieee80211_vif *vif,
1312 				   u32 action)
1313 {
1314 	struct iwl_mac_ctx_cmd cmd = {};
1315 
1316 	WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
1317 
1318 	/* Fill the common data for all mac context types */
1319 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1320 
1321 	/* Fill the data specific for ap mode */
1322 	iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.ap,
1323 				     action == FW_CTXT_ACTION_ADD);
1324 
1325 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1326 }
1327 
1328 static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
1329 				   struct ieee80211_vif *vif,
1330 				   u32 action)
1331 {
1332 	struct iwl_mac_ctx_cmd cmd = {};
1333 	struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
1334 
1335 	WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
1336 
1337 	/* Fill the common data for all mac context types */
1338 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1339 
1340 	/* Fill the data specific for GO mode */
1341 	iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.go.ap,
1342 				     action == FW_CTXT_ACTION_ADD);
1343 
1344 	cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
1345 					IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
1346 	cmd.go.opp_ps_enabled =
1347 			cpu_to_le32(!!(noa->oppps_ctwindow &
1348 					IEEE80211_P2P_OPPPS_ENABLE_BIT));
1349 
1350 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1351 }
1352 
1353 static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1354 				u32 action, bool force_assoc_off,
1355 				const u8 *bssid_override)
1356 {
1357 	switch (vif->type) {
1358 	case NL80211_IFTYPE_STATION:
1359 		return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
1360 						force_assoc_off,
1361 						bssid_override);
1362 	case NL80211_IFTYPE_AP:
1363 		if (!vif->p2p)
1364 			return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
1365 		else
1366 			return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
1367 	case NL80211_IFTYPE_MONITOR:
1368 		return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
1369 	case NL80211_IFTYPE_P2P_DEVICE:
1370 		return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
1371 	case NL80211_IFTYPE_ADHOC:
1372 		return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
1373 	default:
1374 		break;
1375 	}
1376 
1377 	return -EOPNOTSUPP;
1378 }
1379 
1380 int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1381 {
1382 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1383 	int ret;
1384 
1385 	if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
1386 		      vif->addr, ieee80211_vif_type_p2p(vif)))
1387 		return -EIO;
1388 
1389 	ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
1390 				   true, NULL);
1391 	if (ret)
1392 		return ret;
1393 
1394 	/* will only do anything at resume from D3 time */
1395 	iwl_mvm_set_last_nonqos_seq(mvm, vif);
1396 
1397 	mvmvif->uploaded = true;
1398 	return 0;
1399 }
1400 
1401 int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1402 			     bool force_assoc_off, const u8 *bssid_override)
1403 {
1404 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1405 
1406 	if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
1407 		      vif->addr, ieee80211_vif_type_p2p(vif)))
1408 		return -EIO;
1409 
1410 	return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
1411 				    force_assoc_off, bssid_override);
1412 }
1413 
1414 int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1415 {
1416 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1417 	struct iwl_mac_ctx_cmd cmd;
1418 	int ret;
1419 
1420 	if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1421 		      vif->addr, ieee80211_vif_type_p2p(vif)))
1422 		return -EIO;
1423 
1424 	memset(&cmd, 0, sizeof(cmd));
1425 
1426 	cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1427 							   mvmvif->color));
1428 	cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1429 
1430 	ret = iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1431 	if (ret)
1432 		return ret;
1433 
1434 	mvmvif->uploaded = false;
1435 
1436 	if (vif->type == NL80211_IFTYPE_MONITOR) {
1437 		__clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, mvm->hw->flags);
1438 		iwl_mvm_dealloc_snif_sta(mvm);
1439 	}
1440 
1441 	return 0;
1442 }
1443 
1444 static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
1445 				   struct ieee80211_vif *csa_vif, u32 gp2,
1446 				   bool tx_success)
1447 {
1448 	struct iwl_mvm_vif *mvmvif =
1449 			iwl_mvm_vif_from_mac80211(csa_vif);
1450 
1451 	/* Don't start to countdown from a failed beacon */
1452 	if (!tx_success && !mvmvif->csa_countdown)
1453 		return;
1454 
1455 	mvmvif->csa_countdown = true;
1456 
1457 	if (!ieee80211_beacon_cntdwn_is_complete(csa_vif)) {
1458 		int c = ieee80211_beacon_update_cntdwn(csa_vif);
1459 
1460 		iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif,
1461 						&csa_vif->bss_conf);
1462 		if (csa_vif->p2p &&
1463 		    !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2 &&
1464 		    tx_success) {
1465 			u32 rel_time = (c + 1) *
1466 				       csa_vif->bss_conf.beacon_int -
1467 				       IWL_MVM_CHANNEL_SWITCH_TIME_GO;
1468 			u32 apply_time = gp2 + rel_time * 1024;
1469 
1470 			iwl_mvm_schedule_csa_period(mvm, csa_vif,
1471 					 IWL_MVM_CHANNEL_SWITCH_TIME_GO -
1472 					 IWL_MVM_CHANNEL_SWITCH_MARGIN,
1473 					 apply_time);
1474 		}
1475 	} else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
1476 		/* we don't have CSA NoA scheduled yet, switch now */
1477 		ieee80211_csa_finish(csa_vif);
1478 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
1479 	}
1480 }
1481 
1482 void iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1483 			     struct iwl_rx_cmd_buffer *rxb)
1484 {
1485 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1486 	unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
1487 	struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
1488 	struct iwl_extended_beacon_notif_v5 *beacon_v5 = (void *)pkt->data;
1489 	struct ieee80211_vif *csa_vif;
1490 	struct ieee80211_vif *tx_blocked_vif;
1491 	struct agg_tx_status *agg_status;
1492 	u16 status;
1493 
1494 	lockdep_assert_held(&mvm->mutex);
1495 
1496 	mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
1497 
1498 	if (!iwl_mvm_is_short_beacon_notif_supported(mvm)) {
1499 		struct iwl_mvm_tx_resp *beacon_notify_hdr =
1500 			&beacon_v5->beacon_notify_hdr;
1501 
1502 		if (unlikely(pkt_len < sizeof(*beacon_v5)))
1503 			return;
1504 
1505 		mvm->ibss_manager = beacon_v5->ibss_mgr_status != 0;
1506 		agg_status = iwl_mvm_get_agg_status(mvm, beacon_notify_hdr);
1507 		status = le16_to_cpu(agg_status->status) & TX_STATUS_MSK;
1508 		IWL_DEBUG_RX(mvm,
1509 			     "beacon status %#x retries:%d tsf:0x%016llX gp2:0x%X rate:%d\n",
1510 			     status, beacon_notify_hdr->failure_frame,
1511 			     le64_to_cpu(beacon->tsf),
1512 			     mvm->ap_last_beacon_gp2,
1513 			     le32_to_cpu(beacon_notify_hdr->initial_rate));
1514 	} else {
1515 		if (unlikely(pkt_len < sizeof(*beacon)))
1516 			return;
1517 
1518 		mvm->ibss_manager = beacon->ibss_mgr_status != 0;
1519 		status = le32_to_cpu(beacon->status) & TX_STATUS_MSK;
1520 		IWL_DEBUG_RX(mvm,
1521 			     "beacon status %#x tsf:0x%016llX gp2:0x%X\n",
1522 			     status, le64_to_cpu(beacon->tsf),
1523 			     mvm->ap_last_beacon_gp2);
1524 	}
1525 
1526 	csa_vif = rcu_dereference_protected(mvm->csa_vif,
1527 					    lockdep_is_held(&mvm->mutex));
1528 	if (unlikely(csa_vif && csa_vif->bss_conf.csa_active))
1529 		iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2,
1530 				       (status == TX_STATUS_SUCCESS));
1531 
1532 	tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
1533 						lockdep_is_held(&mvm->mutex));
1534 	if (unlikely(tx_blocked_vif)) {
1535 		struct iwl_mvm_vif *mvmvif =
1536 			iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1537 
1538 		/*
1539 		 * The channel switch is started and we have blocked the
1540 		 * stations. If this is the first beacon (the timeout wasn't
1541 		 * set), set the unblock timeout, otherwise countdown
1542 		 */
1543 		if (!mvm->csa_tx_block_bcn_timeout)
1544 			mvm->csa_tx_block_bcn_timeout =
1545 				IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
1546 		else
1547 			mvm->csa_tx_block_bcn_timeout--;
1548 
1549 		/* Check if the timeout is expired, and unblock tx */
1550 		if (mvm->csa_tx_block_bcn_timeout == 0) {
1551 			iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
1552 			RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1553 		}
1554 	}
1555 }
1556 
1557 void iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
1558 				     struct iwl_rx_cmd_buffer *rxb)
1559 {
1560 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1561 	struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
1562 	struct iwl_fw_dbg_trigger_missed_bcon *bcon_trig;
1563 	struct iwl_fw_dbg_trigger_tlv *trigger;
1564 	u32 stop_trig_missed_bcon, stop_trig_missed_bcon_since_rx;
1565 	u32 rx_missed_bcon, rx_missed_bcon_since_rx;
1566 	struct ieee80211_vif *vif;
1567 	/* Id can be mac/link id depending on the notification version */
1568 	u32 id = le32_to_cpu(mb->link_id);
1569 	union iwl_dbg_tlv_tp_data tp_data = { .fw_pkt = pkt };
1570 	u32 mac_type;
1571 	u8 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
1572 					       MISSED_BEACONS_NOTIFICATION,
1573 					       0);
1574 
1575 	rcu_read_lock();
1576 
1577 	/* before version four the ID in the notification refers to mac ID */
1578 	if (notif_ver < 4) {
1579 		vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);
1580 	} else {
1581 		struct ieee80211_bss_conf *bss_conf =
1582 			iwl_mvm_rcu_fw_link_id_to_link_conf(mvm, id, true);
1583 
1584 		if (!bss_conf)
1585 			goto out;
1586 
1587 		vif = bss_conf->vif;
1588 	}
1589 
1590 	IWL_DEBUG_INFO(mvm,
1591 		       "missed bcn %s_id=%u, consecutive=%u (%u, %u, %u)\n",
1592 		       notif_ver < 4 ? "mac" : "link",
1593 		       id,
1594 		       le32_to_cpu(mb->consec_missed_beacons),
1595 		       le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
1596 		       le32_to_cpu(mb->num_recvd_beacons),
1597 		       le32_to_cpu(mb->num_expected_beacons));
1598 
1599 	if (!vif)
1600 		goto out;
1601 
1602 	mac_type = iwl_mvm_get_mac_type(vif);
1603 
1604 	IWL_DEBUG_INFO(mvm, "missed beacon mac_type=%u,\n", mac_type);
1605 
1606 	mvm->trans->dbg.dump_file_name_ext_valid = true;
1607 	snprintf(mvm->trans->dbg.dump_file_name_ext, IWL_FW_INI_MAX_NAME,
1608 		 "MacId_%d_MacType_%d", id, mac_type);
1609 
1610 	rx_missed_bcon = le32_to_cpu(mb->consec_missed_beacons);
1611 	rx_missed_bcon_since_rx =
1612 		le32_to_cpu(mb->consec_missed_beacons_since_last_rx);
1613 	/*
1614 	 * TODO: the threshold should be adjusted based on latency conditions,
1615 	 * and/or in case of a CS flow on one of the other AP vifs.
1616 	 */
1617 	if (rx_missed_bcon > IWL_MVM_MISSED_BEACONS_THRESHOLD_LONG)
1618 		iwl_mvm_connection_loss(mvm, vif, "missed beacons");
1619 	else if (rx_missed_bcon_since_rx > IWL_MVM_MISSED_BEACONS_THRESHOLD)
1620 		ieee80211_beacon_loss(vif);
1621 
1622 	iwl_dbg_tlv_time_point(&mvm->fwrt,
1623 			       IWL_FW_INI_TIME_POINT_MISSED_BEACONS, &tp_data);
1624 
1625 	trigger = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
1626 					FW_DBG_TRIGGER_MISSED_BEACONS);
1627 	if (!trigger)
1628 		goto out;
1629 
1630 	bcon_trig = (void *)trigger->data;
1631 	stop_trig_missed_bcon = le32_to_cpu(bcon_trig->stop_consec_missed_bcon);
1632 	stop_trig_missed_bcon_since_rx =
1633 		le32_to_cpu(bcon_trig->stop_consec_missed_bcon_since_rx);
1634 
1635 	/* TODO: implement start trigger */
1636 
1637 	if (rx_missed_bcon_since_rx >= stop_trig_missed_bcon_since_rx ||
1638 	    rx_missed_bcon >= stop_trig_missed_bcon)
1639 		iwl_fw_dbg_collect_trig(&mvm->fwrt, trigger, NULL);
1640 
1641 out:
1642 	rcu_read_unlock();
1643 }
1644 
1645 void iwl_mvm_rx_stored_beacon_notif(struct iwl_mvm *mvm,
1646 				    struct iwl_rx_cmd_buffer *rxb)
1647 {
1648 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1649 	unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
1650 	struct iwl_stored_beacon_notif_common *sb = (void *)pkt->data;
1651 	struct ieee80211_rx_status rx_status;
1652 	struct sk_buff *skb;
1653 	u8 *data;
1654 	u32 size = le32_to_cpu(sb->byte_count);
1655 	int ver = iwl_fw_lookup_cmd_ver(mvm->fw,
1656 					WIDE_ID(PROT_OFFLOAD_GROUP, STORED_BEACON_NTF),
1657 					0);
1658 
1659 	if (size == 0)
1660 		return;
1661 
1662 	/* handle per-version differences */
1663 	if (ver <= 2) {
1664 		struct iwl_stored_beacon_notif_v2 *sb_v2 = (void *)pkt->data;
1665 
1666 		if (pkt_len < struct_size(sb_v2, data, size))
1667 			return;
1668 
1669 		data = sb_v2->data;
1670 	} else {
1671 		struct iwl_stored_beacon_notif_v3 *sb_v3 = (void *)pkt->data;
1672 
1673 		if (pkt_len < struct_size(sb_v3, data, size))
1674 			return;
1675 
1676 		data = sb_v3->data;
1677 	}
1678 
1679 	skb = alloc_skb(size, GFP_ATOMIC);
1680 	if (!skb) {
1681 		IWL_ERR(mvm, "alloc_skb failed\n");
1682 		return;
1683 	}
1684 
1685 	/* update rx_status according to the notification's metadata */
1686 	memset(&rx_status, 0, sizeof(rx_status));
1687 	rx_status.mactime = le64_to_cpu(sb->tsf);
1688 	/* TSF as indicated by the firmware  is at INA time */
1689 	rx_status.flag |= RX_FLAG_MACTIME_PLCP_START;
1690 	rx_status.device_timestamp = le32_to_cpu(sb->system_time);
1691 	rx_status.band =
1692 		(sb->band & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
1693 				NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
1694 	rx_status.freq =
1695 		ieee80211_channel_to_frequency(le16_to_cpu(sb->channel),
1696 					       rx_status.band);
1697 
1698 	/* copy the data */
1699 	skb_put_data(skb, data, size);
1700 	memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
1701 
1702 	/* pass it as regular rx to mac80211 */
1703 	ieee80211_rx_napi(mvm->hw, NULL, skb, NULL);
1704 }
1705 
1706 void iwl_mvm_probe_resp_data_notif(struct iwl_mvm *mvm,
1707 				   struct iwl_rx_cmd_buffer *rxb)
1708 {
1709 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1710 	struct iwl_probe_resp_data_notif *notif = (void *)pkt->data;
1711 	struct iwl_probe_resp_data *old_data, *new_data;
1712 	u32 id = le32_to_cpu(notif->mac_id);
1713 	struct ieee80211_vif *vif;
1714 	struct iwl_mvm_vif *mvmvif;
1715 
1716 	IWL_DEBUG_INFO(mvm, "Probe response data notif: noa %d, csa %d\n",
1717 		       notif->noa_active, notif->csa_counter);
1718 
1719 	vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, false);
1720 	if (!vif)
1721 		return;
1722 
1723 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
1724 
1725 	new_data = kzalloc(sizeof(*new_data), GFP_KERNEL);
1726 	if (!new_data)
1727 		return;
1728 
1729 	memcpy(&new_data->notif, notif, sizeof(new_data->notif));
1730 
1731 	/* noa_attr contains 1 reserved byte, need to substruct it */
1732 	new_data->noa_len = sizeof(struct ieee80211_vendor_ie) +
1733 			    sizeof(new_data->notif.noa_attr) - 1;
1734 
1735 	/*
1736 	 * If it's a one time NoA, only one descriptor is needed,
1737 	 * adjust the length according to len_low.
1738 	 */
1739 	if (new_data->notif.noa_attr.len_low ==
1740 	    sizeof(struct ieee80211_p2p_noa_desc) + 2)
1741 		new_data->noa_len -= sizeof(struct ieee80211_p2p_noa_desc);
1742 
1743 	old_data = rcu_dereference_protected(mvmvif->deflink.probe_resp_data,
1744 					     lockdep_is_held(&mvmvif->mvm->mutex));
1745 	rcu_assign_pointer(mvmvif->deflink.probe_resp_data, new_data);
1746 
1747 	if (old_data)
1748 		kfree_rcu(old_data, rcu_head);
1749 
1750 	if (notif->csa_counter != IWL_PROBE_RESP_DATA_NO_CSA &&
1751 	    notif->csa_counter >= 1)
1752 		ieee80211_beacon_set_cntdwn(vif, notif->csa_counter);
1753 }
1754 
1755 void iwl_mvm_channel_switch_start_notif(struct iwl_mvm *mvm,
1756 					struct iwl_rx_cmd_buffer *rxb)
1757 {
1758 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1759 	struct ieee80211_vif *csa_vif, *vif;
1760 	struct iwl_mvm_vif *mvmvif, *csa_mvmvif;
1761 	u32 id_n_color, csa_id;
1762 	/* save mac_id or link_id to use later to cancel csa if needed */
1763 	u32 id;
1764 	u8 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
1765 					       CHANNEL_SWITCH_START_NOTIF, 0);
1766 	bool csa_active;
1767 
1768 	rcu_read_lock();
1769 
1770 	if (notif_ver < 3) {
1771 		struct iwl_channel_switch_start_notif_v1 *notif = (void *)pkt->data;
1772 		u32 mac_id;
1773 
1774 		id_n_color = le32_to_cpu(notif->id_and_color);
1775 		mac_id = id_n_color & FW_CTXT_ID_MSK;
1776 
1777 		vif = iwl_mvm_rcu_dereference_vif_id(mvm, mac_id, true);
1778 		if (!vif)
1779 			goto out_unlock;
1780 
1781 		id = mac_id;
1782 		csa_active = vif->bss_conf.csa_active;
1783 	} else {
1784 		struct iwl_channel_switch_start_notif *notif = (void *)pkt->data;
1785 		u32 link_id = le32_to_cpu(notif->link_id);
1786 		struct ieee80211_bss_conf *bss_conf =
1787 			iwl_mvm_rcu_fw_link_id_to_link_conf(mvm, link_id, true);
1788 
1789 		if (!bss_conf)
1790 			goto out_unlock;
1791 
1792 		id = link_id;
1793 		vif = bss_conf->vif;
1794 		csa_active = bss_conf->csa_active;
1795 	}
1796 
1797 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
1798 	if (notif_ver >= 3)
1799 		id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
1800 
1801 	switch (vif->type) {
1802 	case NL80211_IFTYPE_AP:
1803 		csa_vif = rcu_dereference(mvm->csa_vif);
1804 		if (WARN_ON(!csa_vif || !csa_vif->bss_conf.csa_active ||
1805 			    csa_vif != vif))
1806 			goto out_unlock;
1807 
1808 		csa_mvmvif = iwl_mvm_vif_from_mac80211(csa_vif);
1809 		csa_id = FW_CMD_ID_AND_COLOR(csa_mvmvif->id, csa_mvmvif->color);
1810 		if (WARN(csa_id != id_n_color,
1811 			 "channel switch noa notification on unexpected vif (csa_vif=%d, notif=%d)",
1812 			 csa_id, id_n_color))
1813 			goto out_unlock;
1814 
1815 		IWL_DEBUG_INFO(mvm, "Channel Switch Started Notification\n");
1816 
1817 		schedule_delayed_work(&mvm->cs_tx_unblock_dwork,
1818 				      msecs_to_jiffies(IWL_MVM_CS_UNBLOCK_TX_TIMEOUT *
1819 						       csa_vif->bss_conf.beacon_int));
1820 
1821 		ieee80211_csa_finish(csa_vif);
1822 
1823 		rcu_read_unlock();
1824 
1825 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
1826 		return;
1827 	case NL80211_IFTYPE_STATION:
1828 		/*
1829 		 * if we don't know about an ongoing channel switch,
1830 		 * make sure FW cancels it
1831 		 */
1832 		if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
1833 					    CHANNEL_SWITCH_ERROR_NOTIF,
1834 					    0) && !csa_active) {
1835 			IWL_DEBUG_INFO(mvm, "Channel Switch was canceled\n");
1836 			iwl_mvm_cancel_channel_switch(mvm, vif, id);
1837 			break;
1838 		}
1839 
1840 		iwl_mvm_csa_client_absent(mvm, vif);
1841 		cancel_delayed_work(&mvmvif->csa_work);
1842 		ieee80211_chswitch_done(vif, true);
1843 		break;
1844 	default:
1845 		/* should never happen */
1846 		WARN_ON_ONCE(1);
1847 		break;
1848 	}
1849 out_unlock:
1850 	rcu_read_unlock();
1851 }
1852 
1853 void iwl_mvm_channel_switch_error_notif(struct iwl_mvm *mvm,
1854 					struct iwl_rx_cmd_buffer *rxb)
1855 {
1856 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1857 	struct iwl_channel_switch_error_notif *notif = (void *)pkt->data;
1858 	struct ieee80211_vif *vif;
1859 	u32 id = le32_to_cpu(notif->link_id);
1860 	u32 csa_err_mask = le32_to_cpu(notif->csa_err_mask);
1861 
1862 	rcu_read_lock();
1863 	vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);
1864 	if (!vif) {
1865 		rcu_read_unlock();
1866 		return;
1867 	}
1868 
1869 	IWL_DEBUG_INFO(mvm, "FW reports CSA error: id=%u, csa_err_mask=%u\n",
1870 		       id, csa_err_mask);
1871 	if (csa_err_mask & (CS_ERR_COUNT_ERROR |
1872 			    CS_ERR_LONG_DELAY_AFTER_CS |
1873 			    CS_ERR_TX_BLOCK_TIMER_EXPIRED))
1874 		ieee80211_channel_switch_disconnect(vif, true);
1875 	rcu_read_unlock();
1876 }
1877 
1878 void iwl_mvm_rx_missed_vap_notif(struct iwl_mvm *mvm,
1879 				 struct iwl_rx_cmd_buffer *rxb)
1880 {
1881 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1882 	struct iwl_missed_vap_notif *mb = (void *)pkt->data;
1883 	struct ieee80211_vif *vif;
1884 	u32 id = le32_to_cpu(mb->mac_id);
1885 
1886 	IWL_DEBUG_INFO(mvm,
1887 		       "missed_vap notify mac_id=%u, num_beacon_intervals_elapsed=%u, profile_periodicity=%u\n",
1888 		       le32_to_cpu(mb->mac_id),
1889 		       mb->num_beacon_intervals_elapsed,
1890 		       mb->profile_periodicity);
1891 
1892 	rcu_read_lock();
1893 
1894 	vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);
1895 	if (vif)
1896 		iwl_mvm_connection_loss(mvm, vif, "missed vap beacon");
1897 
1898 	rcu_read_unlock();
1899 }
1900