Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright(c) 2009-2012  Realtek Corporation.*/
0003 
0004 #include "wifi.h"
0005 #include "base.h"
0006 #include "rc.h"
0007 
0008 /*
0009  *Finds the highest rate index we can use
0010  *if skb is special data like DHCP/EAPOL, we set should
0011  *it to lowest rate CCK_1M, otherwise we set rate to
0012  *highest rate based on wireless mode used for iwconfig
0013  *show Tx rate.
0014  */
0015 static u8 _rtl_rc_get_highest_rix(struct rtl_priv *rtlpriv,
0016                   struct ieee80211_sta *sta,
0017                   struct sk_buff *skb, bool not_data)
0018 {
0019     struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
0020     struct rtl_phy *rtlphy = &(rtlpriv->phy);
0021     struct rtl_sta_info *sta_entry = NULL;
0022     u16 wireless_mode = 0;
0023     u8 nss;
0024     struct ieee80211_tx_rate rate;
0025 
0026     switch (get_rf_type(rtlphy)) {
0027     case RF_4T4R:
0028         nss = 4;
0029         break;
0030     case RF_3T3R:
0031         nss = 3;
0032         break;
0033     case RF_2T2R:
0034         nss = 2;
0035         break;
0036     default:
0037         nss = 1;
0038         break;
0039     }
0040 
0041     /*
0042      *this rate is no use for true rate, firmware
0043      *will control rate at all it just used for
0044      *1.show in iwconfig in B/G mode
0045      *2.in rtl_get_tcb_desc when we check rate is
0046      *      1M we will not use FW rate but user rate.
0047      */
0048 
0049     if (sta) {
0050         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
0051         wireless_mode = sta_entry->wireless_mode;
0052     }
0053 
0054     if (rtl_is_special_data(rtlpriv->mac80211.hw, skb, true, false) ||
0055         not_data) {
0056         return 0;
0057     } else {
0058         if (rtlhal->current_bandtype == BAND_ON_2_4G) {
0059             if (wireless_mode == WIRELESS_MODE_B) {
0060                 return B_MODE_MAX_RIX;
0061             } else if (wireless_mode == WIRELESS_MODE_G) {
0062                 return G_MODE_MAX_RIX;
0063             } else if (wireless_mode == WIRELESS_MODE_N_24G) {
0064                 if (nss == 1)
0065                     return N_MODE_MCS7_RIX;
0066                 else
0067                     return N_MODE_MCS15_RIX;
0068             } else if (wireless_mode == WIRELESS_MODE_AC_24G) {
0069                 if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_20) {
0070                     ieee80211_rate_set_vht(&rate,
0071                                    AC_MODE_MCS8_RIX,
0072                                    nss);
0073                     goto out;
0074                 } else {
0075                     ieee80211_rate_set_vht(&rate,
0076                                    AC_MODE_MCS9_RIX,
0077                                    nss);
0078                     goto out;
0079                 }
0080             }
0081             return 0;
0082         } else {
0083             if (wireless_mode == WIRELESS_MODE_A) {
0084                 return A_MODE_MAX_RIX;
0085             } else if (wireless_mode == WIRELESS_MODE_N_5G) {
0086                 if (nss == 1)
0087                     return N_MODE_MCS7_RIX;
0088                 else
0089                     return N_MODE_MCS15_RIX;
0090             } else if (wireless_mode == WIRELESS_MODE_AC_5G) {
0091                 if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_20) {
0092                     ieee80211_rate_set_vht(&rate,
0093                                    AC_MODE_MCS8_RIX,
0094                                    nss);
0095                     goto out;
0096                 } else {
0097                     ieee80211_rate_set_vht(&rate,
0098                                    AC_MODE_MCS9_RIX,
0099                                    nss);
0100                     goto out;
0101                 }
0102             }
0103             return 0;
0104         }
0105     }
0106 
0107 out:
0108     return rate.idx;
0109 }
0110 
0111 static void _rtl_rc_rate_set_series(struct rtl_priv *rtlpriv,
0112                     struct ieee80211_sta *sta,
0113                     struct ieee80211_tx_rate *rate,
0114                     struct ieee80211_tx_rate_control *txrc,
0115                     u8 tries, s8 rix, int rtsctsenable,
0116                     bool not_data)
0117 {
0118     struct rtl_mac *mac = rtl_mac(rtlpriv);
0119     struct rtl_sta_info *sta_entry = NULL;
0120     u16 wireless_mode = 0;
0121     u8 sgi_20 = 0, sgi_40 = 0, sgi_80 = 0;
0122 
0123     if (sta) {
0124         sgi_20 = sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_20;
0125         sgi_40 = sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_40;
0126         sgi_80 = sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80;
0127         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
0128         wireless_mode = sta_entry->wireless_mode;
0129     }
0130     rate->count = tries;
0131     rate->idx = rix >= 0x00 ? rix : 0x00;
0132 
0133     if (!not_data) {
0134         if (txrc->short_preamble)
0135             rate->flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
0136         if (mac->opmode == NL80211_IFTYPE_AP ||
0137             mac->opmode == NL80211_IFTYPE_ADHOC) {
0138             if (sta && (sta->deflink.ht_cap.cap &
0139                     IEEE80211_HT_CAP_SUP_WIDTH_20_40))
0140                 rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
0141             if (sta && sta->deflink.vht_cap.vht_supported)
0142                 rate->flags |= IEEE80211_TX_RC_80_MHZ_WIDTH;
0143         } else {
0144             if (mac->bw_80)
0145                 rate->flags |= IEEE80211_TX_RC_80_MHZ_WIDTH;
0146             else if (mac->bw_40)
0147                 rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
0148         }
0149 
0150         if (sgi_20 || sgi_40 || sgi_80)
0151             rate->flags |= IEEE80211_TX_RC_SHORT_GI;
0152         if (sta && sta->deflink.ht_cap.ht_supported &&
0153             (wireless_mode == WIRELESS_MODE_N_5G ||
0154              wireless_mode == WIRELESS_MODE_N_24G))
0155             rate->flags |= IEEE80211_TX_RC_MCS;
0156         if (sta && sta->deflink.vht_cap.vht_supported &&
0157             (wireless_mode == WIRELESS_MODE_AC_5G ||
0158              wireless_mode == WIRELESS_MODE_AC_24G ||
0159              wireless_mode == WIRELESS_MODE_AC_ONLY))
0160             rate->flags |= IEEE80211_TX_RC_VHT_MCS;
0161     }
0162 }
0163 
0164 static void rtl_get_rate(void *ppriv, struct ieee80211_sta *sta,
0165              void *priv_sta,
0166              struct ieee80211_tx_rate_control *txrc)
0167 {
0168     struct rtl_priv *rtlpriv = ppriv;
0169     struct sk_buff *skb = txrc->skb;
0170     struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
0171     struct ieee80211_tx_rate *rates = tx_info->control.rates;
0172     __le16 fc = rtl_get_fc(skb);
0173     u8 try_per_rate, i, rix;
0174     bool not_data = !ieee80211_is_data(fc);
0175 
0176     rix = _rtl_rc_get_highest_rix(rtlpriv, sta, skb, not_data);
0177     try_per_rate = 1;
0178     _rtl_rc_rate_set_series(rtlpriv, sta, &rates[0], txrc,
0179                 try_per_rate, rix, 1, not_data);
0180 
0181     if (!not_data) {
0182         for (i = 1; i < 4; i++)
0183             _rtl_rc_rate_set_series(rtlpriv, sta, &rates[i],
0184                         txrc, i, (rix - i), 1,
0185                         not_data);
0186     }
0187 }
0188 
0189 static bool _rtl_tx_aggr_check(struct rtl_priv *rtlpriv,
0190                    struct rtl_sta_info *sta_entry, u16 tid)
0191 {
0192     struct rtl_mac *mac = rtl_mac(rtlpriv);
0193 
0194     if (mac->act_scanning)
0195         return false;
0196 
0197     if (mac->opmode == NL80211_IFTYPE_STATION &&
0198         mac->cnt_after_linked < 3)
0199         return false;
0200 
0201     if (sta_entry->tids[tid].agg.agg_state == RTL_AGG_STOP)
0202         return true;
0203 
0204     return false;
0205 }
0206 
0207 /*mac80211 Rate Control callbacks*/
0208 static void rtl_tx_status(void *ppriv,
0209               struct ieee80211_supported_band *sband,
0210               struct ieee80211_sta *sta, void *priv_sta,
0211               struct sk_buff *skb)
0212 {
0213     struct rtl_priv *rtlpriv = ppriv;
0214     struct rtl_mac *mac = rtl_mac(rtlpriv);
0215     struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
0216     __le16 fc = rtl_get_fc(skb);
0217     struct rtl_sta_info *sta_entry;
0218 
0219     if (!priv_sta || !ieee80211_is_data(fc))
0220         return;
0221 
0222     if (rtl_is_special_data(mac->hw, skb, true, true))
0223         return;
0224 
0225     if (is_multicast_ether_addr(ieee80211_get_DA(hdr)) ||
0226         is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
0227         return;
0228 
0229     if (sta) {
0230         /* Check if aggregation has to be enabled for this tid */
0231         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
0232         if (sta->deflink.ht_cap.ht_supported &&
0233             !(skb->protocol == cpu_to_be16(ETH_P_PAE))) {
0234             if (ieee80211_is_data_qos(fc)) {
0235                 u8 tid = rtl_get_tid(skb);
0236 
0237                 if (_rtl_tx_aggr_check(rtlpriv, sta_entry,
0238                                tid)) {
0239                     sta_entry->tids[tid].agg.agg_state =
0240                         RTL_AGG_PROGRESS;
0241                     ieee80211_start_tx_ba_session(sta, tid,
0242                                       5000);
0243                 }
0244             }
0245         }
0246     }
0247 }
0248 
0249 static void rtl_rate_init(void *ppriv,
0250               struct ieee80211_supported_band *sband,
0251               struct cfg80211_chan_def *chandef,
0252               struct ieee80211_sta *sta, void *priv_sta)
0253 {
0254 }
0255 
0256 static void rtl_rate_update(void *ppriv,
0257                 struct ieee80211_supported_band *sband,
0258                 struct cfg80211_chan_def *chandef,
0259                 struct ieee80211_sta *sta, void *priv_sta,
0260                 u32 changed)
0261 {
0262 }
0263 
0264 static void *rtl_rate_alloc(struct ieee80211_hw *hw)
0265 {
0266     struct rtl_priv *rtlpriv = rtl_priv(hw);
0267     return rtlpriv;
0268 }
0269 
0270 static void rtl_rate_free(void *rtlpriv)
0271 {
0272     return;
0273 }
0274 
0275 static void *rtl_rate_alloc_sta(void *ppriv,
0276                 struct ieee80211_sta *sta, gfp_t gfp)
0277 {
0278     struct rtl_priv *rtlpriv = ppriv;
0279     struct rtl_rate_priv *rate_priv;
0280 
0281     rate_priv = kzalloc(sizeof(*rate_priv), gfp);
0282     if (!rate_priv)
0283         return NULL;
0284 
0285     rtlpriv->rate_priv = rate_priv;
0286 
0287     return rate_priv;
0288 }
0289 
0290 static void rtl_rate_free_sta(void *rtlpriv,
0291                   struct ieee80211_sta *sta, void *priv_sta)
0292 {
0293     struct rtl_rate_priv *rate_priv = priv_sta;
0294 
0295     kfree(rate_priv);
0296 }
0297 
0298 static const struct rate_control_ops rtl_rate_ops = {
0299     .name = "rtl_rc",
0300     .alloc = rtl_rate_alloc,
0301     .free = rtl_rate_free,
0302     .alloc_sta = rtl_rate_alloc_sta,
0303     .free_sta = rtl_rate_free_sta,
0304     .rate_init = rtl_rate_init,
0305     .rate_update = rtl_rate_update,
0306     .tx_status = rtl_tx_status,
0307     .get_rate = rtl_get_rate,
0308 };
0309 
0310 int rtl_rate_control_register(void)
0311 {
0312     return ieee80211_rate_control_register(&rtl_rate_ops);
0313 }
0314 
0315 void rtl_rate_control_unregister(void)
0316 {
0317     ieee80211_rate_control_unregister(&rtl_rate_ops);
0318 }