Merge branch 'for-john' of git://git.kernel.org/pub/scm/linux/kernel/git/jberg/mac802...
[deliverable/linux.git] / drivers / net / wireless / rtlwifi / rc.c
CommitLineData
0c817338
LF
1/******************************************************************************
2 *
a8d76066 3 * Copyright(c) 2009-2012 Realtek Corporation.
0c817338
LF
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
21 * Contact Information:
22 * wlanfae <wlanfae@realtek.com>
23 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
24 * Hsinchu 300, Taiwan.
25 *
26 * Larry Finger <Larry.Finger@lwfinger.net>
27 *
28 *****************************************************************************/
29
30#include "wifi.h"
31#include "base.h"
32#include "rc.h"
33
34/*
35 *Finds the highest rate index we can use
36 *if skb is special data like DHCP/EAPOL, we set should
37 *it to lowest rate CCK_1M, otherwise we set rate to
38 *CCK11M or OFDM_54M based on wireless mode.
39 */
40static u8 _rtl_rc_get_highest_rix(struct rtl_priv *rtlpriv,
c6a9de08 41 struct ieee80211_sta *sta,
0c817338
LF
42 struct sk_buff *skb, bool not_data)
43{
44 struct rtl_mac *rtlmac = rtl_mac(rtlpriv);
c6a9de08
C
45 struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
46 struct rtl_phy *rtlphy = &(rtlpriv->phy);
47 struct rtl_sta_info *sta_entry = NULL;
48 u8 wireless_mode = 0;
0c817338
LF
49
50 /*
51 *this rate is no use for true rate, firmware
52 *will control rate at all it just used for
53 *1.show in iwconfig in B/G mode
54 *2.in rtl_get_tcb_desc when we check rate is
55 * 1M we will not use FW rate but user rate.
56 */
c6a9de08 57 if (rtlmac->opmode == NL80211_IFTYPE_AP ||
0f015453
LF
58 rtlmac->opmode == NL80211_IFTYPE_ADHOC ||
59 rtlmac->opmode == NL80211_IFTYPE_MESH_POINT) {
c6a9de08
C
60 if (sta) {
61 sta_entry = (struct rtl_sta_info *) sta->drv_priv;
62 wireless_mode = sta_entry->wireless_mode;
63 } else {
64 return 0;
65 }
0c817338 66 } else {
c6a9de08
C
67 wireless_mode = rtlmac->mode;
68 }
69
70 if (rtl_is_special_data(rtlpriv->mac80211.hw, skb, true) ||
71 not_data) {
72 return 0;
73 } else {
74 if (rtlhal->current_bandtype == BAND_ON_2_4G) {
75 if (wireless_mode == WIRELESS_MODE_B) {
76 return B_MODE_MAX_RIX;
77 } else if (wireless_mode == WIRELESS_MODE_G) {
78 return G_MODE_MAX_RIX;
79 } else {
80 if (get_rf_type(rtlphy) != RF_2T2R)
81 return N_MODE_MCS7_RIX;
82 else
83 return N_MODE_MCS15_RIX;
84 }
85 } else {
86 if (wireless_mode == WIRELESS_MODE_A) {
87 return A_MODE_MAX_RIX;
88 } else {
89 if (get_rf_type(rtlphy) != RF_2T2R)
90 return N_MODE_MCS7_RIX;
91 else
92 return N_MODE_MCS15_RIX;
93 }
94 }
0c817338
LF
95 }
96}
97
98static void _rtl_rc_rate_set_series(struct rtl_priv *rtlpriv,
c6a9de08 99 struct ieee80211_sta *sta,
0c817338
LF
100 struct ieee80211_tx_rate *rate,
101 struct ieee80211_tx_rate_control *txrc,
c6a9de08 102 u8 tries, char rix, int rtsctsenable,
0c817338
LF
103 bool not_data)
104{
105 struct rtl_mac *mac = rtl_mac(rtlpriv);
c6a9de08 106 u8 sgi_20 = 0, sgi_40 = 0;
0c817338 107
c6a9de08
C
108 if (sta) {
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 }
0c817338 112 rate->count = tries;
c6a9de08 113 rate->idx = rix >= 0x00 ? rix : 0x00;
0c817338
LF
114
115 if (!not_data) {
116 if (txrc->short_preamble)
117 rate->flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
c6a9de08
C
118 if (mac->opmode == NL80211_IFTYPE_AP ||
119 mac->opmode == NL80211_IFTYPE_ADHOC) {
120 if (sta && (sta->ht_cap.cap &
121 IEEE80211_HT_CAP_SUP_WIDTH_20_40))
122 rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
123 } else {
124 if (mac->bw_40)
125 rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
126 }
127 if (sgi_20 || sgi_40)
0c817338 128 rate->flags |= IEEE80211_TX_RC_SHORT_GI;
c6a9de08 129 if (sta && sta->ht_cap.ht_supported)
0c817338
LF
130 rate->flags |= IEEE80211_TX_RC_MCS;
131 }
132}
133
134static void rtl_get_rate(void *ppriv, struct ieee80211_sta *sta,
135 void *priv_sta, struct ieee80211_tx_rate_control *txrc)
136{
137 struct rtl_priv *rtlpriv = ppriv;
138 struct sk_buff *skb = txrc->skb;
139 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
140 struct ieee80211_tx_rate *rates = tx_info->control.rates;
c6a9de08 141 __le16 fc = rtl_get_fc(skb);
0c817338
LF
142 u8 try_per_rate, i, rix;
143 bool not_data = !ieee80211_is_data(fc);
144
145 if (rate_control_send_low(sta, priv_sta, txrc))
146 return;
147
c6a9de08 148 rix = _rtl_rc_get_highest_rix(rtlpriv, sta, skb, not_data);
0c817338 149 try_per_rate = 1;
c6a9de08 150 _rtl_rc_rate_set_series(rtlpriv, sta, &rates[0], txrc,
0c817338
LF
151 try_per_rate, rix, 1, not_data);
152
153 if (!not_data) {
154 for (i = 1; i < 4; i++)
c6a9de08 155 _rtl_rc_rate_set_series(rtlpriv, sta, &rates[i],
0c817338
LF
156 txrc, i, (rix - i), 1,
157 not_data);
158 }
159}
160
c6a9de08
C
161static bool _rtl_tx_aggr_check(struct rtl_priv *rtlpriv,
162 struct rtl_sta_info *sta_entry, u16 tid)
0c817338
LF
163{
164 struct rtl_mac *mac = rtl_mac(rtlpriv);
165
166 if (mac->act_scanning)
167 return false;
168
c6a9de08
C
169 if (mac->opmode == NL80211_IFTYPE_STATION &&
170 mac->cnt_after_linked < 3)
0c817338
LF
171 return false;
172
c6a9de08 173 if (sta_entry->tids[tid].agg.agg_state == RTL_AGG_STOP)
0c817338
LF
174 return true;
175
176 return false;
177}
178
179/*mac80211 Rate Control callbacks*/
180static void rtl_tx_status(void *ppriv,
181 struct ieee80211_supported_band *sband,
182 struct ieee80211_sta *sta, void *priv_sta,
183 struct sk_buff *skb)
184{
185 struct rtl_priv *rtlpriv = ppriv;
186 struct rtl_mac *mac = rtl_mac(rtlpriv);
c6a9de08
C
187 struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
188 __le16 fc = rtl_get_fc(skb);
189 struct rtl_sta_info *sta_entry;
0c817338
LF
190
191 if (!priv_sta || !ieee80211_is_data(fc))
192 return;
193
194 if (rtl_is_special_data(mac->hw, skb, true))
195 return;
196
197 if (is_multicast_ether_addr(ieee80211_get_DA(hdr))
198 || is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
199 return;
200
c6a9de08
C
201 if (sta) {
202 /* Check if aggregation has to be enabled for this tid */
203 sta_entry = (struct rtl_sta_info *) sta->drv_priv;
e10542c4 204 if ((sta->ht_cap.ht_supported) &&
c6a9de08
C
205 !(skb->protocol == cpu_to_be16(ETH_P_PAE))) {
206 if (ieee80211_is_data_qos(fc)) {
207 u8 tid = rtl_get_tid(skb);
208 if (_rtl_tx_aggr_check(rtlpriv, sta_entry,
209 tid)) {
210 sta_entry->tids[tid].agg.agg_state =
211 RTL_AGG_PROGRESS;
212 ieee80211_start_tx_ba_session(sta,
213 tid, 5000);
214 }
215 }
0c817338
LF
216 }
217 }
218}
219
0c817338
LF
220static void *rtl_rate_alloc(struct ieee80211_hw *hw,
221 struct dentry *debugfsdir)
222{
223 struct rtl_priv *rtlpriv = rtl_priv(hw);
224 return rtlpriv;
225}
226
227static void rtl_rate_free(void *rtlpriv)
228{
229 return;
230}
231
232static void *rtl_rate_alloc_sta(void *ppriv,
233 struct ieee80211_sta *sta, gfp_t gfp)
234{
235 struct rtl_priv *rtlpriv = ppriv;
236 struct rtl_rate_priv *rate_priv;
237
238 rate_priv = kzalloc(sizeof(struct rtl_rate_priv), gfp);
239 if (!rate_priv) {
240 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
f30d7507 241 "Unable to allocate private rc structure\n");
0c817338
LF
242 return NULL;
243 }
244
245 rtlpriv->rate_priv = rate_priv;
246
247 return rate_priv;
248}
249
250static void rtl_rate_free_sta(void *rtlpriv,
251 struct ieee80211_sta *sta, void *priv_sta)
252{
253 struct rtl_rate_priv *rate_priv = priv_sta;
254 kfree(rate_priv);
255}
256
257static struct rate_control_ops rtl_rate_ops = {
258 .module = NULL,
259 .name = "rtl_rc",
260 .alloc = rtl_rate_alloc,
261 .free = rtl_rate_free,
262 .alloc_sta = rtl_rate_alloc_sta,
263 .free_sta = rtl_rate_free_sta,
0c817338
LF
264 .tx_status = rtl_tx_status,
265 .get_rate = rtl_get_rate,
266};
267
268int rtl_rate_control_register(void)
269{
270 return ieee80211_rate_control_register(&rtl_rate_ops);
271}
272
273void rtl_rate_control_unregister(void)
274{
275 ieee80211_rate_control_unregister(&rtl_rate_ops);
276}
This page took 0.20831 seconds and 5 git commands to generate.