iwl-3945-rs.c 24 KB

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