Path: blob/main/sys/contrib/dev/iwlwifi/mvm/mld-sta.c
48286 views
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause1/*2* Copyright (C) 2022-2025 Intel Corporation3*/4#include "mvm.h"5#include "time-sync.h"6#include "sta.h"78u32 iwl_mvm_sta_fw_id_mask(struct iwl_mvm *mvm, struct ieee80211_sta *sta,9int filter_link_id)10{11struct ieee80211_link_sta *link_sta;12struct iwl_mvm_sta *mvmsta;13struct ieee80211_vif *vif;14unsigned int link_id;15u32 result = 0;1617if (!sta)18return 0;1920mvmsta = iwl_mvm_sta_from_mac80211(sta);21vif = mvmsta->vif;2223/* it's easy when the STA is not an MLD */24if (!sta->valid_links)25return BIT(mvmsta->deflink.sta_id);2627/* but if it is an MLD, get the mask of all the FW STAs it has ... */28for_each_sta_active_link(vif, sta, link_sta, link_id) {29struct iwl_mvm_link_sta *mvm_link_sta;3031/* unless we have a specific link in mind */32if (filter_link_id >= 0 && link_id != filter_link_id)33continue;3435mvm_link_sta =36rcu_dereference_check(mvmsta->link[link_id],37lockdep_is_held(&mvm->mutex));38if (!mvm_link_sta)39continue;4041result |= BIT(mvm_link_sta->sta_id);42}4344return result;45}4647static int iwl_mvm_mld_send_sta_cmd(struct iwl_mvm *mvm,48struct iwl_sta_cfg_cmd *cmd)49{50u32 cmd_id = WIDE_ID(MAC_CONF_GROUP, STA_CONFIG_CMD);51int cmd_len = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id, 0) > 1 ?52sizeof(*cmd) :53sizeof(struct iwl_sta_cfg_cmd_v1);54int ret = iwl_mvm_send_cmd_pdu(mvm,55WIDE_ID(MAC_CONF_GROUP, STA_CONFIG_CMD),560, cmd_len, cmd);57if (ret)58IWL_ERR(mvm, "STA_CONFIG_CMD send failed, ret=0x%x\n", ret);59return ret;60}6162/*63* Add an internal station to the FW table64*/65static int iwl_mvm_mld_add_int_sta_to_fw(struct iwl_mvm *mvm,66struct iwl_mvm_int_sta *sta,67const u8 *addr, int link_id)68{69struct iwl_sta_cfg_cmd cmd;7071lockdep_assert_held(&mvm->mutex);7273memset(&cmd, 0, sizeof(cmd));74cmd.sta_id = cpu_to_le32((u8)sta->sta_id);7576cmd.link_id = cpu_to_le32(link_id);7778cmd.station_type = cpu_to_le32(sta->type);7980if (fw_has_capa(&mvm->fw->ucode_capa,81IWL_UCODE_TLV_CAPA_STA_EXP_MFP_SUPPORT) &&82sta->type == STATION_TYPE_BCAST_MGMT)83cmd.mfp = cpu_to_le32(1);8485if (addr) {86memcpy(cmd.peer_mld_address, addr, ETH_ALEN);87memcpy(cmd.peer_link_address, addr, ETH_ALEN);88}8990return iwl_mvm_mld_send_sta_cmd(mvm, &cmd);91}9293/*94* Remove a station from the FW table. Before sending the command to remove95* the station validate that the station is indeed known to the driver (sanity96* only).97*/98static int iwl_mvm_mld_rm_sta_from_fw(struct iwl_mvm *mvm, u32 sta_id)99{100struct iwl_remove_sta_cmd rm_sta_cmd = {101.sta_id = cpu_to_le32(sta_id),102};103int ret;104105/* Note: internal stations are marked as error values */106if (!rcu_access_pointer(mvm->fw_id_to_mac_id[sta_id])) {107IWL_ERR(mvm, "Invalid station id %d\n", sta_id);108return -EINVAL;109}110111ret = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(MAC_CONF_GROUP, STA_REMOVE_CMD),1120, sizeof(rm_sta_cmd), &rm_sta_cmd);113if (ret) {114IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);115return ret;116}117118return 0;119}120121static int iwl_mvm_add_aux_sta_to_fw(struct iwl_mvm *mvm,122struct iwl_mvm_int_sta *sta,123u32 lmac_id)124{125int ret;126127struct iwl_aux_sta_cmd cmd = {128.sta_id = cpu_to_le32(sta->sta_id),129.lmac_id = cpu_to_le32(lmac_id),130};131132ret = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(MAC_CONF_GROUP, AUX_STA_CMD),1330, sizeof(cmd), &cmd);134if (ret)135IWL_ERR(mvm, "Failed to send AUX_STA_CMD\n");136return ret;137}138139/*140* Adds an internal sta to the FW table with its queues141*/142int iwl_mvm_mld_add_int_sta_with_queue(struct iwl_mvm *mvm,143struct iwl_mvm_int_sta *sta,144const u8 *addr, int link_id,145u16 *queue, u8 tid,146unsigned int *_wdg_timeout)147{148int ret, txq;149unsigned int wdg_timeout = _wdg_timeout ? *_wdg_timeout :150mvm->trans->mac_cfg->base->wd_timeout;151152if (WARN_ON_ONCE(sta->sta_id == IWL_INVALID_STA))153return -ENOSPC;154155if (sta->type == STATION_TYPE_AUX)156ret = iwl_mvm_add_aux_sta_to_fw(mvm, sta, link_id);157else158ret = iwl_mvm_mld_add_int_sta_to_fw(mvm, sta, addr, link_id);159if (ret)160return ret;161162/*163* For 22000 firmware and on we cannot add queue to a station unknown164* to firmware so enable queue here - after the station was added165*/166txq = iwl_mvm_tvqm_enable_txq(mvm, NULL, sta->sta_id, tid,167wdg_timeout);168if (txq < 0) {169iwl_mvm_mld_rm_sta_from_fw(mvm, sta->sta_id);170return txq;171}172*queue = txq;173174return 0;175}176177/*178* Adds a new int sta: allocate it in the driver, add it to the FW table,179* and add its queues.180*/181static int iwl_mvm_mld_add_int_sta(struct iwl_mvm *mvm,182struct iwl_mvm_int_sta *int_sta, u16 *queue,183enum nl80211_iftype iftype,184enum iwl_fw_sta_type sta_type,185int link_id, const u8 *addr, u8 tid,186unsigned int *wdg_timeout)187{188int ret;189190lockdep_assert_held(&mvm->mutex);191192/* qmask argument is not used in the new tx api, send a don't care */193ret = iwl_mvm_allocate_int_sta(mvm, int_sta, 0, iftype,194sta_type);195if (ret)196return ret;197198ret = iwl_mvm_mld_add_int_sta_with_queue(mvm, int_sta, addr, link_id,199queue, tid, wdg_timeout);200if (ret) {201iwl_mvm_dealloc_int_sta(mvm, int_sta);202return ret;203}204205return 0;206}207208/* Allocate a new station entry for the broadcast station to the given vif,209* and send it to the FW.210* Note that each P2P mac should have its own broadcast station.211*/212int iwl_mvm_mld_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,213struct ieee80211_bss_conf *link_conf)214{215struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);216struct iwl_mvm_vif_link_info *mvm_link =217mvmvif->link[link_conf->link_id];218struct iwl_mvm_int_sta *bsta = &mvm_link->bcast_sta;219static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};220const u8 *baddr = _baddr;221unsigned int wdg_timeout =222iwl_mvm_get_wd_timeout(mvm, vif);223u16 *queue;224225lockdep_assert_held(&mvm->mutex);226227if (vif->type == NL80211_IFTYPE_ADHOC)228baddr = link_conf->bssid;229230if (vif->type == NL80211_IFTYPE_AP ||231vif->type == NL80211_IFTYPE_ADHOC) {232queue = &mvm_link->mgmt_queue;233} else if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {234queue = &mvm->p2p_dev_queue;235} else {236WARN(1, "Missing required TXQ for adding bcast STA\n");237return -EINVAL;238}239240return iwl_mvm_mld_add_int_sta(mvm, bsta, queue,241ieee80211_vif_type_p2p(vif),242STATION_TYPE_BCAST_MGMT,243mvm_link->fw_link_id, baddr,244IWL_MAX_TID_COUNT, &wdg_timeout);245}246247/* Allocate a new station entry for the multicast station to the given vif,248* and send it to the FW.249* Note that each AP/GO mac should have its own multicast station.250*/251int iwl_mvm_mld_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,252struct ieee80211_bss_conf *link_conf)253{254struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);255struct iwl_mvm_vif_link_info *mvm_link =256mvmvif->link[link_conf->link_id];257struct iwl_mvm_int_sta *msta = &mvm_link->mcast_sta;258static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};259const u8 *maddr = _maddr;260unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif);261262lockdep_assert_held(&mvm->mutex);263264if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&265vif->type != NL80211_IFTYPE_ADHOC))266return -EOPNOTSUPP;267268/* In IBSS, ieee80211_check_queues() sets the cab_queue to be269* invalid, so make sure we use the queue we want.270* Note that this is done here as we want to avoid making DQA271* changes in mac80211 layer.272*/273if (vif->type == NL80211_IFTYPE_ADHOC)274mvm_link->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;275276return iwl_mvm_mld_add_int_sta(mvm, msta, &mvm_link->cab_queue,277vif->type, STATION_TYPE_MCAST,278mvm_link->fw_link_id, maddr, 0,279&timeout);280}281282/* Allocate a new station entry for the sniffer station to the given vif,283* and send it to the FW.284*/285int iwl_mvm_mld_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,286struct ieee80211_bss_conf *link_conf)287{288struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);289struct iwl_mvm_vif_link_info *mvm_link =290mvmvif->link[link_conf->link_id];291292lockdep_assert_held(&mvm->mutex);293294return iwl_mvm_mld_add_int_sta(mvm, &mvm->snif_sta, &mvm->snif_queue,295vif->type, STATION_TYPE_BCAST_MGMT,296mvm_link->fw_link_id, NULL,297IWL_MAX_TID_COUNT, NULL);298}299300int iwl_mvm_mld_add_aux_sta(struct iwl_mvm *mvm, u32 lmac_id)301{302lockdep_assert_held(&mvm->mutex);303304/* In CDB NICs we need to specify which lmac to use for aux activity;305* use the link_id argument place to send lmac_id to the function.306*/307return iwl_mvm_mld_add_int_sta(mvm, &mvm->aux_sta, &mvm->aux_queue,308NL80211_IFTYPE_UNSPECIFIED,309STATION_TYPE_AUX, lmac_id, NULL,310IWL_MAX_TID_COUNT, NULL);311}312313static int iwl_mvm_mld_disable_txq(struct iwl_mvm *mvm, u32 sta_mask,314u16 *queueptr, u8 tid)315{316int queue = *queueptr;317int ret = 0;318319if (tid == IWL_MAX_TID_COUNT)320tid = IWL_MGMT_TID;321322if (mvm->sta_remove_requires_queue_remove) {323u32 cmd_id = WIDE_ID(DATA_PATH_GROUP,324SCD_QUEUE_CONFIG_CMD);325struct iwl_scd_queue_cfg_cmd remove_cmd = {326.operation = cpu_to_le32(IWL_SCD_QUEUE_REMOVE),327.u.remove.tid = cpu_to_le32(tid),328.u.remove.sta_mask = cpu_to_le32(sta_mask),329};330331ret = iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0,332sizeof(remove_cmd),333&remove_cmd);334}335336iwl_trans_txq_free(mvm->trans, queue);337*queueptr = IWL_MVM_INVALID_QUEUE;338339return ret;340}341342/* Removes a sta from the FW table, disable its queues, and dealloc it343*/344static int iwl_mvm_mld_rm_int_sta(struct iwl_mvm *mvm,345struct iwl_mvm_int_sta *int_sta,346bool flush, u8 tid, u16 *queuptr)347{348int ret;349350lockdep_assert_held(&mvm->mutex);351352if (WARN_ON_ONCE(int_sta->sta_id == IWL_INVALID_STA))353return -EINVAL;354355if (flush)356iwl_mvm_flush_sta(mvm, int_sta->sta_id, int_sta->tfd_queue_msk);357358iwl_mvm_mld_disable_txq(mvm, BIT(int_sta->sta_id), queuptr, tid);359360ret = iwl_mvm_mld_rm_sta_from_fw(mvm, int_sta->sta_id);361if (ret)362IWL_WARN(mvm, "Failed sending remove station\n");363364iwl_mvm_dealloc_int_sta(mvm, int_sta);365366return ret;367}368369int iwl_mvm_mld_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,370struct ieee80211_bss_conf *link_conf)371{372struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);373struct iwl_mvm_vif_link_info *link = mvmvif->link[link_conf->link_id];374u16 *queueptr;375376lockdep_assert_held(&mvm->mutex);377378if (WARN_ON(!link))379return -EIO;380381switch (vif->type) {382case NL80211_IFTYPE_AP:383case NL80211_IFTYPE_ADHOC:384queueptr = &link->mgmt_queue;385break;386case NL80211_IFTYPE_P2P_DEVICE:387queueptr = &mvm->p2p_dev_queue;388break;389default:390WARN(1, "Can't free bcast queue on vif type %d\n",391vif->type);392return -EINVAL;393}394395return iwl_mvm_mld_rm_int_sta(mvm, &link->bcast_sta,396true, IWL_MAX_TID_COUNT, queueptr);397}398399/* Send the FW a request to remove the station from it's internal data400* structures, and in addition remove it from the local data structure.401*/402int iwl_mvm_mld_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,403struct ieee80211_bss_conf *link_conf)404{405struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);406struct iwl_mvm_vif_link_info *link = mvmvif->link[link_conf->link_id];407408lockdep_assert_held(&mvm->mutex);409410if (WARN_ON(!link))411return -EIO;412413return iwl_mvm_mld_rm_int_sta(mvm, &link->mcast_sta, true, 0,414&link->cab_queue);415}416417int iwl_mvm_mld_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)418{419lockdep_assert_held(&mvm->mutex);420421return iwl_mvm_mld_rm_int_sta(mvm, &mvm->snif_sta, false,422IWL_MAX_TID_COUNT, &mvm->snif_queue);423}424425int iwl_mvm_mld_rm_aux_sta(struct iwl_mvm *mvm)426{427lockdep_assert_held(&mvm->mutex);428429return iwl_mvm_mld_rm_int_sta(mvm, &mvm->aux_sta, false,430IWL_MAX_TID_COUNT, &mvm->aux_queue);431}432433/* send a cfg sta command to add/update a sta in firmware */434static int iwl_mvm_mld_cfg_sta(struct iwl_mvm *mvm, struct ieee80211_sta *sta,435struct ieee80211_vif *vif,436struct ieee80211_link_sta *link_sta,437struct ieee80211_bss_conf *link_conf,438struct iwl_mvm_link_sta *mvm_link_sta)439{440struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);441struct iwl_mvm_vif *mvm_vif = iwl_mvm_vif_from_mac80211(vif);442struct iwl_mvm_vif_link_info *link_info =443mvm_vif->link[link_conf->link_id];444struct iwl_sta_cfg_cmd cmd = {445.sta_id = cpu_to_le32(mvm_link_sta->sta_id),446.station_type = cpu_to_le32(mvm_sta->sta_type),447};448u32 agg_size = 0, mpdu_dens = 0;449450/* when adding sta, link should exist in FW */451if (WARN_ON(link_info->fw_link_id == IWL_MVM_FW_LINK_ID_INVALID))452return -EINVAL;453454cmd.link_id = cpu_to_le32(link_info->fw_link_id);455456memcpy(&cmd.peer_mld_address, sta->addr, ETH_ALEN);457memcpy(&cmd.peer_link_address, link_sta->addr, ETH_ALEN);458459if (mvm_sta->sta_state >= IEEE80211_STA_ASSOC)460cmd.assoc_id = cpu_to_le32(sta->aid);461462if (fw_has_capa(&mvm->fw->ucode_capa,463IWL_UCODE_TLV_CAPA_STA_EXP_MFP_SUPPORT) &&464(sta->mfp || mvm_sta->sta_state < IEEE80211_STA_AUTHORIZED))465cmd.mfp = cpu_to_le32(1);466467switch (link_sta->rx_nss) {468case 1:469cmd.mimo = cpu_to_le32(0);470break;471case 2 ... 8:472cmd.mimo = cpu_to_le32(1);473break;474}475476switch (link_sta->smps_mode) {477case IEEE80211_SMPS_AUTOMATIC:478case IEEE80211_SMPS_NUM_MODES:479WARN_ON(1);480break;481case IEEE80211_SMPS_STATIC:482/* override NSS */483cmd.mimo = cpu_to_le32(0);484break;485case IEEE80211_SMPS_DYNAMIC:486cmd.mimo_protection = cpu_to_le32(1);487break;488case IEEE80211_SMPS_OFF:489/* nothing */490break;491}492493mpdu_dens = iwl_mvm_get_sta_ampdu_dens(link_sta, link_conf, &agg_size);494cmd.tx_ampdu_spacing = cpu_to_le32(mpdu_dens);495cmd.tx_ampdu_max_size = cpu_to_le32(agg_size);496497if (sta->wme) {498cmd.sp_length =499cpu_to_le32(sta->max_sp ? sta->max_sp * 2 : 128);500cmd.uapsd_acs = cpu_to_le32(iwl_mvm_get_sta_uapsd_acs(sta));501}502503if (link_sta->he_cap.has_he) {504cmd.trig_rnd_alloc =505cpu_to_le32(link_conf->uora_exists ? 1 : 0);506507/* PPE Thresholds */508iwl_mvm_set_sta_pkt_ext(mvm, link_sta, &cmd.pkt_ext);509510/* HTC flags */511cmd.htc_flags = iwl_mvm_get_sta_htc_flags(sta, link_sta);512513if (link_sta->he_cap.he_cap_elem.mac_cap_info[2] &514IEEE80211_HE_MAC_CAP2_ACK_EN)515cmd.ack_enabled = cpu_to_le32(1);516}517518return iwl_mvm_mld_send_sta_cmd(mvm, &cmd);519}520521void iwl_mvm_mld_free_sta_link(struct iwl_mvm *mvm,522struct iwl_mvm_sta *mvm_sta,523struct iwl_mvm_link_sta *mvm_sta_link,524unsigned int link_id)525{526lockdep_assert_wiphy(mvm->hw->wiphy);527lockdep_assert_held(&mvm->mutex);528529RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta_link->sta_id], NULL);530RCU_INIT_POINTER(mvm->fw_id_to_link_sta[mvm_sta_link->sta_id], NULL);531RCU_INIT_POINTER(mvm_sta->link[link_id], NULL);532533if (mvm_sta_link != &mvm_sta->deflink)534kfree_rcu(mvm_sta_link, rcu_head);535}536537static void iwl_mvm_mld_sta_rm_all_sta_links(struct iwl_mvm *mvm,538struct iwl_mvm_sta *mvm_sta)539{540unsigned int link_id;541542for (link_id = 0; link_id < ARRAY_SIZE(mvm_sta->link); link_id++) {543struct iwl_mvm_link_sta *link =544rcu_dereference_protected(mvm_sta->link[link_id],545lockdep_is_held(&mvm->mutex));546547if (!link)548continue;549550iwl_mvm_mld_free_sta_link(mvm, mvm_sta, link, link_id);551}552}553554static int iwl_mvm_mld_alloc_sta_link(struct iwl_mvm *mvm,555struct ieee80211_vif *vif,556struct ieee80211_sta *sta,557unsigned int link_id)558{559struct ieee80211_link_sta *link_sta =560link_sta_dereference_protected(sta, link_id);561struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);562struct iwl_mvm_link_sta *link;563u32 sta_id = iwl_mvm_find_free_sta_id(mvm,564ieee80211_vif_type_p2p(vif));565566lockdep_assert_wiphy(mvm->hw->wiphy);567lockdep_assert_held(&mvm->mutex);568569if (sta_id == IWL_INVALID_STA)570return -ENOSPC;571572if (rcu_access_pointer(sta->link[link_id]) == &sta->deflink) {573link = &mvm_sta->deflink;574} else {575link = kzalloc(sizeof(*link), GFP_KERNEL);576if (!link)577return -ENOMEM;578}579580link->sta_id = sta_id;581rcu_assign_pointer(mvm_sta->link[link_id], link);582rcu_assign_pointer(mvm->fw_id_to_mac_id[link->sta_id], sta);583rcu_assign_pointer(mvm->fw_id_to_link_sta[link->sta_id],584link_sta);585586return 0;587}588589/* allocate all the links of a sta, called when the station is first added */590static int iwl_mvm_mld_alloc_sta_links(struct iwl_mvm *mvm,591struct ieee80211_vif *vif,592struct ieee80211_sta *sta)593{594struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);595struct ieee80211_link_sta *link_sta;596unsigned int link_id;597int ret;598599lockdep_assert_held(&mvm->mutex);600601for_each_sta_active_link(vif, sta, link_sta, link_id) {602if (WARN_ON(mvm_sta->link[link_id]))603continue;604605ret = iwl_mvm_mld_alloc_sta_link(mvm, vif, sta, link_id);606if (ret)607goto err;608}609610return 0;611612err:613iwl_mvm_mld_sta_rm_all_sta_links(mvm, mvm_sta);614return ret;615}616617static void iwl_mvm_mld_set_ap_sta_id(struct ieee80211_sta *sta,618struct iwl_mvm_vif_link_info *vif_link,619struct iwl_mvm_link_sta *sta_link)620{621if (!sta->tdls) {622WARN_ON(vif_link->ap_sta_id != IWL_INVALID_STA);623vif_link->ap_sta_id = sta_link->sta_id;624} else {625WARN_ON(vif_link->ap_sta_id == IWL_INVALID_STA);626}627}628629static int iwl_mvm_alloc_sta_after_restart(struct iwl_mvm *mvm,630struct ieee80211_vif *vif,631struct ieee80211_sta *sta)632{633struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);634struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);635struct ieee80211_link_sta *link_sta;636unsigned int link_id;637/* no active link found */638int ret = -EINVAL;639int sta_id;640641lockdep_assert_wiphy(mvm->hw->wiphy);642lockdep_assert_held(&mvm->mutex);643644/* First add an empty station since allocating a queue requires645* a valid station. Since we need a link_id to allocate a station,646* pick up the first valid one.647*/648for_each_sta_active_link(vif, sta, link_sta, link_id) {649struct iwl_mvm_vif_link_info *mvm_link;650struct ieee80211_bss_conf *link_conf =651link_conf_dereference_protected(vif, link_id);652struct iwl_mvm_link_sta *mvm_link_sta =653rcu_dereference_protected(mvm_sta->link[link_id],654lockdep_is_held(&mvm->mutex));655656if (!link_conf)657continue;658659mvm_link = mvmvif->link[link_conf->link_id];660661if (!mvm_link || !mvm_link_sta)662continue;663664sta_id = mvm_link_sta->sta_id;665ret = iwl_mvm_mld_cfg_sta(mvm, sta, vif, link_sta,666link_conf, mvm_link_sta);667if (ret)668return ret;669670rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);671rcu_assign_pointer(mvm->fw_id_to_link_sta[sta_id], link_sta);672ret = 0;673}674675iwl_mvm_realloc_queues_after_restart(mvm, sta);676677return ret;678}679680int iwl_mvm_mld_add_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,681struct ieee80211_sta *sta)682{683struct iwl_mvm_vif *mvm_vif = iwl_mvm_vif_from_mac80211(vif);684struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);685unsigned long link_sta_added_to_fw = 0;686struct ieee80211_link_sta *link_sta;687int ret = 0;688unsigned int link_id;689690lockdep_assert_held(&mvm->mutex);691692if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {693ret = iwl_mvm_mld_alloc_sta_links(mvm, vif, sta);694if (ret)695return ret;696697spin_lock_init(&mvm_sta->lock);698699ret = iwl_mvm_sta_init(mvm, vif, sta, IWL_INVALID_STA,700STATION_TYPE_PEER);701} else {702ret = iwl_mvm_alloc_sta_after_restart(mvm, vif, sta);703}704705if (ret)706goto err;707708/* at this stage sta link pointers are already allocated */709ret = iwl_mvm_mld_update_sta(mvm, vif, sta);710if (ret)711goto err;712713for_each_sta_active_link(vif, sta, link_sta, link_id) {714struct ieee80211_bss_conf *link_conf =715link_conf_dereference_protected(vif, link_id);716struct iwl_mvm_link_sta *mvm_link_sta =717rcu_dereference_protected(mvm_sta->link[link_id],718lockdep_is_held(&mvm->mutex));719720if (WARN_ON(!link_conf || !mvm_link_sta)) {721ret = -EINVAL;722goto err;723}724725ret = iwl_mvm_mld_cfg_sta(mvm, sta, vif, link_sta, link_conf,726mvm_link_sta);727if (ret)728goto err;729730link_sta_added_to_fw |= BIT(link_id);731732if (vif->type == NL80211_IFTYPE_STATION)733iwl_mvm_mld_set_ap_sta_id(sta, mvm_vif->link[link_id],734mvm_link_sta);735}736return 0;737738err:739/* remove all already allocated stations in FW */740for_each_set_bit(link_id, &link_sta_added_to_fw,741IEEE80211_MLD_MAX_NUM_LINKS) {742struct iwl_mvm_link_sta *mvm_link_sta =743rcu_dereference_protected(mvm_sta->link[link_id],744lockdep_is_held(&mvm->mutex));745746iwl_mvm_mld_rm_sta_from_fw(mvm, mvm_link_sta->sta_id);747}748749/* free all sta resources in the driver */750iwl_mvm_mld_sta_rm_all_sta_links(mvm, mvm_sta);751return ret;752}753754int iwl_mvm_mld_update_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,755struct ieee80211_sta *sta)756{757struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);758struct ieee80211_link_sta *link_sta;759unsigned int link_id;760int ret = -EINVAL;761762lockdep_assert_held(&mvm->mutex);763764for_each_sta_active_link(vif, sta, link_sta, link_id) {765struct ieee80211_bss_conf *link_conf =766link_conf_dereference_protected(vif, link_id);767struct iwl_mvm_link_sta *mvm_link_sta =768rcu_dereference_protected(mvm_sta->link[link_id],769lockdep_is_held(&mvm->mutex));770771if (WARN_ON(!link_conf || !mvm_link_sta))772return -EINVAL;773774ret = iwl_mvm_mld_cfg_sta(mvm, sta, vif, link_sta, link_conf,775mvm_link_sta);776777if (ret) {778IWL_ERR(mvm, "Failed to update sta link %d\n", link_id);779break;780}781}782783return ret;784}785786static void iwl_mvm_mld_disable_sta_queues(struct iwl_mvm *mvm,787struct ieee80211_vif *vif,788struct ieee80211_sta *sta)789{790struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);791u32 sta_mask = iwl_mvm_sta_fw_id_mask(mvm, sta, -1);792int i;793794lockdep_assert_held(&mvm->mutex);795796for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {797if (mvm_sta->tid_data[i].txq_id == IWL_MVM_INVALID_QUEUE)798continue;799800iwl_mvm_mld_disable_txq(mvm, sta_mask,801&mvm_sta->tid_data[i].txq_id, i);802mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;803}804805for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {806struct iwl_mvm_txq *mvmtxq =807iwl_mvm_txq_from_mac80211(sta->txq[i]);808809mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;810}811}812813int iwl_mvm_mld_rm_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,814struct ieee80211_sta *sta)815{816struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);817struct ieee80211_link_sta *link_sta;818unsigned int link_id;819int ret;820821lockdep_assert_held(&mvm->mutex);822823/* flush its queues here since we are freeing mvm_sta */824for_each_sta_active_link(vif, sta, link_sta, link_id) {825struct iwl_mvm_link_sta *mvm_link_sta =826rcu_dereference_protected(mvm_sta->link[link_id],827lockdep_is_held(&mvm->mutex));828829if (WARN_ON(!mvm_link_sta))830return -EINVAL;831832ret = iwl_mvm_flush_sta_tids(mvm, mvm_link_sta->sta_id,8330xffff);834if (ret)835return ret;836}837838ret = iwl_mvm_wait_sta_queues_empty(mvm, mvm_sta);839if (ret)840return ret;841842iwl_mvm_mld_disable_sta_queues(mvm, vif, sta);843844for_each_sta_active_link(vif, sta, link_sta, link_id) {845struct iwl_mvm_link_sta *mvm_link_sta =846rcu_dereference_protected(mvm_sta->link[link_id],847lockdep_is_held(&mvm->mutex));848iwl_mvm_sta_del(mvm, vif, sta, link_sta);849850ret = iwl_mvm_mld_rm_sta_from_fw(mvm, mvm_link_sta->sta_id);851852iwl_mvm_mld_free_sta_link(mvm, mvm_sta, mvm_link_sta, link_id);853}854kfree(mvm_sta->mpdu_counters);855mvm_sta->mpdu_counters = NULL;856857return ret;858}859860int iwl_mvm_mld_rm_sta_id(struct iwl_mvm *mvm, u8 sta_id)861{862int ret;863864lockdep_assert_wiphy(mvm->hw->wiphy);865lockdep_assert_held(&mvm->mutex);866867if (WARN_ON(sta_id == IWL_INVALID_STA))868return 0;869870ret = iwl_mvm_mld_rm_sta_from_fw(mvm, sta_id);871872RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);873RCU_INIT_POINTER(mvm->fw_id_to_link_sta[sta_id], NULL);874return ret;875}876877void iwl_mvm_mld_sta_modify_disable_tx(struct iwl_mvm *mvm,878struct iwl_mvm_sta *mvmsta,879bool disable)880{881struct iwl_mvm_sta_disable_tx_cmd cmd;882int ret;883884cmd.sta_id = cpu_to_le32(mvmsta->deflink.sta_id);885cmd.disable = cpu_to_le32(disable);886887if (WARN_ON(iwl_mvm_has_no_host_disable_tx(mvm)))888return;889890ret = iwl_mvm_send_cmd_pdu(mvm,891WIDE_ID(MAC_CONF_GROUP, STA_DISABLE_TX_CMD),892CMD_ASYNC, sizeof(cmd), &cmd);893if (ret)894IWL_ERR(mvm,895"Failed to send STA_DISABLE_TX_CMD command (%d)\n",896ret);897}898899void iwl_mvm_mld_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,900struct ieee80211_sta *sta,901bool disable)902{903struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);904905spin_lock_bh(&mvm_sta->lock);906907if (mvm_sta->disable_tx == disable) {908spin_unlock_bh(&mvm_sta->lock);909return;910}911912iwl_mvm_mld_sta_modify_disable_tx(mvm, mvm_sta, disable);913914spin_unlock_bh(&mvm_sta->lock);915}916917void iwl_mvm_mld_modify_all_sta_disable_tx(struct iwl_mvm *mvm,918struct iwl_mvm_vif *mvmvif,919bool disable)920{921struct ieee80211_sta *sta;922struct iwl_mvm_sta *mvm_sta;923int i;924925rcu_read_lock();926927/* Block/unblock all the stations of the given mvmvif */928for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {929sta = rcu_dereference(mvm->fw_id_to_mac_id[i]);930if (IS_ERR_OR_NULL(sta))931continue;932933mvm_sta = iwl_mvm_sta_from_mac80211(sta);934if (mvm_sta->mac_id_n_color !=935FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))936continue;937938iwl_mvm_mld_sta_modify_disable_tx(mvm, mvm_sta, disable);939}940941rcu_read_unlock();942}943944static int iwl_mvm_mld_update_sta_queues(struct iwl_mvm *mvm,945struct ieee80211_sta *sta,946u32 old_sta_mask,947u32 new_sta_mask)948{949struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);950struct iwl_scd_queue_cfg_cmd cmd = {951.operation = cpu_to_le32(IWL_SCD_QUEUE_MODIFY),952.u.modify.old_sta_mask = cpu_to_le32(old_sta_mask),953.u.modify.new_sta_mask = cpu_to_le32(new_sta_mask),954};955struct iwl_host_cmd hcmd = {956.id = WIDE_ID(DATA_PATH_GROUP, SCD_QUEUE_CONFIG_CMD),957.len[0] = sizeof(cmd),958.data[0] = &cmd959};960int tid;961int ret;962963lockdep_assert_held(&mvm->mutex);964965for (tid = 0; tid <= IWL_MAX_TID_COUNT; tid++) {966struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[tid];967int txq_id = tid_data->txq_id;968969if (txq_id == IWL_MVM_INVALID_QUEUE)970continue;971972if (tid == IWL_MAX_TID_COUNT)973cmd.u.modify.tid = cpu_to_le32(IWL_MGMT_TID);974else975cmd.u.modify.tid = cpu_to_le32(tid);976977ret = iwl_mvm_send_cmd(mvm, &hcmd);978if (ret)979return ret;980}981982return 0;983}984985static int iwl_mvm_mld_update_sta_baids(struct iwl_mvm *mvm,986u32 old_sta_mask,987u32 new_sta_mask)988{989struct iwl_rx_baid_cfg_cmd cmd = {990.action = cpu_to_le32(IWL_RX_BAID_ACTION_MODIFY),991.modify.old_sta_id_mask = cpu_to_le32(old_sta_mask),992.modify.new_sta_id_mask = cpu_to_le32(new_sta_mask),993};994u32 cmd_id = WIDE_ID(DATA_PATH_GROUP, RX_BAID_ALLOCATION_CONFIG_CMD);995int baid;996997/* mac80211 will remove sessions later, but we ignore all that */998if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))999return 0;10001001BUILD_BUG_ON(sizeof(struct iwl_rx_baid_cfg_resp) != sizeof(baid));10021003for (baid = 0; baid < ARRAY_SIZE(mvm->baid_map); baid++) {1004struct iwl_mvm_baid_data *data;1005int ret;10061007data = rcu_dereference_protected(mvm->baid_map[baid],1008lockdep_is_held(&mvm->mutex));1009if (!data)1010continue;10111012if (!(data->sta_mask & old_sta_mask))1013continue;10141015WARN_ONCE(data->sta_mask != old_sta_mask,1016"BAID data for %d corrupted - expected 0x%x found 0x%x\n",1017baid, old_sta_mask, data->sta_mask);10181019cmd.modify.tid = cpu_to_le32(data->tid);10201021ret = iwl_mvm_send_cmd_pdu(mvm, cmd_id, CMD_SEND_IN_RFKILL,1022sizeof(cmd), &cmd);1023data->sta_mask = new_sta_mask;1024if (ret)1025return ret;1026}10271028return 0;1029}10301031static int iwl_mvm_mld_update_sta_resources(struct iwl_mvm *mvm,1032struct ieee80211_vif *vif,1033struct ieee80211_sta *sta,1034u32 old_sta_mask,1035u32 new_sta_mask)1036{1037int ret;10381039ret = iwl_mvm_mld_update_sta_queues(mvm, sta,1040old_sta_mask,1041new_sta_mask);1042if (ret)1043return ret;10441045ret = iwl_mvm_mld_update_sta_keys(mvm, vif, sta,1046old_sta_mask,1047new_sta_mask);1048if (ret)1049return ret;10501051return iwl_mvm_mld_update_sta_baids(mvm, old_sta_mask, new_sta_mask);1052}10531054int iwl_mvm_mld_update_sta_links(struct iwl_mvm *mvm,1055struct ieee80211_vif *vif,1056struct ieee80211_sta *sta,1057u16 old_links, u16 new_links)1058{1059struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);1060struct iwl_mvm_vif *mvm_vif = iwl_mvm_vif_from_mac80211(vif);1061struct iwl_mvm_link_sta *mvm_sta_link;1062struct iwl_mvm_vif_link_info *mvm_vif_link;1063unsigned long links_to_add = ~old_links & new_links;1064unsigned long links_to_rem = old_links & ~new_links;1065unsigned long old_links_long = old_links;1066u32 current_sta_mask = 0, sta_mask_added = 0, sta_mask_to_rem = 0;1067unsigned long link_sta_added_to_fw = 0, link_sta_allocated = 0;1068unsigned int link_id;1069int ret;10701071lockdep_assert_wiphy(mvm->hw->wiphy);1072lockdep_assert_held(&mvm->mutex);10731074for_each_set_bit(link_id, &old_links_long,1075IEEE80211_MLD_MAX_NUM_LINKS) {1076mvm_sta_link =1077rcu_dereference_protected(mvm_sta->link[link_id],1078lockdep_is_held(&mvm->mutex));10791080if (WARN_ON(!mvm_sta_link)) {1081ret = -EINVAL;1082goto err;1083}10841085current_sta_mask |= BIT(mvm_sta_link->sta_id);1086if (links_to_rem & BIT(link_id))1087sta_mask_to_rem |= BIT(mvm_sta_link->sta_id);1088}10891090if (sta_mask_to_rem) {1091ret = iwl_mvm_mld_update_sta_resources(mvm, vif, sta,1092current_sta_mask,1093current_sta_mask &1094~sta_mask_to_rem);1095if (WARN_ON(ret))1096goto err;10971098current_sta_mask &= ~sta_mask_to_rem;1099}11001101for_each_set_bit(link_id, &links_to_rem, IEEE80211_MLD_MAX_NUM_LINKS) {1102mvm_sta_link =1103rcu_dereference_protected(mvm_sta->link[link_id],1104lockdep_is_held(&mvm->mutex));1105mvm_vif_link = mvm_vif->link[link_id];11061107if (WARN_ON(!mvm_sta_link || !mvm_vif_link)) {1108ret = -EINVAL;1109goto err;1110}11111112ret = iwl_mvm_mld_rm_sta_from_fw(mvm, mvm_sta_link->sta_id);1113if (WARN_ON(ret))1114goto err;11151116if (vif->type == NL80211_IFTYPE_STATION)1117mvm_vif_link->ap_sta_id = IWL_INVALID_STA;11181119iwl_mvm_mld_free_sta_link(mvm, mvm_sta, mvm_sta_link, link_id);1120}11211122for_each_set_bit(link_id, &links_to_add, IEEE80211_MLD_MAX_NUM_LINKS) {1123struct ieee80211_bss_conf *link_conf =1124link_conf_dereference_protected(vif, link_id);1125struct ieee80211_link_sta *link_sta =1126link_sta_dereference_protected(sta, link_id);1127mvm_vif_link = mvm_vif->link[link_id];11281129if (WARN_ON(!mvm_vif_link || !link_conf || !link_sta)) {1130ret = -EINVAL;1131goto err;1132}11331134if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {1135struct iwl_mvm_link_sta *mvm_link_sta =1136rcu_dereference_protected(mvm_sta->link[link_id],1137lockdep_is_held(&mvm->mutex));1138u32 sta_id;11391140if (WARN_ON(!mvm_link_sta)) {1141ret = -EINVAL;1142goto err;1143}11441145sta_id = mvm_link_sta->sta_id;11461147rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);1148rcu_assign_pointer(mvm->fw_id_to_link_sta[sta_id],1149link_sta);1150} else {1151if (WARN_ON(mvm_sta->link[link_id])) {1152ret = -EINVAL;1153goto err;1154}1155ret = iwl_mvm_mld_alloc_sta_link(mvm, vif, sta,1156link_id);1157if (WARN_ON(ret))1158goto err;1159}11601161link_sta->agg.max_rc_amsdu_len = 1;1162ieee80211_sta_recalc_aggregates(sta);11631164mvm_sta_link =1165rcu_dereference_protected(mvm_sta->link[link_id],1166lockdep_is_held(&mvm->mutex));11671168if (WARN_ON(!mvm_sta_link)) {1169ret = -EINVAL;1170goto err;1171}11721173if (vif->type == NL80211_IFTYPE_STATION)1174iwl_mvm_mld_set_ap_sta_id(sta, mvm_vif_link,1175mvm_sta_link);11761177link_sta_allocated |= BIT(link_id);11781179sta_mask_added |= BIT(mvm_sta_link->sta_id);11801181ret = iwl_mvm_mld_cfg_sta(mvm, sta, vif, link_sta, link_conf,1182mvm_sta_link);1183if (WARN_ON(ret))1184goto err;11851186link_sta_added_to_fw |= BIT(link_id);11871188iwl_mvm_rs_add_sta_link(mvm, mvm_sta_link);11891190iwl_mvm_rs_rate_init(mvm, vif, sta, link_conf, link_sta,1191link_conf->chanreq.oper.chan->band);1192}11931194if (sta_mask_added) {1195ret = iwl_mvm_mld_update_sta_resources(mvm, vif, sta,1196current_sta_mask,1197current_sta_mask |1198sta_mask_added);1199if (WARN_ON(ret))1200goto err;1201}12021203return 0;12041205err:1206/* remove all already allocated stations in FW */1207for_each_set_bit(link_id, &link_sta_added_to_fw,1208IEEE80211_MLD_MAX_NUM_LINKS) {1209mvm_sta_link =1210rcu_dereference_protected(mvm_sta->link[link_id],1211lockdep_is_held(&mvm->mutex));12121213iwl_mvm_mld_rm_sta_from_fw(mvm, mvm_sta_link->sta_id);1214}12151216/* remove all already allocated station links in driver */1217for_each_set_bit(link_id, &link_sta_allocated,1218IEEE80211_MLD_MAX_NUM_LINKS) {1219mvm_sta_link =1220rcu_dereference_protected(mvm_sta->link[link_id],1221lockdep_is_held(&mvm->mutex));12221223iwl_mvm_mld_free_sta_link(mvm, mvm_sta, mvm_sta_link, link_id);1224}12251226return ret;1227}122812291230