iwl-3945-rs.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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. 7, 13, 35, 58, 0, 0, 76, 104, 130, 168, 191, 202
  65. };
  66. static s32 iwl_expected_tpt_g_prot[IWL_RATE_COUNT] = {
  67. 7, 13, 35, 58, 0, 0, 0, 80, 93, 113, 123, 125
  68. };
  69. static s32 iwl_expected_tpt_a[IWL_RATE_COUNT] = {
  70. 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186
  71. };
  72. static s32 iwl_expected_tpt_b[IWL_RATE_COUNT] = {
  73. 7, 13, 35, 58, 0, 0, 0, 0, 0, 0, 0, 0
  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. /* For MODE_IEEE80211A mode it start at IWL_FIRST_OFDM_RATE */
  287. if (local->hw.conf.phymode == MODE_IEEE80211A)
  288. sta->last_txrate += IWL_FIRST_OFDM_RATE;
  289. IWL_DEBUG_RATE("leave\n");
  290. }
  291. static void *rs_alloc(struct ieee80211_local *local)
  292. {
  293. return local->hw.priv;
  294. }
  295. /* rate scale requires free function to be implmented */
  296. static void rs_free(void *priv)
  297. {
  298. return;
  299. }
  300. static void rs_clear(void *priv)
  301. {
  302. return;
  303. }
  304. static void *rs_alloc_sta(void *priv, gfp_t gfp)
  305. {
  306. struct iwl_rate_scale_priv *rs_priv;
  307. int i;
  308. IWL_DEBUG_RATE("enter\n");
  309. rs_priv = kzalloc(sizeof(struct iwl_rate_scale_priv), gfp);
  310. if (!rs_priv) {
  311. IWL_DEBUG_RATE("leave: ENOMEM\n");
  312. return NULL;
  313. }
  314. spin_lock_init(&rs_priv->lock);
  315. rs_priv->start_rate = IWL_RATE_INVALID;
  316. /* default to just 802.11b */
  317. rs_priv->expected_tpt = iwl_expected_tpt_b;
  318. rs_priv->last_partial_flush = jiffies;
  319. rs_priv->last_flush = jiffies;
  320. rs_priv->flush_time = IWL_RATE_FLUSH;
  321. rs_priv->last_tx_packets = 0;
  322. rs_priv->ibss_sta_added = 0;
  323. init_timer(&rs_priv->rate_scale_flush);
  324. rs_priv->rate_scale_flush.data = (unsigned long)rs_priv;
  325. rs_priv->rate_scale_flush.function = &iwl_bg_rate_scale_flush;
  326. for (i = 0; i < IWL_RATE_COUNT; i++)
  327. iwl_clear_window(&rs_priv->win[i]);
  328. IWL_DEBUG_RATE("leave\n");
  329. return rs_priv;
  330. }
  331. static void rs_free_sta(void *priv, void *priv_sta)
  332. {
  333. struct iwl_rate_scale_priv *rs_priv = priv_sta;
  334. IWL_DEBUG_RATE("enter\n");
  335. del_timer_sync(&rs_priv->rate_scale_flush);
  336. kfree(rs_priv);
  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 iwl_priv *priv, int rate)
  345. {
  346. int next_rate = iwl_get_prev_ieee_rate(rate);
  347. switch (priv->phymode) {
  348. case MODE_IEEE80211A:
  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. case MODE_IEEE80211B:
  355. if (rate == IWL_RATE_11M_INDEX_TABLE)
  356. next_rate = IWL_RATE_5M_INDEX_TABLE;
  357. break;
  358. default:
  359. break;
  360. }
  361. return next_rate;
  362. }
  363. /**
  364. * rs_tx_status - Update rate control values based on Tx results
  365. *
  366. * NOTE: Uses iwl_priv->retry_rate for the # of retries attempted by
  367. * the hardware for each rate.
  368. */
  369. static void rs_tx_status(void *priv_rate,
  370. struct net_device *dev,
  371. struct sk_buff *skb,
  372. struct ieee80211_tx_status *tx_resp)
  373. {
  374. u8 retries, current_count;
  375. int scale_rate_index, first_index, last_index;
  376. unsigned long flags;
  377. struct sta_info *sta;
  378. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  379. struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
  380. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  381. struct iwl_rate_scale_priv *rs_priv;
  382. IWL_DEBUG_RATE("enter\n");
  383. retries = tx_resp->retry_count;
  384. first_index = tx_resp->control.tx_rate;
  385. if ((first_index < 0) || (first_index >= IWL_RATE_COUNT)) {
  386. IWL_DEBUG_RATE("leave: Rate out of bounds: %0x for %d\n",
  387. tx_resp->control.tx_rate, first_index);
  388. return;
  389. }
  390. sta = sta_info_get(local, hdr->addr1);
  391. if (!sta || !sta->rate_ctrl_priv) {
  392. if (sta)
  393. sta_info_put(sta);
  394. IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
  395. return;
  396. }
  397. rs_priv = (void *)sta->rate_ctrl_priv;
  398. rs_priv->tx_packets++;
  399. scale_rate_index = first_index;
  400. last_index = first_index;
  401. /*
  402. * Update the window for each rate. We determine which rates
  403. * were Tx'd based on the total number of retries vs. the number
  404. * of retries configured for each rate -- currently set to the
  405. * priv value 'retry_rate' vs. rate specific
  406. *
  407. * On exit from this while loop last_index indicates the rate
  408. * at which the frame was finally transmitted (or failed if no
  409. * ACK)
  410. */
  411. while (retries > 0) {
  412. if (retries < priv->retry_rate) {
  413. current_count = retries;
  414. last_index = scale_rate_index;
  415. } else {
  416. current_count = priv->retry_rate;
  417. last_index = rs_adjust_next_rate(priv,
  418. scale_rate_index);
  419. }
  420. /* Update this rate accounting for as many retries
  421. * as was used for it (per current_count) */
  422. iwl_collect_tx_data(rs_priv,
  423. &rs_priv->win[scale_rate_index],
  424. 0, current_count);
  425. IWL_DEBUG_RATE("Update rate %d for %d retries.\n",
  426. scale_rate_index, current_count);
  427. retries -= current_count;
  428. if (retries)
  429. scale_rate_index =
  430. rs_adjust_next_rate(priv, scale_rate_index);
  431. }
  432. /* Update the last index window with success/failure based on ACK */
  433. IWL_DEBUG_RATE("Update rate %d with %s.\n",
  434. last_index,
  435. (tx_resp->flags & IEEE80211_TX_STATUS_ACK) ?
  436. "success" : "failure");
  437. iwl_collect_tx_data(rs_priv,
  438. &rs_priv->win[last_index],
  439. tx_resp->flags & IEEE80211_TX_STATUS_ACK, 1);
  440. /* We updated the rate scale window -- if its been more than
  441. * flush_time since the last run, schedule the flush
  442. * again */
  443. spin_lock_irqsave(&rs_priv->lock, flags);
  444. if (!rs_priv->flush_pending &&
  445. time_after(jiffies, rs_priv->last_partial_flush +
  446. rs_priv->flush_time)) {
  447. rs_priv->flush_pending = 1;
  448. mod_timer(&rs_priv->rate_scale_flush,
  449. jiffies + rs_priv->flush_time);
  450. }
  451. spin_unlock_irqrestore(&rs_priv->lock, flags);
  452. sta_info_put(sta);
  453. IWL_DEBUG_RATE("leave\n");
  454. return;
  455. }
  456. static u16 iwl_get_adjacent_rate(struct iwl_rate_scale_priv *rs_priv,
  457. u8 index, u16 rate_mask, int phymode)
  458. {
  459. u8 high = IWL_RATE_INVALID;
  460. u8 low = IWL_RATE_INVALID;
  461. /* 802.11A walks to the next literal adjascent rate in
  462. * the rate table */
  463. if (unlikely(phymode == MODE_IEEE80211A)) {
  464. int i;
  465. u32 mask;
  466. /* Find the previous rate that is in the rate mask */
  467. i = index - 1;
  468. for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
  469. if (rate_mask & mask) {
  470. low = i;
  471. break;
  472. }
  473. }
  474. /* Find the next rate that is in the rate mask */
  475. i = index + 1;
  476. for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
  477. if (rate_mask & mask) {
  478. high = i;
  479. break;
  480. }
  481. }
  482. return (high << 8) | low;
  483. }
  484. low = index;
  485. while (low != IWL_RATE_INVALID) {
  486. if (rs_priv->tgg)
  487. low = iwl_rates[low].prev_rs_tgg;
  488. else
  489. low = iwl_rates[low].prev_rs;
  490. if (low == IWL_RATE_INVALID)
  491. break;
  492. if (rate_mask & (1 << low))
  493. break;
  494. IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
  495. }
  496. high = index;
  497. while (high != IWL_RATE_INVALID) {
  498. if (rs_priv->tgg)
  499. high = iwl_rates[high].next_rs_tgg;
  500. else
  501. high = iwl_rates[high].next_rs;
  502. if (high == IWL_RATE_INVALID)
  503. break;
  504. if (rate_mask & (1 << high))
  505. break;
  506. IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
  507. }
  508. return (high << 8) | low;
  509. }
  510. /**
  511. * rs_get_rate - find the rate for the requested packet
  512. *
  513. * Returns the ieee80211_rate structure allocated by the driver.
  514. *
  515. * The rate control algorithm has no internal mapping between hw_mode's
  516. * rate ordering and the rate ordering used by the rate control algorithm.
  517. *
  518. * The rate control algorithm uses a single table of rates that goes across
  519. * the entire A/B/G spectrum vs. being limited to just one particular
  520. * hw_mode.
  521. *
  522. * As such, we can't convert the index obtained below into the hw_mode's
  523. * rate table and must reference the driver allocated rate table
  524. *
  525. */
  526. static void rs_get_rate(void *priv_rate, struct net_device *dev,
  527. struct ieee80211_hw_mode *mode, struct sk_buff *skb,
  528. struct rate_selection *sel)
  529. {
  530. u8 low = IWL_RATE_INVALID;
  531. u8 high = IWL_RATE_INVALID;
  532. u16 high_low;
  533. int index;
  534. struct iwl_rate_scale_priv *rs_priv;
  535. struct iwl_rate_scale_data *window = NULL;
  536. int current_tpt = IWL_INVALID_VALUE;
  537. int low_tpt = IWL_INVALID_VALUE;
  538. int high_tpt = IWL_INVALID_VALUE;
  539. u32 fail_count;
  540. s8 scale_action = 0;
  541. unsigned long flags;
  542. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  543. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  544. struct sta_info *sta;
  545. u16 rate_mask;
  546. struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
  547. DECLARE_MAC_BUF(mac);
  548. IWL_DEBUG_RATE("enter\n");
  549. sta = sta_info_get(local, hdr->addr1);
  550. if (!sta || !sta->rate_ctrl_priv) {
  551. IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
  552. sel->rate = rate_lowest(local, local->oper_hw_mode, sta);
  553. if (sta)
  554. sta_info_put(sta);
  555. return;
  556. }
  557. rate_mask = sta->supp_rates;
  558. index = min(sta->last_txrate & 0xffff, IWL_RATE_COUNT - 1);
  559. if (priv->phymode == (u8) MODE_IEEE80211A)
  560. rate_mask = rate_mask << IWL_FIRST_OFDM_RATE;
  561. rs_priv = (void *)sta->rate_ctrl_priv;
  562. if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
  563. !rs_priv->ibss_sta_added) {
  564. u8 sta_id = iwl_hw_find_station(priv, hdr->addr1);
  565. if (sta_id == IWL_INVALID_STATION) {
  566. IWL_DEBUG_RATE("LQ: ADD station %s\n",
  567. print_mac(mac, hdr->addr1));
  568. sta_id = iwl_add_station(priv,
  569. hdr->addr1, 0, CMD_ASYNC);
  570. }
  571. if (sta_id != IWL_INVALID_STATION)
  572. rs_priv->ibss_sta_added = 1;
  573. }
  574. spin_lock_irqsave(&rs_priv->lock, flags);
  575. if (rs_priv->start_rate != IWL_RATE_INVALID) {
  576. index = rs_priv->start_rate;
  577. rs_priv->start_rate = IWL_RATE_INVALID;
  578. }
  579. window = &(rs_priv->win[index]);
  580. fail_count = window->counter - window->success_counter;
  581. if (((fail_count <= IWL_RATE_MIN_FAILURE_TH) &&
  582. (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))) {
  583. window->average_tpt = IWL_INVALID_VALUE;
  584. spin_unlock_irqrestore(&rs_priv->lock, flags);
  585. IWL_DEBUG_RATE("Invalid average_tpt on rate %d: "
  586. "counter: %d, success_counter: %d, "
  587. "expected_tpt is %sNULL\n",
  588. index,
  589. window->counter,
  590. window->success_counter,
  591. rs_priv->expected_tpt ? "not " : "");
  592. goto out;
  593. }
  594. window->average_tpt = ((window->success_ratio *
  595. rs_priv->expected_tpt[index] + 64) / 128);
  596. current_tpt = window->average_tpt;
  597. high_low = iwl_get_adjacent_rate(rs_priv, index, rate_mask,
  598. local->hw.conf.phymode);
  599. low = high_low & 0xff;
  600. high = (high_low >> 8) & 0xff;
  601. if (low != IWL_RATE_INVALID)
  602. low_tpt = rs_priv->win[low].average_tpt;
  603. if (high != IWL_RATE_INVALID)
  604. high_tpt = rs_priv->win[high].average_tpt;
  605. spin_unlock_irqrestore(&rs_priv->lock, flags);
  606. scale_action = 1;
  607. if ((window->success_ratio < IWL_RATE_DECREASE_TH) || !current_tpt) {
  608. IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
  609. scale_action = -1;
  610. } else if ((low_tpt == IWL_INVALID_VALUE) &&
  611. (high_tpt == IWL_INVALID_VALUE))
  612. scale_action = 1;
  613. else if ((low_tpt != IWL_INVALID_VALUE) &&
  614. (high_tpt != IWL_INVALID_VALUE)
  615. && (low_tpt < current_tpt)
  616. && (high_tpt < current_tpt)) {
  617. IWL_DEBUG_RATE("No action -- low [%d] & high [%d] < "
  618. "current_tpt [%d]\n",
  619. low_tpt, high_tpt, current_tpt);
  620. scale_action = 0;
  621. } else {
  622. if (high_tpt != IWL_INVALID_VALUE) {
  623. if (high_tpt > current_tpt)
  624. scale_action = 1;
  625. else {
  626. IWL_DEBUG_RATE
  627. ("decrease rate because of high tpt\n");
  628. scale_action = -1;
  629. }
  630. } else if (low_tpt != IWL_INVALID_VALUE) {
  631. if (low_tpt > current_tpt) {
  632. IWL_DEBUG_RATE
  633. ("decrease rate because of low tpt\n");
  634. scale_action = -1;
  635. } else
  636. scale_action = 1;
  637. }
  638. }
  639. if ((window->success_ratio > IWL_RATE_HIGH_TH) ||
  640. (current_tpt > window->average_tpt)) {
  641. IWL_DEBUG_RATE("No action -- success_ratio [%d] > HIGH_TH or "
  642. "current_tpt [%d] > average_tpt [%d]\n",
  643. window->success_ratio,
  644. current_tpt, window->average_tpt);
  645. scale_action = 0;
  646. }
  647. switch (scale_action) {
  648. case -1:
  649. if (low != IWL_RATE_INVALID)
  650. index = low;
  651. break;
  652. case 1:
  653. if (high != IWL_RATE_INVALID)
  654. index = high;
  655. break;
  656. case 0:
  657. default:
  658. break;
  659. }
  660. IWL_DEBUG_RATE("Selected %d (action %d) - low %d high %d\n",
  661. index, scale_action, low, high);
  662. out:
  663. sta->last_txrate = index;
  664. if (priv->phymode == (u8) MODE_IEEE80211A)
  665. sta->txrate = sta->last_txrate - IWL_FIRST_OFDM_RATE;
  666. else
  667. sta->txrate = sta->last_txrate;
  668. sta_info_put(sta);
  669. IWL_DEBUG_RATE("leave: %d\n", index);
  670. sel->rate = &priv->ieee_rates[index];
  671. }
  672. static struct rate_control_ops rs_ops = {
  673. .module = NULL,
  674. .name = RS_NAME,
  675. .tx_status = rs_tx_status,
  676. .get_rate = rs_get_rate,
  677. .rate_init = rs_rate_init,
  678. .clear = rs_clear,
  679. .alloc = rs_alloc,
  680. .free = rs_free,
  681. .alloc_sta = rs_alloc_sta,
  682. .free_sta = rs_free_sta,
  683. };
  684. int iwl_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
  685. {
  686. struct ieee80211_local *local = hw_to_local(hw);
  687. struct iwl_priv *priv = hw->priv;
  688. struct iwl_rate_scale_priv *rs_priv;
  689. struct sta_info *sta;
  690. unsigned long flags;
  691. int count = 0, i;
  692. u32 samples = 0, success = 0, good = 0;
  693. unsigned long now = jiffies;
  694. u32 max_time = 0;
  695. sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
  696. if (!sta || !sta->rate_ctrl_priv) {
  697. if (sta) {
  698. sta_info_put(sta);
  699. IWL_DEBUG_RATE("leave - no private rate data!\n");
  700. } else
  701. IWL_DEBUG_RATE("leave - no station!\n");
  702. return sprintf(buf, "station %d not found\n", sta_id);
  703. }
  704. rs_priv = (void *)sta->rate_ctrl_priv;
  705. spin_lock_irqsave(&rs_priv->lock, flags);
  706. i = IWL_RATE_54M_INDEX;
  707. while (1) {
  708. u64 mask;
  709. int j;
  710. count +=
  711. sprintf(&buf[count], " %2dMbs: ", iwl_rates[i].ieee / 2);
  712. mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
  713. for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
  714. buf[count++] =
  715. (rs_priv->win[i].data & mask) ? '1' : '0';
  716. samples += rs_priv->win[i].counter;
  717. good += rs_priv->win[i].success_counter;
  718. success += rs_priv->win[i].success_counter * iwl_rates[i].ieee;
  719. if (rs_priv->win[i].stamp) {
  720. int delta =
  721. jiffies_to_msecs(now - rs_priv->win[i].stamp);
  722. if (delta > max_time)
  723. max_time = delta;
  724. count += sprintf(&buf[count], "%5dms\n", delta);
  725. } else
  726. buf[count++] = '\n';
  727. j = iwl_get_prev_ieee_rate(i);
  728. if (j == i)
  729. break;
  730. i = j;
  731. }
  732. spin_unlock_irqrestore(&rs_priv->lock, flags);
  733. sta_info_put(sta);
  734. /* Display the average rate of all samples taken.
  735. *
  736. * NOTE: We multiple # of samples by 2 since the IEEE measurement
  737. * added from iwl_rates is actually 2X the rate */
  738. if (samples)
  739. count += sprintf(
  740. &buf[count],
  741. "\nAverage rate is %3d.%02dMbs over last %4dms\n"
  742. "%3d%% success (%d good packets over %d tries)\n",
  743. success / (2 * samples), (success * 5 / samples) % 10,
  744. max_time, good * 100 / samples, good, samples);
  745. else
  746. count += sprintf(&buf[count], "\nAverage rate: 0Mbs\n");
  747. return count;
  748. }
  749. void iwl_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
  750. {
  751. struct iwl_priv *priv = hw->priv;
  752. s32 rssi = 0;
  753. unsigned long flags;
  754. struct ieee80211_local *local = hw_to_local(hw);
  755. struct iwl_rate_scale_priv *rs_priv;
  756. struct sta_info *sta;
  757. IWL_DEBUG_RATE("enter\n");
  758. if (!local->rate_ctrl->ops->name ||
  759. strcmp(local->rate_ctrl->ops->name, RS_NAME)) {
  760. IWL_WARNING("iwl-3945-rs not selected as rate control algo!\n");
  761. IWL_DEBUG_RATE("leave - mac80211 picked the wrong RC algo.\n");
  762. return;
  763. }
  764. sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
  765. if (!sta || !sta->rate_ctrl_priv) {
  766. if (sta)
  767. sta_info_put(sta);
  768. IWL_DEBUG_RATE("leave - no private rate data!\n");
  769. return;
  770. }
  771. rs_priv = (void *)sta->rate_ctrl_priv;
  772. spin_lock_irqsave(&rs_priv->lock, flags);
  773. rs_priv->tgg = 0;
  774. switch (priv->phymode) {
  775. case MODE_IEEE80211G:
  776. if (priv->active_rxon.flags & RXON_FLG_TGG_PROTECT_MSK) {
  777. rs_priv->tgg = 1;
  778. rs_priv->expected_tpt = iwl_expected_tpt_g_prot;
  779. } else
  780. rs_priv->expected_tpt = iwl_expected_tpt_g;
  781. break;
  782. case MODE_IEEE80211A:
  783. rs_priv->expected_tpt = iwl_expected_tpt_a;
  784. break;
  785. default:
  786. IWL_WARNING("Invalid phymode. Defaulting to 802.11b\n");
  787. case MODE_IEEE80211B:
  788. rs_priv->expected_tpt = iwl_expected_tpt_b;
  789. break;
  790. }
  791. sta_info_put(sta);
  792. spin_unlock_irqrestore(&rs_priv->lock, flags);
  793. rssi = priv->last_rx_rssi;
  794. if (rssi == 0)
  795. rssi = IWL_MIN_RSSI_VAL;
  796. IWL_DEBUG(IWL_DL_INFO | IWL_DL_RATE, "Network RSSI: %d\n", rssi);
  797. rs_priv->start_rate = iwl_get_rate_index_by_rssi(rssi, priv->phymode);
  798. IWL_DEBUG_RATE("leave: rssi %d assign rate index: "
  799. "%d (plcp 0x%x)\n", rssi, rs_priv->start_rate,
  800. iwl_rates[rs_priv->start_rate].plcp);
  801. }
  802. void iwl_rate_control_register(struct ieee80211_hw *hw)
  803. {
  804. ieee80211_rate_control_register(&rs_ops);
  805. }
  806. void iwl_rate_control_unregister(struct ieee80211_hw *hw)
  807. {
  808. ieee80211_rate_control_unregister(&rs_ops);
  809. }