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