iwl-3945-rs.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2005 - 2009 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. * Intel Linux Wireless <ilw@linux.intel.com>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. *****************************************************************************/
  26. #include <linux/kernel.h>
  27. #include <linux/init.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/wireless.h>
  30. #include <net/mac80211.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/etherdevice.h>
  33. #include <linux/delay.h>
  34. #include <linux/workqueue.h>
  35. #include "iwl-commands.h"
  36. #include "iwl-3945.h"
  37. #include "iwl-sta.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. struct iwl_priv *priv;
  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 iwl3945_rate_scale_data win[IWL_RATE_COUNT_3945];
  62. #ifdef CONFIG_MAC80211_DEBUGFS
  63. struct dentry *rs_sta_dbgfs_stats_table_file;
  64. #endif
  65. /* used to be in sta_info */
  66. int last_txrate_idx;
  67. };
  68. static s32 iwl3945_expected_tpt_g[IWL_RATE_COUNT_3945] = {
  69. 7, 13, 35, 58, 0, 0, 76, 104, 130, 168, 191, 202
  70. };
  71. static s32 iwl3945_expected_tpt_g_prot[IWL_RATE_COUNT_3945] = {
  72. 7, 13, 35, 58, 0, 0, 0, 80, 93, 113, 123, 125
  73. };
  74. static s32 iwl3945_expected_tpt_a[IWL_RATE_COUNT_3945] = {
  75. 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186
  76. };
  77. static s32 iwl3945_expected_tpt_b[IWL_RATE_COUNT_3945] = {
  78. 7, 13, 35, 58, 0, 0, 0, 0, 0, 0, 0, 0
  79. };
  80. struct iwl3945_tpt_entry {
  81. s8 min_rssi;
  82. u8 index;
  83. };
  84. static struct iwl3945_tpt_entry iwl3945_tpt_table_a[] = {
  85. {-60, IWL_RATE_54M_INDEX},
  86. {-64, IWL_RATE_48M_INDEX},
  87. {-72, IWL_RATE_36M_INDEX},
  88. {-80, IWL_RATE_24M_INDEX},
  89. {-84, IWL_RATE_18M_INDEX},
  90. {-85, IWL_RATE_12M_INDEX},
  91. {-87, IWL_RATE_9M_INDEX},
  92. {-89, IWL_RATE_6M_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)
  108. #define IWL_RATE_WIN_FLUSH (HZ/2)
  109. #define IWL39_RATE_HIGH_TH 11520
  110. #define IWL_SUCCESS_UP_TH 8960
  111. #define IWL_SUCCESS_DOWN_TH 10880
  112. #define IWL_RATE_MIN_FAILURE_TH 6
  113. #define IWL_RATE_MIN_SUCCESS_TH 8
  114. #define IWL_RATE_DECREASE_TH 1920
  115. #define IWL_RATE_RETRY_TH 15
  116. static u8 iwl3945_get_rate_index_by_rssi(s32 rssi, enum ieee80211_band band)
  117. {
  118. u32 index = 0;
  119. u32 table_size = 0;
  120. struct iwl3945_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 (band) {
  124. case IEEE80211_BAND_2GHZ:
  125. tpt_table = iwl3945_tpt_table_g;
  126. table_size = ARRAY_SIZE(iwl3945_tpt_table_g);
  127. break;
  128. case IEEE80211_BAND_5GHZ:
  129. tpt_table = iwl3945_tpt_table_a;
  130. table_size = ARRAY_SIZE(iwl3945_tpt_table_a);
  131. break;
  132. default:
  133. BUG();
  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 iwl3945_clear_window(struct iwl3945_rate_scale_data *window)
  142. {
  143. window->data = 0;
  144. window->success_counter = 0;
  145. window->success_ratio = -1;
  146. window->counter = 0;
  147. window->average_tpt = IWL_INVALID_VALUE;
  148. window->stamp = 0;
  149. }
  150. /**
  151. * iwl3945_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 iwl3945_rate_scale_flush_windows(struct iwl3945_rs_sta *rs_sta)
  158. {
  159. int unflushed = 0;
  160. int i;
  161. unsigned long flags;
  162. struct iwl_priv *priv __maybe_unused = rs_sta->priv;
  163. /*
  164. * For each rate, if we have collected data on that rate
  165. * and it has been more than IWL_RATE_WIN_FLUSH
  166. * since we flushed, clear out the gathered statistics
  167. */
  168. for (i = 0; i < IWL_RATE_COUNT_3945; i++) {
  169. if (!rs_sta->win[i].counter)
  170. continue;
  171. spin_lock_irqsave(&rs_sta->lock, flags);
  172. if (time_after(jiffies, rs_sta->win[i].stamp +
  173. IWL_RATE_WIN_FLUSH)) {
  174. IWL_DEBUG_RATE(priv, "flushing %d samples of rate "
  175. "index %d\n",
  176. rs_sta->win[i].counter, i);
  177. iwl3945_clear_window(&rs_sta->win[i]);
  178. } else
  179. unflushed++;
  180. spin_unlock_irqrestore(&rs_sta->lock, flags);
  181. }
  182. return unflushed;
  183. }
  184. #define IWL_RATE_FLUSH_MAX 5000 /* msec */
  185. #define IWL_RATE_FLUSH_MIN 50 /* msec */
  186. #define IWL_AVERAGE_PACKETS 1500
  187. static void iwl3945_bg_rate_scale_flush(unsigned long data)
  188. {
  189. struct iwl3945_rs_sta *rs_sta = (void *)data;
  190. struct iwl_priv *priv __maybe_unused = rs_sta->priv;
  191. int unflushed = 0;
  192. unsigned long flags;
  193. u32 packet_count, duration, pps;
  194. IWL_DEBUG_RATE(priv, "enter\n");
  195. unflushed = iwl3945_rate_scale_flush_windows(rs_sta);
  196. spin_lock_irqsave(&rs_sta->lock, flags);
  197. /* Number of packets Rx'd since last time this timer ran */
  198. packet_count = (rs_sta->tx_packets - rs_sta->last_tx_packets) + 1;
  199. rs_sta->last_tx_packets = rs_sta->tx_packets + 1;
  200. if (unflushed) {
  201. duration =
  202. jiffies_to_msecs(jiffies - rs_sta->last_partial_flush);
  203. IWL_DEBUG_RATE(priv, "Tx'd %d packets in %dms\n",
  204. packet_count, duration);
  205. /* Determine packets per second */
  206. if (duration)
  207. pps = (packet_count * 1000) / duration;
  208. else
  209. pps = 0;
  210. if (pps) {
  211. duration = (IWL_AVERAGE_PACKETS * 1000) / pps;
  212. if (duration < IWL_RATE_FLUSH_MIN)
  213. duration = IWL_RATE_FLUSH_MIN;
  214. else if (duration > IWL_RATE_FLUSH_MAX)
  215. duration = IWL_RATE_FLUSH_MAX;
  216. } else
  217. duration = IWL_RATE_FLUSH_MAX;
  218. rs_sta->flush_time = msecs_to_jiffies(duration);
  219. IWL_DEBUG_RATE(priv, "new flush period: %d msec ave %d\n",
  220. duration, packet_count);
  221. mod_timer(&rs_sta->rate_scale_flush, jiffies +
  222. rs_sta->flush_time);
  223. rs_sta->last_partial_flush = jiffies;
  224. } else {
  225. rs_sta->flush_time = IWL_RATE_FLUSH;
  226. rs_sta->flush_pending = 0;
  227. }
  228. /* If there weren't any unflushed entries, we don't schedule the timer
  229. * to run again */
  230. rs_sta->last_flush = jiffies;
  231. spin_unlock_irqrestore(&rs_sta->lock, flags);
  232. IWL_DEBUG_RATE(priv, "leave\n");
  233. }
  234. /**
  235. * iwl3945_collect_tx_data - Update the success/failure sliding window
  236. *
  237. * We keep a sliding window of the last 64 packets transmitted
  238. * at this rate. window->data contains the bitmask of successful
  239. * packets.
  240. */
  241. static void iwl3945_collect_tx_data(struct iwl3945_rs_sta *rs_sta,
  242. struct iwl3945_rate_scale_data *window,
  243. int success, int retries, int index)
  244. {
  245. unsigned long flags;
  246. s32 fail_count;
  247. struct iwl_priv *priv __maybe_unused = rs_sta->priv;
  248. if (!retries) {
  249. IWL_DEBUG_RATE(priv, "leave: retries == 0 -- should be at least 1\n");
  250. return;
  251. }
  252. spin_lock_irqsave(&rs_sta->lock, flags);
  253. /*
  254. * Keep track of only the latest 62 tx frame attempts in this rate's
  255. * history window; anything older isn't really relevant any more.
  256. * If we have filled up the sliding window, drop the oldest attempt;
  257. * if the oldest attempt (highest bit in bitmap) shows "success",
  258. * subtract "1" from the success counter (this is the main reason
  259. * we keep these bitmaps!).
  260. * */
  261. while (retries > 0) {
  262. if (window->counter >= IWL_RATE_MAX_WINDOW) {
  263. /* remove earliest */
  264. window->counter = IWL_RATE_MAX_WINDOW - 1;
  265. if (window->data & (1ULL << (IWL_RATE_MAX_WINDOW - 1))) {
  266. window->data &= ~(1ULL << (IWL_RATE_MAX_WINDOW - 1));
  267. window->success_counter--;
  268. }
  269. }
  270. /* Increment frames-attempted counter */
  271. window->counter++;
  272. /* Shift bitmap by one frame (throw away oldest history),
  273. * OR in "1", and increment "success" if this
  274. * frame was successful. */
  275. window->data <<= 1;
  276. if (success > 0) {
  277. window->success_counter++;
  278. window->data |= 0x1;
  279. success--;
  280. }
  281. retries--;
  282. }
  283. /* Calculate current success ratio, avoid divide-by-0! */
  284. if (window->counter > 0)
  285. window->success_ratio = 128 * (100 * window->success_counter)
  286. / window->counter;
  287. else
  288. window->success_ratio = IWL_INVALID_VALUE;
  289. fail_count = window->counter - window->success_counter;
  290. /* Calculate average throughput, if we have enough history. */
  291. if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
  292. (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
  293. window->average_tpt = ((window->success_ratio *
  294. rs_sta->expected_tpt[index] + 64) / 128);
  295. else
  296. window->average_tpt = IWL_INVALID_VALUE;
  297. /* Tag this window as having been updated */
  298. window->stamp = jiffies;
  299. spin_unlock_irqrestore(&rs_sta->lock, flags);
  300. }
  301. static void rs_rate_init(void *priv_r, struct ieee80211_supported_band *sband,
  302. struct ieee80211_sta *sta, void *priv_sta)
  303. {
  304. struct iwl3945_rs_sta *rs_sta = priv_sta;
  305. struct iwl_priv *priv = (struct iwl_priv *)priv_r;
  306. int i;
  307. IWL_DEBUG_RATE(priv, "enter\n");
  308. /* TODO: what is a good starting rate for STA? About middle? Maybe not
  309. * the lowest or the highest rate.. Could consider using RSSI from
  310. * previous packets? Need to have IEEE 802.1X auth succeed immediately
  311. * after assoc.. */
  312. for (i = sband->n_bitrates - 1; i >= 0; i--) {
  313. if (sta->supp_rates[sband->band] & (1 << i)) {
  314. rs_sta->last_txrate_idx = i;
  315. break;
  316. }
  317. }
  318. priv->sta_supp_rates = sta->supp_rates[sband->band];
  319. /* For 5 GHz band it start at IWL_FIRST_OFDM_RATE */
  320. if (sband->band == IEEE80211_BAND_5GHZ) {
  321. rs_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
  322. priv->sta_supp_rates = priv->sta_supp_rates <<
  323. IWL_FIRST_OFDM_RATE;
  324. }
  325. IWL_DEBUG_RATE(priv, "leave\n");
  326. }
  327. static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
  328. {
  329. return hw->priv;
  330. }
  331. /* rate scale requires free function to be implemented */
  332. static void rs_free(void *priv)
  333. {
  334. return;
  335. }
  336. static void *rs_alloc_sta(void *iwl_priv, struct ieee80211_sta *sta, gfp_t gfp)
  337. {
  338. struct iwl3945_rs_sta *rs_sta;
  339. struct iwl3945_sta_priv *psta = (void *) sta->drv_priv;
  340. struct iwl_priv *priv = iwl_priv;
  341. int i;
  342. /*
  343. * XXX: If it's using sta->drv_priv anyway, it might
  344. * as well just put all the information there.
  345. */
  346. IWL_DEBUG_RATE(priv, "enter\n");
  347. rs_sta = kzalloc(sizeof(struct iwl3945_rs_sta), gfp);
  348. if (!rs_sta) {
  349. IWL_DEBUG_RATE(priv, "leave: ENOMEM\n");
  350. return NULL;
  351. }
  352. psta->rs_sta = rs_sta;
  353. spin_lock_init(&rs_sta->lock);
  354. rs_sta->priv = priv;
  355. rs_sta->start_rate = IWL_RATE_INVALID;
  356. /* default to just 802.11b */
  357. rs_sta->expected_tpt = iwl3945_expected_tpt_b;
  358. rs_sta->last_partial_flush = jiffies;
  359. rs_sta->last_flush = jiffies;
  360. rs_sta->flush_time = IWL_RATE_FLUSH;
  361. rs_sta->last_tx_packets = 0;
  362. rs_sta->ibss_sta_added = 0;
  363. init_timer(&rs_sta->rate_scale_flush);
  364. rs_sta->rate_scale_flush.data = (unsigned long)rs_sta;
  365. rs_sta->rate_scale_flush.function = &iwl3945_bg_rate_scale_flush;
  366. for (i = 0; i < IWL_RATE_COUNT_3945; i++)
  367. iwl3945_clear_window(&rs_sta->win[i]);
  368. IWL_DEBUG_RATE(priv, "leave\n");
  369. return rs_sta;
  370. }
  371. static void rs_free_sta(void *iwl_priv, struct ieee80211_sta *sta,
  372. void *priv_sta)
  373. {
  374. struct iwl3945_sta_priv *psta = (void *) sta->drv_priv;
  375. struct iwl3945_rs_sta *rs_sta = priv_sta;
  376. struct iwl_priv *priv __maybe_unused = rs_sta->priv;
  377. psta->rs_sta = NULL;
  378. IWL_DEBUG_RATE(priv, "enter\n");
  379. del_timer_sync(&rs_sta->rate_scale_flush);
  380. kfree(rs_sta);
  381. IWL_DEBUG_RATE(priv, "leave\n");
  382. }
  383. /**
  384. * rs_tx_status - Update rate control values based on Tx results
  385. *
  386. * NOTE: Uses iwl_priv->retry_rate for the # of retries attempted by
  387. * the hardware for each rate.
  388. */
  389. static void rs_tx_status(void *priv_rate, struct ieee80211_supported_band *sband,
  390. struct ieee80211_sta *sta, void *priv_sta,
  391. struct sk_buff *skb)
  392. {
  393. s8 retries = 0, current_count;
  394. int scale_rate_index, first_index, last_index;
  395. unsigned long flags;
  396. struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
  397. struct iwl3945_rs_sta *rs_sta = priv_sta;
  398. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  399. IWL_DEBUG_RATE(priv, "enter\n");
  400. retries = info->status.rates[0].count;
  401. /* Sanity Check for retries */
  402. if (retries > IWL_RATE_RETRY_TH)
  403. retries = IWL_RATE_RETRY_TH;
  404. first_index = sband->bitrates[info->status.rates[0].idx].hw_value;
  405. if ((first_index < 0) || (first_index >= IWL_RATE_COUNT_3945)) {
  406. IWL_DEBUG_RATE(priv, "leave: Rate out of bounds: %d\n", first_index);
  407. return;
  408. }
  409. if (!priv_sta) {
  410. IWL_DEBUG_RATE(priv, "leave: No STA priv data to update!\n");
  411. return;
  412. }
  413. rs_sta->tx_packets++;
  414. scale_rate_index = first_index;
  415. last_index = first_index;
  416. /*
  417. * Update the window for each rate. We determine which rates
  418. * were Tx'd based on the total number of retries vs. the number
  419. * of retries configured for each rate -- currently set to the
  420. * priv value 'retry_rate' vs. rate specific
  421. *
  422. * On exit from this while loop last_index indicates the rate
  423. * at which the frame was finally transmitted (or failed if no
  424. * ACK)
  425. */
  426. while (retries > 1) {
  427. if ((retries - 1) < priv->retry_rate) {
  428. current_count = (retries - 1);
  429. last_index = scale_rate_index;
  430. } else {
  431. current_count = priv->retry_rate;
  432. last_index = iwl3945_rs_next_rate(priv,
  433. scale_rate_index);
  434. }
  435. /* Update this rate accounting for as many retries
  436. * as was used for it (per current_count) */
  437. iwl3945_collect_tx_data(rs_sta,
  438. &rs_sta->win[scale_rate_index],
  439. 0, current_count, scale_rate_index);
  440. IWL_DEBUG_RATE(priv, "Update rate %d for %d retries.\n",
  441. scale_rate_index, current_count);
  442. retries -= current_count;
  443. scale_rate_index = last_index;
  444. }
  445. /* Update the last index window with success/failure based on ACK */
  446. IWL_DEBUG_RATE(priv, "Update rate %d with %s.\n",
  447. last_index,
  448. (info->flags & IEEE80211_TX_STAT_ACK) ?
  449. "success" : "failure");
  450. iwl3945_collect_tx_data(rs_sta,
  451. &rs_sta->win[last_index],
  452. info->flags & IEEE80211_TX_STAT_ACK, 1, last_index);
  453. /* We updated the rate scale window -- if its been more than
  454. * flush_time since the last run, schedule the flush
  455. * again */
  456. spin_lock_irqsave(&rs_sta->lock, flags);
  457. if (!rs_sta->flush_pending &&
  458. time_after(jiffies, rs_sta->last_flush +
  459. rs_sta->flush_time)) {
  460. rs_sta->last_partial_flush = jiffies;
  461. rs_sta->flush_pending = 1;
  462. mod_timer(&rs_sta->rate_scale_flush,
  463. jiffies + rs_sta->flush_time);
  464. }
  465. spin_unlock_irqrestore(&rs_sta->lock, flags);
  466. IWL_DEBUG_RATE(priv, "leave\n");
  467. return;
  468. }
  469. static u16 iwl3945_get_adjacent_rate(struct iwl3945_rs_sta *rs_sta,
  470. u8 index, u16 rate_mask, enum ieee80211_band band)
  471. {
  472. u8 high = IWL_RATE_INVALID;
  473. u8 low = IWL_RATE_INVALID;
  474. struct iwl_priv *priv __maybe_unused = rs_sta->priv;
  475. /* 802.11A walks to the next literal adjacent rate in
  476. * the rate table */
  477. if (unlikely(band == IEEE80211_BAND_5GHZ)) {
  478. int i;
  479. u32 mask;
  480. /* Find the previous rate that is in the rate mask */
  481. i = index - 1;
  482. for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
  483. if (rate_mask & mask) {
  484. low = i;
  485. break;
  486. }
  487. }
  488. /* Find the next rate that is in the rate mask */
  489. i = index + 1;
  490. for (mask = (1 << i); i < IWL_RATE_COUNT_3945;
  491. i++, mask <<= 1) {
  492. if (rate_mask & mask) {
  493. high = i;
  494. break;
  495. }
  496. }
  497. return (high << 8) | low;
  498. }
  499. low = index;
  500. while (low != IWL_RATE_INVALID) {
  501. if (rs_sta->tgg)
  502. low = iwl3945_rates[low].prev_rs_tgg;
  503. else
  504. low = iwl3945_rates[low].prev_rs;
  505. if (low == IWL_RATE_INVALID)
  506. break;
  507. if (rate_mask & (1 << low))
  508. break;
  509. IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
  510. }
  511. high = index;
  512. while (high != IWL_RATE_INVALID) {
  513. if (rs_sta->tgg)
  514. high = iwl3945_rates[high].next_rs_tgg;
  515. else
  516. high = iwl3945_rates[high].next_rs;
  517. if (high == IWL_RATE_INVALID)
  518. break;
  519. if (rate_mask & (1 << high))
  520. break;
  521. IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
  522. }
  523. return (high << 8) | low;
  524. }
  525. /**
  526. * rs_get_rate - find the rate for the requested packet
  527. *
  528. * Returns the ieee80211_rate structure allocated by the driver.
  529. *
  530. * The rate control algorithm has no internal mapping between hw_mode's
  531. * rate ordering and the rate ordering used by the rate control algorithm.
  532. *
  533. * The rate control algorithm uses a single table of rates that goes across
  534. * the entire A/B/G spectrum vs. being limited to just one particular
  535. * hw_mode.
  536. *
  537. * As such, we can't convert the index obtained below into the hw_mode's
  538. * rate table and must reference the driver allocated rate table
  539. *
  540. */
  541. static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta,
  542. void *priv_sta, struct ieee80211_tx_rate_control *txrc)
  543. {
  544. struct ieee80211_supported_band *sband = txrc->sband;
  545. struct sk_buff *skb = txrc->skb;
  546. u8 low = IWL_RATE_INVALID;
  547. u8 high = IWL_RATE_INVALID;
  548. u16 high_low;
  549. int index;
  550. struct iwl3945_rs_sta *rs_sta = priv_sta;
  551. struct iwl3945_rate_scale_data *window = NULL;
  552. int current_tpt = IWL_INVALID_VALUE;
  553. int low_tpt = IWL_INVALID_VALUE;
  554. int high_tpt = IWL_INVALID_VALUE;
  555. u32 fail_count;
  556. s8 scale_action = 0;
  557. unsigned long flags;
  558. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  559. u16 rate_mask = sta ? sta->supp_rates[sband->band] : 0;
  560. s8 max_rate_idx = -1;
  561. struct iwl_priv *priv = (struct iwl_priv *)priv_r;
  562. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  563. IWL_DEBUG_RATE(priv, "enter\n");
  564. if (rate_control_send_low(sta, priv_sta, txrc))
  565. return;
  566. rate_mask = sta->supp_rates[sband->band];
  567. /* get user max rate if set */
  568. max_rate_idx = txrc->max_rate_idx;
  569. if ((sband->band == IEEE80211_BAND_5GHZ) && (max_rate_idx != -1))
  570. max_rate_idx += IWL_FIRST_OFDM_RATE;
  571. if ((max_rate_idx < 0) || (max_rate_idx >= IWL_RATE_COUNT))
  572. max_rate_idx = -1;
  573. index = min(rs_sta->last_txrate_idx & 0xffff, IWL_RATE_COUNT_3945 - 1);
  574. if (sband->band == IEEE80211_BAND_5GHZ)
  575. rate_mask = rate_mask << IWL_FIRST_OFDM_RATE;
  576. if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
  577. !rs_sta->ibss_sta_added) {
  578. u8 sta_id = iwl_find_station(priv, hdr->addr1);
  579. if (sta_id == IWL_INVALID_STATION) {
  580. IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n",
  581. hdr->addr1);
  582. sta_id = iwl_add_station(priv, hdr->addr1, false,
  583. CMD_ASYNC, NULL);
  584. }
  585. if (sta_id != IWL_INVALID_STATION)
  586. rs_sta->ibss_sta_added = 1;
  587. }
  588. spin_lock_irqsave(&rs_sta->lock, flags);
  589. /* for recent assoc, choose best rate regarding
  590. * to rssi value
  591. */
  592. if (rs_sta->start_rate != IWL_RATE_INVALID) {
  593. if (rs_sta->start_rate < index &&
  594. (rate_mask & (1 << rs_sta->start_rate)))
  595. index = rs_sta->start_rate;
  596. rs_sta->start_rate = IWL_RATE_INVALID;
  597. }
  598. /* force user max rate if set by user */
  599. if ((max_rate_idx != -1) && (max_rate_idx < index)) {
  600. if (rate_mask & (1 << max_rate_idx))
  601. index = max_rate_idx;
  602. }
  603. window = &(rs_sta->win[index]);
  604. fail_count = window->counter - window->success_counter;
  605. if (((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
  606. (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))) {
  607. spin_unlock_irqrestore(&rs_sta->lock, flags);
  608. IWL_DEBUG_RATE(priv, "Invalid average_tpt on rate %d: "
  609. "counter: %d, success_counter: %d, "
  610. "expected_tpt is %sNULL\n",
  611. index,
  612. window->counter,
  613. window->success_counter,
  614. rs_sta->expected_tpt ? "not " : "");
  615. /* Can't calculate this yet; not enough history */
  616. window->average_tpt = IWL_INVALID_VALUE;
  617. goto out;
  618. }
  619. current_tpt = window->average_tpt;
  620. high_low = iwl3945_get_adjacent_rate(rs_sta, index, rate_mask,
  621. sband->band);
  622. low = high_low & 0xff;
  623. high = (high_low >> 8) & 0xff;
  624. /* If user set max rate, dont allow higher than user constrain */
  625. if ((max_rate_idx != -1) && (max_rate_idx < high))
  626. high = IWL_RATE_INVALID;
  627. /* Collect Measured throughputs of adjacent rates */
  628. if (low != IWL_RATE_INVALID)
  629. low_tpt = rs_sta->win[low].average_tpt;
  630. if (high != IWL_RATE_INVALID)
  631. high_tpt = rs_sta->win[high].average_tpt;
  632. spin_unlock_irqrestore(&rs_sta->lock, flags);
  633. scale_action = 0;
  634. /* Low success ratio , need to drop the rate */
  635. if ((window->success_ratio < IWL_RATE_DECREASE_TH) || !current_tpt) {
  636. IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
  637. scale_action = -1;
  638. /* No throughput measured yet for adjacent rates,
  639. * try increase */
  640. } else if ((low_tpt == IWL_INVALID_VALUE) &&
  641. (high_tpt == IWL_INVALID_VALUE)) {
  642. if (high != IWL_RATE_INVALID && window->success_ratio >= IWL_RATE_INCREASE_TH)
  643. scale_action = 1;
  644. else if (low != IWL_RATE_INVALID)
  645. scale_action = 0;
  646. /* Both adjacent throughputs are measured, but neither one has
  647. * better throughput; we're using the best rate, don't change
  648. * it! */
  649. } else if ((low_tpt != IWL_INVALID_VALUE) &&
  650. (high_tpt != IWL_INVALID_VALUE) &&
  651. (low_tpt < current_tpt) && (high_tpt < current_tpt)) {
  652. IWL_DEBUG_RATE(priv, "No action -- low [%d] & high [%d] < "
  653. "current_tpt [%d]\n",
  654. low_tpt, high_tpt, current_tpt);
  655. scale_action = 0;
  656. /* At least one of the rates has better throughput */
  657. } else {
  658. if (high_tpt != IWL_INVALID_VALUE) {
  659. /* High rate has better throughput, Increase
  660. * rate */
  661. if (high_tpt > current_tpt &&
  662. window->success_ratio >= IWL_RATE_INCREASE_TH)
  663. scale_action = 1;
  664. else {
  665. IWL_DEBUG_RATE(priv,
  666. "decrease rate because of high tpt\n");
  667. scale_action = 0;
  668. }
  669. } else if (low_tpt != IWL_INVALID_VALUE) {
  670. if (low_tpt > current_tpt) {
  671. IWL_DEBUG_RATE(priv,
  672. "decrease rate because of low tpt\n");
  673. scale_action = -1;
  674. } else if (window->success_ratio >= IWL_RATE_INCREASE_TH) {
  675. /* Lower rate has better
  676. * throughput,decrease rate */
  677. scale_action = 1;
  678. }
  679. }
  680. }
  681. /* Sanity check; asked for decrease, but success rate or throughput
  682. * has been good at old rate. Don't change it. */
  683. if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
  684. ((window->success_ratio > IWL_RATE_HIGH_TH) ||
  685. (current_tpt > (100 * rs_sta->expected_tpt[low]))))
  686. scale_action = 0;
  687. switch (scale_action) {
  688. case -1:
  689. /* Decrese rate */
  690. if (low != IWL_RATE_INVALID)
  691. index = low;
  692. break;
  693. case 1:
  694. /* Increase rate */
  695. if (high != IWL_RATE_INVALID)
  696. index = high;
  697. break;
  698. case 0:
  699. default:
  700. /* No change */
  701. break;
  702. }
  703. IWL_DEBUG_RATE(priv, "Selected %d (action %d) - low %d high %d\n",
  704. index, scale_action, low, high);
  705. out:
  706. rs_sta->last_txrate_idx = index;
  707. if (sband->band == IEEE80211_BAND_5GHZ)
  708. info->control.rates[0].idx = rs_sta->last_txrate_idx -
  709. IWL_FIRST_OFDM_RATE;
  710. else
  711. info->control.rates[0].idx = rs_sta->last_txrate_idx;
  712. IWL_DEBUG_RATE(priv, "leave: %d\n", index);
  713. }
  714. #ifdef CONFIG_MAC80211_DEBUGFS
  715. static int iwl3945_open_file_generic(struct inode *inode, struct file *file)
  716. {
  717. file->private_data = inode->i_private;
  718. return 0;
  719. }
  720. static ssize_t iwl3945_sta_dbgfs_stats_table_read(struct file *file,
  721. char __user *user_buf,
  722. size_t count, loff_t *ppos)
  723. {
  724. char *buff;
  725. int desc = 0;
  726. int j;
  727. ssize_t ret;
  728. struct iwl3945_rs_sta *lq_sta = file->private_data;
  729. buff = kmalloc(1024, GFP_KERNEL);
  730. if (!buff)
  731. return -ENOMEM;
  732. desc += sprintf(buff + desc, "tx packets=%d last rate index=%d\n"
  733. "rate=0x%X flush time %d\n",
  734. lq_sta->tx_packets,
  735. lq_sta->last_txrate_idx,
  736. lq_sta->start_rate, jiffies_to_msecs(lq_sta->flush_time));
  737. for (j = 0; j < IWL_RATE_COUNT_3945; j++) {
  738. desc += sprintf(buff+desc,
  739. "counter=%d success=%d %%=%d\n",
  740. lq_sta->win[j].counter,
  741. lq_sta->win[j].success_counter,
  742. lq_sta->win[j].success_ratio);
  743. }
  744. ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
  745. kfree(buff);
  746. return ret;
  747. }
  748. static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
  749. .read = iwl3945_sta_dbgfs_stats_table_read,
  750. .open = iwl3945_open_file_generic,
  751. };
  752. static void iwl3945_add_debugfs(void *priv, void *priv_sta,
  753. struct dentry *dir)
  754. {
  755. struct iwl3945_rs_sta *lq_sta = priv_sta;
  756. lq_sta->rs_sta_dbgfs_stats_table_file =
  757. debugfs_create_file("rate_stats_table", 0600, dir,
  758. lq_sta, &rs_sta_dbgfs_stats_table_ops);
  759. }
  760. static void iwl3945_remove_debugfs(void *priv, void *priv_sta)
  761. {
  762. struct iwl3945_rs_sta *lq_sta = priv_sta;
  763. debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
  764. }
  765. #endif
  766. static struct rate_control_ops rs_ops = {
  767. .module = NULL,
  768. .name = RS_NAME,
  769. .tx_status = rs_tx_status,
  770. .get_rate = rs_get_rate,
  771. .rate_init = rs_rate_init,
  772. .alloc = rs_alloc,
  773. .free = rs_free,
  774. .alloc_sta = rs_alloc_sta,
  775. .free_sta = rs_free_sta,
  776. #ifdef CONFIG_MAC80211_DEBUGFS
  777. .add_sta_debugfs = iwl3945_add_debugfs,
  778. .remove_sta_debugfs = iwl3945_remove_debugfs,
  779. #endif
  780. };
  781. void iwl3945_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
  782. {
  783. struct iwl_priv *priv = hw->priv;
  784. s32 rssi = 0;
  785. unsigned long flags;
  786. struct iwl3945_rs_sta *rs_sta;
  787. struct ieee80211_sta *sta;
  788. struct iwl3945_sta_priv *psta;
  789. IWL_DEBUG_RATE(priv, "enter\n");
  790. rcu_read_lock();
  791. sta = ieee80211_find_sta(hw, priv->stations[sta_id].sta.sta.addr);
  792. if (!sta) {
  793. rcu_read_unlock();
  794. return;
  795. }
  796. psta = (void *) sta->drv_priv;
  797. rs_sta = psta->rs_sta;
  798. spin_lock_irqsave(&rs_sta->lock, flags);
  799. rs_sta->tgg = 0;
  800. switch (priv->band) {
  801. case IEEE80211_BAND_2GHZ:
  802. /* TODO: this always does G, not a regression */
  803. if (priv->active_rxon.flags & RXON_FLG_TGG_PROTECT_MSK) {
  804. rs_sta->tgg = 1;
  805. rs_sta->expected_tpt = iwl3945_expected_tpt_g_prot;
  806. } else
  807. rs_sta->expected_tpt = iwl3945_expected_tpt_g;
  808. break;
  809. case IEEE80211_BAND_5GHZ:
  810. rs_sta->expected_tpt = iwl3945_expected_tpt_a;
  811. break;
  812. case IEEE80211_NUM_BANDS:
  813. BUG();
  814. break;
  815. }
  816. spin_unlock_irqrestore(&rs_sta->lock, flags);
  817. rssi = priv->last_rx_rssi;
  818. if (rssi == 0)
  819. rssi = IWL_MIN_RSSI_VAL;
  820. IWL_DEBUG_RATE(priv, "Network RSSI: %d\n", rssi);
  821. rs_sta->start_rate = iwl3945_get_rate_index_by_rssi(rssi, priv->band);
  822. IWL_DEBUG_RATE(priv, "leave: rssi %d assign rate index: "
  823. "%d (plcp 0x%x)\n", rssi, rs_sta->start_rate,
  824. iwl3945_rates[rs_sta->start_rate].plcp);
  825. rcu_read_unlock();
  826. }
  827. int iwl3945_rate_control_register(void)
  828. {
  829. return ieee80211_rate_control_register(&rs_ops);
  830. }
  831. void iwl3945_rate_control_unregister(void)
  832. {
  833. ieee80211_rate_control_unregister(&rs_ops);
  834. }