iwl-3945-rs.c 27 KB

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