3945-rs.c 25 KB

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