iwl-3945-rs.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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. /*
  274. * Called after adding a new station to initialize rate scaling
  275. */
  276. void iwl3945_rs_rate_init(struct iwl_priv *priv, struct ieee80211_sta *sta, u8 sta_id)
  277. {
  278. struct ieee80211_hw *hw = priv->hw;
  279. struct ieee80211_conf *conf = &priv->hw->conf;
  280. struct iwl3945_sta_priv *psta;
  281. struct iwl3945_rs_sta *rs_sta;
  282. struct ieee80211_supported_band *sband;
  283. int i;
  284. IWL_DEBUG_INFO(priv, "enter\n");
  285. if (sta_id == priv->contexts[IWL_RXON_CTX_BSS].bcast_sta_id)
  286. goto out;
  287. psta = (struct iwl3945_sta_priv *) sta->drv_priv;
  288. rs_sta = &psta->rs_sta;
  289. sband = hw->wiphy->bands[conf->channel->band];
  290. rs_sta->priv = priv;
  291. rs_sta->start_rate = IWL_RATE_INVALID;
  292. /* default to just 802.11b */
  293. rs_sta->expected_tpt = iwl3945_expected_tpt_b;
  294. rs_sta->last_partial_flush = jiffies;
  295. rs_sta->last_flush = jiffies;
  296. rs_sta->flush_time = IWL_RATE_FLUSH;
  297. rs_sta->last_tx_packets = 0;
  298. rs_sta->rate_scale_flush.data = (unsigned long)rs_sta;
  299. rs_sta->rate_scale_flush.function = iwl3945_bg_rate_scale_flush;
  300. for (i = 0; i < IWL_RATE_COUNT_3945; i++)
  301. iwl3945_clear_window(&rs_sta->win[i]);
  302. /* TODO: what is a good starting rate for STA? About middle? Maybe not
  303. * the lowest or the highest rate.. Could consider using RSSI from
  304. * previous packets? Need to have IEEE 802.1X auth succeed immediately
  305. * after assoc.. */
  306. for (i = sband->n_bitrates - 1; i >= 0; i--) {
  307. if (sta->supp_rates[sband->band] & (1 << i)) {
  308. rs_sta->last_txrate_idx = i;
  309. break;
  310. }
  311. }
  312. priv->_3945.sta_supp_rates = sta->supp_rates[sband->band];
  313. /* For 5 GHz band it start at IWL_FIRST_OFDM_RATE */
  314. if (sband->band == IEEE80211_BAND_5GHZ) {
  315. rs_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
  316. priv->_3945.sta_supp_rates = priv->_3945.sta_supp_rates <<
  317. IWL_FIRST_OFDM_RATE;
  318. }
  319. out:
  320. priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
  321. IWL_DEBUG_INFO(priv, "leave\n");
  322. }
  323. static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
  324. {
  325. return hw->priv;
  326. }
  327. /* rate scale requires free function to be implemented */
  328. static void rs_free(void *priv)
  329. {
  330. return;
  331. }
  332. static void *rs_alloc_sta(void *iwl_priv, struct ieee80211_sta *sta, gfp_t gfp)
  333. {
  334. struct iwl3945_rs_sta *rs_sta;
  335. struct iwl3945_sta_priv *psta = (void *) sta->drv_priv;
  336. struct iwl_priv *priv __maybe_unused = iwl_priv;
  337. IWL_DEBUG_RATE(priv, "enter\n");
  338. rs_sta = &psta->rs_sta;
  339. spin_lock_init(&rs_sta->lock);
  340. init_timer(&rs_sta->rate_scale_flush);
  341. IWL_DEBUG_RATE(priv, "leave\n");
  342. return rs_sta;
  343. }
  344. static void rs_free_sta(void *iwl_priv, struct ieee80211_sta *sta,
  345. void *priv_sta)
  346. {
  347. struct iwl3945_rs_sta *rs_sta = priv_sta;
  348. /*
  349. * Be careful not to use any members of iwl3945_rs_sta (like trying
  350. * to use iwl_priv to print out debugging) since it may not be fully
  351. * initialized at this point.
  352. */
  353. del_timer_sync(&rs_sta->rate_scale_flush);
  354. }
  355. /**
  356. * rs_tx_status - Update rate control values based on Tx results
  357. *
  358. * NOTE: Uses iwl_priv->retry_rate for the # of retries attempted by
  359. * the hardware for each rate.
  360. */
  361. static void rs_tx_status(void *priv_rate, struct ieee80211_supported_band *sband,
  362. struct ieee80211_sta *sta, void *priv_sta,
  363. struct sk_buff *skb)
  364. {
  365. s8 retries = 0, current_count;
  366. int scale_rate_index, first_index, last_index;
  367. unsigned long flags;
  368. struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
  369. struct iwl3945_rs_sta *rs_sta = priv_sta;
  370. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  371. IWL_DEBUG_RATE(priv, "enter\n");
  372. retries = info->status.rates[0].count;
  373. /* Sanity Check for retries */
  374. if (retries > IWL_RATE_RETRY_TH)
  375. retries = IWL_RATE_RETRY_TH;
  376. first_index = sband->bitrates[info->status.rates[0].idx].hw_value;
  377. if ((first_index < 0) || (first_index >= IWL_RATE_COUNT_3945)) {
  378. IWL_DEBUG_RATE(priv, "leave: Rate out of bounds: %d\n", first_index);
  379. return;
  380. }
  381. if (!priv_sta) {
  382. IWL_DEBUG_RATE(priv, "leave: No STA priv data to update!\n");
  383. return;
  384. }
  385. /* Treat uninitialized rate scaling data same as non-existing. */
  386. if (!rs_sta->priv) {
  387. IWL_DEBUG_RATE(priv, "leave: STA priv data uninitialized!\n");
  388. return;
  389. }
  390. rs_sta->tx_packets++;
  391. scale_rate_index = first_index;
  392. last_index = first_index;
  393. /*
  394. * Update the window for each rate. We determine which rates
  395. * were Tx'd based on the total number of retries vs. the number
  396. * of retries configured for each rate -- currently set to the
  397. * priv value 'retry_rate' vs. rate specific
  398. *
  399. * On exit from this while loop last_index indicates the rate
  400. * at which the frame was finally transmitted (or failed if no
  401. * ACK)
  402. */
  403. while (retries > 1) {
  404. if ((retries - 1) < priv->retry_rate) {
  405. current_count = (retries - 1);
  406. last_index = scale_rate_index;
  407. } else {
  408. current_count = priv->retry_rate;
  409. last_index = iwl3945_rs_next_rate(priv,
  410. scale_rate_index);
  411. }
  412. /* Update this rate accounting for as many retries
  413. * as was used for it (per current_count) */
  414. iwl3945_collect_tx_data(rs_sta,
  415. &rs_sta->win[scale_rate_index],
  416. 0, current_count, scale_rate_index);
  417. IWL_DEBUG_RATE(priv, "Update rate %d for %d retries.\n",
  418. scale_rate_index, current_count);
  419. retries -= current_count;
  420. scale_rate_index = last_index;
  421. }
  422. /* Update the last index window with success/failure based on ACK */
  423. IWL_DEBUG_RATE(priv, "Update rate %d with %s.\n",
  424. last_index,
  425. (info->flags & IEEE80211_TX_STAT_ACK) ?
  426. "success" : "failure");
  427. iwl3945_collect_tx_data(rs_sta,
  428. &rs_sta->win[last_index],
  429. info->flags & IEEE80211_TX_STAT_ACK, 1, last_index);
  430. /* We updated the rate scale window -- if its been more than
  431. * flush_time since the last run, schedule the flush
  432. * again */
  433. spin_lock_irqsave(&rs_sta->lock, flags);
  434. if (!rs_sta->flush_pending &&
  435. time_after(jiffies, rs_sta->last_flush +
  436. rs_sta->flush_time)) {
  437. rs_sta->last_partial_flush = jiffies;
  438. rs_sta->flush_pending = 1;
  439. mod_timer(&rs_sta->rate_scale_flush,
  440. jiffies + rs_sta->flush_time);
  441. }
  442. spin_unlock_irqrestore(&rs_sta->lock, flags);
  443. IWL_DEBUG_RATE(priv, "leave\n");
  444. }
  445. static u16 iwl3945_get_adjacent_rate(struct iwl3945_rs_sta *rs_sta,
  446. u8 index, u16 rate_mask, enum ieee80211_band band)
  447. {
  448. u8 high = IWL_RATE_INVALID;
  449. u8 low = IWL_RATE_INVALID;
  450. struct iwl_priv *priv __maybe_unused = rs_sta->priv;
  451. /* 802.11A walks to the next literal adjacent rate in
  452. * the rate table */
  453. if (unlikely(band == IEEE80211_BAND_5GHZ)) {
  454. int i;
  455. u32 mask;
  456. /* Find the previous rate that is in the rate mask */
  457. i = index - 1;
  458. for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
  459. if (rate_mask & mask) {
  460. low = i;
  461. break;
  462. }
  463. }
  464. /* Find the next rate that is in the rate mask */
  465. i = index + 1;
  466. for (mask = (1 << i); i < IWL_RATE_COUNT_3945;
  467. i++, mask <<= 1) {
  468. if (rate_mask & mask) {
  469. high = i;
  470. break;
  471. }
  472. }
  473. return (high << 8) | low;
  474. }
  475. low = index;
  476. while (low != IWL_RATE_INVALID) {
  477. if (rs_sta->tgg)
  478. low = iwl3945_rates[low].prev_rs_tgg;
  479. else
  480. low = iwl3945_rates[low].prev_rs;
  481. if (low == IWL_RATE_INVALID)
  482. break;
  483. if (rate_mask & (1 << low))
  484. break;
  485. IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
  486. }
  487. high = index;
  488. while (high != IWL_RATE_INVALID) {
  489. if (rs_sta->tgg)
  490. high = iwl3945_rates[high].next_rs_tgg;
  491. else
  492. high = iwl3945_rates[high].next_rs;
  493. if (high == IWL_RATE_INVALID)
  494. break;
  495. if (rate_mask & (1 << high))
  496. break;
  497. IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
  498. }
  499. return (high << 8) | low;
  500. }
  501. /**
  502. * rs_get_rate - find the rate for the requested packet
  503. *
  504. * Returns the ieee80211_rate structure allocated by the driver.
  505. *
  506. * The rate control algorithm has no internal mapping between hw_mode's
  507. * rate ordering and the rate ordering used by the rate control algorithm.
  508. *
  509. * The rate control algorithm uses a single table of rates that goes across
  510. * the entire A/B/G spectrum vs. being limited to just one particular
  511. * hw_mode.
  512. *
  513. * As such, we can't convert the index obtained below into the hw_mode's
  514. * rate table and must reference the driver allocated rate table
  515. *
  516. */
  517. static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta,
  518. void *priv_sta, struct ieee80211_tx_rate_control *txrc)
  519. {
  520. struct ieee80211_supported_band *sband = txrc->sband;
  521. struct sk_buff *skb = txrc->skb;
  522. u8 low = IWL_RATE_INVALID;
  523. u8 high = IWL_RATE_INVALID;
  524. u16 high_low;
  525. int index;
  526. struct iwl3945_rs_sta *rs_sta = priv_sta;
  527. struct iwl3945_rate_scale_data *window = NULL;
  528. int current_tpt = IWL_INVALID_VALUE;
  529. int low_tpt = IWL_INVALID_VALUE;
  530. int high_tpt = IWL_INVALID_VALUE;
  531. u32 fail_count;
  532. s8 scale_action = 0;
  533. unsigned long flags;
  534. u16 rate_mask = sta ? sta->supp_rates[sband->band] : 0;
  535. s8 max_rate_idx = -1;
  536. struct iwl_priv *priv __maybe_unused = (struct iwl_priv *)priv_r;
  537. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  538. IWL_DEBUG_RATE(priv, "enter\n");
  539. /* Treat uninitialized rate scaling data same as non-existing. */
  540. if (rs_sta && !rs_sta->priv) {
  541. IWL_DEBUG_RATE(priv, "Rate scaling information not initialized yet.\n");
  542. priv_sta = NULL;
  543. }
  544. if (rate_control_send_low(sta, priv_sta, txrc))
  545. return;
  546. rate_mask = sta->supp_rates[sband->band];
  547. /* get user max rate if set */
  548. max_rate_idx = txrc->max_rate_idx;
  549. if ((sband->band == IEEE80211_BAND_5GHZ) && (max_rate_idx != -1))
  550. max_rate_idx += IWL_FIRST_OFDM_RATE;
  551. if ((max_rate_idx < 0) || (max_rate_idx >= IWL_RATE_COUNT))
  552. max_rate_idx = -1;
  553. index = min(rs_sta->last_txrate_idx & 0xffff, IWL_RATE_COUNT_3945 - 1);
  554. if (sband->band == IEEE80211_BAND_5GHZ)
  555. rate_mask = rate_mask << IWL_FIRST_OFDM_RATE;
  556. spin_lock_irqsave(&rs_sta->lock, flags);
  557. /* for recent assoc, choose best rate regarding
  558. * to rssi value
  559. */
  560. if (rs_sta->start_rate != IWL_RATE_INVALID) {
  561. if (rs_sta->start_rate < index &&
  562. (rate_mask & (1 << rs_sta->start_rate)))
  563. index = rs_sta->start_rate;
  564. rs_sta->start_rate = IWL_RATE_INVALID;
  565. }
  566. /* force user max rate if set by user */
  567. if ((max_rate_idx != -1) && (max_rate_idx < index)) {
  568. if (rate_mask & (1 << max_rate_idx))
  569. index = max_rate_idx;
  570. }
  571. window = &(rs_sta->win[index]);
  572. fail_count = window->counter - window->success_counter;
  573. if (((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
  574. (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))) {
  575. spin_unlock_irqrestore(&rs_sta->lock, flags);
  576. IWL_DEBUG_RATE(priv, "Invalid average_tpt on rate %d: "
  577. "counter: %d, success_counter: %d, "
  578. "expected_tpt is %sNULL\n",
  579. index,
  580. window->counter,
  581. window->success_counter,
  582. rs_sta->expected_tpt ? "not " : "");
  583. /* Can't calculate this yet; not enough history */
  584. window->average_tpt = IWL_INVALID_VALUE;
  585. goto out;
  586. }
  587. current_tpt = window->average_tpt;
  588. high_low = iwl3945_get_adjacent_rate(rs_sta, index, rate_mask,
  589. sband->band);
  590. low = high_low & 0xff;
  591. high = (high_low >> 8) & 0xff;
  592. /* If user set max rate, dont allow higher than user constrain */
  593. if ((max_rate_idx != -1) && (max_rate_idx < high))
  594. high = IWL_RATE_INVALID;
  595. /* Collect Measured throughputs of adjacent rates */
  596. if (low != IWL_RATE_INVALID)
  597. low_tpt = rs_sta->win[low].average_tpt;
  598. if (high != IWL_RATE_INVALID)
  599. high_tpt = rs_sta->win[high].average_tpt;
  600. spin_unlock_irqrestore(&rs_sta->lock, flags);
  601. scale_action = 0;
  602. /* Low success ratio , need to drop the rate */
  603. if ((window->success_ratio < IWL_RATE_DECREASE_TH) || !current_tpt) {
  604. IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
  605. scale_action = -1;
  606. /* No throughput measured yet for adjacent rates,
  607. * try increase */
  608. } else if ((low_tpt == IWL_INVALID_VALUE) &&
  609. (high_tpt == IWL_INVALID_VALUE)) {
  610. if (high != IWL_RATE_INVALID && window->success_ratio >= IWL_RATE_INCREASE_TH)
  611. scale_action = 1;
  612. else if (low != IWL_RATE_INVALID)
  613. scale_action = 0;
  614. /* Both adjacent throughputs are measured, but neither one has
  615. * better throughput; we're using the best rate, don't change
  616. * it! */
  617. } else if ((low_tpt != IWL_INVALID_VALUE) &&
  618. (high_tpt != IWL_INVALID_VALUE) &&
  619. (low_tpt < current_tpt) && (high_tpt < current_tpt)) {
  620. IWL_DEBUG_RATE(priv, "No action -- low [%d] & high [%d] < "
  621. "current_tpt [%d]\n",
  622. low_tpt, high_tpt, current_tpt);
  623. scale_action = 0;
  624. /* At least one of the rates has better throughput */
  625. } else {
  626. if (high_tpt != IWL_INVALID_VALUE) {
  627. /* High rate has better throughput, Increase
  628. * rate */
  629. if (high_tpt > current_tpt &&
  630. window->success_ratio >= IWL_RATE_INCREASE_TH)
  631. scale_action = 1;
  632. else {
  633. IWL_DEBUG_RATE(priv,
  634. "decrease rate because of high tpt\n");
  635. scale_action = 0;
  636. }
  637. } else if (low_tpt != IWL_INVALID_VALUE) {
  638. if (low_tpt > current_tpt) {
  639. IWL_DEBUG_RATE(priv,
  640. "decrease rate because of low tpt\n");
  641. scale_action = -1;
  642. } else if (window->success_ratio >= IWL_RATE_INCREASE_TH) {
  643. /* Lower rate has better
  644. * throughput,decrease rate */
  645. scale_action = 1;
  646. }
  647. }
  648. }
  649. /* Sanity check; asked for decrease, but success rate or throughput
  650. * has been good at old rate. Don't change it. */
  651. if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
  652. ((window->success_ratio > IWL_RATE_HIGH_TH) ||
  653. (current_tpt > (100 * rs_sta->expected_tpt[low]))))
  654. scale_action = 0;
  655. switch (scale_action) {
  656. case -1:
  657. /* Decrese rate */
  658. if (low != IWL_RATE_INVALID)
  659. index = low;
  660. break;
  661. case 1:
  662. /* Increase rate */
  663. if (high != IWL_RATE_INVALID)
  664. index = high;
  665. break;
  666. case 0:
  667. default:
  668. /* No change */
  669. break;
  670. }
  671. IWL_DEBUG_RATE(priv, "Selected %d (action %d) - low %d high %d\n",
  672. index, scale_action, low, high);
  673. out:
  674. rs_sta->last_txrate_idx = index;
  675. if (sband->band == IEEE80211_BAND_5GHZ)
  676. info->control.rates[0].idx = rs_sta->last_txrate_idx -
  677. IWL_FIRST_OFDM_RATE;
  678. else
  679. info->control.rates[0].idx = rs_sta->last_txrate_idx;
  680. IWL_DEBUG_RATE(priv, "leave: %d\n", index);
  681. }
  682. #ifdef CONFIG_MAC80211_DEBUGFS
  683. static int iwl3945_open_file_generic(struct inode *inode, struct file *file)
  684. {
  685. file->private_data = inode->i_private;
  686. return 0;
  687. }
  688. static ssize_t iwl3945_sta_dbgfs_stats_table_read(struct file *file,
  689. char __user *user_buf,
  690. size_t count, loff_t *ppos)
  691. {
  692. char *buff;
  693. int desc = 0;
  694. int j;
  695. ssize_t ret;
  696. struct iwl3945_rs_sta *lq_sta = file->private_data;
  697. buff = kmalloc(1024, GFP_KERNEL);
  698. if (!buff)
  699. return -ENOMEM;
  700. desc += sprintf(buff + desc, "tx packets=%d last rate index=%d\n"
  701. "rate=0x%X flush time %d\n",
  702. lq_sta->tx_packets,
  703. lq_sta->last_txrate_idx,
  704. lq_sta->start_rate, jiffies_to_msecs(lq_sta->flush_time));
  705. for (j = 0; j < IWL_RATE_COUNT_3945; j++) {
  706. desc += sprintf(buff+desc,
  707. "counter=%d success=%d %%=%d\n",
  708. lq_sta->win[j].counter,
  709. lq_sta->win[j].success_counter,
  710. lq_sta->win[j].success_ratio);
  711. }
  712. ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
  713. kfree(buff);
  714. return ret;
  715. }
  716. static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
  717. .read = iwl3945_sta_dbgfs_stats_table_read,
  718. .open = iwl3945_open_file_generic,
  719. };
  720. static void iwl3945_add_debugfs(void *priv, void *priv_sta,
  721. struct dentry *dir)
  722. {
  723. struct iwl3945_rs_sta *lq_sta = priv_sta;
  724. lq_sta->rs_sta_dbgfs_stats_table_file =
  725. debugfs_create_file("rate_stats_table", 0600, dir,
  726. lq_sta, &rs_sta_dbgfs_stats_table_ops);
  727. }
  728. static void iwl3945_remove_debugfs(void *priv, void *priv_sta)
  729. {
  730. struct iwl3945_rs_sta *lq_sta = priv_sta;
  731. debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
  732. }
  733. #endif
  734. /*
  735. * Initialization of rate scaling information is done by driver after
  736. * the station is added. Since mac80211 calls this function before a
  737. * station is added we ignore it.
  738. */
  739. static void rs_rate_init_stub(void *priv_r, struct ieee80211_supported_band *sband,
  740. struct ieee80211_sta *sta, void *priv_sta)
  741. {
  742. }
  743. static struct rate_control_ops rs_ops = {
  744. .module = NULL,
  745. .name = RS_NAME,
  746. .tx_status = rs_tx_status,
  747. .get_rate = rs_get_rate,
  748. .rate_init = rs_rate_init_stub,
  749. .alloc = rs_alloc,
  750. .free = rs_free,
  751. .alloc_sta = rs_alloc_sta,
  752. .free_sta = rs_free_sta,
  753. #ifdef CONFIG_MAC80211_DEBUGFS
  754. .add_sta_debugfs = iwl3945_add_debugfs,
  755. .remove_sta_debugfs = iwl3945_remove_debugfs,
  756. #endif
  757. };
  758. void iwl3945_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
  759. {
  760. struct iwl_priv *priv = hw->priv;
  761. s32 rssi = 0;
  762. unsigned long flags;
  763. struct iwl3945_rs_sta *rs_sta;
  764. struct ieee80211_sta *sta;
  765. struct iwl3945_sta_priv *psta;
  766. IWL_DEBUG_RATE(priv, "enter\n");
  767. rcu_read_lock();
  768. sta = ieee80211_find_sta(priv->contexts[IWL_RXON_CTX_BSS].vif,
  769. priv->stations[sta_id].sta.sta.addr);
  770. if (!sta) {
  771. IWL_DEBUG_RATE(priv, "Unable to find station to initialize rate scaling.\n");
  772. rcu_read_unlock();
  773. return;
  774. }
  775. psta = (void *) sta->drv_priv;
  776. rs_sta = &psta->rs_sta;
  777. spin_lock_irqsave(&rs_sta->lock, flags);
  778. rs_sta->tgg = 0;
  779. switch (priv->band) {
  780. case IEEE80211_BAND_2GHZ:
  781. /* TODO: this always does G, not a regression */
  782. if (priv->contexts[IWL_RXON_CTX_BSS].active.flags &
  783. RXON_FLG_TGG_PROTECT_MSK) {
  784. rs_sta->tgg = 1;
  785. rs_sta->expected_tpt = iwl3945_expected_tpt_g_prot;
  786. } else
  787. rs_sta->expected_tpt = iwl3945_expected_tpt_g;
  788. break;
  789. case IEEE80211_BAND_5GHZ:
  790. rs_sta->expected_tpt = iwl3945_expected_tpt_a;
  791. break;
  792. case IEEE80211_NUM_BANDS:
  793. BUG();
  794. break;
  795. }
  796. spin_unlock_irqrestore(&rs_sta->lock, flags);
  797. rssi = priv->_3945.last_rx_rssi;
  798. if (rssi == 0)
  799. rssi = IWL_MIN_RSSI_VAL;
  800. IWL_DEBUG_RATE(priv, "Network RSSI: %d\n", rssi);
  801. rs_sta->start_rate = iwl3945_get_rate_index_by_rssi(rssi, priv->band);
  802. IWL_DEBUG_RATE(priv, "leave: rssi %d assign rate index: "
  803. "%d (plcp 0x%x)\n", rssi, rs_sta->start_rate,
  804. iwl3945_rates[rs_sta->start_rate].plcp);
  805. rcu_read_unlock();
  806. }
  807. int iwl3945_rate_control_register(void)
  808. {
  809. return ieee80211_rate_control_register(&rs_ops);
  810. }
  811. void iwl3945_rate_control_unregister(void)
  812. {
  813. ieee80211_rate_control_unregister(&rs_ops);
  814. }