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