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