3945-rs.c 24 KB

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