Path: blob/main/sys/contrib/dev/iwlwifi/mvm/utils.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#if defined(__FreeBSD__)7#include <linux/math64.h>8#endif9#include <net/mac80211.h>1011#include "iwl-debug.h"12#include "iwl-io.h"13#include "iwl-prph.h"14#include "iwl-csr.h"15#include "mvm.h"16#include "fw/api/rs.h"17#include "fw/img.h"1819/*20* Will return 0 even if the cmd failed when RFKILL is asserted unless21* CMD_WANT_SKB is set in cmd->flags.22*/23int iwl_mvm_send_cmd(struct iwl_mvm *mvm, struct iwl_host_cmd *cmd)24{25int ret;2627#if defined(CONFIG_IWLWIFI_DEBUGFS) && defined(CONFIG_PM_SLEEP)28if (WARN_ON(mvm->d3_test_active))29return -EIO;30#endif3132/*33* Synchronous commands from this op-mode must hold34* the mutex, this ensures we don't try to send two35* (or more) synchronous commands at a time.36*/37if (!(cmd->flags & CMD_ASYNC))38lockdep_assert_held(&mvm->mutex);3940ret = iwl_trans_send_cmd(mvm->trans, cmd);4142/*43* If the caller wants the SKB, then don't hide any problems, the44* caller might access the response buffer which will be NULL if45* the command failed.46*/47if (cmd->flags & CMD_WANT_SKB)48return ret;4950/*51* Silently ignore failures if RFKILL is asserted or52* we are in suspend\resume process53*/54if (!ret || ret == -ERFKILL || ret == -EHOSTDOWN)55return 0;56return ret;57}5859int iwl_mvm_send_cmd_pdu(struct iwl_mvm *mvm, u32 id,60u32 flags, u16 len, const void *data)61{62struct iwl_host_cmd cmd = {63.id = id,64.len = { len, },65.data = { data, },66.flags = flags,67};6869return iwl_mvm_send_cmd(mvm, &cmd);70}7172/*73* We assume that the caller set the status to the success value74*/75int iwl_mvm_send_cmd_status(struct iwl_mvm *mvm, struct iwl_host_cmd *cmd,76u32 *status)77{78struct iwl_rx_packet *pkt;79struct iwl_cmd_response *resp;80int ret, resp_len;8182lockdep_assert_held(&mvm->mutex);8384#if defined(CONFIG_IWLWIFI_DEBUGFS) && defined(CONFIG_PM_SLEEP)85if (WARN_ON(mvm->d3_test_active))86return -EIO;87#endif8889/*90* Only synchronous commands can wait for status,91* we use WANT_SKB so the caller can't.92*/93if (WARN_ONCE(cmd->flags & (CMD_ASYNC | CMD_WANT_SKB),94"cmd flags %x", cmd->flags))95return -EINVAL;9697cmd->flags |= CMD_WANT_SKB;9899ret = iwl_trans_send_cmd(mvm->trans, cmd);100if (ret == -ERFKILL) {101/*102* The command failed because of RFKILL, don't update103* the status, leave it as success and return 0.104*/105return 0;106} else if (ret) {107return ret;108}109110pkt = cmd->resp_pkt;111112resp_len = iwl_rx_packet_payload_len(pkt);113if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {114ret = -EIO;115goto out_free_resp;116}117118resp = (void *)pkt->data;119*status = le32_to_cpu(resp->status);120out_free_resp:121iwl_free_resp(cmd);122return ret;123}124125/*126* We assume that the caller set the status to the sucess value127*/128int iwl_mvm_send_cmd_pdu_status(struct iwl_mvm *mvm, u32 id, u16 len,129const void *data, u32 *status)130{131struct iwl_host_cmd cmd = {132.id = id,133.len = { len, },134.data = { data, },135};136137return iwl_mvm_send_cmd_status(mvm, &cmd, status);138}139140int iwl_mvm_legacy_hw_idx_to_mac80211_idx(u32 rate_n_flags,141enum nl80211_band band)142{143int format = rate_n_flags & RATE_MCS_MOD_TYPE_MSK;144int rate = rate_n_flags & RATE_LEGACY_RATE_MSK;145bool is_LB = band == NL80211_BAND_2GHZ;146147if (format == RATE_MCS_MOD_TYPE_LEGACY_OFDM)148return is_LB ? rate + IWL_FIRST_OFDM_RATE :149rate;150151/* CCK is not allowed in HB */152return is_LB ? rate : -1;153}154155int iwl_mvm_legacy_rate_to_mac80211_idx(u32 rate_n_flags,156enum nl80211_band band)157{158int rate = rate_n_flags & RATE_LEGACY_RATE_MSK_V1;159int idx;160int band_offset = 0;161162/* Legacy rate format, search for match in table */163if (band != NL80211_BAND_2GHZ)164band_offset = IWL_FIRST_OFDM_RATE;165for (idx = band_offset; idx < IWL_RATE_COUNT_LEGACY; idx++)166if (iwl_fw_rate_idx_to_plcp(idx) == rate)167return idx - band_offset;168169return -1;170}171172u8 iwl_mvm_mac80211_idx_to_hwrate(const struct iwl_fw *fw, int rate_idx)173{174return (rate_idx >= IWL_FIRST_OFDM_RATE ?175rate_idx - IWL_FIRST_OFDM_RATE :176rate_idx);177}178179u8 iwl_mvm_mac80211_ac_to_ucode_ac(enum ieee80211_ac_numbers ac)180{181static const u8 mac80211_ac_to_ucode_ac[] = {182AC_VO,183AC_VI,184AC_BE,185AC_BK186};187188return mac80211_ac_to_ucode_ac[ac];189}190191void iwl_mvm_rx_fw_error(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)192{193struct iwl_rx_packet *pkt = rxb_addr(rxb);194struct iwl_error_resp *err_resp = (void *)pkt->data;195196IWL_ERR(mvm, "FW Error notification: type 0x%08X cmd_id 0x%02X\n",197le32_to_cpu(err_resp->error_type), err_resp->cmd_id);198IWL_ERR(mvm, "FW Error notification: seq 0x%04X service 0x%08X\n",199le16_to_cpu(err_resp->bad_cmd_seq_num),200le32_to_cpu(err_resp->error_service));201IWL_ERR(mvm, "FW Error notification: timestamp 0x%016llX\n",202le64_to_cpu(err_resp->timestamp));203}204205/*206* Returns the first antenna as ANT_[ABC], as defined in iwl-config.h.207* The parameter should also be a combination of ANT_[ABC].208*/209u8 first_antenna(u8 mask)210{211BUILD_BUG_ON(ANT_A != BIT(0)); /* using ffs is wrong if not */212if (WARN_ON_ONCE(!mask)) /* ffs will return 0 if mask is zeroed */213return BIT(0);214return BIT(ffs(mask) - 1);215}216217#define MAX_ANT_NUM 2218/*219* Toggles between TX antennas to send the probe request on.220* Receives the bitmask of valid TX antennas and the *index* used221* for the last TX, and returns the next valid *index* to use.222* In order to set it in the tx_cmd, must do BIT(idx).223*/224u8 iwl_mvm_next_antenna(struct iwl_mvm *mvm, u8 valid, u8 last_idx)225{226u8 ind = last_idx;227int i;228229for (i = 0; i < MAX_ANT_NUM; i++) {230ind = (ind + 1) % MAX_ANT_NUM;231if (valid & BIT(ind))232return ind;233}234235WARN_ONCE(1, "Failed to toggle between antennas 0x%x", valid);236return last_idx;237}238239/**240* iwl_mvm_send_lq_cmd() - Send link quality command241* @mvm: Driver data.242* @lq: Link quality command to send.243*244* The link quality command is sent as the last step of station creation.245* This is the special case in which init is set and we call a callback in246* this case to clear the state indicating that station creation is in247* progress.248*249* Returns: an error code indicating success or failure250*/251int iwl_mvm_send_lq_cmd(struct iwl_mvm *mvm, struct iwl_lq_cmd *lq)252{253struct iwl_host_cmd cmd = {254.id = LQ_CMD,255.len = { sizeof(struct iwl_lq_cmd), },256.flags = CMD_ASYNC,257.data = { lq, },258};259260if (WARN_ON(lq->sta_id == IWL_INVALID_STA ||261iwl_mvm_has_tlc_offload(mvm)))262return -EINVAL;263264return iwl_mvm_send_cmd(mvm, &cmd);265}266267/**268* iwl_mvm_update_smps - Get a request to change the SMPS mode269* @mvm: Driver data.270* @vif: Pointer to the ieee80211_vif structure271* @req_type: The part of the driver who call for a change.272* @smps_request: The request to change the SMPS mode.273* @link_id: for MLO link_id, otherwise 0 (deflink)274*275* Get a requst to change the SMPS mode,276* and change it according to all other requests in the driver.277*/278void iwl_mvm_update_smps(struct iwl_mvm *mvm, struct ieee80211_vif *vif,279enum iwl_mvm_smps_type_request req_type,280enum ieee80211_smps_mode smps_request,281unsigned int link_id)282{283struct iwl_mvm_vif *mvmvif;284enum ieee80211_smps_mode smps_mode = IEEE80211_SMPS_AUTOMATIC;285int i;286287lockdep_assert_held(&mvm->mutex);288289/* SMPS is irrelevant for NICs that don't have at least 2 RX antenna */290if (num_of_ant(iwl_mvm_get_valid_rx_ant(mvm)) == 1)291return;292293if (vif->type != NL80211_IFTYPE_STATION)294return;295296/* SMPS is handled by firmware */297if (iwl_mvm_has_rlc_offload(mvm))298return;299300mvmvif = iwl_mvm_vif_from_mac80211(vif);301302if (WARN_ON_ONCE(!mvmvif->link[link_id]))303return;304305mvmvif->link[link_id]->smps_requests[req_type] = smps_request;306for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++) {307if (mvmvif->link[link_id]->smps_requests[i] ==308IEEE80211_SMPS_STATIC) {309smps_mode = IEEE80211_SMPS_STATIC;310break;311}312if (mvmvif->link[link_id]->smps_requests[i] ==313IEEE80211_SMPS_DYNAMIC)314smps_mode = IEEE80211_SMPS_DYNAMIC;315}316317/* SMPS is disabled in eSR */318if (mvmvif->esr_active)319smps_mode = IEEE80211_SMPS_OFF;320321ieee80211_request_smps(vif, link_id, smps_mode);322}323324void iwl_mvm_update_smps_on_active_links(struct iwl_mvm *mvm,325struct ieee80211_vif *vif,326enum iwl_mvm_smps_type_request req_type,327enum ieee80211_smps_mode smps_request)328{329struct ieee80211_bss_conf *link_conf;330unsigned int link_id;331332rcu_read_lock();333for_each_vif_active_link(vif, link_conf, link_id)334iwl_mvm_update_smps(mvm, vif, req_type, smps_request,335link_id);336rcu_read_unlock();337}338339static bool iwl_wait_stats_complete(struct iwl_notif_wait_data *notif_wait,340struct iwl_rx_packet *pkt, void *data)341{342WARN_ON(pkt->hdr.cmd != STATISTICS_NOTIFICATION);343344return true;345}346347#define PERIODIC_STAT_RATE 5348349int iwl_mvm_request_periodic_system_statistics(struct iwl_mvm *mvm, bool enable)350{351u32 flags = enable ? 0 : IWL_STATS_CFG_FLG_DISABLE_NTFY_MSK;352u32 type = enable ? (IWL_STATS_NTFY_TYPE_ID_OPER |353IWL_STATS_NTFY_TYPE_ID_OPER_PART1) : 0;354struct iwl_system_statistics_cmd system_cmd = {355.cfg_mask = cpu_to_le32(flags),356.config_time_sec = cpu_to_le32(enable ?357PERIODIC_STAT_RATE : 0),358.type_id_mask = cpu_to_le32(type),359};360361return iwl_mvm_send_cmd_pdu(mvm,362WIDE_ID(SYSTEM_GROUP,363SYSTEM_STATISTICS_CMD),3640, sizeof(system_cmd), &system_cmd);365}366367static int iwl_mvm_request_system_statistics(struct iwl_mvm *mvm, bool clear,368u8 cmd_ver)369{370struct iwl_system_statistics_cmd system_cmd = {371.cfg_mask = clear ?372cpu_to_le32(IWL_STATS_CFG_FLG_ON_DEMAND_NTFY_MSK) :373cpu_to_le32(IWL_STATS_CFG_FLG_RESET_MSK |374IWL_STATS_CFG_FLG_ON_DEMAND_NTFY_MSK),375.type_id_mask = cpu_to_le32(IWL_STATS_NTFY_TYPE_ID_OPER |376IWL_STATS_NTFY_TYPE_ID_OPER_PART1),377};378struct iwl_host_cmd cmd = {379.id = WIDE_ID(SYSTEM_GROUP, SYSTEM_STATISTICS_CMD),380.len[0] = sizeof(system_cmd),381.data[0] = &system_cmd,382};383struct iwl_notification_wait stats_wait;384static const u16 stats_complete[] = {385WIDE_ID(SYSTEM_GROUP, SYSTEM_STATISTICS_END_NOTIF),386};387int ret;388389if (cmd_ver != 1) {390IWL_FW_CHECK_FAILED(mvm,391"Invalid system statistics command version:%d\n",392cmd_ver);393return -EOPNOTSUPP;394}395396iwl_init_notification_wait(&mvm->notif_wait, &stats_wait,397stats_complete, ARRAY_SIZE(stats_complete),398NULL, NULL);399400mvm->statistics_clear = clear;401ret = iwl_mvm_send_cmd(mvm, &cmd);402if (ret) {403iwl_remove_notification(&mvm->notif_wait, &stats_wait);404return ret;405}406407/* 500ms for OPERATIONAL, PART1 and END notification should be enough408* for FW to collect data from all LMACs and send409* STATISTICS_NOTIFICATION to host410*/411ret = iwl_wait_notification(&mvm->notif_wait, &stats_wait, HZ / 2);412if (ret)413return ret;414415if (clear)416iwl_mvm_accu_radio_stats(mvm);417418return ret;419}420421int iwl_mvm_request_statistics(struct iwl_mvm *mvm, bool clear)422{423struct iwl_statistics_cmd scmd = {424.flags = clear ? cpu_to_le32(IWL_STATISTICS_FLG_CLEAR) : 0,425};426427struct iwl_host_cmd cmd = {428.id = STATISTICS_CMD,429.len[0] = sizeof(scmd),430.data[0] = &scmd,431};432u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,433WIDE_ID(SYSTEM_GROUP,434SYSTEM_STATISTICS_CMD),435IWL_FW_CMD_VER_UNKNOWN);436int ret;437438/*439* Don't request statistics during restart, they'll not have any useful440* information right after restart, nor is clearing needed441*/442if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))443return 0;444445if (cmd_ver != IWL_FW_CMD_VER_UNKNOWN)446return iwl_mvm_request_system_statistics(mvm, clear, cmd_ver);447448/* From version 15 - STATISTICS_NOTIFICATION, the reply for449* STATISTICS_CMD is empty, and the response is with450* STATISTICS_NOTIFICATION notification451*/452if (iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,453STATISTICS_NOTIFICATION, 0) < 15) {454cmd.flags = CMD_WANT_SKB;455456ret = iwl_mvm_send_cmd(mvm, &cmd);457if (ret)458return ret;459460iwl_mvm_handle_rx_statistics(mvm, cmd.resp_pkt);461iwl_free_resp(&cmd);462} else {463struct iwl_notification_wait stats_wait;464static const u16 stats_complete[] = {465STATISTICS_NOTIFICATION,466};467468iwl_init_notification_wait(&mvm->notif_wait, &stats_wait,469stats_complete, ARRAY_SIZE(stats_complete),470iwl_wait_stats_complete, NULL);471472ret = iwl_mvm_send_cmd(mvm, &cmd);473if (ret) {474iwl_remove_notification(&mvm->notif_wait, &stats_wait);475return ret;476}477478/* 200ms should be enough for FW to collect data from all479* LMACs and send STATISTICS_NOTIFICATION to host480*/481ret = iwl_wait_notification(&mvm->notif_wait, &stats_wait, HZ / 5);482if (ret)483return ret;484}485486if (clear)487iwl_mvm_accu_radio_stats(mvm);488489return 0;490}491492void iwl_mvm_accu_radio_stats(struct iwl_mvm *mvm)493{494mvm->accu_radio_stats.rx_time += mvm->radio_stats.rx_time;495mvm->accu_radio_stats.tx_time += mvm->radio_stats.tx_time;496mvm->accu_radio_stats.on_time_rf += mvm->radio_stats.on_time_rf;497mvm->accu_radio_stats.on_time_scan += mvm->radio_stats.on_time_scan;498}499500struct iwl_mvm_diversity_iter_data {501struct iwl_mvm_phy_ctxt *ctxt;502bool result;503};504505static void iwl_mvm_diversity_iter(void *_data, u8 *mac,506struct ieee80211_vif *vif)507{508struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);509struct iwl_mvm_diversity_iter_data *data = _data;510int i, link_id;511512for_each_mvm_vif_valid_link(mvmvif, link_id) {513struct iwl_mvm_vif_link_info *link_info = mvmvif->link[link_id];514515if (link_info->phy_ctxt != data->ctxt)516continue;517518for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++) {519if (link_info->smps_requests[i] == IEEE80211_SMPS_STATIC ||520link_info->smps_requests[i] == IEEE80211_SMPS_DYNAMIC) {521data->result = false;522break;523}524}525}526}527528bool iwl_mvm_rx_diversity_allowed(struct iwl_mvm *mvm,529struct iwl_mvm_phy_ctxt *ctxt)530{531struct iwl_mvm_diversity_iter_data data = {532.ctxt = ctxt,533.result = true,534};535536lockdep_assert_held(&mvm->mutex);537538if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)539return false;540541if (num_of_ant(iwl_mvm_get_valid_rx_ant(mvm)) == 1)542return false;543544if (mvm->cfg->rx_with_siso_diversity)545return false;546547ieee80211_iterate_active_interfaces_atomic(548mvm->hw, IEEE80211_IFACE_ITER_NORMAL,549iwl_mvm_diversity_iter, &data);550551return data.result;552}553554void iwl_mvm_send_low_latency_cmd(struct iwl_mvm *mvm,555bool low_latency, u16 mac_id)556{557struct iwl_mac_low_latency_cmd cmd = {558.mac_id = cpu_to_le32(mac_id)559};560561if (!fw_has_capa(&mvm->fw->ucode_capa,562IWL_UCODE_TLV_CAPA_DYNAMIC_QUOTA))563return;564565if (low_latency) {566/* currently we don't care about the direction */567cmd.low_latency_rx = 1;568cmd.low_latency_tx = 1;569}570571if (iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(MAC_CONF_GROUP, LOW_LATENCY_CMD),5720, sizeof(cmd), &cmd))573IWL_ERR(mvm, "Failed to send low latency command\n");574}575576int iwl_mvm_update_low_latency(struct iwl_mvm *mvm, struct ieee80211_vif *vif,577bool low_latency,578enum iwl_mvm_low_latency_cause cause)579{580struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);581int res;582bool prev;583584lockdep_assert_held(&mvm->mutex);585586prev = iwl_mvm_vif_low_latency(mvmvif);587iwl_mvm_vif_set_low_latency(mvmvif, low_latency, cause);588589low_latency = iwl_mvm_vif_low_latency(mvmvif);590591if (low_latency == prev)592return 0;593594iwl_mvm_send_low_latency_cmd(mvm, low_latency, mvmvif->id);595596res = iwl_mvm_update_quotas(mvm, false, NULL);597if (res)598return res;599600iwl_mvm_bt_coex_vif_change(mvm);601602return iwl_mvm_power_update_mac(mvm);603}604605struct iwl_mvm_low_latency_iter {606bool result;607bool result_per_band[NUM_NL80211_BANDS];608};609610static void iwl_mvm_ll_iter(void *_data, u8 *mac, struct ieee80211_vif *vif)611{612struct iwl_mvm_low_latency_iter *result = _data;613struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);614enum nl80211_band band;615616if (iwl_mvm_vif_low_latency(mvmvif)) {617result->result = true;618619if (!mvmvif->deflink.phy_ctxt)620return;621622band = mvmvif->deflink.phy_ctxt->channel->band;623result->result_per_band[band] = true;624}625}626627bool iwl_mvm_low_latency(struct iwl_mvm *mvm)628{629struct iwl_mvm_low_latency_iter data = {};630631ieee80211_iterate_active_interfaces_atomic(632mvm->hw, IEEE80211_IFACE_ITER_NORMAL,633iwl_mvm_ll_iter, &data);634635return data.result;636}637638bool iwl_mvm_low_latency_band(struct iwl_mvm *mvm, enum nl80211_band band)639{640struct iwl_mvm_low_latency_iter data = {};641642ieee80211_iterate_active_interfaces_atomic(643mvm->hw, IEEE80211_IFACE_ITER_NORMAL,644iwl_mvm_ll_iter, &data);645646return data.result_per_band[band];647}648649struct iwl_bss_iter_data {650struct ieee80211_vif *vif;651bool error;652};653654static void iwl_mvm_bss_iface_iterator(void *_data, u8 *mac,655struct ieee80211_vif *vif)656{657struct iwl_bss_iter_data *data = _data;658659if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)660return;661662if (data->vif) {663data->error = true;664return;665}666667data->vif = vif;668}669670struct ieee80211_vif *iwl_mvm_get_bss_vif(struct iwl_mvm *mvm)671{672struct iwl_bss_iter_data bss_iter_data = {};673674ieee80211_iterate_active_interfaces_atomic(675mvm->hw, IEEE80211_IFACE_ITER_NORMAL,676iwl_mvm_bss_iface_iterator, &bss_iter_data);677678if (bss_iter_data.error)679return ERR_PTR(-EINVAL);680681return bss_iter_data.vif;682}683684struct iwl_bss_find_iter_data {685struct ieee80211_vif *vif;686u32 macid;687};688689static void iwl_mvm_bss_find_iface_iterator(void *_data, u8 *mac,690struct ieee80211_vif *vif)691{692struct iwl_bss_find_iter_data *data = _data;693struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);694695if (mvmvif->id == data->macid)696data->vif = vif;697}698699struct ieee80211_vif *iwl_mvm_get_vif_by_macid(struct iwl_mvm *mvm, u32 macid)700{701struct iwl_bss_find_iter_data data = {702.macid = macid,703};704705lockdep_assert_held(&mvm->mutex);706707ieee80211_iterate_active_interfaces_atomic(708mvm->hw, IEEE80211_IFACE_ITER_NORMAL,709iwl_mvm_bss_find_iface_iterator, &data);710711return data.vif;712}713714struct iwl_sta_iter_data {715bool assoc;716};717718static void iwl_mvm_sta_iface_iterator(void *_data, u8 *mac,719struct ieee80211_vif *vif)720{721struct iwl_sta_iter_data *data = _data;722723if (vif->type != NL80211_IFTYPE_STATION)724return;725726if (vif->cfg.assoc)727data->assoc = true;728}729730bool iwl_mvm_is_vif_assoc(struct iwl_mvm *mvm)731{732struct iwl_sta_iter_data data = {733.assoc = false,734};735736ieee80211_iterate_active_interfaces_atomic(mvm->hw,737IEEE80211_IFACE_ITER_NORMAL,738iwl_mvm_sta_iface_iterator,739&data);740return data.assoc;741}742743unsigned int iwl_mvm_get_wd_timeout(struct iwl_mvm *mvm,744struct ieee80211_vif *vif)745{746unsigned int default_timeout =747mvm->trans->mac_cfg->base->wd_timeout;748749/*750* We can't know when the station is asleep or awake, so we751* must disable the queue hang detection.752*/753if (fw_has_capa(&mvm->fw->ucode_capa,754IWL_UCODE_TLV_CAPA_STA_PM_NOTIF) &&755vif->type == NL80211_IFTYPE_AP)756return IWL_WATCHDOG_DISABLED;757return default_timeout;758}759760void iwl_mvm_connection_loss(struct iwl_mvm *mvm, struct ieee80211_vif *vif,761const char *errmsg)762{763struct iwl_fw_dbg_trigger_tlv *trig;764struct iwl_fw_dbg_trigger_mlme *trig_mlme;765766trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),767FW_DBG_TRIGGER_MLME);768if (!trig)769goto out;770771trig_mlme = (void *)trig->data;772773if (trig_mlme->stop_connection_loss &&774--trig_mlme->stop_connection_loss)775goto out;776777iwl_fw_dbg_collect_trig(&mvm->fwrt, trig, "%s", errmsg);778779out:780ieee80211_connection_loss(vif);781}782783void iwl_mvm_event_frame_timeout_callback(struct iwl_mvm *mvm,784struct ieee80211_vif *vif,785const struct ieee80211_sta *sta,786u16 tid)787{788struct iwl_fw_dbg_trigger_tlv *trig;789struct iwl_fw_dbg_trigger_ba *ba_trig;790791trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),792FW_DBG_TRIGGER_BA);793if (!trig)794return;795796ba_trig = (void *)trig->data;797798if (!(le16_to_cpu(ba_trig->frame_timeout) & BIT(tid)))799return;800801iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,802"Frame from %pM timed out, tid %d",803sta->addr, tid);804}805806u8 iwl_mvm_tcm_load_percentage(u32 airtime, u32 elapsed)807{808if (!elapsed)809return 0;810811return (100 * airtime / elapsed) / USEC_PER_MSEC;812}813814static enum iwl_mvm_traffic_load815iwl_mvm_tcm_load(struct iwl_mvm *mvm, u32 airtime, unsigned long elapsed)816{817u8 load = iwl_mvm_tcm_load_percentage(airtime, elapsed);818819if (load > IWL_MVM_TCM_LOAD_HIGH_THRESH)820return IWL_MVM_TRAFFIC_HIGH;821if (load > IWL_MVM_TCM_LOAD_MEDIUM_THRESH)822return IWL_MVM_TRAFFIC_MEDIUM;823824return IWL_MVM_TRAFFIC_LOW;825}826827static void iwl_mvm_tcm_iter(void *_data, u8 *mac, struct ieee80211_vif *vif)828{829struct iwl_mvm *mvm = _data;830struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);831bool low_latency, prev = mvmvif->low_latency & LOW_LATENCY_TRAFFIC;832833if (mvmvif->id >= NUM_MAC_INDEX_DRIVER)834return;835836low_latency = mvm->tcm.result.low_latency[mvmvif->id];837838if (!mvm->tcm.result.change[mvmvif->id] &&839prev == low_latency) {840iwl_mvm_update_quotas(mvm, false, NULL);841return;842}843844if (prev != low_latency) {845/* this sends traffic load and updates quota as well */846iwl_mvm_update_low_latency(mvm, vif, low_latency,847LOW_LATENCY_TRAFFIC);848} else {849iwl_mvm_update_quotas(mvm, false, NULL);850}851}852853static void iwl_mvm_tcm_results(struct iwl_mvm *mvm)854{855guard(mvm)(mvm);856857ieee80211_iterate_active_interfaces(858mvm->hw, IEEE80211_IFACE_ITER_NORMAL,859iwl_mvm_tcm_iter, mvm);860861if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))862iwl_mvm_config_scan(mvm);863}864865static void iwl_mvm_tcm_uapsd_nonagg_detected_wk(struct work_struct *wk)866{867struct iwl_mvm *mvm;868struct iwl_mvm_vif *mvmvif;869struct ieee80211_vif *vif;870871mvmvif = container_of(wk, struct iwl_mvm_vif,872uapsd_nonagg_detected_wk.work);873vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);874mvm = mvmvif->mvm;875876if (mvm->tcm.data[mvmvif->id].opened_rx_ba_sessions)877return;878879/* remember that this AP is broken */880memcpy(mvm->uapsd_noagg_bssids[mvm->uapsd_noagg_bssid_write_idx].addr,881vif->bss_conf.bssid, ETH_ALEN);882mvm->uapsd_noagg_bssid_write_idx++;883if (mvm->uapsd_noagg_bssid_write_idx >= IWL_MVM_UAPSD_NOAGG_LIST_LEN)884mvm->uapsd_noagg_bssid_write_idx = 0;885886iwl_mvm_connection_loss(mvm, vif,887"AP isn't using AMPDU with uAPSD enabled");888}889890static void iwl_mvm_uapsd_agg_disconnect(struct iwl_mvm *mvm,891struct ieee80211_vif *vif)892{893struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);894895if (vif->type != NL80211_IFTYPE_STATION)896return;897898if (!vif->cfg.assoc)899return;900901if (!mvmvif->deflink.queue_params[IEEE80211_AC_VO].uapsd &&902!mvmvif->deflink.queue_params[IEEE80211_AC_VI].uapsd &&903!mvmvif->deflink.queue_params[IEEE80211_AC_BE].uapsd &&904!mvmvif->deflink.queue_params[IEEE80211_AC_BK].uapsd)905return;906907if (mvm->tcm.data[mvmvif->id].uapsd_nonagg_detect.detected)908return;909910mvm->tcm.data[mvmvif->id].uapsd_nonagg_detect.detected = true;911IWL_INFO(mvm,912"detected AP should do aggregation but isn't, likely due to U-APSD\n");913schedule_delayed_work(&mvmvif->uapsd_nonagg_detected_wk,91415 * HZ);915}916917static void iwl_mvm_check_uapsd_agg_expected_tpt(struct iwl_mvm *mvm,918unsigned int elapsed,919int mac)920{921u64 bytes = mvm->tcm.data[mac].uapsd_nonagg_detect.rx_bytes;922u64 tpt;923unsigned long rate;924struct ieee80211_vif *vif;925926rate = ewma_rate_read(&mvm->tcm.data[mac].uapsd_nonagg_detect.rate);927928if (!rate || mvm->tcm.data[mac].opened_rx_ba_sessions ||929mvm->tcm.data[mac].uapsd_nonagg_detect.detected)930return;931932if (iwl_mvm_has_new_rx_api(mvm)) {933tpt = 8 * bytes; /* kbps */934do_div(tpt, elapsed);935rate *= 1000; /* kbps */936if (tpt < 22 * rate / 100)937return;938} else {939/*940* the rate here is actually the threshold, in 100Kbps units,941* so do the needed conversion from bytes to 100Kbps:942* 100kb = bits / (100 * 1000),943* 100kbps = 100kb / (msecs / 1000) ==944* (bits / (100 * 1000)) / (msecs / 1000) ==945* bits / (100 * msecs)946*/947tpt = (8 * bytes);948do_div(tpt, elapsed * 100);949if (tpt < rate)950return;951}952953rcu_read_lock();954vif = rcu_dereference(mvm->vif_id_to_mac[mac]);955if (vif)956iwl_mvm_uapsd_agg_disconnect(mvm, vif);957rcu_read_unlock();958}959960static void iwl_mvm_tcm_iterator(void *_data, u8 *mac,961struct ieee80211_vif *vif)962{963struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);964u32 *band = _data;965966if (!mvmvif->deflink.phy_ctxt)967return;968969band[mvmvif->id] = mvmvif->deflink.phy_ctxt->channel->band;970}971972static unsigned long iwl_mvm_calc_tcm_stats(struct iwl_mvm *mvm,973unsigned long ts,974bool handle_uapsd)975{976unsigned int elapsed = jiffies_to_msecs(ts - mvm->tcm.ts);977unsigned int uapsd_elapsed =978jiffies_to_msecs(ts - mvm->tcm.uapsd_nonagg_ts);979u32 total_airtime = 0;980u32 band_airtime[NUM_NL80211_BANDS] = {0};981u32 band[NUM_MAC_INDEX_DRIVER] = {0};982int ac, mac, i;983bool low_latency = false;984enum iwl_mvm_traffic_load load, band_load;985bool handle_ll = time_after(ts, mvm->tcm.ll_ts + MVM_LL_PERIOD);986987if (handle_ll)988mvm->tcm.ll_ts = ts;989if (handle_uapsd)990mvm->tcm.uapsd_nonagg_ts = ts;991992mvm->tcm.result.elapsed = elapsed;993994ieee80211_iterate_active_interfaces_atomic(mvm->hw,995IEEE80211_IFACE_ITER_NORMAL,996iwl_mvm_tcm_iterator,997&band);998999for (mac = 0; mac < NUM_MAC_INDEX_DRIVER; mac++) {1000struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[mac];1001u32 vo_vi_pkts = 0;1002u32 airtime = mdata->rx.airtime + mdata->tx.airtime;10031004total_airtime += airtime;1005band_airtime[band[mac]] += airtime;10061007load = iwl_mvm_tcm_load(mvm, airtime, elapsed);1008mvm->tcm.result.change[mac] = load != mvm->tcm.result.load[mac];1009mvm->tcm.result.load[mac] = load;1010mvm->tcm.result.airtime[mac] = airtime;10111012for (ac = IEEE80211_AC_VO; ac <= IEEE80211_AC_VI; ac++)1013vo_vi_pkts += mdata->rx.pkts[ac] +1014mdata->tx.pkts[ac];10151016/* enable immediately with enough packets but defer disabling */1017if (vo_vi_pkts > IWL_MVM_TCM_LOWLAT_ENABLE_THRESH)1018mvm->tcm.result.low_latency[mac] = true;1019else if (handle_ll)1020mvm->tcm.result.low_latency[mac] = false;10211022if (handle_ll) {1023/* clear old data */1024memset(&mdata->rx.pkts, 0, sizeof(mdata->rx.pkts));1025memset(&mdata->tx.pkts, 0, sizeof(mdata->tx.pkts));1026}1027low_latency |= mvm->tcm.result.low_latency[mac];10281029if (!mvm->tcm.result.low_latency[mac] && handle_uapsd)1030iwl_mvm_check_uapsd_agg_expected_tpt(mvm, uapsd_elapsed,1031mac);1032/* clear old data */1033if (handle_uapsd)1034mdata->uapsd_nonagg_detect.rx_bytes = 0;1035memset(&mdata->rx.airtime, 0, sizeof(mdata->rx.airtime));1036memset(&mdata->tx.airtime, 0, sizeof(mdata->tx.airtime));1037}10381039load = iwl_mvm_tcm_load(mvm, total_airtime, elapsed);1040mvm->tcm.result.global_load = load;10411042for (i = 0; i < NUM_NL80211_BANDS; i++) {1043band_load = iwl_mvm_tcm_load(mvm, band_airtime[i], elapsed);1044mvm->tcm.result.band_load[i] = band_load;1045}10461047/*1048* If the current load isn't low we need to force re-evaluation1049* in the TCM period, so that we can return to low load if there1050* was no traffic at all (and thus iwl_mvm_recalc_tcm didn't get1051* triggered by traffic).1052*/1053if (load != IWL_MVM_TRAFFIC_LOW)1054return MVM_TCM_PERIOD;1055/*1056* If low-latency is active we need to force re-evaluation after1057* (the longer) MVM_LL_PERIOD, so that we can disable low-latency1058* when there's no traffic at all.1059*/1060if (low_latency)1061return MVM_LL_PERIOD;1062/*1063* Otherwise, we don't need to run the work struct because we're1064* in the default "idle" state - traffic indication is low (which1065* also covers the "no traffic" case) and low-latency is disabled1066* so there's no state that may need to be disabled when there's1067* no traffic at all.1068*1069* Note that this has no impact on the regular scheduling of the1070* updates triggered by traffic - those happen whenever one of the1071* two timeouts expire (if there's traffic at all.)1072*/1073return 0;1074}10751076void iwl_mvm_recalc_tcm(struct iwl_mvm *mvm)1077{1078unsigned long ts = jiffies;1079bool handle_uapsd =1080time_after(ts, mvm->tcm.uapsd_nonagg_ts +1081msecs_to_jiffies(IWL_MVM_UAPSD_NONAGG_PERIOD));10821083spin_lock(&mvm->tcm.lock);1084if (mvm->tcm.paused || !time_after(ts, mvm->tcm.ts + MVM_TCM_PERIOD)) {1085spin_unlock(&mvm->tcm.lock);1086return;1087}1088spin_unlock(&mvm->tcm.lock);10891090if (handle_uapsd && iwl_mvm_has_new_rx_api(mvm)) {1091guard(mvm)(mvm);1092if (iwl_mvm_request_statistics(mvm, true))1093handle_uapsd = false;1094}10951096spin_lock(&mvm->tcm.lock);1097/* re-check if somebody else won the recheck race */1098if (!mvm->tcm.paused && time_after(ts, mvm->tcm.ts + MVM_TCM_PERIOD)) {1099/* calculate statistics */1100unsigned long work_delay = iwl_mvm_calc_tcm_stats(mvm, ts,1101handle_uapsd);11021103/* the memset needs to be visible before the timestamp */1104smp_mb();1105mvm->tcm.ts = ts;1106if (work_delay)1107schedule_delayed_work(&mvm->tcm.work, work_delay);1108}1109spin_unlock(&mvm->tcm.lock);11101111iwl_mvm_tcm_results(mvm);1112}11131114void iwl_mvm_tcm_work(struct work_struct *work)1115{1116struct delayed_work *delayed_work = to_delayed_work(work);1117struct iwl_mvm *mvm = container_of(delayed_work, struct iwl_mvm,1118tcm.work);11191120iwl_mvm_recalc_tcm(mvm);1121}11221123void iwl_mvm_pause_tcm(struct iwl_mvm *mvm, bool with_cancel)1124{1125spin_lock_bh(&mvm->tcm.lock);1126mvm->tcm.paused = true;1127spin_unlock_bh(&mvm->tcm.lock);1128if (with_cancel)1129cancel_delayed_work_sync(&mvm->tcm.work);1130}11311132void iwl_mvm_resume_tcm(struct iwl_mvm *mvm)1133{1134int mac;1135bool low_latency = false;11361137spin_lock_bh(&mvm->tcm.lock);1138mvm->tcm.ts = jiffies;1139mvm->tcm.ll_ts = jiffies;1140for (mac = 0; mac < NUM_MAC_INDEX_DRIVER; mac++) {1141struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[mac];11421143memset(&mdata->rx.pkts, 0, sizeof(mdata->rx.pkts));1144memset(&mdata->tx.pkts, 0, sizeof(mdata->tx.pkts));1145memset(&mdata->rx.airtime, 0, sizeof(mdata->rx.airtime));1146memset(&mdata->tx.airtime, 0, sizeof(mdata->tx.airtime));11471148if (mvm->tcm.result.low_latency[mac])1149low_latency = true;1150}1151/* The TCM data needs to be reset before "paused" flag changes */1152smp_mb();1153mvm->tcm.paused = false;11541155/*1156* if the current load is not low or low latency is active, force1157* re-evaluation to cover the case of no traffic.1158*/1159if (mvm->tcm.result.global_load > IWL_MVM_TRAFFIC_LOW)1160schedule_delayed_work(&mvm->tcm.work, MVM_TCM_PERIOD);1161else if (low_latency)1162schedule_delayed_work(&mvm->tcm.work, MVM_LL_PERIOD);11631164spin_unlock_bh(&mvm->tcm.lock);1165}11661167void iwl_mvm_tcm_add_vif(struct iwl_mvm *mvm, struct ieee80211_vif *vif)1168{1169struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);11701171INIT_DELAYED_WORK(&mvmvif->uapsd_nonagg_detected_wk,1172iwl_mvm_tcm_uapsd_nonagg_detected_wk);1173}11741175void iwl_mvm_tcm_rm_vif(struct iwl_mvm *mvm, struct ieee80211_vif *vif)1176{1177struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);11781179cancel_delayed_work_sync(&mvmvif->uapsd_nonagg_detected_wk);1180}11811182u32 iwl_mvm_get_systime(struct iwl_mvm *mvm)1183{1184u32 reg_addr = DEVICE_SYSTEM_TIME_REG;11851186if (mvm->trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_22000 &&1187mvm->trans->mac_cfg->base->gp2_reg_addr)1188reg_addr = mvm->trans->mac_cfg->base->gp2_reg_addr;11891190return iwl_read_prph(mvm->trans, reg_addr);1191}11921193void iwl_mvm_get_sync_time(struct iwl_mvm *mvm, int clock_type,1194u32 *gp2, u64 *boottime, ktime_t *realtime)1195{1196bool ps_disabled;11971198lockdep_assert_held(&mvm->mutex);11991200/* Disable power save when reading GP2 */1201ps_disabled = mvm->ps_disabled;1202if (!ps_disabled) {1203mvm->ps_disabled = true;1204iwl_mvm_power_update_device(mvm);1205}12061207*gp2 = iwl_mvm_get_systime(mvm);12081209if (clock_type == CLOCK_BOOTTIME && boottime)1210*boottime = ktime_get_boottime_ns();1211else if (clock_type == CLOCK_REALTIME && realtime)1212*realtime = ktime_get_real();12131214if (!ps_disabled) {1215mvm->ps_disabled = ps_disabled;1216iwl_mvm_power_update_device(mvm);1217}1218}12191220/* Find if at least two links from different vifs use same channel1221* FIXME: consider having a refcount array in struct iwl_mvm_vif for1222* used phy_ctxt ids.1223*/1224bool iwl_mvm_have_links_same_channel(struct iwl_mvm_vif *vif1,1225struct iwl_mvm_vif *vif2)1226{1227unsigned int i, j;12281229for_each_mvm_vif_valid_link(vif1, i) {1230for_each_mvm_vif_valid_link(vif2, j) {1231if (vif1->link[i]->phy_ctxt == vif2->link[j]->phy_ctxt)1232return true;1233}1234}12351236return false;1237}12381239static u32 iwl_legacy_rate_to_fw_idx(u32 rate_n_flags)1240{1241int rate = rate_n_flags & RATE_LEGACY_RATE_MSK_V1;1242int idx;1243bool ofdm = !(rate_n_flags & RATE_MCS_CCK_MSK_V1);1244int offset = ofdm ? IWL_FIRST_OFDM_RATE : 0;1245int last = ofdm ? IWL_RATE_COUNT_LEGACY : IWL_FIRST_OFDM_RATE;12461247for (idx = offset; idx < last; idx++)1248if (iwl_fw_rate_idx_to_plcp(idx) == rate)1249return idx - offset;1250return IWL_RATE_INVALID;1251}12521253u32 iwl_mvm_v3_rate_from_fw(__le32 rate, u8 rate_ver)1254{1255u32 rate_v3 = 0, rate_v1;1256u32 dup = 0;12571258if (rate_ver > 1)1259return iwl_v3_rate_from_v2_v3(rate, rate_ver >= 3);12601261rate_v1 = le32_to_cpu(rate);1262if (rate_v1 == 0)1263return rate_v1;1264/* convert rate */1265if (rate_v1 & RATE_MCS_HT_MSK_V1) {1266u32 nss;12671268rate_v3 |= RATE_MCS_MOD_TYPE_HT;1269rate_v3 |=1270rate_v1 & RATE_HT_MCS_RATE_CODE_MSK_V1;1271nss = u32_get_bits(rate_v1, RATE_HT_MCS_MIMO2_MSK);1272rate_v3 |= u32_encode_bits(nss, RATE_MCS_NSS_MSK);1273} else if (rate_v1 & RATE_MCS_VHT_MSK_V1 ||1274rate_v1 & RATE_MCS_HE_MSK_V1) {1275u32 nss = u32_get_bits(rate_v1, RATE_VHT_MCS_NSS_MSK);12761277rate_v3 |= rate_v1 & RATE_VHT_MCS_RATE_CODE_MSK;12781279rate_v3 |= u32_encode_bits(nss, RATE_MCS_NSS_MSK);12801281if (rate_v1 & RATE_MCS_HE_MSK_V1) {1282u32 he_type_bits = rate_v1 & RATE_MCS_HE_TYPE_MSK_V1;1283u32 he_type = he_type_bits >> RATE_MCS_HE_TYPE_POS_V1;1284u32 he_106t = (rate_v1 & RATE_MCS_HE_106T_MSK_V1) >>1285RATE_MCS_HE_106T_POS_V1;1286u32 he_gi_ltf = (rate_v1 & RATE_MCS_HE_GI_LTF_MSK_V1) >>1287RATE_MCS_HE_GI_LTF_POS;12881289if ((he_type_bits == RATE_MCS_HE_TYPE_SU ||1290he_type_bits == RATE_MCS_HE_TYPE_EXT_SU) &&1291he_gi_ltf == RATE_MCS_HE_SU_4_LTF)1292/* the new rate have an additional bit to1293* represent the value 4 rather then using SGI1294* bit for this purpose - as it was done in the1295* old rate1296*/1297he_gi_ltf += (rate_v1 & RATE_MCS_SGI_MSK_V1) >>1298RATE_MCS_SGI_POS_V1;12991300rate_v3 |= he_gi_ltf << RATE_MCS_HE_GI_LTF_POS;1301rate_v3 |= he_type << RATE_MCS_HE_TYPE_POS;1302rate_v3 |= he_106t << RATE_MCS_HE_106T_POS;1303rate_v3 |= rate_v1 & RATE_HE_DUAL_CARRIER_MODE_MSK;1304rate_v3 |= RATE_MCS_MOD_TYPE_HE;1305} else {1306rate_v3 |= RATE_MCS_MOD_TYPE_VHT;1307}1308/* if legacy format */1309} else {1310u32 legacy_rate = iwl_legacy_rate_to_fw_idx(rate_v1);13111312if (WARN_ON_ONCE(legacy_rate == IWL_RATE_INVALID))1313legacy_rate = (rate_v1 & RATE_MCS_CCK_MSK_V1) ?1314IWL_FIRST_CCK_RATE : IWL_FIRST_OFDM_RATE;13151316rate_v3 |= legacy_rate;1317if (!(rate_v1 & RATE_MCS_CCK_MSK_V1))1318rate_v3 |= RATE_MCS_MOD_TYPE_LEGACY_OFDM;1319}13201321/* convert flags */1322if (rate_v1 & RATE_MCS_LDPC_MSK_V1)1323rate_v3 |= RATE_MCS_LDPC_MSK;1324rate_v3 |= (rate_v1 & RATE_MCS_CHAN_WIDTH_MSK_V1) |1325(rate_v1 & RATE_MCS_ANT_AB_MSK) |1326(rate_v1 & RATE_MCS_STBC_MSK) |1327(rate_v1 & RATE_MCS_BF_MSK);13281329dup = (rate_v1 & RATE_MCS_DUP_MSK_V1) >> RATE_MCS_DUP_POS_V1;1330if (dup) {1331rate_v3 |= RATE_MCS_DUP_MSK;1332rate_v3 |= dup << RATE_MCS_CHAN_WIDTH_POS;1333}13341335if ((!(rate_v1 & RATE_MCS_HE_MSK_V1)) &&1336(rate_v1 & RATE_MCS_SGI_MSK_V1))1337rate_v3 |= RATE_MCS_SGI_MSK;13381339return rate_v3;1340}13411342__le32 iwl_mvm_v3_rate_to_fw(u32 rate, u8 rate_ver)1343{1344u32 result = 0;1345int rate_idx;13461347if (rate_ver > 1)1348return iwl_v3_rate_to_v2_v3(rate, rate_ver > 2);13491350switch (rate & RATE_MCS_MOD_TYPE_MSK) {1351case RATE_MCS_MOD_TYPE_CCK:1352result = RATE_MCS_CCK_MSK_V1;1353fallthrough;1354case RATE_MCS_MOD_TYPE_LEGACY_OFDM:1355rate_idx = u32_get_bits(rate, RATE_LEGACY_RATE_MSK);1356if (!(result & RATE_MCS_CCK_MSK_V1))1357rate_idx += IWL_FIRST_OFDM_RATE;1358result |= u32_encode_bits(iwl_fw_rate_idx_to_plcp(rate_idx),1359RATE_LEGACY_RATE_MSK_V1);1360break;1361case RATE_MCS_MOD_TYPE_HT:1362result = RATE_MCS_HT_MSK_V1;1363result |= u32_encode_bits(u32_get_bits(rate,1364RATE_HT_MCS_CODE_MSK),1365RATE_HT_MCS_RATE_CODE_MSK_V1);1366result |= u32_encode_bits(u32_get_bits(rate,1367RATE_MCS_NSS_MSK),1368RATE_HT_MCS_MIMO2_MSK);1369break;1370case RATE_MCS_MOD_TYPE_VHT:1371result = RATE_MCS_VHT_MSK_V1;1372result |= u32_encode_bits(u32_get_bits(rate,1373RATE_VHT_MCS_NSS_MSK),1374RATE_MCS_CODE_MSK);1375result |= u32_encode_bits(u32_get_bits(rate, RATE_MCS_NSS_MSK),1376RATE_VHT_MCS_NSS_MSK);1377break;1378case RATE_MCS_MOD_TYPE_HE: /* not generated */1379default:1380WARN_ONCE(1, "bad modulation type %d\n",1381u32_get_bits(rate, RATE_MCS_MOD_TYPE_MSK));1382return 0;1383}13841385if (rate & RATE_MCS_LDPC_MSK)1386result |= RATE_MCS_LDPC_MSK_V1;1387WARN_ON_ONCE(u32_get_bits(rate, RATE_MCS_CHAN_WIDTH_MSK) >1388RATE_MCS_CHAN_WIDTH_160_VAL);1389result |= (rate & RATE_MCS_CHAN_WIDTH_MSK_V1) |1390(rate & RATE_MCS_ANT_AB_MSK) |1391(rate & RATE_MCS_STBC_MSK) |1392(rate & RATE_MCS_BF_MSK);13931394/* not handling DUP since we don't use it */1395WARN_ON_ONCE(rate & RATE_MCS_DUP_MSK);13961397if (rate & RATE_MCS_SGI_MSK)1398result |= RATE_MCS_SGI_MSK_V1;13991400return cpu_to_le32(result);1401}14021403bool iwl_mvm_vif_is_active(struct iwl_mvm_vif *mvmvif)1404{1405unsigned int i;14061407/* FIXME: can it fail when phy_ctxt is assigned? */1408for_each_mvm_vif_valid_link(mvmvif, i) {1409if (mvmvif->link[i]->phy_ctxt &&1410mvmvif->link[i]->phy_ctxt->id < NUM_PHY_CTX)1411return true;1412}14131414return false;1415}141614171418