brintos

brintos / linux-shallow public Read only

0
0
Text · 58.7 KiB · a7a10e7 Raw
1996 lines · c
1// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause2/*3 * Copyright (C) 2012-2014, 2018-2024 Intel Corporation4 * Copyright (C) 2013-2014 Intel Mobile Communications GmbH5 * Copyright (C) 2015-2017 Intel Deutschland GmbH6 */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 and67	 * the driver should assign and free them as needed. However,68	 * there are cases where 2 MACs should share the same TSF ID69	 * for the purpose of clock sync, an optimization to avoid70	 * clock drift causing overlapping TBTTs/DTIMs for a GO and71	 * client in the system.72	 *73	 * The firmware will decide according to the MAC type which74	 * will be the leader and follower. Clients that need to sync75	 * with a remote station will be the leader, and an AP or GO76	 * will be the follower.77	 *78	 * Depending on the new interface type it can be following79	 * 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 iterating85		 * is an AP, and the beacon interval of the AP is a multiple or86		 * divisor of the beacon interval of the client, the same TSF87		 * should be used to avoid drift between the new client and88		 * existing AP. The existing AP will get drift updates from the89		 * 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 a112		 * multiple or a divisor of the beacon interval of an existing113		 * interface, it should get drift updates from an existing114		 * client or use the same TSF as an existing GO. There's no115		 * drift between TSFs internally but if they used different116		 * TSFs then a new client MAC could update one of them and117		 * 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 to140		 * take drift into account. Either they're exclusive141		 * like IBSS and monitor, or we don't care much about142		 * their TSF (like P2P Device), but we won't be able143		 * to share the TSF resource.144		 */145		break;146	}147 148	/*149	 * Unless we exited above, we can't share the TSF resource150	 * that the virtual interface we're iterating over is using151	 * with the new one, so clear the available bit and if this152	 * 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, and173	 * (below) mark TSFs as used if their existing use is not174	 * compatible with the new interface type.175	 * No locking or atomic bit operations are needed since the176	 * 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 queues225	 * 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 other232	 * virtual interfaces, and all TSF IDs are cleared that can't be used233	 * by this new virtual interface because they're used by an interface234	 * that can't share it with the new one.235	 * At the same time, we check if there's a preferred TSF in the case236	 * 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-MLO240	 * FW API241	 */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 to261	 * firmware restart, and with RESUME_ALL the iterator will find262	 * the vif being added already.263	 * We don't want to reassign any IDs in either case since doing264	 * so would probably assign different IDs (as interfaces aren't265	 * necessarily added in the same order), but the old IDs were266	 * preserved anyway, so skip ID assignment for both resume and267	 * 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	else287		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	mvmvif->roc_activity = ROC_NUM_ACTIVITIES;300 301	mvmvif->deflink.bcast_sta.sta_id = IWL_MVM_INVALID_STA;302	mvmvif->deflink.mcast_sta.sta_id = IWL_MVM_INVALID_STA;303	mvmvif->deflink.ap_sta_id = IWL_MVM_INVALID_STA;304 305	/* No need to allocate data queues to P2P Device MAC and NAN.*/306	if (vif->type == NL80211_IFTYPE_P2P_DEVICE)307		return 0;308 309	/* Allocate the CAB queue for softAP and GO interfaces */310	if (vif->type == NL80211_IFTYPE_AP ||311	    vif->type == NL80211_IFTYPE_ADHOC) {312		/*313		 * For TVQM this will be overwritten later with the FW assigned314		 * queue value (when queue is enabled).315		 */316		mvmvif->deflink.cab_queue = IWL_MVM_DQA_GCAST_QUEUE;317	}318 319	for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)320		mvmvif->deflink.smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;321 322	return 0;323 324exit_fail:325	memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));326	return ret;327}328 329static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,330			      struct ieee80211_vif *vif,331			      enum nl80211_band band,332			      u8 *cck_rates, u8 *ofdm_rates)333{334	struct ieee80211_supported_band *sband;335	unsigned long basic = vif->bss_conf.basic_rates;336	int lowest_present_ofdm = 100;337	int lowest_present_cck = 100;338	u8 cck = 0;339	u8 ofdm = 0;340	int i;341 342	sband = mvm->hw->wiphy->bands[band];343 344	for_each_set_bit(i, &basic, BITS_PER_LONG) {345		int hw = sband->bitrates[i].hw_value;346		if (hw >= IWL_FIRST_OFDM_RATE) {347			ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);348			if (lowest_present_ofdm > hw)349				lowest_present_ofdm = hw;350		} else {351			BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);352 353			cck |= BIT(hw);354			if (lowest_present_cck > hw)355				lowest_present_cck = hw;356		}357	}358 359	/*360	 * Now we've got the basic rates as bitmaps in the ofdm and cck361	 * variables. This isn't sufficient though, as there might not362	 * be all the right rates in the bitmap. E.g. if the only basic363	 * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps364	 * and 6 Mbps because the 802.11-2007 standard says in 9.6:365	 *366	 *    [...] a STA responding to a received frame shall transmit367	 *    its Control Response frame [...] at the highest rate in the368	 *    BSSBasicRateSet parameter that is less than or equal to the369	 *    rate of the immediately previous frame in the frame exchange370	 *    sequence ([...]) and that is of the same modulation class371	 *    ([...]) as the received frame. If no rate contained in the372	 *    BSSBasicRateSet parameter meets these conditions, then the373	 *    control frame sent in response to a received frame shall be374	 *    transmitted at the highest mandatory rate of the PHY that is375	 *    less than or equal to the rate of the received frame, and376	 *    that is of the same modulation class as the received frame.377	 *378	 * As a consequence, we need to add all mandatory rates that are379	 * lower than all of the basic rates to these bitmaps.380	 */381 382	if (IWL_RATE_24M_INDEX < lowest_present_ofdm)383		ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;384	if (IWL_RATE_12M_INDEX < lowest_present_ofdm)385		ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;386	/* 6M already there or needed so always add */387	ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;388 389	/*390	 * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.391	 * Note, however:392	 *  - if no CCK rates are basic, it must be ERP since there must393	 *    be some basic rates at all, so they're OFDM => ERP PHY394	 *    (or we're in 5 GHz, and the cck bitmap will never be used)395	 *  - if 11M is a basic rate, it must be ERP as well, so add 5.5M396	 *  - if 5.5M is basic, 1M and 2M are mandatory397	 *  - if 2M is basic, 1M is mandatory398	 *  - if 1M is basic, that's the only valid ACK rate.399	 * As a consequence, it's not as complicated as it sounds, just add400	 * any lower rates to the ACK rate bitmap.401	 */402	if (IWL_RATE_11M_INDEX < lowest_present_cck)403		cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;404	if (IWL_RATE_5M_INDEX < lowest_present_cck)405		cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;406	if (IWL_RATE_2M_INDEX < lowest_present_cck)407		cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;408	/* 1M already there or needed so always add */409	cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;410 411	*cck_rates = cck;412	*ofdm_rates = ofdm;413}414 415void iwl_mvm_set_fw_basic_rates(struct iwl_mvm *mvm, struct ieee80211_vif *vif,416				struct iwl_mvm_vif_link_info *link_info,417				__le32 *cck_rates, __le32 *ofdm_rates)418{419	struct iwl_mvm_phy_ctxt *phy_ctxt;420	u8 cck_ack_rates = 0, ofdm_ack_rates = 0;421	enum nl80211_band band = NL80211_BAND_2GHZ;422 423	phy_ctxt = link_info->phy_ctxt;424	if (phy_ctxt && phy_ctxt->channel)425		band = phy_ctxt->channel->band;426 427	iwl_mvm_ack_rates(mvm, vif, band, &cck_ack_rates, &ofdm_ack_rates);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->chanreq.oper.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->chanreq.oper.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		else521			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	else563		eth_broadcast_addr(cmd->bssid_addr);564 565	iwl_mvm_set_fw_basic_rates(mvm, vif, &mvmvif->deflink, &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 N607	 * more beacon intervals happen until the DTIM TBTT. Therefore608	 * add this to the current time. If that ends up being in the609	 * future, the firmware will handle it.610	 *611	 * Also note that the system_timestamp (which we get here as612	 * "sync_device_ts") and TSF timestamp aren't at exactly the613	 * same offset in the frame -- the TSF is at the first symbol614	 * of the TSF, the system timestamp is at signal acquisition615	 * time. This means there's an offset between them of at most616	 * a few hundred microseconds (24 * 8 bits + PLCP time gives617	 * 384us in the longest case), this is currently not relevant618	 * 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 not712		 * 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, and759	 * mvm->snif_queue isn't set here (it's still set to760	 * 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 is819	 * discoverable and there is at least another active P2P GO. Settings820	 * this flag will allow the P2P Device to be discoverable on other821	 * channels in addition to its listen channel.822	 * Note that this flag should not be set in other cases as it opens the823	 * 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 the859	 * 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 876u32 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->chanreq.oper.chan)925				band = link_conf->chanreq.oper.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		else950			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_CCK967			  : 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 1021	rate = iwl_mvm_mac_ctxt_get_beacon_rate(mvm, info, vif);1022 1023	tx->rate_n_flags |=1024		cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(mvm->fw, rate));1025	if (rate == IWL_FIRST_CCK_RATE)1026		tx->rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK_V1);1027 1028}1029 1030int iwl_mvm_mac_ctxt_send_beacon_cmd(struct iwl_mvm *mvm,1031				     struct sk_buff *beacon,1032				     void *data, int len)1033{1034	struct iwl_host_cmd cmd = {1035		.id = BEACON_TEMPLATE_CMD,1036		.flags = CMD_ASYNC,1037	};1038 1039	cmd.len[0] = len;1040	cmd.data[0] = data;1041	cmd.dataflags[0] = 0;1042	cmd.len[1] = beacon->len;1043	cmd.data[1] = beacon->data;1044	cmd.dataflags[1] = IWL_HCMD_DFL_DUP;1045 1046	return iwl_mvm_send_cmd(mvm, &cmd);1047}1048 1049static int iwl_mvm_mac_ctxt_send_beacon_v6(struct iwl_mvm *mvm,1050					   struct ieee80211_vif *vif,1051					   struct sk_buff *beacon)1052{1053	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);1054	struct iwl_mac_beacon_cmd_v6 beacon_cmd = {};1055 1056	iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);1057 1058	beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);1059 1060	if (vif->type == NL80211_IFTYPE_AP)1061		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,1062					 &beacon_cmd.tim_size,1063					 beacon->data, beacon->len);1064 1065	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,1066						sizeof(beacon_cmd));1067}1068 1069static int iwl_mvm_mac_ctxt_send_beacon_v7(struct iwl_mvm *mvm,1070					   struct ieee80211_vif *vif,1071					   struct sk_buff *beacon)1072{1073	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);1074	struct iwl_mac_beacon_cmd_v7 beacon_cmd = {};1075 1076	iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);1077 1078	beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);1079 1080	if (vif->type == NL80211_IFTYPE_AP)1081		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,1082					 &beacon_cmd.tim_size,1083					 beacon->data, beacon->len);1084 1085	beacon_cmd.csa_offset =1086		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,1087						   WLAN_EID_CHANNEL_SWITCH,1088						   beacon->len));1089	beacon_cmd.ecsa_offset =1090		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,1091						   WLAN_EID_EXT_CHANSWITCH_ANN,1092						   beacon->len));1093 1094	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,1095						sizeof(beacon_cmd));1096}1097 1098bool iwl_mvm_enable_fils(struct iwl_mvm *mvm,1099			 struct ieee80211_chanctx_conf *ctx)1100{1101	if (IWL_MVM_DISABLE_AP_FILS)1102		return false;1103 1104	if (cfg80211_channel_is_psc(ctx->def.chan))1105		return true;1106 1107	return (ctx->def.chan->band == NL80211_BAND_6GHZ &&1108		ctx->def.width >= NL80211_CHAN_WIDTH_80);1109}1110 1111static int iwl_mvm_mac_ctxt_send_beacon_v9(struct iwl_mvm *mvm,1112					   struct ieee80211_vif *vif,1113					   struct sk_buff *beacon,1114					   struct ieee80211_bss_conf *link_conf)1115{1116	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);1117	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(beacon);1118	struct iwl_mac_beacon_cmd beacon_cmd = {};1119	u8 rate = iwl_mvm_mac_ctxt_get_beacon_rate(mvm, info, vif);1120	u16 flags;1121	struct ieee80211_chanctx_conf *ctx;1122	int channel;1123	flags = iwl_mvm_mac_ctxt_get_beacon_flags(mvm->fw, rate);1124 1125	/* Enable FILS on PSC channels only */1126	rcu_read_lock();1127	ctx = rcu_dereference(link_conf->chanctx_conf);1128	channel = ieee80211_frequency_to_channel(ctx->def.chan->center_freq);1129	WARN_ON(channel == 0);1130	if (iwl_mvm_enable_fils(mvm, ctx)) {1131		flags |= iwl_fw_lookup_cmd_ver(mvm->fw, BEACON_TEMPLATE_CMD,1132					       0) > 10 ?1133			IWL_MAC_BEACON_FILS :1134			IWL_MAC_BEACON_FILS_V1;1135		beacon_cmd.short_ssid =1136			cpu_to_le32(~crc32_le(~0, vif->cfg.ssid,1137					      vif->cfg.ssid_len));1138	}1139	rcu_read_unlock();1140 1141	beacon_cmd.flags = cpu_to_le16(flags);1142	beacon_cmd.byte_cnt = cpu_to_le16((u16)beacon->len);1143 1144	if (WARN_ON(!mvmvif->link[link_conf->link_id]))1145		return -EINVAL;1146 1147	if (iwl_fw_lookup_cmd_ver(mvm->fw, BEACON_TEMPLATE_CMD, 0) > 12)1148		beacon_cmd.link_id =1149			cpu_to_le32(mvmvif->link[link_conf->link_id]->fw_link_id);1150	else1151		beacon_cmd.link_id = cpu_to_le32((u32)mvmvif->id);1152 1153	if (vif->type == NL80211_IFTYPE_AP)1154		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,1155					 &beacon_cmd.tim_size,1156					 beacon->data, beacon->len);1157 1158	beacon_cmd.csa_offset =1159		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,1160						   WLAN_EID_CHANNEL_SWITCH,1161						   beacon->len));1162	beacon_cmd.ecsa_offset =1163		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,1164						   WLAN_EID_EXT_CHANSWITCH_ANN,1165						   beacon->len));1166 1167	if (vif->type == NL80211_IFTYPE_AP &&1168	    iwl_fw_lookup_cmd_ver(mvm->fw, BEACON_TEMPLATE_CMD, 0) >= 14)1169		beacon_cmd.btwt_offset =1170			cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,1171							   WLAN_EID_S1G_TWT,1172							   beacon->len));1173 1174	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,1175						sizeof(beacon_cmd));1176}1177 1178static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,1179					struct ieee80211_vif *vif,1180					struct sk_buff *beacon,1181					struct ieee80211_bss_conf *link_conf)1182{1183	if (WARN_ON(!beacon))1184		return -EINVAL;1185 1186	if (IWL_MVM_NON_TRANSMITTING_AP)1187		return 0;1188 1189	if (!fw_has_capa(&mvm->fw->ucode_capa,1190			 IWL_UCODE_TLV_CAPA_CSA_AND_TBTT_OFFLOAD))1191		return iwl_mvm_mac_ctxt_send_beacon_v6(mvm, vif, beacon);1192 1193	if (fw_has_api(&mvm->fw->ucode_capa,1194		       IWL_UCODE_TLV_API_NEW_BEACON_TEMPLATE))1195		return iwl_mvm_mac_ctxt_send_beacon_v9(mvm, vif, beacon,1196						       link_conf);1197 1198	return iwl_mvm_mac_ctxt_send_beacon_v7(mvm, vif, beacon);1199}1200 1201/* The beacon template for the AP/GO/IBSS has changed and needs update */1202int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,1203				    struct ieee80211_vif *vif,1204				    struct ieee80211_bss_conf *link_conf)1205{1206	struct sk_buff *beacon;1207	int ret;1208 1209	WARN_ON(vif->type != NL80211_IFTYPE_AP &&1210		vif->type != NL80211_IFTYPE_ADHOC);1211 1212	beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL,1213					       link_conf->link_id);1214	if (!beacon)1215		return -ENOMEM;1216 1217#ifdef CONFIG_IWLWIFI_DEBUGFS1218	if (mvm->beacon_inject_active) {1219		dev_kfree_skb(beacon);1220		return -EBUSY;1221	}1222#endif1223 1224	ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon, link_conf);1225	dev_kfree_skb(beacon);1226	return ret;1227}1228 1229struct iwl_mvm_mac_ap_iterator_data {1230	struct iwl_mvm *mvm;1231	struct ieee80211_vif *vif;1232	u32 beacon_device_ts;1233	u16 beacon_int;1234};1235 1236/* Find the beacon_device_ts and beacon_int for a managed interface */1237static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,1238				    struct ieee80211_vif *vif)1239{1240	struct iwl_mvm_mac_ap_iterator_data *data = _data;1241 1242	if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc)1243		return;1244 1245	/* Station client has higher priority over P2P client*/1246	if (vif->p2p && data->beacon_device_ts)1247		return;1248 1249	data->beacon_device_ts = vif->bss_conf.sync_device_ts;1250	data->beacon_int = vif->bss_conf.beacon_int;1251}1252 1253/*1254 * Fill the filter flags for mac context of type AP or P2P GO.1255 */1256void iwl_mvm_mac_ctxt_cmd_ap_set_filter_flags(struct iwl_mvm *mvm,1257					      struct iwl_mvm_vif *mvmvif,1258					      __le32 *filter_flags,1259					      int accept_probe_req_flag,1260					      int accept_beacon_flag)1261{1262	/*1263	 * in AP mode, pass probe requests and beacons from other APs1264	 * (needed for ht protection); when there're no any associated1265	 * station don't ask FW to pass beacons to prevent unnecessary1266	 * wake-ups.1267	 */1268	*filter_flags |= cpu_to_le32(accept_probe_req_flag);1269	if (mvmvif->ap_assoc_sta_count || !mvm->drop_bcn_ap_mode) {1270		*filter_flags |= cpu_to_le32(accept_beacon_flag);1271		IWL_DEBUG_HC(mvm, "Asking FW to pass beacons\n");1272	} else {1273		IWL_DEBUG_HC(mvm, "No need to receive beacons\n");1274	}1275}1276 1277/*1278 * Fill the specific data for mac context of type AP of P2P GO1279 */1280static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,1281					 struct ieee80211_vif *vif,1282					 struct iwl_mac_ctx_cmd *cmd,1283					 struct iwl_mac_data_ap *ctxt_ap,1284					 bool add)1285{1286	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);1287	struct iwl_mvm_mac_ap_iterator_data data = {1288		.mvm = mvm,1289		.vif = vif,1290		.beacon_device_ts = 01291	};1292 1293	/* in AP mode, the MCAST FIFO takes the EDCA params from VO */1294	cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |= BIT(IWL_MVM_TX_FIFO_MCAST);1295 1296	iwl_mvm_mac_ctxt_cmd_ap_set_filter_flags(mvm, mvmvif,1297						 &cmd->filter_flags,1298						 MAC_FILTER_IN_PROBE_REQUEST,1299						 MAC_FILTER_IN_BEACON);1300 1301	ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);1302	ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *1303					     vif->bss_conf.dtim_period);1304 1305	if (!fw_has_api(&mvm->fw->ucode_capa,1306			IWL_UCODE_TLV_API_STA_TYPE))1307		ctxt_ap->mcast_qid = cpu_to_le32(mvmvif->deflink.cab_queue);1308 1309	/*1310	 * Only set the beacon time when the MAC is being added, when we1311	 * just modify the MAC then we should keep the time -- the firmware1312	 * can otherwise have a "jumping" TBTT.1313	 */1314	if (add) {1315		/*1316		 * If there is a station/P2P client interface which is1317		 * associated, set the AP's TBTT far enough from the station's1318		 * TBTT. Otherwise, set it to the current system time1319		 */1320		ieee80211_iterate_active_interfaces_atomic(1321			mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,1322			iwl_mvm_mac_ap_iterator, &data);1323 1324		if (data.beacon_device_ts) {1325			u32 rand = get_random_u32_inclusive(36, 63);1326			mvmvif->ap_beacon_time = data.beacon_device_ts +1327				ieee80211_tu_to_usec(data.beacon_int * rand /1328						     100);1329		} else {1330			mvmvif->ap_beacon_time = iwl_mvm_get_systime(mvm);1331		}1332	}1333 1334	ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);1335	ctxt_ap->beacon_tsf = 0; /* unused */1336 1337	/* TODO: Assume that the beacon id == mac context id */1338	ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);1339}1340 1341static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,1342				   struct ieee80211_vif *vif,1343				   u32 action)1344{1345	struct iwl_mac_ctx_cmd cmd = {};1346 1347	WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);1348 1349	/* Fill the common data for all mac context types */1350	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);1351 1352	/* Fill the data specific for ap mode */1353	iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.ap,1354				     action == FW_CTXT_ACTION_ADD);1355 1356	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);1357}1358 1359static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,1360				   struct ieee80211_vif *vif,1361				   u32 action)1362{1363	struct iwl_mac_ctx_cmd cmd = {};1364	struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;1365 1366	WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);1367 1368	/* Fill the common data for all mac context types */1369	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);1370 1371	/* Fill the data specific for GO mode */1372	iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.go.ap,1373				     action == FW_CTXT_ACTION_ADD);1374 1375	cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &1376					IEEE80211_P2P_OPPPS_CTWINDOW_MASK);1377	cmd.go.opp_ps_enabled =1378			cpu_to_le32(!!(noa->oppps_ctwindow &1379					IEEE80211_P2P_OPPPS_ENABLE_BIT));1380 1381	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);1382}1383 1384static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,1385				u32 action, bool force_assoc_off,1386				const u8 *bssid_override)1387{1388	switch (vif->type) {1389	case NL80211_IFTYPE_STATION:1390		return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,1391						force_assoc_off,1392						bssid_override);1393	case NL80211_IFTYPE_AP:1394		if (!vif->p2p)1395			return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);1396		else1397			return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);1398	case NL80211_IFTYPE_MONITOR:1399		return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);1400	case NL80211_IFTYPE_P2P_DEVICE:1401		return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);1402	case NL80211_IFTYPE_ADHOC:1403		return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);1404	default:1405		break;1406	}1407 1408	return -EOPNOTSUPP;1409}1410 1411int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)1412{1413	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);1414	int ret;1415 1416	if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",1417		      vif->addr, ieee80211_vif_type_p2p(vif)))1418		return -EIO;1419 1420	ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,1421				   true, NULL);1422	if (ret)1423		return ret;1424 1425	/* will only do anything at resume from D3 time */1426	iwl_mvm_set_last_nonqos_seq(mvm, vif);1427 1428	mvmvif->uploaded = true;1429	return 0;1430}1431 1432int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,1433			     bool force_assoc_off, const u8 *bssid_override)1434{1435	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);1436 1437	if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",1438		      vif->addr, ieee80211_vif_type_p2p(vif)))1439		return -EIO;1440 1441	return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,1442				    force_assoc_off, bssid_override);1443}1444 1445int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)1446{1447	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);1448	struct iwl_mac_ctx_cmd cmd;1449	int ret;1450 1451	if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",1452		      vif->addr, ieee80211_vif_type_p2p(vif)))1453		return -EIO;1454 1455	memset(&cmd, 0, sizeof(cmd));1456 1457	cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,1458							   mvmvif->color));1459	cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);1460 1461	ret = iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);1462	if (ret)1463		return ret;1464 1465	mvmvif->uploaded = false;1466 1467	if (vif->type == NL80211_IFTYPE_MONITOR) {1468		__clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, mvm->hw->flags);1469		iwl_mvm_dealloc_snif_sta(mvm);1470	}1471 1472	return 0;1473}1474 1475static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,1476				   struct ieee80211_vif *csa_vif, u32 gp2,1477				   bool tx_success)1478{1479	struct iwl_mvm_vif *mvmvif =1480			iwl_mvm_vif_from_mac80211(csa_vif);1481 1482	/* Don't start to countdown from a failed beacon */1483	if (!tx_success && !mvmvif->csa_countdown)1484		return;1485 1486	mvmvif->csa_countdown = true;1487 1488	if (!ieee80211_beacon_cntdwn_is_complete(csa_vif, 0)) {1489		int c = ieee80211_beacon_update_cntdwn(csa_vif, 0);1490 1491		iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif,1492						&csa_vif->bss_conf);1493		if (csa_vif->p2p &&1494		    !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2 &&1495		    tx_success) {1496			u32 rel_time = (c + 1) *1497				       csa_vif->bss_conf.beacon_int -1498				       IWL_MVM_CHANNEL_SWITCH_TIME_GO;1499			u32 apply_time = gp2 + rel_time * 1024;1500 1501			iwl_mvm_schedule_csa_period(mvm, csa_vif,1502					 IWL_MVM_CHANNEL_SWITCH_TIME_GO -1503					 IWL_MVM_CHANNEL_SWITCH_MARGIN,1504					 apply_time);1505		}1506	} else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {1507		/* we don't have CSA NoA scheduled yet, switch now */1508		ieee80211_csa_finish(csa_vif, 0);1509		RCU_INIT_POINTER(mvm->csa_vif, NULL);1510	}1511}1512 1513void iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,1514			     struct iwl_rx_cmd_buffer *rxb)1515{1516	struct iwl_rx_packet *pkt = rxb_addr(rxb);1517	unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);1518	struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;1519	struct iwl_extended_beacon_notif_v5 *beacon_v5 = (void *)pkt->data;1520	struct ieee80211_vif *csa_vif;1521	struct ieee80211_vif *tx_blocked_vif;1522	struct agg_tx_status *agg_status;1523	u16 status;1524 1525	lockdep_assert_held(&mvm->mutex);1526 1527	mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);1528 1529	if (!iwl_mvm_is_short_beacon_notif_supported(mvm)) {1530		struct iwl_tx_resp *beacon_notify_hdr =1531			&beacon_v5->beacon_notify_hdr;1532 1533		if (unlikely(pkt_len < sizeof(*beacon_v5)))1534			return;1535 1536		mvm->ibss_manager = beacon_v5->ibss_mgr_status != 0;1537		agg_status = iwl_mvm_get_agg_status(mvm, beacon_notify_hdr);1538		status = le16_to_cpu(agg_status->status) & TX_STATUS_MSK;1539		IWL_DEBUG_RX(mvm,1540			     "beacon status %#x retries:%d tsf:0x%016llX gp2:0x%X rate:%d\n",1541			     status, beacon_notify_hdr->failure_frame,1542			     le64_to_cpu(beacon->tsf),1543			     mvm->ap_last_beacon_gp2,1544			     le32_to_cpu(beacon_notify_hdr->initial_rate));1545	} else {1546		if (unlikely(pkt_len < sizeof(*beacon)))1547			return;1548 1549		mvm->ibss_manager = beacon->ibss_mgr_status != 0;1550		status = le32_to_cpu(beacon->status) & TX_STATUS_MSK;1551		IWL_DEBUG_RX(mvm,1552			     "beacon status %#x tsf:0x%016llX gp2:0x%X\n",1553			     status, le64_to_cpu(beacon->tsf),1554			     mvm->ap_last_beacon_gp2);1555	}1556 1557	csa_vif = rcu_dereference_protected(mvm->csa_vif,1558					    lockdep_is_held(&mvm->mutex));1559	if (unlikely(csa_vif && csa_vif->bss_conf.csa_active))1560		iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2,1561				       (status == TX_STATUS_SUCCESS));1562 1563	tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,1564						lockdep_is_held(&mvm->mutex));1565	if (unlikely(tx_blocked_vif)) {1566		struct iwl_mvm_vif *mvmvif =1567			iwl_mvm_vif_from_mac80211(tx_blocked_vif);1568 1569		/*1570		 * The channel switch is started and we have blocked the1571		 * stations. If this is the first beacon (the timeout wasn't1572		 * set), set the unblock timeout, otherwise countdown1573		 */1574		if (!mvm->csa_tx_block_bcn_timeout)1575			mvm->csa_tx_block_bcn_timeout =1576				IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;1577		else1578			mvm->csa_tx_block_bcn_timeout--;1579 1580		/* Check if the timeout is expired, and unblock tx */1581		if (mvm->csa_tx_block_bcn_timeout == 0) {1582			iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);1583			RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);1584		}1585	}1586}1587 1588static void1589iwl_mvm_handle_missed_beacons_notif(struct iwl_mvm *mvm,1590				    const struct iwl_missed_beacons_notif *mb,1591				    struct iwl_rx_packet *pkt)1592{1593	struct iwl_fw_dbg_trigger_missed_bcon *bcon_trig;1594	struct iwl_fw_dbg_trigger_tlv *trigger;1595	u32 stop_trig_missed_bcon, stop_trig_missed_bcon_since_rx;1596	u32 rx_missed_bcon, rx_missed_bcon_since_rx;1597	struct ieee80211_vif *vif;1598	/* Id can be mac/link id depending on the notification version */1599	u32 id = le32_to_cpu(mb->link_id);1600	union iwl_dbg_tlv_tp_data tp_data = { .fw_pkt = pkt };1601	u32 mac_type;1602	int link_id = -1;1603	u8 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,1604					       MISSED_BEACONS_NOTIFICATION,1605					       0);1606	u8 new_notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,1607						   MISSED_BEACONS_NOTIF, 0);1608 1609	/* If the firmware uses the new notification (from MAC_CONF_GROUP),1610	 * refer to that notification's version.1611	 * Note that the new notification from MAC_CONF_GROUP starts from1612	 * version 5.1613	 */1614	if (new_notif_ver)1615		notif_ver = new_notif_ver;1616 1617	/* before version four the ID in the notification refers to mac ID */1618	if (notif_ver < 4) {1619		vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, false);1620	} else {1621		struct ieee80211_bss_conf *bss_conf =1622			iwl_mvm_rcu_fw_link_id_to_link_conf(mvm, id, false);1623 1624		if (!bss_conf)1625			return;1626 1627		vif = bss_conf->vif;1628		link_id = bss_conf->link_id;1629	}1630 1631	IWL_DEBUG_INFO(mvm,1632		       "missed bcn %s_id=%u, consecutive=%u (%u)\n",1633		       notif_ver < 4 ? "mac" : "link",1634		       id,1635		       le32_to_cpu(mb->consec_missed_beacons),1636		       le32_to_cpu(mb->consec_missed_beacons_since_last_rx));1637 1638	if (!vif)1639		return;1640 1641	mac_type = iwl_mvm_get_mac_type(vif);1642 1643	IWL_DEBUG_INFO(mvm, "missed beacon mac_type=%u,\n", mac_type);1644 1645	mvm->trans->dbg.dump_file_name_ext_valid = true;1646	snprintf(mvm->trans->dbg.dump_file_name_ext, IWL_FW_INI_MAX_NAME,1647		 "MacId_%d_MacType_%d", id, mac_type);1648 1649	rx_missed_bcon = le32_to_cpu(mb->consec_missed_beacons);1650	rx_missed_bcon_since_rx =1651		le32_to_cpu(mb->consec_missed_beacons_since_last_rx);1652	/*1653	 * TODO: the threshold should be adjusted based on latency conditions,1654	 * and/or in case of a CS flow on one of the other AP vifs.1655	 */1656	if (rx_missed_bcon >= IWL_MVM_MISSED_BEACONS_THRESHOLD_LONG) {1657		if (rx_missed_bcon_since_rx >= IWL_MVM_MISSED_BEACONS_SINCE_RX_THOLD) {1658			iwl_mvm_connection_loss(mvm, vif, "missed beacons");1659		} else {1660			IWL_WARN(mvm,1661				 "missed beacons exceeds threshold, but receiving data. Stay connected, Expect bugs.\n");1662			IWL_WARN(mvm,1663				 "missed_beacons:%d, missed_beacons_since_rx:%d\n",1664				 rx_missed_bcon, rx_missed_bcon_since_rx);1665		}1666	} else if (link_id >= 0 && hweight16(vif->active_links) > 1) {1667		u32 scnd_lnk_bcn_lost = 0;1668 1669		if (notif_ver >= 5 &&1670		    !IWL_FW_CHECK(mvm,1671				  le32_to_cpu(mb->other_link_id) == IWL_MVM_FW_LINK_ID_INVALID,1672				  "No data for other link id but we are in EMLSR active_links: 0x%x\n",1673				  vif->active_links))1674			scnd_lnk_bcn_lost =1675				le32_to_cpu(mb->consec_missed_beacons_other_link);1676 1677		/* Exit EMLSR if we lost more than1678		 * IWL_MVM_MISSED_BEACONS_EXIT_ESR_THRESH beacons on boths links1679		 * OR more than IWL_MVM_BCN_LOSS_EXIT_ESR_THRESH on any link.1680		 */1681		if ((rx_missed_bcon >= IWL_MVM_BCN_LOSS_EXIT_ESR_THRESH_2_LINKS &&1682		     scnd_lnk_bcn_lost >= IWL_MVM_BCN_LOSS_EXIT_ESR_THRESH_2_LINKS) ||1683		    rx_missed_bcon >= IWL_MVM_BCN_LOSS_EXIT_ESR_THRESH)1684			iwl_mvm_exit_esr(mvm, vif,1685					 IWL_MVM_ESR_EXIT_MISSED_BEACON,1686					 iwl_mvm_get_primary_link(vif));1687	} else if (rx_missed_bcon_since_rx > IWL_MVM_MISSED_BEACONS_THRESHOLD) {1688		if (!iwl_mvm_has_new_tx_api(mvm))1689			ieee80211_beacon_loss(vif);1690		else1691			ieee80211_cqm_beacon_loss_notify(vif, GFP_ATOMIC);1692	}1693 1694	iwl_dbg_tlv_time_point(&mvm->fwrt,1695			       IWL_FW_INI_TIME_POINT_MISSED_BEACONS, &tp_data);1696 1697	trigger = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),1698					FW_DBG_TRIGGER_MISSED_BEACONS);1699	if (!trigger)1700		return;1701 1702	bcon_trig = (void *)trigger->data;1703	stop_trig_missed_bcon = le32_to_cpu(bcon_trig->stop_consec_missed_bcon);1704	stop_trig_missed_bcon_since_rx =1705		le32_to_cpu(bcon_trig->stop_consec_missed_bcon_since_rx);1706 1707	/* TODO: implement start trigger */1708 1709	if (rx_missed_bcon_since_rx >= stop_trig_missed_bcon_since_rx ||1710	    rx_missed_bcon >= stop_trig_missed_bcon)1711		iwl_fw_dbg_collect_trig(&mvm->fwrt, trigger, NULL);1712}1713 1714void iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,1715				     struct iwl_rx_cmd_buffer *rxb)1716{1717	struct iwl_rx_packet *pkt = rxb_addr(rxb);1718 1719	iwl_mvm_handle_missed_beacons_notif(mvm, (const void *)pkt->data, pkt);1720}1721 1722void iwl_mvm_rx_missed_beacons_notif_legacy(struct iwl_mvm *mvm,1723					    struct iwl_rx_cmd_buffer *rxb)1724{1725	struct iwl_rx_packet *pkt = rxb_addr(rxb);1726	const struct iwl_missed_beacons_notif_v4 *mb_v4 =1727		(const void *)pkt->data;1728	struct iwl_missed_beacons_notif mb = {1729		.link_id = mb_v4->link_id,1730		.consec_missed_beacons = mb_v4->consec_missed_beacons,1731		.consec_missed_beacons_since_last_rx =1732			mb_v4->consec_missed_beacons_since_last_rx,1733		.other_link_id = cpu_to_le32(IWL_MVM_FW_LINK_ID_INVALID),1734	};1735 1736	iwl_mvm_handle_missed_beacons_notif(mvm, &mb, pkt);1737}1738 1739void iwl_mvm_rx_stored_beacon_notif(struct iwl_mvm *mvm,1740				    struct iwl_rx_cmd_buffer *rxb)1741{1742	struct iwl_rx_packet *pkt = rxb_addr(rxb);1743	unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);1744	struct iwl_stored_beacon_notif_common *sb = (void *)pkt->data;1745	struct ieee80211_rx_status rx_status;1746	struct sk_buff *skb;1747	u8 *data;1748	u32 size = le32_to_cpu(sb->byte_count);1749	int ver = iwl_fw_lookup_cmd_ver(mvm->fw,1750					WIDE_ID(PROT_OFFLOAD_GROUP, STORED_BEACON_NTF),1751					0);1752 1753	if (size == 0)1754		return;1755 1756	/* handle per-version differences */1757	if (ver <= 2) {1758		struct iwl_stored_beacon_notif_v2 *sb_v2 = (void *)pkt->data;1759 1760		if (pkt_len < struct_size(sb_v2, data, size))1761			return;1762 1763		data = sb_v2->data;1764	} else {1765		struct iwl_stored_beacon_notif_v3 *sb_v3 = (void *)pkt->data;1766 1767		if (pkt_len < struct_size(sb_v3, data, size))1768			return;1769 1770		data = sb_v3->data;1771	}1772 1773	skb = alloc_skb(size, GFP_ATOMIC);1774	if (!skb) {1775		IWL_ERR(mvm, "alloc_skb failed\n");1776		return;1777	}1778 1779	/* update rx_status according to the notification's metadata */1780	memset(&rx_status, 0, sizeof(rx_status));1781	rx_status.mactime = le64_to_cpu(sb->tsf);1782	/* TSF as indicated by the firmware  is at INA time */1783	rx_status.flag |= RX_FLAG_MACTIME_PLCP_START;1784	rx_status.device_timestamp = le32_to_cpu(sb->system_time);1785	rx_status.band =1786		(sb->band & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?1787				NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;1788	rx_status.freq =1789		ieee80211_channel_to_frequency(le16_to_cpu(sb->channel),1790					       rx_status.band);1791 1792	/* copy the data */1793	skb_put_data(skb, data, size);1794	memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));1795 1796	/* pass it as regular rx to mac80211 */1797	ieee80211_rx_napi(mvm->hw, NULL, skb, NULL);1798}1799 1800void iwl_mvm_probe_resp_data_notif(struct iwl_mvm *mvm,1801				   struct iwl_rx_cmd_buffer *rxb)1802{1803	struct iwl_rx_packet *pkt = rxb_addr(rxb);1804	struct iwl_probe_resp_data_notif *notif = (void *)pkt->data;1805	struct iwl_probe_resp_data *old_data, *new_data;1806	u32 id = le32_to_cpu(notif->mac_id);1807	struct ieee80211_vif *vif;1808	struct iwl_mvm_vif *mvmvif;1809 1810	IWL_DEBUG_INFO(mvm, "Probe response data notif: noa %d, csa %d\n",1811		       notif->noa_active, notif->csa_counter);1812 1813	vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, false);1814	if (!vif)1815		return;1816 1817	mvmvif = iwl_mvm_vif_from_mac80211(vif);1818 1819	new_data = kzalloc(sizeof(*new_data), GFP_KERNEL);1820	if (!new_data)1821		return;1822 1823	memcpy(&new_data->notif, notif, sizeof(new_data->notif));1824 1825	/* noa_attr contains 1 reserved byte, need to substruct it */1826	new_data->noa_len = sizeof(struct ieee80211_vendor_ie) +1827			    sizeof(new_data->notif.noa_attr) - 1;1828 1829	/*1830	 * If it's a one time NoA, only one descriptor is needed,1831	 * adjust the length according to len_low.1832	 */1833	if (new_data->notif.noa_attr.len_low ==1834	    sizeof(struct ieee80211_p2p_noa_desc) + 2)1835		new_data->noa_len -= sizeof(struct ieee80211_p2p_noa_desc);1836 1837	old_data = rcu_dereference_protected(mvmvif->deflink.probe_resp_data,1838					     lockdep_is_held(&mvmvif->mvm->mutex));1839	rcu_assign_pointer(mvmvif->deflink.probe_resp_data, new_data);1840 1841	if (old_data)1842		kfree_rcu(old_data, rcu_head);1843 1844	if (notif->csa_counter != IWL_PROBE_RESP_DATA_NO_CSA &&1845	    notif->csa_counter >= 1)1846		ieee80211_beacon_set_cntdwn(vif, notif->csa_counter);1847}1848 1849void iwl_mvm_channel_switch_start_notif(struct iwl_mvm *mvm,1850					struct iwl_rx_cmd_buffer *rxb)1851{1852	struct iwl_rx_packet *pkt = rxb_addr(rxb);1853	struct ieee80211_vif *csa_vif, *vif;1854	struct iwl_mvm_vif *mvmvif, *csa_mvmvif;1855	u32 id_n_color, csa_id;1856	/* save mac_id or link_id to use later to cancel csa if needed */1857	u32 id;1858	u32 mac_link_id = 0;1859	u8 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,1860					       CHANNEL_SWITCH_START_NOTIF, 0);1861	bool csa_active;1862 1863	rcu_read_lock();1864 1865	if (notif_ver < 3) {1866		struct iwl_channel_switch_start_notif_v1 *notif = (void *)pkt->data;1867		u32 mac_id;1868 1869		id_n_color = le32_to_cpu(notif->id_and_color);1870		mac_id = id_n_color & FW_CTXT_ID_MSK;1871 1872		vif = iwl_mvm_rcu_dereference_vif_id(mvm, mac_id, true);1873		if (!vif)1874			goto out_unlock;1875 1876		id = mac_id;1877		csa_active = vif->bss_conf.csa_active;1878	} else {1879		struct iwl_channel_switch_start_notif *notif = (void *)pkt->data;1880		u32 link_id = le32_to_cpu(notif->link_id);1881		struct ieee80211_bss_conf *bss_conf =1882			iwl_mvm_rcu_fw_link_id_to_link_conf(mvm, link_id, true);1883 1884		if (!bss_conf)1885			goto out_unlock;1886 1887		id = link_id;1888		mac_link_id = bss_conf->link_id;1889		vif = bss_conf->vif;1890		csa_active = bss_conf->csa_active;1891	}1892 1893	mvmvif = iwl_mvm_vif_from_mac80211(vif);1894	if (notif_ver >= 3)1895		id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);1896 1897	switch (vif->type) {1898	case NL80211_IFTYPE_AP:1899		csa_vif = rcu_dereference(mvm->csa_vif);1900		if (WARN_ON(!csa_vif || !csa_vif->bss_conf.csa_active ||1901			    csa_vif != vif))1902			goto out_unlock;1903 1904		csa_mvmvif = iwl_mvm_vif_from_mac80211(csa_vif);1905		csa_id = FW_CMD_ID_AND_COLOR(csa_mvmvif->id, csa_mvmvif->color);1906		if (WARN(csa_id != id_n_color,1907			 "channel switch noa notification on unexpected vif (csa_vif=%d, notif=%d)",1908			 csa_id, id_n_color))1909			goto out_unlock;1910 1911		IWL_DEBUG_INFO(mvm, "Channel Switch Started Notification\n");1912 1913		schedule_delayed_work(&mvm->cs_tx_unblock_dwork,1914				      msecs_to_jiffies(IWL_MVM_CS_UNBLOCK_TX_TIMEOUT *1915						       csa_vif->bss_conf.beacon_int));1916 1917		ieee80211_csa_finish(csa_vif, 0);1918 1919		rcu_read_unlock();1920 1921		RCU_INIT_POINTER(mvm->csa_vif, NULL);1922		return;1923	case NL80211_IFTYPE_STATION:1924		/*1925		 * if we don't know about an ongoing channel switch,1926		 * make sure FW cancels it1927		 */1928		if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,1929					    CHANNEL_SWITCH_ERROR_NOTIF,1930					    0) && !csa_active) {1931			IWL_DEBUG_INFO(mvm, "Channel Switch was canceled\n");1932			iwl_mvm_cancel_channel_switch(mvm, vif, id);1933			break;1934		}1935 1936		iwl_mvm_csa_client_absent(mvm, vif);1937		cancel_delayed_work(&mvmvif->csa_work);1938		ieee80211_chswitch_done(vif, true, mac_link_id);1939		break;1940	default:1941		/* should never happen */1942		WARN_ON_ONCE(1);1943		break;1944	}1945out_unlock:1946	rcu_read_unlock();1947}1948 1949void iwl_mvm_channel_switch_error_notif(struct iwl_mvm *mvm,1950					struct iwl_rx_cmd_buffer *rxb)1951{1952	struct iwl_rx_packet *pkt = rxb_addr(rxb);1953	struct iwl_channel_switch_error_notif *notif = (void *)pkt->data;1954	struct ieee80211_vif *vif;1955	u32 id = le32_to_cpu(notif->link_id);1956	u32 csa_err_mask = le32_to_cpu(notif->csa_err_mask);1957 1958	rcu_read_lock();1959	vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);1960	if (!vif) {1961		rcu_read_unlock();1962		return;1963	}1964 1965	IWL_DEBUG_INFO(mvm, "FW reports CSA error: id=%u, csa_err_mask=%u\n",1966		       id, csa_err_mask);1967	if (csa_err_mask & (CS_ERR_COUNT_ERROR |1968			    CS_ERR_LONG_DELAY_AFTER_CS |1969			    CS_ERR_TX_BLOCK_TIMER_EXPIRED))1970		ieee80211_channel_switch_disconnect(vif, true);1971	rcu_read_unlock();1972}1973 1974void iwl_mvm_rx_missed_vap_notif(struct iwl_mvm *mvm,1975				 struct iwl_rx_cmd_buffer *rxb)1976{1977	struct iwl_rx_packet *pkt = rxb_addr(rxb);1978	struct iwl_missed_vap_notif *mb = (void *)pkt->data;1979	struct ieee80211_vif *vif;1980	u32 id = le32_to_cpu(mb->mac_id);1981 1982	IWL_DEBUG_INFO(mvm,1983		       "missed_vap notify mac_id=%u, num_beacon_intervals_elapsed=%u, profile_periodicity=%u\n",1984		       le32_to_cpu(mb->mac_id),1985		       mb->num_beacon_intervals_elapsed,1986		       mb->profile_periodicity);1987 1988	rcu_read_lock();1989 1990	vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);1991	if (vif)1992		iwl_mvm_connection_loss(mvm, vif, "missed vap beacon");1993 1994	rcu_read_unlock();1995}1996