Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Copyright(c) 2005 - 2007 Intel Corporation. All rights reserved. |
| 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> |
| 32 | #include <net/ieee80211.h> |
| 33 | |
| 34 | #include <linux/netdevice.h> |
| 35 | #include <linux/etherdevice.h> |
| 36 | #include <linux/delay.h> |
| 37 | |
| 38 | #include <linux/workqueue.h> |
| 39 | |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 40 | #include "../net/mac80211/ieee80211_rate.h" |
| 41 | |
Christoph Hellwig | 5d08cd1 | 2007-10-25 17:15:50 +0800 | [diff] [blame] | 42 | #include "iwl-3945.h" |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 43 | |
| 44 | #define RS_NAME "iwl-3945-rs" |
| 45 | |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 46 | struct iwl3945_rate_scale_data { |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 47 | u64 data; |
| 48 | s32 success_counter; |
| 49 | s32 success_ratio; |
| 50 | s32 counter; |
| 51 | s32 average_tpt; |
| 52 | unsigned long stamp; |
| 53 | }; |
| 54 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 55 | struct iwl3945_rs_sta { |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 56 | spinlock_t lock; |
| 57 | s32 *expected_tpt; |
| 58 | unsigned long last_partial_flush; |
| 59 | unsigned long last_flush; |
| 60 | u32 flush_time; |
| 61 | u32 last_tx_packets; |
| 62 | u32 tx_packets; |
| 63 | u8 tgg; |
| 64 | u8 flush_pending; |
| 65 | u8 start_rate; |
| 66 | u8 ibss_sta_added; |
| 67 | struct timer_list rate_scale_flush; |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 68 | struct iwl3945_rate_scale_data win[IWL_RATE_COUNT]; |
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_b[] = { |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 104 | {-86, IWL_RATE_11M_INDEX}, |
| 105 | {-88, IWL_RATE_5M_INDEX}, |
| 106 | {-90, IWL_RATE_2M_INDEX}, |
| 107 | {-92, IWL_RATE_1M_INDEX} |
| 108 | |
| 109 | }; |
| 110 | |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 111 | static struct iwl3945_tpt_entry iwl3945_tpt_table_g[] = { |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 112 | {-60, IWL_RATE_54M_INDEX}, |
| 113 | {-64, IWL_RATE_48M_INDEX}, |
| 114 | {-68, IWL_RATE_36M_INDEX}, |
| 115 | {-80, IWL_RATE_24M_INDEX}, |
| 116 | {-84, IWL_RATE_18M_INDEX}, |
| 117 | {-85, IWL_RATE_12M_INDEX}, |
| 118 | {-86, IWL_RATE_11M_INDEX}, |
| 119 | {-88, IWL_RATE_5M_INDEX}, |
| 120 | {-90, IWL_RATE_2M_INDEX}, |
| 121 | {-92, IWL_RATE_1M_INDEX} |
| 122 | }; |
| 123 | |
| 124 | #define IWL_RATE_MAX_WINDOW 62 |
| 125 | #define IWL_RATE_FLUSH (3*HZ/10) |
| 126 | #define IWL_RATE_WIN_FLUSH (HZ/2) |
| 127 | #define IWL_RATE_HIGH_TH 11520 |
| 128 | #define IWL_RATE_MIN_FAILURE_TH 8 |
| 129 | #define IWL_RATE_MIN_SUCCESS_TH 8 |
| 130 | #define IWL_RATE_DECREASE_TH 1920 |
| 131 | |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 132 | static u8 iwl3945_get_rate_index_by_rssi(s32 rssi, u8 mode) |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 133 | { |
| 134 | u32 index = 0; |
| 135 | u32 table_size = 0; |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 136 | struct iwl3945_tpt_entry *tpt_table = NULL; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 137 | |
| 138 | if ((rssi < IWL_MIN_RSSI_VAL) || (rssi > IWL_MAX_RSSI_VAL)) |
| 139 | rssi = IWL_MIN_RSSI_VAL; |
| 140 | |
| 141 | switch (mode) { |
| 142 | case MODE_IEEE80211G: |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 143 | tpt_table = iwl3945_tpt_table_g; |
| 144 | table_size = ARRAY_SIZE(iwl3945_tpt_table_g); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 145 | break; |
| 146 | |
| 147 | case MODE_IEEE80211A: |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 148 | tpt_table = iwl3945_tpt_table_a; |
| 149 | table_size = ARRAY_SIZE(iwl3945_tpt_table_a); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 150 | break; |
| 151 | |
| 152 | default: |
| 153 | case MODE_IEEE80211B: |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 154 | tpt_table = iwl3945_tpt_table_b; |
| 155 | table_size = ARRAY_SIZE(iwl3945_tpt_table_b); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 156 | break; |
| 157 | } |
| 158 | |
| 159 | while ((index < table_size) && (rssi < tpt_table[index].min_rssi)) |
| 160 | index++; |
| 161 | |
| 162 | index = min(index, (table_size - 1)); |
| 163 | |
| 164 | return tpt_table[index].index; |
| 165 | } |
| 166 | |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 167 | static void iwl3945_clear_window(struct iwl3945_rate_scale_data *window) |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 168 | { |
| 169 | window->data = 0; |
| 170 | window->success_counter = 0; |
| 171 | window->success_ratio = IWL_INVALID_VALUE; |
| 172 | window->counter = 0; |
| 173 | window->average_tpt = IWL_INVALID_VALUE; |
| 174 | window->stamp = 0; |
| 175 | } |
| 176 | |
| 177 | /** |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 178 | * iwl3945_rate_scale_flush_windows - flush out the rate scale windows |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 179 | * |
| 180 | * Returns the number of windows that have gathered data but were |
| 181 | * not flushed. If there were any that were not flushed, then |
| 182 | * reschedule the rate flushing routine. |
| 183 | */ |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 184 | 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] | 185 | { |
| 186 | int unflushed = 0; |
| 187 | int i; |
| 188 | unsigned long flags; |
| 189 | |
| 190 | /* |
| 191 | * For each rate, if we have collected data on that rate |
| 192 | * and it has been more than IWL_RATE_WIN_FLUSH |
| 193 | * since we flushed, clear out the gathered statistics |
| 194 | */ |
| 195 | for (i = 0; i < IWL_RATE_COUNT; i++) { |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 196 | if (!rs_sta->win[i].counter) |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 197 | continue; |
| 198 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 199 | spin_lock_irqsave(&rs_sta->lock, flags); |
| 200 | if (time_after(jiffies, rs_sta->win[i].stamp + |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 201 | IWL_RATE_WIN_FLUSH)) { |
| 202 | IWL_DEBUG_RATE("flushing %d samples of rate " |
| 203 | "index %d\n", |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 204 | rs_sta->win[i].counter, i); |
| 205 | iwl3945_clear_window(&rs_sta->win[i]); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 206 | } else |
| 207 | unflushed++; |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 208 | spin_unlock_irqrestore(&rs_sta->lock, flags); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | return unflushed; |
| 212 | } |
| 213 | |
| 214 | #define IWL_RATE_FLUSH_MAX 5000 /* msec */ |
| 215 | #define IWL_RATE_FLUSH_MIN 50 /* msec */ |
| 216 | |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 217 | static void iwl3945_bg_rate_scale_flush(unsigned long data) |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 218 | { |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 219 | struct iwl3945_rs_sta *rs_sta = (void *)data; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 220 | int unflushed = 0; |
| 221 | unsigned long flags; |
| 222 | u32 packet_count, duration, pps; |
| 223 | |
| 224 | IWL_DEBUG_RATE("enter\n"); |
| 225 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 226 | unflushed = iwl3945_rate_scale_flush_windows(rs_sta); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 227 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 228 | spin_lock_irqsave(&rs_sta->lock, flags); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 229 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 230 | rs_sta->flush_pending = 0; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 231 | |
| 232 | /* Number of packets Rx'd since last time this timer ran */ |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 233 | packet_count = (rs_sta->tx_packets - rs_sta->last_tx_packets) + 1; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 234 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 235 | rs_sta->last_tx_packets = rs_sta->tx_packets + 1; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 236 | |
| 237 | if (unflushed) { |
| 238 | duration = |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 239 | jiffies_to_msecs(jiffies - rs_sta->last_partial_flush); |
| 240 | /* duration = jiffies_to_msecs(rs_sta->flush_time); */ |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 241 | |
| 242 | IWL_DEBUG_RATE("Tx'd %d packets in %dms\n", |
| 243 | packet_count, duration); |
| 244 | |
| 245 | /* Determine packets per second */ |
| 246 | if (duration) |
| 247 | pps = (packet_count * 1000) / duration; |
| 248 | else |
| 249 | pps = 0; |
| 250 | |
| 251 | if (pps) { |
| 252 | duration = IWL_RATE_FLUSH_MAX / pps; |
| 253 | if (duration < IWL_RATE_FLUSH_MIN) |
| 254 | duration = IWL_RATE_FLUSH_MIN; |
| 255 | } else |
| 256 | duration = IWL_RATE_FLUSH_MAX; |
| 257 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 258 | rs_sta->flush_time = msecs_to_jiffies(duration); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 259 | |
| 260 | IWL_DEBUG_RATE("new flush period: %d msec ave %d\n", |
| 261 | duration, packet_count); |
| 262 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 263 | mod_timer(&rs_sta->rate_scale_flush, jiffies + |
| 264 | rs_sta->flush_time); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 265 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 266 | rs_sta->last_partial_flush = jiffies; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /* If there weren't any unflushed entries, we don't schedule the timer |
| 270 | * to run again */ |
| 271 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 272 | rs_sta->last_flush = jiffies; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 273 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 274 | spin_unlock_irqrestore(&rs_sta->lock, flags); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 275 | |
| 276 | IWL_DEBUG_RATE("leave\n"); |
| 277 | } |
| 278 | |
| 279 | /** |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 280 | * iwl3945_collect_tx_data - Update the success/failure sliding window |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 281 | * |
| 282 | * We keep a sliding window of the last 64 packets transmitted |
| 283 | * at this rate. window->data contains the bitmask of successful |
| 284 | * packets. |
| 285 | */ |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 286 | static void iwl3945_collect_tx_data(struct iwl3945_rs_sta *rs_sta, |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 287 | struct iwl3945_rate_scale_data *window, |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 288 | int success, int retries) |
| 289 | { |
| 290 | unsigned long flags; |
| 291 | |
| 292 | if (!retries) { |
| 293 | IWL_DEBUG_RATE("leave: retries == 0 -- should be at least 1\n"); |
| 294 | return; |
| 295 | } |
| 296 | |
| 297 | while (retries--) { |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 298 | spin_lock_irqsave(&rs_sta->lock, flags); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 299 | |
| 300 | /* If we have filled up the window then subtract one from the |
| 301 | * success counter if the high-bit is counting toward |
| 302 | * success */ |
| 303 | if (window->counter == IWL_RATE_MAX_WINDOW) { |
| 304 | if (window->data & (1ULL << (IWL_RATE_MAX_WINDOW - 1))) |
| 305 | window->success_counter--; |
| 306 | } else |
| 307 | window->counter++; |
| 308 | |
| 309 | /* Slide the window to the left one bit */ |
| 310 | window->data = (window->data << 1); |
| 311 | |
| 312 | /* If this packet was a success then set the low bit high */ |
| 313 | if (success) { |
| 314 | window->success_counter++; |
| 315 | window->data |= 1; |
| 316 | } |
| 317 | |
| 318 | /* window->counter can't be 0 -- it is either >0 or |
| 319 | * IWL_RATE_MAX_WINDOW */ |
| 320 | window->success_ratio = 12800 * window->success_counter / |
| 321 | window->counter; |
| 322 | |
| 323 | /* Tag this window as having been updated */ |
| 324 | window->stamp = jiffies; |
| 325 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 326 | spin_unlock_irqrestore(&rs_sta->lock, flags); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
| 330 | static void rs_rate_init(void *priv_rate, void *priv_sta, |
| 331 | struct ieee80211_local *local, struct sta_info *sta) |
| 332 | { |
| 333 | int i; |
| 334 | |
| 335 | IWL_DEBUG_RATE("enter\n"); |
| 336 | |
| 337 | /* TODO: what is a good starting rate for STA? About middle? Maybe not |
| 338 | * the lowest or the highest rate.. Could consider using RSSI from |
| 339 | * previous packets? Need to have IEEE 802.1X auth succeed immediately |
| 340 | * after assoc.. */ |
| 341 | |
| 342 | for (i = IWL_RATE_COUNT - 1; i >= 0; i--) { |
| 343 | if (sta->supp_rates & (1 << i)) { |
| 344 | sta->txrate = i; |
| 345 | break; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | sta->last_txrate = sta->txrate; |
| 350 | |
Mohamed Abbas | 14577f2 | 2007-11-12 11:37:42 +0800 | [diff] [blame] | 351 | /* For MODE_IEEE80211A mode it start at IWL_FIRST_OFDM_RATE */ |
| 352 | if (local->hw.conf.phymode == MODE_IEEE80211A) |
| 353 | sta->last_txrate += IWL_FIRST_OFDM_RATE; |
| 354 | |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 355 | IWL_DEBUG_RATE("leave\n"); |
| 356 | } |
| 357 | |
| 358 | static void *rs_alloc(struct ieee80211_local *local) |
| 359 | { |
| 360 | return local->hw.priv; |
| 361 | } |
| 362 | |
Ian Schram | 01ebd06 | 2007-10-25 17:15:22 +0800 | [diff] [blame] | 363 | /* rate scale requires free function to be implemented */ |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 364 | static void rs_free(void *priv) |
| 365 | { |
| 366 | return; |
| 367 | } |
| 368 | static void rs_clear(void *priv) |
| 369 | { |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | |
| 374 | static void *rs_alloc_sta(void *priv, gfp_t gfp) |
| 375 | { |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 376 | struct iwl3945_rs_sta *rs_sta; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 377 | int i; |
| 378 | |
| 379 | IWL_DEBUG_RATE("enter\n"); |
| 380 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 381 | rs_sta = kzalloc(sizeof(struct iwl3945_rs_sta), gfp); |
| 382 | if (!rs_sta) { |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 383 | IWL_DEBUG_RATE("leave: ENOMEM\n"); |
| 384 | return NULL; |
| 385 | } |
| 386 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 387 | spin_lock_init(&rs_sta->lock); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 388 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 389 | rs_sta->start_rate = IWL_RATE_INVALID; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 390 | |
| 391 | /* default to just 802.11b */ |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 392 | rs_sta->expected_tpt = iwl3945_expected_tpt_b; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 393 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 394 | rs_sta->last_partial_flush = jiffies; |
| 395 | rs_sta->last_flush = jiffies; |
| 396 | rs_sta->flush_time = IWL_RATE_FLUSH; |
| 397 | rs_sta->last_tx_packets = 0; |
| 398 | rs_sta->ibss_sta_added = 0; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 399 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 400 | init_timer(&rs_sta->rate_scale_flush); |
| 401 | rs_sta->rate_scale_flush.data = (unsigned long)rs_sta; |
| 402 | rs_sta->rate_scale_flush.function = &iwl3945_bg_rate_scale_flush; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 403 | |
| 404 | for (i = 0; i < IWL_RATE_COUNT; i++) |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 405 | iwl3945_clear_window(&rs_sta->win[i]); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 406 | |
| 407 | IWL_DEBUG_RATE("leave\n"); |
| 408 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 409 | return rs_sta; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | static void rs_free_sta(void *priv, void *priv_sta) |
| 413 | { |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 414 | struct iwl3945_rs_sta *rs_sta = priv_sta; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 415 | |
| 416 | IWL_DEBUG_RATE("enter\n"); |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 417 | del_timer_sync(&rs_sta->rate_scale_flush); |
| 418 | kfree(rs_sta); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 419 | IWL_DEBUG_RATE("leave\n"); |
| 420 | } |
| 421 | |
Mohamed Abbas | 14577f2 | 2007-11-12 11:37:42 +0800 | [diff] [blame] | 422 | |
| 423 | /* |
| 424 | * get ieee prev rate from rate scale table. |
| 425 | * for A and B mode we need to overright prev |
| 426 | * value |
| 427 | */ |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 428 | static int rs_adjust_next_rate(struct iwl3945_priv *priv, int rate) |
Mohamed Abbas | 14577f2 | 2007-11-12 11:37:42 +0800 | [diff] [blame] | 429 | { |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 430 | int next_rate = iwl3945_get_prev_ieee_rate(rate); |
Mohamed Abbas | 14577f2 | 2007-11-12 11:37:42 +0800 | [diff] [blame] | 431 | |
| 432 | switch (priv->phymode) { |
| 433 | case MODE_IEEE80211A: |
| 434 | if (rate == IWL_RATE_12M_INDEX) |
| 435 | next_rate = IWL_RATE_9M_INDEX; |
| 436 | else if (rate == IWL_RATE_6M_INDEX) |
| 437 | next_rate = IWL_RATE_6M_INDEX; |
| 438 | break; |
| 439 | case MODE_IEEE80211B: |
| 440 | if (rate == IWL_RATE_11M_INDEX_TABLE) |
| 441 | next_rate = IWL_RATE_5M_INDEX_TABLE; |
| 442 | break; |
| 443 | default: |
| 444 | break; |
| 445 | } |
| 446 | |
| 447 | return next_rate; |
| 448 | } |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 449 | /** |
| 450 | * rs_tx_status - Update rate control values based on Tx results |
| 451 | * |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 452 | * NOTE: Uses iwl3945_priv->retry_rate for the # of retries attempted by |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 453 | * the hardware for each rate. |
| 454 | */ |
| 455 | static void rs_tx_status(void *priv_rate, |
| 456 | struct net_device *dev, |
| 457 | struct sk_buff *skb, |
| 458 | struct ieee80211_tx_status *tx_resp) |
| 459 | { |
| 460 | u8 retries, current_count; |
| 461 | int scale_rate_index, first_index, last_index; |
| 462 | unsigned long flags; |
| 463 | struct sta_info *sta; |
| 464 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 465 | struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_rate; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 466 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 467 | struct iwl3945_rs_sta *rs_sta; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 468 | |
| 469 | IWL_DEBUG_RATE("enter\n"); |
| 470 | |
| 471 | retries = tx_resp->retry_count; |
| 472 | |
| 473 | first_index = tx_resp->control.tx_rate; |
| 474 | if ((first_index < 0) || (first_index >= IWL_RATE_COUNT)) { |
| 475 | IWL_DEBUG_RATE("leave: Rate out of bounds: %0x for %d\n", |
| 476 | tx_resp->control.tx_rate, first_index); |
| 477 | return; |
| 478 | } |
| 479 | |
| 480 | sta = sta_info_get(local, hdr->addr1); |
| 481 | if (!sta || !sta->rate_ctrl_priv) { |
| 482 | if (sta) |
| 483 | sta_info_put(sta); |
| 484 | IWL_DEBUG_RATE("leave: No STA priv data to update!\n"); |
| 485 | return; |
| 486 | } |
| 487 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 488 | rs_sta = (void *)sta->rate_ctrl_priv; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 489 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 490 | rs_sta->tx_packets++; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 491 | |
| 492 | scale_rate_index = first_index; |
| 493 | last_index = first_index; |
| 494 | |
| 495 | /* |
| 496 | * Update the window for each rate. We determine which rates |
| 497 | * were Tx'd based on the total number of retries vs. the number |
| 498 | * of retries configured for each rate -- currently set to the |
| 499 | * priv value 'retry_rate' vs. rate specific |
| 500 | * |
| 501 | * On exit from this while loop last_index indicates the rate |
| 502 | * at which the frame was finally transmitted (or failed if no |
| 503 | * ACK) |
| 504 | */ |
| 505 | while (retries > 0) { |
| 506 | if (retries < priv->retry_rate) { |
| 507 | current_count = retries; |
| 508 | last_index = scale_rate_index; |
| 509 | } else { |
| 510 | current_count = priv->retry_rate; |
Mohamed Abbas | 14577f2 | 2007-11-12 11:37:42 +0800 | [diff] [blame] | 511 | last_index = rs_adjust_next_rate(priv, |
| 512 | scale_rate_index); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | /* Update this rate accounting for as many retries |
| 516 | * as was used for it (per current_count) */ |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 517 | iwl3945_collect_tx_data(rs_sta, |
| 518 | &rs_sta->win[scale_rate_index], |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 519 | 0, current_count); |
| 520 | IWL_DEBUG_RATE("Update rate %d for %d retries.\n", |
| 521 | scale_rate_index, current_count); |
| 522 | |
| 523 | retries -= current_count; |
| 524 | |
| 525 | if (retries) |
| 526 | scale_rate_index = |
Mohamed Abbas | 14577f2 | 2007-11-12 11:37:42 +0800 | [diff] [blame] | 527 | rs_adjust_next_rate(priv, scale_rate_index); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Mohamed Abbas | 14577f2 | 2007-11-12 11:37:42 +0800 | [diff] [blame] | 530 | |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 531 | /* Update the last index window with success/failure based on ACK */ |
| 532 | IWL_DEBUG_RATE("Update rate %d with %s.\n", |
| 533 | last_index, |
| 534 | (tx_resp->flags & IEEE80211_TX_STATUS_ACK) ? |
| 535 | "success" : "failure"); |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 536 | iwl3945_collect_tx_data(rs_sta, |
| 537 | &rs_sta->win[last_index], |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 538 | tx_resp->flags & IEEE80211_TX_STATUS_ACK, 1); |
| 539 | |
| 540 | /* We updated the rate scale window -- if its been more than |
| 541 | * flush_time since the last run, schedule the flush |
| 542 | * again */ |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 543 | spin_lock_irqsave(&rs_sta->lock, flags); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 544 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 545 | if (!rs_sta->flush_pending && |
| 546 | time_after(jiffies, rs_sta->last_partial_flush + |
| 547 | rs_sta->flush_time)) { |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 548 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 549 | rs_sta->flush_pending = 1; |
| 550 | mod_timer(&rs_sta->rate_scale_flush, |
| 551 | jiffies + rs_sta->flush_time); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 554 | spin_unlock_irqrestore(&rs_sta->lock, flags); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 555 | |
| 556 | sta_info_put(sta); |
| 557 | |
| 558 | IWL_DEBUG_RATE("leave\n"); |
| 559 | |
| 560 | return; |
| 561 | } |
| 562 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 563 | static u16 iwl3945_get_adjacent_rate(struct iwl3945_rs_sta *rs_sta, |
| 564 | u8 index, u16 rate_mask, int phymode) |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 565 | { |
| 566 | u8 high = IWL_RATE_INVALID; |
| 567 | u8 low = IWL_RATE_INVALID; |
| 568 | |
Ian Schram | 01ebd06 | 2007-10-25 17:15:22 +0800 | [diff] [blame] | 569 | /* 802.11A walks to the next literal adjacent rate in |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 570 | * the rate table */ |
| 571 | if (unlikely(phymode == MODE_IEEE80211A)) { |
| 572 | int i; |
| 573 | u32 mask; |
| 574 | |
| 575 | /* Find the previous rate that is in the rate mask */ |
| 576 | i = index - 1; |
| 577 | for (mask = (1 << i); i >= 0; i--, mask >>= 1) { |
| 578 | if (rate_mask & mask) { |
| 579 | low = i; |
| 580 | break; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | /* Find the next rate that is in the rate mask */ |
| 585 | i = index + 1; |
| 586 | for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) { |
| 587 | if (rate_mask & mask) { |
| 588 | high = i; |
| 589 | break; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | return (high << 8) | low; |
| 594 | } |
| 595 | |
| 596 | low = index; |
| 597 | while (low != IWL_RATE_INVALID) { |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 598 | if (rs_sta->tgg) |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 599 | low = iwl3945_rates[low].prev_rs_tgg; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 600 | else |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 601 | low = iwl3945_rates[low].prev_rs; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 602 | if (low == IWL_RATE_INVALID) |
| 603 | break; |
| 604 | if (rate_mask & (1 << low)) |
| 605 | break; |
| 606 | IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low); |
| 607 | } |
| 608 | |
| 609 | high = index; |
| 610 | while (high != IWL_RATE_INVALID) { |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 611 | if (rs_sta->tgg) |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 612 | high = iwl3945_rates[high].next_rs_tgg; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 613 | else |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 614 | high = iwl3945_rates[high].next_rs; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 615 | if (high == IWL_RATE_INVALID) |
| 616 | break; |
| 617 | if (rate_mask & (1 << high)) |
| 618 | break; |
| 619 | IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high); |
| 620 | } |
| 621 | |
| 622 | return (high << 8) | low; |
| 623 | } |
| 624 | |
| 625 | /** |
| 626 | * rs_get_rate - find the rate for the requested packet |
| 627 | * |
| 628 | * Returns the ieee80211_rate structure allocated by the driver. |
| 629 | * |
| 630 | * The rate control algorithm has no internal mapping between hw_mode's |
| 631 | * rate ordering and the rate ordering used by the rate control algorithm. |
| 632 | * |
| 633 | * The rate control algorithm uses a single table of rates that goes across |
| 634 | * the entire A/B/G spectrum vs. being limited to just one particular |
| 635 | * hw_mode. |
| 636 | * |
| 637 | * As such, we can't convert the index obtained below into the hw_mode's |
| 638 | * rate table and must reference the driver allocated rate table |
| 639 | * |
| 640 | */ |
Mattias Nissler | 1abbe49 | 2007-12-20 13:50:07 +0100 | [diff] [blame] | 641 | static void rs_get_rate(void *priv_rate, struct net_device *dev, |
| 642 | struct ieee80211_hw_mode *mode, struct sk_buff *skb, |
| 643 | struct rate_selection *sel) |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 644 | { |
| 645 | u8 low = IWL_RATE_INVALID; |
| 646 | u8 high = IWL_RATE_INVALID; |
| 647 | u16 high_low; |
| 648 | int index; |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 649 | struct iwl3945_rs_sta *rs_sta; |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 650 | struct iwl3945_rate_scale_data *window = NULL; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 651 | int current_tpt = IWL_INVALID_VALUE; |
| 652 | int low_tpt = IWL_INVALID_VALUE; |
| 653 | int high_tpt = IWL_INVALID_VALUE; |
| 654 | u32 fail_count; |
| 655 | s8 scale_action = 0; |
| 656 | unsigned long flags; |
| 657 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); |
| 658 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; |
| 659 | struct sta_info *sta; |
Michael Wu | 2bc454b | 2007-12-25 19:33:16 -0500 | [diff] [blame] | 660 | u16 fc, rate_mask; |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 661 | struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_rate; |
Joe Perches | 0795af5 | 2007-10-03 17:59:30 -0700 | [diff] [blame] | 662 | DECLARE_MAC_BUF(mac); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 663 | |
| 664 | IWL_DEBUG_RATE("enter\n"); |
| 665 | |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 666 | sta = sta_info_get(local, hdr->addr1); |
Michael Wu | 2bc454b | 2007-12-25 19:33:16 -0500 | [diff] [blame] | 667 | |
| 668 | /* Send management frames and broadcast/multicast data using lowest |
| 669 | * rate. */ |
| 670 | fc = le16_to_cpu(hdr->frame_control); |
| 671 | if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA || |
| 672 | is_multicast_ether_addr(hdr->addr1) || |
| 673 | !sta || !sta->rate_ctrl_priv) { |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 674 | IWL_DEBUG_RATE("leave: No STA priv data to update!\n"); |
Mattias Nissler | 1abbe49 | 2007-12-20 13:50:07 +0100 | [diff] [blame] | 675 | sel->rate = rate_lowest(local, local->oper_hw_mode, sta); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 676 | if (sta) |
| 677 | sta_info_put(sta); |
Mattias Nissler | 1abbe49 | 2007-12-20 13:50:07 +0100 | [diff] [blame] | 678 | return; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | rate_mask = sta->supp_rates; |
Mohamed Abbas | 14577f2 | 2007-11-12 11:37:42 +0800 | [diff] [blame] | 682 | index = min(sta->last_txrate & 0xffff, IWL_RATE_COUNT - 1); |
| 683 | |
| 684 | if (priv->phymode == (u8) MODE_IEEE80211A) |
| 685 | rate_mask = rate_mask << IWL_FIRST_OFDM_RATE; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 686 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 687 | rs_sta = (void *)sta->rate_ctrl_priv; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 688 | |
| 689 | if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) && |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 690 | !rs_sta->ibss_sta_added) { |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 691 | u8 sta_id = iwl3945_hw_find_station(priv, hdr->addr1); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 692 | |
| 693 | if (sta_id == IWL_INVALID_STATION) { |
Joe Perches | 0795af5 | 2007-10-03 17:59:30 -0700 | [diff] [blame] | 694 | IWL_DEBUG_RATE("LQ: ADD station %s\n", |
| 695 | print_mac(mac, hdr->addr1)); |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 696 | sta_id = iwl3945_add_station(priv, |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 697 | hdr->addr1, 0, CMD_ASYNC); |
| 698 | } |
| 699 | if (sta_id != IWL_INVALID_STATION) |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 700 | rs_sta->ibss_sta_added = 1; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 701 | } |
| 702 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 703 | spin_lock_irqsave(&rs_sta->lock, flags); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 704 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 705 | if (rs_sta->start_rate != IWL_RATE_INVALID) { |
| 706 | index = rs_sta->start_rate; |
| 707 | rs_sta->start_rate = IWL_RATE_INVALID; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 710 | window = &(rs_sta->win[index]); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 711 | |
| 712 | fail_count = window->counter - window->success_counter; |
| 713 | |
| 714 | if (((fail_count <= IWL_RATE_MIN_FAILURE_TH) && |
| 715 | (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))) { |
| 716 | window->average_tpt = IWL_INVALID_VALUE; |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 717 | spin_unlock_irqrestore(&rs_sta->lock, flags); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 718 | |
| 719 | IWL_DEBUG_RATE("Invalid average_tpt on rate %d: " |
| 720 | "counter: %d, success_counter: %d, " |
| 721 | "expected_tpt is %sNULL\n", |
| 722 | index, |
| 723 | window->counter, |
| 724 | window->success_counter, |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 725 | rs_sta->expected_tpt ? "not " : ""); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 726 | goto out; |
| 727 | |
| 728 | } |
| 729 | |
| 730 | window->average_tpt = ((window->success_ratio * |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 731 | rs_sta->expected_tpt[index] + 64) / 128); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 732 | current_tpt = window->average_tpt; |
| 733 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 734 | high_low = iwl3945_get_adjacent_rate(rs_sta, index, rate_mask, |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 735 | local->hw.conf.phymode); |
| 736 | low = high_low & 0xff; |
| 737 | high = (high_low >> 8) & 0xff; |
| 738 | |
| 739 | if (low != IWL_RATE_INVALID) |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 740 | low_tpt = rs_sta->win[low].average_tpt; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 741 | |
| 742 | if (high != IWL_RATE_INVALID) |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 743 | high_tpt = rs_sta->win[high].average_tpt; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 744 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 745 | spin_unlock_irqrestore(&rs_sta->lock, flags); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 746 | |
| 747 | scale_action = 1; |
| 748 | |
| 749 | if ((window->success_ratio < IWL_RATE_DECREASE_TH) || !current_tpt) { |
| 750 | IWL_DEBUG_RATE("decrease rate because of low success_ratio\n"); |
| 751 | scale_action = -1; |
| 752 | } else if ((low_tpt == IWL_INVALID_VALUE) && |
| 753 | (high_tpt == IWL_INVALID_VALUE)) |
| 754 | scale_action = 1; |
| 755 | else if ((low_tpt != IWL_INVALID_VALUE) && |
| 756 | (high_tpt != IWL_INVALID_VALUE) |
| 757 | && (low_tpt < current_tpt) |
| 758 | && (high_tpt < current_tpt)) { |
| 759 | IWL_DEBUG_RATE("No action -- low [%d] & high [%d] < " |
| 760 | "current_tpt [%d]\n", |
| 761 | low_tpt, high_tpt, current_tpt); |
| 762 | scale_action = 0; |
| 763 | } else { |
| 764 | if (high_tpt != IWL_INVALID_VALUE) { |
| 765 | if (high_tpt > current_tpt) |
| 766 | scale_action = 1; |
| 767 | else { |
| 768 | IWL_DEBUG_RATE |
| 769 | ("decrease rate because of high tpt\n"); |
| 770 | scale_action = -1; |
| 771 | } |
| 772 | } else if (low_tpt != IWL_INVALID_VALUE) { |
| 773 | if (low_tpt > current_tpt) { |
| 774 | IWL_DEBUG_RATE |
| 775 | ("decrease rate because of low tpt\n"); |
| 776 | scale_action = -1; |
| 777 | } else |
| 778 | scale_action = 1; |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | if ((window->success_ratio > IWL_RATE_HIGH_TH) || |
| 783 | (current_tpt > window->average_tpt)) { |
| 784 | IWL_DEBUG_RATE("No action -- success_ratio [%d] > HIGH_TH or " |
| 785 | "current_tpt [%d] > average_tpt [%d]\n", |
| 786 | window->success_ratio, |
| 787 | current_tpt, window->average_tpt); |
| 788 | scale_action = 0; |
| 789 | } |
| 790 | |
| 791 | switch (scale_action) { |
| 792 | case -1: |
| 793 | if (low != IWL_RATE_INVALID) |
| 794 | index = low; |
| 795 | break; |
| 796 | |
| 797 | case 1: |
| 798 | if (high != IWL_RATE_INVALID) |
| 799 | index = high; |
| 800 | |
| 801 | break; |
| 802 | |
| 803 | case 0: |
| 804 | default: |
| 805 | break; |
| 806 | } |
| 807 | |
| 808 | IWL_DEBUG_RATE("Selected %d (action %d) - low %d high %d\n", |
| 809 | index, scale_action, low, high); |
| 810 | |
| 811 | out: |
| 812 | |
| 813 | sta->last_txrate = index; |
Mohamed Abbas | 14577f2 | 2007-11-12 11:37:42 +0800 | [diff] [blame] | 814 | if (priv->phymode == (u8) MODE_IEEE80211A) |
| 815 | sta->txrate = sta->last_txrate - IWL_FIRST_OFDM_RATE; |
| 816 | else |
| 817 | sta->txrate = sta->last_txrate; |
| 818 | |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 819 | sta_info_put(sta); |
| 820 | |
| 821 | IWL_DEBUG_RATE("leave: %d\n", index); |
| 822 | |
Mattias Nissler | 1abbe49 | 2007-12-20 13:50:07 +0100 | [diff] [blame] | 823 | sel->rate = &priv->ieee_rates[index]; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | static struct rate_control_ops rs_ops = { |
| 827 | .module = NULL, |
| 828 | .name = RS_NAME, |
| 829 | .tx_status = rs_tx_status, |
| 830 | .get_rate = rs_get_rate, |
| 831 | .rate_init = rs_rate_init, |
| 832 | .clear = rs_clear, |
| 833 | .alloc = rs_alloc, |
| 834 | .free = rs_free, |
| 835 | .alloc_sta = rs_alloc_sta, |
| 836 | .free_sta = rs_free_sta, |
| 837 | }; |
| 838 | |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 839 | int iwl3945_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id) |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 840 | { |
| 841 | struct ieee80211_local *local = hw_to_local(hw); |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 842 | struct iwl3945_priv *priv = hw->priv; |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 843 | struct iwl3945_rs_sta *rs_sta; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 844 | struct sta_info *sta; |
| 845 | unsigned long flags; |
| 846 | int count = 0, i; |
| 847 | u32 samples = 0, success = 0, good = 0; |
| 848 | unsigned long now = jiffies; |
| 849 | u32 max_time = 0; |
| 850 | |
| 851 | sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr); |
| 852 | if (!sta || !sta->rate_ctrl_priv) { |
| 853 | if (sta) { |
| 854 | sta_info_put(sta); |
| 855 | IWL_DEBUG_RATE("leave - no private rate data!\n"); |
| 856 | } else |
| 857 | IWL_DEBUG_RATE("leave - no station!\n"); |
| 858 | return sprintf(buf, "station %d not found\n", sta_id); |
| 859 | } |
| 860 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 861 | rs_sta = (void *)sta->rate_ctrl_priv; |
| 862 | spin_lock_irqsave(&rs_sta->lock, flags); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 863 | i = IWL_RATE_54M_INDEX; |
| 864 | while (1) { |
| 865 | u64 mask; |
| 866 | int j; |
| 867 | |
| 868 | count += |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 869 | sprintf(&buf[count], " %2dMbs: ", iwl3945_rates[i].ieee / 2); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 870 | |
| 871 | mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1)); |
| 872 | for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1) |
| 873 | buf[count++] = |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 874 | (rs_sta->win[i].data & mask) ? '1' : '0'; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 875 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 876 | samples += rs_sta->win[i].counter; |
| 877 | good += rs_sta->win[i].success_counter; |
| 878 | success += rs_sta->win[i].success_counter * |
| 879 | iwl3945_rates[i].ieee; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 880 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 881 | if (rs_sta->win[i].stamp) { |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 882 | int delta = |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 883 | jiffies_to_msecs(now - rs_sta->win[i].stamp); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 884 | |
| 885 | if (delta > max_time) |
| 886 | max_time = delta; |
| 887 | |
| 888 | count += sprintf(&buf[count], "%5dms\n", delta); |
| 889 | } else |
| 890 | buf[count++] = '\n'; |
| 891 | |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 892 | j = iwl3945_get_prev_ieee_rate(i); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 893 | if (j == i) |
| 894 | break; |
| 895 | i = j; |
| 896 | } |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 897 | spin_unlock_irqrestore(&rs_sta->lock, flags); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 898 | sta_info_put(sta); |
| 899 | |
| 900 | /* Display the average rate of all samples taken. |
| 901 | * |
| 902 | * NOTE: We multiple # of samples by 2 since the IEEE measurement |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 903 | * added from iwl3945_rates is actually 2X the rate */ |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 904 | if (samples) |
| 905 | count += sprintf( |
| 906 | &buf[count], |
| 907 | "\nAverage rate is %3d.%02dMbs over last %4dms\n" |
| 908 | "%3d%% success (%d good packets over %d tries)\n", |
| 909 | success / (2 * samples), (success * 5 / samples) % 10, |
| 910 | max_time, good * 100 / samples, good, samples); |
| 911 | else |
| 912 | count += sprintf(&buf[count], "\nAverage rate: 0Mbs\n"); |
| 913 | |
| 914 | return count; |
| 915 | } |
| 916 | |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 917 | void iwl3945_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id) |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 918 | { |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 919 | struct iwl3945_priv *priv = hw->priv; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 920 | s32 rssi = 0; |
| 921 | unsigned long flags; |
| 922 | struct ieee80211_local *local = hw_to_local(hw); |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 923 | struct iwl3945_rs_sta *rs_sta; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 924 | struct sta_info *sta; |
| 925 | |
| 926 | IWL_DEBUG_RATE("enter\n"); |
| 927 | |
| 928 | if (!local->rate_ctrl->ops->name || |
| 929 | strcmp(local->rate_ctrl->ops->name, RS_NAME)) { |
| 930 | IWL_WARNING("iwl-3945-rs not selected as rate control algo!\n"); |
| 931 | IWL_DEBUG_RATE("leave - mac80211 picked the wrong RC algo.\n"); |
| 932 | return; |
| 933 | } |
| 934 | |
| 935 | sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr); |
| 936 | if (!sta || !sta->rate_ctrl_priv) { |
| 937 | if (sta) |
| 938 | sta_info_put(sta); |
| 939 | IWL_DEBUG_RATE("leave - no private rate data!\n"); |
| 940 | return; |
| 941 | } |
| 942 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 943 | rs_sta = (void *)sta->rate_ctrl_priv; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 944 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 945 | spin_lock_irqsave(&rs_sta->lock, flags); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 946 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 947 | rs_sta->tgg = 0; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 948 | switch (priv->phymode) { |
| 949 | case MODE_IEEE80211G: |
| 950 | if (priv->active_rxon.flags & RXON_FLG_TGG_PROTECT_MSK) { |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 951 | rs_sta->tgg = 1; |
| 952 | rs_sta->expected_tpt = iwl3945_expected_tpt_g_prot; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 953 | } else |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 954 | rs_sta->expected_tpt = iwl3945_expected_tpt_g; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 955 | break; |
| 956 | |
| 957 | case MODE_IEEE80211A: |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 958 | rs_sta->expected_tpt = iwl3945_expected_tpt_a; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 959 | break; |
| 960 | |
| 961 | default: |
| 962 | IWL_WARNING("Invalid phymode. Defaulting to 802.11b\n"); |
| 963 | case MODE_IEEE80211B: |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 964 | rs_sta->expected_tpt = iwl3945_expected_tpt_b; |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 965 | break; |
| 966 | } |
| 967 | |
| 968 | sta_info_put(sta); |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 969 | spin_unlock_irqrestore(&rs_sta->lock, flags); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 970 | |
| 971 | rssi = priv->last_rx_rssi; |
| 972 | if (rssi == 0) |
| 973 | rssi = IWL_MIN_RSSI_VAL; |
| 974 | |
| 975 | IWL_DEBUG(IWL_DL_INFO | IWL_DL_RATE, "Network RSSI: %d\n", rssi); |
| 976 | |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 977 | rs_sta->start_rate = |
| 978 | iwl3945_get_rate_index_by_rssi(rssi, priv->phymode); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 979 | |
| 980 | IWL_DEBUG_RATE("leave: rssi %d assign rate index: " |
Tomas Winkler | fa254a6 | 2008-01-27 20:41:38 -0800 | [diff] [blame^] | 981 | "%d (plcp 0x%x)\n", rssi, rs_sta->start_rate, |
| 982 | iwl3945_rates[rs_sta->start_rate].plcp); |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 983 | } |
| 984 | |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 985 | void iwl3945_rate_control_register(struct ieee80211_hw *hw) |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 986 | { |
| 987 | ieee80211_rate_control_register(&rs_ops); |
| 988 | } |
| 989 | |
Christoph Hellwig | bb8c093 | 2008-01-27 16:41:47 -0800 | [diff] [blame] | 990 | void iwl3945_rate_control_unregister(struct ieee80211_hw *hw) |
Zhu Yi | b481de9 | 2007-09-25 17:54:57 -0700 | [diff] [blame] | 991 | { |
| 992 | ieee80211_rate_control_unregister(&rs_ops); |
| 993 | } |
| 994 | |
| 995 | |