rt2x00link.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. Copyright (C) 2004 - 2009 Ivo van Doorn <[email protected]>
  4. <http://rt2x00.serialmonkey.com>
  5. */
  6. /*
  7. Module: rt2x00lib
  8. Abstract: rt2x00 generic link tuning routines.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include "rt2x00.h"
  13. #include "rt2x00lib.h"
  14. /*
  15. * When we lack RSSI information return something less then -80 to
  16. * tell the driver to tune the device to maximum sensitivity.
  17. */
  18. #define DEFAULT_RSSI -128
  19. static inline int rt2x00link_get_avg_rssi(struct ewma_rssi *ewma)
  20. {
  21. unsigned long avg;
  22. avg = ewma_rssi_read(ewma);
  23. if (avg)
  24. return -avg;
  25. return DEFAULT_RSSI;
  26. }
  27. static int rt2x00link_antenna_get_link_rssi(struct rt2x00_dev *rt2x00dev)
  28. {
  29. struct link_ant *ant = &rt2x00dev->link.ant;
  30. if (rt2x00dev->link.qual.rx_success)
  31. return rt2x00link_get_avg_rssi(&ant->rssi_ant);
  32. return DEFAULT_RSSI;
  33. }
  34. static int rt2x00link_antenna_get_rssi_history(struct rt2x00_dev *rt2x00dev)
  35. {
  36. struct link_ant *ant = &rt2x00dev->link.ant;
  37. if (ant->rssi_history)
  38. return ant->rssi_history;
  39. return DEFAULT_RSSI;
  40. }
  41. static void rt2x00link_antenna_update_rssi_history(struct rt2x00_dev *rt2x00dev,
  42. int rssi)
  43. {
  44. struct link_ant *ant = &rt2x00dev->link.ant;
  45. ant->rssi_history = rssi;
  46. }
  47. static void rt2x00link_antenna_reset(struct rt2x00_dev *rt2x00dev)
  48. {
  49. ewma_rssi_init(&rt2x00dev->link.ant.rssi_ant);
  50. }
  51. static void rt2x00lib_antenna_diversity_sample(struct rt2x00_dev *rt2x00dev)
  52. {
  53. struct link_ant *ant = &rt2x00dev->link.ant;
  54. struct antenna_setup new_ant;
  55. int other_antenna;
  56. int sample_current = rt2x00link_antenna_get_link_rssi(rt2x00dev);
  57. int sample_other = rt2x00link_antenna_get_rssi_history(rt2x00dev);
  58. memcpy(&new_ant, &ant->active, sizeof(new_ant));
  59. /*
  60. * We are done sampling. Now we should evaluate the results.
  61. */
  62. ant->flags &= ~ANTENNA_MODE_SAMPLE;
  63. /*
  64. * During the last period we have sampled the RSSI
  65. * from both antennas. It now is time to determine
  66. * which antenna demonstrated the best performance.
  67. * When we are already on the antenna with the best
  68. * performance, just create a good starting point
  69. * for the history and we are done.
  70. */
  71. if (sample_current >= sample_other) {
  72. rt2x00link_antenna_update_rssi_history(rt2x00dev,
  73. sample_current);
  74. return;
  75. }
  76. other_antenna = (ant->active.rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
  77. if (ant->flags & ANTENNA_RX_DIVERSITY)
  78. new_ant.rx = other_antenna;
  79. if (ant->flags & ANTENNA_TX_DIVERSITY)
  80. new_ant.tx = other_antenna;
  81. rt2x00lib_config_antenna(rt2x00dev, new_ant);
  82. }
  83. static void rt2x00lib_antenna_diversity_eval(struct rt2x00_dev *rt2x00dev)
  84. {
  85. struct link_ant *ant = &rt2x00dev->link.ant;
  86. struct antenna_setup new_ant;
  87. int rssi_curr;
  88. int rssi_old;
  89. memcpy(&new_ant, &ant->active, sizeof(new_ant));
  90. /*
  91. * Get current RSSI value along with the historical value,
  92. * after that update the history with the current value.
  93. */
  94. rssi_curr = rt2x00link_antenna_get_link_rssi(rt2x00dev);
  95. rssi_old = rt2x00link_antenna_get_rssi_history(rt2x00dev);
  96. rt2x00link_antenna_update_rssi_history(rt2x00dev, rssi_curr);
  97. /*
  98. * Legacy driver indicates that we should swap antenna's
  99. * when the difference in RSSI is greater that 5. This
  100. * also should be done when the RSSI was actually better
  101. * then the previous sample.
  102. * When the difference exceeds the threshold we should
  103. * sample the rssi from the other antenna to make a valid
  104. * comparison between the 2 antennas.
  105. */
  106. if (abs(rssi_curr - rssi_old) < 5)
  107. return;
  108. ant->flags |= ANTENNA_MODE_SAMPLE;
  109. if (ant->flags & ANTENNA_RX_DIVERSITY)
  110. new_ant.rx = (new_ant.rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
  111. if (ant->flags & ANTENNA_TX_DIVERSITY)
  112. new_ant.tx = (new_ant.tx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
  113. rt2x00lib_config_antenna(rt2x00dev, new_ant);
  114. }
  115. static bool rt2x00lib_antenna_diversity(struct rt2x00_dev *rt2x00dev)
  116. {
  117. struct link_ant *ant = &rt2x00dev->link.ant;
  118. /*
  119. * Determine if software diversity is enabled for
  120. * either the TX or RX antenna (or both).
  121. */
  122. if (!(ant->flags & ANTENNA_RX_DIVERSITY) &&
  123. !(ant->flags & ANTENNA_TX_DIVERSITY)) {
  124. ant->flags = 0;
  125. return true;
  126. }
  127. /*
  128. * If we have only sampled the data over the last period
  129. * we should now harvest the data. Otherwise just evaluate
  130. * the data. The latter should only be performed once
  131. * every 2 seconds.
  132. */
  133. if (ant->flags & ANTENNA_MODE_SAMPLE) {
  134. rt2x00lib_antenna_diversity_sample(rt2x00dev);
  135. return true;
  136. } else if (rt2x00dev->link.count & 1) {
  137. rt2x00lib_antenna_diversity_eval(rt2x00dev);
  138. return true;
  139. }
  140. return false;
  141. }
  142. void rt2x00link_update_stats(struct rt2x00_dev *rt2x00dev,
  143. struct sk_buff *skb,
  144. struct rxdone_entry_desc *rxdesc)
  145. {
  146. struct link *link = &rt2x00dev->link;
  147. struct link_qual *qual = &rt2x00dev->link.qual;
  148. struct link_ant *ant = &rt2x00dev->link.ant;
  149. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  150. /*
  151. * No need to update the stats for !=STA interfaces
  152. */
  153. if (!rt2x00dev->intf_sta_count)
  154. return;
  155. /*
  156. * Frame was received successfully since non-succesfull
  157. * frames would have been dropped by the hardware.
  158. */
  159. qual->rx_success++;
  160. /*
  161. * We are only interested in quality statistics from
  162. * beacons which came from the BSS which we are
  163. * associated with.
  164. */
  165. if (!ieee80211_is_beacon(hdr->frame_control) ||
  166. !(rxdesc->dev_flags & RXDONE_MY_BSS))
  167. return;
  168. /*
  169. * Update global RSSI
  170. */
  171. ewma_rssi_add(&link->avg_rssi, -rxdesc->rssi);
  172. /*
  173. * Update antenna RSSI
  174. */
  175. ewma_rssi_add(&ant->rssi_ant, -rxdesc->rssi);
  176. }
  177. void rt2x00link_start_tuner(struct rt2x00_dev *rt2x00dev)
  178. {
  179. struct link *link = &rt2x00dev->link;
  180. /*
  181. * Single monitor mode interfaces should never have
  182. * work with link tuners.
  183. */
  184. if (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count)
  185. return;
  186. /*
  187. * While scanning, link tuning is disabled. By default
  188. * the most sensitive settings will be used to make sure
  189. * that all beacons and probe responses will be received
  190. * during the scan.
  191. */
  192. if (test_bit(DEVICE_STATE_SCANNING, &rt2x00dev->flags))
  193. return;
  194. rt2x00link_reset_tuner(rt2x00dev, false);
  195. if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  196. ieee80211_queue_delayed_work(rt2x00dev->hw,
  197. &link->work, LINK_TUNE_INTERVAL);
  198. }
  199. void rt2x00link_stop_tuner(struct rt2x00_dev *rt2x00dev)
  200. {
  201. cancel_delayed_work_sync(&rt2x00dev->link.work);
  202. }
  203. void rt2x00link_reset_tuner(struct rt2x00_dev *rt2x00dev, bool antenna)
  204. {
  205. struct link_qual *qual = &rt2x00dev->link.qual;
  206. u8 vgc_level = qual->vgc_level_reg;
  207. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  208. return;
  209. /*
  210. * Reset link information.
  211. * Both the currently active vgc level as well as
  212. * the link tuner counter should be reset. Resetting
  213. * the counter is important for devices where the
  214. * device should only perform link tuning during the
  215. * first minute after being enabled.
  216. */
  217. rt2x00dev->link.count = 0;
  218. memset(qual, 0, sizeof(*qual));
  219. ewma_rssi_init(&rt2x00dev->link.avg_rssi);
  220. /*
  221. * Restore the VGC level as stored in the registers,
  222. * the driver can use this to determine if the register
  223. * must be updated during reset or not.
  224. */
  225. qual->vgc_level_reg = vgc_level;
  226. /*
  227. * Reset the link tuner.
  228. */
  229. rt2x00dev->ops->lib->reset_tuner(rt2x00dev, qual);
  230. if (antenna)
  231. rt2x00link_antenna_reset(rt2x00dev);
  232. }
  233. static void rt2x00link_reset_qual(struct rt2x00_dev *rt2x00dev)
  234. {
  235. struct link_qual *qual = &rt2x00dev->link.qual;
  236. qual->rx_success = 0;
  237. qual->rx_failed = 0;
  238. qual->tx_success = 0;
  239. qual->tx_failed = 0;
  240. }
  241. static void rt2x00link_tuner_sta(struct rt2x00_dev *rt2x00dev, struct link *link)
  242. {
  243. struct link_qual *qual = &rt2x00dev->link.qual;
  244. /*
  245. * Update statistics.
  246. */
  247. rt2x00dev->ops->lib->link_stats(rt2x00dev, qual);
  248. rt2x00dev->low_level_stats.dot11FCSErrorCount += qual->rx_failed;
  249. /*
  250. * Update quality RSSI for link tuning,
  251. * when we have received some frames and we managed to
  252. * collect the RSSI data we could use this. Otherwise we
  253. * must fallback to the default RSSI value.
  254. */
  255. if (!qual->rx_success)
  256. qual->rssi = DEFAULT_RSSI;
  257. else
  258. qual->rssi = rt2x00link_get_avg_rssi(&link->avg_rssi);
  259. /*
  260. * Check if link tuning is supported by the hardware, some hardware
  261. * do not support link tuning at all, while other devices can disable
  262. * the feature from the EEPROM.
  263. */
  264. if (rt2x00_has_cap_link_tuning(rt2x00dev))
  265. rt2x00dev->ops->lib->link_tuner(rt2x00dev, qual, link->count);
  266. /*
  267. * Send a signal to the led to update the led signal strength.
  268. */
  269. rt2x00leds_led_quality(rt2x00dev, qual->rssi);
  270. /*
  271. * Evaluate antenna setup, make this the last step when
  272. * rt2x00lib_antenna_diversity made changes the quality
  273. * statistics will be reset.
  274. */
  275. if (rt2x00lib_antenna_diversity(rt2x00dev))
  276. rt2x00link_reset_qual(rt2x00dev);
  277. }
  278. static void rt2x00link_tuner(struct work_struct *work)
  279. {
  280. struct rt2x00_dev *rt2x00dev =
  281. container_of(work, struct rt2x00_dev, link.work.work);
  282. struct link *link = &rt2x00dev->link;
  283. /*
  284. * When the radio is shutting down we should
  285. * immediately cease all link tuning.
  286. */
  287. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags) ||
  288. test_bit(DEVICE_STATE_SCANNING, &rt2x00dev->flags))
  289. return;
  290. /* Do not race with rt2x00mac_config(). */
  291. mutex_lock(&rt2x00dev->conf_mutex);
  292. if (rt2x00dev->intf_sta_count)
  293. rt2x00link_tuner_sta(rt2x00dev, link);
  294. if (rt2x00dev->ops->lib->gain_calibration &&
  295. (link->count % (AGC_SECONDS / LINK_TUNE_SECONDS)) == 0)
  296. rt2x00dev->ops->lib->gain_calibration(rt2x00dev);
  297. if (rt2x00dev->ops->lib->vco_calibration &&
  298. rt2x00_has_cap_vco_recalibration(rt2x00dev) &&
  299. (link->count % (VCO_SECONDS / LINK_TUNE_SECONDS)) == 0)
  300. rt2x00dev->ops->lib->vco_calibration(rt2x00dev);
  301. mutex_unlock(&rt2x00dev->conf_mutex);
  302. /*
  303. * Increase tuner counter, and reschedule the next link tuner run.
  304. */
  305. link->count++;
  306. if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  307. ieee80211_queue_delayed_work(rt2x00dev->hw,
  308. &link->work, LINK_TUNE_INTERVAL);
  309. }
  310. void rt2x00link_start_watchdog(struct rt2x00_dev *rt2x00dev)
  311. {
  312. struct link *link = &rt2x00dev->link;
  313. if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) &&
  314. rt2x00dev->ops->lib->watchdog && !link->watchdog_disabled)
  315. ieee80211_queue_delayed_work(rt2x00dev->hw,
  316. &link->watchdog_work,
  317. link->watchdog_interval);
  318. }
  319. void rt2x00link_stop_watchdog(struct rt2x00_dev *rt2x00dev)
  320. {
  321. cancel_delayed_work_sync(&rt2x00dev->link.watchdog_work);
  322. }
  323. static void rt2x00link_watchdog(struct work_struct *work)
  324. {
  325. struct rt2x00_dev *rt2x00dev =
  326. container_of(work, struct rt2x00_dev, link.watchdog_work.work);
  327. struct link *link = &rt2x00dev->link;
  328. /*
  329. * When the radio is shutting down we should
  330. * immediately cease the watchdog monitoring.
  331. */
  332. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  333. return;
  334. rt2x00dev->ops->lib->watchdog(rt2x00dev);
  335. if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  336. ieee80211_queue_delayed_work(rt2x00dev->hw,
  337. &link->watchdog_work,
  338. link->watchdog_interval);
  339. }
  340. void rt2x00link_register(struct rt2x00_dev *rt2x00dev)
  341. {
  342. struct link *link = &rt2x00dev->link;
  343. INIT_DELAYED_WORK(&link->work, rt2x00link_tuner);
  344. INIT_DELAYED_WORK(&link->watchdog_work, rt2x00link_watchdog);
  345. if (link->watchdog_interval == 0)
  346. link->watchdog_interval = WATCHDOG_INTERVAL;
  347. }