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