iwl-3945-rs.c 25 KB

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