Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /******************************************************************************
0003  *
0004  * Copyright(c) 2005 - 2011 Intel Corporation. All rights reserved.
0005  *
0006  * Contact Information:
0007  *  Intel Linux Wireless <ilw@linux.intel.com>
0008  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
0009  *
0010  *****************************************************************************/
0011 
0012 #include <linux/kernel.h>
0013 #include <linux/skbuff.h>
0014 #include <linux/slab.h>
0015 #include <net/mac80211.h>
0016 
0017 #include <linux/netdevice.h>
0018 #include <linux/etherdevice.h>
0019 #include <linux/delay.h>
0020 
0021 #include <linux/workqueue.h>
0022 
0023 #include "commands.h"
0024 #include "3945.h"
0025 
0026 #define RS_NAME "iwl-3945-rs"
0027 
0028 static s32 il3945_expected_tpt_g[RATE_COUNT_3945] = {
0029     7, 13, 35, 58, 0, 0, 76, 104, 130, 168, 191, 202
0030 };
0031 
0032 static s32 il3945_expected_tpt_g_prot[RATE_COUNT_3945] = {
0033     7, 13, 35, 58, 0, 0, 0, 80, 93, 113, 123, 125
0034 };
0035 
0036 static s32 il3945_expected_tpt_a[RATE_COUNT_3945] = {
0037     0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186
0038 };
0039 
0040 static s32 il3945_expected_tpt_b[RATE_COUNT_3945] = {
0041     7, 13, 35, 58, 0, 0, 0, 0, 0, 0, 0, 0
0042 };
0043 
0044 struct il3945_tpt_entry {
0045     s8 min_rssi;
0046     u8 idx;
0047 };
0048 
0049 static struct il3945_tpt_entry il3945_tpt_table_a[] = {
0050     {-60, RATE_54M_IDX},
0051     {-64, RATE_48M_IDX},
0052     {-72, RATE_36M_IDX},
0053     {-80, RATE_24M_IDX},
0054     {-84, RATE_18M_IDX},
0055     {-85, RATE_12M_IDX},
0056     {-87, RATE_9M_IDX},
0057     {-89, RATE_6M_IDX}
0058 };
0059 
0060 static struct il3945_tpt_entry il3945_tpt_table_g[] = {
0061     {-60, RATE_54M_IDX},
0062     {-64, RATE_48M_IDX},
0063     {-68, RATE_36M_IDX},
0064     {-80, RATE_24M_IDX},
0065     {-84, RATE_18M_IDX},
0066     {-85, RATE_12M_IDX},
0067     {-86, RATE_11M_IDX},
0068     {-88, RATE_5M_IDX},
0069     {-90, RATE_2M_IDX},
0070     {-92, RATE_1M_IDX}
0071 };
0072 
0073 #define RATE_MAX_WINDOW     62
0074 #define RATE_FLUSH      (3*HZ)
0075 #define RATE_WIN_FLUSH      (HZ/2)
0076 #define IL39_RATE_HIGH_TH   11520
0077 #define IL_SUCCESS_UP_TH    8960
0078 #define IL_SUCCESS_DOWN_TH  10880
0079 #define RATE_MIN_FAILURE_TH 6
0080 #define RATE_MIN_SUCCESS_TH 8
0081 #define RATE_DECREASE_TH    1920
0082 #define RATE_RETRY_TH       15
0083 
0084 static u8
0085 il3945_get_rate_idx_by_rssi(s32 rssi, enum nl80211_band band)
0086 {
0087     u32 idx = 0;
0088     u32 table_size = 0;
0089     struct il3945_tpt_entry *tpt_table = NULL;
0090 
0091     if (rssi < IL_MIN_RSSI_VAL || rssi > IL_MAX_RSSI_VAL)
0092         rssi = IL_MIN_RSSI_VAL;
0093 
0094     switch (band) {
0095     case NL80211_BAND_2GHZ:
0096         tpt_table = il3945_tpt_table_g;
0097         table_size = ARRAY_SIZE(il3945_tpt_table_g);
0098         break;
0099     case NL80211_BAND_5GHZ:
0100         tpt_table = il3945_tpt_table_a;
0101         table_size = ARRAY_SIZE(il3945_tpt_table_a);
0102         break;
0103     default:
0104         BUG();
0105         break;
0106     }
0107 
0108     while (idx < table_size && rssi < tpt_table[idx].min_rssi)
0109         idx++;
0110 
0111     idx = min(idx, table_size - 1);
0112 
0113     return tpt_table[idx].idx;
0114 }
0115 
0116 static void
0117 il3945_clear_win(struct il3945_rate_scale_data *win)
0118 {
0119     win->data = 0;
0120     win->success_counter = 0;
0121     win->success_ratio = -1;
0122     win->counter = 0;
0123     win->average_tpt = IL_INVALID_VALUE;
0124     win->stamp = 0;
0125 }
0126 
0127 /*
0128  * il3945_rate_scale_flush_wins - flush out the rate scale wins
0129  *
0130  * Returns the number of wins that have gathered data but were
0131  * not flushed.  If there were any that were not flushed, then
0132  * reschedule the rate flushing routine.
0133  */
0134 static int
0135 il3945_rate_scale_flush_wins(struct il3945_rs_sta *rs_sta)
0136 {
0137     int unflushed = 0;
0138     int i;
0139     unsigned long flags;
0140     struct il_priv *il __maybe_unused = rs_sta->il;
0141 
0142     /*
0143      * For each rate, if we have collected data on that rate
0144      * and it has been more than RATE_WIN_FLUSH
0145      * since we flushed, clear out the gathered stats
0146      */
0147     for (i = 0; i < RATE_COUNT_3945; i++) {
0148         if (!rs_sta->win[i].counter)
0149             continue;
0150 
0151         spin_lock_irqsave(&rs_sta->lock, flags);
0152         if (time_after(jiffies, rs_sta->win[i].stamp + RATE_WIN_FLUSH)) {
0153             D_RATE("flushing %d samples of rate " "idx %d\n",
0154                    rs_sta->win[i].counter, i);
0155             il3945_clear_win(&rs_sta->win[i]);
0156         } else
0157             unflushed++;
0158         spin_unlock_irqrestore(&rs_sta->lock, flags);
0159     }
0160 
0161     return unflushed;
0162 }
0163 
0164 #define RATE_FLUSH_MAX              5000    /* msec */
0165 #define RATE_FLUSH_MIN              50  /* msec */
0166 #define IL_AVERAGE_PACKETS             1500
0167 
0168 static void
0169 il3945_bg_rate_scale_flush(struct timer_list *t)
0170 {
0171     struct il3945_rs_sta *rs_sta = from_timer(rs_sta, t, rate_scale_flush);
0172     struct il_priv *il __maybe_unused = rs_sta->il;
0173     int unflushed = 0;
0174     unsigned long flags;
0175     u32 packet_count, duration, pps;
0176 
0177     D_RATE("enter\n");
0178 
0179     unflushed = il3945_rate_scale_flush_wins(rs_sta);
0180 
0181     spin_lock_irqsave(&rs_sta->lock, flags);
0182 
0183     /* Number of packets Rx'd since last time this timer ran */
0184     packet_count = (rs_sta->tx_packets - rs_sta->last_tx_packets) + 1;
0185 
0186     rs_sta->last_tx_packets = rs_sta->tx_packets + 1;
0187 
0188     if (unflushed) {
0189         duration =
0190             jiffies_to_msecs(jiffies - rs_sta->last_partial_flush);
0191 
0192         D_RATE("Tx'd %d packets in %dms\n", packet_count, duration);
0193 
0194         /* Determine packets per second */
0195         if (duration)
0196             pps = (packet_count * 1000) / duration;
0197         else
0198             pps = 0;
0199 
0200         if (pps) {
0201             duration = (IL_AVERAGE_PACKETS * 1000) / pps;
0202             if (duration < RATE_FLUSH_MIN)
0203                 duration = RATE_FLUSH_MIN;
0204             else if (duration > RATE_FLUSH_MAX)
0205                 duration = RATE_FLUSH_MAX;
0206         } else
0207             duration = RATE_FLUSH_MAX;
0208 
0209         rs_sta->flush_time = msecs_to_jiffies(duration);
0210 
0211         D_RATE("new flush period: %d msec ave %d\n", duration,
0212                packet_count);
0213 
0214         mod_timer(&rs_sta->rate_scale_flush,
0215               jiffies + rs_sta->flush_time);
0216 
0217         rs_sta->last_partial_flush = jiffies;
0218     } else {
0219         rs_sta->flush_time = RATE_FLUSH;
0220         rs_sta->flush_pending = 0;
0221     }
0222     /* If there weren't any unflushed entries, we don't schedule the timer
0223      * to run again */
0224 
0225     rs_sta->last_flush = jiffies;
0226 
0227     spin_unlock_irqrestore(&rs_sta->lock, flags);
0228 
0229     D_RATE("leave\n");
0230 }
0231 
0232 /*
0233  * il3945_collect_tx_data - Update the success/failure sliding win
0234  *
0235  * We keep a sliding win of the last 64 packets transmitted
0236  * at this rate.  win->data contains the bitmask of successful
0237  * packets.
0238  */
0239 static void
0240 il3945_collect_tx_data(struct il3945_rs_sta *rs_sta,
0241                struct il3945_rate_scale_data *win, int success,
0242                int retries, int idx)
0243 {
0244     unsigned long flags;
0245     s32 fail_count;
0246     struct il_priv *il __maybe_unused = rs_sta->il;
0247 
0248     if (!retries) {
0249         D_RATE("leave: retries == 0 -- should be at least 1\n");
0250         return;
0251     }
0252 
0253     spin_lock_irqsave(&rs_sta->lock, flags);
0254 
0255     /*
0256      * Keep track of only the latest 62 tx frame attempts in this rate's
0257      * history win; anything older isn't really relevant any more.
0258      * If we have filled up the sliding win, drop the oldest attempt;
0259      * if the oldest attempt (highest bit in bitmap) shows "success",
0260      * subtract "1" from the success counter (this is the main reason
0261      * we keep these bitmaps!).
0262      * */
0263     while (retries > 0) {
0264         if (win->counter >= RATE_MAX_WINDOW) {
0265 
0266             /* remove earliest */
0267             win->counter = RATE_MAX_WINDOW - 1;
0268 
0269             if (win->data & (1ULL << (RATE_MAX_WINDOW - 1))) {
0270                 win->data &= ~(1ULL << (RATE_MAX_WINDOW - 1));
0271                 win->success_counter--;
0272             }
0273         }
0274 
0275         /* Increment frames-attempted counter */
0276         win->counter++;
0277 
0278         /* Shift bitmap by one frame (throw away oldest history),
0279          * OR in "1", and increment "success" if this
0280          * frame was successful. */
0281         win->data <<= 1;
0282         if (success > 0) {
0283             win->success_counter++;
0284             win->data |= 0x1;
0285             success--;
0286         }
0287 
0288         retries--;
0289     }
0290 
0291     /* Calculate current success ratio, avoid divide-by-0! */
0292     if (win->counter > 0)
0293         win->success_ratio =
0294             128 * (100 * win->success_counter) / win->counter;
0295     else
0296         win->success_ratio = IL_INVALID_VALUE;
0297 
0298     fail_count = win->counter - win->success_counter;
0299 
0300     /* Calculate average throughput, if we have enough history. */
0301     if (fail_count >= RATE_MIN_FAILURE_TH ||
0302         win->success_counter >= RATE_MIN_SUCCESS_TH)
0303         win->average_tpt =
0304             ((win->success_ratio * rs_sta->expected_tpt[idx] +
0305               64) / 128);
0306     else
0307         win->average_tpt = IL_INVALID_VALUE;
0308 
0309     /* Tag this win as having been updated */
0310     win->stamp = jiffies;
0311 
0312     spin_unlock_irqrestore(&rs_sta->lock, flags);
0313 }
0314 
0315 /*
0316  * Called after adding a new station to initialize rate scaling
0317  */
0318 void
0319 il3945_rs_rate_init(struct il_priv *il, struct ieee80211_sta *sta, u8 sta_id)
0320 {
0321     struct ieee80211_hw *hw = il->hw;
0322     struct ieee80211_conf *conf = &il->hw->conf;
0323     struct il3945_sta_priv *psta;
0324     struct il3945_rs_sta *rs_sta;
0325     struct ieee80211_supported_band *sband;
0326     int i;
0327 
0328     D_INFO("enter\n");
0329     if (sta_id == il->hw_params.bcast_id)
0330         goto out;
0331 
0332     psta = (struct il3945_sta_priv *)sta->drv_priv;
0333     rs_sta = &psta->rs_sta;
0334     sband = hw->wiphy->bands[conf->chandef.chan->band];
0335 
0336     rs_sta->il = il;
0337 
0338     rs_sta->start_rate = RATE_INVALID;
0339 
0340     /* default to just 802.11b */
0341     rs_sta->expected_tpt = il3945_expected_tpt_b;
0342 
0343     rs_sta->last_partial_flush = jiffies;
0344     rs_sta->last_flush = jiffies;
0345     rs_sta->flush_time = RATE_FLUSH;
0346     rs_sta->last_tx_packets = 0;
0347 
0348     for (i = 0; i < RATE_COUNT_3945; i++)
0349         il3945_clear_win(&rs_sta->win[i]);
0350 
0351     /* TODO: what is a good starting rate for STA? About middle? Maybe not
0352      * the lowest or the highest rate.. Could consider using RSSI from
0353      * previous packets? Need to have IEEE 802.1X auth succeed immediately
0354      * after assoc.. */
0355 
0356     for (i = sband->n_bitrates - 1; i >= 0; i--) {
0357         if (sta->deflink.supp_rates[sband->band] & (1 << i)) {
0358             rs_sta->last_txrate_idx = i;
0359             break;
0360         }
0361     }
0362 
0363     il->_3945.sta_supp_rates = sta->deflink.supp_rates[sband->band];
0364     /* For 5 GHz band it start at IL_FIRST_OFDM_RATE */
0365     if (sband->band == NL80211_BAND_5GHZ) {
0366         rs_sta->last_txrate_idx += IL_FIRST_OFDM_RATE;
0367         il->_3945.sta_supp_rates <<= IL_FIRST_OFDM_RATE;
0368     }
0369 
0370 out:
0371     il->stations[sta_id].used &= ~IL_STA_UCODE_INPROGRESS;
0372 
0373     D_INFO("leave\n");
0374 }
0375 
0376 static void *
0377 il3945_rs_alloc(struct ieee80211_hw *hw)
0378 {
0379     return hw->priv;
0380 }
0381 
0382 /* rate scale requires free function to be implemented */
0383 static void
0384 il3945_rs_free(void *il)
0385 {
0386 }
0387 
0388 static void *
0389 il3945_rs_alloc_sta(void *il_priv, struct ieee80211_sta *sta, gfp_t gfp)
0390 {
0391     struct il3945_rs_sta *rs_sta;
0392     struct il3945_sta_priv *psta = (void *)sta->drv_priv;
0393     struct il_priv *il __maybe_unused = il_priv;
0394 
0395     D_RATE("enter\n");
0396 
0397     rs_sta = &psta->rs_sta;
0398 
0399     spin_lock_init(&rs_sta->lock);
0400     timer_setup(&rs_sta->rate_scale_flush, il3945_bg_rate_scale_flush, 0);
0401     D_RATE("leave\n");
0402 
0403     return rs_sta;
0404 }
0405 
0406 static void
0407 il3945_rs_free_sta(void *il_priv, struct ieee80211_sta *sta, void *il_sta)
0408 {
0409     struct il3945_rs_sta *rs_sta = il_sta;
0410 
0411     /*
0412      * Be careful not to use any members of il3945_rs_sta (like trying
0413      * to use il_priv to print out debugging) since it may not be fully
0414      * initialized at this point.
0415      */
0416     del_timer_sync(&rs_sta->rate_scale_flush);
0417 }
0418 
0419 /*
0420  * il3945_rs_tx_status - Update rate control values based on Tx results
0421  *
0422  * NOTE: Uses il_priv->retry_rate for the # of retries attempted by
0423  * the hardware for each rate.
0424  */
0425 static void
0426 il3945_rs_tx_status(void *il_rate, struct ieee80211_supported_band *sband,
0427             struct ieee80211_sta *sta, void *il_sta,
0428             struct sk_buff *skb)
0429 {
0430     s8 retries = 0, current_count;
0431     int scale_rate_idx, first_idx, last_idx;
0432     unsigned long flags;
0433     struct il_priv *il = (struct il_priv *)il_rate;
0434     struct il3945_rs_sta *rs_sta = il_sta;
0435     struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
0436 
0437     D_RATE("enter\n");
0438 
0439     retries = info->status.rates[0].count;
0440     /* Sanity Check for retries */
0441     if (retries > RATE_RETRY_TH)
0442         retries = RATE_RETRY_TH;
0443 
0444     first_idx = sband->bitrates[info->status.rates[0].idx].hw_value;
0445     if (first_idx < 0 || first_idx >= RATE_COUNT_3945) {
0446         D_RATE("leave: Rate out of bounds: %d\n", first_idx);
0447         return;
0448     }
0449 
0450     if (!il_sta) {
0451         D_RATE("leave: No STA il data to update!\n");
0452         return;
0453     }
0454 
0455     /* Treat uninitialized rate scaling data same as non-existing. */
0456     if (!rs_sta->il) {
0457         D_RATE("leave: STA il data uninitialized!\n");
0458         return;
0459     }
0460 
0461     rs_sta->tx_packets++;
0462 
0463     scale_rate_idx = first_idx;
0464     last_idx = first_idx;
0465 
0466     /*
0467      * Update the win for each rate.  We determine which rates
0468      * were Tx'd based on the total number of retries vs. the number
0469      * of retries configured for each rate -- currently set to the
0470      * il value 'retry_rate' vs. rate specific
0471      *
0472      * On exit from this while loop last_idx indicates the rate
0473      * at which the frame was finally transmitted (or failed if no
0474      * ACK)
0475      */
0476     while (retries > 1) {
0477         if ((retries - 1) < il->retry_rate) {
0478             current_count = (retries - 1);
0479             last_idx = scale_rate_idx;
0480         } else {
0481             current_count = il->retry_rate;
0482             last_idx = il3945_rs_next_rate(il, scale_rate_idx);
0483         }
0484 
0485         /* Update this rate accounting for as many retries
0486          * as was used for it (per current_count) */
0487         il3945_collect_tx_data(rs_sta, &rs_sta->win[scale_rate_idx], 0,
0488                        current_count, scale_rate_idx);
0489         D_RATE("Update rate %d for %d retries.\n", scale_rate_idx,
0490                current_count);
0491 
0492         retries -= current_count;
0493 
0494         scale_rate_idx = last_idx;
0495     }
0496 
0497     /* Update the last idx win with success/failure based on ACK */
0498     D_RATE("Update rate %d with %s.\n", last_idx,
0499            (info->flags & IEEE80211_TX_STAT_ACK) ? "success" : "failure");
0500     il3945_collect_tx_data(rs_sta, &rs_sta->win[last_idx],
0501                    info->flags & IEEE80211_TX_STAT_ACK, 1,
0502                    last_idx);
0503 
0504     /* We updated the rate scale win -- if its been more than
0505      * flush_time since the last run, schedule the flush
0506      * again */
0507     spin_lock_irqsave(&rs_sta->lock, flags);
0508 
0509     if (!rs_sta->flush_pending &&
0510         time_after(jiffies, rs_sta->last_flush + rs_sta->flush_time)) {
0511 
0512         rs_sta->last_partial_flush = jiffies;
0513         rs_sta->flush_pending = 1;
0514         mod_timer(&rs_sta->rate_scale_flush,
0515               jiffies + rs_sta->flush_time);
0516     }
0517 
0518     spin_unlock_irqrestore(&rs_sta->lock, flags);
0519 
0520     D_RATE("leave\n");
0521 }
0522 
0523 static u16
0524 il3945_get_adjacent_rate(struct il3945_rs_sta *rs_sta, u8 idx, u16 rate_mask,
0525              enum nl80211_band band)
0526 {
0527     u8 high = RATE_INVALID;
0528     u8 low = RATE_INVALID;
0529     struct il_priv *il __maybe_unused = rs_sta->il;
0530 
0531     /* 802.11A walks to the next literal adjacent rate in
0532      * the rate table */
0533     if (unlikely(band == NL80211_BAND_5GHZ)) {
0534         int i;
0535         u32 mask;
0536 
0537         /* Find the previous rate that is in the rate mask */
0538         i = idx - 1;
0539         for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
0540             if (rate_mask & mask) {
0541                 low = i;
0542                 break;
0543             }
0544         }
0545 
0546         /* Find the next rate that is in the rate mask */
0547         i = idx + 1;
0548         for (mask = (1 << i); i < RATE_COUNT_3945; i++, mask <<= 1) {
0549             if (rate_mask & mask) {
0550                 high = i;
0551                 break;
0552             }
0553         }
0554 
0555         return (high << 8) | low;
0556     }
0557 
0558     low = idx;
0559     while (low != RATE_INVALID) {
0560         if (rs_sta->tgg)
0561             low = il3945_rates[low].prev_rs_tgg;
0562         else
0563             low = il3945_rates[low].prev_rs;
0564         if (low == RATE_INVALID)
0565             break;
0566         if (rate_mask & (1 << low))
0567             break;
0568         D_RATE("Skipping masked lower rate: %d\n", low);
0569     }
0570 
0571     high = idx;
0572     while (high != RATE_INVALID) {
0573         if (rs_sta->tgg)
0574             high = il3945_rates[high].next_rs_tgg;
0575         else
0576             high = il3945_rates[high].next_rs;
0577         if (high == RATE_INVALID)
0578             break;
0579         if (rate_mask & (1 << high))
0580             break;
0581         D_RATE("Skipping masked higher rate: %d\n", high);
0582     }
0583 
0584     return (high << 8) | low;
0585 }
0586 
0587 /*
0588  * il3945_rs_get_rate - find the rate for the requested packet
0589  *
0590  * Returns the ieee80211_rate structure allocated by the driver.
0591  *
0592  * The rate control algorithm has no internal mapping between hw_mode's
0593  * rate ordering and the rate ordering used by the rate control algorithm.
0594  *
0595  * The rate control algorithm uses a single table of rates that goes across
0596  * the entire A/B/G spectrum vs. being limited to just one particular
0597  * hw_mode.
0598  *
0599  * As such, we can't convert the idx obtained below into the hw_mode's
0600  * rate table and must reference the driver allocated rate table
0601  *
0602  */
0603 static void
0604 il3945_rs_get_rate(void *il_r, struct ieee80211_sta *sta, void *il_sta,
0605            struct ieee80211_tx_rate_control *txrc)
0606 {
0607     struct ieee80211_supported_band *sband = txrc->sband;
0608     struct sk_buff *skb = txrc->skb;
0609     u8 low = RATE_INVALID;
0610     u8 high = RATE_INVALID;
0611     u16 high_low;
0612     int idx;
0613     struct il3945_rs_sta *rs_sta = il_sta;
0614     struct il3945_rate_scale_data *win = NULL;
0615     int current_tpt = IL_INVALID_VALUE;
0616     int low_tpt = IL_INVALID_VALUE;
0617     int high_tpt = IL_INVALID_VALUE;
0618     u32 fail_count;
0619     s8 scale_action = 0;
0620     unsigned long flags;
0621     u16 rate_mask;
0622     s8 max_rate_idx = -1;
0623     struct il_priv *il __maybe_unused = (struct il_priv *)il_r;
0624     struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
0625 
0626     D_RATE("enter\n");
0627 
0628     /* Treat uninitialized rate scaling data same as non-existing. */
0629     if (rs_sta && !rs_sta->il) {
0630         D_RATE("Rate scaling information not initialized yet.\n");
0631         il_sta = NULL;
0632     }
0633 
0634     rate_mask = sta->deflink.supp_rates[sband->band];
0635 
0636     /* get user max rate if set */
0637     max_rate_idx = fls(txrc->rate_idx_mask) - 1;
0638     if (sband->band == NL80211_BAND_5GHZ && max_rate_idx != -1)
0639         max_rate_idx += IL_FIRST_OFDM_RATE;
0640     if (max_rate_idx < 0 || max_rate_idx >= RATE_COUNT)
0641         max_rate_idx = -1;
0642 
0643     idx = min(rs_sta->last_txrate_idx & 0xffff, RATE_COUNT_3945 - 1);
0644 
0645     if (sband->band == NL80211_BAND_5GHZ)
0646         rate_mask = rate_mask << IL_FIRST_OFDM_RATE;
0647 
0648     spin_lock_irqsave(&rs_sta->lock, flags);
0649 
0650     /* for recent assoc, choose best rate regarding
0651      * to rssi value
0652      */
0653     if (rs_sta->start_rate != RATE_INVALID) {
0654         if (rs_sta->start_rate < idx &&
0655             (rate_mask & (1 << rs_sta->start_rate)))
0656             idx = rs_sta->start_rate;
0657         rs_sta->start_rate = RATE_INVALID;
0658     }
0659 
0660     /* force user max rate if set by user */
0661     if (max_rate_idx != -1 && max_rate_idx < idx) {
0662         if (rate_mask & (1 << max_rate_idx))
0663             idx = max_rate_idx;
0664     }
0665 
0666     win = &(rs_sta->win[idx]);
0667 
0668     fail_count = win->counter - win->success_counter;
0669 
0670     if (fail_count < RATE_MIN_FAILURE_TH &&
0671         win->success_counter < RATE_MIN_SUCCESS_TH) {
0672         spin_unlock_irqrestore(&rs_sta->lock, flags);
0673 
0674         D_RATE("Invalid average_tpt on rate %d: "
0675                "counter: %d, success_counter: %d, "
0676                "expected_tpt is %sNULL\n", idx, win->counter,
0677                win->success_counter,
0678                rs_sta->expected_tpt ? "not " : "");
0679 
0680         /* Can't calculate this yet; not enough history */
0681         win->average_tpt = IL_INVALID_VALUE;
0682         goto out;
0683 
0684     }
0685 
0686     current_tpt = win->average_tpt;
0687 
0688     high_low =
0689         il3945_get_adjacent_rate(rs_sta, idx, rate_mask, sband->band);
0690     low = high_low & 0xff;
0691     high = (high_low >> 8) & 0xff;
0692 
0693     /* If user set max rate, dont allow higher than user constrain */
0694     if (max_rate_idx != -1 && max_rate_idx < high)
0695         high = RATE_INVALID;
0696 
0697     /* Collect Measured throughputs of adjacent rates */
0698     if (low != RATE_INVALID)
0699         low_tpt = rs_sta->win[low].average_tpt;
0700 
0701     if (high != RATE_INVALID)
0702         high_tpt = rs_sta->win[high].average_tpt;
0703 
0704     spin_unlock_irqrestore(&rs_sta->lock, flags);
0705 
0706     scale_action = 0;
0707 
0708     /* Low success ratio , need to drop the rate */
0709     if (win->success_ratio < RATE_DECREASE_TH || !current_tpt) {
0710         D_RATE("decrease rate because of low success_ratio\n");
0711         scale_action = -1;
0712         /* No throughput measured yet for adjacent rates,
0713          * try increase */
0714     } else if (low_tpt == IL_INVALID_VALUE && high_tpt == IL_INVALID_VALUE) {
0715 
0716         if (high != RATE_INVALID &&
0717             win->success_ratio >= RATE_INCREASE_TH)
0718             scale_action = 1;
0719         else if (low != RATE_INVALID)
0720             scale_action = 0;
0721 
0722         /* Both adjacent throughputs are measured, but neither one has
0723          * better throughput; we're using the best rate, don't change
0724          * it! */
0725     } else if (low_tpt != IL_INVALID_VALUE && high_tpt != IL_INVALID_VALUE
0726            && low_tpt < current_tpt && high_tpt < current_tpt) {
0727 
0728         D_RATE("No action -- low [%d] & high [%d] < "
0729                "current_tpt [%d]\n", low_tpt, high_tpt, current_tpt);
0730         scale_action = 0;
0731 
0732         /* At least one of the rates has better throughput */
0733     } else {
0734         if (high_tpt != IL_INVALID_VALUE) {
0735 
0736             /* High rate has better throughput, Increase
0737              * rate */
0738             if (high_tpt > current_tpt &&
0739                 win->success_ratio >= RATE_INCREASE_TH)
0740                 scale_action = 1;
0741             else {
0742                 D_RATE("decrease rate because of high tpt\n");
0743                 scale_action = 0;
0744             }
0745         } else if (low_tpt != IL_INVALID_VALUE) {
0746             if (low_tpt > current_tpt) {
0747                 D_RATE("decrease rate because of low tpt\n");
0748                 scale_action = -1;
0749             } else if (win->success_ratio >= RATE_INCREASE_TH) {
0750                 /* Lower rate has better
0751                  * throughput,decrease rate */
0752                 scale_action = 1;
0753             }
0754         }
0755     }
0756 
0757     /* Sanity check; asked for decrease, but success rate or throughput
0758      * has been good at old rate.  Don't change it. */
0759     if (scale_action == -1 && low != RATE_INVALID &&
0760         (win->success_ratio > RATE_HIGH_TH ||
0761          current_tpt > 100 * rs_sta->expected_tpt[low]))
0762         scale_action = 0;
0763 
0764     switch (scale_action) {
0765     case -1:
0766         /* Decrease rate */
0767         if (low != RATE_INVALID)
0768             idx = low;
0769         break;
0770     case 1:
0771         /* Increase rate */
0772         if (high != RATE_INVALID)
0773             idx = high;
0774 
0775         break;
0776     case 0:
0777     default:
0778         /* No change */
0779         break;
0780     }
0781 
0782     D_RATE("Selected %d (action %d) - low %d high %d\n", idx, scale_action,
0783            low, high);
0784 
0785 out:
0786 
0787     if (sband->band == NL80211_BAND_5GHZ) {
0788         if (WARN_ON_ONCE(idx < IL_FIRST_OFDM_RATE))
0789             idx = IL_FIRST_OFDM_RATE;
0790         rs_sta->last_txrate_idx = idx;
0791         info->control.rates[0].idx = idx - IL_FIRST_OFDM_RATE;
0792     } else {
0793         rs_sta->last_txrate_idx = idx;
0794         info->control.rates[0].idx = rs_sta->last_txrate_idx;
0795     }
0796     info->control.rates[0].count = 1;
0797 
0798     D_RATE("leave: %d\n", idx);
0799 }
0800 
0801 #ifdef CONFIG_MAC80211_DEBUGFS
0802 
0803 static ssize_t
0804 il3945_sta_dbgfs_stats_table_read(struct file *file, char __user *user_buf,
0805                   size_t count, loff_t *ppos)
0806 {
0807     char *buff;
0808     int desc = 0;
0809     int j;
0810     ssize_t ret;
0811     struct il3945_rs_sta *lq_sta = file->private_data;
0812 
0813     buff = kmalloc(1024, GFP_KERNEL);
0814     if (!buff)
0815         return -ENOMEM;
0816 
0817     desc +=
0818         sprintf(buff + desc,
0819             "tx packets=%d last rate idx=%d\n"
0820             "rate=0x%X flush time %d\n", lq_sta->tx_packets,
0821             lq_sta->last_txrate_idx, lq_sta->start_rate,
0822             jiffies_to_msecs(lq_sta->flush_time));
0823     for (j = 0; j < RATE_COUNT_3945; j++) {
0824         desc +=
0825             sprintf(buff + desc, "counter=%d success=%d %%=%d\n",
0826                 lq_sta->win[j].counter,
0827                 lq_sta->win[j].success_counter,
0828                 lq_sta->win[j].success_ratio);
0829     }
0830     ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
0831     kfree(buff);
0832     return ret;
0833 }
0834 
0835 static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
0836     .read = il3945_sta_dbgfs_stats_table_read,
0837     .open = simple_open,
0838     .llseek = default_llseek,
0839 };
0840 
0841 static void
0842 il3945_add_debugfs(void *il, void *il_sta, struct dentry *dir)
0843 {
0844     struct il3945_rs_sta *lq_sta = il_sta;
0845 
0846     debugfs_create_file("rate_stats_table", 0600, dir, lq_sta,
0847                 &rs_sta_dbgfs_stats_table_ops);
0848 }
0849 #endif
0850 
0851 /*
0852  * Initialization of rate scaling information is done by driver after
0853  * the station is added. Since mac80211 calls this function before a
0854  * station is added we ignore it.
0855  */
0856 static void
0857 il3945_rs_rate_init_stub(void *il_r, struct ieee80211_supported_band *sband,
0858              struct cfg80211_chan_def *chandef,
0859              struct ieee80211_sta *sta, void *il_sta)
0860 {
0861 }
0862 
0863 static const struct rate_control_ops rs_ops = {
0864     .name = RS_NAME,
0865     .tx_status = il3945_rs_tx_status,
0866     .get_rate = il3945_rs_get_rate,
0867     .rate_init = il3945_rs_rate_init_stub,
0868     .alloc = il3945_rs_alloc,
0869     .free = il3945_rs_free,
0870     .alloc_sta = il3945_rs_alloc_sta,
0871     .free_sta = il3945_rs_free_sta,
0872 #ifdef CONFIG_MAC80211_DEBUGFS
0873     .add_sta_debugfs = il3945_add_debugfs,
0874 #endif
0875 
0876 };
0877 
0878 void
0879 il3945_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
0880 {
0881     struct il_priv *il = hw->priv;
0882     s32 rssi = 0;
0883     unsigned long flags;
0884     struct il3945_rs_sta *rs_sta;
0885     struct ieee80211_sta *sta;
0886     struct il3945_sta_priv *psta;
0887 
0888     D_RATE("enter\n");
0889 
0890     rcu_read_lock();
0891 
0892     sta = ieee80211_find_sta(il->vif, il->stations[sta_id].sta.sta.addr);
0893     if (!sta) {
0894         D_RATE("Unable to find station to initialize rate scaling.\n");
0895         rcu_read_unlock();
0896         return;
0897     }
0898 
0899     psta = (void *)sta->drv_priv;
0900     rs_sta = &psta->rs_sta;
0901 
0902     spin_lock_irqsave(&rs_sta->lock, flags);
0903 
0904     rs_sta->tgg = 0;
0905     switch (il->band) {
0906     case NL80211_BAND_2GHZ:
0907         /* TODO: this always does G, not a regression */
0908         if (il->active.flags & RXON_FLG_TGG_PROTECT_MSK) {
0909             rs_sta->tgg = 1;
0910             rs_sta->expected_tpt = il3945_expected_tpt_g_prot;
0911         } else
0912             rs_sta->expected_tpt = il3945_expected_tpt_g;
0913         break;
0914     case NL80211_BAND_5GHZ:
0915         rs_sta->expected_tpt = il3945_expected_tpt_a;
0916         break;
0917     default:
0918         BUG();
0919         break;
0920     }
0921 
0922     spin_unlock_irqrestore(&rs_sta->lock, flags);
0923 
0924     rssi = il->_3945.last_rx_rssi;
0925     if (rssi == 0)
0926         rssi = IL_MIN_RSSI_VAL;
0927 
0928     D_RATE("Network RSSI: %d\n", rssi);
0929 
0930     rs_sta->start_rate = il3945_get_rate_idx_by_rssi(rssi, il->band);
0931 
0932     D_RATE("leave: rssi %d assign rate idx: " "%d (plcp 0x%x)\n", rssi,
0933            rs_sta->start_rate, il3945_rates[rs_sta->start_rate].plcp);
0934     rcu_read_unlock();
0935 }
0936 
0937 int
0938 il3945_rate_control_register(void)
0939 {
0940     return ieee80211_rate_control_register(&rs_ops);
0941 }
0942 
0943 void
0944 il3945_rate_control_unregister(void)
0945 {
0946     ieee80211_rate_control_unregister(&rs_ops);
0947 }