Path: blob/main/sys/contrib/dev/iwlwifi/mvm/phy-ctxt.c
48285 views
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause1/*2* Copyright (C) 2012-2014, 2018-2024 Intel Corporation3* Copyright (C) 2013-2014 Intel Mobile Communications GmbH4* Copyright (C) 2017 Intel Deutschland GmbH5*/6#include <net/mac80211.h>7#include "fw-api.h"8#include "mvm.h"910/* Maps the driver specific channel width definition to the fw values */11u8 iwl_mvm_get_channel_width(const struct cfg80211_chan_def *chandef)12{13switch (chandef->width) {14case NL80211_CHAN_WIDTH_20_NOHT:15case NL80211_CHAN_WIDTH_20:16return IWL_PHY_CHANNEL_MODE20;17case NL80211_CHAN_WIDTH_40:18return IWL_PHY_CHANNEL_MODE40;19case NL80211_CHAN_WIDTH_80:20return IWL_PHY_CHANNEL_MODE80;21case NL80211_CHAN_WIDTH_160:22return IWL_PHY_CHANNEL_MODE160;23case NL80211_CHAN_WIDTH_320:24return IWL_PHY_CHANNEL_MODE320;25default:26WARN(1, "Invalid channel width=%u", chandef->width);27return IWL_PHY_CHANNEL_MODE20;28}29}3031/*32* Maps the driver specific control channel position (relative to the center33* freq) definitions to the fw values34*/35u8 iwl_mvm_get_ctrl_pos(const struct cfg80211_chan_def *chandef)36{37int offs = chandef->chan->center_freq - chandef->center_freq1;38int abs_offs = abs(offs);39u8 ret;4041if (offs == 0) {42/*43* The FW is expected to check the control channel position only44* when in HT/VHT and the channel width is not 20MHz. Return45* this value as the default one.46*/47return 0;48}4950/* this results in a value 0-7, i.e. fitting into 0b0111 */51ret = (abs_offs - 10) / 20;52/*53* But we need the value to be in 0b1011 because 0b0100 is54* IWL_PHY_CTRL_POS_ABOVE, so shift bit 2 up to land in55* IWL_PHY_CTRL_POS_OFFS_EXT (0b1000)56*/57ret = (ret & IWL_PHY_CTRL_POS_OFFS_MSK) |58((ret & BIT(2)) << 1);59/* and add the above bit */60ret |= (offs > 0) * IWL_PHY_CTRL_POS_ABOVE;6162return ret;63}6465/*66* Construct the generic fields of the PHY context command67*/68static void iwl_mvm_phy_ctxt_cmd_hdr(struct iwl_mvm_phy_ctxt *ctxt,69struct iwl_phy_context_cmd *cmd,70u32 action)71{72cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(ctxt->id,73ctxt->color));74cmd->action = cpu_to_le32(action);75}7677static void iwl_mvm_phy_ctxt_set_rxchain(struct iwl_mvm *mvm,78struct iwl_mvm_phy_ctxt *ctxt,79__le32 *rxchain_info,80u8 chains_static,81u8 chains_dynamic)82{83u8 active_cnt, idle_cnt;8485/* Set rx the chains */86idle_cnt = chains_static;87active_cnt = chains_dynamic;8889/* In scenarios where we only ever use a single-stream rates,90* i.e. legacy 11b/g/a associations, single-stream APs or even91* static SMPS, enable both chains to get diversity, improving92* the case where we're far enough from the AP that attenuation93* between the two antennas is sufficiently different to impact94* performance.95*/96if (active_cnt == 1 && iwl_mvm_rx_diversity_allowed(mvm, ctxt)) {97idle_cnt = 2;98active_cnt = 2;99}100101*rxchain_info = cpu_to_le32(iwl_mvm_get_valid_rx_ant(mvm) <<102PHY_RX_CHAIN_VALID_POS);103*rxchain_info |= cpu_to_le32(idle_cnt << PHY_RX_CHAIN_CNT_POS);104*rxchain_info |= cpu_to_le32(active_cnt <<105PHY_RX_CHAIN_MIMO_CNT_POS);106#ifdef CONFIG_IWLWIFI_DEBUGFS107if (unlikely(mvm->dbgfs_rx_phyinfo))108*rxchain_info = cpu_to_le32(mvm->dbgfs_rx_phyinfo);109#endif110}111112/*113* Add the phy configuration to the PHY context command114*/115static void iwl_mvm_phy_ctxt_cmd_data_v1(struct iwl_mvm *mvm,116struct iwl_mvm_phy_ctxt *ctxt,117struct iwl_phy_context_cmd_v1 *cmd,118const struct cfg80211_chan_def *chandef,119u8 chains_static, u8 chains_dynamic)120{121struct iwl_phy_context_cmd_tail *tail =122iwl_mvm_chan_info_cmd_tail(mvm, &cmd->ci);123124/* Set the channel info data */125iwl_mvm_set_chan_info_chandef(mvm, &cmd->ci, chandef);126127iwl_mvm_phy_ctxt_set_rxchain(mvm, ctxt, &tail->rxchain_info,128chains_static, chains_dynamic);129130tail->txchain_info = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));131}132133/*134* Add the phy configuration to the PHY context command135*/136static void iwl_mvm_phy_ctxt_cmd_data(struct iwl_mvm *mvm,137struct iwl_mvm_phy_ctxt *ctxt,138struct iwl_phy_context_cmd *cmd,139const struct cfg80211_chan_def *chandef,140u8 chains_static, u8 chains_dynamic)141{142cmd->lmac_id = cpu_to_le32(iwl_mvm_get_lmac_id(mvm,143chandef->chan->band));144145/* Set the channel info data */146iwl_mvm_set_chan_info_chandef(mvm, &cmd->ci, chandef);147148/* we only support RLC command version 2 */149if (iwl_fw_lookup_cmd_ver(mvm->fw, WIDE_ID(DATA_PATH_GROUP, RLC_CONFIG_CMD), 0) < 2)150iwl_mvm_phy_ctxt_set_rxchain(mvm, ctxt, &cmd->rxchain_info,151chains_static, chains_dynamic);152}153154int iwl_mvm_phy_send_rlc(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt,155u8 chains_static, u8 chains_dynamic)156{157struct iwl_rlc_config_cmd cmd = {158.phy_id = cpu_to_le32(ctxt->id),159};160161/* From version 3, RLC is offloaded to firmware, so the driver no162* longer needs to send cmd.rlc, note that we are not using any163* other fields in the command - don't send it.164*/165if (iwl_mvm_has_rlc_offload(mvm) || ctxt->rlc_disabled)166return 0;167168if (iwl_fw_lookup_cmd_ver(mvm->fw, WIDE_ID(DATA_PATH_GROUP,169RLC_CONFIG_CMD), 0) < 2)170return 0;171172BUILD_BUG_ON(IWL_RLC_CHAIN_INFO_DRIVER_FORCE !=173PHY_RX_CHAIN_DRIVER_FORCE_MSK);174BUILD_BUG_ON(IWL_RLC_CHAIN_INFO_VALID !=175PHY_RX_CHAIN_VALID_MSK);176BUILD_BUG_ON(IWL_RLC_CHAIN_INFO_FORCE !=177PHY_RX_CHAIN_FORCE_SEL_MSK);178BUILD_BUG_ON(IWL_RLC_CHAIN_INFO_FORCE_MIMO !=179PHY_RX_CHAIN_FORCE_MIMO_SEL_MSK);180BUILD_BUG_ON(IWL_RLC_CHAIN_INFO_COUNT != PHY_RX_CHAIN_CNT_MSK);181BUILD_BUG_ON(IWL_RLC_CHAIN_INFO_MIMO_COUNT !=182PHY_RX_CHAIN_MIMO_CNT_MSK);183184iwl_mvm_phy_ctxt_set_rxchain(mvm, ctxt, &cmd.rlc.rx_chain_info,185chains_static, chains_dynamic);186187IWL_DEBUG_FW(mvm, "Send RLC command: phy=%d, rx_chain_info=0x%x\n",188ctxt->id, cmd.rlc.rx_chain_info);189190return iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(RLC_CONFIG_CMD,191DATA_PATH_GROUP, 2),1920, sizeof(cmd), &cmd);193}194195/*196* Send a command to apply the current phy configuration. The command is send197* only if something in the configuration changed: in case that this is the198* first time that the phy configuration is applied or in case that the phy199* configuration changed from the previous apply.200*/201static int iwl_mvm_phy_ctxt_apply(struct iwl_mvm *mvm,202struct iwl_mvm_phy_ctxt *ctxt,203const struct cfg80211_chan_def *chandef,204const struct cfg80211_chan_def *ap,205u8 chains_static, u8 chains_dynamic,206u32 action)207{208int ret;209int ver = iwl_fw_lookup_cmd_ver(mvm->fw, PHY_CONTEXT_CMD, 1);210211if (ver < 5 || !ap || !ap->chan)212ap = NULL;213214if (ver >= 3 && ver <= 6) {215struct iwl_phy_context_cmd cmd = {};216217/* Set the command header fields */218iwl_mvm_phy_ctxt_cmd_hdr(ctxt, &cmd, action);219220/* Set the command data */221iwl_mvm_phy_ctxt_cmd_data(mvm, ctxt, &cmd, chandef,222chains_static,223chains_dynamic);224225if (ap) {226cmd.sbb_bandwidth = iwl_mvm_get_channel_width(ap);227cmd.sbb_ctrl_channel_loc = iwl_mvm_get_ctrl_pos(ap);228}229230if (ver == 6)231cmd.puncture_mask = cpu_to_le16(chandef->punctured);232233ret = iwl_mvm_send_cmd_pdu(mvm, PHY_CONTEXT_CMD,2340, sizeof(cmd), &cmd);235} else if (ver < 3) {236struct iwl_phy_context_cmd_v1 cmd = {};237u16 len = sizeof(cmd) - iwl_mvm_chan_info_padding(mvm);238239/* Set the command header fields */240iwl_mvm_phy_ctxt_cmd_hdr(ctxt,241(struct iwl_phy_context_cmd *)&cmd,242action);243244/* Set the command data */245iwl_mvm_phy_ctxt_cmd_data_v1(mvm, ctxt, &cmd, chandef,246chains_static,247chains_dynamic);248ret = iwl_mvm_send_cmd_pdu(mvm, PHY_CONTEXT_CMD,2490, len, &cmd);250} else {251IWL_ERR(mvm, "PHY ctxt cmd error ver %d not supported\n", ver);252return -EOPNOTSUPP;253}254255256if (ret) {257IWL_ERR(mvm, "PHY ctxt cmd error. ret=%d\n", ret);258return ret;259}260261if (action != FW_CTXT_ACTION_REMOVE)262return iwl_mvm_phy_send_rlc(mvm, ctxt, chains_static,263chains_dynamic);264265return 0;266}267268/*269* Send a command to add a PHY context based on the current HW configuration.270*/271int iwl_mvm_phy_ctxt_add(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt,272const struct cfg80211_chan_def *chandef,273const struct cfg80211_chan_def *ap,274u8 chains_static, u8 chains_dynamic)275{276int ret;277278WARN_ON(!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&279ctxt->ref);280lockdep_assert_held(&mvm->mutex);281282ctxt->channel = chandef->chan;283ctxt->width = chandef->width;284ctxt->center_freq1 = chandef->center_freq1;285286ret = iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef, ap,287chains_static, chains_dynamic,288FW_CTXT_ACTION_ADD);289290if (ret)291return ret;292293ctxt->ref++;294295return 0;296}297298/*299* Update the number of references to the given PHY context. This is valid only300* in case the PHY context was already created, i.e., its reference count > 0.301*/302void iwl_mvm_phy_ctxt_ref(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt)303{304lockdep_assert_held(&mvm->mutex);305306/* If we were taking the first ref, we should have307* called iwl_mvm_phy_ctxt_add.308*/309WARN_ON(!ctxt->ref);310ctxt->ref++;311}312313/*314* Send a command to modify the PHY context based on the current HW315* configuration. Note that the function does not check that the configuration316* changed.317*/318int iwl_mvm_phy_ctxt_changed(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt,319const struct cfg80211_chan_def *chandef,320const struct cfg80211_chan_def *ap,321u8 chains_static, u8 chains_dynamic)322{323enum iwl_ctxt_action action = FW_CTXT_ACTION_MODIFY;324325lockdep_assert_held(&mvm->mutex);326327if (WARN_ON_ONCE(!ctxt->ref))328return -EINVAL;329330if (iwl_fw_lookup_cmd_ver(mvm->fw, WIDE_ID(DATA_PATH_GROUP,331RLC_CONFIG_CMD), 0) >= 2 &&332ctxt->channel == chandef->chan &&333ctxt->width == chandef->width &&334ctxt->center_freq1 == chandef->center_freq1)335return iwl_mvm_phy_send_rlc(mvm, ctxt, chains_static,336chains_dynamic);337338if (fw_has_capa(&mvm->fw->ucode_capa,339IWL_UCODE_TLV_CAPA_BINDING_CDB_SUPPORT) &&340ctxt->channel->band != chandef->chan->band) {341int ret;342343/* ... remove it here ...*/344ret = iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef, NULL,345chains_static, chains_dynamic,346FW_CTXT_ACTION_REMOVE);347if (ret)348return ret;349350/* ... and proceed to add it again */351action = FW_CTXT_ACTION_ADD;352}353354ctxt->channel = chandef->chan;355ctxt->width = chandef->width;356ctxt->center_freq1 = chandef->center_freq1;357358return iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef, ap,359chains_static, chains_dynamic,360action);361}362363void iwl_mvm_phy_ctxt_unref(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt)364{365struct cfg80211_chan_def chandef;366lockdep_assert_held(&mvm->mutex);367368if (WARN_ON_ONCE(!ctxt))369return;370371ctxt->ref--;372373if (ctxt->ref)374return;375376cfg80211_chandef_create(&chandef, ctxt->channel, NL80211_CHAN_NO_HT);377378iwl_mvm_phy_ctxt_apply(mvm, ctxt, &chandef, NULL, 1, 1,379FW_CTXT_ACTION_REMOVE);380}381382static void iwl_mvm_binding_iterator(void *_data, u8 *mac,383struct ieee80211_vif *vif)384{385unsigned long *data = _data;386struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);387388if (!mvmvif->deflink.phy_ctxt)389return;390391if (vif->type == NL80211_IFTYPE_STATION ||392vif->type == NL80211_IFTYPE_AP)393__set_bit(mvmvif->deflink.phy_ctxt->id, data);394}395396int iwl_mvm_phy_ctx_count(struct iwl_mvm *mvm)397{398unsigned long phy_ctxt_counter = 0;399400ieee80211_iterate_active_interfaces_atomic(mvm->hw,401IEEE80211_IFACE_ITER_NORMAL,402iwl_mvm_binding_iterator,403&phy_ctxt_counter);404405return hweight8(phy_ctxt_counter);406}407408409