Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
Reinette Chatre | eb7ae89 | 2008-03-11 16:17:17 -0700 | [diff] [blame] | 3 | * Copyright(c) 2005 - 2008 Intel Corporation. All rights reserved. |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 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 | * James P. Ketrenos <ipw2100-admin@linux.intel.com> |
| 23 | * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 |
| 24 | * |
| 25 | *****************************************************************************/ |
| 26 | |
| 27 | #include <linux/kernel.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/skbuff.h> |
| 30 | #include <linux/wireless.h> |
| 31 | #include <net/mac80211.h> |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 32 | |
| 33 | #include <linux/netdevice.h> |
| 34 | #include <linux/etherdevice.h> |
| 35 | #include <linux/delay.h> |
| 36 | |
| 37 | #include <linux/workqueue.h> |
| 38 | |
Christoph Hellwig | 5d08cd1 | 2007-10-25 17:15:50 +0800 | [diff] [blame] | 39 | #include "iwl-3945.h" |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 40 | |
| 41 | #define RS_NAME "iwl-3945-rs" |
| 42 | |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 43 | struct iwl3945_rate_scale_data { |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 44 | u64 data; |
| 45 | s32 success_counter; |
| 46 | s32 success_ratio; |
| 47 | s32 counter; |
| 48 | s32 average_tpt; |
| 49 | unsigned long stamp; |
| 50 | }; |
| 51 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 52 | struct iwl3945_rs_sta { |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 53 | spinlock_t lock; |
| 54 | s32 *expected_tpt; |
| 55 | unsigned long last_partial_flush; |
| 56 | unsigned long last_flush; |
| 57 | u32 flush_time; |
| 58 | u32 last_tx_packets; |
| 59 | u32 tx_packets; |
| 60 | u8 tgg; |
| 61 | u8 flush_pending; |
| 62 | u8 start_rate; |
| 63 | u8 ibss_sta_added; |
| 64 | struct timer_list rate_scale_flush; |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 65 | struct iwl3945_rate_scale_data win[IWL_RATE_COUNT]; |
Johannes Berg | b7e3500 | 2008-09-11 02:22:58 +0200 | [diff] [blame] | 66 | |
| 67 | /* used to be in sta_info */ |
| 68 | int last_txrate_idx; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 71 | static s32 iwl3945_expected_tpt_g[IWL_RATE_COUNT] = { |
Mohamed Abbas | 14577f2 | 2007-11-12 11:37:42 +0800 | [diff] [blame] | 72 | 7, 13, 35, 58, 0, 0, 76, 104, 130, 168, 191, 202 |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 75 | static s32 iwl3945_expected_tpt_g_prot[IWL_RATE_COUNT] = { |
Mohamed Abbas | 14577f2 | 2007-11-12 11:37:42 +0800 | [diff] [blame] | 76 | 7, 13, 35, 58, 0, 0, 0, 80, 93, 113, 123, 125 |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 79 | static s32 iwl3945_expected_tpt_a[IWL_RATE_COUNT] = { |
Mohamed Abbas | 14577f2 | 2007-11-12 11:37:42 +0800 | [diff] [blame] | 80 | 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186 |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 81 | }; |
| 82 | |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 83 | static s32 iwl3945_expected_tpt_b[IWL_RATE_COUNT] = { |
Mohamed Abbas | 14577f2 | 2007-11-12 11:37:42 +0800 | [diff] [blame] | 84 | 7, 13, 35, 58, 0, 0, 0, 0, 0, 0, 0, 0 |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 87 | struct iwl3945_tpt_entry { |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 88 | s8 min_rssi; |
| 89 | u8 index; |
| 90 | }; |
| 91 | |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 92 | static struct iwl3945_tpt_entry iwl3945_tpt_table_a[] = { |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 93 | {-60, IWL_RATE_54M_INDEX}, |
| 94 | {-64, IWL_RATE_48M_INDEX}, |
| 95 | {-72, IWL_RATE_36M_INDEX}, |
| 96 | {-80, IWL_RATE_24M_INDEX}, |
| 97 | {-84, IWL_RATE_18M_INDEX}, |
| 98 | {-85, IWL_RATE_12M_INDEX}, |
| 99 | {-87, IWL_RATE_9M_INDEX}, |
| 100 | {-89, IWL_RATE_6M_INDEX} |
| 101 | }; |
| 102 | |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 103 | static struct iwl3945_tpt_entry iwl3945_tpt_table_g[] = { |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 104 | {-60, IWL_RATE_54M_INDEX}, |
| 105 | {-64, IWL_RATE_48M_INDEX}, |
| 106 | {-68, IWL_RATE_36M_INDEX}, |
| 107 | {-80, IWL_RATE_24M_INDEX}, |
| 108 | {-84, IWL_RATE_18M_INDEX}, |
| 109 | {-85, IWL_RATE_12M_INDEX}, |
| 110 | {-86, IWL_RATE_11M_INDEX}, |
| 111 | {-88, IWL_RATE_5M_INDEX}, |
| 112 | {-90, IWL_RATE_2M_INDEX}, |
| 113 | {-92, IWL_RATE_1M_INDEX} |
| 114 | }; |
| 115 | |
| 116 | #define IWL_RATE_MAX_WINDOW 62 |
| 117 | #define IWL_RATE_FLUSH (3*HZ/10) |
| 118 | #define IWL_RATE_WIN_FLUSH (HZ/2) |
| 119 | #define IWL_RATE_HIGH_TH 11520 |
| 120 | #define IWL_RATE_MIN_FAILURE_TH 8 |
| 121 | #define IWL_RATE_MIN_SUCCESS_TH 8 |
| 122 | #define IWL_RATE_DECREASE_TH 1920 |
| 123 | |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 124 | static u8 iwl3945_get_rate_index_by_rssi(s32 rssi, enum ieee80211_band band) |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 125 | { |
| 126 | u32 index = 0; |
| 127 | u32 table_size = 0; |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 128 | struct iwl3945_tpt_entry *tpt_table = NULL; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 129 | |
| 130 | if ((rssi < IWL_MIN_RSSI_VAL) || (rssi > IWL_MAX_RSSI_VAL)) |
| 131 | rssi = IWL_MIN_RSSI_VAL; |
| 132 | |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 133 | switch (band) { |
| 134 | case IEEE80211_BAND_2GHZ: |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 135 | tpt_table = iwl3945_tpt_table_g; |
| 136 | table_size = ARRAY_SIZE(iwl3945_tpt_table_g); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 137 | break; |
| 138 | |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 139 | case IEEE80211_BAND_5GHZ: |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 140 | tpt_table = iwl3945_tpt_table_a; |
| 141 | table_size = ARRAY_SIZE(iwl3945_tpt_table_a); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 142 | break; |
| 143 | |
| 144 | default: |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 145 | BUG(); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 146 | break; |
| 147 | } |
| 148 | |
| 149 | while ((index < table_size) && (rssi < tpt_table[index].min_rssi)) |
| 150 | index++; |
| 151 | |
| 152 | index = min(index, (table_size - 1)); |
| 153 | |
| 154 | return tpt_table[index].index; |
| 155 | } |
| 156 | |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 157 | static void iwl3945_clear_window(struct iwl3945_rate_scale_data *window) |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 158 | { |
| 159 | window->data = 0; |
| 160 | window->success_counter = 0; |
Tomas Winkler | 28447f3 | 2008-03-06 17:36:54 -0800 | [diff] [blame] | 161 | window->success_ratio = -1; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 162 | window->counter = 0; |
Tomas Winkler | 28447f3 | 2008-03-06 17:36:54 -0800 | [diff] [blame] | 163 | window->average_tpt = IWL_INV_TPT; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 164 | window->stamp = 0; |
| 165 | } |
| 166 | |
| 167 | /** |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 168 | * iwl3945_rate_scale_flush_windows - flush out the rate scale windows |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 169 | * |
| 170 | * Returns the number of windows that have gathered data but were |
| 171 | * not flushed. If there were any that were not flushed, then |
| 172 | * reschedule the rate flushing routine. |
| 173 | */ |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 174 | static int iwl3945_rate_scale_flush_windows(struct iwl3945_rs_sta *rs_sta) |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 175 | { |
| 176 | int unflushed = 0; |
| 177 | int i; |
| 178 | unsigned long flags; |
| 179 | |
| 180 | /* |
| 181 | * For each rate, if we have collected data on that rate |
| 182 | * and it has been more than IWL_RATE_WIN_FLUSH |
| 183 | * since we flushed, clear out the gathered statistics |
| 184 | */ |
| 185 | for (i = 0; i < IWL_RATE_COUNT; i++) { |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 186 | if (!rs_sta->win[i].counter) |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 187 | continue; |
| 188 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 189 | spin_lock_irqsave(&rs_sta->lock, flags); |
| 190 | if (time_after(jiffies, rs_sta->win[i].stamp + |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 191 | IWL_RATE_WIN_FLUSH)) { |
| 192 | IWL_DEBUG_RATE("flushing %d samples of rate " |
| 193 | "index %d\n", |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 194 | rs_sta->win[i].counter, i); |
| 195 | iwl3945_clear_window(&rs_sta->win[i]); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 196 | } else |
| 197 | unflushed++; |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 198 | spin_unlock_irqrestore(&rs_sta->lock, flags); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | return unflushed; |
| 202 | } |
| 203 | |
| 204 | #define IWL_RATE_FLUSH_MAX 5000 /* msec */ |
| 205 | #define IWL_RATE_FLUSH_MIN 50 /* msec */ |
| 206 | |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 207 | static void iwl3945_bg_rate_scale_flush(unsigned long data) |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 208 | { |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 209 | struct iwl3945_rs_sta *rs_sta = (void *)data; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 210 | int unflushed = 0; |
| 211 | unsigned long flags; |
| 212 | u32 packet_count, duration, pps; |
| 213 | |
| 214 | IWL_DEBUG_RATE("enter\n"); |
| 215 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 216 | unflushed = iwl3945_rate_scale_flush_windows(rs_sta); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 217 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 218 | spin_lock_irqsave(&rs_sta->lock, flags); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 219 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 220 | rs_sta->flush_pending = 0; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 221 | |
| 222 | /* Number of packets Rx'd since last time this timer ran */ |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 223 | packet_count = (rs_sta->tx_packets - rs_sta->last_tx_packets) + 1; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 224 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 225 | rs_sta->last_tx_packets = rs_sta->tx_packets + 1; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 226 | |
| 227 | if (unflushed) { |
| 228 | duration = |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 229 | jiffies_to_msecs(jiffies - rs_sta->last_partial_flush); |
| 230 | /* duration = jiffies_to_msecs(rs_sta->flush_time); */ |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 231 | |
| 232 | IWL_DEBUG_RATE("Tx'd %d packets in %dms\n", |
| 233 | packet_count, duration); |
| 234 | |
| 235 | /* Determine packets per second */ |
| 236 | if (duration) |
| 237 | pps = (packet_count * 1000) / duration; |
| 238 | else |
| 239 | pps = 0; |
| 240 | |
| 241 | if (pps) { |
| 242 | duration = IWL_RATE_FLUSH_MAX / pps; |
| 243 | if (duration < IWL_RATE_FLUSH_MIN) |
| 244 | duration = IWL_RATE_FLUSH_MIN; |
| 245 | } else |
| 246 | duration = IWL_RATE_FLUSH_MAX; |
| 247 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 248 | rs_sta->flush_time = msecs_to_jiffies(duration); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 249 | |
| 250 | IWL_DEBUG_RATE("new flush period: %d msec ave %d\n", |
| 251 | duration, packet_count); |
| 252 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 253 | mod_timer(&rs_sta->rate_scale_flush, jiffies + |
| 254 | rs_sta->flush_time); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 255 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 256 | rs_sta->last_partial_flush = jiffies; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /* If there weren't any unflushed entries, we don't schedule the timer |
| 260 | * to run again */ |
| 261 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 262 | rs_sta->last_flush = jiffies; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 263 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 264 | spin_unlock_irqrestore(&rs_sta->lock, flags); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 265 | |
| 266 | IWL_DEBUG_RATE("leave\n"); |
| 267 | } |
| 268 | |
| 269 | /** |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 270 | * iwl3945_collect_tx_data - Update the success/failure sliding window |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 271 | * |
| 272 | * We keep a sliding window of the last 64 packets transmitted |
| 273 | * at this rate. window->data contains the bitmask of successful |
| 274 | * packets. |
| 275 | */ |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 276 | static void iwl3945_collect_tx_data(struct iwl3945_rs_sta *rs_sta, |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 277 | struct iwl3945_rate_scale_data *window, |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 278 | int success, int retries) |
| 279 | { |
| 280 | unsigned long flags; |
| 281 | |
| 282 | if (!retries) { |
| 283 | IWL_DEBUG_RATE("leave: retries == 0 -- should be at least 1\n"); |
| 284 | return; |
| 285 | } |
| 286 | |
| 287 | while (retries--) { |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 288 | spin_lock_irqsave(&rs_sta->lock, flags); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 289 | |
| 290 | /* If we have filled up the window then subtract one from the |
| 291 | * success counter if the high-bit is counting toward |
| 292 | * success */ |
| 293 | if (window->counter == IWL_RATE_MAX_WINDOW) { |
| 294 | if (window->data & (1ULL << (IWL_RATE_MAX_WINDOW - 1))) |
| 295 | window->success_counter--; |
| 296 | } else |
| 297 | window->counter++; |
| 298 | |
| 299 | /* Slide the window to the left one bit */ |
| 300 | window->data = (window->data << 1); |
| 301 | |
| 302 | /* If this packet was a success then set the low bit high */ |
| 303 | if (success) { |
| 304 | window->success_counter++; |
| 305 | window->data |= 1; |
| 306 | } |
| 307 | |
| 308 | /* window->counter can't be 0 -- it is either >0 or |
| 309 | * IWL_RATE_MAX_WINDOW */ |
| 310 | window->success_ratio = 12800 * window->success_counter / |
| 311 | window->counter; |
| 312 | |
| 313 | /* Tag this window as having been updated */ |
| 314 | window->stamp = jiffies; |
| 315 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 316 | spin_unlock_irqrestore(&rs_sta->lock, flags); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
Johannes Berg | 4b7679a | 2008-09-18 18:14:18 +0200 | [diff] [blame] | 320 | static void rs_rate_init(void *priv, struct ieee80211_supported_band *sband, |
| 321 | struct ieee80211_sta *sta, void *priv_sta) |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 322 | { |
Johannes Berg | 4b7679a | 2008-09-18 18:14:18 +0200 | [diff] [blame] | 323 | struct iwl3945_rs_sta *rs_sta = priv_sta; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 324 | int i; |
| 325 | |
| 326 | IWL_DEBUG_RATE("enter\n"); |
| 327 | |
| 328 | /* TODO: what is a good starting rate for STA? About middle? Maybe not |
| 329 | * the lowest or the highest rate.. Could consider using RSSI from |
| 330 | * previous packets? Need to have IEEE 802.1X auth succeed immediately |
| 331 | * after assoc.. */ |
| 332 | |
| 333 | for (i = IWL_RATE_COUNT - 1; i >= 0; i--) { |
Johannes Berg | 4b7679a | 2008-09-18 18:14:18 +0200 | [diff] [blame] | 334 | if (sta->supp_rates[sband->band] & (1 << i)) { |
Johannes Berg | ae17e98 | 2008-09-11 03:04:36 +0200 | [diff] [blame] | 335 | rs_sta->last_txrate_idx = i; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 336 | break; |
| 337 | } |
| 338 | } |
| 339 | |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 340 | /* For 5 GHz band it start at IWL_FIRST_OFDM_RATE */ |
Johannes Berg | 4b7679a | 2008-09-18 18:14:18 +0200 | [diff] [blame] | 341 | if (sband->band == IEEE80211_BAND_5GHZ) |
Johannes Berg | b7e3500 | 2008-09-11 02:22:58 +0200 | [diff] [blame] | 342 | rs_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE; |
Mohamed Abbas | 14577f2 | 2007-11-12 11:37:42 +0800 | [diff] [blame] | 343 | |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 344 | IWL_DEBUG_RATE("leave\n"); |
| 345 | } |
| 346 | |
Johannes Berg | 4b7679a | 2008-09-18 18:14:18 +0200 | [diff] [blame] | 347 | static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir) |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 348 | { |
Johannes Berg | 4b7679a | 2008-09-18 18:14:18 +0200 | [diff] [blame] | 349 | return hw->priv; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Ian Schram | 01ebd06 | 2007-10-25 17:15:22 +0800 | [diff] [blame] | 352 | /* rate scale requires free function to be implemented */ |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 353 | static void rs_free(void *priv) |
| 354 | { |
| 355 | return; |
| 356 | } |
Johannes Berg | 4b7679a | 2008-09-18 18:14:18 +0200 | [diff] [blame] | 357 | |
Johannes Berg | 4b7679a | 2008-09-18 18:14:18 +0200 | [diff] [blame] | 358 | static void *rs_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp) |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 359 | { |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 360 | struct iwl3945_rs_sta *rs_sta; |
Johannes Berg | 4b7679a | 2008-09-18 18:14:18 +0200 | [diff] [blame] | 361 | struct iwl3945_sta_priv *psta = (void *) sta->drv_priv; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 362 | int i; |
| 363 | |
Johannes Berg | 4b7679a | 2008-09-18 18:14:18 +0200 | [diff] [blame] | 364 | /* |
| 365 | * XXX: If it's using sta->drv_priv anyway, it might |
| 366 | * as well just put all the information there. |
| 367 | */ |
| 368 | |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 369 | IWL_DEBUG_RATE("enter\n"); |
| 370 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 371 | rs_sta = kzalloc(sizeof(struct iwl3945_rs_sta), gfp); |
| 372 | if (!rs_sta) { |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 373 | IWL_DEBUG_RATE("leave: ENOMEM\n"); |
| 374 | return NULL; |
| 375 | } |
| 376 | |
Johannes Berg | 4b7679a | 2008-09-18 18:14:18 +0200 | [diff] [blame] | 377 | psta->rs_sta = rs_sta; |
| 378 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 379 | spin_lock_init(&rs_sta->lock); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 380 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 381 | rs_sta->start_rate = IWL_RATE_INVALID; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 382 | |
| 383 | /* default to just 802.11b */ |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 384 | rs_sta->expected_tpt = iwl3945_expected_tpt_b; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 385 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 386 | rs_sta->last_partial_flush = jiffies; |
| 387 | rs_sta->last_flush = jiffies; |
| 388 | rs_sta->flush_time = IWL_RATE_FLUSH; |
| 389 | rs_sta->last_tx_packets = 0; |
| 390 | rs_sta->ibss_sta_added = 0; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 391 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 392 | init_timer(&rs_sta->rate_scale_flush); |
| 393 | rs_sta->rate_scale_flush.data = (unsigned long)rs_sta; |
| 394 | rs_sta->rate_scale_flush.function = &iwl3945_bg_rate_scale_flush; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 395 | |
| 396 | for (i = 0; i < IWL_RATE_COUNT; i++) |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 397 | iwl3945_clear_window(&rs_sta->win[i]); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 398 | |
| 399 | IWL_DEBUG_RATE("leave\n"); |
| 400 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 401 | return rs_sta; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Johannes Berg | 4b7679a | 2008-09-18 18:14:18 +0200 | [diff] [blame] | 404 | static void rs_free_sta(void *priv, struct ieee80211_sta *sta, |
| 405 | void *priv_sta) |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 406 | { |
Johannes Berg | 4b7679a | 2008-09-18 18:14:18 +0200 | [diff] [blame] | 407 | struct iwl3945_sta_priv *psta = (void *) sta->drv_priv; |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 408 | struct iwl3945_rs_sta *rs_sta = priv_sta; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 409 | |
Johannes Berg | 4b7679a | 2008-09-18 18:14:18 +0200 | [diff] [blame] | 410 | psta->rs_sta = NULL; |
| 411 | |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 412 | IWL_DEBUG_RATE("enter\n"); |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 413 | del_timer_sync(&rs_sta->rate_scale_flush); |
| 414 | kfree(rs_sta); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 415 | IWL_DEBUG_RATE("leave\n"); |
| 416 | } |
| 417 | |
Mohamed Abbas | 14577f2 | 2007-11-12 11:37:42 +0800 | [diff] [blame] | 418 | |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 419 | /** |
| 420 | * rs_tx_status - Update rate control values based on Tx results |
| 421 | * |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 422 | * NOTE: Uses iwl3945_priv->retry_rate for the # of retries attempted by |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 423 | * the hardware for each rate. |
| 424 | */ |
Johannes Berg | 4b7679a | 2008-09-18 18:14:18 +0200 | [diff] [blame] | 425 | static void rs_tx_status(void *priv_rate, struct ieee80211_supported_band *sband, |
| 426 | struct ieee80211_sta *sta, void *priv_sta, |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 427 | struct sk_buff *skb) |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 428 | { |
Johannes Berg | e6a9854 | 2008-10-21 12:40:02 +0200 | [diff] [blame] | 429 | u8 retries = 0, current_count; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 430 | int scale_rate_index, first_index, last_index; |
| 431 | unsigned long flags; |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 432 | struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_rate; |
Johannes Berg | 4b7679a | 2008-09-18 18:14:18 +0200 | [diff] [blame] | 433 | struct iwl3945_rs_sta *rs_sta = priv_sta; |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 434 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); |
Johannes Berg | e6a9854 | 2008-10-21 12:40:02 +0200 | [diff] [blame] | 435 | int i; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 436 | |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 437 | IWL_DEBUG_RATE("enter\n"); |
| 438 | |
Johannes Berg | e6a9854 | 2008-10-21 12:40:02 +0200 | [diff] [blame] | 439 | for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) |
| 440 | retries += info->status.rates[i].count; |
| 441 | retries--; |
| 442 | |
| 443 | first_index = sband->bitrates[info->status.rates[0].idx].hw_value; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 444 | if ((first_index < 0) || (first_index >= IWL_RATE_COUNT)) { |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 445 | IWL_DEBUG_RATE("leave: Rate out of bounds: %d\n", first_index); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 446 | return; |
| 447 | } |
| 448 | |
Johannes Berg | 4b7679a | 2008-09-18 18:14:18 +0200 | [diff] [blame] | 449 | if (!priv_sta) { |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 450 | IWL_DEBUG_RATE("leave: No STA priv data to update!\n"); |
| 451 | return; |
| 452 | } |
| 453 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 454 | rs_sta->tx_packets++; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 455 | |
| 456 | scale_rate_index = first_index; |
| 457 | last_index = first_index; |
| 458 | |
| 459 | /* |
| 460 | * Update the window for each rate. We determine which rates |
| 461 | * were Tx'd based on the total number of retries vs. the number |
| 462 | * of retries configured for each rate -- currently set to the |
| 463 | * priv value 'retry_rate' vs. rate specific |
| 464 | * |
| 465 | * On exit from this while loop last_index indicates the rate |
| 466 | * at which the frame was finally transmitted (or failed if no |
| 467 | * ACK) |
| 468 | */ |
| 469 | while (retries > 0) { |
| 470 | if (retries < priv->retry_rate) { |
| 471 | current_count = retries; |
| 472 | last_index = scale_rate_index; |
| 473 | } else { |
| 474 | current_count = priv->retry_rate; |
Johannes Berg | e6a9854 | 2008-10-21 12:40:02 +0200 | [diff] [blame] | 475 | last_index = iwl3945_rs_next_rate(priv, |
Mohamed Abbas | 14577f2 | 2007-11-12 11:37:42 +0800 | [diff] [blame] | 476 | scale_rate_index); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /* Update this rate accounting for as many retries |
| 480 | * as was used for it (per current_count) */ |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 481 | iwl3945_collect_tx_data(rs_sta, |
| 482 | &rs_sta->win[scale_rate_index], |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 483 | 0, current_count); |
| 484 | IWL_DEBUG_RATE("Update rate %d for %d retries.\n", |
| 485 | scale_rate_index, current_count); |
| 486 | |
| 487 | retries -= current_count; |
| 488 | |
| 489 | if (retries) |
| 490 | scale_rate_index = |
Johannes Berg | e6a9854 | 2008-10-21 12:40:02 +0200 | [diff] [blame] | 491 | iwl3945_rs_next_rate(priv, scale_rate_index); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Mohamed Abbas | 14577f2 | 2007-11-12 11:37:42 +0800 | [diff] [blame] | 494 | |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 495 | /* Update the last index window with success/failure based on ACK */ |
| 496 | IWL_DEBUG_RATE("Update rate %d with %s.\n", |
| 497 | last_index, |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 498 | (info->flags & IEEE80211_TX_STAT_ACK) ? |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 499 | "success" : "failure"); |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 500 | iwl3945_collect_tx_data(rs_sta, |
| 501 | &rs_sta->win[last_index], |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 502 | info->flags & IEEE80211_TX_STAT_ACK, 1); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 503 | |
| 504 | /* We updated the rate scale window -- if its been more than |
| 505 | * flush_time since the last run, schedule the flush |
| 506 | * again */ |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 507 | spin_lock_irqsave(&rs_sta->lock, flags); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 508 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 509 | if (!rs_sta->flush_pending && |
| 510 | time_after(jiffies, rs_sta->last_partial_flush + |
| 511 | rs_sta->flush_time)) { |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 512 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 513 | rs_sta->flush_pending = 1; |
| 514 | mod_timer(&rs_sta->rate_scale_flush, |
| 515 | jiffies + rs_sta->flush_time); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 518 | spin_unlock_irqrestore(&rs_sta->lock, flags); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 519 | |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 520 | IWL_DEBUG_RATE("leave\n"); |
| 521 | |
| 522 | return; |
| 523 | } |
| 524 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 525 | static u16 iwl3945_get_adjacent_rate(struct iwl3945_rs_sta *rs_sta, |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 526 | u8 index, u16 rate_mask, enum ieee80211_band band) |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 527 | { |
| 528 | u8 high = IWL_RATE_INVALID; |
| 529 | u8 low = IWL_RATE_INVALID; |
| 530 | |
Ian Schram | 01ebd06 | 2007-10-25 17:15:22 +0800 | [diff] [blame] | 531 | /* 802.11A walks to the next literal adjacent rate in |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 532 | * the rate table */ |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 533 | if (unlikely(band == IEEE80211_BAND_5GHZ)) { |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 534 | int i; |
| 535 | u32 mask; |
| 536 | |
| 537 | /* Find the previous rate that is in the rate mask */ |
| 538 | i = index - 1; |
| 539 | for (mask = (1 << i); i >= 0; i--, mask >>= 1) { |
| 540 | if (rate_mask & mask) { |
| 541 | low = i; |
| 542 | break; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | /* Find the next rate that is in the rate mask */ |
| 547 | i = index + 1; |
| 548 | for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) { |
| 549 | if (rate_mask & mask) { |
| 550 | high = i; |
| 551 | break; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | return (high << 8) | low; |
| 556 | } |
| 557 | |
| 558 | low = index; |
| 559 | while (low != IWL_RATE_INVALID) { |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 560 | if (rs_sta->tgg) |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 561 | low = iwl3945_rates[low].prev_rs_tgg; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 562 | else |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 563 | low = iwl3945_rates[low].prev_rs; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 564 | if (low == IWL_RATE_INVALID) |
| 565 | break; |
| 566 | if (rate_mask & (1 << low)) |
| 567 | break; |
| 568 | IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low); |
| 569 | } |
| 570 | |
| 571 | high = index; |
| 572 | while (high != IWL_RATE_INVALID) { |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 573 | if (rs_sta->tgg) |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 574 | high = iwl3945_rates[high].next_rs_tgg; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 575 | else |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 576 | high = iwl3945_rates[high].next_rs; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 577 | if (high == IWL_RATE_INVALID) |
| 578 | break; |
| 579 | if (rate_mask & (1 << high)) |
| 580 | break; |
| 581 | IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high); |
| 582 | } |
| 583 | |
| 584 | return (high << 8) | low; |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * rs_get_rate - find the rate for the requested packet |
| 589 | * |
| 590 | * Returns the ieee80211_rate structure allocated by the driver. |
| 591 | * |
| 592 | * The rate control algorithm has no internal mapping between hw_mode's |
| 593 | * rate ordering and the rate ordering used by the rate control algorithm. |
| 594 | * |
| 595 | * The rate control algorithm uses a single table of rates that goes across |
| 596 | * the entire A/B/G spectrum vs. being limited to just one particular |
| 597 | * hw_mode. |
| 598 | * |
| 599 | * As such, we can't convert the index obtained below into the hw_mode's |
| 600 | * rate table and must reference the driver allocated rate table |
| 601 | * |
| 602 | */ |
Johannes Berg | e6a9854 | 2008-10-21 12:40:02 +0200 | [diff] [blame] | 603 | static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, |
| 604 | void *priv_sta, struct ieee80211_tx_rate_control *txrc) |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 605 | { |
Johannes Berg | e6a9854 | 2008-10-21 12:40:02 +0200 | [diff] [blame] | 606 | struct ieee80211_supported_band *sband = txrc->sband; |
| 607 | struct sk_buff *skb = txrc->skb; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 608 | u8 low = IWL_RATE_INVALID; |
| 609 | u8 high = IWL_RATE_INVALID; |
| 610 | u16 high_low; |
| 611 | int index; |
Johannes Berg | 4b7679a | 2008-09-18 18:14:18 +0200 | [diff] [blame] | 612 | struct iwl3945_rs_sta *rs_sta = priv_sta; |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 613 | struct iwl3945_rate_scale_data *window = NULL; |
Tomas Winkler | 28447f3 | 2008-03-06 17:36:54 -0800 | [diff] [blame] | 614 | int current_tpt = IWL_INV_TPT; |
| 615 | int low_tpt = IWL_INV_TPT; |
| 616 | int high_tpt = IWL_INV_TPT; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 617 | u32 fail_count; |
| 618 | s8 scale_action = 0; |
| 619 | unsigned long flags; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 620 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; |
Michael Wu | 2bc454b | 2007-12-25 19:33:16 -0500 | [diff] [blame] | 621 | u16 fc, rate_mask; |
Johannes Berg | 4b7679a | 2008-09-18 18:14:18 +0200 | [diff] [blame] | 622 | struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_r; |
Johannes Berg | e6a9854 | 2008-10-21 12:40:02 +0200 | [diff] [blame] | 623 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 624 | |
| 625 | IWL_DEBUG_RATE("enter\n"); |
| 626 | |
Michael Wu | 2bc454b | 2007-12-25 19:33:16 -0500 | [diff] [blame] | 627 | /* Send management frames and broadcast/multicast data using lowest |
| 628 | * rate. */ |
| 629 | fc = le16_to_cpu(hdr->frame_control); |
| 630 | if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA || |
| 631 | is_multicast_ether_addr(hdr->addr1) || |
Johannes Berg | 4b7679a | 2008-09-18 18:14:18 +0200 | [diff] [blame] | 632 | !sta || !priv_sta) { |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 633 | IWL_DEBUG_RATE("leave: No STA priv data to update!\n"); |
Johannes Berg | e6a9854 | 2008-10-21 12:40:02 +0200 | [diff] [blame] | 634 | info->control.rates[0].idx = rate_lowest_index(sband, sta); |
Mattias Nissler | 1abbe49 | 2007-12-20 13:50:07 +0100 | [diff] [blame] | 635 | return; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 636 | } |
| 637 | |
Johannes Berg | 4b7679a | 2008-09-18 18:14:18 +0200 | [diff] [blame] | 638 | rate_mask = sta->supp_rates[sband->band]; |
Johannes Berg | b7e3500 | 2008-09-11 02:22:58 +0200 | [diff] [blame] | 639 | index = min(rs_sta->last_txrate_idx & 0xffff, IWL_RATE_COUNT - 1); |
Mohamed Abbas | 14577f2 | 2007-11-12 11:37:42 +0800 | [diff] [blame] | 640 | |
Tomas Winkler | 28447f3 | 2008-03-06 17:36:54 -0800 | [diff] [blame] | 641 | if (sband->band == IEEE80211_BAND_5GHZ) |
Mohamed Abbas | 14577f2 | 2007-11-12 11:37:42 +0800 | [diff] [blame] | 642 | rate_mask = rate_mask << IWL_FIRST_OFDM_RATE; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 643 | |
Johannes Berg | 05c914f | 2008-09-11 00:01:58 +0200 | [diff] [blame] | 644 | if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) && |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 645 | !rs_sta->ibss_sta_added) { |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 646 | u8 sta_id = iwl3945_hw_find_station(priv, hdr->addr1); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 647 | |
| 648 | if (sta_id == IWL_INVALID_STATION) { |
Johannes Berg | e174961 | 2008-10-27 15:59:26 -0700 | [diff] [blame] | 649 | IWL_DEBUG_RATE("LQ: ADD station %pm\n", |
| 650 | hdr->addr1); |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 651 | sta_id = iwl3945_add_station(priv, |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 652 | hdr->addr1, 0, CMD_ASYNC); |
| 653 | } |
| 654 | if (sta_id != IWL_INVALID_STATION) |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 655 | rs_sta->ibss_sta_added = 1; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 656 | } |
| 657 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 658 | spin_lock_irqsave(&rs_sta->lock, flags); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 659 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 660 | if (rs_sta->start_rate != IWL_RATE_INVALID) { |
| 661 | index = rs_sta->start_rate; |
| 662 | rs_sta->start_rate = IWL_RATE_INVALID; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 665 | window = &(rs_sta->win[index]); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 666 | |
| 667 | fail_count = window->counter - window->success_counter; |
| 668 | |
| 669 | if (((fail_count <= IWL_RATE_MIN_FAILURE_TH) && |
| 670 | (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))) { |
Tomas Winkler | 28447f3 | 2008-03-06 17:36:54 -0800 | [diff] [blame] | 671 | window->average_tpt = IWL_INV_TPT; |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 672 | spin_unlock_irqrestore(&rs_sta->lock, flags); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 673 | |
| 674 | IWL_DEBUG_RATE("Invalid average_tpt on rate %d: " |
| 675 | "counter: %d, success_counter: %d, " |
| 676 | "expected_tpt is %sNULL\n", |
| 677 | index, |
| 678 | window->counter, |
| 679 | window->success_counter, |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 680 | rs_sta->expected_tpt ? "not " : ""); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 681 | goto out; |
| 682 | |
| 683 | } |
| 684 | |
| 685 | window->average_tpt = ((window->success_ratio * |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 686 | rs_sta->expected_tpt[index] + 64) / 128); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 687 | current_tpt = window->average_tpt; |
| 688 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 689 | high_low = iwl3945_get_adjacent_rate(rs_sta, index, rate_mask, |
Tomas Winkler | 28447f3 | 2008-03-06 17:36:54 -0800 | [diff] [blame] | 690 | sband->band); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 691 | low = high_low & 0xff; |
| 692 | high = (high_low >> 8) & 0xff; |
| 693 | |
| 694 | if (low != IWL_RATE_INVALID) |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 695 | low_tpt = rs_sta->win[low].average_tpt; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 696 | |
| 697 | if (high != IWL_RATE_INVALID) |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 698 | high_tpt = rs_sta->win[high].average_tpt; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 699 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 700 | spin_unlock_irqrestore(&rs_sta->lock, flags); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 701 | |
| 702 | scale_action = 1; |
| 703 | |
| 704 | if ((window->success_ratio < IWL_RATE_DECREASE_TH) || !current_tpt) { |
| 705 | IWL_DEBUG_RATE("decrease rate because of low success_ratio\n"); |
| 706 | scale_action = -1; |
Tomas Winkler | 28447f3 | 2008-03-06 17:36:54 -0800 | [diff] [blame] | 707 | } else if ((low_tpt == IWL_INV_TPT) && (high_tpt == IWL_INV_TPT)) |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 708 | scale_action = 1; |
Tomas Winkler | 28447f3 | 2008-03-06 17:36:54 -0800 | [diff] [blame] | 709 | else if ((low_tpt != IWL_INV_TPT) && (high_tpt != IWL_INV_TPT) && |
| 710 | (low_tpt < current_tpt) && (high_tpt < current_tpt)) { |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 711 | IWL_DEBUG_RATE("No action -- low [%d] & high [%d] < " |
| 712 | "current_tpt [%d]\n", |
| 713 | low_tpt, high_tpt, current_tpt); |
| 714 | scale_action = 0; |
| 715 | } else { |
Tomas Winkler | 28447f3 | 2008-03-06 17:36:54 -0800 | [diff] [blame] | 716 | if (high_tpt != IWL_INV_TPT) { |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 717 | if (high_tpt > current_tpt) |
| 718 | scale_action = 1; |
| 719 | else { |
| 720 | IWL_DEBUG_RATE |
| 721 | ("decrease rate because of high tpt\n"); |
| 722 | scale_action = -1; |
| 723 | } |
Tomas Winkler | 28447f3 | 2008-03-06 17:36:54 -0800 | [diff] [blame] | 724 | } else if (low_tpt != IWL_INV_TPT) { |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 725 | if (low_tpt > current_tpt) { |
| 726 | IWL_DEBUG_RATE |
| 727 | ("decrease rate because of low tpt\n"); |
| 728 | scale_action = -1; |
| 729 | } else |
| 730 | scale_action = 1; |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | if ((window->success_ratio > IWL_RATE_HIGH_TH) || |
| 735 | (current_tpt > window->average_tpt)) { |
| 736 | IWL_DEBUG_RATE("No action -- success_ratio [%d] > HIGH_TH or " |
| 737 | "current_tpt [%d] > average_tpt [%d]\n", |
| 738 | window->success_ratio, |
| 739 | current_tpt, window->average_tpt); |
| 740 | scale_action = 0; |
| 741 | } |
| 742 | |
| 743 | switch (scale_action) { |
| 744 | case -1: |
| 745 | if (low != IWL_RATE_INVALID) |
| 746 | index = low; |
| 747 | break; |
| 748 | |
| 749 | case 1: |
| 750 | if (high != IWL_RATE_INVALID) |
| 751 | index = high; |
| 752 | |
| 753 | break; |
| 754 | |
| 755 | case 0: |
| 756 | default: |
| 757 | break; |
| 758 | } |
| 759 | |
| 760 | IWL_DEBUG_RATE("Selected %d (action %d) - low %d high %d\n", |
| 761 | index, scale_action, low, high); |
| 762 | |
| 763 | out: |
| 764 | |
Johannes Berg | b7e3500 | 2008-09-11 02:22:58 +0200 | [diff] [blame] | 765 | rs_sta->last_txrate_idx = index; |
Tomas Winkler | 28447f3 | 2008-03-06 17:36:54 -0800 | [diff] [blame] | 766 | if (sband->band == IEEE80211_BAND_5GHZ) |
Johannes Berg | e6a9854 | 2008-10-21 12:40:02 +0200 | [diff] [blame] | 767 | info->control.rates[0].idx = rs_sta->last_txrate_idx - |
| 768 | IWL_FIRST_OFDM_RATE; |
Mohamed Abbas | 14577f2 | 2007-11-12 11:37:42 +0800 | [diff] [blame] | 769 | else |
Johannes Berg | e6a9854 | 2008-10-21 12:40:02 +0200 | [diff] [blame] | 770 | info->control.rates[0].idx = rs_sta->last_txrate_idx; |
Mohamed Abbas | 14577f2 | 2007-11-12 11:37:42 +0800 | [diff] [blame] | 771 | |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 772 | IWL_DEBUG_RATE("leave: %d\n", index); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | static struct rate_control_ops rs_ops = { |
| 776 | .module = NULL, |
| 777 | .name = RS_NAME, |
| 778 | .tx_status = rs_tx_status, |
| 779 | .get_rate = rs_get_rate, |
| 780 | .rate_init = rs_rate_init, |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 781 | .alloc = rs_alloc, |
| 782 | .free = rs_free, |
| 783 | .alloc_sta = rs_alloc_sta, |
| 784 | .free_sta = rs_free_sta, |
| 785 | }; |
| 786 | |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 787 | void iwl3945_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id) |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 788 | { |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 789 | struct iwl3945_priv *priv = hw->priv; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 790 | s32 rssi = 0; |
| 791 | unsigned long flags; |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 792 | struct iwl3945_rs_sta *rs_sta; |
Johannes Berg | 4b7679a | 2008-09-18 18:14:18 +0200 | [diff] [blame] | 793 | struct ieee80211_sta *sta; |
| 794 | struct iwl3945_sta_priv *psta; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 795 | |
| 796 | IWL_DEBUG_RATE("enter\n"); |
| 797 | |
Johannes Berg | d0709a6 | 2008-02-25 16:27:46 +0100 | [diff] [blame] | 798 | rcu_read_lock(); |
| 799 | |
Johannes Berg | 4b7679a | 2008-09-18 18:14:18 +0200 | [diff] [blame] | 800 | sta = ieee80211_find_sta(hw, priv->stations[sta_id].sta.sta.addr); |
Johannes Berg | 5251818 | 2008-10-11 01:46:25 +0200 | [diff] [blame] | 801 | if (!sta) { |
Johannes Berg | d0709a6 | 2008-02-25 16:27:46 +0100 | [diff] [blame] | 802 | rcu_read_unlock(); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 803 | return; |
| 804 | } |
| 805 | |
Johannes Berg | 5251818 | 2008-10-11 01:46:25 +0200 | [diff] [blame] | 806 | psta = (void *) sta->drv_priv; |
Johannes Berg | 4b7679a | 2008-09-18 18:14:18 +0200 | [diff] [blame] | 807 | rs_sta = psta->rs_sta; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 808 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 809 | spin_lock_irqsave(&rs_sta->lock, flags); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 810 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 811 | rs_sta->tgg = 0; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 812 | switch (priv->band) { |
| 813 | case IEEE80211_BAND_2GHZ: |
| 814 | /* TODO: this always does G, not a regression */ |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 815 | if (priv->active_rxon.flags & RXON_FLG_TGG_PROTECT_MSK) { |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 816 | rs_sta->tgg = 1; |
| 817 | rs_sta->expected_tpt = iwl3945_expected_tpt_g_prot; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 818 | } else |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 819 | rs_sta->expected_tpt = iwl3945_expected_tpt_g; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 820 | break; |
| 821 | |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 822 | case IEEE80211_BAND_5GHZ: |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 823 | rs_sta->expected_tpt = iwl3945_expected_tpt_a; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 824 | break; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 825 | case IEEE80211_NUM_BANDS: |
| 826 | BUG(); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 827 | break; |
| 828 | } |
| 829 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 830 | spin_unlock_irqrestore(&rs_sta->lock, flags); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 831 | |
| 832 | rssi = priv->last_rx_rssi; |
| 833 | if (rssi == 0) |
| 834 | rssi = IWL_MIN_RSSI_VAL; |
| 835 | |
| 836 | IWL_DEBUG(IWL_DL_INFO | IWL_DL_RATE, "Network RSSI: %d\n", rssi); |
| 837 | |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 838 | rs_sta->start_rate = iwl3945_get_rate_index_by_rssi(rssi, priv->band); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 839 | |
| 840 | IWL_DEBUG_RATE("leave: rssi %d assign rate index: " |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame] | 841 | "%d (plcp 0x%x)\n", rssi, rs_sta->start_rate, |
| 842 | iwl3945_rates[rs_sta->start_rate].plcp); |
Johannes Berg | 5251818 | 2008-10-11 01:46:25 +0200 | [diff] [blame] | 843 | rcu_read_unlock(); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 844 | } |
| 845 | |
Reinette Chatre | 897e1cf | 2008-03-28 16:21:09 -0700 | [diff] [blame] | 846 | int iwl3945_rate_control_register(void) |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 847 | { |
Reinette Chatre | 897e1cf | 2008-03-28 16:21:09 -0700 | [diff] [blame] | 848 | return ieee80211_rate_control_register(&rs_ops); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 849 | } |
| 850 | |
Reinette Chatre | 897e1cf | 2008-03-28 16:21:09 -0700 | [diff] [blame] | 851 | void iwl3945_rate_control_unregister(void) |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 852 | { |
| 853 | ieee80211_rate_control_unregister(&rs_ops); |
| 854 | } |
| 855 | |
| 856 | |