1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
4 * Copyright(c) 2009-2012 Realtek Corporation.
7 * wlanfae <wlanfae@realtek.com>
8 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
11 * Larry Finger <Larry.Finger@lwfinger.net>
13 *****************************************************************************/
20 *Finds the highest rate index we can use
21 *if skb is special data like DHCP/EAPOL, we set should
22 *it to lowest rate CCK_1M, otherwise we set rate to
23 *highest rate based on wireless mode used for iwconfig
26 static u8
_rtl_rc_get_highest_rix(struct rtl_priv
*rtlpriv
,
27 struct ieee80211_sta
*sta
,
28 struct sk_buff
*skb
, bool not_data
)
30 struct rtl_hal
*rtlhal
= rtl_hal(rtlpriv
);
31 struct rtl_phy
*rtlphy
= &rtlpriv
->phy
;
32 struct rtl_sta_info
*sta_entry
= NULL
;
33 u16 wireless_mode
= 0;
36 if (get_rf_type(rtlphy
) >= RF_4T4R
)
38 else if (get_rf_type(rtlphy
) >= RF_3T3R
)
40 else if (get_rf_type(rtlphy
) >= RF_2T2R
)
46 *this rate is no use for true rate, firmware
47 *will control rate at all it just used for
48 *1.show in iwconfig in B/G mode
49 *2.in rtl_get_tcb_desc when we check rate is
50 * 1M we will not use FW rate but user rate.
54 sta_entry
= (struct rtl_sta_info
*)sta
->drv_priv
;
55 wireless_mode
= sta_entry
->wireless_mode
;
58 if (rtl_is_special_data(rtlpriv
->mac80211
.hw
, skb
, true, false) ||
62 if (rtlhal
->current_bandtype
== BAND_ON_2_4G
) {
63 if (wireless_mode
== WIRELESS_MODE_B
) {
64 return B_MODE_MAX_RIX
;
65 } else if (wireless_mode
== WIRELESS_MODE_G
) {
66 return G_MODE_MAX_RIX
;
67 } else if (wireless_mode
== WIRELESS_MODE_N_24G
) {
69 return N_MODE_MCS7_RIX
;
71 return N_MODE_MCS15_RIX
;
72 } else if (wireless_mode
== WIRELESS_MODE_AC_24G
) {
73 if (sta
->bandwidth
== IEEE80211_STA_RX_BW_20
)
74 return AC_MODE_MCS8_RIX
| (nss
<< 4);
76 return AC_MODE_MCS9_RIX
| (nss
<< 4);
80 if (wireless_mode
== WIRELESS_MODE_A
) {
81 return A_MODE_MAX_RIX
;
82 } else if (wireless_mode
== WIRELESS_MODE_N_5G
) {
84 return N_MODE_MCS7_RIX
;
86 return N_MODE_MCS15_RIX
;
87 } else if (wireless_mode
== WIRELESS_MODE_AC_5G
) {
88 if (sta
->bandwidth
== IEEE80211_STA_RX_BW_20
)
89 return AC_MODE_MCS8_RIX
| (nss
<< 4);
91 return AC_MODE_MCS9_RIX
| (nss
<< 4);
96 static void _rtl_rc_rate_set_series(struct rtl_priv
*rtlpriv
,
97 struct ieee80211_sta
*sta
,
98 struct ieee80211_tx_rate
*rate
,
99 struct ieee80211_tx_rate_control
*txrc
,
100 u8 tries
, s8 rix
, int rtsctsenable
,
103 struct rtl_mac
*mac
= rtl_mac(rtlpriv
);
104 struct rtl_sta_info
*sta_entry
= NULL
;
105 u16 wireless_mode
= 0;
106 u8 sgi_20
= 0, sgi_40
= 0, sgi_80
= 0;
109 sgi_20
= sta
->ht_cap
.cap
& IEEE80211_HT_CAP_SGI_20
;
110 sgi_40
= sta
->ht_cap
.cap
& IEEE80211_HT_CAP_SGI_40
;
111 sgi_80
= sta
->vht_cap
.cap
& IEEE80211_VHT_CAP_SHORT_GI_80
;
112 sta_entry
= (struct rtl_sta_info
*)sta
->drv_priv
;
113 wireless_mode
= sta_entry
->wireless_mode
;
116 rate
->idx
= rix
>= 0x00 ? rix
: 0x00;
117 if ((rtlpriv
->rtlhal
.hw_type
== HARDWARE_TYPE_RTL8812AE
||
118 rtlpriv
->rtlhal
.hw_type
== HARDWARE_TYPE_RTL8822BE
) &&
119 wireless_mode
== WIRELESS_MODE_AC_5G
)
120 rate
->idx
|= 0x10;/*2NSS for 8812AE, 8822BE*/
123 if (txrc
->short_preamble
)
124 rate
->flags
|= IEEE80211_TX_RC_USE_SHORT_PREAMBLE
;
125 if (mac
->opmode
== NL80211_IFTYPE_AP
||
126 mac
->opmode
== NL80211_IFTYPE_ADHOC
) {
127 if (sta
&& (sta
->ht_cap
.cap
&
128 IEEE80211_HT_CAP_SUP_WIDTH_20_40
))
129 rate
->flags
|= IEEE80211_TX_RC_40_MHZ_WIDTH
;
130 if (sta
&& sta
->vht_cap
.vht_supported
)
131 rate
->flags
|= IEEE80211_TX_RC_80_MHZ_WIDTH
;
134 rate
->flags
|= IEEE80211_TX_RC_80_MHZ_WIDTH
;
136 rate
->flags
|= IEEE80211_TX_RC_40_MHZ_WIDTH
;
139 if (sgi_20
|| sgi_40
|| sgi_80
)
140 rate
->flags
|= IEEE80211_TX_RC_SHORT_GI
;
141 if (sta
&& sta
->ht_cap
.ht_supported
&&
142 (wireless_mode
== WIRELESS_MODE_N_5G
||
143 wireless_mode
== WIRELESS_MODE_N_24G
))
144 rate
->flags
|= IEEE80211_TX_RC_MCS
;
145 if (sta
&& sta
->vht_cap
.vht_supported
&&
146 (wireless_mode
== WIRELESS_MODE_AC_5G
||
147 wireless_mode
== WIRELESS_MODE_AC_24G
||
148 wireless_mode
== WIRELESS_MODE_AC_ONLY
))
149 rate
->flags
|= IEEE80211_TX_RC_VHT_MCS
;
153 static void rtl_get_rate(void *ppriv
, struct ieee80211_sta
*sta
,
155 struct ieee80211_tx_rate_control
*txrc
)
157 struct rtl_priv
*rtlpriv
= ppriv
;
158 struct sk_buff
*skb
= txrc
->skb
;
159 struct ieee80211_tx_info
*tx_info
= IEEE80211_SKB_CB(skb
);
160 struct ieee80211_tx_rate
*rates
= tx_info
->control
.rates
;
161 __le16 fc
= rtl_get_fc(skb
);
162 u8 try_per_rate
, i
, rix
;
163 bool not_data
= !ieee80211_is_data(fc
);
165 if (rate_control_send_low(sta
, priv_sta
, txrc
))
168 rix
= _rtl_rc_get_highest_rix(rtlpriv
, sta
, skb
, not_data
);
170 _rtl_rc_rate_set_series(rtlpriv
, sta
, &rates
[0], txrc
,
171 try_per_rate
, rix
, 1, not_data
);
174 for (i
= 1; i
< 4; i
++)
175 _rtl_rc_rate_set_series(rtlpriv
, sta
, &rates
[i
],
176 txrc
, i
, (rix
- i
), 1,
181 static bool _rtl_tx_aggr_check(struct rtl_priv
*rtlpriv
,
182 struct rtl_sta_info
*sta_entry
, u16 tid
)
184 struct rtl_mac
*mac
= rtl_mac(rtlpriv
);
186 if (mac
->act_scanning
)
189 if (mac
->opmode
== NL80211_IFTYPE_STATION
&&
190 mac
->cnt_after_linked
< 3)
193 if (sta_entry
->tids
[tid
].agg
.agg_state
== RTL_AGG_STOP
)
199 /*mac80211 Rate Control callbacks*/
200 static void rtl_tx_status(void *ppriv
,
201 struct ieee80211_supported_band
*sband
,
202 struct ieee80211_sta
*sta
, void *priv_sta
,
205 struct rtl_priv
*rtlpriv
= ppriv
;
206 struct rtl_mac
*mac
= rtl_mac(rtlpriv
);
207 struct ieee80211_hdr
*hdr
= rtl_get_hdr(skb
);
208 __le16 fc
= rtl_get_fc(skb
);
209 struct rtl_sta_info
*sta_entry
;
211 if (!priv_sta
|| !ieee80211_is_data(fc
))
214 if (rtl_is_special_data(mac
->hw
, skb
, true, true))
217 if (is_multicast_ether_addr(ieee80211_get_DA(hdr
)) ||
218 is_broadcast_ether_addr(ieee80211_get_DA(hdr
)))
222 /* Check if aggregation has to be enabled for this tid */
223 sta_entry
= (struct rtl_sta_info
*)sta
->drv_priv
;
224 if (sta
->ht_cap
.ht_supported
&&
225 !(skb
->protocol
== cpu_to_be16(ETH_P_PAE
))) {
226 if (ieee80211_is_data_qos(fc
)) {
227 u8 tid
= rtl_get_tid(skb
);
229 if (_rtl_tx_aggr_check(rtlpriv
, sta_entry
,
231 sta_entry
->tids
[tid
].agg
.agg_state
=
233 ieee80211_start_tx_ba_session(sta
, tid
,
241 static void rtl_rate_init(void *ppriv
,
242 struct ieee80211_supported_band
*sband
,
243 struct cfg80211_chan_def
*chandef
,
244 struct ieee80211_sta
*sta
, void *priv_sta
)
248 static void rtl_rate_update(void *ppriv
,
249 struct ieee80211_supported_band
*sband
,
250 struct cfg80211_chan_def
*chandef
,
251 struct ieee80211_sta
*sta
, void *priv_sta
,
256 static void *rtl_rate_alloc(struct ieee80211_hw
*hw
, struct dentry
*debugfsdir
)
258 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
262 static void rtl_rate_free(void *rtlpriv
)
266 static void *rtl_rate_alloc_sta(void *ppriv
,
267 struct ieee80211_sta
*sta
, gfp_t gfp
)
269 struct rtl_priv
*rtlpriv
= ppriv
;
270 struct rtl_rate_priv
*rate_priv
;
272 rate_priv
= kzalloc(sizeof(*rate_priv
), gfp
);
276 rtlpriv
->rate_priv
= rate_priv
;
281 static void rtl_rate_free_sta(void *rtlpriv
,
282 struct ieee80211_sta
*sta
, void *priv_sta
)
284 struct rtl_rate_priv
*rate_priv
= priv_sta
;
289 static const struct rate_control_ops rtl_rate_ops
= {
291 .alloc
= rtl_rate_alloc
,
292 .free
= rtl_rate_free
,
293 .alloc_sta
= rtl_rate_alloc_sta
,
294 .free_sta
= rtl_rate_free_sta
,
295 .rate_init
= rtl_rate_init
,
296 .rate_update
= rtl_rate_update
,
297 .tx_status
= rtl_tx_status
,
298 .get_rate
= rtl_get_rate
,
301 int rtl_rate_control_register(void)
303 return ieee80211_rate_control_register(&rtl_rate_ops
);
306 void rtl_rate_control_unregister(void)
308 ieee80211_rate_control_unregister(&rtl_rate_ops
);