Path: blob/main/sys/contrib/dev/iwlwifi/mvm/mac-ctxt.c
48287 views
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause1/*2* Copyright (C) 2012-2014, 2018-2025 Intel Corporation3* Copyright (C) 2013-2014 Intel Mobile Communications GmbH4* Copyright (C) 2015-2017 Intel Deutschland GmbH5*/6#include <linux/etherdevice.h>7#include <linux/crc32.h>8#include <net/mac80211.h>9#include "iwl-io.h"10#include "iwl-prph.h"11#include "fw-api.h"12#include "mvm.h"13#include "time-event.h"14#include "iwl-utils.h"1516const u8 iwl_mvm_ac_to_tx_fifo[] = {17IWL_MVM_TX_FIFO_VO,18IWL_MVM_TX_FIFO_VI,19IWL_MVM_TX_FIFO_BE,20IWL_MVM_TX_FIFO_BK,21};2223const u8 iwl_mvm_ac_to_gen2_tx_fifo[] = {24IWL_GEN2_EDCA_TX_FIFO_VO,25IWL_GEN2_EDCA_TX_FIFO_VI,26IWL_GEN2_EDCA_TX_FIFO_BE,27IWL_GEN2_EDCA_TX_FIFO_BK,28IWL_GEN2_TRIG_TX_FIFO_VO,29IWL_GEN2_TRIG_TX_FIFO_VI,30IWL_GEN2_TRIG_TX_FIFO_BE,31IWL_GEN2_TRIG_TX_FIFO_BK,32};3334const u8 iwl_mvm_ac_to_bz_tx_fifo[] = {35IWL_BZ_EDCA_TX_FIFO_VO,36IWL_BZ_EDCA_TX_FIFO_VI,37IWL_BZ_EDCA_TX_FIFO_BE,38IWL_BZ_EDCA_TX_FIFO_BK,39IWL_BZ_TRIG_TX_FIFO_VO,40IWL_BZ_TRIG_TX_FIFO_VI,41IWL_BZ_TRIG_TX_FIFO_BE,42IWL_BZ_TRIG_TX_FIFO_BK,43};4445struct iwl_mvm_mac_iface_iterator_data {46struct iwl_mvm *mvm;47struct ieee80211_vif *vif;48unsigned long available_mac_ids[BITS_TO_LONGS(NUM_MAC_INDEX_DRIVER)];49unsigned long available_tsf_ids[BITS_TO_LONGS(NUM_TSF_IDS)];50enum iwl_tsf_id preferred_tsf;51bool found_vif;52};5354static void iwl_mvm_mac_tsf_id_iter(void *_data, u8 *mac,55struct ieee80211_vif *vif)56{57struct iwl_mvm_mac_iface_iterator_data *data = _data;58struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);59u16 min_bi;6061/* Skip the interface for which we are trying to assign a tsf_id */62if (vif == data->vif)63return;6465/*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*/81switch (data->vif->type) {82case 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*/91if (vif->type != NL80211_IFTYPE_AP ||92data->preferred_tsf != NUM_TSF_IDS ||93!test_bit(mvmvif->tsf_id, data->available_tsf_ids))94break;9596min_bi = min(data->vif->bss_conf.beacon_int,97vif->bss_conf.beacon_int);9899if (!min_bi)100break;101102if ((data->vif->bss_conf.beacon_int -103vif->bss_conf.beacon_int) % min_bi == 0) {104data->preferred_tsf = mvmvif->tsf_id;105return;106}107break;108109case 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*/119if ((vif->type != NL80211_IFTYPE_AP &&120vif->type != NL80211_IFTYPE_STATION) ||121data->preferred_tsf != NUM_TSF_IDS ||122!test_bit(mvmvif->tsf_id, data->available_tsf_ids))123break;124125min_bi = min(data->vif->bss_conf.beacon_int,126vif->bss_conf.beacon_int);127128if (!min_bi)129break;130131if ((data->vif->bss_conf.beacon_int -132vif->bss_conf.beacon_int) % min_bi == 0) {133data->preferred_tsf = mvmvif->tsf_id;134return;135}136break;137default: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*/145break;146}147148/*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);155156if (data->preferred_tsf == mvmvif->tsf_id)157data->preferred_tsf = NUM_TSF_IDS;158}159160static void iwl_mvm_mac_iface_iterator(void *_data, u8 *mac,161struct ieee80211_vif *vif)162{163struct iwl_mvm_mac_iface_iterator_data *data = _data;164struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);165166/* Iterator may already find the interface being added -- skip it */167if (vif == data->vif) {168data->found_vif = true;169return;170}171172/* 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);179180/* find a suitable tsf_id */181iwl_mvm_mac_tsf_id_iter(_data, mac, vif);182}183184void iwl_mvm_mac_ctxt_recalc_tsf_id(struct iwl_mvm *mvm,185struct ieee80211_vif *vif)186{187struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);188struct 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};195196ieee80211_iterate_active_interfaces_atomic(197mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,198iwl_mvm_mac_tsf_id_iter, &data);199200if (data.preferred_tsf != NUM_TSF_IDS)201mvmvif->tsf_id = data.preferred_tsf;202else if (!test_bit(mvmvif->tsf_id, data.available_tsf_ids))203mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,204NUM_TSF_IDS);205}206207int iwl_mvm_mac_ctxt_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif)208{209struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);210struct 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};219int ret;220221lockdep_assert_held(&mvm->mutex);222223/*224* Allocate a MAC ID and a TSF for this MAC, along with the queues225* and other resources.226*/227228/*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*/238239/* MAC ID 0 should be used only for the managed/IBSS vif with non-MLO240* FW API241*/242if (!mvm->mld_api_is_used) {243switch (vif->type) {244case NL80211_IFTYPE_ADHOC:245break;246case NL80211_IFTYPE_STATION:247if (!vif->p2p)248break;249fallthrough;250default:251__clear_bit(0, data.available_mac_ids);252}253}254255ieee80211_iterate_active_interfaces_atomic(256mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,257iwl_mvm_mac_iface_iterator, &data);258259/*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*/269if (data.found_vif)270return 0;271272/* Therefore, in recovery, we can't get here */273if (WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))274return -EBUSY;275276mvmvif->id = find_first_bit(data.available_mac_ids,277NUM_MAC_INDEX_DRIVER);278if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {279IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");280ret = -EIO;281goto exit_fail;282}283284if (data.preferred_tsf != NUM_TSF_IDS)285mvmvif->tsf_id = data.preferred_tsf;286else287mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,288NUM_TSF_IDS);289if (mvmvif->tsf_id == NUM_TSF_IDS) {290IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");291ret = -EIO;292goto exit_fail;293}294295mvmvif->color = 0;296297INIT_LIST_HEAD(&mvmvif->time_event_data.list);298mvmvif->time_event_data.id = TE_MAX;299mvmvif->roc_activity = ROC_NUM_ACTIVITIES;300301iwl_mvm_init_link(&mvmvif->deflink);302303/* No need to allocate data queues to P2P Device MAC */304if (vif->type == NL80211_IFTYPE_P2P_DEVICE)305return 0;306307/* Allocate the CAB queue for softAP and GO interfaces */308if (vif->type == NL80211_IFTYPE_AP ||309vif->type == NL80211_IFTYPE_ADHOC) {310/*311* For TVQM this will be overwritten later with the FW assigned312* queue value (when queue is enabled).313*/314mvmvif->deflink.cab_queue = IWL_MVM_DQA_GCAST_QUEUE;315}316317return 0;318319exit_fail:320memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));321return ret;322}323324static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,325struct ieee80211_vif *vif,326enum nl80211_band band,327u8 *cck_rates, u8 *ofdm_rates)328{329struct ieee80211_supported_band *sband;330unsigned long basic = vif->bss_conf.basic_rates;331int lowest_present_ofdm = 100;332int lowest_present_cck = 100;333u8 cck = 0;334u8 ofdm = 0;335int i;336337sband = mvm->hw->wiphy->bands[band];338339for_each_set_bit(i, &basic, BITS_PER_LONG) {340int hw = sband->bitrates[i].hw_value;341if (hw >= IWL_FIRST_OFDM_RATE) {342ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);343if (lowest_present_ofdm > hw)344lowest_present_ofdm = hw;345} else {346BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);347348cck |= BIT(hw);349if (lowest_present_cck > hw)350lowest_present_cck = hw;351}352}353354/*355* Now we've got the basic rates as bitmaps in the ofdm and cck356* variables. This isn't sufficient though, as there might not357* be all the right rates in the bitmap. E.g. if the only basic358* rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps359* and 6 Mbps because the 802.11-2007 standard says in 9.6:360*361* [...] a STA responding to a received frame shall transmit362* its Control Response frame [...] at the highest rate in the363* BSSBasicRateSet parameter that is less than or equal to the364* rate of the immediately previous frame in the frame exchange365* sequence ([...]) and that is of the same modulation class366* ([...]) as the received frame. If no rate contained in the367* BSSBasicRateSet parameter meets these conditions, then the368* control frame sent in response to a received frame shall be369* transmitted at the highest mandatory rate of the PHY that is370* less than or equal to the rate of the received frame, and371* that is of the same modulation class as the received frame.372*373* As a consequence, we need to add all mandatory rates that are374* lower than all of the basic rates to these bitmaps.375*/376377if (IWL_RATE_24M_INDEX < lowest_present_ofdm)378ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;379if (IWL_RATE_12M_INDEX < lowest_present_ofdm)380ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;381/* 6M already there or needed so always add */382ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;383384/*385* CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.386* Note, however:387* - if no CCK rates are basic, it must be ERP since there must388* be some basic rates at all, so they're OFDM => ERP PHY389* (or we're in 5 GHz, and the cck bitmap will never be used)390* - if 11M is a basic rate, it must be ERP as well, so add 5.5M391* - if 5.5M is basic, 1M and 2M are mandatory392* - if 2M is basic, 1M is mandatory393* - if 1M is basic, that's the only valid ACK rate.394* As a consequence, it's not as complicated as it sounds, just add395* any lower rates to the ACK rate bitmap.396*/397if (IWL_RATE_11M_INDEX < lowest_present_cck)398cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;399if (IWL_RATE_5M_INDEX < lowest_present_cck)400cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;401if (IWL_RATE_2M_INDEX < lowest_present_cck)402cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;403/* 1M already there or needed so always add */404cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;405406*cck_rates = cck;407*ofdm_rates = ofdm;408}409410void iwl_mvm_set_fw_basic_rates(struct iwl_mvm *mvm, struct ieee80211_vif *vif,411struct iwl_mvm_vif_link_info *link_info,412__le32 *cck_rates, __le32 *ofdm_rates)413{414struct iwl_mvm_phy_ctxt *phy_ctxt;415u8 cck_ack_rates = 0, ofdm_ack_rates = 0;416enum nl80211_band band = NL80211_BAND_2GHZ;417418phy_ctxt = link_info->phy_ctxt;419if (phy_ctxt && phy_ctxt->channel)420band = phy_ctxt->channel->band;421422iwl_mvm_ack_rates(mvm, vif, band, &cck_ack_rates, &ofdm_ack_rates);423424*cck_rates = cpu_to_le32((u32)cck_ack_rates);425*ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);426}427428void iwl_mvm_set_fw_protection_flags(struct iwl_mvm *mvm,429struct ieee80211_vif *vif,430struct ieee80211_bss_conf *link_conf,431__le32 *protection_flags, u32 ht_flag,432u32 tgg_flag)433{434/* for both sta and ap, ht_operation_mode hold the protection_mode */435u8 protection_mode = link_conf->ht_operation_mode &436IEEE80211_HT_OP_MODE_PROTECTION;437bool ht_enabled = !!(link_conf->ht_operation_mode &438IEEE80211_HT_OP_MODE_PROTECTION);439440if (link_conf->use_cts_prot)441*protection_flags |= cpu_to_le32(tgg_flag);442443IWL_DEBUG_RATE(mvm, "use_cts_prot %d, ht_operation_mode %d\n",444link_conf->use_cts_prot,445link_conf->ht_operation_mode);446447if (!ht_enabled)448return;449450IWL_DEBUG_RATE(mvm, "protection mode set to %d\n", protection_mode);451/*452* See section 9.23.3.1 of IEEE 80211-2012.453* Nongreenfield HT STAs Present is not supported.454*/455switch (protection_mode) {456case IEEE80211_HT_OP_MODE_PROTECTION_NONE:457break;458case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:459case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:460*protection_flags |= cpu_to_le32(ht_flag);461break;462case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:463/* Protect when channel wider than 20MHz */464if (link_conf->chanreq.oper.width > NL80211_CHAN_WIDTH_20)465*protection_flags |= cpu_to_le32(ht_flag);466break;467default:468IWL_ERR(mvm, "Illegal protection mode %d\n",469protection_mode);470break;471}472}473474void iwl_mvm_set_fw_qos_params(struct iwl_mvm *mvm, struct ieee80211_vif *vif,475struct ieee80211_bss_conf *link_conf,476struct iwl_ac_qos *ac, __le32 *qos_flags)477{478struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);479struct iwl_mvm_vif_link_info *mvm_link =480mvmvif->link[link_conf->link_id];481int i;482483if (!mvm_link)484return;485486for (i = 0; i < IEEE80211_NUM_ACS; i++) {487u8 txf = iwl_mvm_mac_ac_to_tx_fifo(mvm, i);488u8 ucode_ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);489490ac[ucode_ac].cw_min =491cpu_to_le16(mvm_link->queue_params[i].cw_min);492ac[ucode_ac].cw_max =493cpu_to_le16(mvm_link->queue_params[i].cw_max);494ac[ucode_ac].edca_txop =495cpu_to_le16(mvm_link->queue_params[i].txop * 32);496ac[ucode_ac].aifsn = mvm_link->queue_params[i].aifs;497ac[ucode_ac].fifos_mask = BIT(txf);498}499500if (link_conf->qos)501*qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);502503if (link_conf->chanreq.oper.width != NL80211_CHAN_WIDTH_20_NOHT)504*qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);505}506507int iwl_mvm_get_mac_type(struct ieee80211_vif *vif)508{509u32 mac_type = FW_MAC_TYPE_BSS_STA;510511switch (vif->type) {512case NL80211_IFTYPE_STATION:513if (vif->p2p)514mac_type = FW_MAC_TYPE_P2P_STA;515else516mac_type = FW_MAC_TYPE_BSS_STA;517break;518case NL80211_IFTYPE_AP:519mac_type = FW_MAC_TYPE_GO;520break;521case NL80211_IFTYPE_MONITOR:522mac_type = FW_MAC_TYPE_LISTENER;523break;524case NL80211_IFTYPE_P2P_DEVICE:525mac_type = FW_MAC_TYPE_P2P_DEVICE;526break;527case NL80211_IFTYPE_ADHOC:528mac_type = FW_MAC_TYPE_IBSS;529break;530default:531WARN_ON_ONCE(1);532}533return mac_type;534}535536static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,537struct ieee80211_vif *vif,538struct iwl_mac_ctx_cmd *cmd,539const u8 *bssid_override,540u32 action)541{542struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);543const u8 *bssid = bssid_override ?: vif->bss_conf.bssid;544u32 ht_flag;545546cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,547mvmvif->color));548cmd->action = cpu_to_le32(action);549cmd->mac_type = cpu_to_le32(iwl_mvm_get_mac_type(vif));550551cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);552553memcpy(cmd->node_addr, vif->addr, ETH_ALEN);554555if (bssid)556memcpy(cmd->bssid_addr, bssid, ETH_ALEN);557else558eth_broadcast_addr(cmd->bssid_addr);559560iwl_mvm_set_fw_basic_rates(mvm, vif, &mvmvif->deflink, &cmd->cck_rates,561&cmd->ofdm_rates);562563cmd->cck_short_preamble =564cpu_to_le32(vif->bss_conf.use_short_preamble ?565MAC_FLG_SHORT_PREAMBLE : 0);566cmd->short_slot =567cpu_to_le32(vif->bss_conf.use_short_slot ?568MAC_FLG_SHORT_SLOT : 0);569570cmd->filter_flags = 0;571572iwl_mvm_set_fw_qos_params(mvm, vif, &vif->bss_conf, cmd->ac,573&cmd->qos_flags);574575/* The fw does not distinguish between ht and fat */576ht_flag = MAC_PROT_FLG_HT_PROT | MAC_PROT_FLG_FAT_PROT;577iwl_mvm_set_fw_protection_flags(mvm, vif, &vif->bss_conf,578&cmd->protection_flags,579ht_flag, MAC_PROT_FLG_TGG_PROTECT);580}581582static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,583struct iwl_mac_ctx_cmd *cmd)584{585int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,586sizeof(*cmd), cmd);587if (ret)588IWL_ERR(mvm, "Failed to send MAC_CONTEXT_CMD (action:%d): %d\n",589le32_to_cpu(cmd->action), ret);590return ret;591}592593void iwl_mvm_set_fw_dtim_tbtt(struct iwl_mvm *mvm, struct ieee80211_vif *vif,594struct ieee80211_bss_conf *link_conf,595__le64 *dtim_tsf, __le32 *dtim_time,596__le32 *assoc_beacon_arrive_time)597{598u32 dtim_offs;599600/*601* The DTIM count counts down, so when it is N that means N602* more beacon intervals happen until the DTIM TBTT. Therefore603* add this to the current time. If that ends up being in the604* future, the firmware will handle it.605*606* Also note that the system_timestamp (which we get here as607* "sync_device_ts") and TSF timestamp aren't at exactly the608* same offset in the frame -- the TSF is at the first symbol609* of the TSF, the system timestamp is at signal acquisition610* time. This means there's an offset between them of at most611* a few hundred microseconds (24 * 8 bits + PLCP time gives612* 384us in the longest case), this is currently not relevant613* as the firmware wakes up around 2ms before the TBTT.614*/615dtim_offs = link_conf->sync_dtim_count *616link_conf->beacon_int;617/* convert TU to usecs */618dtim_offs *= 1024;619620*dtim_tsf =621cpu_to_le64(link_conf->sync_tsf + dtim_offs);622*dtim_time =623cpu_to_le32(link_conf->sync_device_ts + dtim_offs);624*assoc_beacon_arrive_time =625cpu_to_le32(link_conf->sync_device_ts);626627IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",628le64_to_cpu(*dtim_tsf),629le32_to_cpu(*dtim_time),630dtim_offs);631}632633__le32 iwl_mvm_mac_ctxt_cmd_p2p_sta_get_oppps_ctwin(struct iwl_mvm *mvm,634struct ieee80211_vif *vif)635{636struct ieee80211_p2p_noa_attr *noa =637&vif->bss_conf.p2p_noa_attr;638639return cpu_to_le32(noa->oppps_ctwindow &640IEEE80211_P2P_OPPPS_CTWINDOW_MASK);641}642643u32 iwl_mvm_mac_ctxt_cmd_sta_get_twt_policy(struct iwl_mvm *mvm,644struct ieee80211_vif *vif)645{646u32 twt_policy = 0;647648if (vif->bss_conf.twt_requester && IWL_MVM_USE_TWT)649twt_policy |= TWT_SUPPORTED;650if (vif->bss_conf.twt_protected)651twt_policy |= PROTECTED_TWT_SUPPORTED;652if (vif->bss_conf.twt_broadcast)653twt_policy |= BROADCAST_TWT_SUPPORTED;654655return twt_policy;656}657658static int iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm *mvm,659struct ieee80211_vif *vif,660u32 action, bool force_assoc_off,661const u8 *bssid_override)662{663struct iwl_mac_ctx_cmd cmd = {};664struct iwl_mac_data_sta *ctxt_sta;665666WARN_ON(vif->type != NL80211_IFTYPE_STATION);667668/* Fill the common data for all mac context types */669iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, bssid_override, action);670671/*672* We always want to hear MCAST frames, if we're not authorized yet,673* we'll drop them.674*/675cmd.filter_flags |= cpu_to_le32(MAC_FILTER_ACCEPT_GRP);676677if (vif->p2p) {678cmd.p2p_sta.ctwin =679iwl_mvm_mac_ctxt_cmd_p2p_sta_get_oppps_ctwin(mvm, vif);680681ctxt_sta = &cmd.p2p_sta.sta;682} else {683ctxt_sta = &cmd.sta;684}685686/* We need the dtim_period to set the MAC as associated */687if (vif->cfg.assoc && vif->bss_conf.dtim_period &&688!force_assoc_off) {689struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);690691iwl_mvm_set_fw_dtim_tbtt(mvm, vif, &vif->bss_conf,692&ctxt_sta->dtim_tsf,693&ctxt_sta->dtim_time,694&ctxt_sta->assoc_beacon_arrive_time);695696ctxt_sta->is_assoc = cpu_to_le32(1);697698if (!mvmvif->authorized &&699fw_has_capa(&mvm->fw->ucode_capa,700IWL_UCODE_TLV_CAPA_COEX_HIGH_PRIO))701ctxt_sta->data_policy |=702cpu_to_le32(COEX_HIGH_PRIORITY_ENABLE);703} else {704ctxt_sta->is_assoc = cpu_to_le32(0);705706/* Allow beacons to pass through as long as we are not707* associated, or we do not have dtim period information.708*/709cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);710}711712ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);713ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *714vif->bss_conf.dtim_period);715716ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);717ctxt_sta->assoc_id = cpu_to_le32(vif->cfg.aid);718719if (vif->probe_req_reg && vif->cfg.assoc && vif->p2p)720cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);721722if (vif->bss_conf.he_support && !iwlwifi_mod_params.disable_11ax) {723cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_11AX);724ctxt_sta->data_policy |=725cpu_to_le32(iwl_mvm_mac_ctxt_cmd_sta_get_twt_policy(mvm, vif));726}727728729return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);730}731732static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,733struct ieee80211_vif *vif,734u32 action)735{736struct iwl_mac_ctx_cmd cmd = {};737u32 tfd_queue_msk = 0;738int ret;739740WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);741742iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);743744cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |745MAC_FILTER_IN_CONTROL_AND_MGMT |746MAC_FILTER_IN_BEACON |747MAC_FILTER_IN_PROBE_REQUEST |748MAC_FILTER_IN_CRC32 |749MAC_FILTER_ACCEPT_GRP);750ieee80211_hw_set(mvm->hw, RX_INCLUDES_FCS);751752/*753* the queue mask is only relevant for old TX API, and754* mvm->snif_queue isn't set here (it's still set to755* IWL_MVM_INVALID_QUEUE so the BIT() of it is UB)756*/757if (!iwl_mvm_has_new_tx_api(mvm))758tfd_queue_msk = BIT(mvm->snif_queue);759760/* Allocate sniffer station */761ret = iwl_mvm_allocate_int_sta(mvm, &mvm->snif_sta, tfd_queue_msk,762vif->type, IWL_STA_GENERAL_PURPOSE);763if (ret)764return ret;765766return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);767}768769static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,770struct ieee80211_vif *vif,771u32 action)772{773struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);774struct iwl_mac_ctx_cmd cmd = {};775776WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);777778iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);779780cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |781MAC_FILTER_IN_PROBE_REQUEST |782MAC_FILTER_ACCEPT_GRP);783784/* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */785cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);786787/* TODO: Assumes that the beacon id == mac context id */788cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);789790return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);791}792793struct iwl_mvm_go_iterator_data {794bool go_active;795};796797static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)798{799struct iwl_mvm_go_iterator_data *data = _data;800struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);801802if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&803mvmvif->ap_ibss_active)804data->go_active = true;805}806807__le32 iwl_mac_ctxt_p2p_dev_has_extended_disc(struct iwl_mvm *mvm,808struct ieee80211_vif *vif)809{810struct iwl_mvm_go_iterator_data data = {};811812/*813* This flag should be set to true when the P2P Device is814* discoverable and there is at least another active P2P GO. Settings815* this flag will allow the P2P Device to be discoverable on other816* channels in addition to its listen channel.817* Note that this flag should not be set in other cases as it opens the818* Rx filters on all MAC and increases the number of interrupts.819*/820ieee80211_iterate_active_interfaces_atomic(821mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,822iwl_mvm_go_iterator, &data);823824return cpu_to_le32(data.go_active ? 1 : 0);825}826827static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,828struct ieee80211_vif *vif,829u32 action)830{831struct iwl_mac_ctx_cmd cmd = {};832833WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);834835iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);836837cmd.p2p_dev.is_disc_extended =838iwl_mac_ctxt_p2p_dev_has_extended_disc(mvm, vif);839840/* Override the filter flags to accept only probe requests */841cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);842843return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);844}845846void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,847__le32 *tim_index, __le32 *tim_size,848u8 *beacon, u32 frame_size)849{850u32 tim_idx;851struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;852853/* The index is relative to frame start but we start looking at the854* variable-length part of the beacon. */855tim_idx = mgmt->u.beacon.variable - beacon;856857/* Parse variable-length elements of beacon to find WLAN_EID_TIM */858while ((tim_idx < (frame_size - 2)) &&859(beacon[tim_idx] != WLAN_EID_TIM))860tim_idx += beacon[tim_idx+1] + 2;861862/* If TIM field was found, set variables */863if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {864*tim_index = cpu_to_le32(tim_idx);865*tim_size = cpu_to_le32((u32)beacon[tim_idx + 1]);866} else {867IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");868}869}870871u8 iwl_mvm_mac_ctxt_get_lowest_rate(struct iwl_mvm *mvm,872struct ieee80211_tx_info *info,873struct ieee80211_vif *vif)874{875struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);876struct ieee80211_supported_band *sband;877unsigned long basic = vif->bss_conf.basic_rates;878u16 lowest_cck = IWL_RATE_COUNT, lowest_ofdm = IWL_RATE_COUNT;879u32 link_id = u32_get_bits(info->control.flags,880IEEE80211_TX_CTRL_MLO_LINK);881u8 band = info->band;882u8 rate;883u32 i;884885if (link_id == IEEE80211_LINK_UNSPECIFIED && ieee80211_vif_is_mld(vif)) {886for (i = 0; i < ARRAY_SIZE(mvmvif->link); i++) {887if (!mvmvif->link[i])888continue;889/* shouldn't do this when >1 link is active */890WARN_ON_ONCE(link_id != IEEE80211_LINK_UNSPECIFIED);891link_id = i;892}893}894895if (link_id < IEEE80211_LINK_UNSPECIFIED) {896struct ieee80211_bss_conf *link_conf;897898rcu_read_lock();899link_conf = rcu_dereference(vif->link_conf[link_id]);900if (link_conf) {901basic = link_conf->basic_rates;902if (link_conf->chanreq.oper.chan)903band = link_conf->chanreq.oper.chan->band;904}905rcu_read_unlock();906}907908sband = mvm->hw->wiphy->bands[band];909for_each_set_bit(i, &basic, BITS_PER_LONG) {910u16 hw = sband->bitrates[i].hw_value;911912if (hw >= IWL_FIRST_OFDM_RATE) {913if (lowest_ofdm > hw)914lowest_ofdm = hw;915} else if (lowest_cck > hw) {916lowest_cck = hw;917}918}919920if (band == NL80211_BAND_2GHZ && !vif->p2p &&921vif->type != NL80211_IFTYPE_P2P_DEVICE &&922!(info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)) {923if (lowest_cck != IWL_RATE_COUNT)924rate = lowest_cck;925else if (lowest_ofdm != IWL_RATE_COUNT)926rate = lowest_ofdm;927else928rate = IWL_RATE_1M_INDEX;929} else if (lowest_ofdm != IWL_RATE_COUNT) {930rate = lowest_ofdm;931} else {932rate = IWL_RATE_6M_INDEX;933}934935return rate;936}937938u16 iwl_mvm_mac_ctxt_get_beacon_flags(const struct iwl_fw *fw, u8 rate_idx)939{940bool is_new_rate = iwl_fw_lookup_cmd_ver(fw, BEACON_TEMPLATE_CMD, 0) > 10;941u16 flags, cck_flag;942943if (is_new_rate) {944flags = iwl_mvm_mac80211_idx_to_hwrate(fw, rate_idx);945cck_flag = IWL_MAC_BEACON_CCK;946} else {947cck_flag = IWL_MAC_BEACON_CCK_V1;948flags = iwl_fw_rate_idx_to_plcp(rate_idx);949}950951if (rate_idx <= IWL_LAST_CCK_RATE)952flags |= cck_flag;953954return flags;955}956957u8 iwl_mvm_mac_ctxt_get_beacon_rate(struct iwl_mvm *mvm,958struct ieee80211_tx_info *info,959struct ieee80211_vif *vif)960{961struct ieee80211_supported_band *sband =962mvm->hw->wiphy->bands[info->band];963u32 legacy = vif->bss_conf.beacon_tx_rate.control[info->band].legacy;964965/* if beacon rate was configured try using it */966if (hweight32(legacy) == 1) {967u32 rate = ffs(legacy) - 1;968969return sband->bitrates[rate].hw_value;970}971972return iwl_mvm_mac_ctxt_get_lowest_rate(mvm, info, vif);973}974975static void iwl_mvm_mac_ctxt_set_tx(struct iwl_mvm *mvm,976struct ieee80211_vif *vif,977struct sk_buff *beacon,978struct iwl_tx_cmd_v6_params *tx_params)979{980struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);981struct ieee80211_tx_info *info;982u8 rate;983u32 tx_flags;984985info = IEEE80211_SKB_CB(beacon);986987/* Set up TX command fields */988tx_params->len = cpu_to_le16((u16)beacon->len);989tx_params->sta_id = mvmvif->deflink.bcast_sta.sta_id;990tx_params->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);991tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;992tx_flags |=993iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<994TX_CMD_FLG_BT_PRIO_POS;995tx_params->tx_flags = cpu_to_le32(tx_flags);996997if (!fw_has_capa(&mvm->fw->ucode_capa,998IWL_UCODE_TLV_CAPA_BEACON_ANT_SELECTION)) {999iwl_mvm_toggle_tx_ant(mvm, &mvm->mgmt_last_antenna_idx);10001001tx_params->rate_n_flags =1002cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<1003RATE_MCS_ANT_POS);1004}10051006rate = iwl_mvm_mac_ctxt_get_beacon_rate(mvm, info, vif);10071008tx_params->rate_n_flags |=1009cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(mvm->fw, rate));1010if (rate == IWL_FIRST_CCK_RATE)1011tx_params->rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK_V1);10121013}10141015int iwl_mvm_mac_ctxt_send_beacon_cmd(struct iwl_mvm *mvm,1016struct sk_buff *beacon,1017void *data, int len)1018{1019struct iwl_host_cmd cmd = {1020.id = BEACON_TEMPLATE_CMD,1021.flags = CMD_ASYNC,1022};10231024cmd.len[0] = len;1025cmd.data[0] = data;1026cmd.dataflags[0] = 0;1027cmd.len[1] = beacon->len;1028cmd.data[1] = beacon->data;1029cmd.dataflags[1] = IWL_HCMD_DFL_DUP;10301031return iwl_mvm_send_cmd(mvm, &cmd);1032}10331034static int iwl_mvm_mac_ctxt_send_beacon_v6(struct iwl_mvm *mvm,1035struct ieee80211_vif *vif,1036struct sk_buff *beacon)1037{1038struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);1039struct iwl_mac_beacon_cmd_v6 beacon_cmd = {};10401041iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);10421043beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);10441045if (vif->type == NL80211_IFTYPE_AP)1046iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,1047&beacon_cmd.tim_size,1048beacon->data, beacon->len);10491050return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,1051sizeof(beacon_cmd));1052}10531054static int iwl_mvm_mac_ctxt_send_beacon_v7(struct iwl_mvm *mvm,1055struct ieee80211_vif *vif,1056struct sk_buff *beacon)1057{1058struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);1059struct iwl_mac_beacon_cmd_v7 beacon_cmd = {};10601061iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);10621063beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);10641065if (vif->type == NL80211_IFTYPE_AP)1066iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,1067&beacon_cmd.tim_size,1068beacon->data, beacon->len);10691070beacon_cmd.csa_offset =1071cpu_to_le32(iwl_find_ie_offset(beacon->data,1072WLAN_EID_CHANNEL_SWITCH,1073beacon->len));1074beacon_cmd.ecsa_offset =1075cpu_to_le32(iwl_find_ie_offset(beacon->data,1076WLAN_EID_EXT_CHANSWITCH_ANN,1077beacon->len));10781079return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,1080sizeof(beacon_cmd));1081}10821083bool iwl_mvm_enable_fils(struct iwl_mvm *mvm,1084struct ieee80211_vif *vif,1085struct ieee80211_chanctx_conf *ctx)1086{1087if (vif->type != NL80211_IFTYPE_AP || IWL_MVM_DISABLE_AP_FILS)1088return false;10891090if (cfg80211_channel_is_psc(ctx->def.chan))1091return true;10921093return (ctx->def.chan->band == NL80211_BAND_6GHZ &&1094ctx->def.width >= NL80211_CHAN_WIDTH_80);1095}10961097static int iwl_mvm_mac_ctxt_send_beacon_v9(struct iwl_mvm *mvm,1098struct ieee80211_vif *vif,1099struct sk_buff *beacon,1100struct ieee80211_bss_conf *link_conf)1101{1102struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);1103struct ieee80211_tx_info *info = IEEE80211_SKB_CB(beacon);1104struct iwl_mac_beacon_cmd beacon_cmd = {};1105u8 rate = iwl_mvm_mac_ctxt_get_beacon_rate(mvm, info, vif);1106u16 flags;1107struct ieee80211_chanctx_conf *ctx;1108int channel;1109flags = iwl_mvm_mac_ctxt_get_beacon_flags(mvm->fw, rate);11101111/* Enable FILS on PSC channels only */1112rcu_read_lock();1113ctx = rcu_dereference(link_conf->chanctx_conf);1114channel = ieee80211_frequency_to_channel(ctx->def.chan->center_freq);1115WARN_ON(channel == 0);1116if (iwl_mvm_enable_fils(mvm, vif, ctx)) {1117flags |= iwl_fw_lookup_cmd_ver(mvm->fw, BEACON_TEMPLATE_CMD,11180) > 10 ?1119IWL_MAC_BEACON_FILS :1120IWL_MAC_BEACON_FILS_V1;1121beacon_cmd.short_ssid =1122cpu_to_le32(~crc32_le(~0, vif->cfg.ssid,1123vif->cfg.ssid_len));1124}1125rcu_read_unlock();11261127beacon_cmd.flags = cpu_to_le16(flags);1128beacon_cmd.byte_cnt = cpu_to_le16((u16)beacon->len);11291130if (WARN_ON(!mvmvif->link[link_conf->link_id]))1131return -EINVAL;11321133if (iwl_fw_lookup_cmd_ver(mvm->fw, BEACON_TEMPLATE_CMD, 0) > 12)1134beacon_cmd.link_id =1135cpu_to_le32(mvmvif->link[link_conf->link_id]->fw_link_id);1136else1137beacon_cmd.link_id = cpu_to_le32((u32)mvmvif->id);11381139if (vif->type == NL80211_IFTYPE_AP)1140iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,1141&beacon_cmd.tim_size,1142beacon->data, beacon->len);11431144beacon_cmd.csa_offset =1145cpu_to_le32(iwl_find_ie_offset(beacon->data,1146WLAN_EID_CHANNEL_SWITCH,1147beacon->len));1148beacon_cmd.ecsa_offset =1149cpu_to_le32(iwl_find_ie_offset(beacon->data,1150WLAN_EID_EXT_CHANSWITCH_ANN,1151beacon->len));11521153if (vif->type == NL80211_IFTYPE_AP &&1154iwl_fw_lookup_cmd_ver(mvm->fw, BEACON_TEMPLATE_CMD, 0) >= 14)1155beacon_cmd.btwt_offset =1156cpu_to_le32(iwl_find_ie_offset(beacon->data,1157WLAN_EID_S1G_TWT,1158beacon->len));11591160return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,1161sizeof(beacon_cmd));1162}11631164static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,1165struct ieee80211_vif *vif,1166struct sk_buff *beacon,1167struct ieee80211_bss_conf *link_conf)1168{1169if (WARN_ON(!beacon))1170return -EINVAL;11711172if (IWL_MVM_NON_TRANSMITTING_AP)1173return 0;11741175if (!fw_has_capa(&mvm->fw->ucode_capa,1176IWL_UCODE_TLV_CAPA_CSA_AND_TBTT_OFFLOAD))1177return iwl_mvm_mac_ctxt_send_beacon_v6(mvm, vif, beacon);11781179if (fw_has_api(&mvm->fw->ucode_capa,1180IWL_UCODE_TLV_API_NEW_BEACON_TEMPLATE))1181return iwl_mvm_mac_ctxt_send_beacon_v9(mvm, vif, beacon,1182link_conf);11831184return iwl_mvm_mac_ctxt_send_beacon_v7(mvm, vif, beacon);1185}11861187/* The beacon template for the AP/GO/IBSS has changed and needs update */1188int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,1189struct ieee80211_vif *vif,1190struct ieee80211_bss_conf *link_conf)1191{1192struct sk_buff *beacon;1193int ret;11941195WARN_ON(vif->type != NL80211_IFTYPE_AP &&1196vif->type != NL80211_IFTYPE_ADHOC);11971198beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL,1199link_conf->link_id);1200if (!beacon)1201return -ENOMEM;12021203#ifdef CONFIG_IWLWIFI_DEBUGFS1204if (mvm->beacon_inject_active) {1205dev_kfree_skb(beacon);1206return -EBUSY;1207}1208#endif12091210ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon, link_conf);1211dev_kfree_skb(beacon);1212return ret;1213}12141215struct iwl_mvm_mac_ap_iterator_data {1216struct iwl_mvm *mvm;1217struct ieee80211_vif *vif;1218u32 beacon_device_ts;1219u16 beacon_int;1220};12211222/* Find the beacon_device_ts and beacon_int for a managed interface */1223static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,1224struct ieee80211_vif *vif)1225{1226struct iwl_mvm_mac_ap_iterator_data *data = _data;12271228if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc)1229return;12301231/* Station client has higher priority over P2P client*/1232if (vif->p2p && data->beacon_device_ts)1233return;12341235data->beacon_device_ts = vif->bss_conf.sync_device_ts;1236data->beacon_int = vif->bss_conf.beacon_int;1237}12381239/*1240* Fill the filter flags for mac context of type AP or P2P GO.1241*/1242void iwl_mvm_mac_ctxt_cmd_ap_set_filter_flags(struct iwl_mvm *mvm,1243struct iwl_mvm_vif *mvmvif,1244__le32 *filter_flags,1245int accept_probe_req_flag,1246int accept_beacon_flag)1247{1248/*1249* in AP mode, pass probe requests and beacons from other APs1250* (needed for ht protection); when there're no any associated1251* station don't ask FW to pass beacons to prevent unnecessary1252* wake-ups.1253*/1254*filter_flags |= cpu_to_le32(accept_probe_req_flag);1255if (mvmvif->ap_assoc_sta_count || !mvm->drop_bcn_ap_mode) {1256*filter_flags |= cpu_to_le32(accept_beacon_flag);1257IWL_DEBUG_HC(mvm, "Asking FW to pass beacons\n");1258} else {1259IWL_DEBUG_HC(mvm, "No need to receive beacons\n");1260}1261}12621263/*1264* Fill the specific data for mac context of type AP of P2P GO1265*/1266static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,1267struct ieee80211_vif *vif,1268struct iwl_mac_ctx_cmd *cmd,1269struct iwl_mac_data_ap *ctxt_ap,1270bool add)1271{1272struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);1273struct iwl_mvm_mac_ap_iterator_data data = {1274.mvm = mvm,1275.vif = vif,1276.beacon_device_ts = 01277};12781279/* in AP mode, the MCAST FIFO takes the EDCA params from VO */1280cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |= BIT(IWL_MVM_TX_FIFO_MCAST);12811282iwl_mvm_mac_ctxt_cmd_ap_set_filter_flags(mvm, mvmvif,1283&cmd->filter_flags,1284MAC_FILTER_IN_PROBE_REQUEST,1285MAC_FILTER_IN_BEACON);12861287ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);1288ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *1289vif->bss_conf.dtim_period);12901291if (!fw_has_api(&mvm->fw->ucode_capa,1292IWL_UCODE_TLV_API_STA_TYPE))1293ctxt_ap->mcast_qid = cpu_to_le32(mvmvif->deflink.cab_queue);12941295/*1296* Only set the beacon time when the MAC is being added, when we1297* just modify the MAC then we should keep the time -- the firmware1298* can otherwise have a "jumping" TBTT.1299*/1300if (add) {1301/*1302* If there is a station/P2P client interface which is1303* associated, set the AP's TBTT far enough from the station's1304* TBTT. Otherwise, set it to the current system time1305*/1306ieee80211_iterate_active_interfaces_atomic(1307mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,1308iwl_mvm_mac_ap_iterator, &data);13091310if (data.beacon_device_ts) {1311u32 rand = get_random_u32_inclusive(36, 63);1312mvmvif->ap_beacon_time = data.beacon_device_ts +1313ieee80211_tu_to_usec(data.beacon_int * rand /1314100);1315} else {1316mvmvif->ap_beacon_time = iwl_mvm_get_systime(mvm);1317}1318}13191320ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);1321ctxt_ap->beacon_tsf = 0; /* unused */13221323/* TODO: Assume that the beacon id == mac context id */1324ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);1325}13261327static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,1328struct ieee80211_vif *vif,1329u32 action)1330{1331struct iwl_mac_ctx_cmd cmd = {};13321333WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);13341335/* Fill the common data for all mac context types */1336iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);13371338/* Fill the data specific for ap mode */1339iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.ap,1340action == FW_CTXT_ACTION_ADD);13411342return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);1343}13441345static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,1346struct ieee80211_vif *vif,1347u32 action)1348{1349struct iwl_mac_ctx_cmd cmd = {};1350struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;13511352WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);13531354/* Fill the common data for all mac context types */1355iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);13561357/* Fill the data specific for GO mode */1358iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.go.ap,1359action == FW_CTXT_ACTION_ADD);13601361cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &1362IEEE80211_P2P_OPPPS_CTWINDOW_MASK);1363cmd.go.opp_ps_enabled =1364cpu_to_le32(!!(noa->oppps_ctwindow &1365IEEE80211_P2P_OPPPS_ENABLE_BIT));13661367return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);1368}13691370static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,1371u32 action, bool force_assoc_off,1372const u8 *bssid_override)1373{1374switch (vif->type) {1375case NL80211_IFTYPE_STATION:1376return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,1377force_assoc_off,1378bssid_override);1379case NL80211_IFTYPE_AP:1380if (!vif->p2p)1381return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);1382else1383return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);1384case NL80211_IFTYPE_MONITOR:1385return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);1386case NL80211_IFTYPE_P2P_DEVICE:1387return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);1388case NL80211_IFTYPE_ADHOC:1389return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);1390default:1391break;1392}13931394return -EOPNOTSUPP;1395}13961397int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)1398{1399struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);1400int ret;14011402if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",1403vif->addr, ieee80211_vif_type_p2p(vif)))1404return -EIO;14051406ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,1407true, NULL);1408if (ret)1409return ret;14101411/* will only do anything at resume from D3 time */1412iwl_mvm_set_last_nonqos_seq(mvm, vif);14131414mvmvif->uploaded = true;1415return 0;1416}14171418int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,1419bool force_assoc_off, const u8 *bssid_override)1420{1421struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);14221423if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",1424vif->addr, ieee80211_vif_type_p2p(vif)))1425return -EIO;14261427return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,1428force_assoc_off, bssid_override);1429}14301431int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)1432{1433struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);1434struct iwl_mac_ctx_cmd cmd;1435int ret;14361437if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",1438vif->addr, ieee80211_vif_type_p2p(vif)))1439return -EIO;14401441memset(&cmd, 0, sizeof(cmd));14421443cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,1444mvmvif->color));1445cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);14461447ret = iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);1448if (ret)1449return ret;14501451mvmvif->uploaded = false;14521453if (vif->type == NL80211_IFTYPE_MONITOR) {1454__clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, mvm->hw->flags);1455iwl_mvm_dealloc_snif_sta(mvm);1456}14571458return 0;1459}14601461static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,1462struct ieee80211_vif *csa_vif, u32 gp2,1463bool tx_success)1464{1465struct iwl_mvm_vif *mvmvif =1466iwl_mvm_vif_from_mac80211(csa_vif);14671468/* Don't start to countdown from a failed beacon */1469if (!tx_success && !mvmvif->csa_countdown)1470return;14711472mvmvif->csa_countdown = true;14731474if (!ieee80211_beacon_cntdwn_is_complete(csa_vif, 0)) {1475int c = ieee80211_beacon_update_cntdwn(csa_vif, 0);14761477iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif,1478&csa_vif->bss_conf);1479if (csa_vif->p2p &&1480!iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2 &&1481tx_success) {1482u32 rel_time = (c + 1) *1483csa_vif->bss_conf.beacon_int -1484IWL_MVM_CHANNEL_SWITCH_TIME_GO;1485u32 apply_time = gp2 + rel_time * 1024;14861487iwl_mvm_schedule_csa_period(mvm, csa_vif,1488IWL_MVM_CHANNEL_SWITCH_TIME_GO -1489IWL_MVM_CHANNEL_SWITCH_MARGIN,1490apply_time);1491}1492} else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {1493/* we don't have CSA NoA scheduled yet, switch now */1494ieee80211_csa_finish(csa_vif, 0);1495RCU_INIT_POINTER(mvm->csa_vif, NULL);1496}1497}14981499void iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,1500struct iwl_rx_cmd_buffer *rxb)1501{1502struct iwl_rx_packet *pkt = rxb_addr(rxb);1503unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);1504struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;1505struct iwl_extended_beacon_notif_v5 *beacon_v5 = (void *)pkt->data;1506struct ieee80211_vif *csa_vif;1507struct ieee80211_vif *tx_blocked_vif;1508struct agg_tx_status *agg_status;1509u16 status;15101511lockdep_assert_held(&mvm->mutex);15121513mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);15141515if (!iwl_mvm_is_short_beacon_notif_supported(mvm)) {1516struct iwl_tx_resp *beacon_notify_hdr =1517&beacon_v5->beacon_notify_hdr;15181519if (unlikely(pkt_len < sizeof(*beacon_v5)))1520return;15211522mvm->ibss_manager = beacon_v5->ibss_mgr_status != 0;1523agg_status = iwl_mvm_get_agg_status(mvm, beacon_notify_hdr);1524status = le16_to_cpu(agg_status->status) & TX_STATUS_MSK;1525IWL_DEBUG_RX(mvm,1526"beacon status %#x retries:%d tsf:0x%016llX gp2:0x%X rate:%d\n",1527status, beacon_notify_hdr->failure_frame,1528le64_to_cpu(beacon->tsf),1529mvm->ap_last_beacon_gp2,1530le32_to_cpu(beacon_notify_hdr->initial_rate));1531} else {1532if (unlikely(pkt_len < sizeof(*beacon)))1533return;15341535mvm->ibss_manager = beacon->ibss_mgr_status != 0;1536status = le32_to_cpu(beacon->status) & TX_STATUS_MSK;1537IWL_DEBUG_RX(mvm,1538"beacon status %#x tsf:0x%016llX gp2:0x%X\n",1539status, le64_to_cpu(beacon->tsf),1540mvm->ap_last_beacon_gp2);1541}15421543csa_vif = rcu_dereference_protected(mvm->csa_vif,1544lockdep_is_held(&mvm->mutex));1545if (unlikely(csa_vif && csa_vif->bss_conf.csa_active))1546iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2,1547(status == TX_STATUS_SUCCESS));15481549tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,1550lockdep_is_held(&mvm->mutex));1551if (unlikely(tx_blocked_vif)) {1552struct iwl_mvm_vif *mvmvif =1553iwl_mvm_vif_from_mac80211(tx_blocked_vif);15541555/*1556* The channel switch is started and we have blocked the1557* stations. If this is the first beacon (the timeout wasn't1558* set), set the unblock timeout, otherwise countdown1559*/1560if (!mvm->csa_tx_block_bcn_timeout)1561mvm->csa_tx_block_bcn_timeout =1562IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;1563else1564mvm->csa_tx_block_bcn_timeout--;15651566/* Check if the timeout is expired, and unblock tx */1567if (mvm->csa_tx_block_bcn_timeout == 0) {1568iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);1569RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);1570}1571}1572}15731574static void1575iwl_mvm_handle_missed_beacons_notif(struct iwl_mvm *mvm,1576const struct iwl_missed_beacons_notif *mb,1577struct iwl_rx_packet *pkt)1578{1579struct iwl_fw_dbg_trigger_missed_bcon *bcon_trig;1580struct iwl_fw_dbg_trigger_tlv *trigger;1581u32 stop_trig_missed_bcon, stop_trig_missed_bcon_since_rx;1582u32 rx_missed_bcon, rx_missed_bcon_since_rx;1583struct ieee80211_vif *vif;1584/* Id can be mac/link id depending on the notification version */1585u32 id = le32_to_cpu(mb->link_id);1586union iwl_dbg_tlv_tp_data tp_data = { .fw_pkt = pkt };1587u32 mac_type;1588int link_id;1589u8 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,1590MISSED_BEACONS_NOTIFICATION,15910);1592u8 new_notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,1593MISSED_BEACONS_NOTIF, 0);1594struct ieee80211_bss_conf *bss_conf;15951596/* If the firmware uses the new notification (from MAC_CONF_GROUP),1597* refer to that notification's version.1598* Note that the new notification from MAC_CONF_GROUP starts from1599* version 5.1600*/1601if (new_notif_ver)1602notif_ver = new_notif_ver;16031604IWL_DEBUG_INFO(mvm,1605"missed bcn %s_id=%u, consecutive=%u (%u)\n",1606notif_ver < 4 ? "mac" : "link",1607id,1608le32_to_cpu(mb->consec_missed_beacons),1609le32_to_cpu(mb->consec_missed_beacons_since_last_rx));16101611/*1612* starting from version 4 the ID is link ID, but driver1613* uses link ID == MAC ID, so always treat as MAC ID1614*/1615vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, false);1616if (!vif)1617return;16181619bss_conf = &vif->bss_conf;1620link_id = bss_conf->link_id;1621mac_type = iwl_mvm_get_mac_type(vif);16221623IWL_DEBUG_INFO(mvm, "missed beacon mac_type=%u,\n", mac_type);16241625mvm->trans->dbg.dump_file_name_ext_valid = true;1626snprintf(mvm->trans->dbg.dump_file_name_ext, IWL_FW_INI_MAX_NAME,1627"MacId_%d_MacType_%d", id, mac_type);16281629rx_missed_bcon = le32_to_cpu(mb->consec_missed_beacons);1630rx_missed_bcon_since_rx =1631le32_to_cpu(mb->consec_missed_beacons_since_last_rx);1632/*1633* TODO: the threshold should be adjusted based on latency conditions,1634* and/or in case of a CS flow on one of the other AP vifs.1635*/1636if (rx_missed_bcon >= IWL_MVM_MISSED_BEACONS_THRESHOLD_LONG) {1637if (rx_missed_bcon_since_rx >= IWL_MVM_MISSED_BEACONS_SINCE_RX_THOLD) {1638iwl_mvm_connection_loss(mvm, vif, "missed beacons");1639} else {1640IWL_WARN(mvm,1641"missed beacons exceeds threshold, but receiving data. Stay connected, Expect bugs.\n");1642IWL_WARN(mvm,1643"missed_beacons:%d, missed_beacons_since_rx:%d\n",1644rx_missed_bcon, rx_missed_bcon_since_rx);1645}1646} else if (link_id >= 0 && hweight16(vif->active_links) > 1) {1647u32 bss_param_ch_cnt_link_id =1648bss_conf->bss_param_ch_cnt_link_id;1649u32 scnd_lnk_bcn_lost = 0;16501651if (notif_ver >= 5 &&1652!IWL_FW_CHECK(mvm,1653le32_to_cpu(mb->other_link_id) == IWL_MVM_FW_LINK_ID_INVALID,1654"No data for other link id but we are in EMLSR active_links: 0x%x\n",1655vif->active_links))1656scnd_lnk_bcn_lost =1657le32_to_cpu(mb->consec_missed_beacons_other_link);16581659/* Exit EMLSR if we lost more than1660* IWL_MVM_MISSED_BEACONS_EXIT_ESR_THRESH beacons on boths links1661* OR more than IWL_MVM_BCN_LOSS_EXIT_ESR_THRESH on any link.1662* OR more than IWL_MVM_BCN_LOSS_EXIT_ESR_THRESH_BSS_PARAM_CHANGED1663* and the link's bss_param_ch_count has changed.1664*/1665if ((rx_missed_bcon >= IWL_MVM_BCN_LOSS_EXIT_ESR_THRESH_2_LINKS &&1666scnd_lnk_bcn_lost >= IWL_MVM_BCN_LOSS_EXIT_ESR_THRESH_2_LINKS) ||1667rx_missed_bcon >= IWL_MVM_BCN_LOSS_EXIT_ESR_THRESH ||1668(bss_param_ch_cnt_link_id != link_id &&1669rx_missed_bcon >= IWL_MVM_BCN_LOSS_EXIT_ESR_THRESH_BSS_PARAM_CHANGED))1670iwl_mvm_exit_esr(mvm, vif,1671IWL_MVM_ESR_EXIT_MISSED_BEACON,1672iwl_mvm_get_primary_link(vif));1673} else if (rx_missed_bcon_since_rx > IWL_MVM_MISSED_BEACONS_THRESHOLD) {1674if (!iwl_mvm_has_new_tx_api(mvm))1675ieee80211_beacon_loss(vif);1676else1677ieee80211_cqm_beacon_loss_notify(vif, GFP_ATOMIC);16781679/* try to switch links, no-op if we don't have MLO */1680iwl_mvm_int_mlo_scan(mvm, vif);1681}16821683iwl_dbg_tlv_time_point(&mvm->fwrt,1684IWL_FW_INI_TIME_POINT_MISSED_BEACONS, &tp_data);16851686trigger = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),1687FW_DBG_TRIGGER_MISSED_BEACONS);1688if (!trigger)1689return;16901691bcon_trig = (void *)trigger->data;1692stop_trig_missed_bcon = le32_to_cpu(bcon_trig->stop_consec_missed_bcon);1693stop_trig_missed_bcon_since_rx =1694le32_to_cpu(bcon_trig->stop_consec_missed_bcon_since_rx);16951696/* TODO: implement start trigger */16971698if (rx_missed_bcon_since_rx >= stop_trig_missed_bcon_since_rx ||1699rx_missed_bcon >= stop_trig_missed_bcon)1700#if defined(__linux__)1701iwl_fw_dbg_collect_trig(&mvm->fwrt, trigger, NULL);1702#elif defined(__FreeBSD__)1703iwl_fw_dbg_collect_trig(&mvm->fwrt, trigger, "");1704#endif1705}17061707void iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,1708struct iwl_rx_cmd_buffer *rxb)1709{1710struct iwl_rx_packet *pkt = rxb_addr(rxb);17111712iwl_mvm_handle_missed_beacons_notif(mvm, (const void *)pkt->data, pkt);1713}17141715void iwl_mvm_rx_missed_beacons_notif_legacy(struct iwl_mvm *mvm,1716struct iwl_rx_cmd_buffer *rxb)1717{1718struct iwl_rx_packet *pkt = rxb_addr(rxb);1719const struct iwl_missed_beacons_notif_v4 *mb_v4 =1720(const void *)pkt->data;1721struct iwl_missed_beacons_notif mb = {1722.link_id = mb_v4->link_id,1723.consec_missed_beacons = mb_v4->consec_missed_beacons,1724.consec_missed_beacons_since_last_rx =1725mb_v4->consec_missed_beacons_since_last_rx,1726.other_link_id = cpu_to_le32(IWL_MVM_FW_LINK_ID_INVALID),1727};17281729iwl_mvm_handle_missed_beacons_notif(mvm, &mb, pkt);1730}17311732void iwl_mvm_rx_stored_beacon_notif(struct iwl_mvm *mvm,1733struct iwl_rx_cmd_buffer *rxb)1734{1735struct iwl_rx_packet *pkt = rxb_addr(rxb);1736unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);1737struct iwl_stored_beacon_notif_common *sb = (void *)pkt->data;1738struct ieee80211_rx_status rx_status;1739struct sk_buff *skb;1740u8 *data;1741u32 size = le32_to_cpu(sb->byte_count);1742int ver = iwl_fw_lookup_cmd_ver(mvm->fw,1743WIDE_ID(PROT_OFFLOAD_GROUP, STORED_BEACON_NTF),17440);17451746if (size == 0)1747return;17481749/* handle per-version differences */1750if (ver <= 2) {1751struct iwl_stored_beacon_notif_v2 *sb_v2 = (void *)pkt->data;17521753if (pkt_len < struct_size(sb_v2, data, size))1754return;17551756data = sb_v2->data;1757} else {1758struct iwl_stored_beacon_notif *sb_v3 = (void *)pkt->data;17591760if (pkt_len < struct_size(sb_v3, data, size))1761return;17621763data = sb_v3->data;1764}17651766skb = alloc_skb(size, GFP_ATOMIC);1767if (!skb) {1768IWL_ERR(mvm, "alloc_skb failed\n");1769return;1770}17711772/* update rx_status according to the notification's metadata */1773memset(&rx_status, 0, sizeof(rx_status));1774rx_status.mactime = le64_to_cpu(sb->tsf);1775/* TSF as indicated by the firmware is at INA time */1776rx_status.flag |= RX_FLAG_MACTIME_PLCP_START;1777rx_status.device_timestamp = le32_to_cpu(sb->system_time);1778rx_status.band =1779(sb->band & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?1780NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;1781rx_status.freq =1782ieee80211_channel_to_frequency(le16_to_cpu(sb->channel),1783rx_status.band);17841785/* copy the data */1786skb_put_data(skb, data, size);1787memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));17881789/* pass it as regular rx to mac80211 */1790ieee80211_rx_napi(mvm->hw, NULL, skb, NULL);1791}17921793void iwl_mvm_probe_resp_data_notif(struct iwl_mvm *mvm,1794struct iwl_rx_cmd_buffer *rxb)1795{1796struct iwl_rx_packet *pkt = rxb_addr(rxb);1797struct iwl_probe_resp_data_notif *notif = (void *)pkt->data;1798struct iwl_probe_resp_data *old_data, *new_data;1799u32 id = le32_to_cpu(notif->mac_id);1800struct ieee80211_vif *vif;1801struct iwl_mvm_vif *mvmvif;18021803IWL_DEBUG_INFO(mvm, "Probe response data notif: noa %d, csa %d\n",1804notif->noa_active, notif->csa_counter);18051806vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, false);1807if (!vif)1808return;18091810mvmvif = iwl_mvm_vif_from_mac80211(vif);18111812new_data = kzalloc(sizeof(*new_data), GFP_KERNEL);1813if (!new_data)1814return;18151816memcpy(&new_data->notif, notif, sizeof(new_data->notif));18171818/* noa_attr contains 1 reserved byte, need to substruct it */1819new_data->noa_len = sizeof(struct ieee80211_vendor_ie) +1820sizeof(new_data->notif.noa_attr) - 1;18211822/*1823* If it's a one time NoA, only one descriptor is needed,1824* adjust the length according to len_low.1825*/1826if (new_data->notif.noa_attr.len_low ==1827sizeof(struct ieee80211_p2p_noa_desc) + 2)1828new_data->noa_len -= sizeof(struct ieee80211_p2p_noa_desc);18291830old_data = rcu_dereference_protected(mvmvif->deflink.probe_resp_data,1831lockdep_is_held(&mvmvif->mvm->mutex));1832rcu_assign_pointer(mvmvif->deflink.probe_resp_data, new_data);18331834if (old_data)1835kfree_rcu(old_data, rcu_head);18361837if (notif->csa_counter != IWL_PROBE_RESP_DATA_NO_CSA &&1838notif->csa_counter >= 1)1839ieee80211_beacon_set_cntdwn(vif, notif->csa_counter);1840}18411842void iwl_mvm_channel_switch_start_notif(struct iwl_mvm *mvm,1843struct iwl_rx_cmd_buffer *rxb)1844{1845struct iwl_rx_packet *pkt = rxb_addr(rxb);1846struct ieee80211_vif *csa_vif, *vif;1847struct iwl_mvm_vif *mvmvif, *csa_mvmvif;1848u32 id_n_color, csa_id;1849/* save mac_id or link_id to use later to cancel csa if needed */1850u32 id;1851u32 mac_link_id = 0;1852u8 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,1853CHANNEL_SWITCH_START_NOTIF, 0);1854bool csa_active;18551856rcu_read_lock();18571858if (notif_ver < 3) {1859struct iwl_channel_switch_start_notif_v1 *notif = (void *)pkt->data;1860u32 mac_id;18611862id_n_color = le32_to_cpu(notif->id_and_color);1863mac_id = id_n_color & FW_CTXT_ID_MSK;18641865vif = iwl_mvm_rcu_dereference_vif_id(mvm, mac_id, true);1866if (!vif)1867goto out_unlock;18681869id = mac_id;1870csa_active = vif->bss_conf.csa_active;1871} else {1872struct iwl_channel_switch_start_notif *notif = (void *)pkt->data;1873u32 link_id = le32_to_cpu(notif->link_id);18741875/* we use link ID == MAC ID */1876vif = iwl_mvm_rcu_dereference_vif_id(mvm, link_id, true);1877if (!vif)1878goto out_unlock;18791880id = link_id;1881mac_link_id = vif->bss_conf.link_id;1882csa_active = vif->bss_conf.csa_active;1883}18841885mvmvif = iwl_mvm_vif_from_mac80211(vif);1886if (notif_ver >= 3)1887id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);18881889switch (vif->type) {1890case NL80211_IFTYPE_AP:1891csa_vif = rcu_dereference(mvm->csa_vif);1892if (WARN_ON(!csa_vif || !csa_vif->bss_conf.csa_active ||1893csa_vif != vif))1894goto out_unlock;18951896csa_mvmvif = iwl_mvm_vif_from_mac80211(csa_vif);1897csa_id = FW_CMD_ID_AND_COLOR(csa_mvmvif->id, csa_mvmvif->color);1898if (WARN(csa_id != id_n_color,1899"channel switch noa notification on unexpected vif (csa_vif=%d, notif=%d)",1900csa_id, id_n_color))1901goto out_unlock;19021903IWL_DEBUG_INFO(mvm, "Channel Switch Started Notification\n");19041905schedule_delayed_work(&mvm->cs_tx_unblock_dwork,1906msecs_to_jiffies(IWL_MVM_CS_UNBLOCK_TX_TIMEOUT *1907csa_vif->bss_conf.beacon_int));19081909ieee80211_csa_finish(csa_vif, 0);19101911rcu_read_unlock();19121913RCU_INIT_POINTER(mvm->csa_vif, NULL);1914return;1915case NL80211_IFTYPE_STATION:1916/*1917* if we don't know about an ongoing channel switch,1918* make sure FW cancels it1919*/1920if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,1921CHANNEL_SWITCH_ERROR_NOTIF,19220) && !csa_active) {1923IWL_DEBUG_INFO(mvm, "Channel Switch was canceled\n");1924iwl_mvm_cancel_channel_switch(mvm, vif, id);1925break;1926}19271928iwl_mvm_csa_client_absent(mvm, vif);1929cancel_delayed_work(&mvmvif->csa_work);1930ieee80211_chswitch_done(vif, true, mac_link_id);1931break;1932default:1933/* should never happen */1934WARN_ON_ONCE(1);1935break;1936}1937out_unlock:1938rcu_read_unlock();1939}19401941void iwl_mvm_channel_switch_error_notif(struct iwl_mvm *mvm,1942struct iwl_rx_cmd_buffer *rxb)1943{1944struct iwl_rx_packet *pkt = rxb_addr(rxb);1945struct iwl_channel_switch_error_notif *notif = (void *)pkt->data;1946struct ieee80211_vif *vif;1947u32 id = le32_to_cpu(notif->link_id);1948u32 csa_err_mask = le32_to_cpu(notif->csa_err_mask);19491950rcu_read_lock();1951vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);1952if (!vif) {1953rcu_read_unlock();1954return;1955}19561957IWL_DEBUG_INFO(mvm, "FW reports CSA error: id=%u, csa_err_mask=%u\n",1958id, csa_err_mask);1959if (csa_err_mask & (CS_ERR_COUNT_ERROR |1960CS_ERR_LONG_DELAY_AFTER_CS |1961CS_ERR_TX_BLOCK_TIMER_EXPIRED))1962ieee80211_channel_switch_disconnect(vif);1963rcu_read_unlock();1964}196519661967