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