iwl-3945-rs.c 25 KB

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