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