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