18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org> 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify 58c2ecf20Sopenharmony_ci * it under the terms of the GNU General Public License version 2 as 68c2ecf20Sopenharmony_ci * published by the Free Software Foundation. 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Based on minstrel.c: 98c2ecf20Sopenharmony_ci * Copyright (C) 2005-2007 Derek Smithies <derek@indranet.co.nz> 108c2ecf20Sopenharmony_ci * Sponsored by Indranet Technologies Ltd 118c2ecf20Sopenharmony_ci * 128c2ecf20Sopenharmony_ci * Based on sample.c: 138c2ecf20Sopenharmony_ci * Copyright (c) 2005 John Bicket 148c2ecf20Sopenharmony_ci * All rights reserved. 158c2ecf20Sopenharmony_ci * 168c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without 178c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions 188c2ecf20Sopenharmony_ci * are met: 198c2ecf20Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright 208c2ecf20Sopenharmony_ci * notice, this list of conditions and the following disclaimer, 218c2ecf20Sopenharmony_ci * without modification. 228c2ecf20Sopenharmony_ci * 2. Redistributions in binary form must reproduce at minimum a disclaimer 238c2ecf20Sopenharmony_ci * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 248c2ecf20Sopenharmony_ci * redistribution must be conditioned upon including a substantially 258c2ecf20Sopenharmony_ci * similar Disclaimer requirement for further binary redistribution. 268c2ecf20Sopenharmony_ci * 3. Neither the names of the above-listed copyright holders nor the names 278c2ecf20Sopenharmony_ci * of any contributors may be used to endorse or promote products derived 288c2ecf20Sopenharmony_ci * from this software without specific prior written permission. 298c2ecf20Sopenharmony_ci * 308c2ecf20Sopenharmony_ci * Alternatively, this software may be distributed under the terms of the 318c2ecf20Sopenharmony_ci * GNU General Public License ("GPL") version 2 as published by the Free 328c2ecf20Sopenharmony_ci * Software Foundation. 338c2ecf20Sopenharmony_ci * 348c2ecf20Sopenharmony_ci * NO WARRANTY 358c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 368c2ecf20Sopenharmony_ci * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 378c2ecf20Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 388c2ecf20Sopenharmony_ci * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 398c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 408c2ecf20Sopenharmony_ci * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 418c2ecf20Sopenharmony_ci * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 428c2ecf20Sopenharmony_ci * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 438c2ecf20Sopenharmony_ci * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 448c2ecf20Sopenharmony_ci * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 458c2ecf20Sopenharmony_ci * THE POSSIBILITY OF SUCH DAMAGES. 468c2ecf20Sopenharmony_ci */ 478c2ecf20Sopenharmony_ci#include <linux/netdevice.h> 488c2ecf20Sopenharmony_ci#include <linux/types.h> 498c2ecf20Sopenharmony_ci#include <linux/skbuff.h> 508c2ecf20Sopenharmony_ci#include <linux/debugfs.h> 518c2ecf20Sopenharmony_ci#include <linux/random.h> 528c2ecf20Sopenharmony_ci#include <linux/ieee80211.h> 538c2ecf20Sopenharmony_ci#include <linux/slab.h> 548c2ecf20Sopenharmony_ci#include <net/mac80211.h> 558c2ecf20Sopenharmony_ci#include "rate.h" 568c2ecf20Sopenharmony_ci#include "rc80211_minstrel.h" 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci#define SAMPLE_TBL(_mi, _idx, _col) \ 598c2ecf20Sopenharmony_ci _mi->sample_table[(_idx * SAMPLE_COLUMNS) + _col] 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci/* convert mac80211 rate index to local array index */ 628c2ecf20Sopenharmony_cistatic inline int 638c2ecf20Sopenharmony_cirix_to_ndx(struct minstrel_sta_info *mi, int rix) 648c2ecf20Sopenharmony_ci{ 658c2ecf20Sopenharmony_ci int i = rix; 668c2ecf20Sopenharmony_ci for (i = rix; i >= 0; i--) 678c2ecf20Sopenharmony_ci if (mi->r[i].rix == rix) 688c2ecf20Sopenharmony_ci break; 698c2ecf20Sopenharmony_ci return i; 708c2ecf20Sopenharmony_ci} 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci/* return current EMWA throughput */ 738c2ecf20Sopenharmony_ciint minstrel_get_tp_avg(struct minstrel_rate *mr, int prob_avg) 748c2ecf20Sopenharmony_ci{ 758c2ecf20Sopenharmony_ci int usecs; 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci usecs = mr->perfect_tx_time; 788c2ecf20Sopenharmony_ci if (!usecs) 798c2ecf20Sopenharmony_ci usecs = 1000000; 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci /* reset thr. below 10% success */ 828c2ecf20Sopenharmony_ci if (mr->stats.prob_avg < MINSTREL_FRAC(10, 100)) 838c2ecf20Sopenharmony_ci return 0; 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci if (prob_avg > MINSTREL_FRAC(90, 100)) 868c2ecf20Sopenharmony_ci return MINSTREL_TRUNC(100000 * (MINSTREL_FRAC(90, 100) / usecs)); 878c2ecf20Sopenharmony_ci else 888c2ecf20Sopenharmony_ci return MINSTREL_TRUNC(100000 * (prob_avg / usecs)); 898c2ecf20Sopenharmony_ci} 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci/* find & sort topmost throughput rates */ 928c2ecf20Sopenharmony_cistatic inline void 938c2ecf20Sopenharmony_ciminstrel_sort_best_tp_rates(struct minstrel_sta_info *mi, int i, u8 *tp_list) 948c2ecf20Sopenharmony_ci{ 958c2ecf20Sopenharmony_ci int j; 968c2ecf20Sopenharmony_ci struct minstrel_rate_stats *tmp_mrs; 978c2ecf20Sopenharmony_ci struct minstrel_rate_stats *cur_mrs = &mi->r[i].stats; 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci for (j = MAX_THR_RATES; j > 0; --j) { 1008c2ecf20Sopenharmony_ci tmp_mrs = &mi->r[tp_list[j - 1]].stats; 1018c2ecf20Sopenharmony_ci if (minstrel_get_tp_avg(&mi->r[i], cur_mrs->prob_avg) <= 1028c2ecf20Sopenharmony_ci minstrel_get_tp_avg(&mi->r[tp_list[j - 1]], tmp_mrs->prob_avg)) 1038c2ecf20Sopenharmony_ci break; 1048c2ecf20Sopenharmony_ci } 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci if (j < MAX_THR_RATES - 1) 1078c2ecf20Sopenharmony_ci memmove(&tp_list[j + 1], &tp_list[j], MAX_THR_RATES - (j + 1)); 1088c2ecf20Sopenharmony_ci if (j < MAX_THR_RATES) 1098c2ecf20Sopenharmony_ci tp_list[j] = i; 1108c2ecf20Sopenharmony_ci} 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_cistatic void 1138c2ecf20Sopenharmony_ciminstrel_set_rate(struct minstrel_sta_info *mi, struct ieee80211_sta_rates *ratetbl, 1148c2ecf20Sopenharmony_ci int offset, int idx) 1158c2ecf20Sopenharmony_ci{ 1168c2ecf20Sopenharmony_ci struct minstrel_rate *r = &mi->r[idx]; 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci ratetbl->rate[offset].idx = r->rix; 1198c2ecf20Sopenharmony_ci ratetbl->rate[offset].count = r->adjusted_retry_count; 1208c2ecf20Sopenharmony_ci ratetbl->rate[offset].count_cts = r->retry_count_cts; 1218c2ecf20Sopenharmony_ci ratetbl->rate[offset].count_rts = r->stats.retry_count_rtscts; 1228c2ecf20Sopenharmony_ci} 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_cistatic void 1258c2ecf20Sopenharmony_ciminstrel_update_rates(struct minstrel_priv *mp, struct minstrel_sta_info *mi) 1268c2ecf20Sopenharmony_ci{ 1278c2ecf20Sopenharmony_ci struct ieee80211_sta_rates *ratetbl; 1288c2ecf20Sopenharmony_ci int i = 0; 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci ratetbl = kzalloc(sizeof(*ratetbl), GFP_ATOMIC); 1318c2ecf20Sopenharmony_ci if (!ratetbl) 1328c2ecf20Sopenharmony_ci return; 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci /* Start with max_tp_rate */ 1358c2ecf20Sopenharmony_ci minstrel_set_rate(mi, ratetbl, i++, mi->max_tp_rate[0]); 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci if (mp->hw->max_rates >= 3) { 1388c2ecf20Sopenharmony_ci /* At least 3 tx rates supported, use max_tp_rate2 next */ 1398c2ecf20Sopenharmony_ci minstrel_set_rate(mi, ratetbl, i++, mi->max_tp_rate[1]); 1408c2ecf20Sopenharmony_ci } 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci if (mp->hw->max_rates >= 2) { 1438c2ecf20Sopenharmony_ci /* At least 2 tx rates supported, use max_prob_rate next */ 1448c2ecf20Sopenharmony_ci minstrel_set_rate(mi, ratetbl, i++, mi->max_prob_rate); 1458c2ecf20Sopenharmony_ci } 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci /* Use lowest rate last */ 1488c2ecf20Sopenharmony_ci ratetbl->rate[i].idx = mi->lowest_rix; 1498c2ecf20Sopenharmony_ci ratetbl->rate[i].count = mp->max_retry; 1508c2ecf20Sopenharmony_ci ratetbl->rate[i].count_cts = mp->max_retry; 1518c2ecf20Sopenharmony_ci ratetbl->rate[i].count_rts = mp->max_retry; 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci rate_control_set_rates(mp->hw, mi->sta, ratetbl); 1548c2ecf20Sopenharmony_ci} 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci/* 1578c2ecf20Sopenharmony_ci* Recalculate statistics and counters of a given rate 1588c2ecf20Sopenharmony_ci*/ 1598c2ecf20Sopenharmony_civoid 1608c2ecf20Sopenharmony_ciminstrel_calc_rate_stats(struct minstrel_priv *mp, 1618c2ecf20Sopenharmony_ci struct minstrel_rate_stats *mrs) 1628c2ecf20Sopenharmony_ci{ 1638c2ecf20Sopenharmony_ci unsigned int cur_prob; 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci if (unlikely(mrs->attempts > 0)) { 1668c2ecf20Sopenharmony_ci mrs->sample_skipped = 0; 1678c2ecf20Sopenharmony_ci cur_prob = MINSTREL_FRAC(mrs->success, mrs->attempts); 1688c2ecf20Sopenharmony_ci if (mp->new_avg) { 1698c2ecf20Sopenharmony_ci minstrel_filter_avg_add(&mrs->prob_avg, 1708c2ecf20Sopenharmony_ci &mrs->prob_avg_1, cur_prob); 1718c2ecf20Sopenharmony_ci } else if (unlikely(!mrs->att_hist)) { 1728c2ecf20Sopenharmony_ci mrs->prob_avg = cur_prob; 1738c2ecf20Sopenharmony_ci } else { 1748c2ecf20Sopenharmony_ci /*update exponential weighted moving avarage */ 1758c2ecf20Sopenharmony_ci mrs->prob_avg = minstrel_ewma(mrs->prob_avg, 1768c2ecf20Sopenharmony_ci cur_prob, 1778c2ecf20Sopenharmony_ci EWMA_LEVEL); 1788c2ecf20Sopenharmony_ci } 1798c2ecf20Sopenharmony_ci mrs->att_hist += mrs->attempts; 1808c2ecf20Sopenharmony_ci mrs->succ_hist += mrs->success; 1818c2ecf20Sopenharmony_ci } else { 1828c2ecf20Sopenharmony_ci mrs->sample_skipped++; 1838c2ecf20Sopenharmony_ci } 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci mrs->last_success = mrs->success; 1868c2ecf20Sopenharmony_ci mrs->last_attempts = mrs->attempts; 1878c2ecf20Sopenharmony_ci mrs->success = 0; 1888c2ecf20Sopenharmony_ci mrs->attempts = 0; 1898c2ecf20Sopenharmony_ci} 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_cistatic void 1928c2ecf20Sopenharmony_ciminstrel_update_stats(struct minstrel_priv *mp, struct minstrel_sta_info *mi) 1938c2ecf20Sopenharmony_ci{ 1948c2ecf20Sopenharmony_ci u8 tmp_tp_rate[MAX_THR_RATES]; 1958c2ecf20Sopenharmony_ci u8 tmp_prob_rate = 0; 1968c2ecf20Sopenharmony_ci int i, tmp_cur_tp, tmp_prob_tp; 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci for (i = 0; i < MAX_THR_RATES; i++) 1998c2ecf20Sopenharmony_ci tmp_tp_rate[i] = 0; 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci for (i = 0; i < mi->n_rates; i++) { 2028c2ecf20Sopenharmony_ci struct minstrel_rate *mr = &mi->r[i]; 2038c2ecf20Sopenharmony_ci struct minstrel_rate_stats *mrs = &mi->r[i].stats; 2048c2ecf20Sopenharmony_ci struct minstrel_rate_stats *tmp_mrs = &mi->r[tmp_prob_rate].stats; 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci /* Update statistics of success probability per rate */ 2078c2ecf20Sopenharmony_ci minstrel_calc_rate_stats(mp, mrs); 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci /* Sample less often below the 10% chance of success. 2108c2ecf20Sopenharmony_ci * Sample less often above the 95% chance of success. */ 2118c2ecf20Sopenharmony_ci if (mrs->prob_avg > MINSTREL_FRAC(95, 100) || 2128c2ecf20Sopenharmony_ci mrs->prob_avg < MINSTREL_FRAC(10, 100)) { 2138c2ecf20Sopenharmony_ci mr->adjusted_retry_count = mrs->retry_count >> 1; 2148c2ecf20Sopenharmony_ci if (mr->adjusted_retry_count > 2) 2158c2ecf20Sopenharmony_ci mr->adjusted_retry_count = 2; 2168c2ecf20Sopenharmony_ci mr->sample_limit = 4; 2178c2ecf20Sopenharmony_ci } else { 2188c2ecf20Sopenharmony_ci mr->sample_limit = -1; 2198c2ecf20Sopenharmony_ci mr->adjusted_retry_count = mrs->retry_count; 2208c2ecf20Sopenharmony_ci } 2218c2ecf20Sopenharmony_ci if (!mr->adjusted_retry_count) 2228c2ecf20Sopenharmony_ci mr->adjusted_retry_count = 2; 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci minstrel_sort_best_tp_rates(mi, i, tmp_tp_rate); 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_ci /* To determine the most robust rate (max_prob_rate) used at 2278c2ecf20Sopenharmony_ci * 3rd mmr stage we distinct between two cases: 2288c2ecf20Sopenharmony_ci * (1) if any success probabilitiy >= 95%, out of those rates 2298c2ecf20Sopenharmony_ci * choose the maximum throughput rate as max_prob_rate 2308c2ecf20Sopenharmony_ci * (2) if all success probabilities < 95%, the rate with 2318c2ecf20Sopenharmony_ci * highest success probability is chosen as max_prob_rate */ 2328c2ecf20Sopenharmony_ci if (mrs->prob_avg >= MINSTREL_FRAC(95, 100)) { 2338c2ecf20Sopenharmony_ci tmp_cur_tp = minstrel_get_tp_avg(mr, mrs->prob_avg); 2348c2ecf20Sopenharmony_ci tmp_prob_tp = minstrel_get_tp_avg(&mi->r[tmp_prob_rate], 2358c2ecf20Sopenharmony_ci tmp_mrs->prob_avg); 2368c2ecf20Sopenharmony_ci if (tmp_cur_tp >= tmp_prob_tp) 2378c2ecf20Sopenharmony_ci tmp_prob_rate = i; 2388c2ecf20Sopenharmony_ci } else { 2398c2ecf20Sopenharmony_ci if (mrs->prob_avg >= tmp_mrs->prob_avg) 2408c2ecf20Sopenharmony_ci tmp_prob_rate = i; 2418c2ecf20Sopenharmony_ci } 2428c2ecf20Sopenharmony_ci } 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_ci /* Assign the new rate set */ 2458c2ecf20Sopenharmony_ci memcpy(mi->max_tp_rate, tmp_tp_rate, sizeof(mi->max_tp_rate)); 2468c2ecf20Sopenharmony_ci mi->max_prob_rate = tmp_prob_rate; 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci#ifdef CONFIG_MAC80211_DEBUGFS 2498c2ecf20Sopenharmony_ci /* use fixed index if set */ 2508c2ecf20Sopenharmony_ci if (mp->fixed_rate_idx != -1) { 2518c2ecf20Sopenharmony_ci mi->max_tp_rate[0] = mp->fixed_rate_idx; 2528c2ecf20Sopenharmony_ci mi->max_tp_rate[1] = mp->fixed_rate_idx; 2538c2ecf20Sopenharmony_ci mi->max_prob_rate = mp->fixed_rate_idx; 2548c2ecf20Sopenharmony_ci } 2558c2ecf20Sopenharmony_ci#endif 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_ci /* Reset update timer */ 2588c2ecf20Sopenharmony_ci mi->last_stats_update = jiffies; 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ci minstrel_update_rates(mp, mi); 2618c2ecf20Sopenharmony_ci} 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_cistatic void 2648c2ecf20Sopenharmony_ciminstrel_tx_status(void *priv, struct ieee80211_supported_band *sband, 2658c2ecf20Sopenharmony_ci void *priv_sta, struct ieee80211_tx_status *st) 2668c2ecf20Sopenharmony_ci{ 2678c2ecf20Sopenharmony_ci struct ieee80211_tx_info *info = st->info; 2688c2ecf20Sopenharmony_ci struct minstrel_priv *mp = priv; 2698c2ecf20Sopenharmony_ci struct minstrel_sta_info *mi = priv_sta; 2708c2ecf20Sopenharmony_ci struct ieee80211_tx_rate *ar = info->status.rates; 2718c2ecf20Sopenharmony_ci int i, ndx; 2728c2ecf20Sopenharmony_ci int success; 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ci success = !!(info->flags & IEEE80211_TX_STAT_ACK); 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_ci for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) { 2778c2ecf20Sopenharmony_ci if (ar[i].idx < 0 || !ar[i].count) 2788c2ecf20Sopenharmony_ci break; 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_ci ndx = rix_to_ndx(mi, ar[i].idx); 2818c2ecf20Sopenharmony_ci if (ndx < 0) 2828c2ecf20Sopenharmony_ci continue; 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci mi->r[ndx].stats.attempts += ar[i].count; 2858c2ecf20Sopenharmony_ci 2868c2ecf20Sopenharmony_ci if ((i != IEEE80211_TX_MAX_RATES - 1) && (ar[i + 1].idx < 0)) 2878c2ecf20Sopenharmony_ci mi->r[ndx].stats.success += success; 2888c2ecf20Sopenharmony_ci } 2898c2ecf20Sopenharmony_ci 2908c2ecf20Sopenharmony_ci if (time_after(jiffies, mi->last_stats_update + 2918c2ecf20Sopenharmony_ci mp->update_interval / (mp->new_avg ? 2 : 1))) 2928c2ecf20Sopenharmony_ci minstrel_update_stats(mp, mi); 2938c2ecf20Sopenharmony_ci} 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ci 2968c2ecf20Sopenharmony_cistatic inline unsigned int 2978c2ecf20Sopenharmony_ciminstrel_get_retry_count(struct minstrel_rate *mr, 2988c2ecf20Sopenharmony_ci struct ieee80211_tx_info *info) 2998c2ecf20Sopenharmony_ci{ 3008c2ecf20Sopenharmony_ci u8 retry = mr->adjusted_retry_count; 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_ci if (info->control.use_rts) 3038c2ecf20Sopenharmony_ci retry = max_t(u8, 2, min(mr->stats.retry_count_rtscts, retry)); 3048c2ecf20Sopenharmony_ci else if (info->control.use_cts_prot) 3058c2ecf20Sopenharmony_ci retry = max_t(u8, 2, min(mr->retry_count_cts, retry)); 3068c2ecf20Sopenharmony_ci return retry; 3078c2ecf20Sopenharmony_ci} 3088c2ecf20Sopenharmony_ci 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_cistatic int 3118c2ecf20Sopenharmony_ciminstrel_get_next_sample(struct minstrel_sta_info *mi) 3128c2ecf20Sopenharmony_ci{ 3138c2ecf20Sopenharmony_ci unsigned int sample_ndx; 3148c2ecf20Sopenharmony_ci sample_ndx = SAMPLE_TBL(mi, mi->sample_row, mi->sample_column); 3158c2ecf20Sopenharmony_ci mi->sample_row++; 3168c2ecf20Sopenharmony_ci if ((int) mi->sample_row >= mi->n_rates) { 3178c2ecf20Sopenharmony_ci mi->sample_row = 0; 3188c2ecf20Sopenharmony_ci mi->sample_column++; 3198c2ecf20Sopenharmony_ci if (mi->sample_column >= SAMPLE_COLUMNS) 3208c2ecf20Sopenharmony_ci mi->sample_column = 0; 3218c2ecf20Sopenharmony_ci } 3228c2ecf20Sopenharmony_ci return sample_ndx; 3238c2ecf20Sopenharmony_ci} 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_cistatic void 3268c2ecf20Sopenharmony_ciminstrel_get_rate(void *priv, struct ieee80211_sta *sta, 3278c2ecf20Sopenharmony_ci void *priv_sta, struct ieee80211_tx_rate_control *txrc) 3288c2ecf20Sopenharmony_ci{ 3298c2ecf20Sopenharmony_ci struct sk_buff *skb = txrc->skb; 3308c2ecf20Sopenharmony_ci struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 3318c2ecf20Sopenharmony_ci struct minstrel_sta_info *mi = priv_sta; 3328c2ecf20Sopenharmony_ci struct minstrel_priv *mp = priv; 3338c2ecf20Sopenharmony_ci struct ieee80211_tx_rate *rate = &info->control.rates[0]; 3348c2ecf20Sopenharmony_ci struct minstrel_rate *msr, *mr; 3358c2ecf20Sopenharmony_ci unsigned int ndx; 3368c2ecf20Sopenharmony_ci bool mrr_capable; 3378c2ecf20Sopenharmony_ci bool prev_sample; 3388c2ecf20Sopenharmony_ci int delta; 3398c2ecf20Sopenharmony_ci int sampling_ratio; 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_ci /* check multi-rate-retry capabilities & adjust lookaround_rate */ 3428c2ecf20Sopenharmony_ci mrr_capable = mp->has_mrr && 3438c2ecf20Sopenharmony_ci !txrc->rts && 3448c2ecf20Sopenharmony_ci !txrc->bss_conf->use_cts_prot; 3458c2ecf20Sopenharmony_ci if (mrr_capable) 3468c2ecf20Sopenharmony_ci sampling_ratio = mp->lookaround_rate_mrr; 3478c2ecf20Sopenharmony_ci else 3488c2ecf20Sopenharmony_ci sampling_ratio = mp->lookaround_rate; 3498c2ecf20Sopenharmony_ci 3508c2ecf20Sopenharmony_ci /* increase sum packet counter */ 3518c2ecf20Sopenharmony_ci mi->total_packets++; 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ci#ifdef CONFIG_MAC80211_DEBUGFS 3548c2ecf20Sopenharmony_ci if (mp->fixed_rate_idx != -1) 3558c2ecf20Sopenharmony_ci return; 3568c2ecf20Sopenharmony_ci#endif 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci /* Don't use EAPOL frames for sampling on non-mrr hw */ 3598c2ecf20Sopenharmony_ci if (mp->hw->max_rates == 1 && 3608c2ecf20Sopenharmony_ci (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO)) 3618c2ecf20Sopenharmony_ci return; 3628c2ecf20Sopenharmony_ci 3638c2ecf20Sopenharmony_ci delta = (mi->total_packets * sampling_ratio / 100) - 3648c2ecf20Sopenharmony_ci mi->sample_packets; 3658c2ecf20Sopenharmony_ci 3668c2ecf20Sopenharmony_ci /* delta < 0: no sampling required */ 3678c2ecf20Sopenharmony_ci prev_sample = mi->prev_sample; 3688c2ecf20Sopenharmony_ci mi->prev_sample = false; 3698c2ecf20Sopenharmony_ci if (delta < 0 || (!mrr_capable && prev_sample)) 3708c2ecf20Sopenharmony_ci return; 3718c2ecf20Sopenharmony_ci 3728c2ecf20Sopenharmony_ci if (mi->total_packets >= 10000) { 3738c2ecf20Sopenharmony_ci mi->sample_packets = 0; 3748c2ecf20Sopenharmony_ci mi->total_packets = 0; 3758c2ecf20Sopenharmony_ci } else if (delta > mi->n_rates * 2) { 3768c2ecf20Sopenharmony_ci /* With multi-rate retry, not every planned sample 3778c2ecf20Sopenharmony_ci * attempt actually gets used, due to the way the retry 3788c2ecf20Sopenharmony_ci * chain is set up - [max_tp,sample,prob,lowest] for 3798c2ecf20Sopenharmony_ci * sample_rate < max_tp. 3808c2ecf20Sopenharmony_ci * 3818c2ecf20Sopenharmony_ci * If there's too much sampling backlog and the link 3828c2ecf20Sopenharmony_ci * starts getting worse, minstrel would start bursting 3838c2ecf20Sopenharmony_ci * out lots of sampling frames, which would result 3848c2ecf20Sopenharmony_ci * in a large throughput loss. */ 3858c2ecf20Sopenharmony_ci mi->sample_packets += (delta - mi->n_rates * 2); 3868c2ecf20Sopenharmony_ci } 3878c2ecf20Sopenharmony_ci 3888c2ecf20Sopenharmony_ci /* get next random rate sample */ 3898c2ecf20Sopenharmony_ci ndx = minstrel_get_next_sample(mi); 3908c2ecf20Sopenharmony_ci msr = &mi->r[ndx]; 3918c2ecf20Sopenharmony_ci mr = &mi->r[mi->max_tp_rate[0]]; 3928c2ecf20Sopenharmony_ci 3938c2ecf20Sopenharmony_ci /* Decide if direct ( 1st mrr stage) or indirect (2nd mrr stage) 3948c2ecf20Sopenharmony_ci * rate sampling method should be used. 3958c2ecf20Sopenharmony_ci * Respect such rates that are not sampled for 20 interations. 3968c2ecf20Sopenharmony_ci */ 3978c2ecf20Sopenharmony_ci if (msr->perfect_tx_time < mr->perfect_tx_time || 3988c2ecf20Sopenharmony_ci msr->stats.sample_skipped >= 20) { 3998c2ecf20Sopenharmony_ci if (!msr->sample_limit) 4008c2ecf20Sopenharmony_ci return; 4018c2ecf20Sopenharmony_ci 4028c2ecf20Sopenharmony_ci mi->sample_packets++; 4038c2ecf20Sopenharmony_ci if (msr->sample_limit > 0) 4048c2ecf20Sopenharmony_ci msr->sample_limit--; 4058c2ecf20Sopenharmony_ci } 4068c2ecf20Sopenharmony_ci 4078c2ecf20Sopenharmony_ci /* If we're not using MRR and the sampling rate already 4088c2ecf20Sopenharmony_ci * has a probability of >95%, we shouldn't be attempting 4098c2ecf20Sopenharmony_ci * to use it, as this only wastes precious airtime */ 4108c2ecf20Sopenharmony_ci if (!mrr_capable && 4118c2ecf20Sopenharmony_ci (mi->r[ndx].stats.prob_avg > MINSTREL_FRAC(95, 100))) 4128c2ecf20Sopenharmony_ci return; 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_ci mi->prev_sample = true; 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_ci rate->idx = mi->r[ndx].rix; 4178c2ecf20Sopenharmony_ci rate->count = minstrel_get_retry_count(&mi->r[ndx], info); 4188c2ecf20Sopenharmony_ci info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE; 4198c2ecf20Sopenharmony_ci} 4208c2ecf20Sopenharmony_ci 4218c2ecf20Sopenharmony_ci 4228c2ecf20Sopenharmony_cistatic void 4238c2ecf20Sopenharmony_cicalc_rate_durations(enum nl80211_band band, 4248c2ecf20Sopenharmony_ci struct minstrel_rate *d, 4258c2ecf20Sopenharmony_ci struct ieee80211_rate *rate, 4268c2ecf20Sopenharmony_ci struct cfg80211_chan_def *chandef) 4278c2ecf20Sopenharmony_ci{ 4288c2ecf20Sopenharmony_ci int erp = !!(rate->flags & IEEE80211_RATE_ERP_G); 4298c2ecf20Sopenharmony_ci int shift = ieee80211_chandef_get_shift(chandef); 4308c2ecf20Sopenharmony_ci 4318c2ecf20Sopenharmony_ci d->perfect_tx_time = ieee80211_frame_duration(band, 1200, 4328c2ecf20Sopenharmony_ci DIV_ROUND_UP(rate->bitrate, 1 << shift), erp, 1, 4338c2ecf20Sopenharmony_ci shift); 4348c2ecf20Sopenharmony_ci d->ack_time = ieee80211_frame_duration(band, 10, 4358c2ecf20Sopenharmony_ci DIV_ROUND_UP(rate->bitrate, 1 << shift), erp, 1, 4368c2ecf20Sopenharmony_ci shift); 4378c2ecf20Sopenharmony_ci} 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_cistatic void 4408c2ecf20Sopenharmony_ciinit_sample_table(struct minstrel_sta_info *mi) 4418c2ecf20Sopenharmony_ci{ 4428c2ecf20Sopenharmony_ci unsigned int i, col, new_idx; 4438c2ecf20Sopenharmony_ci u8 rnd[8]; 4448c2ecf20Sopenharmony_ci 4458c2ecf20Sopenharmony_ci mi->sample_column = 0; 4468c2ecf20Sopenharmony_ci mi->sample_row = 0; 4478c2ecf20Sopenharmony_ci memset(mi->sample_table, 0xff, SAMPLE_COLUMNS * mi->n_rates); 4488c2ecf20Sopenharmony_ci 4498c2ecf20Sopenharmony_ci for (col = 0; col < SAMPLE_COLUMNS; col++) { 4508c2ecf20Sopenharmony_ci prandom_bytes(rnd, sizeof(rnd)); 4518c2ecf20Sopenharmony_ci for (i = 0; i < mi->n_rates; i++) { 4528c2ecf20Sopenharmony_ci new_idx = (i + rnd[i & 7]) % mi->n_rates; 4538c2ecf20Sopenharmony_ci while (SAMPLE_TBL(mi, new_idx, col) != 0xff) 4548c2ecf20Sopenharmony_ci new_idx = (new_idx + 1) % mi->n_rates; 4558c2ecf20Sopenharmony_ci 4568c2ecf20Sopenharmony_ci SAMPLE_TBL(mi, new_idx, col) = i; 4578c2ecf20Sopenharmony_ci } 4588c2ecf20Sopenharmony_ci } 4598c2ecf20Sopenharmony_ci} 4608c2ecf20Sopenharmony_ci 4618c2ecf20Sopenharmony_cistatic void 4628c2ecf20Sopenharmony_ciminstrel_rate_init(void *priv, struct ieee80211_supported_band *sband, 4638c2ecf20Sopenharmony_ci struct cfg80211_chan_def *chandef, 4648c2ecf20Sopenharmony_ci struct ieee80211_sta *sta, void *priv_sta) 4658c2ecf20Sopenharmony_ci{ 4668c2ecf20Sopenharmony_ci struct minstrel_sta_info *mi = priv_sta; 4678c2ecf20Sopenharmony_ci struct minstrel_priv *mp = priv; 4688c2ecf20Sopenharmony_ci struct ieee80211_rate *ctl_rate; 4698c2ecf20Sopenharmony_ci unsigned int i, n = 0; 4708c2ecf20Sopenharmony_ci unsigned int t_slot = 9; /* FIXME: get real slot time */ 4718c2ecf20Sopenharmony_ci u32 rate_flags; 4728c2ecf20Sopenharmony_ci 4738c2ecf20Sopenharmony_ci mi->sta = sta; 4748c2ecf20Sopenharmony_ci mi->lowest_rix = rate_lowest_index(sband, sta); 4758c2ecf20Sopenharmony_ci ctl_rate = &sband->bitrates[mi->lowest_rix]; 4768c2ecf20Sopenharmony_ci mi->sp_ack_dur = ieee80211_frame_duration(sband->band, 10, 4778c2ecf20Sopenharmony_ci ctl_rate->bitrate, 4788c2ecf20Sopenharmony_ci !!(ctl_rate->flags & IEEE80211_RATE_ERP_G), 1, 4798c2ecf20Sopenharmony_ci ieee80211_chandef_get_shift(chandef)); 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_ci rate_flags = ieee80211_chandef_rate_flags(&mp->hw->conf.chandef); 4828c2ecf20Sopenharmony_ci memset(mi->max_tp_rate, 0, sizeof(mi->max_tp_rate)); 4838c2ecf20Sopenharmony_ci mi->max_prob_rate = 0; 4848c2ecf20Sopenharmony_ci 4858c2ecf20Sopenharmony_ci for (i = 0; i < sband->n_bitrates; i++) { 4868c2ecf20Sopenharmony_ci struct minstrel_rate *mr = &mi->r[n]; 4878c2ecf20Sopenharmony_ci struct minstrel_rate_stats *mrs = &mi->r[n].stats; 4888c2ecf20Sopenharmony_ci unsigned int tx_time = 0, tx_time_cts = 0, tx_time_rtscts = 0; 4898c2ecf20Sopenharmony_ci unsigned int tx_time_single; 4908c2ecf20Sopenharmony_ci unsigned int cw = mp->cw_min; 4918c2ecf20Sopenharmony_ci int shift; 4928c2ecf20Sopenharmony_ci 4938c2ecf20Sopenharmony_ci if (!rate_supported(sta, sband->band, i)) 4948c2ecf20Sopenharmony_ci continue; 4958c2ecf20Sopenharmony_ci if ((rate_flags & sband->bitrates[i].flags) != rate_flags) 4968c2ecf20Sopenharmony_ci continue; 4978c2ecf20Sopenharmony_ci 4988c2ecf20Sopenharmony_ci n++; 4998c2ecf20Sopenharmony_ci memset(mr, 0, sizeof(*mr)); 5008c2ecf20Sopenharmony_ci memset(mrs, 0, sizeof(*mrs)); 5018c2ecf20Sopenharmony_ci 5028c2ecf20Sopenharmony_ci mr->rix = i; 5038c2ecf20Sopenharmony_ci shift = ieee80211_chandef_get_shift(chandef); 5048c2ecf20Sopenharmony_ci mr->bitrate = DIV_ROUND_UP(sband->bitrates[i].bitrate, 5058c2ecf20Sopenharmony_ci (1 << shift) * 5); 5068c2ecf20Sopenharmony_ci calc_rate_durations(sband->band, mr, &sband->bitrates[i], 5078c2ecf20Sopenharmony_ci chandef); 5088c2ecf20Sopenharmony_ci 5098c2ecf20Sopenharmony_ci /* calculate maximum number of retransmissions before 5108c2ecf20Sopenharmony_ci * fallback (based on maximum segment size) */ 5118c2ecf20Sopenharmony_ci mr->sample_limit = -1; 5128c2ecf20Sopenharmony_ci mrs->retry_count = 1; 5138c2ecf20Sopenharmony_ci mr->retry_count_cts = 1; 5148c2ecf20Sopenharmony_ci mrs->retry_count_rtscts = 1; 5158c2ecf20Sopenharmony_ci tx_time = mr->perfect_tx_time + mi->sp_ack_dur; 5168c2ecf20Sopenharmony_ci do { 5178c2ecf20Sopenharmony_ci /* add one retransmission */ 5188c2ecf20Sopenharmony_ci tx_time_single = mr->ack_time + mr->perfect_tx_time; 5198c2ecf20Sopenharmony_ci 5208c2ecf20Sopenharmony_ci /* contention window */ 5218c2ecf20Sopenharmony_ci tx_time_single += (t_slot * cw) >> 1; 5228c2ecf20Sopenharmony_ci cw = min((cw << 1) | 1, mp->cw_max); 5238c2ecf20Sopenharmony_ci 5248c2ecf20Sopenharmony_ci tx_time += tx_time_single; 5258c2ecf20Sopenharmony_ci tx_time_cts += tx_time_single + mi->sp_ack_dur; 5268c2ecf20Sopenharmony_ci tx_time_rtscts += tx_time_single + 2 * mi->sp_ack_dur; 5278c2ecf20Sopenharmony_ci if ((tx_time_cts < mp->segment_size) && 5288c2ecf20Sopenharmony_ci (mr->retry_count_cts < mp->max_retry)) 5298c2ecf20Sopenharmony_ci mr->retry_count_cts++; 5308c2ecf20Sopenharmony_ci if ((tx_time_rtscts < mp->segment_size) && 5318c2ecf20Sopenharmony_ci (mrs->retry_count_rtscts < mp->max_retry)) 5328c2ecf20Sopenharmony_ci mrs->retry_count_rtscts++; 5338c2ecf20Sopenharmony_ci } while ((tx_time < mp->segment_size) && 5348c2ecf20Sopenharmony_ci (++mr->stats.retry_count < mp->max_retry)); 5358c2ecf20Sopenharmony_ci mr->adjusted_retry_count = mrs->retry_count; 5368c2ecf20Sopenharmony_ci if (!(sband->bitrates[i].flags & IEEE80211_RATE_ERP_G)) 5378c2ecf20Sopenharmony_ci mr->retry_count_cts = mrs->retry_count; 5388c2ecf20Sopenharmony_ci } 5398c2ecf20Sopenharmony_ci 5408c2ecf20Sopenharmony_ci for (i = n; i < sband->n_bitrates; i++) { 5418c2ecf20Sopenharmony_ci struct minstrel_rate *mr = &mi->r[i]; 5428c2ecf20Sopenharmony_ci mr->rix = -1; 5438c2ecf20Sopenharmony_ci } 5448c2ecf20Sopenharmony_ci 5458c2ecf20Sopenharmony_ci mi->n_rates = n; 5468c2ecf20Sopenharmony_ci mi->last_stats_update = jiffies; 5478c2ecf20Sopenharmony_ci 5488c2ecf20Sopenharmony_ci init_sample_table(mi); 5498c2ecf20Sopenharmony_ci minstrel_update_rates(mp, mi); 5508c2ecf20Sopenharmony_ci} 5518c2ecf20Sopenharmony_ci 5528c2ecf20Sopenharmony_cistatic u32 minstrel_get_expected_throughput(void *priv_sta) 5538c2ecf20Sopenharmony_ci{ 5548c2ecf20Sopenharmony_ci struct minstrel_sta_info *mi = priv_sta; 5558c2ecf20Sopenharmony_ci struct minstrel_rate_stats *tmp_mrs; 5568c2ecf20Sopenharmony_ci int idx = mi->max_tp_rate[0]; 5578c2ecf20Sopenharmony_ci int tmp_cur_tp; 5588c2ecf20Sopenharmony_ci 5598c2ecf20Sopenharmony_ci /* convert pkt per sec in kbps (1200 is the average pkt size used for 5608c2ecf20Sopenharmony_ci * computing cur_tp 5618c2ecf20Sopenharmony_ci */ 5628c2ecf20Sopenharmony_ci tmp_mrs = &mi->r[idx].stats; 5638c2ecf20Sopenharmony_ci tmp_cur_tp = minstrel_get_tp_avg(&mi->r[idx], tmp_mrs->prob_avg) * 10; 5648c2ecf20Sopenharmony_ci tmp_cur_tp = tmp_cur_tp * 1200 * 8 / 1024; 5658c2ecf20Sopenharmony_ci 5668c2ecf20Sopenharmony_ci return tmp_cur_tp; 5678c2ecf20Sopenharmony_ci} 5688c2ecf20Sopenharmony_ci 5698c2ecf20Sopenharmony_ciconst struct rate_control_ops mac80211_minstrel = { 5708c2ecf20Sopenharmony_ci .tx_status_ext = minstrel_tx_status, 5718c2ecf20Sopenharmony_ci .get_rate = minstrel_get_rate, 5728c2ecf20Sopenharmony_ci .rate_init = minstrel_rate_init, 5738c2ecf20Sopenharmony_ci .get_expected_throughput = minstrel_get_expected_throughput, 5748c2ecf20Sopenharmony_ci}; 575