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