iwl-3945-rs.c 26 KB

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