iwl-3945-rs.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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 struct ieee80211_rate *iwl_get_lowest_rate(struct ieee80211_local
  457. *local)
  458. {
  459. struct ieee80211_hw_mode *mode = local->oper_hw_mode;
  460. int i;
  461. for (i = 0; i < mode->num_rates; i++) {
  462. struct ieee80211_rate *rate = &mode->rates[i];
  463. if (rate->flags & IEEE80211_RATE_SUPPORTED)
  464. return rate;
  465. }
  466. return &mode->rates[0];
  467. }
  468. static u16 iwl_get_adjacent_rate(struct iwl_rate_scale_priv *rs_priv,
  469. u8 index, u16 rate_mask, int phymode)
  470. {
  471. u8 high = IWL_RATE_INVALID;
  472. u8 low = IWL_RATE_INVALID;
  473. /* 802.11A walks to the next literal adjascent rate in
  474. * the rate table */
  475. if (unlikely(phymode == MODE_IEEE80211A)) {
  476. int i;
  477. u32 mask;
  478. /* Find the previous rate that is in the rate mask */
  479. i = index - 1;
  480. for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
  481. if (rate_mask & mask) {
  482. low = i;
  483. break;
  484. }
  485. }
  486. /* Find the next rate that is in the rate mask */
  487. i = index + 1;
  488. for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
  489. if (rate_mask & mask) {
  490. high = i;
  491. break;
  492. }
  493. }
  494. return (high << 8) | low;
  495. }
  496. low = index;
  497. while (low != IWL_RATE_INVALID) {
  498. if (rs_priv->tgg)
  499. low = iwl_rates[low].prev_rs_tgg;
  500. else
  501. low = iwl_rates[low].prev_rs;
  502. if (low == IWL_RATE_INVALID)
  503. break;
  504. if (rate_mask & (1 << low))
  505. break;
  506. IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
  507. }
  508. high = index;
  509. while (high != IWL_RATE_INVALID) {
  510. if (rs_priv->tgg)
  511. high = iwl_rates[high].next_rs_tgg;
  512. else
  513. high = iwl_rates[high].next_rs;
  514. if (high == IWL_RATE_INVALID)
  515. break;
  516. if (rate_mask & (1 << high))
  517. break;
  518. IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
  519. }
  520. return (high << 8) | low;
  521. }
  522. /**
  523. * rs_get_rate - find the rate for the requested packet
  524. *
  525. * Returns the ieee80211_rate structure allocated by the driver.
  526. *
  527. * The rate control algorithm has no internal mapping between hw_mode's
  528. * rate ordering and the rate ordering used by the rate control algorithm.
  529. *
  530. * The rate control algorithm uses a single table of rates that goes across
  531. * the entire A/B/G spectrum vs. being limited to just one particular
  532. * hw_mode.
  533. *
  534. * As such, we can't convert the index obtained below into the hw_mode's
  535. * rate table and must reference the driver allocated rate table
  536. *
  537. */
  538. static struct ieee80211_rate *rs_get_rate(void *priv_rate,
  539. struct net_device *dev,
  540. struct sk_buff *skb,
  541. struct rate_control_extra *extra)
  542. {
  543. u8 low = IWL_RATE_INVALID;
  544. u8 high = IWL_RATE_INVALID;
  545. u16 high_low;
  546. int index;
  547. struct iwl_rate_scale_priv *rs_priv;
  548. struct iwl_rate_scale_data *window = NULL;
  549. int current_tpt = IWL_INVALID_VALUE;
  550. int low_tpt = IWL_INVALID_VALUE;
  551. int high_tpt = IWL_INVALID_VALUE;
  552. u32 fail_count;
  553. s8 scale_action = 0;
  554. unsigned long flags;
  555. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  556. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  557. struct sta_info *sta;
  558. u16 fc, rate_mask;
  559. struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
  560. DECLARE_MAC_BUF(mac);
  561. IWL_DEBUG_RATE("enter\n");
  562. memset(extra, 0, sizeof(*extra));
  563. fc = le16_to_cpu(hdr->frame_control);
  564. if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
  565. (is_multicast_ether_addr(hdr->addr1))) {
  566. /* Send management frames and broadcast/multicast data using
  567. * lowest rate. */
  568. /* TODO: this could probably be improved.. */
  569. IWL_DEBUG_RATE("leave: lowest rate (not data or is "
  570. "multicast)\n");
  571. return iwl_get_lowest_rate(local);
  572. }
  573. sta = sta_info_get(local, hdr->addr1);
  574. if (!sta || !sta->rate_ctrl_priv) {
  575. IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
  576. if (sta)
  577. sta_info_put(sta);
  578. return NULL;
  579. }
  580. rate_mask = sta->supp_rates;
  581. index = min(sta->last_txrate & 0xffff, IWL_RATE_COUNT - 1);
  582. if (priv->phymode == (u8) MODE_IEEE80211A)
  583. rate_mask = rate_mask << IWL_FIRST_OFDM_RATE;
  584. rs_priv = (void *)sta->rate_ctrl_priv;
  585. if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
  586. !rs_priv->ibss_sta_added) {
  587. u8 sta_id = iwl_hw_find_station(priv, hdr->addr1);
  588. if (sta_id == IWL_INVALID_STATION) {
  589. IWL_DEBUG_RATE("LQ: ADD station %s\n",
  590. print_mac(mac, hdr->addr1));
  591. sta_id = iwl_add_station(priv,
  592. hdr->addr1, 0, CMD_ASYNC);
  593. }
  594. if (sta_id != IWL_INVALID_STATION)
  595. rs_priv->ibss_sta_added = 1;
  596. }
  597. spin_lock_irqsave(&rs_priv->lock, flags);
  598. if (rs_priv->start_rate != IWL_RATE_INVALID) {
  599. index = rs_priv->start_rate;
  600. rs_priv->start_rate = IWL_RATE_INVALID;
  601. }
  602. window = &(rs_priv->win[index]);
  603. fail_count = window->counter - window->success_counter;
  604. if (((fail_count <= IWL_RATE_MIN_FAILURE_TH) &&
  605. (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))) {
  606. window->average_tpt = IWL_INVALID_VALUE;
  607. spin_unlock_irqrestore(&rs_priv->lock, flags);
  608. IWL_DEBUG_RATE("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_priv->expected_tpt ? "not " : "");
  615. goto out;
  616. }
  617. window->average_tpt = ((window->success_ratio *
  618. rs_priv->expected_tpt[index] + 64) / 128);
  619. current_tpt = window->average_tpt;
  620. high_low = iwl_get_adjacent_rate(rs_priv, index, rate_mask,
  621. local->hw.conf.phymode);
  622. low = high_low & 0xff;
  623. high = (high_low >> 8) & 0xff;
  624. if (low != IWL_RATE_INVALID)
  625. low_tpt = rs_priv->win[low].average_tpt;
  626. if (high != IWL_RATE_INVALID)
  627. high_tpt = rs_priv->win[high].average_tpt;
  628. spin_unlock_irqrestore(&rs_priv->lock, flags);
  629. scale_action = 1;
  630. if ((window->success_ratio < IWL_RATE_DECREASE_TH) || !current_tpt) {
  631. IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
  632. scale_action = -1;
  633. } else if ((low_tpt == IWL_INVALID_VALUE) &&
  634. (high_tpt == IWL_INVALID_VALUE))
  635. scale_action = 1;
  636. else if ((low_tpt != IWL_INVALID_VALUE) &&
  637. (high_tpt != IWL_INVALID_VALUE)
  638. && (low_tpt < current_tpt)
  639. && (high_tpt < current_tpt)) {
  640. IWL_DEBUG_RATE("No action -- low [%d] & high [%d] < "
  641. "current_tpt [%d]\n",
  642. low_tpt, high_tpt, current_tpt);
  643. scale_action = 0;
  644. } else {
  645. if (high_tpt != IWL_INVALID_VALUE) {
  646. if (high_tpt > current_tpt)
  647. scale_action = 1;
  648. else {
  649. IWL_DEBUG_RATE
  650. ("decrease rate because of high tpt\n");
  651. scale_action = -1;
  652. }
  653. } else if (low_tpt != IWL_INVALID_VALUE) {
  654. if (low_tpt > current_tpt) {
  655. IWL_DEBUG_RATE
  656. ("decrease rate because of low tpt\n");
  657. scale_action = -1;
  658. } else
  659. scale_action = 1;
  660. }
  661. }
  662. if ((window->success_ratio > IWL_RATE_HIGH_TH) ||
  663. (current_tpt > window->average_tpt)) {
  664. IWL_DEBUG_RATE("No action -- success_ratio [%d] > HIGH_TH or "
  665. "current_tpt [%d] > average_tpt [%d]\n",
  666. window->success_ratio,
  667. current_tpt, window->average_tpt);
  668. scale_action = 0;
  669. }
  670. switch (scale_action) {
  671. case -1:
  672. if (low != IWL_RATE_INVALID)
  673. index = low;
  674. break;
  675. case 1:
  676. if (high != IWL_RATE_INVALID)
  677. index = high;
  678. break;
  679. case 0:
  680. default:
  681. break;
  682. }
  683. IWL_DEBUG_RATE("Selected %d (action %d) - low %d high %d\n",
  684. index, scale_action, low, high);
  685. out:
  686. sta->last_txrate = index;
  687. if (priv->phymode == (u8) MODE_IEEE80211A)
  688. sta->txrate = sta->last_txrate - IWL_FIRST_OFDM_RATE;
  689. else
  690. sta->txrate = sta->last_txrate;
  691. sta_info_put(sta);
  692. IWL_DEBUG_RATE("leave: %d\n", index);
  693. return &priv->ieee_rates[index];
  694. }
  695. static struct rate_control_ops rs_ops = {
  696. .module = NULL,
  697. .name = RS_NAME,
  698. .tx_status = rs_tx_status,
  699. .get_rate = rs_get_rate,
  700. .rate_init = rs_rate_init,
  701. .clear = rs_clear,
  702. .alloc = rs_alloc,
  703. .free = rs_free,
  704. .alloc_sta = rs_alloc_sta,
  705. .free_sta = rs_free_sta,
  706. };
  707. int iwl_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
  708. {
  709. struct ieee80211_local *local = hw_to_local(hw);
  710. struct iwl_priv *priv = hw->priv;
  711. struct iwl_rate_scale_priv *rs_priv;
  712. struct sta_info *sta;
  713. unsigned long flags;
  714. int count = 0, i;
  715. u32 samples = 0, success = 0, good = 0;
  716. unsigned long now = jiffies;
  717. u32 max_time = 0;
  718. sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
  719. if (!sta || !sta->rate_ctrl_priv) {
  720. if (sta) {
  721. sta_info_put(sta);
  722. IWL_DEBUG_RATE("leave - no private rate data!\n");
  723. } else
  724. IWL_DEBUG_RATE("leave - no station!\n");
  725. return sprintf(buf, "station %d not found\n", sta_id);
  726. }
  727. rs_priv = (void *)sta->rate_ctrl_priv;
  728. spin_lock_irqsave(&rs_priv->lock, flags);
  729. i = IWL_RATE_54M_INDEX;
  730. while (1) {
  731. u64 mask;
  732. int j;
  733. count +=
  734. sprintf(&buf[count], " %2dMbs: ", iwl_rates[i].ieee / 2);
  735. mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
  736. for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
  737. buf[count++] =
  738. (rs_priv->win[i].data & mask) ? '1' : '0';
  739. samples += rs_priv->win[i].counter;
  740. good += rs_priv->win[i].success_counter;
  741. success += rs_priv->win[i].success_counter * iwl_rates[i].ieee;
  742. if (rs_priv->win[i].stamp) {
  743. int delta =
  744. jiffies_to_msecs(now - rs_priv->win[i].stamp);
  745. if (delta > max_time)
  746. max_time = delta;
  747. count += sprintf(&buf[count], "%5dms\n", delta);
  748. } else
  749. buf[count++] = '\n';
  750. j = iwl_get_prev_ieee_rate(i);
  751. if (j == i)
  752. break;
  753. i = j;
  754. }
  755. spin_unlock_irqrestore(&rs_priv->lock, flags);
  756. sta_info_put(sta);
  757. /* Display the average rate of all samples taken.
  758. *
  759. * NOTE: We multiple # of samples by 2 since the IEEE measurement
  760. * added from iwl_rates is actually 2X the rate */
  761. if (samples)
  762. count += sprintf(
  763. &buf[count],
  764. "\nAverage rate is %3d.%02dMbs over last %4dms\n"
  765. "%3d%% success (%d good packets over %d tries)\n",
  766. success / (2 * samples), (success * 5 / samples) % 10,
  767. max_time, good * 100 / samples, good, samples);
  768. else
  769. count += sprintf(&buf[count], "\nAverage rate: 0Mbs\n");
  770. return count;
  771. }
  772. void iwl_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
  773. {
  774. struct iwl_priv *priv = hw->priv;
  775. s32 rssi = 0;
  776. unsigned long flags;
  777. struct ieee80211_local *local = hw_to_local(hw);
  778. struct iwl_rate_scale_priv *rs_priv;
  779. struct sta_info *sta;
  780. IWL_DEBUG_RATE("enter\n");
  781. if (!local->rate_ctrl->ops->name ||
  782. strcmp(local->rate_ctrl->ops->name, RS_NAME)) {
  783. IWL_WARNING("iwl-3945-rs not selected as rate control algo!\n");
  784. IWL_DEBUG_RATE("leave - mac80211 picked the wrong RC algo.\n");
  785. return;
  786. }
  787. sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
  788. if (!sta || !sta->rate_ctrl_priv) {
  789. if (sta)
  790. sta_info_put(sta);
  791. IWL_DEBUG_RATE("leave - no private rate data!\n");
  792. return;
  793. }
  794. rs_priv = (void *)sta->rate_ctrl_priv;
  795. spin_lock_irqsave(&rs_priv->lock, flags);
  796. rs_priv->tgg = 0;
  797. switch (priv->phymode) {
  798. case MODE_IEEE80211G:
  799. if (priv->active_rxon.flags & RXON_FLG_TGG_PROTECT_MSK) {
  800. rs_priv->tgg = 1;
  801. rs_priv->expected_tpt = iwl_expected_tpt_g_prot;
  802. } else
  803. rs_priv->expected_tpt = iwl_expected_tpt_g;
  804. break;
  805. case MODE_IEEE80211A:
  806. rs_priv->expected_tpt = iwl_expected_tpt_a;
  807. break;
  808. default:
  809. IWL_WARNING("Invalid phymode. Defaulting to 802.11b\n");
  810. case MODE_IEEE80211B:
  811. rs_priv->expected_tpt = iwl_expected_tpt_b;
  812. break;
  813. }
  814. sta_info_put(sta);
  815. spin_unlock_irqrestore(&rs_priv->lock, flags);
  816. rssi = priv->last_rx_rssi;
  817. if (rssi == 0)
  818. rssi = IWL_MIN_RSSI_VAL;
  819. IWL_DEBUG(IWL_DL_INFO | IWL_DL_RATE, "Network RSSI: %d\n", rssi);
  820. rs_priv->start_rate = iwl_get_rate_index_by_rssi(rssi, priv->phymode);
  821. IWL_DEBUG_RATE("leave: rssi %d assign rate index: "
  822. "%d (plcp 0x%x)\n", rssi, rs_priv->start_rate,
  823. iwl_rates[rs_priv->start_rate].plcp);
  824. }
  825. void iwl_rate_control_register(struct ieee80211_hw *hw)
  826. {
  827. ieee80211_rate_control_register(&rs_ops);
  828. }
  829. void iwl_rate_control_unregister(struct ieee80211_hw *hw)
  830. {
  831. ieee80211_rate_control_unregister(&rs_ops);
  832. }