iwl-3945-rs.c 26 KB

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