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