iwl-3945-rs.c 25 KB

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