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