3945-rs.c 25 KB

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