18 #include <linux/slab.h>
19 #include <linux/export.h>
382 static int ath_rc_get_rateindex(
struct ath_rate_priv *ath_rc_priv,
395 rate_table->
info[idx].ratecode == rate->
idx)
407 static void ath_rc_sort_validrates(
struct ath_rate_priv *ath_rc_priv)
413 for (j = 0; j <= i-1; j++) {
417 if (rate_table->
info[idx].ratekbps >
418 rate_table->
info[idx_next].ratekbps) {
427 int ath_rc_get_nextvalid_txrate(
const struct ath_rate_table *rate_table,
449 static int ath_rc_valid_phyrate(
u32 phy,
u32 capflag,
int ignore_cw)
467 u8 cur_valid_txrate,
u8 *next_idx)
486 for (i = 0; i < rate_table->
rate_cnt; i++) {
489 u8 valid_rate_count = 0;
491 if (!ath_rc_valid_phyrate(phy, ath_rc_priv->
ht_cap, 0))
521 static inline bool ath_rc_check_ht(
u8 rate,
u8 dot11rate,
u16 rate_flags,
536 static u8 ath_rc_setvalid_rates(
struct ath_rate_priv *ath_rc_priv,
bool legacy)
542 u8 i,
j, hi = 0,
rate, dot11rate, valid_rate_count;
549 for (i = 0; i < rateset->
rs_nrates; i++) {
550 for (j = 0; j < rate_table->
rate_cnt; j++) {
551 phy = rate_table->
info[
j].phy;
552 rate_flags = rate_table->
info[
j].rate_flags;
554 dot11rate = rate_table->
info[
j].dot11rate;
557 !ath_rc_check_legacy(rate, dot11rate,
558 rate_flags, phy, capflag))
562 !ath_rc_check_ht(rate, dot11rate,
563 rate_flags, phy, capflag))
566 if (!ath_rc_valid_phyrate(phy, capflag, 0))
584 u32 best_thruput, this_thruput, now_msec;
585 u8 rate, next_rate, best_rate, maxindex, minindex;
593 best_rate = minindex;
599 for (index = maxindex; index >= minindex ; index--) {
617 per_thres = ath_rc_priv->
per[
rate];
621 this_thruput = rate_table->
info[
rate].user_ratekbps *
624 if (best_thruput <= this_thruput) {
625 best_thruput = this_thruput;
641 if (ath_rc_get_nextvalid_txrate(rate_table,
642 ath_rc_priv, rate, &next_rate) &&
676 static void ath_rc_rate_set_series(
const struct ath_rate_table *rate_table,
679 u8 tries,
u8 rix,
int rtsctsenable)
682 rate->
idx = rate_table->
info[rix].ratecode;
684 if (txrc->
rts || rtsctsenable)
690 conf_is_ht40(&txrc->
hw->conf))
697 static void ath_rc_rate_set_rtscts(
struct ath_softc *
sc,
711 bss_conf = &tx_info->
control.vif->bss_conf;
733 u8 try_per_rate, i = 0, rix;
752 rix = ath_rc_get_highest_rix(ath_rc_priv, &is_probe);
754 if (conf_is_ht(&sc->
hw->conf) &&
758 if (conf_is_ht(&sc->
hw->conf) &&
767 ath_rc_rate_set_series(rate_table, &rates[i++], txrc,
773 ath_rc_get_lower_rix(ath_rc_priv, rix, &rix);
774 ath_rc_rate_set_series(rate_table, &rates[i++], txrc,
775 try_per_rate, rix, 0);
782 ath_rc_rate_set_series(rate_table, &rates[i++], txrc,
783 try_per_rate, rix, 0);
786 for ( ; i < 4; i++) {
793 ath_rc_get_lower_rix(ath_rc_priv, rix, &rix);
798 ath_rc_rate_set_series(rate_table, &rates[i], txrc,
799 try_per_rate, rix, 1);
818 (conf_is_ht(&sc->
hw->conf))) {
819 u8 dot11rate = rate_table->
info[rix].dot11rate;
820 u8 phy = rate_table->
info[rix].phy;
824 rates[3].
idx = rates[2].
idx;
836 if (ieee80211_has_morefrags(
fc) ||
839 rates[1].
idx = rates[2].
idx = rates[3].
idx = 0;
843 ath_rc_rate_set_rtscts(sc, rate_table, tx_info);
846 static void ath_rc_update_per(
struct ath_softc *sc,
850 int tx_rate,
int xretries,
int retries,
853 int count, n_bad_frames;
855 static const u32 nretry_to_per_lookup[10] = {
868 last_per = ath_rc_priv->
per[tx_rate];
869 n_bad_frames = tx_info->
status.ampdu_len - tx_info->
status.ampdu_ack_len;
873 ath_rc_priv->
per[tx_rate] += 30;
874 if (ath_rc_priv->
per[tx_rate] > 100)
875 ath_rc_priv->
per[tx_rate] = 100;
879 if (retries >= count)
883 ath_rc_priv->
per[tx_rate] =
884 (
u8)(last_per - (last_per >> 3) + (100 >> 3));
894 if (retries >= count)
910 if (tx_info->
status.ampdu_len > 0) {
911 int n_frames, n_bad_tries;
914 n_bad_tries = retries * tx_info->
status.ampdu_len +
916 n_frames = tx_info->
status.ampdu_len * (retries + 1);
917 cur_per = (100 * n_bad_tries / n_frames) >> 3;
918 new_per = (
u8)(last_per - (last_per >> 3) + cur_per);
919 ath_rc_priv->
per[tx_rate] = new_per;
922 ath_rc_priv->
per[tx_rate] =
923 (
u8)(last_per - (last_per >> 3) +
924 (nretry_to_per_lookup[
retries] >> 3));
933 if (retries > 0 || 2 * n_bad_frames > tx_info->
status.ampdu_len) {
949 if (ath_rc_priv->
per[probe_rate] > 30)
950 ath_rc_priv->
per[probe_rate] = 20;
986 int xretries,
int retries,
u8 per)
995 static void ath_rc_update_ht(
struct ath_softc *sc,
998 int tx_rate,
int xretries,
int retries)
1006 if ((tx_rate < 0) || (tx_rate > rate_table->
rate_cnt))
1009 last_per = ath_rc_priv->
per[tx_rate];
1012 ath_rc_update_per(sc, rate_table, ath_rc_priv,
1013 tx_info, tx_rate, xretries,
1020 if (ath_rc_priv->
per[tx_rate] >= 55 && tx_rate > 0 &&
1021 rate_table->
info[tx_rate].ratekbps <=
1023 ath_rc_get_lower_rix(ath_rc_priv, (
u8)tx_rate,
1032 if (ath_rc_priv->
per[tx_rate] < last_per) {
1033 for (rate = tx_rate - 1; rate >= 0; rate--) {
1035 if (ath_rc_priv->
per[rate] >
1036 ath_rc_priv->
per[rate+1]) {
1038 ath_rc_priv->
per[rate+1];
1044 for (rate = tx_rate; rate < size - 1; rate++) {
1045 if (ath_rc_priv->
per[rate+1] <
1046 ath_rc_priv->
per[rate])
1047 ath_rc_priv->
per[rate+1] =
1055 for (rate = 0; rate <
size; rate++) {
1057 7 * ath_rc_priv->
per[
rate] / 8;
1063 ath_debug_stat_retries(ath_rc_priv, tx_rate, xretries, retries,
1064 ath_rc_priv->
per[tx_rate]);
1068 static void ath_debug_stat_rc(
struct ath_rate_priv *
rc,
int final_rate)
1072 stats = &rc->
rcstats[final_rate];
1076 static void ath_rc_tx_status(
struct ath_softc *sc,
1083 int final_ts_idx = 0, xretries = 0, long_retry = 0;
1087 for (i = 0; i < sc->
hw->max_rates; i++) {
1088 rate = &tx_info->
status.rates[
i];
1093 long_retry = rate->
count - 1;
1103 if (final_ts_idx != 0) {
1104 for (i = 0; i < final_ts_idx ; i++) {
1105 if (rates[i].count != 0 && (rates[i].idx >= 0)) {
1115 rix = ath_rc_get_rateindex(ath_rc_priv, &rates[i]);
1116 ath_rc_update_ht(sc, ath_rc_priv, tx_info,
1117 rix, xretries ? 1 : 2,
1123 flags = rates[final_ts_idx].
flags;
1130 rix = ath_rc_get_rateindex(ath_rc_priv, &rates[final_ts_idx]);
1131 ath_rc_update_ht(sc, ath_rc_priv, tx_info, rix, xretries, long_retry);
1132 ath_debug_stat_rc(ath_rc_priv, rix);
1143 return &ar5416_11ng_ratetable;
1144 return &ar5416_11g_ratetable;
1147 return &ar5416_11na_ratetable;
1148 return &ar5416_11a_ratetable;
1154 static void ath_rc_init(
struct ath_softc *sc,
1160 u8 i,
j,
k, hi = 0, hthi = 0;
1165 ath_rc_priv->
per[
i] = 0;
1176 hi = ath_rc_init_validrates(ath_rc_priv);
1178 hi = ath_rc_setvalid_rates(ath_rc_priv,
true);
1180 if (ath_rc_priv->
ht_cap & WLAN_RC_HT_FLAG)
1181 hthi = ath_rc_setvalid_rates(ath_rc_priv,
false);
1206 ath_rc_sort_validrates(ath_rc_priv);
1211 ath_dbg(common,
CONFIG,
"RC Initialized with capabilities: 0x%x\n",
1219 if (sta->
ht_cap.ht_supported) {
1221 if (sta->
ht_cap.mcs.rx_mask[1] && sta->
ht_cap.mcs.rx_mask[2])
1223 else if (sta->
ht_cap.mcs.rx_mask[1])
1269 if (!priv_sta || !ieee80211_is_data(
fc))
1280 ath_rc_tx_status(sc, ath_rc_priv, skb);
1283 if (conf_is_ht(&sc->
hw->conf) &&
1285 if (ieee80211_is_data_qos(
fc) &&
1289 qc = ieee80211_get_qos_ctl(hdr);
1292 if(ath_tx_aggr_check(sc, sta, tid))
1309 = (sband->
bitrates[
i].bitrate * 2) / 10;
1315 if (sta->
ht_cap.ht_supported) {
1316 for (i = 0, j = 0; i < 77; i++) {
1317 if (sta->
ht_cap.mcs.rx_mask[i/8] & (1<<(i%8)))
1326 sta->
ht_cap.ht_supported);
1328 ath_err(common,
"No rate table chosen\n");
1332 ath_rc_priv->
ht_cap = ath_rc_build_ht_caps(sc, sta);
1333 ath_rc_init(sc, priv_sta);
1344 ath_rc_priv->
ht_cap = ath_rc_build_ht_caps(sc, sta);
1345 ath_rc_init(sc, priv_sta);
1348 "Operating HT Bandwidth changed to: %d\n",
1349 sc->
hw->conf.channel_type);
1353 #ifdef CONFIG_ATH9K_DEBUGFS
1355 static ssize_t read_file_rcstat(
struct file *
file,
char __user *user_buf,
1356 size_t count, loff_t *ppos)
1360 unsigned int len = 0,
max;
1372 len +=
sprintf(buf,
"%6s %6s %6s "
1373 "%10s %10s %10s %10s\n",
1374 "HT",
"MCS",
"Rate",
1375 "Success",
"Retries",
"XRetries",
"PER");
1383 int used_mcs = 0, used_htmode = 0;
1390 used_htmode =
snprintf(htmode, 5,
"HT40");
1392 used_htmode =
snprintf(htmode, 5,
"HT20");
1394 used_htmode =
snprintf(htmode, 5,
"????");
1397 mcs[used_mcs] =
'\0';
1398 htmode[used_htmode] =
'\0';
1402 "%10u %10u %10u %10u\n",
1406 (ratekbps % 1000) / 100,
1422 .
read = read_file_rcstat,
1427 static void ath_rate_add_sta_debugfs(
void *priv,
void *priv_sta,
1441 static void ath_rate_free(
void *priv)
1454 "Unable to allocate private rc structure\n");
1461 static void ath_rate_free_sta(
void *priv,
struct ieee80211_sta *sta,
1470 .name =
"ath9k_rate_control",
1472 .get_rate = ath_get_rate,
1473 .rate_init = ath_rate_init,
1474 .rate_update = ath_rate_update,
1475 .alloc = ath_rate_alloc,
1476 .free = ath_rate_free,
1477 .alloc_sta = ath_rate_alloc_sta,
1478 .free_sta = ath_rate_free_sta,
1479 #ifdef CONFIG_ATH9K_DEBUGFS
1480 .add_sta_debugfs = ath_rate_add_sta_debugfs,