iwl-3945-rs.c 27 KB

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