iwl-3945-rs.c 25 KB

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