iwl-3945-rs.c 25 KB

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