3945-rs.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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/skbuff.h>
  28. #include <linux/slab.h>
  29. #include <net/mac80211.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/etherdevice.h>
  32. #include <linux/delay.h>
  33. #include <linux/workqueue.h>
  34. #include "commands.h"
  35. #include "3945.h"
  36. #define RS_NAME "iwl-3945-rs"
  37. static s32 il3945_expected_tpt_g[RATE_COUNT_3945] = {
  38. 7, 13, 35, 58, 0, 0, 76, 104, 130, 168, 191, 202
  39. };
  40. static s32 il3945_expected_tpt_g_prot[RATE_COUNT_3945] = {
  41. 7, 13, 35, 58, 0, 0, 0, 80, 93, 113, 123, 125
  42. };
  43. static s32 il3945_expected_tpt_a[RATE_COUNT_3945] = {
  44. 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186
  45. };
  46. static s32 il3945_expected_tpt_b[RATE_COUNT_3945] = {
  47. 7, 13, 35, 58, 0, 0, 0, 0, 0, 0, 0, 0
  48. };
  49. struct il3945_tpt_entry {
  50. s8 min_rssi;
  51. u8 idx;
  52. };
  53. static struct il3945_tpt_entry il3945_tpt_table_a[] = {
  54. {-60, RATE_54M_IDX},
  55. {-64, RATE_48M_IDX},
  56. {-72, RATE_36M_IDX},
  57. {-80, RATE_24M_IDX},
  58. {-84, RATE_18M_IDX},
  59. {-85, RATE_12M_IDX},
  60. {-87, RATE_9M_IDX},
  61. {-89, RATE_6M_IDX}
  62. };
  63. static struct il3945_tpt_entry il3945_tpt_table_g[] = {
  64. {-60, RATE_54M_IDX},
  65. {-64, RATE_48M_IDX},
  66. {-68, RATE_36M_IDX},
  67. {-80, RATE_24M_IDX},
  68. {-84, RATE_18M_IDX},
  69. {-85, RATE_12M_IDX},
  70. {-86, RATE_11M_IDX},
  71. {-88, RATE_5M_IDX},
  72. {-90, RATE_2M_IDX},
  73. {-92, RATE_1M_IDX}
  74. };
  75. #define RATE_MAX_WINDOW 62
  76. #define RATE_FLUSH (3*HZ)
  77. #define RATE_WIN_FLUSH (HZ/2)
  78. #define IL39_RATE_HIGH_TH 11520
  79. #define IL_SUCCESS_UP_TH 8960
  80. #define IL_SUCCESS_DOWN_TH 10880
  81. #define RATE_MIN_FAILURE_TH 6
  82. #define RATE_MIN_SUCCESS_TH 8
  83. #define RATE_DECREASE_TH 1920
  84. #define RATE_RETRY_TH 15
  85. static u8
  86. il3945_get_rate_idx_by_rssi(s32 rssi, enum ieee80211_band band)
  87. {
  88. u32 idx = 0;
  89. u32 table_size = 0;
  90. struct il3945_tpt_entry *tpt_table = NULL;
  91. if (rssi < IL_MIN_RSSI_VAL || rssi > IL_MAX_RSSI_VAL)
  92. rssi = IL_MIN_RSSI_VAL;
  93. switch (band) {
  94. case IEEE80211_BAND_2GHZ:
  95. tpt_table = il3945_tpt_table_g;
  96. table_size = ARRAY_SIZE(il3945_tpt_table_g);
  97. break;
  98. case IEEE80211_BAND_5GHZ:
  99. tpt_table = il3945_tpt_table_a;
  100. table_size = ARRAY_SIZE(il3945_tpt_table_a);
  101. break;
  102. default:
  103. BUG();
  104. break;
  105. }
  106. while (idx < table_size && rssi < tpt_table[idx].min_rssi)
  107. idx++;
  108. idx = min(idx, table_size - 1);
  109. return tpt_table[idx].idx;
  110. }
  111. static void
  112. il3945_clear_win(struct il3945_rate_scale_data *win)
  113. {
  114. win->data = 0;
  115. win->success_counter = 0;
  116. win->success_ratio = -1;
  117. win->counter = 0;
  118. win->average_tpt = IL_INVALID_VALUE;
  119. win->stamp = 0;
  120. }
  121. /**
  122. * il3945_rate_scale_flush_wins - flush out the rate scale wins
  123. *
  124. * Returns the number of wins 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
  129. il3945_rate_scale_flush_wins(struct il3945_rs_sta *rs_sta)
  130. {
  131. int unflushed = 0;
  132. int i;
  133. unsigned long flags;
  134. struct il_priv *il __maybe_unused = rs_sta->il;
  135. /*
  136. * For each rate, if we have collected data on that rate
  137. * and it has been more than RATE_WIN_FLUSH
  138. * since we flushed, clear out the gathered stats
  139. */
  140. for (i = 0; i < 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 + RATE_WIN_FLUSH)) {
  145. D_RATE("flushing %d samples of rate " "idx %d\n",
  146. rs_sta->win[i].counter, i);
  147. il3945_clear_win(&rs_sta->win[i]);
  148. } else
  149. unflushed++;
  150. spin_unlock_irqrestore(&rs_sta->lock, flags);
  151. }
  152. return unflushed;
  153. }
  154. #define RATE_FLUSH_MAX 5000 /* msec */
  155. #define RATE_FLUSH_MIN 50 /* msec */
  156. #define IL_AVERAGE_PACKETS 1500
  157. static void
  158. il3945_bg_rate_scale_flush(unsigned long data)
  159. {
  160. struct il3945_rs_sta *rs_sta = (void *)data;
  161. struct il_priv *il __maybe_unused = rs_sta->il;
  162. int unflushed = 0;
  163. unsigned long flags;
  164. u32 packet_count, duration, pps;
  165. D_RATE("enter\n");
  166. unflushed = il3945_rate_scale_flush_wins(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. D_RATE("Tx'd %d packets in %dms\n", packet_count, duration);
  175. /* Determine packets per second */
  176. if (duration)
  177. pps = (packet_count * 1000) / duration;
  178. else
  179. pps = 0;
  180. if (pps) {
  181. duration = (IL_AVERAGE_PACKETS * 1000) / pps;
  182. if (duration < RATE_FLUSH_MIN)
  183. duration = RATE_FLUSH_MIN;
  184. else if (duration > RATE_FLUSH_MAX)
  185. duration = RATE_FLUSH_MAX;
  186. } else
  187. duration = RATE_FLUSH_MAX;
  188. rs_sta->flush_time = msecs_to_jiffies(duration);
  189. D_RATE("new flush period: %d msec ave %d\n", duration,
  190. packet_count);
  191. mod_timer(&rs_sta->rate_scale_flush,
  192. jiffies + rs_sta->flush_time);
  193. rs_sta->last_partial_flush = jiffies;
  194. } else {
  195. rs_sta->flush_time = RATE_FLUSH;
  196. rs_sta->flush_pending = 0;
  197. }
  198. /* If there weren't any unflushed entries, we don't schedule the timer
  199. * to run again */
  200. rs_sta->last_flush = jiffies;
  201. spin_unlock_irqrestore(&rs_sta->lock, flags);
  202. D_RATE("leave\n");
  203. }
  204. /**
  205. * il3945_collect_tx_data - Update the success/failure sliding win
  206. *
  207. * We keep a sliding win of the last 64 packets transmitted
  208. * at this rate. win->data contains the bitmask of successful
  209. * packets.
  210. */
  211. static void
  212. il3945_collect_tx_data(struct il3945_rs_sta *rs_sta,
  213. struct il3945_rate_scale_data *win, int success,
  214. int retries, int idx)
  215. {
  216. unsigned long flags;
  217. s32 fail_count;
  218. struct il_priv *il __maybe_unused = rs_sta->il;
  219. if (!retries) {
  220. D_RATE("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 win; anything older isn't really relevant any more.
  227. * If we have filled up the sliding win, 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 (win->counter >= RATE_MAX_WINDOW) {
  234. /* remove earliest */
  235. win->counter = RATE_MAX_WINDOW - 1;
  236. if (win->data & (1ULL << (RATE_MAX_WINDOW - 1))) {
  237. win->data &= ~(1ULL << (RATE_MAX_WINDOW - 1));
  238. win->success_counter--;
  239. }
  240. }
  241. /* Increment frames-attempted counter */
  242. win->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. win->data <<= 1;
  247. if (success > 0) {
  248. win->success_counter++;
  249. win->data |= 0x1;
  250. success--;
  251. }
  252. retries--;
  253. }
  254. /* Calculate current success ratio, avoid divide-by-0! */
  255. if (win->counter > 0)
  256. win->success_ratio =
  257. 128 * (100 * win->success_counter) / win->counter;
  258. else
  259. win->success_ratio = IL_INVALID_VALUE;
  260. fail_count = win->counter - win->success_counter;
  261. /* Calculate average throughput, if we have enough history. */
  262. if (fail_count >= RATE_MIN_FAILURE_TH ||
  263. win->success_counter >= RATE_MIN_SUCCESS_TH)
  264. win->average_tpt =
  265. ((win->success_ratio * rs_sta->expected_tpt[idx] +
  266. 64) / 128);
  267. else
  268. win->average_tpt = IL_INVALID_VALUE;
  269. /* Tag this win as having been updated */
  270. win->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
  277. il3945_rs_rate_init(struct il_priv *il, struct ieee80211_sta *sta, u8 sta_id)
  278. {
  279. struct ieee80211_hw *hw = il->hw;
  280. struct ieee80211_conf *conf = &il->hw->conf;
  281. struct il3945_sta_priv *psta;
  282. struct il3945_rs_sta *rs_sta;
  283. struct ieee80211_supported_band *sband;
  284. int i;
  285. D_INFO("enter\n");
  286. if (sta_id == il->hw_params.bcast_id)
  287. goto out;
  288. psta = (struct il3945_sta_priv *)sta->drv_priv;
  289. rs_sta = &psta->rs_sta;
  290. sband = hw->wiphy->bands[conf->chandef.chan->band];
  291. rs_sta->il = il;
  292. rs_sta->start_rate = RATE_INVALID;
  293. /* default to just 802.11b */
  294. rs_sta->expected_tpt = il3945_expected_tpt_b;
  295. rs_sta->last_partial_flush = jiffies;
  296. rs_sta->last_flush = jiffies;
  297. rs_sta->flush_time = RATE_FLUSH;
  298. rs_sta->last_tx_packets = 0;
  299. rs_sta->rate_scale_flush.data = (unsigned long)rs_sta;
  300. rs_sta->rate_scale_flush.function = il3945_bg_rate_scale_flush;
  301. for (i = 0; i < RATE_COUNT_3945; i++)
  302. il3945_clear_win(&rs_sta->win[i]);
  303. /* TODO: what is a good starting rate for STA? About middle? Maybe not
  304. * the lowest or the highest rate.. Could consider using RSSI from
  305. * previous packets? Need to have IEEE 802.1X auth succeed immediately
  306. * after assoc.. */
  307. for (i = sband->n_bitrates - 1; i >= 0; i--) {
  308. if (sta->supp_rates[sband->band] & (1 << i)) {
  309. rs_sta->last_txrate_idx = i;
  310. break;
  311. }
  312. }
  313. il->_3945.sta_supp_rates = sta->supp_rates[sband->band];
  314. /* For 5 GHz band it start at IL_FIRST_OFDM_RATE */
  315. if (sband->band == IEEE80211_BAND_5GHZ) {
  316. rs_sta->last_txrate_idx += IL_FIRST_OFDM_RATE;
  317. il->_3945.sta_supp_rates <<= IL_FIRST_OFDM_RATE;
  318. }
  319. out:
  320. il->stations[sta_id].used &= ~IL_STA_UCODE_INPROGRESS;
  321. D_INFO("leave\n");
  322. }
  323. static void *
  324. il3945_rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
  325. {
  326. return hw->priv;
  327. }
  328. /* rate scale requires free function to be implemented */
  329. static void
  330. il3945_rs_free(void *il)
  331. {
  332. }
  333. static void *
  334. il3945_rs_alloc_sta(void *il_priv, struct ieee80211_sta *sta, gfp_t gfp)
  335. {
  336. struct il3945_rs_sta *rs_sta;
  337. struct il3945_sta_priv *psta = (void *)sta->drv_priv;
  338. struct il_priv *il __maybe_unused = il_priv;
  339. D_RATE("enter\n");
  340. rs_sta = &psta->rs_sta;
  341. spin_lock_init(&rs_sta->lock);
  342. init_timer(&rs_sta->rate_scale_flush);
  343. D_RATE("leave\n");
  344. return rs_sta;
  345. }
  346. static void
  347. il3945_rs_free_sta(void *il_priv, struct ieee80211_sta *sta, void *il_sta)
  348. {
  349. struct il3945_rs_sta *rs_sta = il_sta;
  350. /*
  351. * Be careful not to use any members of il3945_rs_sta (like trying
  352. * to use il_priv to print out debugging) since it may not be fully
  353. * initialized at this point.
  354. */
  355. del_timer_sync(&rs_sta->rate_scale_flush);
  356. }
  357. /**
  358. * il3945_rs_tx_status - Update rate control values based on Tx results
  359. *
  360. * NOTE: Uses il_priv->retry_rate for the # of retries attempted by
  361. * the hardware for each rate.
  362. */
  363. static void
  364. il3945_rs_tx_status(void *il_rate, struct ieee80211_supported_band *sband,
  365. struct ieee80211_sta *sta, void *il_sta,
  366. struct sk_buff *skb)
  367. {
  368. s8 retries = 0, current_count;
  369. int scale_rate_idx, first_idx, last_idx;
  370. unsigned long flags;
  371. struct il_priv *il = (struct il_priv *)il_rate;
  372. struct il3945_rs_sta *rs_sta = il_sta;
  373. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  374. D_RATE("enter\n");
  375. retries = info->status.rates[0].count;
  376. /* Sanity Check for retries */
  377. if (retries > RATE_RETRY_TH)
  378. retries = RATE_RETRY_TH;
  379. first_idx = sband->bitrates[info->status.rates[0].idx].hw_value;
  380. if (first_idx < 0 || first_idx >= RATE_COUNT_3945) {
  381. D_RATE("leave: Rate out of bounds: %d\n", first_idx);
  382. return;
  383. }
  384. if (!il_sta) {
  385. D_RATE("leave: No STA il data to update!\n");
  386. return;
  387. }
  388. /* Treat uninitialized rate scaling data same as non-existing. */
  389. if (!rs_sta->il) {
  390. D_RATE("leave: STA il data uninitialized!\n");
  391. return;
  392. }
  393. rs_sta->tx_packets++;
  394. scale_rate_idx = first_idx;
  395. last_idx = first_idx;
  396. /*
  397. * Update the win for each rate. We determine which rates
  398. * were Tx'd based on the total number of retries vs. the number
  399. * of retries configured for each rate -- currently set to the
  400. * il value 'retry_rate' vs. rate specific
  401. *
  402. * On exit from this while loop last_idx indicates the rate
  403. * at which the frame was finally transmitted (or failed if no
  404. * ACK)
  405. */
  406. while (retries > 1) {
  407. if ((retries - 1) < il->retry_rate) {
  408. current_count = (retries - 1);
  409. last_idx = scale_rate_idx;
  410. } else {
  411. current_count = il->retry_rate;
  412. last_idx = il3945_rs_next_rate(il, scale_rate_idx);
  413. }
  414. /* Update this rate accounting for as many retries
  415. * as was used for it (per current_count) */
  416. il3945_collect_tx_data(rs_sta, &rs_sta->win[scale_rate_idx], 0,
  417. current_count, scale_rate_idx);
  418. D_RATE("Update rate %d for %d retries.\n", scale_rate_idx,
  419. current_count);
  420. retries -= current_count;
  421. scale_rate_idx = last_idx;
  422. }
  423. /* Update the last idx win with success/failure based on ACK */
  424. D_RATE("Update rate %d with %s.\n", last_idx,
  425. (info->flags & IEEE80211_TX_STAT_ACK) ? "success" : "failure");
  426. il3945_collect_tx_data(rs_sta, &rs_sta->win[last_idx],
  427. info->flags & IEEE80211_TX_STAT_ACK, 1,
  428. last_idx);
  429. /* We updated the rate scale win -- 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 + rs_sta->flush_time)) {
  435. rs_sta->last_partial_flush = jiffies;
  436. rs_sta->flush_pending = 1;
  437. mod_timer(&rs_sta->rate_scale_flush,
  438. jiffies + rs_sta->flush_time);
  439. }
  440. spin_unlock_irqrestore(&rs_sta->lock, flags);
  441. D_RATE("leave\n");
  442. }
  443. static u16
  444. il3945_get_adjacent_rate(struct il3945_rs_sta *rs_sta, u8 idx, u16 rate_mask,
  445. enum ieee80211_band band)
  446. {
  447. u8 high = RATE_INVALID;
  448. u8 low = RATE_INVALID;
  449. struct il_priv *il __maybe_unused = rs_sta->il;
  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 = idx - 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 = idx + 1;
  465. for (mask = (1 << i); i < RATE_COUNT_3945; i++, mask <<= 1) {
  466. if (rate_mask & mask) {
  467. high = i;
  468. break;
  469. }
  470. }
  471. return (high << 8) | low;
  472. }
  473. low = idx;
  474. while (low != RATE_INVALID) {
  475. if (rs_sta->tgg)
  476. low = il3945_rates[low].prev_rs_tgg;
  477. else
  478. low = il3945_rates[low].prev_rs;
  479. if (low == RATE_INVALID)
  480. break;
  481. if (rate_mask & (1 << low))
  482. break;
  483. D_RATE("Skipping masked lower rate: %d\n", low);
  484. }
  485. high = idx;
  486. while (high != RATE_INVALID) {
  487. if (rs_sta->tgg)
  488. high = il3945_rates[high].next_rs_tgg;
  489. else
  490. high = il3945_rates[high].next_rs;
  491. if (high == RATE_INVALID)
  492. break;
  493. if (rate_mask & (1 << high))
  494. break;
  495. D_RATE("Skipping masked higher rate: %d\n", high);
  496. }
  497. return (high << 8) | low;
  498. }
  499. /**
  500. * il3945_rs_get_rate - find the rate for the requested packet
  501. *
  502. * Returns the ieee80211_rate structure allocated by the driver.
  503. *
  504. * The rate control algorithm has no internal mapping between hw_mode's
  505. * rate ordering and the rate ordering used by the rate control algorithm.
  506. *
  507. * The rate control algorithm uses a single table of rates that goes across
  508. * the entire A/B/G spectrum vs. being limited to just one particular
  509. * hw_mode.
  510. *
  511. * As such, we can't convert the idx obtained below into the hw_mode's
  512. * rate table and must reference the driver allocated rate table
  513. *
  514. */
  515. static void
  516. il3945_rs_get_rate(void *il_r, struct ieee80211_sta *sta, void *il_sta,
  517. 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 = RATE_INVALID;
  522. u8 high = RATE_INVALID;
  523. u16 high_low;
  524. int idx;
  525. struct il3945_rs_sta *rs_sta = il_sta;
  526. struct il3945_rate_scale_data *win = NULL;
  527. int current_tpt = IL_INVALID_VALUE;
  528. int low_tpt = IL_INVALID_VALUE;
  529. int high_tpt = IL_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 il_priv *il __maybe_unused = (struct il_priv *)il_r;
  536. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  537. D_RATE("enter\n");
  538. /* Treat uninitialized rate scaling data same as non-existing. */
  539. if (rs_sta && !rs_sta->il) {
  540. D_RATE("Rate scaling information not initialized yet.\n");
  541. il_sta = NULL;
  542. }
  543. if (rate_control_send_low(sta, il_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 += IL_FIRST_OFDM_RATE;
  550. if (max_rate_idx < 0 || max_rate_idx >= RATE_COUNT)
  551. max_rate_idx = -1;
  552. idx = min(rs_sta->last_txrate_idx & 0xffff, RATE_COUNT_3945 - 1);
  553. if (sband->band == IEEE80211_BAND_5GHZ)
  554. rate_mask = rate_mask << IL_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 != RATE_INVALID) {
  560. if (rs_sta->start_rate < idx &&
  561. (rate_mask & (1 << rs_sta->start_rate)))
  562. idx = rs_sta->start_rate;
  563. rs_sta->start_rate = RATE_INVALID;
  564. }
  565. /* force user max rate if set by user */
  566. if (max_rate_idx != -1 && max_rate_idx < idx) {
  567. if (rate_mask & (1 << max_rate_idx))
  568. idx = max_rate_idx;
  569. }
  570. win = &(rs_sta->win[idx]);
  571. fail_count = win->counter - win->success_counter;
  572. if (fail_count < RATE_MIN_FAILURE_TH &&
  573. win->success_counter < RATE_MIN_SUCCESS_TH) {
  574. spin_unlock_irqrestore(&rs_sta->lock, flags);
  575. D_RATE("Invalid average_tpt on rate %d: "
  576. "counter: %d, success_counter: %d, "
  577. "expected_tpt is %sNULL\n", idx, win->counter,
  578. win->success_counter,
  579. rs_sta->expected_tpt ? "not " : "");
  580. /* Can't calculate this yet; not enough history */
  581. win->average_tpt = IL_INVALID_VALUE;
  582. goto out;
  583. }
  584. current_tpt = win->average_tpt;
  585. high_low =
  586. il3945_get_adjacent_rate(rs_sta, idx, rate_mask, sband->band);
  587. low = high_low & 0xff;
  588. high = (high_low >> 8) & 0xff;
  589. /* If user set max rate, dont allow higher than user constrain */
  590. if (max_rate_idx != -1 && max_rate_idx < high)
  591. high = RATE_INVALID;
  592. /* Collect Measured throughputs of adjacent rates */
  593. if (low != RATE_INVALID)
  594. low_tpt = rs_sta->win[low].average_tpt;
  595. if (high != RATE_INVALID)
  596. high_tpt = rs_sta->win[high].average_tpt;
  597. spin_unlock_irqrestore(&rs_sta->lock, flags);
  598. scale_action = 0;
  599. /* Low success ratio , need to drop the rate */
  600. if (win->success_ratio < RATE_DECREASE_TH || !current_tpt) {
  601. D_RATE("decrease rate because of low success_ratio\n");
  602. scale_action = -1;
  603. /* No throughput measured yet for adjacent rates,
  604. * try increase */
  605. } else if (low_tpt == IL_INVALID_VALUE && high_tpt == IL_INVALID_VALUE) {
  606. if (high != RATE_INVALID &&
  607. win->success_ratio >= RATE_INCREASE_TH)
  608. scale_action = 1;
  609. else if (low != RATE_INVALID)
  610. scale_action = 0;
  611. /* Both adjacent throughputs are measured, but neither one has
  612. * better throughput; we're using the best rate, don't change
  613. * it! */
  614. } else if (low_tpt != IL_INVALID_VALUE && high_tpt != IL_INVALID_VALUE
  615. && low_tpt < current_tpt && high_tpt < current_tpt) {
  616. D_RATE("No action -- low [%d] & high [%d] < "
  617. "current_tpt [%d]\n", low_tpt, high_tpt, current_tpt);
  618. scale_action = 0;
  619. /* At least one of the rates has better throughput */
  620. } else {
  621. if (high_tpt != IL_INVALID_VALUE) {
  622. /* High rate has better throughput, Increase
  623. * rate */
  624. if (high_tpt > current_tpt &&
  625. win->success_ratio >= RATE_INCREASE_TH)
  626. scale_action = 1;
  627. else {
  628. D_RATE("decrease rate because of high tpt\n");
  629. scale_action = 0;
  630. }
  631. } else if (low_tpt != IL_INVALID_VALUE) {
  632. if (low_tpt > current_tpt) {
  633. D_RATE("decrease rate because of low tpt\n");
  634. scale_action = -1;
  635. } else if (win->success_ratio >= RATE_INCREASE_TH) {
  636. /* Lower rate has better
  637. * throughput,decrease rate */
  638. scale_action = 1;
  639. }
  640. }
  641. }
  642. /* Sanity check; asked for decrease, but success rate or throughput
  643. * has been good at old rate. Don't change it. */
  644. if (scale_action == -1 && low != RATE_INVALID &&
  645. (win->success_ratio > RATE_HIGH_TH ||
  646. current_tpt > 100 * rs_sta->expected_tpt[low]))
  647. scale_action = 0;
  648. switch (scale_action) {
  649. case -1:
  650. /* Decrese rate */
  651. if (low != RATE_INVALID)
  652. idx = low;
  653. break;
  654. case 1:
  655. /* Increase rate */
  656. if (high != RATE_INVALID)
  657. idx = high;
  658. break;
  659. case 0:
  660. default:
  661. /* No change */
  662. break;
  663. }
  664. D_RATE("Selected %d (action %d) - low %d high %d\n", idx, scale_action,
  665. low, high);
  666. out:
  667. if (sband->band == IEEE80211_BAND_5GHZ) {
  668. if (WARN_ON_ONCE(idx < IL_FIRST_OFDM_RATE))
  669. idx = IL_FIRST_OFDM_RATE;
  670. rs_sta->last_txrate_idx = idx;
  671. info->control.rates[0].idx = idx - IL_FIRST_OFDM_RATE;
  672. } else {
  673. rs_sta->last_txrate_idx = idx;
  674. info->control.rates[0].idx = rs_sta->last_txrate_idx;
  675. }
  676. info->control.rates[0].count = 1;
  677. D_RATE("leave: %d\n", idx);
  678. }
  679. #ifdef CONFIG_MAC80211_DEBUGFS
  680. static ssize_t
  681. il3945_sta_dbgfs_stats_table_read(struct file *file, char __user *user_buf,
  682. size_t count, loff_t *ppos)
  683. {
  684. char *buff;
  685. int desc = 0;
  686. int j;
  687. ssize_t ret;
  688. struct il3945_rs_sta *lq_sta = file->private_data;
  689. buff = kmalloc(1024, GFP_KERNEL);
  690. if (!buff)
  691. return -ENOMEM;
  692. desc +=
  693. sprintf(buff + desc,
  694. "tx packets=%d last rate idx=%d\n"
  695. "rate=0x%X flush time %d\n", lq_sta->tx_packets,
  696. lq_sta->last_txrate_idx, lq_sta->start_rate,
  697. jiffies_to_msecs(lq_sta->flush_time));
  698. for (j = 0; j < RATE_COUNT_3945; j++) {
  699. desc +=
  700. sprintf(buff + desc, "counter=%d success=%d %%=%d\n",
  701. lq_sta->win[j].counter,
  702. lq_sta->win[j].success_counter,
  703. lq_sta->win[j].success_ratio);
  704. }
  705. ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
  706. kfree(buff);
  707. return ret;
  708. }
  709. static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
  710. .read = il3945_sta_dbgfs_stats_table_read,
  711. .open = simple_open,
  712. .llseek = default_llseek,
  713. };
  714. static void
  715. il3945_add_debugfs(void *il, void *il_sta, struct dentry *dir)
  716. {
  717. struct il3945_rs_sta *lq_sta = il_sta;
  718. lq_sta->rs_sta_dbgfs_stats_table_file =
  719. debugfs_create_file("rate_stats_table", 0600, dir, lq_sta,
  720. &rs_sta_dbgfs_stats_table_ops);
  721. }
  722. static void
  723. il3945_remove_debugfs(void *il, void *il_sta)
  724. {
  725. struct il3945_rs_sta *lq_sta = il_sta;
  726. debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
  727. }
  728. #endif
  729. /*
  730. * Initialization of rate scaling information is done by driver after
  731. * the station is added. Since mac80211 calls this function before a
  732. * station is added we ignore it.
  733. */
  734. static void
  735. il3945_rs_rate_init_stub(void *il_r, struct ieee80211_supported_band *sband,
  736. struct cfg80211_chan_def *chandef,
  737. struct ieee80211_sta *sta, void *il_sta)
  738. {
  739. }
  740. static const struct rate_control_ops rs_ops = {
  741. .name = RS_NAME,
  742. .tx_status = il3945_rs_tx_status,
  743. .get_rate = il3945_rs_get_rate,
  744. .rate_init = il3945_rs_rate_init_stub,
  745. .alloc = il3945_rs_alloc,
  746. .free = il3945_rs_free,
  747. .alloc_sta = il3945_rs_alloc_sta,
  748. .free_sta = il3945_rs_free_sta,
  749. #ifdef CONFIG_MAC80211_DEBUGFS
  750. .add_sta_debugfs = il3945_add_debugfs,
  751. .remove_sta_debugfs = il3945_remove_debugfs,
  752. #endif
  753. };
  754. void
  755. il3945_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
  756. {
  757. struct il_priv *il = hw->priv;
  758. s32 rssi = 0;
  759. unsigned long flags;
  760. struct il3945_rs_sta *rs_sta;
  761. struct ieee80211_sta *sta;
  762. struct il3945_sta_priv *psta;
  763. D_RATE("enter\n");
  764. rcu_read_lock();
  765. sta = ieee80211_find_sta(il->vif, il->stations[sta_id].sta.sta.addr);
  766. if (!sta) {
  767. D_RATE("Unable to find station to initialize rate scaling.\n");
  768. rcu_read_unlock();
  769. return;
  770. }
  771. psta = (void *)sta->drv_priv;
  772. rs_sta = &psta->rs_sta;
  773. spin_lock_irqsave(&rs_sta->lock, flags);
  774. rs_sta->tgg = 0;
  775. switch (il->band) {
  776. case IEEE80211_BAND_2GHZ:
  777. /* TODO: this always does G, not a regression */
  778. if (il->active.flags & RXON_FLG_TGG_PROTECT_MSK) {
  779. rs_sta->tgg = 1;
  780. rs_sta->expected_tpt = il3945_expected_tpt_g_prot;
  781. } else
  782. rs_sta->expected_tpt = il3945_expected_tpt_g;
  783. break;
  784. case IEEE80211_BAND_5GHZ:
  785. rs_sta->expected_tpt = il3945_expected_tpt_a;
  786. break;
  787. default:
  788. BUG();
  789. break;
  790. }
  791. spin_unlock_irqrestore(&rs_sta->lock, flags);
  792. rssi = il->_3945.last_rx_rssi;
  793. if (rssi == 0)
  794. rssi = IL_MIN_RSSI_VAL;
  795. D_RATE("Network RSSI: %d\n", rssi);
  796. rs_sta->start_rate = il3945_get_rate_idx_by_rssi(rssi, il->band);
  797. D_RATE("leave: rssi %d assign rate idx: " "%d (plcp 0x%x)\n", rssi,
  798. rs_sta->start_rate, il3945_rates[rs_sta->start_rate].plcp);
  799. rcu_read_unlock();
  800. }
  801. int
  802. il3945_rate_control_register(void)
  803. {
  804. return ieee80211_rate_control_register(&rs_ops);
  805. }
  806. void
  807. il3945_rate_control_unregister(void)
  808. {
  809. ieee80211_rate_control_unregister(&rs_ops);
  810. }