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