iwl-3945-rs.c 22 KB

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