iwl-3945-rs.c 25 KB

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