1 /******************************************************************************
2  *
3  * This file is provided under a dual BSD/GPLv2 license.  When using or
4  * redistributing this file, you may do so under either license.
5  *
6  * GPL LICENSE SUMMARY
7  *
8  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9  * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
10  * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
11  * Copyright(c) 2018 Intel Corporation
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of version 2 of the GNU General Public License as
15  * published by the Free Software Foundation.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * General Public License for more details.
21  *
22  * The full GNU General Public License is included in this distribution
23  * in the file called COPYING.
24  *
25  * Contact Information:
26  *  Intel Linux Wireless <linuxwifi@intel.com>
27  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28  *
29  * BSD LICENSE
30  *
31  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
32  * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
33  * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
34  * Copyright(c) 2018 Intel Corporation
35  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  *
41  *  * Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  *  * Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in
45  *    the documentation and/or other materials provided with the
46  *    distribution.
47  *  * Neither the name Intel Corporation nor the names of its
48  *    contributors may be used to endorse or promote products derived
49  *    from this software without specific prior written permission.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
57  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62  *
63  *****************************************************************************/
64 
65 #include <linux/etherdevice.h>
66 #include <net/mac80211.h>
67 #include "iwl-io.h"
68 #include "iwl-prph.h"
69 #include "fw-api.h"
70 #include "mvm.h"
71 #include "time-event.h"
72 
73 const u8 iwl_mvm_ac_to_tx_fifo[] = {
74 	IWL_MVM_TX_FIFO_VO,
75 	IWL_MVM_TX_FIFO_VI,
76 	IWL_MVM_TX_FIFO_BE,
77 	IWL_MVM_TX_FIFO_BK,
78 };
79 
80 const u8 iwl_mvm_ac_to_gen2_tx_fifo[] = {
81 	IWL_GEN2_EDCA_TX_FIFO_VO,
82 	IWL_GEN2_EDCA_TX_FIFO_VI,
83 	IWL_GEN2_EDCA_TX_FIFO_BE,
84 	IWL_GEN2_EDCA_TX_FIFO_BK,
85 	IWL_GEN2_TRIG_TX_FIFO_VO,
86 	IWL_GEN2_TRIG_TX_FIFO_VI,
87 	IWL_GEN2_TRIG_TX_FIFO_BE,
88 	IWL_GEN2_TRIG_TX_FIFO_BK,
89 };
90 
91 struct iwl_mvm_mac_iface_iterator_data {
92 	struct iwl_mvm *mvm;
93 	struct ieee80211_vif *vif;
94 	unsigned long available_mac_ids[BITS_TO_LONGS(NUM_MAC_INDEX_DRIVER)];
95 	unsigned long available_tsf_ids[BITS_TO_LONGS(NUM_TSF_IDS)];
96 	enum iwl_tsf_id preferred_tsf;
97 	bool found_vif;
98 };
99 
100 struct iwl_mvm_hw_queues_iface_iterator_data {
101 	struct ieee80211_vif *exclude_vif;
102 	unsigned long used_hw_queues;
103 };
104 
105 static void iwl_mvm_mac_tsf_id_iter(void *_data, u8 *mac,
106 				    struct ieee80211_vif *vif)
107 {
108 	struct iwl_mvm_mac_iface_iterator_data *data = _data;
109 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
110 	u16 min_bi;
111 
112 	/* Skip the interface for which we are trying to assign a tsf_id  */
113 	if (vif == data->vif)
114 		return;
115 
116 	/*
117 	 * The TSF is a hardware/firmware resource, there are 4 and
118 	 * the driver should assign and free them as needed. However,
119 	 * there are cases where 2 MACs should share the same TSF ID
120 	 * for the purpose of clock sync, an optimization to avoid
121 	 * clock drift causing overlapping TBTTs/DTIMs for a GO and
122 	 * client in the system.
123 	 *
124 	 * The firmware will decide according to the MAC type which
125 	 * will be the master and slave. Clients that need to sync
126 	 * with a remote station will be the master, and an AP or GO
127 	 * will be the slave.
128 	 *
129 	 * Depending on the new interface type it can be slaved to
130 	 * or become the master of an existing interface.
131 	 */
132 	switch (data->vif->type) {
133 	case NL80211_IFTYPE_STATION:
134 		/*
135 		 * The new interface is a client, so if the one we're iterating
136 		 * is an AP, and the beacon interval of the AP is a multiple or
137 		 * divisor of the beacon interval of the client, the same TSF
138 		 * should be used to avoid drift between the new client and
139 		 * existing AP. The existing AP will get drift updates from the
140 		 * new client context in this case.
141 		 */
142 		if (vif->type != NL80211_IFTYPE_AP ||
143 		    data->preferred_tsf != NUM_TSF_IDS ||
144 		    !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
145 			break;
146 
147 		min_bi = min(data->vif->bss_conf.beacon_int,
148 			     vif->bss_conf.beacon_int);
149 
150 		if (!min_bi)
151 			break;
152 
153 		if ((data->vif->bss_conf.beacon_int -
154 		     vif->bss_conf.beacon_int) % min_bi == 0) {
155 			data->preferred_tsf = mvmvif->tsf_id;
156 			return;
157 		}
158 		break;
159 
160 	case NL80211_IFTYPE_AP:
161 		/*
162 		 * The new interface is AP/GO, so if its beacon interval is a
163 		 * multiple or a divisor of the beacon interval of an existing
164 		 * interface, it should get drift updates from an existing
165 		 * client or use the same TSF as an existing GO. There's no
166 		 * drift between TSFs internally but if they used different
167 		 * TSFs then a new client MAC could update one of them and
168 		 * cause drift that way.
169 		 */
170 		if ((vif->type != NL80211_IFTYPE_AP &&
171 		     vif->type != NL80211_IFTYPE_STATION) ||
172 		    data->preferred_tsf != NUM_TSF_IDS ||
173 		    !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
174 			break;
175 
176 		min_bi = min(data->vif->bss_conf.beacon_int,
177 			     vif->bss_conf.beacon_int);
178 
179 		if (!min_bi)
180 			break;
181 
182 		if ((data->vif->bss_conf.beacon_int -
183 		     vif->bss_conf.beacon_int) % min_bi == 0) {
184 			data->preferred_tsf = mvmvif->tsf_id;
185 			return;
186 		}
187 		break;
188 	default:
189 		/*
190 		 * For all other interface types there's no need to
191 		 * take drift into account. Either they're exclusive
192 		 * like IBSS and monitor, or we don't care much about
193 		 * their TSF (like P2P Device), but we won't be able
194 		 * to share the TSF resource.
195 		 */
196 		break;
197 	}
198 
199 	/*
200 	 * Unless we exited above, we can't share the TSF resource
201 	 * that the virtual interface we're iterating over is using
202 	 * with the new one, so clear the available bit and if this
203 	 * was the preferred one, reset that as well.
204 	 */
205 	__clear_bit(mvmvif->tsf_id, data->available_tsf_ids);
206 
207 	if (data->preferred_tsf == mvmvif->tsf_id)
208 		data->preferred_tsf = NUM_TSF_IDS;
209 }
210 
211 /*
212  * Get the mask of the queues used by the vif
213  */
214 u32 iwl_mvm_mac_get_queues_mask(struct ieee80211_vif *vif)
215 {
216 	u32 qmask = 0, ac;
217 
218 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
219 		return BIT(IWL_MVM_OFFCHANNEL_QUEUE);
220 
221 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
222 		if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
223 			qmask |= BIT(vif->hw_queue[ac]);
224 	}
225 
226 	if (vif->type == NL80211_IFTYPE_AP ||
227 	    vif->type == NL80211_IFTYPE_ADHOC)
228 		qmask |= BIT(vif->cab_queue);
229 
230 	return qmask;
231 }
232 
233 static void iwl_mvm_iface_hw_queues_iter(void *_data, u8 *mac,
234 					 struct ieee80211_vif *vif)
235 {
236 	struct iwl_mvm_hw_queues_iface_iterator_data *data = _data;
237 
238 	/* exclude the given vif */
239 	if (vif == data->exclude_vif)
240 		return;
241 
242 	data->used_hw_queues |= iwl_mvm_mac_get_queues_mask(vif);
243 }
244 
245 unsigned long iwl_mvm_get_used_hw_queues(struct iwl_mvm *mvm,
246 					 struct ieee80211_vif *exclude_vif)
247 {
248 	struct iwl_mvm_hw_queues_iface_iterator_data data = {
249 		.exclude_vif = exclude_vif,
250 		.used_hw_queues =
251 			BIT(IWL_MVM_OFFCHANNEL_QUEUE) |
252 			BIT(mvm->aux_queue) |
253 			BIT(IWL_MVM_DQA_GCAST_QUEUE),
254 	};
255 
256 	lockdep_assert_held(&mvm->mutex);
257 
258 	/* mark all VIF used hw queues */
259 	ieee80211_iterate_active_interfaces_atomic(
260 		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
261 		iwl_mvm_iface_hw_queues_iter, &data);
262 
263 	return data.used_hw_queues;
264 }
265 
266 static void iwl_mvm_mac_iface_iterator(void *_data, u8 *mac,
267 				       struct ieee80211_vif *vif)
268 {
269 	struct iwl_mvm_mac_iface_iterator_data *data = _data;
270 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
271 
272 	/* Iterator may already find the interface being added -- skip it */
273 	if (vif == data->vif) {
274 		data->found_vif = true;
275 		return;
276 	}
277 
278 	/* Mark MAC IDs as used by clearing the available bit, and
279 	 * (below) mark TSFs as used if their existing use is not
280 	 * compatible with the new interface type.
281 	 * No locking or atomic bit operations are needed since the
282 	 * data is on the stack of the caller function.
283 	 */
284 	__clear_bit(mvmvif->id, data->available_mac_ids);
285 
286 	/* find a suitable tsf_id */
287 	iwl_mvm_mac_tsf_id_iter(_data, mac, vif);
288 }
289 
290 void iwl_mvm_mac_ctxt_recalc_tsf_id(struct iwl_mvm *mvm,
291 				    struct ieee80211_vif *vif)
292 {
293 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
294 	struct iwl_mvm_mac_iface_iterator_data data = {
295 		.mvm = mvm,
296 		.vif = vif,
297 		.available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
298 		/* no preference yet */
299 		.preferred_tsf = NUM_TSF_IDS,
300 	};
301 
302 	ieee80211_iterate_active_interfaces_atomic(
303 		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
304 		iwl_mvm_mac_tsf_id_iter, &data);
305 
306 	if (data.preferred_tsf != NUM_TSF_IDS)
307 		mvmvif->tsf_id = data.preferred_tsf;
308 	else if (!test_bit(mvmvif->tsf_id, data.available_tsf_ids))
309 		mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
310 						NUM_TSF_IDS);
311 }
312 
313 int iwl_mvm_mac_ctxt_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
314 {
315 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
316 	struct iwl_mvm_mac_iface_iterator_data data = {
317 		.mvm = mvm,
318 		.vif = vif,
319 		.available_mac_ids = { (1 << NUM_MAC_INDEX_DRIVER) - 1 },
320 		.available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
321 		/* no preference yet */
322 		.preferred_tsf = NUM_TSF_IDS,
323 		.found_vif = false,
324 	};
325 	u32 ac;
326 	int ret, i, queue_limit;
327 	unsigned long used_hw_queues;
328 
329 	lockdep_assert_held(&mvm->mutex);
330 
331 	/*
332 	 * Allocate a MAC ID and a TSF for this MAC, along with the queues
333 	 * and other resources.
334 	 */
335 
336 	/*
337 	 * Before the iterator, we start with all MAC IDs and TSFs available.
338 	 *
339 	 * During iteration, all MAC IDs are cleared that are in use by other
340 	 * virtual interfaces, and all TSF IDs are cleared that can't be used
341 	 * by this new virtual interface because they're used by an interface
342 	 * that can't share it with the new one.
343 	 * At the same time, we check if there's a preferred TSF in the case
344 	 * that we should share it with another interface.
345 	 */
346 
347 	/* Currently, MAC ID 0 should be used only for the managed/IBSS vif */
348 	switch (vif->type) {
349 	case NL80211_IFTYPE_ADHOC:
350 		break;
351 	case NL80211_IFTYPE_STATION:
352 		if (!vif->p2p)
353 			break;
354 		/* fall through */
355 	default:
356 		__clear_bit(0, data.available_mac_ids);
357 	}
358 
359 	ieee80211_iterate_active_interfaces_atomic(
360 		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
361 		iwl_mvm_mac_iface_iterator, &data);
362 
363 	used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, vif);
364 
365 	/*
366 	 * In the case we're getting here during resume, it's similar to
367 	 * firmware restart, and with RESUME_ALL the iterator will find
368 	 * the vif being added already.
369 	 * We don't want to reassign any IDs in either case since doing
370 	 * so would probably assign different IDs (as interfaces aren't
371 	 * necessarily added in the same order), but the old IDs were
372 	 * preserved anyway, so skip ID assignment for both resume and
373 	 * recovery.
374 	 */
375 	if (data.found_vif)
376 		return 0;
377 
378 	/* Therefore, in recovery, we can't get here */
379 	if (WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
380 		return -EBUSY;
381 
382 	mvmvif->id = find_first_bit(data.available_mac_ids,
383 				    NUM_MAC_INDEX_DRIVER);
384 	if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
385 		IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
386 		ret = -EIO;
387 		goto exit_fail;
388 	}
389 
390 	if (data.preferred_tsf != NUM_TSF_IDS)
391 		mvmvif->tsf_id = data.preferred_tsf;
392 	else
393 		mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
394 						NUM_TSF_IDS);
395 	if (mvmvif->tsf_id == NUM_TSF_IDS) {
396 		IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
397 		ret = -EIO;
398 		goto exit_fail;
399 	}
400 
401 	mvmvif->color = 0;
402 
403 	INIT_LIST_HEAD(&mvmvif->time_event_data.list);
404 	mvmvif->time_event_data.id = TE_MAX;
405 
406 	/* No need to allocate data queues to P2P Device MAC.*/
407 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
408 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
409 			vif->hw_queue[ac] = IEEE80211_INVAL_HW_QUEUE;
410 
411 		return 0;
412 	}
413 
414 	/*
415 	 * queues in mac80211 almost entirely independent of
416 	 * the ones here - no real limit
417 	 */
418 	queue_limit = IEEE80211_MAX_QUEUES;
419 	BUILD_BUG_ON(IEEE80211_MAX_QUEUES >
420 		     BITS_PER_BYTE *
421 		     sizeof(mvm->hw_queue_to_mac80211[0]));
422 
423 	/*
424 	 * Find available queues, and allocate them to the ACs. When in
425 	 * DQA-mode they aren't really used, and this is done only so the
426 	 * mac80211 ieee80211_check_queues() function won't fail
427 	 */
428 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
429 		u8 queue = find_first_zero_bit(&used_hw_queues, queue_limit);
430 
431 		if (queue >= queue_limit) {
432 			IWL_ERR(mvm, "Failed to allocate queue\n");
433 			ret = -EIO;
434 			goto exit_fail;
435 		}
436 
437 		__set_bit(queue, &used_hw_queues);
438 		vif->hw_queue[ac] = queue;
439 	}
440 
441 	/* Allocate the CAB queue for softAP and GO interfaces */
442 	if (vif->type == NL80211_IFTYPE_AP ||
443 	    vif->type == NL80211_IFTYPE_ADHOC) {
444 		/*
445 		 * For TVQM this will be overwritten later with the FW assigned
446 		 * queue value (when queue is enabled).
447 		 */
448 		mvmvif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
449 		vif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
450 	} else {
451 		vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
452 	}
453 
454 	mvmvif->bcast_sta.sta_id = IWL_MVM_INVALID_STA;
455 	mvmvif->mcast_sta.sta_id = IWL_MVM_INVALID_STA;
456 	mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
457 
458 	for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
459 		mvmvif->smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
460 
461 	return 0;
462 
463 exit_fail:
464 	memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
465 	memset(vif->hw_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(vif->hw_queue));
466 	vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
467 	return ret;
468 }
469 
470 static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
471 			      struct ieee80211_vif *vif,
472 			      enum nl80211_band band,
473 			      u8 *cck_rates, u8 *ofdm_rates)
474 {
475 	struct ieee80211_supported_band *sband;
476 	unsigned long basic = vif->bss_conf.basic_rates;
477 	int lowest_present_ofdm = 100;
478 	int lowest_present_cck = 100;
479 	u8 cck = 0;
480 	u8 ofdm = 0;
481 	int i;
482 
483 	sband = mvm->hw->wiphy->bands[band];
484 
485 	for_each_set_bit(i, &basic, BITS_PER_LONG) {
486 		int hw = sband->bitrates[i].hw_value;
487 		if (hw >= IWL_FIRST_OFDM_RATE) {
488 			ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
489 			if (lowest_present_ofdm > hw)
490 				lowest_present_ofdm = hw;
491 		} else {
492 			BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
493 
494 			cck |= BIT(hw);
495 			if (lowest_present_cck > hw)
496 				lowest_present_cck = hw;
497 		}
498 	}
499 
500 	/*
501 	 * Now we've got the basic rates as bitmaps in the ofdm and cck
502 	 * variables. This isn't sufficient though, as there might not
503 	 * be all the right rates in the bitmap. E.g. if the only basic
504 	 * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
505 	 * and 6 Mbps because the 802.11-2007 standard says in 9.6:
506 	 *
507 	 *    [...] a STA responding to a received frame shall transmit
508 	 *    its Control Response frame [...] at the highest rate in the
509 	 *    BSSBasicRateSet parameter that is less than or equal to the
510 	 *    rate of the immediately previous frame in the frame exchange
511 	 *    sequence ([...]) and that is of the same modulation class
512 	 *    ([...]) as the received frame. If no rate contained in the
513 	 *    BSSBasicRateSet parameter meets these conditions, then the
514 	 *    control frame sent in response to a received frame shall be
515 	 *    transmitted at the highest mandatory rate of the PHY that is
516 	 *    less than or equal to the rate of the received frame, and
517 	 *    that is of the same modulation class as the received frame.
518 	 *
519 	 * As a consequence, we need to add all mandatory rates that are
520 	 * lower than all of the basic rates to these bitmaps.
521 	 */
522 
523 	if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
524 		ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
525 	if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
526 		ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
527 	/* 6M already there or needed so always add */
528 	ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
529 
530 	/*
531 	 * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
532 	 * Note, however:
533 	 *  - if no CCK rates are basic, it must be ERP since there must
534 	 *    be some basic rates at all, so they're OFDM => ERP PHY
535 	 *    (or we're in 5 GHz, and the cck bitmap will never be used)
536 	 *  - if 11M is a basic rate, it must be ERP as well, so add 5.5M
537 	 *  - if 5.5M is basic, 1M and 2M are mandatory
538 	 *  - if 2M is basic, 1M is mandatory
539 	 *  - if 1M is basic, that's the only valid ACK rate.
540 	 * As a consequence, it's not as complicated as it sounds, just add
541 	 * any lower rates to the ACK rate bitmap.
542 	 */
543 	if (IWL_RATE_11M_INDEX < lowest_present_cck)
544 		cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
545 	if (IWL_RATE_5M_INDEX < lowest_present_cck)
546 		cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
547 	if (IWL_RATE_2M_INDEX < lowest_present_cck)
548 		cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
549 	/* 1M already there or needed so always add */
550 	cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
551 
552 	*cck_rates = cck;
553 	*ofdm_rates = ofdm;
554 }
555 
556 static void iwl_mvm_mac_ctxt_set_ht_flags(struct iwl_mvm *mvm,
557 					 struct ieee80211_vif *vif,
558 					 struct iwl_mac_ctx_cmd *cmd)
559 {
560 	/* for both sta and ap, ht_operation_mode hold the protection_mode */
561 	u8 protection_mode = vif->bss_conf.ht_operation_mode &
562 				 IEEE80211_HT_OP_MODE_PROTECTION;
563 	/* The fw does not distinguish between ht and fat */
564 	u32 ht_flag = MAC_PROT_FLG_HT_PROT | MAC_PROT_FLG_FAT_PROT;
565 
566 	IWL_DEBUG_RATE(mvm, "protection mode set to %d\n", protection_mode);
567 	/*
568 	 * See section 9.23.3.1 of IEEE 80211-2012.
569 	 * Nongreenfield HT STAs Present is not supported.
570 	 */
571 	switch (protection_mode) {
572 	case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
573 		break;
574 	case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
575 	case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
576 		cmd->protection_flags |= cpu_to_le32(ht_flag);
577 		break;
578 	case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
579 		/* Protect when channel wider than 20MHz */
580 		if (vif->bss_conf.chandef.width > NL80211_CHAN_WIDTH_20)
581 			cmd->protection_flags |= cpu_to_le32(ht_flag);
582 		break;
583 	default:
584 		IWL_ERR(mvm, "Illegal protection mode %d\n",
585 			protection_mode);
586 		break;
587 	}
588 }
589 
590 static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
591 					struct ieee80211_vif *vif,
592 					struct iwl_mac_ctx_cmd *cmd,
593 					const u8 *bssid_override,
594 					u32 action)
595 {
596 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
597 	struct ieee80211_chanctx_conf *chanctx;
598 	bool ht_enabled = !!(vif->bss_conf.ht_operation_mode &
599 			     IEEE80211_HT_OP_MODE_PROTECTION);
600 	u8 cck_ack_rates, ofdm_ack_rates;
601 	const u8 *bssid = bssid_override ?: vif->bss_conf.bssid;
602 	int i;
603 
604 	cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
605 							    mvmvif->color));
606 	cmd->action = cpu_to_le32(action);
607 
608 	switch (vif->type) {
609 	case NL80211_IFTYPE_STATION:
610 		if (vif->p2p)
611 			cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_STA);
612 		else
613 			cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_BSS_STA);
614 		break;
615 	case NL80211_IFTYPE_AP:
616 		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_GO);
617 		break;
618 	case NL80211_IFTYPE_MONITOR:
619 		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_LISTENER);
620 		break;
621 	case NL80211_IFTYPE_P2P_DEVICE:
622 		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_DEVICE);
623 		break;
624 	case NL80211_IFTYPE_ADHOC:
625 		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_IBSS);
626 		break;
627 	default:
628 		WARN_ON_ONCE(1);
629 	}
630 
631 	cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
632 
633 	memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
634 
635 	if (bssid)
636 		memcpy(cmd->bssid_addr, bssid, ETH_ALEN);
637 	else
638 		eth_broadcast_addr(cmd->bssid_addr);
639 
640 	rcu_read_lock();
641 	chanctx = rcu_dereference(vif->chanctx_conf);
642 	iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
643 					    : NL80211_BAND_2GHZ,
644 			  &cck_ack_rates, &ofdm_ack_rates);
645 	rcu_read_unlock();
646 
647 	cmd->cck_rates = cpu_to_le32((u32)cck_ack_rates);
648 	cmd->ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
649 
650 	cmd->cck_short_preamble =
651 		cpu_to_le32(vif->bss_conf.use_short_preamble ?
652 			    MAC_FLG_SHORT_PREAMBLE : 0);
653 	cmd->short_slot =
654 		cpu_to_le32(vif->bss_conf.use_short_slot ?
655 			    MAC_FLG_SHORT_SLOT : 0);
656 
657 	cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
658 
659 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
660 		u8 txf = iwl_mvm_mac_ac_to_tx_fifo(mvm, i);
661 
662 		cmd->ac[txf].cw_min =
663 			cpu_to_le16(mvmvif->queue_params[i].cw_min);
664 		cmd->ac[txf].cw_max =
665 			cpu_to_le16(mvmvif->queue_params[i].cw_max);
666 		cmd->ac[txf].edca_txop =
667 			cpu_to_le16(mvmvif->queue_params[i].txop * 32);
668 		cmd->ac[txf].aifsn = mvmvif->queue_params[i].aifs;
669 		cmd->ac[txf].fifos_mask = BIT(txf);
670 	}
671 
672 	if (vif->bss_conf.qos)
673 		cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
674 
675 	if (vif->bss_conf.use_cts_prot)
676 		cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
677 
678 	IWL_DEBUG_RATE(mvm, "use_cts_prot %d, ht_operation_mode %d\n",
679 		       vif->bss_conf.use_cts_prot,
680 		       vif->bss_conf.ht_operation_mode);
681 	if (vif->bss_conf.chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
682 		cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
683 	if (ht_enabled)
684 		iwl_mvm_mac_ctxt_set_ht_flags(mvm, vif, cmd);
685 }
686 
687 static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
688 				     struct iwl_mac_ctx_cmd *cmd)
689 {
690 	int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
691 				       sizeof(*cmd), cmd);
692 	if (ret)
693 		IWL_ERR(mvm, "Failed to send MAC context (action:%d): %d\n",
694 			le32_to_cpu(cmd->action), ret);
695 	return ret;
696 }
697 
698 static int iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm *mvm,
699 				    struct ieee80211_vif *vif,
700 				    u32 action, bool force_assoc_off,
701 				    const u8 *bssid_override)
702 {
703 	struct iwl_mac_ctx_cmd cmd = {};
704 	struct iwl_mac_data_sta *ctxt_sta;
705 
706 	WARN_ON(vif->type != NL80211_IFTYPE_STATION);
707 
708 	/* Fill the common data for all mac context types */
709 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, bssid_override, action);
710 
711 	if (vif->p2p) {
712 		struct ieee80211_p2p_noa_attr *noa =
713 			&vif->bss_conf.p2p_noa_attr;
714 
715 		cmd.p2p_sta.ctwin = cpu_to_le32(noa->oppps_ctwindow &
716 					IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
717 		ctxt_sta = &cmd.p2p_sta.sta;
718 	} else {
719 		ctxt_sta = &cmd.sta;
720 	}
721 
722 	/* We need the dtim_period to set the MAC as associated */
723 	if (vif->bss_conf.assoc && vif->bss_conf.dtim_period &&
724 	    !force_assoc_off) {
725 		u32 dtim_offs;
726 
727 		/*
728 		 * The DTIM count counts down, so when it is N that means N
729 		 * more beacon intervals happen until the DTIM TBTT. Therefore
730 		 * add this to the current time. If that ends up being in the
731 		 * future, the firmware will handle it.
732 		 *
733 		 * Also note that the system_timestamp (which we get here as
734 		 * "sync_device_ts") and TSF timestamp aren't at exactly the
735 		 * same offset in the frame -- the TSF is at the first symbol
736 		 * of the TSF, the system timestamp is at signal acquisition
737 		 * time. This means there's an offset between them of at most
738 		 * a few hundred microseconds (24 * 8 bits + PLCP time gives
739 		 * 384us in the longest case), this is currently not relevant
740 		 * as the firmware wakes up around 2ms before the TBTT.
741 		 */
742 		dtim_offs = vif->bss_conf.sync_dtim_count *
743 				vif->bss_conf.beacon_int;
744 		/* convert TU to usecs */
745 		dtim_offs *= 1024;
746 
747 		ctxt_sta->dtim_tsf =
748 			cpu_to_le64(vif->bss_conf.sync_tsf + dtim_offs);
749 		ctxt_sta->dtim_time =
750 			cpu_to_le32(vif->bss_conf.sync_device_ts + dtim_offs);
751 		ctxt_sta->assoc_beacon_arrive_time =
752 			cpu_to_le32(vif->bss_conf.sync_device_ts);
753 
754 		IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
755 			       le64_to_cpu(ctxt_sta->dtim_tsf),
756 			       le32_to_cpu(ctxt_sta->dtim_time),
757 			       dtim_offs);
758 
759 		ctxt_sta->is_assoc = cpu_to_le32(1);
760 	} else {
761 		ctxt_sta->is_assoc = cpu_to_le32(0);
762 
763 		/* Allow beacons to pass through as long as we are not
764 		 * associated, or we do not have dtim period information.
765 		 */
766 		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
767 	}
768 
769 	ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
770 	ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
771 					      vif->bss_conf.dtim_period);
772 
773 	ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
774 	ctxt_sta->assoc_id = cpu_to_le32(vif->bss_conf.aid);
775 
776 	if (vif->probe_req_reg && vif->bss_conf.assoc && vif->p2p)
777 		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
778 
779 	if (vif->bss_conf.assoc && vif->bss_conf.he_support &&
780 	    !iwlwifi_mod_params.disable_11ax) {
781 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
782 		u8 sta_id = mvmvif->ap_sta_id;
783 
784 		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_11AX);
785 		if (sta_id != IWL_MVM_INVALID_STA) {
786 			struct ieee80211_sta *sta;
787 
788 			sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
789 				lockdep_is_held(&mvm->mutex));
790 
791 			/*
792 			 * TODO: we should check the ext cap IE but it is
793 			 * unclear why the spec requires two bits (one in HE
794 			 * cap IE, and one in the ext cap IE). In the meantime
795 			 * rely on the HE cap IE only.
796 			 */
797 			if (sta && (sta->he_cap.he_cap_elem.mac_cap_info[0] &
798 				    IEEE80211_HE_MAC_CAP0_TWT_RES))
799 				ctxt_sta->data_policy |=
800 					cpu_to_le32(TWT_SUPPORTED);
801 		}
802 	}
803 
804 
805 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
806 }
807 
808 static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
809 					 struct ieee80211_vif *vif,
810 					 u32 action)
811 {
812 	struct iwl_mac_ctx_cmd cmd = {};
813 	u32 tfd_queue_msk = BIT(mvm->snif_queue);
814 	int ret;
815 
816 	WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
817 
818 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
819 
820 	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
821 				       MAC_FILTER_IN_CONTROL_AND_MGMT |
822 				       MAC_FILTER_IN_BEACON |
823 				       MAC_FILTER_IN_PROBE_REQUEST |
824 				       MAC_FILTER_IN_CRC32);
825 	ieee80211_hw_set(mvm->hw, RX_INCLUDES_FCS);
826 
827 	/* Allocate sniffer station */
828 	ret = iwl_mvm_allocate_int_sta(mvm, &mvm->snif_sta, tfd_queue_msk,
829 				       vif->type, IWL_STA_GENERAL_PURPOSE);
830 	if (ret)
831 		return ret;
832 
833 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
834 }
835 
836 static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
837 				     struct ieee80211_vif *vif,
838 				     u32 action)
839 {
840 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
841 	struct iwl_mac_ctx_cmd cmd = {};
842 
843 	WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
844 
845 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
846 
847 	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
848 				       MAC_FILTER_IN_PROBE_REQUEST);
849 
850 	/* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */
851 	cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
852 
853 	/* TODO: Assumes that the beacon id == mac context id */
854 	cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
855 
856 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
857 }
858 
859 struct iwl_mvm_go_iterator_data {
860 	bool go_active;
861 };
862 
863 static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
864 {
865 	struct iwl_mvm_go_iterator_data *data = _data;
866 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
867 
868 	if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
869 	    mvmvif->ap_ibss_active)
870 		data->go_active = true;
871 }
872 
873 static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
874 					   struct ieee80211_vif *vif,
875 					   u32 action)
876 {
877 	struct iwl_mac_ctx_cmd cmd = {};
878 	struct iwl_mvm_go_iterator_data data = {};
879 
880 	WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
881 
882 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
883 
884 	cmd.protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
885 
886 	/* Override the filter flags to accept only probe requests */
887 	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
888 
889 	/*
890 	 * This flag should be set to true when the P2P Device is
891 	 * discoverable and there is at least another active P2P GO. Settings
892 	 * this flag will allow the P2P Device to be discoverable on other
893 	 * channels in addition to its listen channel.
894 	 * Note that this flag should not be set in other cases as it opens the
895 	 * Rx filters on all MAC and increases the number of interrupts.
896 	 */
897 	ieee80211_iterate_active_interfaces_atomic(
898 		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
899 		iwl_mvm_go_iterator, &data);
900 
901 	cmd.p2p_dev.is_disc_extended = cpu_to_le32(data.go_active ? 1 : 0);
902 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
903 }
904 
905 static void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
906 				     __le32 *tim_index, __le32 *tim_size,
907 				     u8 *beacon, u32 frame_size)
908 {
909 	u32 tim_idx;
910 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
911 
912 	/* The index is relative to frame start but we start looking at the
913 	 * variable-length part of the beacon. */
914 	tim_idx = mgmt->u.beacon.variable - beacon;
915 
916 	/* Parse variable-length elements of beacon to find WLAN_EID_TIM */
917 	while ((tim_idx < (frame_size - 2)) &&
918 			(beacon[tim_idx] != WLAN_EID_TIM))
919 		tim_idx += beacon[tim_idx+1] + 2;
920 
921 	/* If TIM field was found, set variables */
922 	if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
923 		*tim_index = cpu_to_le32(tim_idx);
924 		*tim_size = cpu_to_le32((u32)beacon[tim_idx + 1]);
925 	} else {
926 		IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
927 	}
928 }
929 
930 static u32 iwl_mvm_find_ie_offset(u8 *beacon, u8 eid, u32 frame_size)
931 {
932 	struct ieee80211_mgmt *mgmt = (void *)beacon;
933 	const u8 *ie;
934 
935 	if (WARN_ON_ONCE(frame_size <= (mgmt->u.beacon.variable - beacon)))
936 		return 0;
937 
938 	frame_size -= mgmt->u.beacon.variable - beacon;
939 
940 	ie = cfg80211_find_ie(eid, mgmt->u.beacon.variable, frame_size);
941 	if (!ie)
942 		return 0;
943 
944 	return ie - beacon;
945 }
946 
947 static u8 iwl_mvm_mac_ctxt_get_lowest_rate(struct ieee80211_tx_info *info,
948 					   struct ieee80211_vif *vif)
949 {
950 	u8 rate;
951 
952 	if (info->band == NL80211_BAND_5GHZ || vif->p2p)
953 		rate = IWL_FIRST_OFDM_RATE;
954 	else
955 		rate = IWL_FIRST_CCK_RATE;
956 
957 	return rate;
958 }
959 
960 static void iwl_mvm_mac_ctxt_set_tx(struct iwl_mvm *mvm,
961 				    struct ieee80211_vif *vif,
962 				    struct sk_buff *beacon,
963 				    struct iwl_tx_cmd *tx)
964 {
965 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
966 	struct ieee80211_tx_info *info;
967 	u8 rate;
968 	u32 tx_flags;
969 
970 	info = IEEE80211_SKB_CB(beacon);
971 
972 	/* Set up TX command fields */
973 	tx->len = cpu_to_le16((u16)beacon->len);
974 	tx->sta_id = mvmvif->bcast_sta.sta_id;
975 	tx->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
976 	tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;
977 	tx_flags |=
978 		iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<
979 						TX_CMD_FLG_BT_PRIO_POS;
980 	tx->tx_flags = cpu_to_le32(tx_flags);
981 
982 	if (!fw_has_capa(&mvm->fw->ucode_capa,
983 			 IWL_UCODE_TLV_CAPA_BEACON_ANT_SELECTION))
984 		iwl_mvm_toggle_tx_ant(mvm, &mvm->mgmt_last_antenna_idx);
985 
986 	tx->rate_n_flags =
987 		cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
988 			    RATE_MCS_ANT_POS);
989 
990 	rate = iwl_mvm_mac_ctxt_get_lowest_rate(info, vif);
991 
992 	tx->rate_n_flags |= cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(rate));
993 	if (rate == IWL_FIRST_CCK_RATE)
994 		tx->rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK);
995 
996 }
997 
998 static int iwl_mvm_mac_ctxt_send_beacon_cmd(struct iwl_mvm *mvm,
999 					    struct sk_buff *beacon,
1000 					    void *data, int len)
1001 {
1002 	struct iwl_host_cmd cmd = {
1003 		.id = BEACON_TEMPLATE_CMD,
1004 		.flags = CMD_ASYNC,
1005 	};
1006 
1007 	cmd.len[0] = len;
1008 	cmd.data[0] = data;
1009 	cmd.dataflags[0] = 0;
1010 	cmd.len[1] = beacon->len;
1011 	cmd.data[1] = beacon->data;
1012 	cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
1013 
1014 	return iwl_mvm_send_cmd(mvm, &cmd);
1015 }
1016 
1017 static int iwl_mvm_mac_ctxt_send_beacon_v6(struct iwl_mvm *mvm,
1018 					   struct ieee80211_vif *vif,
1019 					   struct sk_buff *beacon)
1020 {
1021 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1022 	struct iwl_mac_beacon_cmd_v6 beacon_cmd = {};
1023 
1024 	iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
1025 
1026 	beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
1027 
1028 	if (vif->type == NL80211_IFTYPE_AP)
1029 		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
1030 					 &beacon_cmd.tim_size,
1031 					 beacon->data, beacon->len);
1032 
1033 	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
1034 						sizeof(beacon_cmd));
1035 }
1036 
1037 static int iwl_mvm_mac_ctxt_send_beacon_v7(struct iwl_mvm *mvm,
1038 					   struct ieee80211_vif *vif,
1039 					   struct sk_buff *beacon)
1040 {
1041 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1042 	struct iwl_mac_beacon_cmd_v7 beacon_cmd = {};
1043 
1044 	iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
1045 
1046 	beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
1047 
1048 	if (vif->type == NL80211_IFTYPE_AP)
1049 		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
1050 					 &beacon_cmd.tim_size,
1051 					 beacon->data, beacon->len);
1052 
1053 	beacon_cmd.csa_offset =
1054 		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1055 						   WLAN_EID_CHANNEL_SWITCH,
1056 						   beacon->len));
1057 	beacon_cmd.ecsa_offset =
1058 		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1059 						   WLAN_EID_EXT_CHANSWITCH_ANN,
1060 						   beacon->len));
1061 
1062 	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
1063 						sizeof(beacon_cmd));
1064 }
1065 
1066 static int iwl_mvm_mac_ctxt_send_beacon_v9(struct iwl_mvm *mvm,
1067 					   struct ieee80211_vif *vif,
1068 					   struct sk_buff *beacon)
1069 {
1070 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1071 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(beacon);
1072 	struct iwl_mac_beacon_cmd beacon_cmd = {};
1073 	u8 rate = iwl_mvm_mac_ctxt_get_lowest_rate(info, vif);
1074 	u16 flags;
1075 
1076 	flags = iwl_mvm_mac80211_idx_to_hwrate(rate);
1077 
1078 	if (rate == IWL_FIRST_CCK_RATE)
1079 		flags |= IWL_MAC_BEACON_CCK;
1080 
1081 	beacon_cmd.flags = cpu_to_le16(flags);
1082 	beacon_cmd.byte_cnt = cpu_to_le16((u16)beacon->len);
1083 	beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
1084 
1085 	if (vif->type == NL80211_IFTYPE_AP)
1086 		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
1087 					 &beacon_cmd.tim_size,
1088 					 beacon->data, beacon->len);
1089 
1090 	beacon_cmd.csa_offset =
1091 		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1092 						   WLAN_EID_CHANNEL_SWITCH,
1093 						   beacon->len));
1094 	beacon_cmd.ecsa_offset =
1095 		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1096 						   WLAN_EID_EXT_CHANSWITCH_ANN,
1097 						   beacon->len));
1098 
1099 	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
1100 						sizeof(beacon_cmd));
1101 }
1102 
1103 static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
1104 					struct ieee80211_vif *vif,
1105 					struct sk_buff *beacon)
1106 {
1107 	if (WARN_ON(!beacon))
1108 		return -EINVAL;
1109 
1110 	if (!fw_has_capa(&mvm->fw->ucode_capa,
1111 			 IWL_UCODE_TLV_CAPA_CSA_AND_TBTT_OFFLOAD))
1112 		return iwl_mvm_mac_ctxt_send_beacon_v6(mvm, vif, beacon);
1113 
1114 	if (fw_has_api(&mvm->fw->ucode_capa,
1115 		       IWL_UCODE_TLV_API_NEW_BEACON_TEMPLATE))
1116 		return iwl_mvm_mac_ctxt_send_beacon_v9(mvm, vif, beacon);
1117 
1118 	return iwl_mvm_mac_ctxt_send_beacon_v7(mvm, vif, beacon);
1119 }
1120 
1121 /* The beacon template for the AP/GO/IBSS has changed and needs update */
1122 int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
1123 				    struct ieee80211_vif *vif)
1124 {
1125 	struct sk_buff *beacon;
1126 	int ret;
1127 
1128 	WARN_ON(vif->type != NL80211_IFTYPE_AP &&
1129 		vif->type != NL80211_IFTYPE_ADHOC);
1130 
1131 	beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL);
1132 	if (!beacon)
1133 		return -ENOMEM;
1134 
1135 	ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
1136 	dev_kfree_skb(beacon);
1137 	return ret;
1138 }
1139 
1140 struct iwl_mvm_mac_ap_iterator_data {
1141 	struct iwl_mvm *mvm;
1142 	struct ieee80211_vif *vif;
1143 	u32 beacon_device_ts;
1144 	u16 beacon_int;
1145 };
1146 
1147 /* Find the beacon_device_ts and beacon_int for a managed interface */
1148 static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
1149 				    struct ieee80211_vif *vif)
1150 {
1151 	struct iwl_mvm_mac_ap_iterator_data *data = _data;
1152 
1153 	if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc)
1154 		return;
1155 
1156 	/* Station client has higher priority over P2P client*/
1157 	if (vif->p2p && data->beacon_device_ts)
1158 		return;
1159 
1160 	data->beacon_device_ts = vif->bss_conf.sync_device_ts;
1161 	data->beacon_int = vif->bss_conf.beacon_int;
1162 }
1163 
1164 /*
1165  * Fill the specific data for mac context of type AP of P2P GO
1166  */
1167 static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
1168 					 struct ieee80211_vif *vif,
1169 					 struct iwl_mac_ctx_cmd *cmd,
1170 					 struct iwl_mac_data_ap *ctxt_ap,
1171 					 bool add)
1172 {
1173 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1174 	struct iwl_mvm_mac_ap_iterator_data data = {
1175 		.mvm = mvm,
1176 		.vif = vif,
1177 		.beacon_device_ts = 0
1178 	};
1179 
1180 	/* in AP mode, the MCAST FIFO takes the EDCA params from VO */
1181 	cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |= BIT(IWL_MVM_TX_FIFO_MCAST);
1182 
1183 	/*
1184 	 * in AP mode, pass probe requests and beacons from other APs
1185 	 * (needed for ht protection); when there're no any associated
1186 	 * station don't ask FW to pass beacons to prevent unnecessary
1187 	 * wake-ups.
1188 	 */
1189 	cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
1190 	if (mvmvif->ap_assoc_sta_count || !mvm->drop_bcn_ap_mode) {
1191 		cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
1192 		IWL_DEBUG_HC(mvm, "Asking FW to pass beacons\n");
1193 	} else {
1194 		IWL_DEBUG_HC(mvm, "No need to receive beacons\n");
1195 	}
1196 
1197 	if (vif->bss_conf.he_support && !iwlwifi_mod_params.disable_11ax)
1198 		cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_11AX);
1199 
1200 	ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
1201 	ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
1202 					     vif->bss_conf.dtim_period);
1203 
1204 	if (!fw_has_api(&mvm->fw->ucode_capa,
1205 			IWL_UCODE_TLV_API_STA_TYPE))
1206 		ctxt_ap->mcast_qid = cpu_to_le32(vif->cab_queue);
1207 
1208 	/*
1209 	 * Only set the beacon time when the MAC is being added, when we
1210 	 * just modify the MAC then we should keep the time -- the firmware
1211 	 * can otherwise have a "jumping" TBTT.
1212 	 */
1213 	if (add) {
1214 		/*
1215 		 * If there is a station/P2P client interface which is
1216 		 * associated, set the AP's TBTT far enough from the station's
1217 		 * TBTT. Otherwise, set it to the current system time
1218 		 */
1219 		ieee80211_iterate_active_interfaces_atomic(
1220 			mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1221 			iwl_mvm_mac_ap_iterator, &data);
1222 
1223 		if (data.beacon_device_ts) {
1224 			u32 rand = (prandom_u32() % (64 - 36)) + 36;
1225 			mvmvif->ap_beacon_time = data.beacon_device_ts +
1226 				ieee80211_tu_to_usec(data.beacon_int * rand /
1227 						     100);
1228 		} else {
1229 			mvmvif->ap_beacon_time =
1230 				iwl_read_prph(mvm->trans,
1231 					      DEVICE_SYSTEM_TIME_REG);
1232 		}
1233 	}
1234 
1235 	ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
1236 	ctxt_ap->beacon_tsf = 0; /* unused */
1237 
1238 	/* TODO: Assume that the beacon id == mac context id */
1239 	ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
1240 }
1241 
1242 static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
1243 				   struct ieee80211_vif *vif,
1244 				   u32 action)
1245 {
1246 	struct iwl_mac_ctx_cmd cmd = {};
1247 
1248 	WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
1249 
1250 	/* Fill the common data for all mac context types */
1251 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1252 
1253 	/* Fill the data specific for ap mode */
1254 	iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.ap,
1255 				     action == FW_CTXT_ACTION_ADD);
1256 
1257 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1258 }
1259 
1260 static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
1261 				   struct ieee80211_vif *vif,
1262 				   u32 action)
1263 {
1264 	struct iwl_mac_ctx_cmd cmd = {};
1265 	struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
1266 
1267 	WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
1268 
1269 	/* Fill the common data for all mac context types */
1270 	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1271 
1272 	/* Fill the data specific for GO mode */
1273 	iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.go.ap,
1274 				     action == FW_CTXT_ACTION_ADD);
1275 
1276 	cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
1277 					IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
1278 	cmd.go.opp_ps_enabled =
1279 			cpu_to_le32(!!(noa->oppps_ctwindow &
1280 					IEEE80211_P2P_OPPPS_ENABLE_BIT));
1281 
1282 	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1283 }
1284 
1285 static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1286 				u32 action, bool force_assoc_off,
1287 				const u8 *bssid_override)
1288 {
1289 	switch (vif->type) {
1290 	case NL80211_IFTYPE_STATION:
1291 		return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
1292 						force_assoc_off,
1293 						bssid_override);
1294 		break;
1295 	case NL80211_IFTYPE_AP:
1296 		if (!vif->p2p)
1297 			return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
1298 		else
1299 			return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
1300 		break;
1301 	case NL80211_IFTYPE_MONITOR:
1302 		return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
1303 	case NL80211_IFTYPE_P2P_DEVICE:
1304 		return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
1305 	case NL80211_IFTYPE_ADHOC:
1306 		return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
1307 	default:
1308 		break;
1309 	}
1310 
1311 	return -EOPNOTSUPP;
1312 }
1313 
1314 int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1315 {
1316 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1317 	int ret;
1318 
1319 	if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
1320 		      vif->addr, ieee80211_vif_type_p2p(vif)))
1321 		return -EIO;
1322 
1323 	ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
1324 				   true, NULL);
1325 	if (ret)
1326 		return ret;
1327 
1328 	/* will only do anything at resume from D3 time */
1329 	iwl_mvm_set_last_nonqos_seq(mvm, vif);
1330 
1331 	mvmvif->uploaded = true;
1332 	return 0;
1333 }
1334 
1335 int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1336 			     bool force_assoc_off, const u8 *bssid_override)
1337 {
1338 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1339 
1340 	if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
1341 		      vif->addr, ieee80211_vif_type_p2p(vif)))
1342 		return -EIO;
1343 
1344 	return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
1345 				    force_assoc_off, bssid_override);
1346 }
1347 
1348 int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1349 {
1350 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1351 	struct iwl_mac_ctx_cmd cmd;
1352 	int ret;
1353 
1354 	if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1355 		      vif->addr, ieee80211_vif_type_p2p(vif)))
1356 		return -EIO;
1357 
1358 	memset(&cmd, 0, sizeof(cmd));
1359 
1360 	cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1361 							   mvmvif->color));
1362 	cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1363 
1364 	ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
1365 				   sizeof(cmd), &cmd);
1366 	if (ret) {
1367 		IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
1368 		return ret;
1369 	}
1370 
1371 	mvmvif->uploaded = false;
1372 
1373 	if (vif->type == NL80211_IFTYPE_MONITOR) {
1374 		__clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, mvm->hw->flags);
1375 		iwl_mvm_dealloc_snif_sta(mvm);
1376 	}
1377 
1378 	return 0;
1379 }
1380 
1381 static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
1382 				   struct ieee80211_vif *csa_vif, u32 gp2,
1383 				   bool tx_success)
1384 {
1385 	struct iwl_mvm_vif *mvmvif =
1386 			iwl_mvm_vif_from_mac80211(csa_vif);
1387 
1388 	/* Don't start to countdown from a failed beacon */
1389 	if (!tx_success && !mvmvif->csa_countdown)
1390 		return;
1391 
1392 	mvmvif->csa_countdown = true;
1393 
1394 	if (!ieee80211_csa_is_complete(csa_vif)) {
1395 		int c = ieee80211_csa_update_counter(csa_vif);
1396 
1397 		iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif);
1398 		if (csa_vif->p2p &&
1399 		    !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2 &&
1400 		    tx_success) {
1401 			u32 rel_time = (c + 1) *
1402 				       csa_vif->bss_conf.beacon_int -
1403 				       IWL_MVM_CHANNEL_SWITCH_TIME_GO;
1404 			u32 apply_time = gp2 + rel_time * 1024;
1405 
1406 			iwl_mvm_schedule_csa_period(mvm, csa_vif,
1407 					 IWL_MVM_CHANNEL_SWITCH_TIME_GO -
1408 					 IWL_MVM_CHANNEL_SWITCH_MARGIN,
1409 					 apply_time);
1410 		}
1411 	} else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
1412 		/* we don't have CSA NoA scheduled yet, switch now */
1413 		ieee80211_csa_finish(csa_vif);
1414 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
1415 	}
1416 }
1417 
1418 void iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1419 			     struct iwl_rx_cmd_buffer *rxb)
1420 {
1421 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1422 	struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
1423 	struct iwl_mvm_tx_resp *beacon_notify_hdr;
1424 	struct ieee80211_vif *csa_vif;
1425 	struct ieee80211_vif *tx_blocked_vif;
1426 	struct agg_tx_status *agg_status;
1427 	u16 status;
1428 
1429 	lockdep_assert_held(&mvm->mutex);
1430 
1431 	beacon_notify_hdr = &beacon->beacon_notify_hdr;
1432 	mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
1433 	mvm->ibss_manager = beacon->ibss_mgr_status != 0;
1434 
1435 	agg_status = iwl_mvm_get_agg_status(mvm, beacon_notify_hdr);
1436 	status = le16_to_cpu(agg_status->status) & TX_STATUS_MSK;
1437 	IWL_DEBUG_RX(mvm,
1438 		     "beacon status %#x retries:%d tsf:0x%16llX gp2:0x%X rate:%d\n",
1439 		     status, beacon_notify_hdr->failure_frame,
1440 		     le64_to_cpu(beacon->tsf),
1441 		     mvm->ap_last_beacon_gp2,
1442 		     le32_to_cpu(beacon_notify_hdr->initial_rate));
1443 
1444 	csa_vif = rcu_dereference_protected(mvm->csa_vif,
1445 					    lockdep_is_held(&mvm->mutex));
1446 	if (unlikely(csa_vif && csa_vif->csa_active))
1447 		iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2,
1448 				       (status == TX_STATUS_SUCCESS));
1449 
1450 	tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
1451 						lockdep_is_held(&mvm->mutex));
1452 	if (unlikely(tx_blocked_vif)) {
1453 		struct iwl_mvm_vif *mvmvif =
1454 			iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1455 
1456 		/*
1457 		 * The channel switch is started and we have blocked the
1458 		 * stations. If this is the first beacon (the timeout wasn't
1459 		 * set), set the unblock timeout, otherwise countdown
1460 		 */
1461 		if (!mvm->csa_tx_block_bcn_timeout)
1462 			mvm->csa_tx_block_bcn_timeout =
1463 				IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
1464 		else
1465 			mvm->csa_tx_block_bcn_timeout--;
1466 
1467 		/* Check if the timeout is expired, and unblock tx */
1468 		if (mvm->csa_tx_block_bcn_timeout == 0) {
1469 			iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
1470 			RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1471 		}
1472 	}
1473 }
1474 
1475 static void iwl_mvm_beacon_loss_iterator(void *_data, u8 *mac,
1476 					 struct ieee80211_vif *vif)
1477 {
1478 	struct iwl_missed_beacons_notif *missed_beacons = _data;
1479 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1480 	struct iwl_mvm *mvm = mvmvif->mvm;
1481 	struct iwl_fw_dbg_trigger_missed_bcon *bcon_trig;
1482 	struct iwl_fw_dbg_trigger_tlv *trigger;
1483 	u32 stop_trig_missed_bcon, stop_trig_missed_bcon_since_rx;
1484 	u32 rx_missed_bcon, rx_missed_bcon_since_rx;
1485 
1486 	if (mvmvif->id != (u16)le32_to_cpu(missed_beacons->mac_id))
1487 		return;
1488 
1489 	rx_missed_bcon = le32_to_cpu(missed_beacons->consec_missed_beacons);
1490 	rx_missed_bcon_since_rx =
1491 		le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx);
1492 	/*
1493 	 * TODO: the threshold should be adjusted based on latency conditions,
1494 	 * and/or in case of a CS flow on one of the other AP vifs.
1495 	 */
1496 	if (le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx) >
1497 	     IWL_MVM_MISSED_BEACONS_THRESHOLD)
1498 		ieee80211_beacon_loss(vif);
1499 
1500 	trigger = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
1501 					FW_DBG_TRIGGER_MISSED_BEACONS);
1502 	if (!trigger)
1503 		return;
1504 
1505 	bcon_trig = (void *)trigger->data;
1506 	stop_trig_missed_bcon = le32_to_cpu(bcon_trig->stop_consec_missed_bcon);
1507 	stop_trig_missed_bcon_since_rx =
1508 		le32_to_cpu(bcon_trig->stop_consec_missed_bcon_since_rx);
1509 
1510 	/* TODO: implement start trigger */
1511 
1512 	if (rx_missed_bcon_since_rx >= stop_trig_missed_bcon_since_rx ||
1513 	    rx_missed_bcon >= stop_trig_missed_bcon)
1514 		iwl_fw_dbg_collect_trig(&mvm->fwrt, trigger, NULL);
1515 }
1516 
1517 void iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
1518 				     struct iwl_rx_cmd_buffer *rxb)
1519 {
1520 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1521 	struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
1522 
1523 	IWL_DEBUG_INFO(mvm,
1524 		       "missed bcn mac_id=%u, consecutive=%u (%u, %u, %u)\n",
1525 		       le32_to_cpu(mb->mac_id),
1526 		       le32_to_cpu(mb->consec_missed_beacons),
1527 		       le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
1528 		       le32_to_cpu(mb->num_recvd_beacons),
1529 		       le32_to_cpu(mb->num_expected_beacons));
1530 
1531 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
1532 						   IEEE80211_IFACE_ITER_NORMAL,
1533 						   iwl_mvm_beacon_loss_iterator,
1534 						   mb);
1535 
1536 	iwl_fw_dbg_apply_point(&mvm->fwrt, IWL_FW_INI_APPLY_MISSED_BEACONS);
1537 }
1538 
1539 void iwl_mvm_rx_stored_beacon_notif(struct iwl_mvm *mvm,
1540 				    struct iwl_rx_cmd_buffer *rxb)
1541 {
1542 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1543 	struct iwl_stored_beacon_notif *sb = (void *)pkt->data;
1544 	struct ieee80211_rx_status rx_status;
1545 	struct sk_buff *skb;
1546 	u32 size = le32_to_cpu(sb->byte_count);
1547 
1548 	if (size == 0)
1549 		return;
1550 
1551 	skb = alloc_skb(size, GFP_ATOMIC);
1552 	if (!skb) {
1553 		IWL_ERR(mvm, "alloc_skb failed\n");
1554 		return;
1555 	}
1556 
1557 	/* update rx_status according to the notification's metadata */
1558 	memset(&rx_status, 0, sizeof(rx_status));
1559 	rx_status.mactime = le64_to_cpu(sb->tsf);
1560 	/* TSF as indicated by the firmware  is at INA time */
1561 	rx_status.flag |= RX_FLAG_MACTIME_PLCP_START;
1562 	rx_status.device_timestamp = le32_to_cpu(sb->system_time);
1563 	rx_status.band =
1564 		(sb->band & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
1565 				NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
1566 	rx_status.freq =
1567 		ieee80211_channel_to_frequency(le16_to_cpu(sb->channel),
1568 					       rx_status.band);
1569 
1570 	/* copy the data */
1571 	skb_put_data(skb, sb->data, size);
1572 	memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
1573 
1574 	/* pass it as regular rx to mac80211 */
1575 	ieee80211_rx_napi(mvm->hw, NULL, skb, NULL);
1576 }
1577 
1578 static void iwl_mvm_probe_resp_data_iter(void *_data, u8 *mac,
1579 					 struct ieee80211_vif *vif)
1580 {
1581 	struct iwl_probe_resp_data_notif *notif = _data;
1582 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1583 	struct iwl_probe_resp_data *old_data, *new_data;
1584 
1585 	if (mvmvif->id != (u16)le32_to_cpu(notif->mac_id))
1586 		return;
1587 
1588 	new_data = kzalloc(sizeof(*new_data), GFP_KERNEL);
1589 	if (!new_data)
1590 		return;
1591 
1592 	memcpy(&new_data->notif, notif, sizeof(new_data->notif));
1593 
1594 	/* noa_attr contains 1 reserved byte, need to substruct it */
1595 	new_data->noa_len = sizeof(struct ieee80211_vendor_ie) +
1596 			    sizeof(new_data->notif.noa_attr) - 1;
1597 
1598 	/*
1599 	 * If it's a one time NoA, only one descriptor is needed,
1600 	 * adjust the length according to len_low.
1601 	 */
1602 	if (new_data->notif.noa_attr.len_low ==
1603 	    sizeof(struct ieee80211_p2p_noa_desc) + 2)
1604 		new_data->noa_len -= sizeof(struct ieee80211_p2p_noa_desc);
1605 
1606 	old_data = rcu_dereference_protected(mvmvif->probe_resp_data,
1607 					lockdep_is_held(&mvmvif->mvm->mutex));
1608 	rcu_assign_pointer(mvmvif->probe_resp_data, new_data);
1609 
1610 	if (old_data)
1611 		kfree_rcu(old_data, rcu_head);
1612 
1613 	if (notif->csa_counter != IWL_PROBE_RESP_DATA_NO_CSA &&
1614 	    notif->csa_counter >= 1)
1615 		ieee80211_csa_set_counter(vif, notif->csa_counter);
1616 }
1617 
1618 void iwl_mvm_probe_resp_data_notif(struct iwl_mvm *mvm,
1619 				   struct iwl_rx_cmd_buffer *rxb)
1620 {
1621 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1622 	struct iwl_probe_resp_data_notif *notif = (void *)pkt->data;
1623 	int len = iwl_rx_packet_payload_len(pkt);
1624 
1625 	if (WARN_ON_ONCE(len < sizeof(*notif)))
1626 		return;
1627 
1628 	IWL_DEBUG_INFO(mvm, "Probe response data notif: noa %d, csa %d\n",
1629 		       notif->noa_active, notif->csa_counter);
1630 
1631 	ieee80211_iterate_active_interfaces(mvm->hw,
1632 					    IEEE80211_IFACE_ITER_ACTIVE,
1633 					    iwl_mvm_probe_resp_data_iter,
1634 					    notif);
1635 }
1636 
1637 void iwl_mvm_channel_switch_noa_notif(struct iwl_mvm *mvm,
1638 				      struct iwl_rx_cmd_buffer *rxb)
1639 {
1640 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1641 	struct iwl_channel_switch_noa_notif *notif = (void *)pkt->data;
1642 	struct ieee80211_vif *csa_vif;
1643 	struct iwl_mvm_vif *mvmvif;
1644 	int len = iwl_rx_packet_payload_len(pkt);
1645 	u32 id_n_color;
1646 
1647 	if (WARN_ON_ONCE(len < sizeof(*notif)))
1648 		return;
1649 
1650 	rcu_read_lock();
1651 
1652 	csa_vif = rcu_dereference(mvm->csa_vif);
1653 	if (WARN_ON(!csa_vif || !csa_vif->csa_active))
1654 		goto out_unlock;
1655 
1656 	id_n_color = le32_to_cpu(notif->id_and_color);
1657 
1658 	mvmvif = iwl_mvm_vif_from_mac80211(csa_vif);
1659 	if (WARN(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color) != id_n_color,
1660 		 "channel switch noa notification on unexpected vif (csa_vif=%d, notif=%d)",
1661 		 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color), id_n_color))
1662 		goto out_unlock;
1663 
1664 	IWL_DEBUG_INFO(mvm, "Channel Switch Started Notification\n");
1665 
1666 	schedule_delayed_work(&mvm->cs_tx_unblock_dwork,
1667 			      msecs_to_jiffies(IWL_MVM_CS_UNBLOCK_TX_TIMEOUT *
1668 					       csa_vif->bss_conf.beacon_int));
1669 
1670 	ieee80211_csa_finish(csa_vif);
1671 
1672 	rcu_read_unlock();
1673 
1674 	RCU_INIT_POINTER(mvm->csa_vif, NULL);
1675 
1676 	return;
1677 
1678 out_unlock:
1679 	rcu_read_unlock();
1680 }
1681