Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.1.
   1// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
   2/*
   3 * Copyright (C) 2012-2014, 2018-2023 Intel Corporation
   4 * Copyright (C) 2013-2014 Intel Mobile Communications GmbH
   5 * Copyright (C) 2015-2017 Intel Deutschland GmbH
   6 */
   7#include <linux/etherdevice.h>
   8#include <linux/crc32.h>
   9#include <net/mac80211.h>
  10#include "iwl-io.h"
  11#include "iwl-prph.h"
  12#include "fw-api.h"
  13#include "mvm.h"
  14#include "time-event.h"
  15
  16const u8 iwl_mvm_ac_to_tx_fifo[] = {
  17	IWL_MVM_TX_FIFO_VO,
  18	IWL_MVM_TX_FIFO_VI,
  19	IWL_MVM_TX_FIFO_BE,
  20	IWL_MVM_TX_FIFO_BK,
  21};
  22
  23const u8 iwl_mvm_ac_to_gen2_tx_fifo[] = {
  24	IWL_GEN2_EDCA_TX_FIFO_VO,
  25	IWL_GEN2_EDCA_TX_FIFO_VI,
  26	IWL_GEN2_EDCA_TX_FIFO_BE,
  27	IWL_GEN2_EDCA_TX_FIFO_BK,
  28	IWL_GEN2_TRIG_TX_FIFO_VO,
  29	IWL_GEN2_TRIG_TX_FIFO_VI,
  30	IWL_GEN2_TRIG_TX_FIFO_BE,
  31	IWL_GEN2_TRIG_TX_FIFO_BK,
  32};
  33
  34const u8 iwl_mvm_ac_to_bz_tx_fifo[] = {
  35	IWL_BZ_EDCA_TX_FIFO_VO,
  36	IWL_BZ_EDCA_TX_FIFO_VI,
  37	IWL_BZ_EDCA_TX_FIFO_BE,
  38	IWL_BZ_EDCA_TX_FIFO_BK,
  39	IWL_BZ_TRIG_TX_FIFO_VO,
  40	IWL_BZ_TRIG_TX_FIFO_VI,
  41	IWL_BZ_TRIG_TX_FIFO_BE,
  42	IWL_BZ_TRIG_TX_FIFO_BK,
  43};
  44
  45struct iwl_mvm_mac_iface_iterator_data {
  46	struct iwl_mvm *mvm;
  47	struct ieee80211_vif *vif;
  48	unsigned long available_mac_ids[BITS_TO_LONGS(NUM_MAC_INDEX_DRIVER)];
  49	unsigned long available_tsf_ids[BITS_TO_LONGS(NUM_TSF_IDS)];
  50	enum iwl_tsf_id preferred_tsf;
  51	bool found_vif;
  52};
  53
  54static void iwl_mvm_mac_tsf_id_iter(void *_data, u8 *mac,
  55				    struct ieee80211_vif *vif)
  56{
  57	struct iwl_mvm_mac_iface_iterator_data *data = _data;
  58	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  59	u16 min_bi;
  60
  61	/* Skip the interface for which we are trying to assign a tsf_id  */
  62	if (vif == data->vif)
  63		return;
  64
  65	/*
  66	 * The TSF is a hardware/firmware resource, there are 4 and
  67	 * the driver should assign and free them as needed. However,
  68	 * there are cases where 2 MACs should share the same TSF ID
  69	 * for the purpose of clock sync, an optimization to avoid
  70	 * clock drift causing overlapping TBTTs/DTIMs for a GO and
  71	 * client in the system.
  72	 *
  73	 * The firmware will decide according to the MAC type which
  74	 * will be the leader and follower. Clients that need to sync
  75	 * with a remote station will be the leader, and an AP or GO
  76	 * will be the follower.
  77	 *
  78	 * Depending on the new interface type it can be following
  79	 * or become the leader of an existing interface.
  80	 */
  81	switch (data->vif->type) {
  82	case NL80211_IFTYPE_STATION:
  83		/*
  84		 * The new interface is a client, so if the one we're iterating
  85		 * is an AP, and the beacon interval of the AP is a multiple or
  86		 * divisor of the beacon interval of the client, the same TSF
  87		 * should be used to avoid drift between the new client and
  88		 * existing AP. The existing AP will get drift updates from the
  89		 * new client context in this case.
  90		 */
  91		if (vif->type != NL80211_IFTYPE_AP ||
  92		    data->preferred_tsf != NUM_TSF_IDS ||
  93		    !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
  94			break;
  95
  96		min_bi = min(data->vif->bss_conf.beacon_int,
  97			     vif->bss_conf.beacon_int);
  98
  99		if (!min_bi)
 100			break;
 101
 102		if ((data->vif->bss_conf.beacon_int -
 103		     vif->bss_conf.beacon_int) % min_bi == 0) {
 104			data->preferred_tsf = mvmvif->tsf_id;
 105			return;
 106		}
 107		break;
 108
 109	case NL80211_IFTYPE_AP:
 110		/*
 111		 * The new interface is AP/GO, so if its beacon interval is a
 112		 * multiple or a divisor of the beacon interval of an existing
 113		 * interface, it should get drift updates from an existing
 114		 * client or use the same TSF as an existing GO. There's no
 115		 * drift between TSFs internally but if they used different
 116		 * TSFs then a new client MAC could update one of them and
 117		 * cause drift that way.
 118		 */
 119		if ((vif->type != NL80211_IFTYPE_AP &&
 120		     vif->type != NL80211_IFTYPE_STATION) ||
 121		    data->preferred_tsf != NUM_TSF_IDS ||
 122		    !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
 123			break;
 124
 125		min_bi = min(data->vif->bss_conf.beacon_int,
 126			     vif->bss_conf.beacon_int);
 127
 128		if (!min_bi)
 129			break;
 130
 131		if ((data->vif->bss_conf.beacon_int -
 132		     vif->bss_conf.beacon_int) % min_bi == 0) {
 133			data->preferred_tsf = mvmvif->tsf_id;
 134			return;
 135		}
 136		break;
 137	default:
 138		/*
 139		 * For all other interface types there's no need to
 140		 * take drift into account. Either they're exclusive
 141		 * like IBSS and monitor, or we don't care much about
 142		 * their TSF (like P2P Device), but we won't be able
 143		 * to share the TSF resource.
 144		 */
 145		break;
 146	}
 147
 148	/*
 149	 * Unless we exited above, we can't share the TSF resource
 150	 * that the virtual interface we're iterating over is using
 151	 * with the new one, so clear the available bit and if this
 152	 * was the preferred one, reset that as well.
 153	 */
 154	__clear_bit(mvmvif->tsf_id, data->available_tsf_ids);
 155
 156	if (data->preferred_tsf == mvmvif->tsf_id)
 157		data->preferred_tsf = NUM_TSF_IDS;
 158}
 159
 160static void iwl_mvm_mac_iface_iterator(void *_data, u8 *mac,
 161				       struct ieee80211_vif *vif)
 162{
 163	struct iwl_mvm_mac_iface_iterator_data *data = _data;
 164	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 165
 166	/* Iterator may already find the interface being added -- skip it */
 167	if (vif == data->vif) {
 168		data->found_vif = true;
 169		return;
 170	}
 171
 172	/* Mark MAC IDs as used by clearing the available bit, and
 173	 * (below) mark TSFs as used if their existing use is not
 174	 * compatible with the new interface type.
 175	 * No locking or atomic bit operations are needed since the
 176	 * data is on the stack of the caller function.
 177	 */
 178	__clear_bit(mvmvif->id, data->available_mac_ids);
 179
 180	/* find a suitable tsf_id */
 181	iwl_mvm_mac_tsf_id_iter(_data, mac, vif);
 182}
 183
 184void iwl_mvm_mac_ctxt_recalc_tsf_id(struct iwl_mvm *mvm,
 185				    struct ieee80211_vif *vif)
 186{
 187	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 188	struct iwl_mvm_mac_iface_iterator_data data = {
 189		.mvm = mvm,
 190		.vif = vif,
 191		.available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
 192		/* no preference yet */
 193		.preferred_tsf = NUM_TSF_IDS,
 194	};
 195
 196	ieee80211_iterate_active_interfaces_atomic(
 197		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
 198		iwl_mvm_mac_tsf_id_iter, &data);
 199
 200	if (data.preferred_tsf != NUM_TSF_IDS)
 201		mvmvif->tsf_id = data.preferred_tsf;
 202	else if (!test_bit(mvmvif->tsf_id, data.available_tsf_ids))
 203		mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
 204						NUM_TSF_IDS);
 205}
 206
 207int iwl_mvm_mac_ctxt_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
 208{
 209	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 210	struct iwl_mvm_mac_iface_iterator_data data = {
 211		.mvm = mvm,
 212		.vif = vif,
 213		.available_mac_ids = { (1 << NUM_MAC_INDEX_DRIVER) - 1 },
 214		.available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
 215		/* no preference yet */
 216		.preferred_tsf = NUM_TSF_IDS,
 217		.found_vif = false,
 218	};
 219	int ret, i;
 220
 221	lockdep_assert_held(&mvm->mutex);
 222
 223	/*
 224	 * Allocate a MAC ID and a TSF for this MAC, along with the queues
 225	 * and other resources.
 226	 */
 227
 228	/*
 229	 * Before the iterator, we start with all MAC IDs and TSFs available.
 230	 *
 231	 * During iteration, all MAC IDs are cleared that are in use by other
 232	 * virtual interfaces, and all TSF IDs are cleared that can't be used
 233	 * by this new virtual interface because they're used by an interface
 234	 * that can't share it with the new one.
 235	 * At the same time, we check if there's a preferred TSF in the case
 236	 * that we should share it with another interface.
 237	 */
 238
 239	/* MAC ID 0 should be used only for the managed/IBSS vif with non-MLO
 240	 * FW API
 241	 */
 242	if (!mvm->mld_api_is_used) {
 243		switch (vif->type) {
 244		case NL80211_IFTYPE_ADHOC:
 245			break;
 246		case NL80211_IFTYPE_STATION:
 247			if (!vif->p2p)
 248				break;
 249			fallthrough;
 250		default:
 251			__clear_bit(0, data.available_mac_ids);
 252		}
 253	}
 254
 255	ieee80211_iterate_active_interfaces_atomic(
 256		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
 257		iwl_mvm_mac_iface_iterator, &data);
 258
 259	/*
 260	 * In the case we're getting here during resume, it's similar to
 261	 * firmware restart, and with RESUME_ALL the iterator will find
 262	 * the vif being added already.
 263	 * We don't want to reassign any IDs in either case since doing
 264	 * so would probably assign different IDs (as interfaces aren't
 265	 * necessarily added in the same order), but the old IDs were
 266	 * preserved anyway, so skip ID assignment for both resume and
 267	 * recovery.
 268	 */
 269	if (data.found_vif)
 270		return 0;
 271
 272	/* Therefore, in recovery, we can't get here */
 273	if (WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
 274		return -EBUSY;
 275
 276	mvmvif->id = find_first_bit(data.available_mac_ids,
 277				    NUM_MAC_INDEX_DRIVER);
 278	if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
 279		IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
 280		ret = -EIO;
 281		goto exit_fail;
 282	}
 283
 284	if (data.preferred_tsf != NUM_TSF_IDS)
 285		mvmvif->tsf_id = data.preferred_tsf;
 286	else
 287		mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
 288						NUM_TSF_IDS);
 289	if (mvmvif->tsf_id == NUM_TSF_IDS) {
 290		IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
 291		ret = -EIO;
 292		goto exit_fail;
 293	}
 294
 295	mvmvif->color = 0;
 296
 297	INIT_LIST_HEAD(&mvmvif->time_event_data.list);
 298	mvmvif->time_event_data.id = TE_MAX;
 299
 300	mvmvif->deflink.bcast_sta.sta_id = IWL_MVM_INVALID_STA;
 301	mvmvif->deflink.mcast_sta.sta_id = IWL_MVM_INVALID_STA;
 302	mvmvif->deflink.ap_sta_id = IWL_MVM_INVALID_STA;
 303
 304	/* No need to allocate data queues to P2P Device MAC and NAN.*/
 305	if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
 306		return 0;
 307
 308	/* Allocate the CAB queue for softAP and GO interfaces */
 309	if (vif->type == NL80211_IFTYPE_AP ||
 310	    vif->type == NL80211_IFTYPE_ADHOC) {
 311		/*
 312		 * For TVQM this will be overwritten later with the FW assigned
 313		 * queue value (when queue is enabled).
 314		 */
 315		mvmvif->deflink.cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
 316	}
 317
 318	for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
 319		mvmvif->deflink.smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
 320
 321	return 0;
 322
 323exit_fail:
 324	memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
 325	return ret;
 326}
 327
 328static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
 329			      struct ieee80211_vif *vif,
 330			      enum nl80211_band band,
 331			      u8 *cck_rates, u8 *ofdm_rates)
 332{
 333	struct ieee80211_supported_band *sband;
 334	unsigned long basic = vif->bss_conf.basic_rates;
 335	int lowest_present_ofdm = 100;
 336	int lowest_present_cck = 100;
 337	u8 cck = 0;
 338	u8 ofdm = 0;
 339	int i;
 340
 341	sband = mvm->hw->wiphy->bands[band];
 342
 343	for_each_set_bit(i, &basic, BITS_PER_LONG) {
 344		int hw = sband->bitrates[i].hw_value;
 345		if (hw >= IWL_FIRST_OFDM_RATE) {
 346			ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
 347			if (lowest_present_ofdm > hw)
 348				lowest_present_ofdm = hw;
 349		} else {
 350			BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
 351
 352			cck |= BIT(hw);
 353			if (lowest_present_cck > hw)
 354				lowest_present_cck = hw;
 355		}
 356	}
 357
 358	/*
 359	 * Now we've got the basic rates as bitmaps in the ofdm and cck
 360	 * variables. This isn't sufficient though, as there might not
 361	 * be all the right rates in the bitmap. E.g. if the only basic
 362	 * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
 363	 * and 6 Mbps because the 802.11-2007 standard says in 9.6:
 364	 *
 365	 *    [...] a STA responding to a received frame shall transmit
 366	 *    its Control Response frame [...] at the highest rate in the
 367	 *    BSSBasicRateSet parameter that is less than or equal to the
 368	 *    rate of the immediately previous frame in the frame exchange
 369	 *    sequence ([...]) and that is of the same modulation class
 370	 *    ([...]) as the received frame. If no rate contained in the
 371	 *    BSSBasicRateSet parameter meets these conditions, then the
 372	 *    control frame sent in response to a received frame shall be
 373	 *    transmitted at the highest mandatory rate of the PHY that is
 374	 *    less than or equal to the rate of the received frame, and
 375	 *    that is of the same modulation class as the received frame.
 376	 *
 377	 * As a consequence, we need to add all mandatory rates that are
 378	 * lower than all of the basic rates to these bitmaps.
 379	 */
 380
 381	if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
 382		ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
 383	if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
 384		ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
 385	/* 6M already there or needed so always add */
 386	ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
 387
 388	/*
 389	 * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
 390	 * Note, however:
 391	 *  - if no CCK rates are basic, it must be ERP since there must
 392	 *    be some basic rates at all, so they're OFDM => ERP PHY
 393	 *    (or we're in 5 GHz, and the cck bitmap will never be used)
 394	 *  - if 11M is a basic rate, it must be ERP as well, so add 5.5M
 395	 *  - if 5.5M is basic, 1M and 2M are mandatory
 396	 *  - if 2M is basic, 1M is mandatory
 397	 *  - if 1M is basic, that's the only valid ACK rate.
 398	 * As a consequence, it's not as complicated as it sounds, just add
 399	 * any lower rates to the ACK rate bitmap.
 400	 */
 401	if (IWL_RATE_11M_INDEX < lowest_present_cck)
 402		cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
 403	if (IWL_RATE_5M_INDEX < lowest_present_cck)
 404		cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
 405	if (IWL_RATE_2M_INDEX < lowest_present_cck)
 406		cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
 407	/* 1M already there or needed so always add */
 408	cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
 409
 410	*cck_rates = cck;
 411	*ofdm_rates = ofdm;
 412}
 413
 414void iwl_mvm_set_fw_basic_rates(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
 415				struct ieee80211_bss_conf *link_conf,
 416				__le32 *cck_rates, __le32 *ofdm_rates)
 417{
 418	struct ieee80211_chanctx_conf *chanctx;
 419	u8 cck_ack_rates = 0, ofdm_ack_rates = 0;
 420
 421	rcu_read_lock();
 422	chanctx = rcu_dereference(link_conf->chanctx_conf);
 423	iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
 424					    : NL80211_BAND_2GHZ,
 425			  &cck_ack_rates, &ofdm_ack_rates);
 426
 427	rcu_read_unlock();
 428
 429	*cck_rates = cpu_to_le32((u32)cck_ack_rates);
 430	*ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
 431}
 432
 433void iwl_mvm_set_fw_protection_flags(struct iwl_mvm *mvm,
 434				     struct ieee80211_vif *vif,
 435				     struct ieee80211_bss_conf *link_conf,
 436				     __le32 *protection_flags, u32 ht_flag,
 437				     u32 tgg_flag)
 438{
 439	/* for both sta and ap, ht_operation_mode hold the protection_mode */
 440	u8 protection_mode = link_conf->ht_operation_mode &
 441				 IEEE80211_HT_OP_MODE_PROTECTION;
 442	bool ht_enabled = !!(link_conf->ht_operation_mode &
 443			     IEEE80211_HT_OP_MODE_PROTECTION);
 444
 445	if (link_conf->use_cts_prot)
 446		*protection_flags |= cpu_to_le32(tgg_flag);
 447
 448	IWL_DEBUG_RATE(mvm, "use_cts_prot %d, ht_operation_mode %d\n",
 449		       link_conf->use_cts_prot,
 450		       link_conf->ht_operation_mode);
 451
 452	if (!ht_enabled)
 453		return;
 454
 455	IWL_DEBUG_RATE(mvm, "protection mode set to %d\n", protection_mode);
 456	/*
 457	 * See section 9.23.3.1 of IEEE 80211-2012.
 458	 * Nongreenfield HT STAs Present is not supported.
 459	 */
 460	switch (protection_mode) {
 461	case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
 462		break;
 463	case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
 464	case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
 465		*protection_flags |= cpu_to_le32(ht_flag);
 466		break;
 467	case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
 468		/* Protect when channel wider than 20MHz */
 469		if (link_conf->chandef.width > NL80211_CHAN_WIDTH_20)
 470			*protection_flags |= cpu_to_le32(ht_flag);
 471		break;
 472	default:
 473		IWL_ERR(mvm, "Illegal protection mode %d\n",
 474			protection_mode);
 475		break;
 476	}
 477}
 478
 479void iwl_mvm_set_fw_qos_params(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
 480			       struct ieee80211_bss_conf *link_conf,
 481			       struct iwl_ac_qos *ac, __le32 *qos_flags)
 482{
 483	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 484	struct iwl_mvm_vif_link_info *mvm_link =
 485		mvmvif->link[link_conf->link_id];
 486	int i;
 487
 488	if (!mvm_link)
 489		return;
 490
 491	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
 492		u8 txf = iwl_mvm_mac_ac_to_tx_fifo(mvm, i);
 493		u8 ucode_ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);
 494
 495		ac[ucode_ac].cw_min =
 496			cpu_to_le16(mvm_link->queue_params[i].cw_min);
 497		ac[ucode_ac].cw_max =
 498			cpu_to_le16(mvm_link->queue_params[i].cw_max);
 499		ac[ucode_ac].edca_txop =
 500			cpu_to_le16(mvm_link->queue_params[i].txop * 32);
 501		ac[ucode_ac].aifsn = mvm_link->queue_params[i].aifs;
 502		ac[ucode_ac].fifos_mask = BIT(txf);
 503	}
 504
 505	if (link_conf->qos)
 506		*qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
 507
 508	if (link_conf->chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
 509		*qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
 510}
 511
 512int iwl_mvm_get_mac_type(struct ieee80211_vif *vif)
 513{
 514	u32 mac_type = FW_MAC_TYPE_BSS_STA;
 515
 516	switch (vif->type) {
 517	case NL80211_IFTYPE_STATION:
 518		if (vif->p2p)
 519			mac_type = FW_MAC_TYPE_P2P_STA;
 520		else
 521			mac_type = FW_MAC_TYPE_BSS_STA;
 522		break;
 523	case NL80211_IFTYPE_AP:
 524		mac_type = FW_MAC_TYPE_GO;
 525		break;
 526	case NL80211_IFTYPE_MONITOR:
 527		mac_type = FW_MAC_TYPE_LISTENER;
 528		break;
 529	case NL80211_IFTYPE_P2P_DEVICE:
 530		mac_type = FW_MAC_TYPE_P2P_DEVICE;
 531		break;
 532	case NL80211_IFTYPE_ADHOC:
 533		mac_type = FW_MAC_TYPE_IBSS;
 534		break;
 535	default:
 536		WARN_ON_ONCE(1);
 537	}
 538	return mac_type;
 539}
 540
 541static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
 542					struct ieee80211_vif *vif,
 543					struct iwl_mac_ctx_cmd *cmd,
 544					const u8 *bssid_override,
 545					u32 action)
 546{
 547	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 548	const u8 *bssid = bssid_override ?: vif->bss_conf.bssid;
 549	u32 ht_flag;
 550
 551	cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
 552							    mvmvif->color));
 553	cmd->action = cpu_to_le32(action);
 554	cmd->mac_type = cpu_to_le32(iwl_mvm_get_mac_type(vif));
 555
 556	cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
 557
 558	memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
 559
 560	if (bssid)
 561		memcpy(cmd->bssid_addr, bssid, ETH_ALEN);
 562	else
 563		eth_broadcast_addr(cmd->bssid_addr);
 564
 565	iwl_mvm_set_fw_basic_rates(mvm, vif, &vif->bss_conf, &cmd->cck_rates,
 566				   &cmd->ofdm_rates);
 567
 568	cmd->cck_short_preamble =
 569		cpu_to_le32(vif->bss_conf.use_short_preamble ?
 570			    MAC_FLG_SHORT_PREAMBLE : 0);
 571	cmd->short_slot =
 572		cpu_to_le32(vif->bss_conf.use_short_slot ?
 573			    MAC_FLG_SHORT_SLOT : 0);
 574
 575	cmd->filter_flags = 0;
 576
 577	iwl_mvm_set_fw_qos_params(mvm, vif, &vif->bss_conf, cmd->ac,
 578				  &cmd->qos_flags);
 579
 580	/* The fw does not distinguish between ht and fat */
 581	ht_flag = MAC_PROT_FLG_HT_PROT | MAC_PROT_FLG_FAT_PROT;
 582	iwl_mvm_set_fw_protection_flags(mvm, vif, &vif->bss_conf,
 583					&cmd->protection_flags,
 584					ht_flag, MAC_PROT_FLG_TGG_PROTECT);
 585}
 586
 587static 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_CMD (action:%d): %d\n",
 594			le32_to_cpu(cmd->action), ret);
 595	return ret;
 596}
 597
 598void iwl_mvm_set_fw_dtim_tbtt(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
 599			      struct ieee80211_bss_conf *link_conf,
 600			      __le64 *dtim_tsf, __le32 *dtim_time,
 601			      __le32 *assoc_beacon_arrive_time)
 602{
 603	u32 dtim_offs;
 604
 605	/*
 606	 * The DTIM count counts down, so when it is N that means N
 607	 * more beacon intervals happen until the DTIM TBTT. Therefore
 608	 * add this to the current time. If that ends up being in the
 609	 * future, the firmware will handle it.
 610	 *
 611	 * Also note that the system_timestamp (which we get here as
 612	 * "sync_device_ts") and TSF timestamp aren't at exactly the
 613	 * same offset in the frame -- the TSF is at the first symbol
 614	 * of the TSF, the system timestamp is at signal acquisition
 615	 * time. This means there's an offset between them of at most
 616	 * a few hundred microseconds (24 * 8 bits + PLCP time gives
 617	 * 384us in the longest case), this is currently not relevant
 618	 * as the firmware wakes up around 2ms before the TBTT.
 619	 */
 620	dtim_offs = link_conf->sync_dtim_count *
 621			link_conf->beacon_int;
 622	/* convert TU to usecs */
 623	dtim_offs *= 1024;
 624
 625	*dtim_tsf =
 626		cpu_to_le64(link_conf->sync_tsf + dtim_offs);
 627	*dtim_time =
 628		cpu_to_le32(link_conf->sync_device_ts + dtim_offs);
 629	*assoc_beacon_arrive_time =
 630		cpu_to_le32(link_conf->sync_device_ts);
 631
 632	IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
 633		       le64_to_cpu(*dtim_tsf),
 634		       le32_to_cpu(*dtim_time),
 635		       dtim_offs);
 636}
 637
 638__le32 iwl_mvm_mac_ctxt_cmd_p2p_sta_get_oppps_ctwin(struct iwl_mvm *mvm,
 639						    struct ieee80211_vif *vif)
 640{
 641	struct ieee80211_p2p_noa_attr *noa =
 642		&vif->bss_conf.p2p_noa_attr;
 643
 644	return cpu_to_le32(noa->oppps_ctwindow &
 645			IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
 646}
 647
 648u32 iwl_mvm_mac_ctxt_cmd_sta_get_twt_policy(struct iwl_mvm *mvm,
 649					    struct ieee80211_vif *vif)
 650{
 651	u32 twt_policy = 0;
 652
 653	if (vif->bss_conf.twt_requester && IWL_MVM_USE_TWT)
 654		twt_policy |= TWT_SUPPORTED;
 655	if (vif->bss_conf.twt_protected)
 656		twt_policy |= PROTECTED_TWT_SUPPORTED;
 657	if (vif->bss_conf.twt_broadcast)
 658		twt_policy |= BROADCAST_TWT_SUPPORTED;
 659
 660	return twt_policy;
 661}
 662
 663static int iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm *mvm,
 664				    struct ieee80211_vif *vif,
 665				    u32 action, bool force_assoc_off,
 666				    const u8 *bssid_override)
 667{
 668	struct iwl_mac_ctx_cmd cmd = {};
 669	struct iwl_mac_data_sta *ctxt_sta;
 670
 671	WARN_ON(vif->type != NL80211_IFTYPE_STATION);
 672
 673	/* Fill the common data for all mac context types */
 674	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, bssid_override, action);
 675
 676	/*
 677	 * We always want to hear MCAST frames, if we're not authorized yet,
 678	 * we'll drop them.
 679	 */
 680	cmd.filter_flags |= cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
 681
 682	if (vif->p2p) {
 683		cmd.p2p_sta.ctwin =
 684			iwl_mvm_mac_ctxt_cmd_p2p_sta_get_oppps_ctwin(mvm, vif);
 685
 686		ctxt_sta = &cmd.p2p_sta.sta;
 687	} else {
 688		ctxt_sta = &cmd.sta;
 689	}
 690
 691	/* We need the dtim_period to set the MAC as associated */
 692	if (vif->cfg.assoc && vif->bss_conf.dtim_period &&
 693	    !force_assoc_off) {
 694		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 695
 696		iwl_mvm_set_fw_dtim_tbtt(mvm, vif, &vif->bss_conf,
 697					 &ctxt_sta->dtim_tsf,
 698					 &ctxt_sta->dtim_time,
 699					 &ctxt_sta->assoc_beacon_arrive_time);
 700
 701		ctxt_sta->is_assoc = cpu_to_le32(1);
 702
 703		if (!mvmvif->authorized &&
 704		    fw_has_capa(&mvm->fw->ucode_capa,
 705				IWL_UCODE_TLV_CAPA_COEX_HIGH_PRIO))
 706			ctxt_sta->data_policy |=
 707				cpu_to_le32(COEX_HIGH_PRIORITY_ENABLE);
 708	} else {
 709		ctxt_sta->is_assoc = cpu_to_le32(0);
 710
 711		/* Allow beacons to pass through as long as we are not
 712		 * associated, or we do not have dtim period information.
 713		 */
 714		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
 715	}
 716
 717	ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
 718	ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
 719					      vif->bss_conf.dtim_period);
 720
 721	ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
 722	ctxt_sta->assoc_id = cpu_to_le32(vif->cfg.aid);
 723
 724	if (vif->probe_req_reg && vif->cfg.assoc && vif->p2p)
 725		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
 726
 727	if (vif->bss_conf.he_support && !iwlwifi_mod_params.disable_11ax) {
 728		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_11AX);
 729		ctxt_sta->data_policy |=
 730			cpu_to_le32(iwl_mvm_mac_ctxt_cmd_sta_get_twt_policy(mvm, vif));
 731	}
 732
 733
 734	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
 735}
 736
 737static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
 738					 struct ieee80211_vif *vif,
 739					 u32 action)
 740{
 741	struct iwl_mac_ctx_cmd cmd = {};
 742	u32 tfd_queue_msk = 0;
 743	int ret;
 744
 745	WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
 746
 747	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
 748
 749	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
 750				       MAC_FILTER_IN_CONTROL_AND_MGMT |
 751				       MAC_FILTER_IN_BEACON |
 752				       MAC_FILTER_IN_PROBE_REQUEST |
 753				       MAC_FILTER_IN_CRC32 |
 754				       MAC_FILTER_ACCEPT_GRP);
 755	ieee80211_hw_set(mvm->hw, RX_INCLUDES_FCS);
 756
 757	/*
 758	 * the queue mask is only relevant for old TX API, and
 759	 * mvm->snif_queue isn't set here (it's still set to
 760	 * IWL_MVM_INVALID_QUEUE so the BIT() of it is UB)
 761	 */
 762	if (!iwl_mvm_has_new_tx_api(mvm))
 763		tfd_queue_msk = BIT(mvm->snif_queue);
 764
 765	/* Allocate sniffer station */
 766	ret = iwl_mvm_allocate_int_sta(mvm, &mvm->snif_sta, tfd_queue_msk,
 767				       vif->type, IWL_STA_GENERAL_PURPOSE);
 768	if (ret)
 769		return ret;
 770
 771	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
 772}
 773
 774static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
 775				     struct ieee80211_vif *vif,
 776				     u32 action)
 777{
 778	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 779	struct iwl_mac_ctx_cmd cmd = {};
 780
 781	WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
 782
 783	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
 784
 785	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
 786				       MAC_FILTER_IN_PROBE_REQUEST |
 787				       MAC_FILTER_ACCEPT_GRP);
 788
 789	/* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */
 790	cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
 791
 792	/* TODO: Assumes that the beacon id == mac context id */
 793	cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
 794
 795	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
 796}
 797
 798struct iwl_mvm_go_iterator_data {
 799	bool go_active;
 800};
 801
 802static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
 803{
 804	struct iwl_mvm_go_iterator_data *data = _data;
 805	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 806
 807	if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
 808	    mvmvif->ap_ibss_active)
 809		data->go_active = true;
 810}
 811
 812__le32 iwl_mac_ctxt_p2p_dev_has_extended_disc(struct iwl_mvm *mvm,
 813					      struct ieee80211_vif *vif)
 814{
 815	struct iwl_mvm_go_iterator_data data = {};
 816
 817	/*
 818	 * This flag should be set to true when the P2P Device is
 819	 * discoverable and there is at least another active P2P GO. Settings
 820	 * this flag will allow the P2P Device to be discoverable on other
 821	 * channels in addition to its listen channel.
 822	 * Note that this flag should not be set in other cases as it opens the
 823	 * Rx filters on all MAC and increases the number of interrupts.
 824	 */
 825	ieee80211_iterate_active_interfaces_atomic(
 826		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
 827		iwl_mvm_go_iterator, &data);
 828
 829	return cpu_to_le32(data.go_active ? 1 : 0);
 830}
 831
 832static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
 833					   struct ieee80211_vif *vif,
 834					   u32 action)
 835{
 836	struct iwl_mac_ctx_cmd cmd = {};
 837
 838	WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
 839
 840	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
 841
 842	cmd.p2p_dev.is_disc_extended =
 843		iwl_mac_ctxt_p2p_dev_has_extended_disc(mvm, vif);
 844
 845	/* Override the filter flags to accept only probe requests */
 846	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
 847
 848	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
 849}
 850
 851void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
 852			      __le32 *tim_index, __le32 *tim_size,
 853			      u8 *beacon, u32 frame_size)
 854{
 855	u32 tim_idx;
 856	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
 857
 858	/* The index is relative to frame start but we start looking at the
 859	 * variable-length part of the beacon. */
 860	tim_idx = mgmt->u.beacon.variable - beacon;
 861
 862	/* Parse variable-length elements of beacon to find WLAN_EID_TIM */
 863	while ((tim_idx < (frame_size - 2)) &&
 864			(beacon[tim_idx] != WLAN_EID_TIM))
 865		tim_idx += beacon[tim_idx+1] + 2;
 866
 867	/* If TIM field was found, set variables */
 868	if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
 869		*tim_index = cpu_to_le32(tim_idx);
 870		*tim_size = cpu_to_le32((u32)beacon[tim_idx + 1]);
 871	} else {
 872		IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
 873	}
 874}
 875
 876static u32 iwl_mvm_find_ie_offset(u8 *beacon, u8 eid, u32 frame_size)
 877{
 878	struct ieee80211_mgmt *mgmt = (void *)beacon;
 879	const u8 *ie;
 880
 881	if (WARN_ON_ONCE(frame_size <= (mgmt->u.beacon.variable - beacon)))
 882		return 0;
 883
 884	frame_size -= mgmt->u.beacon.variable - beacon;
 885
 886	ie = cfg80211_find_ie(eid, mgmt->u.beacon.variable, frame_size);
 887	if (!ie)
 888		return 0;
 889
 890	return ie - beacon;
 891}
 892
 893u8 iwl_mvm_mac_ctxt_get_lowest_rate(struct iwl_mvm *mvm,
 894				    struct ieee80211_tx_info *info,
 895				    struct ieee80211_vif *vif)
 896{
 897	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 898	struct ieee80211_supported_band *sband;
 899	unsigned long basic = vif->bss_conf.basic_rates;
 900	u16 lowest_cck = IWL_RATE_COUNT, lowest_ofdm = IWL_RATE_COUNT;
 901	u32 link_id = u32_get_bits(info->control.flags,
 902				   IEEE80211_TX_CTRL_MLO_LINK);
 903	u8 band = info->band;
 904	u8 rate;
 905	u32 i;
 906
 907	if (link_id == IEEE80211_LINK_UNSPECIFIED && ieee80211_vif_is_mld(vif)) {
 908		for (i = 0; i < ARRAY_SIZE(mvmvif->link); i++) {
 909			if (!mvmvif->link[i])
 910				continue;
 911			/* shouldn't do this when >1 link is active */
 912			WARN_ON_ONCE(link_id != IEEE80211_LINK_UNSPECIFIED);
 913			link_id = i;
 914		}
 915	}
 916
 917	if (link_id < IEEE80211_LINK_UNSPECIFIED) {
 918		struct ieee80211_bss_conf *link_conf;
 919
 920		rcu_read_lock();
 921		link_conf = rcu_dereference(vif->link_conf[link_id]);
 922		if (link_conf) {
 923			basic = link_conf->basic_rates;
 924			if (link_conf->chandef.chan)
 925				band = link_conf->chandef.chan->band;
 926		}
 927		rcu_read_unlock();
 928	}
 929
 930	sband = mvm->hw->wiphy->bands[band];
 931	for_each_set_bit(i, &basic, BITS_PER_LONG) {
 932		u16 hw = sband->bitrates[i].hw_value;
 933
 934		if (hw >= IWL_FIRST_OFDM_RATE) {
 935			if (lowest_ofdm > hw)
 936				lowest_ofdm = hw;
 937		} else if (lowest_cck > hw) {
 938			lowest_cck = hw;
 939		}
 940	}
 941
 942	if (band == NL80211_BAND_2GHZ && !vif->p2p &&
 943	    vif->type != NL80211_IFTYPE_P2P_DEVICE &&
 944	    !(info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)) {
 945		if (lowest_cck != IWL_RATE_COUNT)
 946			rate = lowest_cck;
 947		else if (lowest_ofdm != IWL_RATE_COUNT)
 948			rate = lowest_ofdm;
 949		else
 950			rate = IWL_RATE_1M_INDEX;
 951	} else if (lowest_ofdm != IWL_RATE_COUNT) {
 952		rate = lowest_ofdm;
 953	} else {
 954		rate = IWL_RATE_6M_INDEX;
 955	}
 956
 957	return rate;
 958}
 959
 960u16 iwl_mvm_mac_ctxt_get_beacon_flags(const struct iwl_fw *fw, u8 rate_idx)
 961{
 962	u16 flags = iwl_mvm_mac80211_idx_to_hwrate(fw, rate_idx);
 963	bool is_new_rate = iwl_fw_lookup_cmd_ver(fw, BEACON_TEMPLATE_CMD, 0) > 10;
 964
 965	if (rate_idx <= IWL_FIRST_CCK_RATE)
 966		flags |= is_new_rate ? IWL_MAC_BEACON_CCK
 967			  : IWL_MAC_BEACON_CCK_V1;
 968
 969	return flags;
 970}
 971
 972u8 iwl_mvm_mac_ctxt_get_beacon_rate(struct iwl_mvm *mvm,
 973				    struct ieee80211_tx_info *info,
 974				    struct ieee80211_vif *vif)
 975{
 976	struct ieee80211_supported_band *sband =
 977		mvm->hw->wiphy->bands[info->band];
 978	u32 legacy = vif->bss_conf.beacon_tx_rate.control[info->band].legacy;
 979
 980	/* if beacon rate was configured try using it */
 981	if (hweight32(legacy) == 1) {
 982		u32 rate = ffs(legacy) - 1;
 983
 984		return sband->bitrates[rate].hw_value;
 985	}
 986
 987	return iwl_mvm_mac_ctxt_get_lowest_rate(mvm, info, vif);
 988}
 989
 990static void iwl_mvm_mac_ctxt_set_tx(struct iwl_mvm *mvm,
 991				    struct ieee80211_vif *vif,
 992				    struct sk_buff *beacon,
 993				    struct iwl_tx_cmd *tx)
 994{
 995	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 996	struct ieee80211_tx_info *info;
 997	u8 rate;
 998	u32 tx_flags;
 999
1000	info = IEEE80211_SKB_CB(beacon);
1001
1002	/* Set up TX command fields */
1003	tx->len = cpu_to_le16((u16)beacon->len);
1004	tx->sta_id = mvmvif->deflink.bcast_sta.sta_id;
1005	tx->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
1006	tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;
1007	tx_flags |=
1008		iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<
1009						TX_CMD_FLG_BT_PRIO_POS;
1010	tx->tx_flags = cpu_to_le32(tx_flags);
1011
1012	if (!fw_has_capa(&mvm->fw->ucode_capa,
1013			 IWL_UCODE_TLV_CAPA_BEACON_ANT_SELECTION))
1014		iwl_mvm_toggle_tx_ant(mvm, &mvm->mgmt_last_antenna_idx);
1015
1016	tx->rate_n_flags =
1017		cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
1018			    RATE_MCS_ANT_POS);
1019
1020	rate = iwl_mvm_mac_ctxt_get_beacon_rate(mvm, info, vif);
1021
1022	tx->rate_n_flags |=
1023		cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(mvm->fw, rate));
1024	if (rate == IWL_FIRST_CCK_RATE)
1025		tx->rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK_V1);
1026
1027}
1028
1029int iwl_mvm_mac_ctxt_send_beacon_cmd(struct iwl_mvm *mvm,
1030				     struct sk_buff *beacon,
1031				     void *data, int len)
1032{
1033	struct iwl_host_cmd cmd = {
1034		.id = BEACON_TEMPLATE_CMD,
1035		.flags = CMD_ASYNC,
1036	};
1037
1038	cmd.len[0] = len;
1039	cmd.data[0] = data;
1040	cmd.dataflags[0] = 0;
1041	cmd.len[1] = beacon->len;
1042	cmd.data[1] = beacon->data;
1043	cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
1044
1045	return iwl_mvm_send_cmd(mvm, &cmd);
1046}
1047
1048static int iwl_mvm_mac_ctxt_send_beacon_v6(struct iwl_mvm *mvm,
1049					   struct ieee80211_vif *vif,
1050					   struct sk_buff *beacon)
1051{
1052	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1053	struct iwl_mac_beacon_cmd_v6 beacon_cmd = {};
1054
1055	iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
1056
1057	beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
1058
1059	if (vif->type == NL80211_IFTYPE_AP)
1060		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
1061					 &beacon_cmd.tim_size,
1062					 beacon->data, beacon->len);
1063
1064	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
1065						sizeof(beacon_cmd));
1066}
1067
1068static int iwl_mvm_mac_ctxt_send_beacon_v7(struct iwl_mvm *mvm,
1069					   struct ieee80211_vif *vif,
1070					   struct sk_buff *beacon)
1071{
1072	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1073	struct iwl_mac_beacon_cmd_v7 beacon_cmd = {};
1074
1075	iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
1076
1077	beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
1078
1079	if (vif->type == NL80211_IFTYPE_AP)
1080		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
1081					 &beacon_cmd.tim_size,
1082					 beacon->data, beacon->len);
1083
1084	beacon_cmd.csa_offset =
1085		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1086						   WLAN_EID_CHANNEL_SWITCH,
1087						   beacon->len));
1088	beacon_cmd.ecsa_offset =
1089		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1090						   WLAN_EID_EXT_CHANSWITCH_ANN,
1091						   beacon->len));
1092
1093	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
1094						sizeof(beacon_cmd));
1095}
1096
1097bool iwl_mvm_enable_fils(struct iwl_mvm *mvm,
1098			 struct ieee80211_chanctx_conf *ctx)
1099{
1100	if (IWL_MVM_DISABLE_AP_FILS)
1101		return false;
1102
1103	if (cfg80211_channel_is_psc(ctx->def.chan))
1104		return true;
1105
1106	return (ctx->def.chan->band == NL80211_BAND_6GHZ &&
1107		ctx->def.width >= NL80211_CHAN_WIDTH_80);
1108}
1109
1110static int iwl_mvm_mac_ctxt_send_beacon_v9(struct iwl_mvm *mvm,
1111					   struct ieee80211_vif *vif,
1112					   struct sk_buff *beacon,
1113					   struct ieee80211_bss_conf *link_conf)
1114{
1115	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1116	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(beacon);
1117	struct iwl_mac_beacon_cmd beacon_cmd = {};
1118	u8 rate = iwl_mvm_mac_ctxt_get_beacon_rate(mvm, info, vif);
1119	u16 flags;
1120	struct ieee80211_chanctx_conf *ctx;
1121	int channel;
1122	flags = iwl_mvm_mac_ctxt_get_beacon_flags(mvm->fw, rate);
1123
1124	/* Enable FILS on PSC channels only */
1125	rcu_read_lock();
1126	ctx = rcu_dereference(link_conf->chanctx_conf);
1127	channel = ieee80211_frequency_to_channel(ctx->def.chan->center_freq);
1128	WARN_ON(channel == 0);
1129	if (iwl_mvm_enable_fils(mvm, ctx)) {
1130		flags |= iwl_fw_lookup_cmd_ver(mvm->fw, BEACON_TEMPLATE_CMD,
1131					       0) > 10 ?
1132			IWL_MAC_BEACON_FILS :
1133			IWL_MAC_BEACON_FILS_V1;
1134		beacon_cmd.short_ssid =
1135			cpu_to_le32(~crc32_le(~0, vif->cfg.ssid,
1136					      vif->cfg.ssid_len));
1137	}
1138	rcu_read_unlock();
1139
1140	beacon_cmd.flags = cpu_to_le16(flags);
1141	beacon_cmd.byte_cnt = cpu_to_le16((u16)beacon->len);
1142
1143	if (WARN_ON(!mvmvif->link[link_conf->link_id]))
1144		return -EINVAL;
1145
1146	if (iwl_fw_lookup_cmd_ver(mvm->fw, BEACON_TEMPLATE_CMD, 0) > 12)
1147		beacon_cmd.link_id =
1148			cpu_to_le32(mvmvif->link[link_conf->link_id]->fw_link_id);
1149	else
1150		beacon_cmd.link_id = cpu_to_le32((u32)mvmvif->id);
1151
1152	if (vif->type == NL80211_IFTYPE_AP)
1153		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
1154					 &beacon_cmd.tim_size,
1155					 beacon->data, beacon->len);
1156
1157	beacon_cmd.csa_offset =
1158		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1159						   WLAN_EID_CHANNEL_SWITCH,
1160						   beacon->len));
1161	beacon_cmd.ecsa_offset =
1162		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1163						   WLAN_EID_EXT_CHANSWITCH_ANN,
1164						   beacon->len));
1165
1166	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
1167						sizeof(beacon_cmd));
1168}
1169
1170static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
1171					struct ieee80211_vif *vif,
1172					struct sk_buff *beacon,
1173					struct ieee80211_bss_conf *link_conf)
1174{
1175	if (WARN_ON(!beacon))
1176		return -EINVAL;
1177
1178	if (IWL_MVM_NON_TRANSMITTING_AP)
1179		return 0;
1180
1181	if (!fw_has_capa(&mvm->fw->ucode_capa,
1182			 IWL_UCODE_TLV_CAPA_CSA_AND_TBTT_OFFLOAD))
1183		return iwl_mvm_mac_ctxt_send_beacon_v6(mvm, vif, beacon);
1184
1185	if (fw_has_api(&mvm->fw->ucode_capa,
1186		       IWL_UCODE_TLV_API_NEW_BEACON_TEMPLATE))
1187		return iwl_mvm_mac_ctxt_send_beacon_v9(mvm, vif, beacon,
1188						       link_conf);
1189
1190	return iwl_mvm_mac_ctxt_send_beacon_v7(mvm, vif, beacon);
1191}
1192
1193/* The beacon template for the AP/GO/IBSS has changed and needs update */
1194int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
1195				    struct ieee80211_vif *vif,
1196				    struct ieee80211_bss_conf *link_conf)
1197{
1198	struct sk_buff *beacon;
1199	int ret;
1200
1201	WARN_ON(vif->type != NL80211_IFTYPE_AP &&
1202		vif->type != NL80211_IFTYPE_ADHOC);
1203
1204	beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL,
1205					       link_conf->link_id);
1206	if (!beacon)
1207		return -ENOMEM;
1208
1209#ifdef CONFIG_IWLWIFI_DEBUGFS
1210	if (mvm->beacon_inject_active) {
1211		dev_kfree_skb(beacon);
1212		return -EBUSY;
1213	}
1214#endif
1215
1216	ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon, link_conf);
1217	dev_kfree_skb(beacon);
1218	return ret;
1219}
1220
1221struct iwl_mvm_mac_ap_iterator_data {
1222	struct iwl_mvm *mvm;
1223	struct ieee80211_vif *vif;
1224	u32 beacon_device_ts;
1225	u16 beacon_int;
1226};
1227
1228/* Find the beacon_device_ts and beacon_int for a managed interface */
1229static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
1230				    struct ieee80211_vif *vif)
1231{
1232	struct iwl_mvm_mac_ap_iterator_data *data = _data;
1233
1234	if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc)
1235		return;
1236
1237	/* Station client has higher priority over P2P client*/
1238	if (vif->p2p && data->beacon_device_ts)
1239		return;
1240
1241	data->beacon_device_ts = vif->bss_conf.sync_device_ts;
1242	data->beacon_int = vif->bss_conf.beacon_int;
1243}
1244
1245/*
1246 * Fill the filter flags for mac context of type AP or P2P GO.
1247 */
1248void iwl_mvm_mac_ctxt_cmd_ap_set_filter_flags(struct iwl_mvm *mvm,
1249					      struct iwl_mvm_vif *mvmvif,
1250					      __le32 *filter_flags,
1251					      int accept_probe_req_flag,
1252					      int accept_beacon_flag)
1253{
1254	/*
1255	 * in AP mode, pass probe requests and beacons from other APs
1256	 * (needed for ht protection); when there're no any associated
1257	 * station don't ask FW to pass beacons to prevent unnecessary
1258	 * wake-ups.
1259	 */
1260	*filter_flags |= cpu_to_le32(accept_probe_req_flag);
1261	if (mvmvif->ap_assoc_sta_count || !mvm->drop_bcn_ap_mode) {
1262		*filter_flags |= cpu_to_le32(accept_beacon_flag);
1263		IWL_DEBUG_HC(mvm, "Asking FW to pass beacons\n");
1264	} else {
1265		IWL_DEBUG_HC(mvm, "No need to receive beacons\n");
1266	}
1267}
1268
1269/*
1270 * Fill the specific data for mac context of type AP of P2P GO
1271 */
1272static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
1273					 struct ieee80211_vif *vif,
1274					 struct iwl_mac_ctx_cmd *cmd,
1275					 struct iwl_mac_data_ap *ctxt_ap,
1276					 bool add)
1277{
1278	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1279	struct iwl_mvm_mac_ap_iterator_data data = {
1280		.mvm = mvm,
1281		.vif = vif,
1282		.beacon_device_ts = 0
1283	};
1284
1285	/* in AP mode, the MCAST FIFO takes the EDCA params from VO */
1286	cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |= BIT(IWL_MVM_TX_FIFO_MCAST);
1287
1288	iwl_mvm_mac_ctxt_cmd_ap_set_filter_flags(mvm, mvmvif,
1289						 &cmd->filter_flags,
1290						 MAC_FILTER_IN_PROBE_REQUEST,
1291						 MAC_FILTER_IN_BEACON);
1292
1293	ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
1294	ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
1295					     vif->bss_conf.dtim_period);
1296
1297	if (!fw_has_api(&mvm->fw->ucode_capa,
1298			IWL_UCODE_TLV_API_STA_TYPE))
1299		ctxt_ap->mcast_qid = cpu_to_le32(mvmvif->deflink.cab_queue);
1300
1301	/*
1302	 * Only set the beacon time when the MAC is being added, when we
1303	 * just modify the MAC then we should keep the time -- the firmware
1304	 * can otherwise have a "jumping" TBTT.
1305	 */
1306	if (add) {
1307		/*
1308		 * If there is a station/P2P client interface which is
1309		 * associated, set the AP's TBTT far enough from the station's
1310		 * TBTT. Otherwise, set it to the current system time
1311		 */
1312		ieee80211_iterate_active_interfaces_atomic(
1313			mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1314			iwl_mvm_mac_ap_iterator, &data);
1315
1316		if (data.beacon_device_ts) {
1317			u32 rand = get_random_u32_inclusive(36, 63);
1318			mvmvif->ap_beacon_time = data.beacon_device_ts +
1319				ieee80211_tu_to_usec(data.beacon_int * rand /
1320						     100);
1321		} else {
1322			mvmvif->ap_beacon_time = iwl_mvm_get_systime(mvm);
1323		}
1324	}
1325
1326	ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
1327	ctxt_ap->beacon_tsf = 0; /* unused */
1328
1329	/* TODO: Assume that the beacon id == mac context id */
1330	ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
1331}
1332
1333static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
1334				   struct ieee80211_vif *vif,
1335				   u32 action)
1336{
1337	struct iwl_mac_ctx_cmd cmd = {};
1338
1339	WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
1340
1341	/* Fill the common data for all mac context types */
1342	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1343
1344	/* Fill the data specific for ap mode */
1345	iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.ap,
1346				     action == FW_CTXT_ACTION_ADD);
1347
1348	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1349}
1350
1351static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
1352				   struct ieee80211_vif *vif,
1353				   u32 action)
1354{
1355	struct iwl_mac_ctx_cmd cmd = {};
1356	struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
1357
1358	WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
1359
1360	/* Fill the common data for all mac context types */
1361	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1362
1363	/* Fill the data specific for GO mode */
1364	iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.go.ap,
1365				     action == FW_CTXT_ACTION_ADD);
1366
1367	cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
1368					IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
1369	cmd.go.opp_ps_enabled =
1370			cpu_to_le32(!!(noa->oppps_ctwindow &
1371					IEEE80211_P2P_OPPPS_ENABLE_BIT));
1372
1373	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1374}
1375
1376static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1377				u32 action, bool force_assoc_off,
1378				const u8 *bssid_override)
1379{
1380	switch (vif->type) {
1381	case NL80211_IFTYPE_STATION:
1382		return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
1383						force_assoc_off,
1384						bssid_override);
1385	case NL80211_IFTYPE_AP:
1386		if (!vif->p2p)
1387			return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
1388		else
1389			return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
1390	case NL80211_IFTYPE_MONITOR:
1391		return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
1392	case NL80211_IFTYPE_P2P_DEVICE:
1393		return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
1394	case NL80211_IFTYPE_ADHOC:
1395		return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
1396	default:
1397		break;
1398	}
1399
1400	return -EOPNOTSUPP;
1401}
1402
1403int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1404{
1405	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1406	int ret;
1407
1408	if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
1409		      vif->addr, ieee80211_vif_type_p2p(vif)))
1410		return -EIO;
1411
1412	ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
1413				   true, NULL);
1414	if (ret)
1415		return ret;
1416
1417	/* will only do anything at resume from D3 time */
1418	iwl_mvm_set_last_nonqos_seq(mvm, vif);
1419
1420	mvmvif->uploaded = true;
1421	return 0;
1422}
1423
1424int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1425			     bool force_assoc_off, const u8 *bssid_override)
1426{
1427	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1428
1429	if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
1430		      vif->addr, ieee80211_vif_type_p2p(vif)))
1431		return -EIO;
1432
1433	return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
1434				    force_assoc_off, bssid_override);
1435}
1436
1437int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1438{
1439	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1440	struct iwl_mac_ctx_cmd cmd;
1441	int ret;
1442
1443	if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1444		      vif->addr, ieee80211_vif_type_p2p(vif)))
1445		return -EIO;
1446
1447	memset(&cmd, 0, sizeof(cmd));
1448
1449	cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1450							   mvmvif->color));
1451	cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1452
1453	ret = iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1454	if (ret)
1455		return ret;
1456
1457	mvmvif->uploaded = false;
1458
1459	if (vif->type == NL80211_IFTYPE_MONITOR) {
1460		__clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, mvm->hw->flags);
1461		iwl_mvm_dealloc_snif_sta(mvm);
1462	}
1463
1464	return 0;
1465}
1466
1467static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
1468				   struct ieee80211_vif *csa_vif, u32 gp2,
1469				   bool tx_success)
1470{
1471	struct iwl_mvm_vif *mvmvif =
1472			iwl_mvm_vif_from_mac80211(csa_vif);
1473
1474	/* Don't start to countdown from a failed beacon */
1475	if (!tx_success && !mvmvif->csa_countdown)
1476		return;
1477
1478	mvmvif->csa_countdown = true;
1479
1480	if (!ieee80211_beacon_cntdwn_is_complete(csa_vif)) {
1481		int c = ieee80211_beacon_update_cntdwn(csa_vif);
1482
1483		iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif,
1484						&csa_vif->bss_conf);
1485		if (csa_vif->p2p &&
1486		    !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2 &&
1487		    tx_success) {
1488			u32 rel_time = (c + 1) *
1489				       csa_vif->bss_conf.beacon_int -
1490				       IWL_MVM_CHANNEL_SWITCH_TIME_GO;
1491			u32 apply_time = gp2 + rel_time * 1024;
1492
1493			iwl_mvm_schedule_csa_period(mvm, csa_vif,
1494					 IWL_MVM_CHANNEL_SWITCH_TIME_GO -
1495					 IWL_MVM_CHANNEL_SWITCH_MARGIN,
1496					 apply_time);
1497		}
1498	} else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
1499		/* we don't have CSA NoA scheduled yet, switch now */
1500		ieee80211_csa_finish(csa_vif);
1501		RCU_INIT_POINTER(mvm->csa_vif, NULL);
1502	}
1503}
1504
1505void iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1506			     struct iwl_rx_cmd_buffer *rxb)
1507{
1508	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1509	unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
1510	struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
1511	struct iwl_extended_beacon_notif_v5 *beacon_v5 = (void *)pkt->data;
1512	struct ieee80211_vif *csa_vif;
1513	struct ieee80211_vif *tx_blocked_vif;
1514	struct agg_tx_status *agg_status;
1515	u16 status;
1516
1517	lockdep_assert_held(&mvm->mutex);
1518
1519	mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
1520
1521	if (!iwl_mvm_is_short_beacon_notif_supported(mvm)) {
1522		struct iwl_mvm_tx_resp *beacon_notify_hdr =
1523			&beacon_v5->beacon_notify_hdr;
1524
1525		if (unlikely(pkt_len < sizeof(*beacon_v5)))
1526			return;
1527
1528		mvm->ibss_manager = beacon_v5->ibss_mgr_status != 0;
1529		agg_status = iwl_mvm_get_agg_status(mvm, beacon_notify_hdr);
1530		status = le16_to_cpu(agg_status->status) & TX_STATUS_MSK;
1531		IWL_DEBUG_RX(mvm,
1532			     "beacon status %#x retries:%d tsf:0x%016llX gp2:0x%X rate:%d\n",
1533			     status, beacon_notify_hdr->failure_frame,
1534			     le64_to_cpu(beacon->tsf),
1535			     mvm->ap_last_beacon_gp2,
1536			     le32_to_cpu(beacon_notify_hdr->initial_rate));
1537	} else {
1538		if (unlikely(pkt_len < sizeof(*beacon)))
1539			return;
1540
1541		mvm->ibss_manager = beacon->ibss_mgr_status != 0;
1542		status = le32_to_cpu(beacon->status) & TX_STATUS_MSK;
1543		IWL_DEBUG_RX(mvm,
1544			     "beacon status %#x tsf:0x%016llX gp2:0x%X\n",
1545			     status, le64_to_cpu(beacon->tsf),
1546			     mvm->ap_last_beacon_gp2);
1547	}
1548
1549	csa_vif = rcu_dereference_protected(mvm->csa_vif,
1550					    lockdep_is_held(&mvm->mutex));
1551	if (unlikely(csa_vif && csa_vif->bss_conf.csa_active))
1552		iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2,
1553				       (status == TX_STATUS_SUCCESS));
1554
1555	tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
1556						lockdep_is_held(&mvm->mutex));
1557	if (unlikely(tx_blocked_vif)) {
1558		struct iwl_mvm_vif *mvmvif =
1559			iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1560
1561		/*
1562		 * The channel switch is started and we have blocked the
1563		 * stations. If this is the first beacon (the timeout wasn't
1564		 * set), set the unblock timeout, otherwise countdown
1565		 */
1566		if (!mvm->csa_tx_block_bcn_timeout)
1567			mvm->csa_tx_block_bcn_timeout =
1568				IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
1569		else
1570			mvm->csa_tx_block_bcn_timeout--;
1571
1572		/* Check if the timeout is expired, and unblock tx */
1573		if (mvm->csa_tx_block_bcn_timeout == 0) {
1574			iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
1575			RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1576		}
1577	}
1578}
1579
1580void iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
1581				     struct iwl_rx_cmd_buffer *rxb)
1582{
1583	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1584	struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
1585	struct iwl_fw_dbg_trigger_missed_bcon *bcon_trig;
1586	struct iwl_fw_dbg_trigger_tlv *trigger;
1587	u32 stop_trig_missed_bcon, stop_trig_missed_bcon_since_rx;
1588	u32 rx_missed_bcon, rx_missed_bcon_since_rx;
1589	struct ieee80211_vif *vif;
1590	/* Id can be mac/link id depending on the notification version */
1591	u32 id = le32_to_cpu(mb->link_id);
1592	union iwl_dbg_tlv_tp_data tp_data = { .fw_pkt = pkt };
1593	u32 mac_type;
1594	u8 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
1595					       MISSED_BEACONS_NOTIFICATION,
1596					       0);
1597
1598	rcu_read_lock();
1599
1600	/* before version four the ID in the notification refers to mac ID */
1601	if (notif_ver < 4) {
1602		vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);
1603	} else {
1604		struct ieee80211_bss_conf *bss_conf =
1605			iwl_mvm_rcu_fw_link_id_to_link_conf(mvm, id, true);
1606
1607		if (!bss_conf)
1608			goto out;
1609
1610		vif = bss_conf->vif;
1611	}
1612
1613	IWL_DEBUG_INFO(mvm,
1614		       "missed bcn %s_id=%u, consecutive=%u (%u, %u, %u)\n",
1615		       notif_ver < 4 ? "mac" : "link",
1616		       id,
1617		       le32_to_cpu(mb->consec_missed_beacons),
1618		       le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
1619		       le32_to_cpu(mb->num_recvd_beacons),
1620		       le32_to_cpu(mb->num_expected_beacons));
1621
1622	if (!vif)
1623		goto out;
1624
1625	mac_type = iwl_mvm_get_mac_type(vif);
1626
1627	IWL_DEBUG_INFO(mvm, "missed beacon mac_type=%u,\n", mac_type);
1628
1629	mvm->trans->dbg.dump_file_name_ext_valid = true;
1630	snprintf(mvm->trans->dbg.dump_file_name_ext, IWL_FW_INI_MAX_NAME,
1631		 "MacId_%d_MacType_%d", id, mac_type);
1632
1633	rx_missed_bcon = le32_to_cpu(mb->consec_missed_beacons);
1634	rx_missed_bcon_since_rx =
1635		le32_to_cpu(mb->consec_missed_beacons_since_last_rx);
1636	/*
1637	 * TODO: the threshold should be adjusted based on latency conditions,
1638	 * and/or in case of a CS flow on one of the other AP vifs.
1639	 */
1640	if (rx_missed_bcon > IWL_MVM_MISSED_BEACONS_THRESHOLD_LONG)
1641		iwl_mvm_connection_loss(mvm, vif, "missed beacons");
1642	else if (rx_missed_bcon_since_rx > IWL_MVM_MISSED_BEACONS_THRESHOLD)
1643		ieee80211_beacon_loss(vif);
1644
1645	iwl_dbg_tlv_time_point(&mvm->fwrt,
1646			       IWL_FW_INI_TIME_POINT_MISSED_BEACONS, &tp_data);
1647
1648	trigger = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
1649					FW_DBG_TRIGGER_MISSED_BEACONS);
1650	if (!trigger)
1651		goto out;
1652
1653	bcon_trig = (void *)trigger->data;
1654	stop_trig_missed_bcon = le32_to_cpu(bcon_trig->stop_consec_missed_bcon);
1655	stop_trig_missed_bcon_since_rx =
1656		le32_to_cpu(bcon_trig->stop_consec_missed_bcon_since_rx);
1657
1658	/* TODO: implement start trigger */
1659
1660	if (rx_missed_bcon_since_rx >= stop_trig_missed_bcon_since_rx ||
1661	    rx_missed_bcon >= stop_trig_missed_bcon)
1662		iwl_fw_dbg_collect_trig(&mvm->fwrt, trigger, NULL);
1663
1664out:
1665	rcu_read_unlock();
1666}
1667
1668void iwl_mvm_rx_stored_beacon_notif(struct iwl_mvm *mvm,
1669				    struct iwl_rx_cmd_buffer *rxb)
1670{
1671	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1672	unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
1673	struct iwl_stored_beacon_notif_common *sb = (void *)pkt->data;
1674	struct ieee80211_rx_status rx_status;
1675	struct sk_buff *skb;
1676	u8 *data;
1677	u32 size = le32_to_cpu(sb->byte_count);
1678	int ver = iwl_fw_lookup_cmd_ver(mvm->fw,
1679					WIDE_ID(PROT_OFFLOAD_GROUP, STORED_BEACON_NTF),
1680					0);
1681
1682	if (size == 0)
1683		return;
1684
1685	/* handle per-version differences */
1686	if (ver <= 2) {
1687		struct iwl_stored_beacon_notif_v2 *sb_v2 = (void *)pkt->data;
1688
1689		if (pkt_len < struct_size(sb_v2, data, size))
1690			return;
1691
1692		data = sb_v2->data;
1693	} else {
1694		struct iwl_stored_beacon_notif_v3 *sb_v3 = (void *)pkt->data;
1695
1696		if (pkt_len < struct_size(sb_v3, data, size))
1697			return;
1698
1699		data = sb_v3->data;
1700	}
1701
1702	skb = alloc_skb(size, GFP_ATOMIC);
1703	if (!skb) {
1704		IWL_ERR(mvm, "alloc_skb failed\n");
1705		return;
1706	}
1707
1708	/* update rx_status according to the notification's metadata */
1709	memset(&rx_status, 0, sizeof(rx_status));
1710	rx_status.mactime = le64_to_cpu(sb->tsf);
1711	/* TSF as indicated by the firmware  is at INA time */
1712	rx_status.flag |= RX_FLAG_MACTIME_PLCP_START;
1713	rx_status.device_timestamp = le32_to_cpu(sb->system_time);
1714	rx_status.band =
1715		(sb->band & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
1716				NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
1717	rx_status.freq =
1718		ieee80211_channel_to_frequency(le16_to_cpu(sb->channel),
1719					       rx_status.band);
1720
1721	/* copy the data */
1722	skb_put_data(skb, data, size);
1723	memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
1724
1725	/* pass it as regular rx to mac80211 */
1726	ieee80211_rx_napi(mvm->hw, NULL, skb, NULL);
1727}
1728
1729void iwl_mvm_probe_resp_data_notif(struct iwl_mvm *mvm,
1730				   struct iwl_rx_cmd_buffer *rxb)
1731{
1732	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1733	struct iwl_probe_resp_data_notif *notif = (void *)pkt->data;
1734	struct iwl_probe_resp_data *old_data, *new_data;
1735	u32 id = le32_to_cpu(notif->mac_id);
1736	struct ieee80211_vif *vif;
1737	struct iwl_mvm_vif *mvmvif;
1738
1739	IWL_DEBUG_INFO(mvm, "Probe response data notif: noa %d, csa %d\n",
1740		       notif->noa_active, notif->csa_counter);
1741
1742	vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, false);
1743	if (!vif)
1744		return;
1745
1746	mvmvif = iwl_mvm_vif_from_mac80211(vif);
1747
1748	new_data = kzalloc(sizeof(*new_data), GFP_KERNEL);
1749	if (!new_data)
1750		return;
1751
1752	memcpy(&new_data->notif, notif, sizeof(new_data->notif));
1753
1754	/* noa_attr contains 1 reserved byte, need to substruct it */
1755	new_data->noa_len = sizeof(struct ieee80211_vendor_ie) +
1756			    sizeof(new_data->notif.noa_attr) - 1;
1757
1758	/*
1759	 * If it's a one time NoA, only one descriptor is needed,
1760	 * adjust the length according to len_low.
1761	 */
1762	if (new_data->notif.noa_attr.len_low ==
1763	    sizeof(struct ieee80211_p2p_noa_desc) + 2)
1764		new_data->noa_len -= sizeof(struct ieee80211_p2p_noa_desc);
1765
1766	old_data = rcu_dereference_protected(mvmvif->deflink.probe_resp_data,
1767					     lockdep_is_held(&mvmvif->mvm->mutex));
1768	rcu_assign_pointer(mvmvif->deflink.probe_resp_data, new_data);
1769
1770	if (old_data)
1771		kfree_rcu(old_data, rcu_head);
1772
1773	if (notif->csa_counter != IWL_PROBE_RESP_DATA_NO_CSA &&
1774	    notif->csa_counter >= 1)
1775		ieee80211_beacon_set_cntdwn(vif, notif->csa_counter);
1776}
1777
1778void iwl_mvm_channel_switch_start_notif(struct iwl_mvm *mvm,
1779					struct iwl_rx_cmd_buffer *rxb)
1780{
1781	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1782	struct ieee80211_vif *csa_vif, *vif;
1783	struct iwl_mvm_vif *mvmvif, *csa_mvmvif;
1784	u32 id_n_color, csa_id;
1785	/* save mac_id or link_id to use later to cancel csa if needed */
1786	u32 id;
1787	u32 mac_link_id = 0;
1788	u8 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
1789					       CHANNEL_SWITCH_START_NOTIF, 0);
1790	bool csa_active;
1791
1792	rcu_read_lock();
1793
1794	if (notif_ver < 3) {
1795		struct iwl_channel_switch_start_notif_v1 *notif = (void *)pkt->data;
1796		u32 mac_id;
1797
1798		id_n_color = le32_to_cpu(notif->id_and_color);
1799		mac_id = id_n_color & FW_CTXT_ID_MSK;
1800
1801		vif = iwl_mvm_rcu_dereference_vif_id(mvm, mac_id, true);
1802		if (!vif)
1803			goto out_unlock;
1804
1805		id = mac_id;
1806		csa_active = vif->bss_conf.csa_active;
1807	} else {
1808		struct iwl_channel_switch_start_notif *notif = (void *)pkt->data;
1809		u32 link_id = le32_to_cpu(notif->link_id);
1810		struct ieee80211_bss_conf *bss_conf =
1811			iwl_mvm_rcu_fw_link_id_to_link_conf(mvm, link_id, true);
1812
1813		if (!bss_conf)
1814			goto out_unlock;
1815
1816		id = link_id;
1817		mac_link_id = bss_conf->link_id;
1818		vif = bss_conf->vif;
1819		csa_active = bss_conf->csa_active;
1820	}
1821
1822	mvmvif = iwl_mvm_vif_from_mac80211(vif);
1823	if (notif_ver >= 3)
1824		id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
1825
1826	switch (vif->type) {
1827	case NL80211_IFTYPE_AP:
1828		csa_vif = rcu_dereference(mvm->csa_vif);
1829		if (WARN_ON(!csa_vif || !csa_vif->bss_conf.csa_active ||
1830			    csa_vif != vif))
1831			goto out_unlock;
1832
1833		csa_mvmvif = iwl_mvm_vif_from_mac80211(csa_vif);
1834		csa_id = FW_CMD_ID_AND_COLOR(csa_mvmvif->id, csa_mvmvif->color);
1835		if (WARN(csa_id != id_n_color,
1836			 "channel switch noa notification on unexpected vif (csa_vif=%d, notif=%d)",
1837			 csa_id, id_n_color))
1838			goto out_unlock;
1839
1840		IWL_DEBUG_INFO(mvm, "Channel Switch Started Notification\n");
1841
1842		schedule_delayed_work(&mvm->cs_tx_unblock_dwork,
1843				      msecs_to_jiffies(IWL_MVM_CS_UNBLOCK_TX_TIMEOUT *
1844						       csa_vif->bss_conf.beacon_int));
1845
1846		ieee80211_csa_finish(csa_vif);
1847
1848		rcu_read_unlock();
1849
1850		RCU_INIT_POINTER(mvm->csa_vif, NULL);
1851		return;
1852	case NL80211_IFTYPE_STATION:
1853		/*
1854		 * if we don't know about an ongoing channel switch,
1855		 * make sure FW cancels it
1856		 */
1857		if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
1858					    CHANNEL_SWITCH_ERROR_NOTIF,
1859					    0) && !csa_active) {
1860			IWL_DEBUG_INFO(mvm, "Channel Switch was canceled\n");
1861			iwl_mvm_cancel_channel_switch(mvm, vif, id);
1862			break;
1863		}
1864
1865		iwl_mvm_csa_client_absent(mvm, vif);
1866		cancel_delayed_work(&mvmvif->csa_work);
1867		ieee80211_chswitch_done(vif, true, mac_link_id);
1868		break;
1869	default:
1870		/* should never happen */
1871		WARN_ON_ONCE(1);
1872		break;
1873	}
1874out_unlock:
1875	rcu_read_unlock();
1876}
1877
1878void iwl_mvm_channel_switch_error_notif(struct iwl_mvm *mvm,
1879					struct iwl_rx_cmd_buffer *rxb)
1880{
1881	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1882	struct iwl_channel_switch_error_notif *notif = (void *)pkt->data;
1883	struct ieee80211_vif *vif;
1884	u32 id = le32_to_cpu(notif->link_id);
1885	u32 csa_err_mask = le32_to_cpu(notif->csa_err_mask);
1886
1887	rcu_read_lock();
1888	vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);
1889	if (!vif) {
1890		rcu_read_unlock();
1891		return;
1892	}
1893
1894	IWL_DEBUG_INFO(mvm, "FW reports CSA error: id=%u, csa_err_mask=%u\n",
1895		       id, csa_err_mask);
1896	if (csa_err_mask & (CS_ERR_COUNT_ERROR |
1897			    CS_ERR_LONG_DELAY_AFTER_CS |
1898			    CS_ERR_TX_BLOCK_TIMER_EXPIRED))
1899		ieee80211_channel_switch_disconnect(vif, true);
1900	rcu_read_unlock();
1901}
1902
1903void iwl_mvm_rx_missed_vap_notif(struct iwl_mvm *mvm,
1904				 struct iwl_rx_cmd_buffer *rxb)
1905{
1906	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1907	struct iwl_missed_vap_notif *mb = (void *)pkt->data;
1908	struct ieee80211_vif *vif;
1909	u32 id = le32_to_cpu(mb->mac_id);
1910
1911	IWL_DEBUG_INFO(mvm,
1912		       "missed_vap notify mac_id=%u, num_beacon_intervals_elapsed=%u, profile_periodicity=%u\n",
1913		       le32_to_cpu(mb->mac_id),
1914		       mb->num_beacon_intervals_elapsed,
1915		       mb->profile_periodicity);
1916
1917	rcu_read_lock();
1918
1919	vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);
1920	if (vif)
1921		iwl_mvm_connection_loss(mvm, vif, "missed vap beacon");
1922
1923	rcu_read_unlock();
1924}