cfg80211 API for channels/bitrates, mac80211 and driver conversion
[safe/jmp/linux-2.6] / net / mac80211 / rc80211_pid_algo.c
index 3fac3a5..c5a607c 100644 (file)
@@ -12,7 +12,7 @@
 #include <linux/netdevice.h>
 #include <linux/types.h>
 #include <linux/skbuff.h>
-
+#include <linux/debugfs.h>
 #include <net/mac80211.h>
 #include "ieee80211_rate.h"
 
@@ -74,29 +74,27 @@ static int rate_control_pid_shift_adjust(struct rc_pid_rateinfo *r,
 {
        int i, j, k, tmp;
 
-       if (cur + adj < 0)
-               return 0;
-       if (cur + adj >= l)
-               return l - 1;
+       j = r[cur].rev_index;
+       i = j + adj;
 
-       i = r[cur + adj].rev_index;
+       if (i < 0)
+               return r[0].index;
+       if (i >= l - 1)
+               return r[l - 1].index;
 
-       j = r[cur].rev_index;
+       tmp = i;
 
        if (adj < 0) {
-                       tmp = i;
-                       for (k = j; k >= i; k--)
-                               if (r[k].diff <= r[j].diff)
-                                       tmp = k;
-                       return r[tmp].index;
-       } else if (adj > 0) {
-                       tmp = i;
-                       for (k = i + 1; k + i < l; k++)
-                               if (r[k].diff <= r[i].diff)
-                                       tmp = k;
-                       return r[tmp].index;
+               for (k = j; k >= i; k--)
+                       if (r[k].diff <= r[j].diff)
+                               tmp = k;
+       } else {
+               for (k = i + 1; k + i < l; k++)
+                       if (r[k].diff <= r[i].diff)
+                               tmp = k;
        }
-       return cur + adj;
+
+       return r[tmp].index;
 }
 
 static void rate_control_pid_adjust_rate(struct ieee80211_local *local,
@@ -104,27 +102,23 @@ static void rate_control_pid_adjust_rate(struct ieee80211_local *local,
                                         struct rc_pid_rateinfo *rinfo)
 {
        struct ieee80211_sub_if_data *sdata;
-       struct ieee80211_hw_mode *mode;
+       struct ieee80211_supported_band *sband;
        int newidx;
        int maxrate;
        int back = (adj > 0) ? 1 : -1;
 
        sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
-       if (sdata->bss && sdata->bss->force_unicast_rateidx > -1) {
-               /* forced unicast rate - do not change STA rate */
-               return;
-       }
 
-       mode = local->oper_hw_mode;
+       sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
        maxrate = sdata->bss ? sdata->bss->max_ratectrl_rateidx : -1;
 
-       newidx = rate_control_pid_shift_adjust(rinfo, adj, sta->txrate,
-                                              mode->num_rates);
+       newidx = rate_control_pid_shift_adjust(rinfo, adj, sta->txrate_idx,
+                                              sband->n_bitrates);
 
-       while (newidx != sta->txrate) {
-               if (rate_supported(sta, mode, newidx) &&
+       while (newidx != sta->txrate_idx) {
+               if (rate_supported(sta, sband->band, newidx) &&
                    (maxrate < 0 || newidx <= maxrate)) {
-                       sta->txrate = newidx;
+                       sta->txrate_idx = newidx;
                        break;
                }
 
@@ -134,24 +128,25 @@ static void rate_control_pid_adjust_rate(struct ieee80211_local *local,
 #ifdef CONFIG_MAC80211_DEBUGFS
        rate_control_pid_event_rate_change(
                &((struct rc_pid_sta_info *)sta->rate_ctrl_priv)->events,
-               newidx, mode->rates[newidx].rate);
+               newidx, sband->bitrates[newidx].bitrate);
 #endif
 }
 
 /* Normalize the failed frames per-rate differences. */
-static void rate_control_pid_normalize(struct rc_pid_rateinfo *r, int l)
+static void rate_control_pid_normalize(struct rc_pid_info *pinfo, int l)
 {
-       int i;
+       int i, norm_offset = pinfo->norm_offset;
+       struct rc_pid_rateinfo *r = pinfo->rinfo;
 
-       if (r[0].diff > RC_PID_NORM_OFFSET)
-               r[0].diff -= RC_PID_NORM_OFFSET;
-       else if (r[0].diff < -RC_PID_NORM_OFFSET)
-               r[0].diff += RC_PID_NORM_OFFSET;
+       if (r[0].diff > norm_offset)
+               r[0].diff -= norm_offset;
+       else if (r[0].diff < -norm_offset)
+               r[0].diff += norm_offset;
        for (i = 0; i < l - 1; i++)
-               if (r[i + 1].diff > r[i].diff + RC_PID_NORM_OFFSET)
-                       r[i + 1].diff -= RC_PID_NORM_OFFSET;
+               if (r[i + 1].diff > r[i].diff + norm_offset)
+                       r[i + 1].diff -= norm_offset;
                else if (r[i + 1].diff <= r[i].diff)
-                       r[i + 1].diff += RC_PID_NORM_OFFSET;
+                       r[i + 1].diff += norm_offset;
 }
 
 static void rate_control_pid_sample(struct rc_pid_info *pinfo,
@@ -160,21 +155,25 @@ static void rate_control_pid_sample(struct rc_pid_info *pinfo,
 {
        struct rc_pid_sta_info *spinfo = sta->rate_ctrl_priv;
        struct rc_pid_rateinfo *rinfo = pinfo->rinfo;
-       struct ieee80211_hw_mode *mode;
+       struct ieee80211_supported_band *sband;
        u32 pf;
        s32 err_avg;
-       s32 err_prop;
-       s32 err_int;
-       s32 err_der;
+       u32 err_prop;
+       u32 err_int;
+       u32 err_der;
        int adj, i, j, tmp;
+       unsigned long period;
 
-       mode = local->oper_hw_mode;
+       sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
        spinfo = sta->rate_ctrl_priv;
 
        /* In case nothing happened during the previous control interval, turn
         * the sharpening factor on. */
-       if (jiffies - spinfo->last_sample > 2 * RC_PID_INTERVAL)
-               spinfo->sharp_cnt = RC_PID_SHARPENING_DURATION;
+       period = (HZ * pinfo->sampling_period + 500) / 1000;
+       if (!period)
+               period = 1;
+       if (jiffies - spinfo->last_sample > 2 * period)
+               spinfo->sharp_cnt = pinfo->sharpen_duration;
 
        spinfo->last_sample = jiffies;
 
@@ -191,28 +190,28 @@ static void rate_control_pid_sample(struct rc_pid_info *pinfo,
        spinfo->tx_num_failed = 0;
 
        /* If we just switched rate, update the rate behaviour info. */
-       if (pinfo->oldrate != sta->txrate) {
+       if (pinfo->oldrate != sta->txrate_idx) {
 
                i = rinfo[pinfo->oldrate].rev_index;
-               j = rinfo[sta->txrate].rev_index;
+               j = rinfo[sta->txrate_idx].rev_index;
 
                tmp = (pf - spinfo->last_pf);
                tmp = RC_PID_DO_ARITH_RIGHT_SHIFT(tmp, RC_PID_ARITH_SHIFT);
 
                rinfo[j].diff = rinfo[i].diff + tmp;
-               pinfo->oldrate = sta->txrate;
+               pinfo->oldrate = sta->txrate_idx;
        }
-       rate_control_pid_normalize(rinfo, mode->num_rates);
+       rate_control_pid_normalize(pinfo, sband->n_bitrates);
 
        /* Compute the proportional, integral and derivative errors. */
-       err_prop = RC_PID_TARGET_PF - pf;
+       err_prop = (pinfo->target << RC_PID_ARITH_SHIFT) - pf;
 
-       err_avg = spinfo->err_avg_sc >> RC_PID_SMOOTHING_SHIFT;
+       err_avg = spinfo->err_avg_sc >> pinfo->smoothing_shift;
        spinfo->err_avg_sc = spinfo->err_avg_sc - err_avg + err_prop;
-       err_int = spinfo->err_avg_sc >> RC_PID_SMOOTHING_SHIFT;
+       err_int = spinfo->err_avg_sc >> pinfo->smoothing_shift;
 
-       err_der = pf - spinfo->last_pf
-                 * (1 + RC_PID_SHARPENING_FACTOR * spinfo->sharp_cnt);
+       err_der = (pf - spinfo->last_pf) *
+                 (1 + pinfo->sharpen_factor * spinfo->sharp_cnt);
        spinfo->last_pf = pf;
        if (spinfo->sharp_cnt)
                        spinfo->sharp_cnt--;
@@ -238,19 +237,30 @@ static void rate_control_pid_tx_status(void *priv, struct net_device *dev,
 {
        struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
+       struct ieee80211_sub_if_data *sdata;
        struct rc_pid_info *pinfo = priv;
        struct sta_info *sta;
        struct rc_pid_sta_info *spinfo;
+       unsigned long period;
+       struct ieee80211_supported_band *sband;
 
        sta = sta_info_get(local, hdr->addr1);
+       sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
 
        if (!sta)
                return;
 
+       /* Don't update the state if we're not controlling the rate. */
+       sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
+       if (sdata->bss && sdata->bss->force_unicast_rateidx > -1) {
+               sta->txrate_idx = sdata->bss->max_ratectrl_rateidx;
+               return;
+       }
+
        /* Ignore all frames that were sent with a different rate than the rate
         * we currently advise mac80211 to use. */
-       if (status->control.rate != &local->oper_hw_mode->rates[sta->txrate])
-               return;
+       if (status->control.tx_rate != &sband->bitrates[sta->txrate_idx])
+               goto ignore;
 
        spinfo = sta->rate_ctrl_priv;
        spinfo->tx_num_xmit++;
@@ -285,43 +295,61 @@ static void rate_control_pid_tx_status(void *priv, struct net_device *dev,
        sta->tx_num_mpdu_fail += status->retry_count;
 
        /* Update PID controller state. */
-       if (time_after(jiffies, spinfo->last_sample + RC_PID_INTERVAL))
+       period = (HZ * pinfo->sampling_period + 500) / 1000;
+       if (!period)
+               period = 1;
+       if (time_after(jiffies, spinfo->last_sample + period))
                rate_control_pid_sample(pinfo, local, sta);
 
+ignore:
        sta_info_put(sta);
 }
 
 static void rate_control_pid_get_rate(void *priv, struct net_device *dev,
-                                     struct ieee80211_hw_mode *mode,
+                                     struct ieee80211_supported_band *sband,
                                      struct sk_buff *skb,
                                      struct rate_selection *sel)
 {
        struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
+       struct ieee80211_sub_if_data *sdata;
        struct sta_info *sta;
        int rateidx;
+       u16 fc;
 
        sta = sta_info_get(local, hdr->addr1);
 
-       if (!sta) {
-               sel->rate = rate_lowest(local, mode, NULL);
-               sta_info_put(sta);
+       /* Send management frames and broadcast/multicast data using lowest
+        * rate. */
+       fc = le16_to_cpu(hdr->frame_control);
+       if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
+           is_multicast_ether_addr(hdr->addr1) || !sta) {
+               sel->rate = rate_lowest(local, sband, sta);
+               if (sta)
+                       sta_info_put(sta);
                return;
        }
 
-       rateidx = sta->txrate;
+       /* If a forced rate is in effect, select it. */
+       sdata = IEEE80211_DEV_TO_SUB_IF(dev);
+       if (sdata->bss && sdata->bss->force_unicast_rateidx > -1)
+               sta->txrate_idx = sdata->bss->force_unicast_rateidx;
+
+       rateidx = sta->txrate_idx;
+
+       if (rateidx >= sband->n_bitrates)
+               rateidx = sband->n_bitrates - 1;
 
-       if (rateidx >= mode->num_rates)
-               rateidx = mode->num_rates - 1;
+       sta->last_txrate_idx = rateidx;
 
        sta_info_put(sta);
 
-       sel->rate = &mode->rates[rateidx];
+       sel->rate = &sband->bitrates[rateidx];
 
 #ifdef CONFIG_MAC80211_DEBUGFS
        rate_control_pid_event_tx_rate(
                &((struct rc_pid_sta_info *) sta->rate_ctrl_priv)->events,
-               rateidx, mode->rates[rateidx].rate);
+               rateidx, sband->bitrates[rateidx].bitrate);
 #endif
 }
 
@@ -333,25 +361,32 @@ static void rate_control_pid_rate_init(void *priv, void *priv_sta,
         * as we need to have IEEE 802.1X auth succeed immediately after assoc..
         * Until that method is implemented, we will use the lowest supported
         * rate as a workaround. */
-       sta->txrate = rate_lowest_index(local, local->oper_hw_mode, sta);
+       struct ieee80211_supported_band *sband;
+
+       sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
+       sta->txrate_idx = rate_lowest_index(local, sband, sta);
 }
 
 static void *rate_control_pid_alloc(struct ieee80211_local *local)
 {
        struct rc_pid_info *pinfo;
        struct rc_pid_rateinfo *rinfo;
-       struct ieee80211_hw_mode *mode;
+       struct ieee80211_supported_band *sband;
        int i, j, tmp;
        bool s;
+#ifdef CONFIG_MAC80211_DEBUGFS
+       struct rc_pid_debugfs_entries *de;
+#endif
+
+       sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
 
        pinfo = kmalloc(sizeof(*pinfo), GFP_ATOMIC);
        if (!pinfo)
                return NULL;
 
-       /* We can safely assume that oper_hw_mode won't change unless we get
+       /* We can safely assume that sband won't change unless we get
         * reinitialized. */
-       mode = local->oper_hw_mode;
-       rinfo = kmalloc(sizeof(*rinfo) * mode->num_rates, GFP_ATOMIC);
+       rinfo = kmalloc(sizeof(*rinfo) * sband->n_bitrates, GFP_ATOMIC);
        if (!rinfo) {
                kfree(pinfo);
                return NULL;
@@ -360,19 +395,19 @@ static void *rate_control_pid_alloc(struct ieee80211_local *local)
        /* Sort the rates. This is optimized for the most common case (i.e.
         * almost-sorted CCK+OFDM rates). Kind of bubble-sort with reversed
         * mapping too. */
-       for (i = 0; i < mode->num_rates; i++) {
+       for (i = 0; i < sband->n_bitrates; i++) {
                rinfo[i].index = i;
                rinfo[i].rev_index = i;
-               if (RC_PID_FAST_START)
+               if (pinfo->fast_start)
                        rinfo[i].diff = 0;
                else
-                       rinfo[i].diff = i * RC_PID_NORM_OFFSET;
+                       rinfo[i].diff = i * pinfo->norm_offset;
        }
-       for (i = 1; i < mode->num_rates; i++) {
+       for (i = 1; i < sband->n_bitrates; i++) {
                s = 0;
-               for (j = 0; j < mode->num_rates - i; j++)
-                       if (unlikely(mode->rates[rinfo[j].index].rate >
-                                    mode->rates[rinfo[j + 1].index].rate)) {
+               for (j = 0; j < sband->n_bitrates - i; j++)
+                       if (unlikely(sband->bitrates[rinfo[j].index].bitrate >
+                                    sband->bitrates[rinfo[j + 1].index].bitrate)) {
                                tmp = rinfo[j].index;
                                rinfo[j].index = rinfo[j + 1].index;
                                rinfo[j + 1].index = tmp;
@@ -385,18 +420,72 @@ static void *rate_control_pid_alloc(struct ieee80211_local *local)
        }
 
        pinfo->target = RC_PID_TARGET_PF;
+       pinfo->sampling_period = RC_PID_INTERVAL;
        pinfo->coeff_p = RC_PID_COEFF_P;
        pinfo->coeff_i = RC_PID_COEFF_I;
        pinfo->coeff_d = RC_PID_COEFF_D;
+       pinfo->smoothing_shift = RC_PID_SMOOTHING_SHIFT;
+       pinfo->sharpen_factor = RC_PID_SHARPENING_FACTOR;
+       pinfo->sharpen_duration = RC_PID_SHARPENING_DURATION;
+       pinfo->norm_offset = RC_PID_NORM_OFFSET;
+       pinfo->fast_start = RC_PID_FAST_START;
        pinfo->rinfo = rinfo;
        pinfo->oldrate = 0;
 
+#ifdef CONFIG_MAC80211_DEBUGFS
+       de = &pinfo->dentries;
+       de->dir = debugfs_create_dir("rc80211_pid",
+                                    local->hw.wiphy->debugfsdir);
+       de->target = debugfs_create_u32("target_pf", S_IRUSR | S_IWUSR,
+                                       de->dir, &pinfo->target);
+       de->sampling_period = debugfs_create_u32("sampling_period",
+                                                S_IRUSR | S_IWUSR, de->dir,
+                                                &pinfo->sampling_period);
+       de->coeff_p = debugfs_create_u32("coeff_p", S_IRUSR | S_IWUSR,
+                                        de->dir, &pinfo->coeff_p);
+       de->coeff_i = debugfs_create_u32("coeff_i", S_IRUSR | S_IWUSR,
+                                        de->dir, &pinfo->coeff_i);
+       de->coeff_d = debugfs_create_u32("coeff_d", S_IRUSR | S_IWUSR,
+                                        de->dir, &pinfo->coeff_d);
+       de->smoothing_shift = debugfs_create_u32("smoothing_shift",
+                                                S_IRUSR | S_IWUSR, de->dir,
+                                                &pinfo->smoothing_shift);
+       de->sharpen_factor = debugfs_create_u32("sharpen_factor",
+                                              S_IRUSR | S_IWUSR, de->dir,
+                                              &pinfo->sharpen_factor);
+       de->sharpen_duration = debugfs_create_u32("sharpen_duration",
+                                                 S_IRUSR | S_IWUSR, de->dir,
+                                                 &pinfo->sharpen_duration);
+       de->norm_offset = debugfs_create_u32("norm_offset",
+                                            S_IRUSR | S_IWUSR, de->dir,
+                                            &pinfo->norm_offset);
+       de->fast_start = debugfs_create_bool("fast_start",
+                                            S_IRUSR | S_IWUSR, de->dir,
+                                            &pinfo->fast_start);
+#endif
+
        return pinfo;
 }
 
 static void rate_control_pid_free(void *priv)
 {
        struct rc_pid_info *pinfo = priv;
+#ifdef CONFIG_MAC80211_DEBUGFS
+       struct rc_pid_debugfs_entries *de = &pinfo->dentries;
+
+       debugfs_remove(de->fast_start);
+       debugfs_remove(de->norm_offset);
+       debugfs_remove(de->sharpen_duration);
+       debugfs_remove(de->sharpen_factor);
+       debugfs_remove(de->smoothing_shift);
+       debugfs_remove(de->coeff_d);
+       debugfs_remove(de->coeff_i);
+       debugfs_remove(de->coeff_p);
+       debugfs_remove(de->sampling_period);
+       debugfs_remove(de->target);
+       debugfs_remove(de->dir);
+#endif
+
        kfree(pinfo->rinfo);
        kfree(pinfo);
 }
@@ -413,6 +502,8 @@ static void *rate_control_pid_alloc_sta(void *priv, gfp_t gfp)
        if (spinfo == NULL)
                return NULL;
 
+       spinfo->last_sample = jiffies;
+
 #ifdef CONFIG_MAC80211_DEBUGFS
        spin_lock_init(&spinfo->events.lock);
        init_waitqueue_head(&spinfo->events.waitqueue);
@@ -427,7 +518,7 @@ static void rate_control_pid_free_sta(void *priv, void *priv_sta)
        kfree(spinfo);
 }
 
-struct rate_control_ops mac80211_rcpid = {
+static struct rate_control_ops mac80211_rcpid = {
        .name = "pid",
        .tx_status = rate_control_pid_tx_status,
        .get_rate = rate_control_pid_get_rate,
@@ -442,3 +533,23 @@ struct rate_control_ops mac80211_rcpid = {
        .remove_sta_debugfs = rate_control_pid_remove_sta_debugfs,
 #endif
 };
+
+MODULE_DESCRIPTION("PID controller based rate control algorithm");
+MODULE_AUTHOR("Stefano Brivio");
+MODULE_AUTHOR("Mattias Nissler");
+MODULE_LICENSE("GPL");
+
+int __init rc80211_pid_init(void)
+{
+       return ieee80211_rate_control_register(&mac80211_rcpid);
+}
+
+void rc80211_pid_exit(void)
+{
+       ieee80211_rate_control_unregister(&mac80211_rcpid);
+}
+
+#ifdef CONFIG_MAC80211_RC_PID_MODULE
+module_init(rc80211_pid_init);
+module_exit(rc80211_pid_exit);
+#endif