iwl-3945-rs.c 24 KB

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