3945-rs.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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 "commands.h"
  36. #include "3945.h"
  37. #define RS_NAME "iwl-3945-rs"
  38. static s32 il3945_expected_tpt_g[RATE_COUNT_3945] = {
  39. 7, 13, 35, 58, 0, 0, 76, 104, 130, 168, 191, 202
  40. };
  41. static s32 il3945_expected_tpt_g_prot[RATE_COUNT_3945] = {
  42. 7, 13, 35, 58, 0, 0, 0, 80, 93, 113, 123, 125
  43. };
  44. static s32 il3945_expected_tpt_a[RATE_COUNT_3945] = {
  45. 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186
  46. };
  47. static s32 il3945_expected_tpt_b[RATE_COUNT_3945] = {
  48. 7, 13, 35, 58, 0, 0, 0, 0, 0, 0, 0, 0
  49. };
  50. struct il3945_tpt_entry {
  51. s8 min_rssi;
  52. u8 idx;
  53. };
  54. static struct il3945_tpt_entry il3945_tpt_table_a[] = {
  55. {-60, RATE_54M_IDX},
  56. {-64, RATE_48M_IDX},
  57. {-72, RATE_36M_IDX},
  58. {-80, RATE_24M_IDX},
  59. {-84, RATE_18M_IDX},
  60. {-85, RATE_12M_IDX},
  61. {-87, RATE_9M_IDX},
  62. {-89, RATE_6M_IDX}
  63. };
  64. static struct il3945_tpt_entry il3945_tpt_table_g[] = {
  65. {-60, RATE_54M_IDX},
  66. {-64, RATE_48M_IDX},
  67. {-68, RATE_36M_IDX},
  68. {-80, RATE_24M_IDX},
  69. {-84, RATE_18M_IDX},
  70. {-85, RATE_12M_IDX},
  71. {-86, RATE_11M_IDX},
  72. {-88, RATE_5M_IDX},
  73. {-90, RATE_2M_IDX},
  74. {-92, RATE_1M_IDX}
  75. };
  76. #define RATE_MAX_WINDOW 62
  77. #define RATE_FLUSH (3*HZ)
  78. #define RATE_WIN_FLUSH (HZ/2)
  79. #define IL39_RATE_HIGH_TH 11520
  80. #define IL_SUCCESS_UP_TH 8960
  81. #define IL_SUCCESS_DOWN_TH 10880
  82. #define RATE_MIN_FAILURE_TH 6
  83. #define RATE_MIN_SUCCESS_TH 8
  84. #define RATE_DECREASE_TH 1920
  85. #define RATE_RETRY_TH 15
  86. static u8 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 il3945_clear_win(struct il3945_rate_scale_data *win)
  112. {
  113. win->data = 0;
  114. win->success_counter = 0;
  115. win->success_ratio = -1;
  116. win->counter = 0;
  117. win->average_tpt = IL_INVALID_VALUE;
  118. win->stamp = 0;
  119. }
  120. /**
  121. * il3945_rate_scale_flush_wins - flush out the rate scale wins
  122. *
  123. * Returns the number of wins that have gathered data but were
  124. * not flushed. If there were any that were not flushed, then
  125. * reschedule the rate flushing routine.
  126. */
  127. static int il3945_rate_scale_flush_wins(struct il3945_rs_sta *rs_sta)
  128. {
  129. int unflushed = 0;
  130. int i;
  131. unsigned long flags;
  132. struct il_priv *il __maybe_unused = rs_sta->il;
  133. /*
  134. * For each rate, if we have collected data on that rate
  135. * and it has been more than RATE_WIN_FLUSH
  136. * since we flushed, clear out the gathered stats
  137. */
  138. for (i = 0; i < RATE_COUNT_3945; i++) {
  139. if (!rs_sta->win[i].counter)
  140. continue;
  141. spin_lock_irqsave(&rs_sta->lock, flags);
  142. if (time_after(jiffies, rs_sta->win[i].stamp +
  143. RATE_WIN_FLUSH)) {
  144. D_RATE("flushing %d samples of rate "
  145. "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 il3945_bg_rate_scale_flush(unsigned long data)
  158. {
  159. struct il3945_rs_sta *rs_sta = (void *)data;
  160. struct il_priv *il __maybe_unused = rs_sta->il;
  161. int unflushed = 0;
  162. unsigned long flags;
  163. u32 packet_count, duration, pps;
  164. D_RATE("enter\n");
  165. unflushed = il3945_rate_scale_flush_wins(rs_sta);
  166. spin_lock_irqsave(&rs_sta->lock, flags);
  167. /* Number of packets Rx'd since last time this timer ran */
  168. packet_count = (rs_sta->tx_packets - rs_sta->last_tx_packets) + 1;
  169. rs_sta->last_tx_packets = rs_sta->tx_packets + 1;
  170. if (unflushed) {
  171. duration =
  172. jiffies_to_msecs(jiffies - rs_sta->last_partial_flush);
  173. D_RATE("Tx'd %d packets in %dms\n",
  174. 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",
  190. duration, packet_count);
  191. mod_timer(&rs_sta->rate_scale_flush, jiffies +
  192. 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 il3945_collect_tx_data(struct il3945_rs_sta *rs_sta,
  212. struct il3945_rate_scale_data *win,
  213. int success, int retries, int idx)
  214. {
  215. unsigned long flags;
  216. s32 fail_count;
  217. struct il_priv *il __maybe_unused = rs_sta->il;
  218. if (!retries) {
  219. D_RATE("leave: retries == 0 -- should be at least 1\n");
  220. return;
  221. }
  222. spin_lock_irqsave(&rs_sta->lock, flags);
  223. /*
  224. * Keep track of only the latest 62 tx frame attempts in this rate's
  225. * history win; anything older isn't really relevant any more.
  226. * If we have filled up the sliding win, drop the oldest attempt;
  227. * if the oldest attempt (highest bit in bitmap) shows "success",
  228. * subtract "1" from the success counter (this is the main reason
  229. * we keep these bitmaps!).
  230. * */
  231. while (retries > 0) {
  232. if (win->counter >= RATE_MAX_WINDOW) {
  233. /* remove earliest */
  234. win->counter = RATE_MAX_WINDOW - 1;
  235. if (win->data & (1ULL << (RATE_MAX_WINDOW - 1))) {
  236. win->data &= ~(1ULL << (RATE_MAX_WINDOW - 1));
  237. win->success_counter--;
  238. }
  239. }
  240. /* Increment frames-attempted counter */
  241. win->counter++;
  242. /* Shift bitmap by one frame (throw away oldest history),
  243. * OR in "1", and increment "success" if this
  244. * frame was successful. */
  245. win->data <<= 1;
  246. if (success > 0) {
  247. win->success_counter++;
  248. win->data |= 0x1;
  249. success--;
  250. }
  251. retries--;
  252. }
  253. /* Calculate current success ratio, avoid divide-by-0! */
  254. if (win->counter > 0)
  255. win->success_ratio = 128 * (100 * win->success_counter)
  256. / win->counter;
  257. else
  258. win->success_ratio = IL_INVALID_VALUE;
  259. fail_count = win->counter - win->success_counter;
  260. /* Calculate average throughput, if we have enough history. */
  261. if (fail_count >= RATE_MIN_FAILURE_TH ||
  262. win->success_counter >= RATE_MIN_SUCCESS_TH)
  263. win->average_tpt = ((win->success_ratio *
  264. rs_sta->expected_tpt[idx] + 64) / 128);
  265. else
  266. win->average_tpt = IL_INVALID_VALUE;
  267. /* Tag this win as having been updated */
  268. win->stamp = jiffies;
  269. spin_unlock_irqrestore(&rs_sta->lock, flags);
  270. }
  271. /*
  272. * Called after adding a new station to initialize rate scaling
  273. */
  274. void il3945_rs_rate_init(struct il_priv *il, struct ieee80211_sta *sta, u8 sta_id)
  275. {
  276. struct ieee80211_hw *hw = il->hw;
  277. struct ieee80211_conf *conf = &il->hw->conf;
  278. struct il3945_sta_priv *psta;
  279. struct il3945_rs_sta *rs_sta;
  280. struct ieee80211_supported_band *sband;
  281. int i;
  282. D_INFO("enter\n");
  283. if (sta_id == il->ctx.bcast_sta_id)
  284. goto out;
  285. psta = (struct il3945_sta_priv *) sta->drv_priv;
  286. rs_sta = &psta->rs_sta;
  287. sband = hw->wiphy->bands[conf->channel->band];
  288. rs_sta->il = il;
  289. rs_sta->start_rate = RATE_INVALID;
  290. /* default to just 802.11b */
  291. rs_sta->expected_tpt = il3945_expected_tpt_b;
  292. rs_sta->last_partial_flush = jiffies;
  293. rs_sta->last_flush = jiffies;
  294. rs_sta->flush_time = RATE_FLUSH;
  295. rs_sta->last_tx_packets = 0;
  296. rs_sta->rate_scale_flush.data = (unsigned long)rs_sta;
  297. rs_sta->rate_scale_flush.function = il3945_bg_rate_scale_flush;
  298. for (i = 0; i < RATE_COUNT_3945; i++)
  299. il3945_clear_win(&rs_sta->win[i]);
  300. /* TODO: what is a good starting rate for STA? About middle? Maybe not
  301. * the lowest or the highest rate.. Could consider using RSSI from
  302. * previous packets? Need to have IEEE 802.1X auth succeed immediately
  303. * after assoc.. */
  304. for (i = sband->n_bitrates - 1; i >= 0; i--) {
  305. if (sta->supp_rates[sband->band] & (1 << i)) {
  306. rs_sta->last_txrate_idx = i;
  307. break;
  308. }
  309. }
  310. il->_3945.sta_supp_rates = sta->supp_rates[sband->band];
  311. /* For 5 GHz band it start at IL_FIRST_OFDM_RATE */
  312. if (sband->band == IEEE80211_BAND_5GHZ) {
  313. rs_sta->last_txrate_idx += IL_FIRST_OFDM_RATE;
  314. il->_3945.sta_supp_rates = il->_3945.sta_supp_rates <<
  315. IL_FIRST_OFDM_RATE;
  316. }
  317. out:
  318. il->stations[sta_id].used &= ~IL_STA_UCODE_INPROGRESS;
  319. D_INFO("leave\n");
  320. }
  321. static void *il3945_rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
  322. {
  323. return hw->priv;
  324. }
  325. /* rate scale requires free function to be implemented */
  326. static void il3945_rs_free(void *il)
  327. {
  328. return;
  329. }
  330. static void *il3945_rs_alloc_sta(void *il_priv, struct ieee80211_sta *sta, gfp_t gfp)
  331. {
  332. struct il3945_rs_sta *rs_sta;
  333. struct il3945_sta_priv *psta = (void *) sta->drv_priv;
  334. struct il_priv *il __maybe_unused = il_priv;
  335. D_RATE("enter\n");
  336. rs_sta = &psta->rs_sta;
  337. spin_lock_init(&rs_sta->lock);
  338. init_timer(&rs_sta->rate_scale_flush);
  339. D_RATE("leave\n");
  340. return rs_sta;
  341. }
  342. static void il3945_rs_free_sta(void *il_priv, struct ieee80211_sta *sta,
  343. void *il_sta)
  344. {
  345. struct il3945_rs_sta *rs_sta = il_sta;
  346. /*
  347. * Be careful not to use any members of il3945_rs_sta (like trying
  348. * to use il_priv to print out debugging) since it may not be fully
  349. * initialized at this point.
  350. */
  351. del_timer_sync(&rs_sta->rate_scale_flush);
  352. }
  353. /**
  354. * il3945_rs_tx_status - Update rate control values based on Tx results
  355. *
  356. * NOTE: Uses il_priv->retry_rate for the # of retries attempted by
  357. * the hardware for each rate.
  358. */
  359. static void il3945_rs_tx_status(void *il_rate, struct ieee80211_supported_band *sband,
  360. struct ieee80211_sta *sta, void *il_sta,
  361. struct sk_buff *skb)
  362. {
  363. s8 retries = 0, current_count;
  364. int scale_rate_idx, first_idx, last_idx;
  365. unsigned long flags;
  366. struct il_priv *il = (struct il_priv *)il_rate;
  367. struct il3945_rs_sta *rs_sta = il_sta;
  368. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  369. D_RATE("enter\n");
  370. retries = info->status.rates[0].count;
  371. /* Sanity Check for retries */
  372. if (retries > RATE_RETRY_TH)
  373. retries = RATE_RETRY_TH;
  374. first_idx = sband->bitrates[info->status.rates[0].idx].hw_value;
  375. if (first_idx < 0 || first_idx >= RATE_COUNT_3945) {
  376. D_RATE("leave: Rate out of bounds: %d\n", first_idx);
  377. return;
  378. }
  379. if (!il_sta) {
  380. D_RATE("leave: No STA il data to update!\n");
  381. return;
  382. }
  383. /* Treat uninitialized rate scaling data same as non-existing. */
  384. if (!rs_sta->il) {
  385. D_RATE("leave: STA il data uninitialized!\n");
  386. return;
  387. }
  388. rs_sta->tx_packets++;
  389. scale_rate_idx = first_idx;
  390. last_idx = first_idx;
  391. /*
  392. * Update the win for each rate. We determine which rates
  393. * were Tx'd based on the total number of retries vs. the number
  394. * of retries configured for each rate -- currently set to the
  395. * il value 'retry_rate' vs. rate specific
  396. *
  397. * On exit from this while loop last_idx indicates the rate
  398. * at which the frame was finally transmitted (or failed if no
  399. * ACK)
  400. */
  401. while (retries > 1) {
  402. if ((retries - 1) < il->retry_rate) {
  403. current_count = (retries - 1);
  404. last_idx = scale_rate_idx;
  405. } else {
  406. current_count = il->retry_rate;
  407. last_idx = il3945_rs_next_rate(il,
  408. scale_rate_idx);
  409. }
  410. /* Update this rate accounting for as many retries
  411. * as was used for it (per current_count) */
  412. il3945_collect_tx_data(rs_sta,
  413. &rs_sta->win[scale_rate_idx],
  414. 0, current_count, scale_rate_idx);
  415. D_RATE("Update rate %d for %d retries.\n",
  416. scale_rate_idx, current_count);
  417. retries -= current_count;
  418. scale_rate_idx = last_idx;
  419. }
  420. /* Update the last idx win with success/failure based on ACK */
  421. D_RATE("Update rate %d with %s.\n",
  422. last_idx,
  423. (info->flags & IEEE80211_TX_STAT_ACK) ?
  424. "success" : "failure");
  425. il3945_collect_tx_data(rs_sta,
  426. &rs_sta->win[last_idx],
  427. info->flags & IEEE80211_TX_STAT_ACK, 1, last_idx);
  428. /* We updated the rate scale win -- if its been more than
  429. * flush_time since the last run, schedule the flush
  430. * again */
  431. spin_lock_irqsave(&rs_sta->lock, flags);
  432. if (!rs_sta->flush_pending &&
  433. time_after(jiffies, rs_sta->last_flush +
  434. 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 il3945_get_adjacent_rate(struct il3945_rs_sta *rs_sta,
  444. u8 idx, u16 rate_mask, enum ieee80211_band band)
  445. {
  446. u8 high = RATE_INVALID;
  447. u8 low = RATE_INVALID;
  448. struct il_priv *il __maybe_unused = rs_sta->il;
  449. /* 802.11A walks to the next literal adjacent rate in
  450. * the rate table */
  451. if (unlikely(band == IEEE80211_BAND_5GHZ)) {
  452. int i;
  453. u32 mask;
  454. /* Find the previous rate that is in the rate mask */
  455. i = idx - 1;
  456. for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
  457. if (rate_mask & mask) {
  458. low = i;
  459. break;
  460. }
  461. }
  462. /* Find the next rate that is in the rate mask */
  463. i = idx + 1;
  464. for (mask = (1 << i); i < RATE_COUNT_3945;
  465. 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 il3945_rs_get_rate(void *il_r, struct ieee80211_sta *sta,
  516. void *il_sta, struct ieee80211_tx_rate_control *txrc)
  517. {
  518. struct ieee80211_supported_band *sband = txrc->sband;
  519. struct sk_buff *skb = txrc->skb;
  520. u8 low = RATE_INVALID;
  521. u8 high = RATE_INVALID;
  522. u16 high_low;
  523. int idx;
  524. struct il3945_rs_sta *rs_sta = il_sta;
  525. struct il3945_rate_scale_data *win = NULL;
  526. int current_tpt = IL_INVALID_VALUE;
  527. int low_tpt = IL_INVALID_VALUE;
  528. int high_tpt = IL_INVALID_VALUE;
  529. u32 fail_count;
  530. s8 scale_action = 0;
  531. unsigned long flags;
  532. u16 rate_mask;
  533. s8 max_rate_idx = -1;
  534. struct il_priv *il __maybe_unused = (struct il_priv *)il_r;
  535. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  536. D_RATE("enter\n");
  537. /* Treat uninitialized rate scaling data same as non-existing. */
  538. if (rs_sta && !rs_sta->il) {
  539. D_RATE("Rate scaling information not initialized yet.\n");
  540. il_sta = NULL;
  541. }
  542. if (rate_control_send_low(sta, il_sta, txrc))
  543. return;
  544. rate_mask = sta->supp_rates[sband->band];
  545. /* get user max rate if set */
  546. max_rate_idx = txrc->max_rate_idx;
  547. if (sband->band == IEEE80211_BAND_5GHZ && max_rate_idx != -1)
  548. max_rate_idx += IL_FIRST_OFDM_RATE;
  549. if (max_rate_idx < 0 || max_rate_idx >= RATE_COUNT)
  550. max_rate_idx = -1;
  551. idx = min(rs_sta->last_txrate_idx & 0xffff, RATE_COUNT_3945 - 1);
  552. if (sband->band == IEEE80211_BAND_5GHZ)
  553. rate_mask = rate_mask << IL_FIRST_OFDM_RATE;
  554. spin_lock_irqsave(&rs_sta->lock, flags);
  555. /* for recent assoc, choose best rate regarding
  556. * to rssi value
  557. */
  558. if (rs_sta->start_rate != RATE_INVALID) {
  559. if (rs_sta->start_rate < idx &&
  560. (rate_mask & (1 << rs_sta->start_rate)))
  561. idx = rs_sta->start_rate;
  562. rs_sta->start_rate = RATE_INVALID;
  563. }
  564. /* force user max rate if set by user */
  565. if (max_rate_idx != -1 && max_rate_idx < idx) {
  566. if (rate_mask & (1 << max_rate_idx))
  567. idx = max_rate_idx;
  568. }
  569. win = &(rs_sta->win[idx]);
  570. fail_count = win->counter - win->success_counter;
  571. if (fail_count < RATE_MIN_FAILURE_TH &&
  572. win->success_counter < RATE_MIN_SUCCESS_TH) {
  573. spin_unlock_irqrestore(&rs_sta->lock, flags);
  574. D_RATE("Invalid average_tpt on rate %d: "
  575. "counter: %d, success_counter: %d, "
  576. "expected_tpt is %sNULL\n",
  577. idx,
  578. win->counter,
  579. win->success_counter,
  580. rs_sta->expected_tpt ? "not " : "");
  581. /* Can't calculate this yet; not enough history */
  582. win->average_tpt = IL_INVALID_VALUE;
  583. goto out;
  584. }
  585. current_tpt = win->average_tpt;
  586. high_low = il3945_get_adjacent_rate(rs_sta, idx, rate_mask,
  587. sband->band);
  588. low = high_low & 0xff;
  589. high = (high_low >> 8) & 0xff;
  590. /* If user set max rate, dont allow higher than user constrain */
  591. if (max_rate_idx != -1 && max_rate_idx < high)
  592. high = RATE_INVALID;
  593. /* Collect Measured throughputs of adjacent rates */
  594. if (low != RATE_INVALID)
  595. low_tpt = rs_sta->win[low].average_tpt;
  596. if (high != RATE_INVALID)
  597. high_tpt = rs_sta->win[high].average_tpt;
  598. spin_unlock_irqrestore(&rs_sta->lock, flags);
  599. scale_action = 0;
  600. /* Low success ratio , need to drop the rate */
  601. if (win->success_ratio < RATE_DECREASE_TH || !current_tpt) {
  602. D_RATE("decrease rate because of low success_ratio\n");
  603. scale_action = -1;
  604. /* No throughput measured yet for adjacent rates,
  605. * try increase */
  606. } else if (low_tpt == IL_INVALID_VALUE &&
  607. high_tpt == IL_INVALID_VALUE) {
  608. if (high != RATE_INVALID && win->success_ratio >= RATE_INCREASE_TH)
  609. scale_action = 1;
  610. else if (low != RATE_INVALID)
  611. scale_action = 0;
  612. /* Both adjacent throughputs are measured, but neither one has
  613. * better throughput; we're using the best rate, don't change
  614. * it! */
  615. } else if (low_tpt != IL_INVALID_VALUE &&
  616. high_tpt != IL_INVALID_VALUE &&
  617. low_tpt < current_tpt && high_tpt < current_tpt) {
  618. D_RATE("No action -- low [%d] & high [%d] < "
  619. "current_tpt [%d]\n",
  620. low_tpt, high_tpt, current_tpt);
  621. scale_action = 0;
  622. /* At least one of the rates has better throughput */
  623. } else {
  624. if (high_tpt != IL_INVALID_VALUE) {
  625. /* High rate has better throughput, Increase
  626. * rate */
  627. if (high_tpt > current_tpt &&
  628. win->success_ratio >= RATE_INCREASE_TH)
  629. scale_action = 1;
  630. else {
  631. D_RATE(
  632. "decrease rate because of high tpt\n");
  633. scale_action = 0;
  634. }
  635. } else if (low_tpt != IL_INVALID_VALUE) {
  636. if (low_tpt > current_tpt) {
  637. D_RATE(
  638. "decrease rate because of low tpt\n");
  639. scale_action = -1;
  640. } else if (win->success_ratio >= RATE_INCREASE_TH) {
  641. /* Lower rate has better
  642. * throughput,decrease rate */
  643. scale_action = 1;
  644. }
  645. }
  646. }
  647. /* Sanity check; asked for decrease, but success rate or throughput
  648. * has been good at old rate. Don't change it. */
  649. if (scale_action == -1 && low != RATE_INVALID &&
  650. (win->success_ratio > RATE_HIGH_TH ||
  651. current_tpt > 100 * rs_sta->expected_tpt[low]))
  652. scale_action = 0;
  653. switch (scale_action) {
  654. case -1:
  655. /* Decrese rate */
  656. if (low != RATE_INVALID)
  657. idx = low;
  658. break;
  659. case 1:
  660. /* Increase rate */
  661. if (high != RATE_INVALID)
  662. idx = high;
  663. break;
  664. case 0:
  665. default:
  666. /* No change */
  667. break;
  668. }
  669. D_RATE("Selected %d (action %d) - low %d high %d\n",
  670. idx, scale_action, low, high);
  671. out:
  672. if (sband->band == IEEE80211_BAND_5GHZ) {
  673. if (WARN_ON_ONCE(idx < IL_FIRST_OFDM_RATE))
  674. idx = IL_FIRST_OFDM_RATE;
  675. rs_sta->last_txrate_idx = idx;
  676. info->control.rates[0].idx = idx - IL_FIRST_OFDM_RATE;
  677. } else {
  678. rs_sta->last_txrate_idx = idx;
  679. info->control.rates[0].idx = rs_sta->last_txrate_idx;
  680. }
  681. D_RATE("leave: %d\n", idx);
  682. }
  683. #ifdef CONFIG_MAC80211_DEBUGFS
  684. static int il3945_open_file_generic(struct inode *inode, struct file *file)
  685. {
  686. file->private_data = inode->i_private;
  687. return 0;
  688. }
  689. static ssize_t il3945_sta_dbgfs_stats_table_read(struct file *file,
  690. char __user *user_buf,
  691. size_t count, loff_t *ppos)
  692. {
  693. char *buff;
  694. int desc = 0;
  695. int j;
  696. ssize_t ret;
  697. struct il3945_rs_sta *lq_sta = file->private_data;
  698. buff = kmalloc(1024, GFP_KERNEL);
  699. if (!buff)
  700. return -ENOMEM;
  701. desc += sprintf(buff + desc, "tx packets=%d last rate idx=%d\n"
  702. "rate=0x%X flush time %d\n",
  703. lq_sta->tx_packets,
  704. lq_sta->last_txrate_idx,
  705. lq_sta->start_rate, jiffies_to_msecs(lq_sta->flush_time));
  706. for (j = 0; j < RATE_COUNT_3945; j++) {
  707. desc += sprintf(buff+desc,
  708. "counter=%d success=%d %%=%d\n",
  709. lq_sta->win[j].counter,
  710. lq_sta->win[j].success_counter,
  711. lq_sta->win[j].success_ratio);
  712. }
  713. ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
  714. kfree(buff);
  715. return ret;
  716. }
  717. static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
  718. .read = il3945_sta_dbgfs_stats_table_read,
  719. .open = il3945_open_file_generic,
  720. .llseek = default_llseek,
  721. };
  722. static void il3945_add_debugfs(void *il, void *il_sta,
  723. struct dentry *dir)
  724. {
  725. struct il3945_rs_sta *lq_sta = il_sta;
  726. lq_sta->rs_sta_dbgfs_stats_table_file =
  727. debugfs_create_file("rate_stats_table", 0600, dir,
  728. lq_sta, &rs_sta_dbgfs_stats_table_ops);
  729. }
  730. static void il3945_remove_debugfs(void *il, void *il_sta)
  731. {
  732. struct il3945_rs_sta *lq_sta = il_sta;
  733. debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
  734. }
  735. #endif
  736. /*
  737. * Initialization of rate scaling information is done by driver after
  738. * the station is added. Since mac80211 calls this function before a
  739. * station is added we ignore it.
  740. */
  741. static void il3945_rs_rate_init_stub(void *il_r,
  742. struct ieee80211_supported_band *sband,
  743. struct ieee80211_sta *sta, void *il_sta)
  744. {
  745. }
  746. static struct rate_control_ops rs_ops = {
  747. .module = NULL,
  748. .name = RS_NAME,
  749. .tx_status = il3945_rs_tx_status,
  750. .get_rate = il3945_rs_get_rate,
  751. .rate_init = il3945_rs_rate_init_stub,
  752. .alloc = il3945_rs_alloc,
  753. .free = il3945_rs_free,
  754. .alloc_sta = il3945_rs_alloc_sta,
  755. .free_sta = il3945_rs_free_sta,
  756. #ifdef CONFIG_MAC80211_DEBUGFS
  757. .add_sta_debugfs = il3945_add_debugfs,
  758. .remove_sta_debugfs = il3945_remove_debugfs,
  759. #endif
  760. };
  761. void il3945_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
  762. {
  763. struct il_priv *il = hw->priv;
  764. s32 rssi = 0;
  765. unsigned long flags;
  766. struct il3945_rs_sta *rs_sta;
  767. struct ieee80211_sta *sta;
  768. struct il3945_sta_priv *psta;
  769. D_RATE("enter\n");
  770. rcu_read_lock();
  771. sta = ieee80211_find_sta(il->ctx.vif,
  772. il->stations[sta_id].sta.sta.addr);
  773. if (!sta) {
  774. D_RATE("Unable to find station to initialize rate scaling.\n");
  775. rcu_read_unlock();
  776. return;
  777. }
  778. psta = (void *) sta->drv_priv;
  779. rs_sta = &psta->rs_sta;
  780. spin_lock_irqsave(&rs_sta->lock, flags);
  781. rs_sta->tgg = 0;
  782. switch (il->band) {
  783. case IEEE80211_BAND_2GHZ:
  784. /* TODO: this always does G, not a regression */
  785. if (il->ctx.active.flags &
  786. RXON_FLG_TGG_PROTECT_MSK) {
  787. rs_sta->tgg = 1;
  788. rs_sta->expected_tpt = il3945_expected_tpt_g_prot;
  789. } else
  790. rs_sta->expected_tpt = il3945_expected_tpt_g;
  791. break;
  792. case IEEE80211_BAND_5GHZ:
  793. rs_sta->expected_tpt = il3945_expected_tpt_a;
  794. break;
  795. case IEEE80211_NUM_BANDS:
  796. BUG();
  797. break;
  798. }
  799. spin_unlock_irqrestore(&rs_sta->lock, flags);
  800. rssi = il->_3945.last_rx_rssi;
  801. if (rssi == 0)
  802. rssi = IL_MIN_RSSI_VAL;
  803. D_RATE("Network RSSI: %d\n", rssi);
  804. rs_sta->start_rate = il3945_get_rate_idx_by_rssi(rssi, il->band);
  805. D_RATE("leave: rssi %d assign rate idx: "
  806. "%d (plcp 0x%x)\n", rssi, rs_sta->start_rate,
  807. il3945_rates[rs_sta->start_rate].plcp);
  808. rcu_read_unlock();
  809. }
  810. int il3945_rate_control_register(void)
  811. {
  812. return ieee80211_rate_control_register(&rs_ops);
  813. }
  814. void il3945_rate_control_unregister(void)
  815. {
  816. ieee80211_rate_control_unregister(&rs_ops);
  817. }