iwl-3945-rs.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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. * James P. Ketrenos <ipw2100-admin@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, struct ieee80211_supported_band *sband,
  279. struct ieee80211_sta *sta, void *priv_sta)
  280. {
  281. struct iwl3945_rs_sta *rs_sta = priv_sta;
  282. int i;
  283. IWL_DEBUG_RATE("enter\n");
  284. /* TODO: what is a good starting rate for STA? About middle? Maybe not
  285. * the lowest or the highest rate.. Could consider using RSSI from
  286. * previous packets? Need to have IEEE 802.1X auth succeed immediately
  287. * after assoc.. */
  288. for (i = IWL_RATE_COUNT - 1; i >= 0; i--) {
  289. if (sta->supp_rates[sband->band] & (1 << i)) {
  290. rs_sta->last_txrate_idx = i;
  291. break;
  292. }
  293. }
  294. /* For 5 GHz band it start at IWL_FIRST_OFDM_RATE */
  295. if (sband->band == IEEE80211_BAND_5GHZ)
  296. rs_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
  297. IWL_DEBUG_RATE("leave\n");
  298. }
  299. static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
  300. {
  301. return hw->priv;
  302. }
  303. /* rate scale requires free function to be implemented */
  304. static void rs_free(void *priv)
  305. {
  306. return;
  307. }
  308. static void *rs_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
  309. {
  310. struct iwl3945_rs_sta *rs_sta;
  311. struct iwl3945_sta_priv *psta = (void *) sta->drv_priv;
  312. int i;
  313. /*
  314. * XXX: If it's using sta->drv_priv anyway, it might
  315. * as well just put all the information there.
  316. */
  317. IWL_DEBUG_RATE("enter\n");
  318. rs_sta = kzalloc(sizeof(struct iwl3945_rs_sta), gfp);
  319. if (!rs_sta) {
  320. IWL_DEBUG_RATE("leave: ENOMEM\n");
  321. return NULL;
  322. }
  323. psta->rs_sta = rs_sta;
  324. spin_lock_init(&rs_sta->lock);
  325. rs_sta->start_rate = IWL_RATE_INVALID;
  326. /* default to just 802.11b */
  327. rs_sta->expected_tpt = iwl3945_expected_tpt_b;
  328. rs_sta->last_partial_flush = jiffies;
  329. rs_sta->last_flush = jiffies;
  330. rs_sta->flush_time = IWL_RATE_FLUSH;
  331. rs_sta->last_tx_packets = 0;
  332. rs_sta->ibss_sta_added = 0;
  333. init_timer(&rs_sta->rate_scale_flush);
  334. rs_sta->rate_scale_flush.data = (unsigned long)rs_sta;
  335. rs_sta->rate_scale_flush.function = &iwl3945_bg_rate_scale_flush;
  336. for (i = 0; i < IWL_RATE_COUNT; i++)
  337. iwl3945_clear_window(&rs_sta->win[i]);
  338. IWL_DEBUG_RATE("leave\n");
  339. return rs_sta;
  340. }
  341. static void rs_free_sta(void *priv, struct ieee80211_sta *sta,
  342. void *priv_sta)
  343. {
  344. struct iwl3945_sta_priv *psta = (void *) sta->drv_priv;
  345. struct iwl3945_rs_sta *rs_sta = priv_sta;
  346. psta->rs_sta = NULL;
  347. IWL_DEBUG_RATE("enter\n");
  348. del_timer_sync(&rs_sta->rate_scale_flush);
  349. kfree(rs_sta);
  350. IWL_DEBUG_RATE("leave\n");
  351. }
  352. /**
  353. * rs_tx_status - Update rate control values based on Tx results
  354. *
  355. * NOTE: Uses iwl3945_priv->retry_rate for the # of retries attempted by
  356. * the hardware for each rate.
  357. */
  358. static void rs_tx_status(void *priv_rate, struct ieee80211_supported_band *sband,
  359. struct ieee80211_sta *sta, void *priv_sta,
  360. struct sk_buff *skb)
  361. {
  362. s8 retries = 0, current_count;
  363. int scale_rate_index, first_index, last_index;
  364. unsigned long flags;
  365. struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_rate;
  366. struct iwl3945_rs_sta *rs_sta = priv_sta;
  367. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  368. IWL_DEBUG_RATE("enter\n");
  369. retries = info->status.rates[0].count;
  370. first_index = sband->bitrates[info->status.rates[0].idx].hw_value;
  371. if ((first_index < 0) || (first_index >= IWL_RATE_COUNT)) {
  372. IWL_DEBUG_RATE("leave: Rate out of bounds: %d\n", first_index);
  373. return;
  374. }
  375. if (!priv_sta) {
  376. IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
  377. return;
  378. }
  379. rs_sta->tx_packets++;
  380. scale_rate_index = first_index;
  381. last_index = first_index;
  382. /*
  383. * Update the window for each rate. We determine which rates
  384. * were Tx'd based on the total number of retries vs. the number
  385. * of retries configured for each rate -- currently set to the
  386. * priv value 'retry_rate' vs. rate specific
  387. *
  388. * On exit from this while loop last_index indicates the rate
  389. * at which the frame was finally transmitted (or failed if no
  390. * ACK)
  391. */
  392. while (retries > 1) {
  393. if ((retries - 1) < priv->retry_rate) {
  394. current_count = (retries - 1);
  395. last_index = scale_rate_index;
  396. } else {
  397. current_count = priv->retry_rate;
  398. last_index = iwl3945_rs_next_rate(priv,
  399. scale_rate_index);
  400. }
  401. /* Update this rate accounting for as many retries
  402. * as was used for it (per current_count) */
  403. iwl3945_collect_tx_data(rs_sta,
  404. &rs_sta->win[scale_rate_index],
  405. 0, current_count, scale_rate_index);
  406. IWL_DEBUG_RATE("Update rate %d for %d retries.\n",
  407. scale_rate_index, current_count);
  408. retries -= current_count;
  409. scale_rate_index = last_index;
  410. }
  411. /* Update the last index window with success/failure based on ACK */
  412. IWL_DEBUG_RATE("Update rate %d with %s.\n",
  413. last_index,
  414. (info->flags & IEEE80211_TX_STAT_ACK) ?
  415. "success" : "failure");
  416. iwl3945_collect_tx_data(rs_sta,
  417. &rs_sta->win[last_index],
  418. info->flags & IEEE80211_TX_STAT_ACK, 1, last_index);
  419. /* We updated the rate scale window -- if its been more than
  420. * flush_time since the last run, schedule the flush
  421. * again */
  422. spin_lock_irqsave(&rs_sta->lock, flags);
  423. if (!rs_sta->flush_pending &&
  424. time_after(jiffies, rs_sta->last_flush +
  425. rs_sta->flush_time)) {
  426. rs_sta->last_partial_flush = jiffies;
  427. rs_sta->flush_pending = 1;
  428. mod_timer(&rs_sta->rate_scale_flush,
  429. jiffies + rs_sta->flush_time);
  430. }
  431. spin_unlock_irqrestore(&rs_sta->lock, flags);
  432. IWL_DEBUG_RATE("leave\n");
  433. return;
  434. }
  435. static u16 iwl3945_get_adjacent_rate(struct iwl3945_rs_sta *rs_sta,
  436. u8 index, u16 rate_mask, enum ieee80211_band band)
  437. {
  438. u8 high = IWL_RATE_INVALID;
  439. u8 low = IWL_RATE_INVALID;
  440. /* 802.11A walks to the next literal adjacent rate in
  441. * the rate table */
  442. if (unlikely(band == IEEE80211_BAND_5GHZ)) {
  443. int i;
  444. u32 mask;
  445. /* Find the previous rate that is in the rate mask */
  446. i = index - 1;
  447. for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
  448. if (rate_mask & mask) {
  449. low = i;
  450. break;
  451. }
  452. }
  453. /* Find the next rate that is in the rate mask */
  454. i = index + 1;
  455. for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
  456. if (rate_mask & mask) {
  457. high = i;
  458. break;
  459. }
  460. }
  461. return (high << 8) | low;
  462. }
  463. low = index;
  464. while (low != IWL_RATE_INVALID) {
  465. if (rs_sta->tgg)
  466. low = iwl3945_rates[low].prev_rs_tgg;
  467. else
  468. low = iwl3945_rates[low].prev_rs;
  469. if (low == IWL_RATE_INVALID)
  470. break;
  471. if (rate_mask & (1 << low))
  472. break;
  473. IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
  474. }
  475. high = index;
  476. while (high != IWL_RATE_INVALID) {
  477. if (rs_sta->tgg)
  478. high = iwl3945_rates[high].next_rs_tgg;
  479. else
  480. high = iwl3945_rates[high].next_rs;
  481. if (high == IWL_RATE_INVALID)
  482. break;
  483. if (rate_mask & (1 << high))
  484. break;
  485. IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
  486. }
  487. return (high << 8) | low;
  488. }
  489. /**
  490. * rs_get_rate - find the rate for the requested packet
  491. *
  492. * Returns the ieee80211_rate structure allocated by the driver.
  493. *
  494. * The rate control algorithm has no internal mapping between hw_mode's
  495. * rate ordering and the rate ordering used by the rate control algorithm.
  496. *
  497. * The rate control algorithm uses a single table of rates that goes across
  498. * the entire A/B/G spectrum vs. being limited to just one particular
  499. * hw_mode.
  500. *
  501. * As such, we can't convert the index obtained below into the hw_mode's
  502. * rate table and must reference the driver allocated rate table
  503. *
  504. */
  505. static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta,
  506. void *priv_sta, struct ieee80211_tx_rate_control *txrc)
  507. {
  508. struct ieee80211_supported_band *sband = txrc->sband;
  509. struct sk_buff *skb = txrc->skb;
  510. u8 low = IWL_RATE_INVALID;
  511. u8 high = IWL_RATE_INVALID;
  512. u16 high_low;
  513. int index;
  514. struct iwl3945_rs_sta *rs_sta = priv_sta;
  515. struct iwl3945_rate_scale_data *window = NULL;
  516. int current_tpt = IWL_INV_TPT;
  517. int low_tpt = IWL_INV_TPT;
  518. int high_tpt = IWL_INV_TPT;
  519. u32 fail_count;
  520. s8 scale_action = 0;
  521. unsigned long flags;
  522. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  523. u16 fc, rate_mask;
  524. struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_r;
  525. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  526. IWL_DEBUG_RATE("enter\n");
  527. /* Send management frames and broadcast/multicast data using lowest
  528. * rate. */
  529. fc = le16_to_cpu(hdr->frame_control);
  530. if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
  531. is_multicast_ether_addr(hdr->addr1) ||
  532. !sta || !priv_sta) {
  533. IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
  534. info->control.rates[0].idx = rate_lowest_index(sband, sta);
  535. return;
  536. }
  537. rate_mask = sta->supp_rates[sband->band];
  538. index = min(rs_sta->last_txrate_idx & 0xffff, IWL_RATE_COUNT - 1);
  539. if (sband->band == IEEE80211_BAND_5GHZ)
  540. rate_mask = rate_mask << IWL_FIRST_OFDM_RATE;
  541. if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
  542. !rs_sta->ibss_sta_added) {
  543. u8 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
  544. if (sta_id == IWL_INVALID_STATION) {
  545. IWL_DEBUG_RATE("LQ: ADD station %pm\n",
  546. hdr->addr1);
  547. sta_id = iwl3945_add_station(priv,
  548. hdr->addr1, 0, CMD_ASYNC);
  549. }
  550. if (sta_id != IWL_INVALID_STATION)
  551. rs_sta->ibss_sta_added = 1;
  552. }
  553. spin_lock_irqsave(&rs_sta->lock, flags);
  554. /* for recent assoc, choose best rate regarding
  555. * to rssi value
  556. */
  557. if (rs_sta->start_rate != IWL_RATE_INVALID) {
  558. if (rs_sta->start_rate < index &&
  559. (rate_mask & (1 << rs_sta->start_rate)))
  560. index = rs_sta->start_rate;
  561. rs_sta->start_rate = IWL_RATE_INVALID;
  562. }
  563. window = &(rs_sta->win[index]);
  564. fail_count = window->counter - window->success_counter;
  565. if (((fail_count <= IWL_RATE_MIN_FAILURE_TH) &&
  566. (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))) {
  567. spin_unlock_irqrestore(&rs_sta->lock, flags);
  568. IWL_DEBUG_RATE("Invalid average_tpt on rate %d: "
  569. "counter: %d, success_counter: %d, "
  570. "expected_tpt is %sNULL\n",
  571. index,
  572. window->counter,
  573. window->success_counter,
  574. rs_sta->expected_tpt ? "not " : "");
  575. goto out;
  576. }
  577. current_tpt = window->average_tpt;
  578. high_low = iwl3945_get_adjacent_rate(rs_sta, index, rate_mask,
  579. sband->band);
  580. low = high_low & 0xff;
  581. high = (high_low >> 8) & 0xff;
  582. if (low != IWL_RATE_INVALID)
  583. low_tpt = rs_sta->win[low].average_tpt;
  584. if (high != IWL_RATE_INVALID)
  585. high_tpt = rs_sta->win[high].average_tpt;
  586. spin_unlock_irqrestore(&rs_sta->lock, flags);
  587. scale_action = 1;
  588. if ((window->success_ratio < IWL_RATE_DECREASE_TH) || !current_tpt) {
  589. IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
  590. scale_action = -1;
  591. } else if ((low_tpt == IWL_INV_TPT) && (high_tpt == IWL_INV_TPT))
  592. scale_action = 1;
  593. else if ((low_tpt != IWL_INV_TPT) && (high_tpt != IWL_INV_TPT) &&
  594. (low_tpt < current_tpt) && (high_tpt < current_tpt)) {
  595. IWL_DEBUG_RATE("No action -- low [%d] & high [%d] < "
  596. "current_tpt [%d]\n",
  597. low_tpt, high_tpt, current_tpt);
  598. scale_action = 0;
  599. } else {
  600. if (high_tpt != IWL_INV_TPT) {
  601. if (high_tpt > current_tpt)
  602. scale_action = 1;
  603. else {
  604. IWL_DEBUG_RATE
  605. ("decrease rate because of high tpt\n");
  606. scale_action = -1;
  607. }
  608. } else if (low_tpt != IWL_INV_TPT) {
  609. if (low_tpt > current_tpt) {
  610. IWL_DEBUG_RATE
  611. ("decrease rate because of low tpt\n");
  612. scale_action = -1;
  613. } else
  614. scale_action = 1;
  615. }
  616. }
  617. if (scale_action == -1) {
  618. if (window->success_ratio > IWL_SUCCESS_DOWN_TH)
  619. scale_action = 0;
  620. } else if (scale_action == 1) {
  621. if (window->success_ratio < IWL_SUCCESS_UP_TH) {
  622. IWL_DEBUG_RATE("No action -- success_ratio [%d] < "
  623. "SUCCESS UP\n", window->success_ratio);
  624. scale_action = 0;
  625. }
  626. }
  627. switch (scale_action) {
  628. case -1:
  629. if (low != IWL_RATE_INVALID)
  630. index = low;
  631. break;
  632. case 1:
  633. if (high != IWL_RATE_INVALID)
  634. index = high;
  635. break;
  636. case 0:
  637. default:
  638. break;
  639. }
  640. IWL_DEBUG_RATE("Selected %d (action %d) - low %d high %d\n",
  641. index, scale_action, low, high);
  642. out:
  643. rs_sta->last_txrate_idx = index;
  644. if (sband->band == IEEE80211_BAND_5GHZ)
  645. info->control.rates[0].idx = rs_sta->last_txrate_idx -
  646. IWL_FIRST_OFDM_RATE;
  647. else
  648. info->control.rates[0].idx = rs_sta->last_txrate_idx;
  649. IWL_DEBUG_RATE("leave: %d\n", index);
  650. }
  651. #ifdef CONFIG_MAC80211_DEBUGFS
  652. static int iwl3945_open_file_generic(struct inode *inode, struct file *file)
  653. {
  654. file->private_data = inode->i_private;
  655. return 0;
  656. }
  657. static ssize_t iwl3945_sta_dbgfs_stats_table_read(struct file *file,
  658. char __user *user_buf,
  659. size_t count, loff_t *ppos)
  660. {
  661. char buff[1024];
  662. int desc = 0;
  663. int j;
  664. struct iwl3945_rs_sta *lq_sta = file->private_data;
  665. desc += sprintf(buff + desc, "tx packets=%d last rate index=%d\n"
  666. "rate=0x%X flush time %d\n",
  667. lq_sta->tx_packets,
  668. lq_sta->last_txrate_idx,
  669. lq_sta->start_rate, jiffies_to_msecs(lq_sta->flush_time));
  670. for (j = 0; j < IWL_RATE_COUNT; j++) {
  671. desc += sprintf(buff+desc,
  672. "counter=%d success=%d %%=%d\n",
  673. lq_sta->win[j].counter,
  674. lq_sta->win[j].success_counter,
  675. lq_sta->win[j].success_ratio);
  676. }
  677. return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
  678. }
  679. static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
  680. .read = iwl3945_sta_dbgfs_stats_table_read,
  681. .open = iwl3945_open_file_generic,
  682. };
  683. static void iwl3945_add_debugfs(void *priv, void *priv_sta,
  684. struct dentry *dir)
  685. {
  686. struct iwl3945_rs_sta *lq_sta = priv_sta;
  687. lq_sta->rs_sta_dbgfs_stats_table_file =
  688. debugfs_create_file("rate_stats_table", 0600, dir,
  689. lq_sta, &rs_sta_dbgfs_stats_table_ops);
  690. }
  691. static void iwl3945_remove_debugfs(void *priv, void *priv_sta)
  692. {
  693. struct iwl3945_rs_sta *lq_sta = priv_sta;
  694. debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
  695. }
  696. #endif
  697. static struct rate_control_ops rs_ops = {
  698. .module = NULL,
  699. .name = RS_NAME,
  700. .tx_status = rs_tx_status,
  701. .get_rate = rs_get_rate,
  702. .rate_init = rs_rate_init,
  703. .alloc = rs_alloc,
  704. .free = rs_free,
  705. .alloc_sta = rs_alloc_sta,
  706. .free_sta = rs_free_sta,
  707. #ifdef CONFIG_MAC80211_DEBUGFS
  708. .add_sta_debugfs = iwl3945_add_debugfs,
  709. .remove_sta_debugfs = iwl3945_remove_debugfs,
  710. #endif
  711. };
  712. void iwl3945_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
  713. {
  714. struct iwl3945_priv *priv = hw->priv;
  715. s32 rssi = 0;
  716. unsigned long flags;
  717. struct iwl3945_rs_sta *rs_sta;
  718. struct ieee80211_sta *sta;
  719. struct iwl3945_sta_priv *psta;
  720. IWL_DEBUG_RATE("enter\n");
  721. rcu_read_lock();
  722. sta = ieee80211_find_sta(hw, priv->stations[sta_id].sta.sta.addr);
  723. if (!sta) {
  724. rcu_read_unlock();
  725. return;
  726. }
  727. psta = (void *) sta->drv_priv;
  728. rs_sta = psta->rs_sta;
  729. spin_lock_irqsave(&rs_sta->lock, flags);
  730. rs_sta->tgg = 0;
  731. switch (priv->band) {
  732. case IEEE80211_BAND_2GHZ:
  733. /* TODO: this always does G, not a regression */
  734. if (priv->active_rxon.flags & RXON_FLG_TGG_PROTECT_MSK) {
  735. rs_sta->tgg = 1;
  736. rs_sta->expected_tpt = iwl3945_expected_tpt_g_prot;
  737. } else
  738. rs_sta->expected_tpt = iwl3945_expected_tpt_g;
  739. break;
  740. case IEEE80211_BAND_5GHZ:
  741. rs_sta->expected_tpt = iwl3945_expected_tpt_a;
  742. break;
  743. case IEEE80211_NUM_BANDS:
  744. BUG();
  745. break;
  746. }
  747. spin_unlock_irqrestore(&rs_sta->lock, flags);
  748. rssi = priv->last_rx_rssi;
  749. if (rssi == 0)
  750. rssi = IWL_MIN_RSSI_VAL;
  751. IWL_DEBUG(IWL_DL_INFO | IWL_DL_RATE, "Network RSSI: %d\n", rssi);
  752. rs_sta->start_rate = iwl3945_get_rate_index_by_rssi(rssi, priv->band);
  753. IWL_DEBUG_RATE("leave: rssi %d assign rate index: "
  754. "%d (plcp 0x%x)\n", rssi, rs_sta->start_rate,
  755. iwl3945_rates[rs_sta->start_rate].plcp);
  756. rcu_read_unlock();
  757. }
  758. int iwl3945_rate_control_register(void)
  759. {
  760. return ieee80211_rate_control_register(&rs_ops);
  761. }
  762. void iwl3945_rate_control_unregister(void)
  763. {
  764. ieee80211_rate_control_unregister(&rs_ops);
  765. }