iwl-3945-rs.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2005 - 2009 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/wireless.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 "iwl-commands.h"
  36. #include "iwl-3945.h"
  37. #define RS_NAME "iwl-3945-rs"
  38. struct iwl3945_rate_scale_data {
  39. u64 data;
  40. s32 success_counter;
  41. s32 success_ratio;
  42. s32 counter;
  43. s32 average_tpt;
  44. unsigned long stamp;
  45. };
  46. struct iwl3945_rs_sta {
  47. spinlock_t lock;
  48. struct iwl_priv *priv;
  49. s32 *expected_tpt;
  50. unsigned long last_partial_flush;
  51. unsigned long last_flush;
  52. u32 flush_time;
  53. u32 last_tx_packets;
  54. u32 tx_packets;
  55. u8 tgg;
  56. u8 flush_pending;
  57. u8 start_rate;
  58. u8 ibss_sta_added;
  59. struct timer_list rate_scale_flush;
  60. struct iwl3945_rate_scale_data win[IWL_RATE_COUNT_3945];
  61. #ifdef CONFIG_MAC80211_DEBUGFS
  62. struct dentry *rs_sta_dbgfs_stats_table_file;
  63. #endif
  64. /* used to be in sta_info */
  65. int last_txrate_idx;
  66. };
  67. static s32 iwl3945_expected_tpt_g[IWL_RATE_COUNT_3945] = {
  68. 7, 13, 35, 58, 0, 0, 76, 104, 130, 168, 191, 202
  69. };
  70. static s32 iwl3945_expected_tpt_g_prot[IWL_RATE_COUNT_3945] = {
  71. 7, 13, 35, 58, 0, 0, 0, 80, 93, 113, 123, 125
  72. };
  73. static s32 iwl3945_expected_tpt_a[IWL_RATE_COUNT_3945] = {
  74. 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186
  75. };
  76. static s32 iwl3945_expected_tpt_b[IWL_RATE_COUNT_3945] = {
  77. 7, 13, 35, 58, 0, 0, 0, 0, 0, 0, 0, 0
  78. };
  79. struct iwl3945_tpt_entry {
  80. s8 min_rssi;
  81. u8 index;
  82. };
  83. static struct iwl3945_tpt_entry iwl3945_tpt_table_a[] = {
  84. {-60, IWL_RATE_54M_INDEX},
  85. {-64, IWL_RATE_48M_INDEX},
  86. {-72, IWL_RATE_36M_INDEX},
  87. {-80, IWL_RATE_24M_INDEX},
  88. {-84, IWL_RATE_18M_INDEX},
  89. {-85, IWL_RATE_12M_INDEX},
  90. {-87, IWL_RATE_9M_INDEX},
  91. {-89, IWL_RATE_6M_INDEX}
  92. };
  93. static struct iwl3945_tpt_entry iwl3945_tpt_table_g[] = {
  94. {-60, IWL_RATE_54M_INDEX},
  95. {-64, IWL_RATE_48M_INDEX},
  96. {-68, IWL_RATE_36M_INDEX},
  97. {-80, IWL_RATE_24M_INDEX},
  98. {-84, IWL_RATE_18M_INDEX},
  99. {-85, IWL_RATE_12M_INDEX},
  100. {-86, IWL_RATE_11M_INDEX},
  101. {-88, IWL_RATE_5M_INDEX},
  102. {-90, IWL_RATE_2M_INDEX},
  103. {-92, IWL_RATE_1M_INDEX}
  104. };
  105. #define IWL_RATE_MAX_WINDOW 62
  106. #define IWL_RATE_FLUSH (3*HZ)
  107. #define IWL_RATE_WIN_FLUSH (HZ/2)
  108. #define IWL39_RATE_HIGH_TH 11520
  109. #define IWL_SUCCESS_UP_TH 8960
  110. #define IWL_SUCCESS_DOWN_TH 10880
  111. #define IWL_RATE_MIN_FAILURE_TH 6
  112. #define IWL_RATE_MIN_SUCCESS_TH 8
  113. #define IWL_RATE_DECREASE_TH 1920
  114. #define IWL_RATE_RETRY_TH 15
  115. static u8 iwl3945_get_rate_index_by_rssi(s32 rssi, enum ieee80211_band band)
  116. {
  117. u32 index = 0;
  118. u32 table_size = 0;
  119. struct iwl3945_tpt_entry *tpt_table = NULL;
  120. if ((rssi < IWL_MIN_RSSI_VAL) || (rssi > IWL_MAX_RSSI_VAL))
  121. rssi = IWL_MIN_RSSI_VAL;
  122. switch (band) {
  123. case IEEE80211_BAND_2GHZ:
  124. tpt_table = iwl3945_tpt_table_g;
  125. table_size = ARRAY_SIZE(iwl3945_tpt_table_g);
  126. break;
  127. case IEEE80211_BAND_5GHZ:
  128. tpt_table = iwl3945_tpt_table_a;
  129. table_size = ARRAY_SIZE(iwl3945_tpt_table_a);
  130. break;
  131. default:
  132. BUG();
  133. break;
  134. }
  135. while ((index < table_size) && (rssi < tpt_table[index].min_rssi))
  136. index++;
  137. index = min(index, (table_size - 1));
  138. return tpt_table[index].index;
  139. }
  140. static void iwl3945_clear_window(struct iwl3945_rate_scale_data *window)
  141. {
  142. window->data = 0;
  143. window->success_counter = 0;
  144. window->success_ratio = -1;
  145. window->counter = 0;
  146. window->average_tpt = IWL_INVALID_VALUE;
  147. window->stamp = 0;
  148. }
  149. /**
  150. * iwl3945_rate_scale_flush_windows - flush out the rate scale windows
  151. *
  152. * Returns the number of windows that have gathered data but were
  153. * not flushed. If there were any that were not flushed, then
  154. * reschedule the rate flushing routine.
  155. */
  156. static int iwl3945_rate_scale_flush_windows(struct iwl3945_rs_sta *rs_sta)
  157. {
  158. int unflushed = 0;
  159. int i;
  160. unsigned long flags;
  161. struct iwl_priv *priv __maybe_unused = rs_sta->priv;
  162. /*
  163. * For each rate, if we have collected data on that rate
  164. * and it has been more than IWL_RATE_WIN_FLUSH
  165. * since we flushed, clear out the gathered statistics
  166. */
  167. for (i = 0; i < IWL_RATE_COUNT_3945; i++) {
  168. if (!rs_sta->win[i].counter)
  169. continue;
  170. spin_lock_irqsave(&rs_sta->lock, flags);
  171. if (time_after(jiffies, rs_sta->win[i].stamp +
  172. IWL_RATE_WIN_FLUSH)) {
  173. IWL_DEBUG_RATE(priv, "flushing %d samples of rate "
  174. "index %d\n",
  175. rs_sta->win[i].counter, i);
  176. iwl3945_clear_window(&rs_sta->win[i]);
  177. } else
  178. unflushed++;
  179. spin_unlock_irqrestore(&rs_sta->lock, flags);
  180. }
  181. return unflushed;
  182. }
  183. #define IWL_RATE_FLUSH_MAX 5000 /* msec */
  184. #define IWL_RATE_FLUSH_MIN 50 /* msec */
  185. #define IWL_AVERAGE_PACKETS 1500
  186. static void iwl3945_bg_rate_scale_flush(unsigned long data)
  187. {
  188. struct iwl3945_rs_sta *rs_sta = (void *)data;
  189. struct iwl_priv *priv __maybe_unused = rs_sta->priv;
  190. int unflushed = 0;
  191. unsigned long flags;
  192. u32 packet_count, duration, pps;
  193. IWL_DEBUG_RATE(priv, "enter\n");
  194. unflushed = iwl3945_rate_scale_flush_windows(rs_sta);
  195. spin_lock_irqsave(&rs_sta->lock, flags);
  196. /* Number of packets Rx'd since last time this timer ran */
  197. packet_count = (rs_sta->tx_packets - rs_sta->last_tx_packets) + 1;
  198. rs_sta->last_tx_packets = rs_sta->tx_packets + 1;
  199. if (unflushed) {
  200. duration =
  201. jiffies_to_msecs(jiffies - rs_sta->last_partial_flush);
  202. IWL_DEBUG_RATE(priv, "Tx'd %d packets in %dms\n",
  203. packet_count, duration);
  204. /* Determine packets per second */
  205. if (duration)
  206. pps = (packet_count * 1000) / duration;
  207. else
  208. pps = 0;
  209. if (pps) {
  210. duration = (IWL_AVERAGE_PACKETS * 1000) / pps;
  211. if (duration < IWL_RATE_FLUSH_MIN)
  212. duration = IWL_RATE_FLUSH_MIN;
  213. else if (duration > IWL_RATE_FLUSH_MAX)
  214. duration = IWL_RATE_FLUSH_MAX;
  215. } else
  216. duration = IWL_RATE_FLUSH_MAX;
  217. rs_sta->flush_time = msecs_to_jiffies(duration);
  218. IWL_DEBUG_RATE(priv, "new flush period: %d msec ave %d\n",
  219. duration, packet_count);
  220. mod_timer(&rs_sta->rate_scale_flush, jiffies +
  221. rs_sta->flush_time);
  222. rs_sta->last_partial_flush = jiffies;
  223. } else {
  224. rs_sta->flush_time = IWL_RATE_FLUSH;
  225. rs_sta->flush_pending = 0;
  226. }
  227. /* If there weren't any unflushed entries, we don't schedule the timer
  228. * to run again */
  229. rs_sta->last_flush = jiffies;
  230. spin_unlock_irqrestore(&rs_sta->lock, flags);
  231. IWL_DEBUG_RATE(priv, "leave\n");
  232. }
  233. /**
  234. * iwl3945_collect_tx_data - Update the success/failure sliding window
  235. *
  236. * We keep a sliding window of the last 64 packets transmitted
  237. * at this rate. window->data contains the bitmask of successful
  238. * packets.
  239. */
  240. static void iwl3945_collect_tx_data(struct iwl3945_rs_sta *rs_sta,
  241. struct iwl3945_rate_scale_data *window,
  242. int success, int retries, int index)
  243. {
  244. unsigned long flags;
  245. s32 fail_count;
  246. struct iwl_priv *priv __maybe_unused = rs_sta->priv;
  247. if (!retries) {
  248. IWL_DEBUG_RATE(priv, "leave: retries == 0 -- should be at least 1\n");
  249. return;
  250. }
  251. spin_lock_irqsave(&rs_sta->lock, flags);
  252. /*
  253. * Keep track of only the latest 62 tx frame attempts in this rate's
  254. * history window; anything older isn't really relevant any more.
  255. * If we have filled up the sliding window, drop the oldest attempt;
  256. * if the oldest attempt (highest bit in bitmap) shows "success",
  257. * subtract "1" from the success counter (this is the main reason
  258. * we keep these bitmaps!).
  259. * */
  260. while (retries > 0) {
  261. if (window->counter >= IWL_RATE_MAX_WINDOW) {
  262. /* remove earliest */
  263. window->counter = IWL_RATE_MAX_WINDOW - 1;
  264. if (window->data & (1ULL << (IWL_RATE_MAX_WINDOW - 1))) {
  265. window->data &= ~(1ULL << (IWL_RATE_MAX_WINDOW - 1));
  266. window->success_counter--;
  267. }
  268. }
  269. /* Increment frames-attempted counter */
  270. window->counter++;
  271. /* Shift bitmap by one frame (throw away oldest history),
  272. * OR in "1", and increment "success" if this
  273. * frame was successful. */
  274. window->data <<= 1;
  275. if (success > 0) {
  276. window->success_counter++;
  277. window->data |= 0x1;
  278. success--;
  279. }
  280. retries--;
  281. }
  282. /* Calculate current success ratio, avoid divide-by-0! */
  283. if (window->counter > 0)
  284. window->success_ratio = 128 * (100 * window->success_counter)
  285. / window->counter;
  286. else
  287. window->success_ratio = IWL_INVALID_VALUE;
  288. fail_count = window->counter - window->success_counter;
  289. /* Calculate average throughput, if we have enough history. */
  290. if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
  291. (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
  292. window->average_tpt = ((window->success_ratio *
  293. rs_sta->expected_tpt[index] + 64) / 128);
  294. else
  295. window->average_tpt = IWL_INVALID_VALUE;
  296. /* Tag this window as having been updated */
  297. window->stamp = jiffies;
  298. spin_unlock_irqrestore(&rs_sta->lock, flags);
  299. }
  300. static void rs_rate_init(void *priv_r, struct ieee80211_supported_band *sband,
  301. struct ieee80211_sta *sta, void *priv_sta)
  302. {
  303. struct iwl3945_rs_sta *rs_sta = priv_sta;
  304. struct iwl_priv *priv = (struct iwl_priv *)priv_r;
  305. int i;
  306. IWL_DEBUG_RATE(priv, "enter\n");
  307. /* TODO: what is a good starting rate for STA? About middle? Maybe not
  308. * the lowest or the highest rate.. Could consider using RSSI from
  309. * previous packets? Need to have IEEE 802.1X auth succeed immediately
  310. * after assoc.. */
  311. for (i = sband->n_bitrates - 1; i >= 0; i--) {
  312. if (sta->supp_rates[sband->band] & (1 << i)) {
  313. rs_sta->last_txrate_idx = i;
  314. break;
  315. }
  316. }
  317. priv->sta_supp_rates = sta->supp_rates[sband->band];
  318. /* For 5 GHz band it start at IWL_FIRST_OFDM_RATE */
  319. if (sband->band == IEEE80211_BAND_5GHZ) {
  320. rs_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
  321. priv->sta_supp_rates = priv->sta_supp_rates <<
  322. IWL_FIRST_OFDM_RATE;
  323. }
  324. IWL_DEBUG_RATE(priv, "leave\n");
  325. }
  326. static void *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 rs_free(void *priv)
  332. {
  333. return;
  334. }
  335. static void *rs_alloc_sta(void *iwl_priv, struct ieee80211_sta *sta, gfp_t gfp)
  336. {
  337. struct iwl3945_rs_sta *rs_sta;
  338. struct iwl3945_sta_priv *psta = (void *) sta->drv_priv;
  339. struct iwl_priv *priv = iwl_priv;
  340. int i;
  341. /*
  342. * XXX: If it's using sta->drv_priv anyway, it might
  343. * as well just put all the information there.
  344. */
  345. IWL_DEBUG_RATE(priv, "enter\n");
  346. rs_sta = kzalloc(sizeof(struct iwl3945_rs_sta), gfp);
  347. if (!rs_sta) {
  348. IWL_DEBUG_RATE(priv, "leave: ENOMEM\n");
  349. return NULL;
  350. }
  351. psta->rs_sta = rs_sta;
  352. spin_lock_init(&rs_sta->lock);
  353. rs_sta->priv = priv;
  354. rs_sta->start_rate = IWL_RATE_INVALID;
  355. /* default to just 802.11b */
  356. rs_sta->expected_tpt = iwl3945_expected_tpt_b;
  357. rs_sta->last_partial_flush = jiffies;
  358. rs_sta->last_flush = jiffies;
  359. rs_sta->flush_time = IWL_RATE_FLUSH;
  360. rs_sta->last_tx_packets = 0;
  361. rs_sta->ibss_sta_added = 0;
  362. init_timer(&rs_sta->rate_scale_flush);
  363. rs_sta->rate_scale_flush.data = (unsigned long)rs_sta;
  364. rs_sta->rate_scale_flush.function = &iwl3945_bg_rate_scale_flush;
  365. for (i = 0; i < IWL_RATE_COUNT_3945; i++)
  366. iwl3945_clear_window(&rs_sta->win[i]);
  367. IWL_DEBUG_RATE(priv, "leave\n");
  368. return rs_sta;
  369. }
  370. static void rs_free_sta(void *iwl_priv, struct ieee80211_sta *sta,
  371. void *priv_sta)
  372. {
  373. struct iwl3945_sta_priv *psta = (void *) sta->drv_priv;
  374. struct iwl3945_rs_sta *rs_sta = priv_sta;
  375. struct iwl_priv *priv __maybe_unused = rs_sta->priv;
  376. psta->rs_sta = NULL;
  377. IWL_DEBUG_RATE(priv, "enter\n");
  378. del_timer_sync(&rs_sta->rate_scale_flush);
  379. kfree(rs_sta);
  380. IWL_DEBUG_RATE(priv, "leave\n");
  381. }
  382. /**
  383. * rs_tx_status - Update rate control values based on Tx results
  384. *
  385. * NOTE: Uses iwl_priv->retry_rate for the # of retries attempted by
  386. * the hardware for each rate.
  387. */
  388. static void rs_tx_status(void *priv_rate, struct ieee80211_supported_band *sband,
  389. struct ieee80211_sta *sta, void *priv_sta,
  390. struct sk_buff *skb)
  391. {
  392. s8 retries = 0, current_count;
  393. int scale_rate_index, first_index, last_index;
  394. unsigned long flags;
  395. struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
  396. struct iwl3945_rs_sta *rs_sta = priv_sta;
  397. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  398. IWL_DEBUG_RATE(priv, "enter\n");
  399. retries = info->status.rates[0].count;
  400. /* Sanity Check for retries */
  401. if (retries > IWL_RATE_RETRY_TH)
  402. retries = IWL_RATE_RETRY_TH;
  403. first_index = sband->bitrates[info->status.rates[0].idx].hw_value;
  404. if ((first_index < 0) || (first_index >= IWL_RATE_COUNT_3945)) {
  405. IWL_DEBUG_RATE(priv, "leave: Rate out of bounds: %d\n", first_index);
  406. return;
  407. }
  408. if (!priv_sta) {
  409. IWL_DEBUG_RATE(priv, "leave: No STA priv data to update!\n");
  410. return;
  411. }
  412. rs_sta->tx_packets++;
  413. scale_rate_index = first_index;
  414. last_index = first_index;
  415. /*
  416. * Update the window for each rate. We determine which rates
  417. * were Tx'd based on the total number of retries vs. the number
  418. * of retries configured for each rate -- currently set to the
  419. * priv value 'retry_rate' vs. rate specific
  420. *
  421. * On exit from this while loop last_index indicates the rate
  422. * at which the frame was finally transmitted (or failed if no
  423. * ACK)
  424. */
  425. while (retries > 1) {
  426. if ((retries - 1) < priv->retry_rate) {
  427. current_count = (retries - 1);
  428. last_index = scale_rate_index;
  429. } else {
  430. current_count = priv->retry_rate;
  431. last_index = iwl3945_rs_next_rate(priv,
  432. scale_rate_index);
  433. }
  434. /* Update this rate accounting for as many retries
  435. * as was used for it (per current_count) */
  436. iwl3945_collect_tx_data(rs_sta,
  437. &rs_sta->win[scale_rate_index],
  438. 0, current_count, scale_rate_index);
  439. IWL_DEBUG_RATE(priv, "Update rate %d for %d retries.\n",
  440. scale_rate_index, current_count);
  441. retries -= current_count;
  442. scale_rate_index = last_index;
  443. }
  444. /* Update the last index window with success/failure based on ACK */
  445. IWL_DEBUG_RATE(priv, "Update rate %d with %s.\n",
  446. last_index,
  447. (info->flags & IEEE80211_TX_STAT_ACK) ?
  448. "success" : "failure");
  449. iwl3945_collect_tx_data(rs_sta,
  450. &rs_sta->win[last_index],
  451. info->flags & IEEE80211_TX_STAT_ACK, 1, last_index);
  452. /* We updated the rate scale window -- if its been more than
  453. * flush_time since the last run, schedule the flush
  454. * again */
  455. spin_lock_irqsave(&rs_sta->lock, flags);
  456. if (!rs_sta->flush_pending &&
  457. time_after(jiffies, rs_sta->last_flush +
  458. rs_sta->flush_time)) {
  459. rs_sta->last_partial_flush = jiffies;
  460. rs_sta->flush_pending = 1;
  461. mod_timer(&rs_sta->rate_scale_flush,
  462. jiffies + rs_sta->flush_time);
  463. }
  464. spin_unlock_irqrestore(&rs_sta->lock, flags);
  465. IWL_DEBUG_RATE(priv, "leave\n");
  466. return;
  467. }
  468. static u16 iwl3945_get_adjacent_rate(struct iwl3945_rs_sta *rs_sta,
  469. u8 index, u16 rate_mask, enum ieee80211_band band)
  470. {
  471. u8 high = IWL_RATE_INVALID;
  472. u8 low = IWL_RATE_INVALID;
  473. struct iwl_priv *priv __maybe_unused = rs_sta->priv;
  474. /* 802.11A walks to the next literal adjacent rate in
  475. * the rate table */
  476. if (unlikely(band == IEEE80211_BAND_5GHZ)) {
  477. int i;
  478. u32 mask;
  479. /* Find the previous rate that is in the rate mask */
  480. i = index - 1;
  481. for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
  482. if (rate_mask & mask) {
  483. low = i;
  484. break;
  485. }
  486. }
  487. /* Find the next rate that is in the rate mask */
  488. i = index + 1;
  489. for (mask = (1 << i); i < IWL_RATE_COUNT_3945;
  490. i++, mask <<= 1) {
  491. if (rate_mask & mask) {
  492. high = i;
  493. break;
  494. }
  495. }
  496. return (high << 8) | low;
  497. }
  498. low = index;
  499. while (low != IWL_RATE_INVALID) {
  500. if (rs_sta->tgg)
  501. low = iwl3945_rates[low].prev_rs_tgg;
  502. else
  503. low = iwl3945_rates[low].prev_rs;
  504. if (low == IWL_RATE_INVALID)
  505. break;
  506. if (rate_mask & (1 << low))
  507. break;
  508. IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
  509. }
  510. high = index;
  511. while (high != IWL_RATE_INVALID) {
  512. if (rs_sta->tgg)
  513. high = iwl3945_rates[high].next_rs_tgg;
  514. else
  515. high = iwl3945_rates[high].next_rs;
  516. if (high == IWL_RATE_INVALID)
  517. break;
  518. if (rate_mask & (1 << high))
  519. break;
  520. IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
  521. }
  522. return (high << 8) | low;
  523. }
  524. /**
  525. * rs_get_rate - find the rate for the requested packet
  526. *
  527. * Returns the ieee80211_rate structure allocated by the driver.
  528. *
  529. * The rate control algorithm has no internal mapping between hw_mode's
  530. * rate ordering and the rate ordering used by the rate control algorithm.
  531. *
  532. * The rate control algorithm uses a single table of rates that goes across
  533. * the entire A/B/G spectrum vs. being limited to just one particular
  534. * hw_mode.
  535. *
  536. * As such, we can't convert the index obtained below into the hw_mode's
  537. * rate table and must reference the driver allocated rate table
  538. *
  539. */
  540. static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta,
  541. void *priv_sta, struct ieee80211_tx_rate_control *txrc)
  542. {
  543. struct ieee80211_supported_band *sband = txrc->sband;
  544. struct sk_buff *skb = txrc->skb;
  545. u8 low = IWL_RATE_INVALID;
  546. u8 high = IWL_RATE_INVALID;
  547. u16 high_low;
  548. int index;
  549. struct iwl3945_rs_sta *rs_sta = priv_sta;
  550. struct iwl3945_rate_scale_data *window = NULL;
  551. int current_tpt = IWL_INVALID_VALUE;
  552. int low_tpt = IWL_INVALID_VALUE;
  553. int high_tpt = IWL_INVALID_VALUE;
  554. u32 fail_count;
  555. s8 scale_action = 0;
  556. unsigned long flags;
  557. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  558. u16 fc;
  559. u16 rate_mask = 0;
  560. s8 max_rate_idx = -1;
  561. struct iwl_priv *priv = (struct iwl_priv *)priv_r;
  562. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  563. IWL_DEBUG_RATE(priv, "enter\n");
  564. if (sta)
  565. rate_mask = sta->supp_rates[sband->band];
  566. /* Send management frames and broadcast/multicast data using lowest
  567. * rate. */
  568. fc = le16_to_cpu(hdr->frame_control);
  569. if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
  570. is_multicast_ether_addr(hdr->addr1) ||
  571. !sta || !priv_sta) {
  572. IWL_DEBUG_RATE(priv, "leave: No STA priv data to update!\n");
  573. if (!rate_mask)
  574. info->control.rates[0].idx =
  575. rate_lowest_index(sband, NULL);
  576. else
  577. info->control.rates[0].idx =
  578. rate_lowest_index(sband, sta);
  579. return;
  580. }
  581. /* get user max rate if set */
  582. max_rate_idx = txrc->max_rate_idx;
  583. if ((sband->band == IEEE80211_BAND_5GHZ) && (max_rate_idx != -1))
  584. max_rate_idx += IWL_FIRST_OFDM_RATE;
  585. if ((max_rate_idx < 0) || (max_rate_idx >= IWL_RATE_COUNT))
  586. max_rate_idx = -1;
  587. index = min(rs_sta->last_txrate_idx & 0xffff, IWL_RATE_COUNT_3945 - 1);
  588. if (sband->band == IEEE80211_BAND_5GHZ)
  589. rate_mask = rate_mask << IWL_FIRST_OFDM_RATE;
  590. if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
  591. !rs_sta->ibss_sta_added) {
  592. u8 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
  593. if (sta_id == IWL_INVALID_STATION) {
  594. IWL_DEBUG_RATE(priv, "LQ: ADD station %pm\n",
  595. hdr->addr1);
  596. sta_id = iwl3945_add_station(priv,
  597. hdr->addr1, 0, CMD_ASYNC);
  598. }
  599. if (sta_id != IWL_INVALID_STATION)
  600. rs_sta->ibss_sta_added = 1;
  601. }
  602. spin_lock_irqsave(&rs_sta->lock, flags);
  603. /* for recent assoc, choose best rate regarding
  604. * to rssi value
  605. */
  606. if (rs_sta->start_rate != IWL_RATE_INVALID) {
  607. if (rs_sta->start_rate < index &&
  608. (rate_mask & (1 << rs_sta->start_rate)))
  609. index = rs_sta->start_rate;
  610. rs_sta->start_rate = IWL_RATE_INVALID;
  611. }
  612. /* force user max rate if set by user */
  613. if ((max_rate_idx != -1) && (max_rate_idx < index)) {
  614. if (rate_mask & (1 << max_rate_idx))
  615. index = max_rate_idx;
  616. }
  617. window = &(rs_sta->win[index]);
  618. fail_count = window->counter - window->success_counter;
  619. if (((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
  620. (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))) {
  621. spin_unlock_irqrestore(&rs_sta->lock, flags);
  622. IWL_DEBUG_RATE(priv, "Invalid average_tpt on rate %d: "
  623. "counter: %d, success_counter: %d, "
  624. "expected_tpt is %sNULL\n",
  625. index,
  626. window->counter,
  627. window->success_counter,
  628. rs_sta->expected_tpt ? "not " : "");
  629. /* Can't calculate this yet; not enough history */
  630. window->average_tpt = IWL_INVALID_VALUE;
  631. goto out;
  632. }
  633. current_tpt = window->average_tpt;
  634. high_low = iwl3945_get_adjacent_rate(rs_sta, index, rate_mask,
  635. sband->band);
  636. low = high_low & 0xff;
  637. high = (high_low >> 8) & 0xff;
  638. /* If user set max rate, dont allow higher than user constrain */
  639. if ((max_rate_idx != -1) && (max_rate_idx < high))
  640. high = IWL_RATE_INVALID;
  641. /* Collect Measured throughputs of adjacent rates */
  642. if (low != IWL_RATE_INVALID)
  643. low_tpt = rs_sta->win[low].average_tpt;
  644. if (high != IWL_RATE_INVALID)
  645. high_tpt = rs_sta->win[high].average_tpt;
  646. spin_unlock_irqrestore(&rs_sta->lock, flags);
  647. scale_action = 0;
  648. /* Low success ratio , need to drop the rate */
  649. if ((window->success_ratio < IWL_RATE_DECREASE_TH) || !current_tpt) {
  650. IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
  651. scale_action = -1;
  652. /* No throughput measured yet for adjacent rates,
  653. * try increase */
  654. } else if ((low_tpt == IWL_INVALID_VALUE) &&
  655. (high_tpt == IWL_INVALID_VALUE)) {
  656. if (high != IWL_RATE_INVALID && window->success_ratio >= IWL_RATE_INCREASE_TH)
  657. scale_action = 1;
  658. else if (low != IWL_RATE_INVALID)
  659. scale_action = 0;
  660. /* Both adjacent throughputs are measured, but neither one has
  661. * better throughput; we're using the best rate, don't change
  662. * it! */
  663. } else if ((low_tpt != IWL_INVALID_VALUE) &&
  664. (high_tpt != IWL_INVALID_VALUE) &&
  665. (low_tpt < current_tpt) && (high_tpt < current_tpt)) {
  666. IWL_DEBUG_RATE(priv, "No action -- low [%d] & high [%d] < "
  667. "current_tpt [%d]\n",
  668. low_tpt, high_tpt, current_tpt);
  669. scale_action = 0;
  670. /* At least one of the rates has better throughput */
  671. } else {
  672. if (high_tpt != IWL_INVALID_VALUE) {
  673. /* High rate has better throughput, Increase
  674. * rate */
  675. if (high_tpt > current_tpt &&
  676. window->success_ratio >= IWL_RATE_INCREASE_TH)
  677. scale_action = 1;
  678. else {
  679. IWL_DEBUG_RATE(priv,
  680. "decrease rate because of high tpt\n");
  681. scale_action = 0;
  682. }
  683. } else if (low_tpt != IWL_INVALID_VALUE) {
  684. if (low_tpt > current_tpt) {
  685. IWL_DEBUG_RATE(priv,
  686. "decrease rate because of low tpt\n");
  687. scale_action = -1;
  688. } else if (window->success_ratio >= IWL_RATE_INCREASE_TH) {
  689. /* Lower rate has better
  690. * throughput,decrease rate */
  691. scale_action = 1;
  692. }
  693. }
  694. }
  695. /* Sanity check; asked for decrease, but success rate or throughput
  696. * has been good at old rate. Don't change it. */
  697. if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
  698. ((window->success_ratio > IWL_RATE_HIGH_TH) ||
  699. (current_tpt > (100 * rs_sta->expected_tpt[low]))))
  700. scale_action = 0;
  701. switch (scale_action) {
  702. case -1:
  703. /* Decrese rate */
  704. if (low != IWL_RATE_INVALID)
  705. index = low;
  706. break;
  707. case 1:
  708. /* Increase rate */
  709. if (high != IWL_RATE_INVALID)
  710. index = high;
  711. break;
  712. case 0:
  713. default:
  714. /* No change */
  715. break;
  716. }
  717. IWL_DEBUG_RATE(priv, "Selected %d (action %d) - low %d high %d\n",
  718. index, scale_action, low, high);
  719. out:
  720. rs_sta->last_txrate_idx = index;
  721. if (sband->band == IEEE80211_BAND_5GHZ)
  722. info->control.rates[0].idx = rs_sta->last_txrate_idx -
  723. IWL_FIRST_OFDM_RATE;
  724. else
  725. info->control.rates[0].idx = rs_sta->last_txrate_idx;
  726. IWL_DEBUG_RATE(priv, "leave: %d\n", index);
  727. }
  728. #ifdef CONFIG_MAC80211_DEBUGFS
  729. static int iwl3945_open_file_generic(struct inode *inode, struct file *file)
  730. {
  731. file->private_data = inode->i_private;
  732. return 0;
  733. }
  734. static ssize_t iwl3945_sta_dbgfs_stats_table_read(struct file *file,
  735. char __user *user_buf,
  736. size_t count, loff_t *ppos)
  737. {
  738. char *buff;
  739. int desc = 0;
  740. int j;
  741. ssize_t ret;
  742. struct iwl3945_rs_sta *lq_sta = file->private_data;
  743. buff = kmalloc(1024, GFP_KERNEL);
  744. if (!buff)
  745. return -ENOMEM;
  746. desc += sprintf(buff + desc, "tx packets=%d last rate index=%d\n"
  747. "rate=0x%X flush time %d\n",
  748. lq_sta->tx_packets,
  749. lq_sta->last_txrate_idx,
  750. lq_sta->start_rate, jiffies_to_msecs(lq_sta->flush_time));
  751. for (j = 0; j < IWL_RATE_COUNT_3945; j++) {
  752. desc += sprintf(buff+desc,
  753. "counter=%d success=%d %%=%d\n",
  754. lq_sta->win[j].counter,
  755. lq_sta->win[j].success_counter,
  756. lq_sta->win[j].success_ratio);
  757. }
  758. ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
  759. kfree(buff);
  760. return ret;
  761. }
  762. static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
  763. .read = iwl3945_sta_dbgfs_stats_table_read,
  764. .open = iwl3945_open_file_generic,
  765. };
  766. static void iwl3945_add_debugfs(void *priv, void *priv_sta,
  767. struct dentry *dir)
  768. {
  769. struct iwl3945_rs_sta *lq_sta = priv_sta;
  770. lq_sta->rs_sta_dbgfs_stats_table_file =
  771. debugfs_create_file("rate_stats_table", 0600, dir,
  772. lq_sta, &rs_sta_dbgfs_stats_table_ops);
  773. }
  774. static void iwl3945_remove_debugfs(void *priv, void *priv_sta)
  775. {
  776. struct iwl3945_rs_sta *lq_sta = priv_sta;
  777. debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
  778. }
  779. #endif
  780. static struct rate_control_ops rs_ops = {
  781. .module = NULL,
  782. .name = RS_NAME,
  783. .tx_status = rs_tx_status,
  784. .get_rate = rs_get_rate,
  785. .rate_init = rs_rate_init,
  786. .alloc = rs_alloc,
  787. .free = rs_free,
  788. .alloc_sta = rs_alloc_sta,
  789. .free_sta = rs_free_sta,
  790. #ifdef CONFIG_MAC80211_DEBUGFS
  791. .add_sta_debugfs = iwl3945_add_debugfs,
  792. .remove_sta_debugfs = iwl3945_remove_debugfs,
  793. #endif
  794. };
  795. void iwl3945_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
  796. {
  797. struct iwl_priv *priv = hw->priv;
  798. s32 rssi = 0;
  799. unsigned long flags;
  800. struct iwl3945_rs_sta *rs_sta;
  801. struct ieee80211_sta *sta;
  802. struct iwl3945_sta_priv *psta;
  803. IWL_DEBUG_RATE(priv, "enter\n");
  804. rcu_read_lock();
  805. sta = ieee80211_find_sta(hw, priv->stations_39[sta_id].sta.sta.addr);
  806. if (!sta) {
  807. rcu_read_unlock();
  808. return;
  809. }
  810. psta = (void *) sta->drv_priv;
  811. rs_sta = psta->rs_sta;
  812. spin_lock_irqsave(&rs_sta->lock, flags);
  813. rs_sta->tgg = 0;
  814. switch (priv->band) {
  815. case IEEE80211_BAND_2GHZ:
  816. /* TODO: this always does G, not a regression */
  817. if (priv->active_rxon.flags & RXON_FLG_TGG_PROTECT_MSK) {
  818. rs_sta->tgg = 1;
  819. rs_sta->expected_tpt = iwl3945_expected_tpt_g_prot;
  820. } else
  821. rs_sta->expected_tpt = iwl3945_expected_tpt_g;
  822. break;
  823. case IEEE80211_BAND_5GHZ:
  824. rs_sta->expected_tpt = iwl3945_expected_tpt_a;
  825. break;
  826. case IEEE80211_NUM_BANDS:
  827. BUG();
  828. break;
  829. }
  830. spin_unlock_irqrestore(&rs_sta->lock, flags);
  831. rssi = priv->last_rx_rssi;
  832. if (rssi == 0)
  833. rssi = IWL_MIN_RSSI_VAL;
  834. IWL_DEBUG_RATE(priv, "Network RSSI: %d\n", rssi);
  835. rs_sta->start_rate = iwl3945_get_rate_index_by_rssi(rssi, priv->band);
  836. IWL_DEBUG_RATE(priv, "leave: rssi %d assign rate index: "
  837. "%d (plcp 0x%x)\n", rssi, rs_sta->start_rate,
  838. iwl3945_rates[rs_sta->start_rate].plcp);
  839. rcu_read_unlock();
  840. }
  841. int iwl3945_rate_control_register(void)
  842. {
  843. return ieee80211_rate_control_register(&rs_ops);
  844. }
  845. void iwl3945_rate_control_unregister(void)
  846. {
  847. ieee80211_rate_control_unregister(&rs_ops);
  848. }