util.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: ISC
  2. /*
  3. * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
  4. */
  5. #include <linux/module.h>
  6. #include "mt76.h"
  7. bool __mt76_poll(struct mt76_dev *dev, u32 offset, u32 mask, u32 val,
  8. int timeout)
  9. {
  10. u32 cur;
  11. timeout /= 10;
  12. do {
  13. cur = __mt76_rr(dev, offset) & mask;
  14. if (cur == val)
  15. return true;
  16. udelay(10);
  17. } while (timeout-- > 0);
  18. return false;
  19. }
  20. EXPORT_SYMBOL_GPL(__mt76_poll);
  21. bool __mt76_poll_msec(struct mt76_dev *dev, u32 offset, u32 mask, u32 val,
  22. int timeout)
  23. {
  24. u32 cur;
  25. timeout /= 10;
  26. do {
  27. cur = __mt76_rr(dev, offset) & mask;
  28. if (cur == val)
  29. return true;
  30. usleep_range(10000, 20000);
  31. } while (timeout-- > 0);
  32. return false;
  33. }
  34. EXPORT_SYMBOL_GPL(__mt76_poll_msec);
  35. int mt76_wcid_alloc(u32 *mask, int size)
  36. {
  37. int i, idx = 0, cur;
  38. for (i = 0; i < DIV_ROUND_UP(size, 32); i++) {
  39. idx = ffs(~mask[i]);
  40. if (!idx)
  41. continue;
  42. idx--;
  43. cur = i * 32 + idx;
  44. if (cur >= size)
  45. break;
  46. mask[i] |= BIT(idx);
  47. return cur;
  48. }
  49. return -1;
  50. }
  51. EXPORT_SYMBOL_GPL(mt76_wcid_alloc);
  52. int mt76_get_min_avg_rssi(struct mt76_dev *dev, bool ext_phy)
  53. {
  54. struct mt76_wcid *wcid;
  55. int i, j, min_rssi = 0;
  56. s8 cur_rssi;
  57. local_bh_disable();
  58. rcu_read_lock();
  59. for (i = 0; i < ARRAY_SIZE(dev->wcid_mask); i++) {
  60. u32 mask = dev->wcid_mask[i];
  61. u32 phy_mask = dev->wcid_phy_mask[i];
  62. if (!mask)
  63. continue;
  64. for (j = i * 32; mask; j++, mask >>= 1, phy_mask >>= 1) {
  65. if (!(mask & 1))
  66. continue;
  67. if (!!(phy_mask & 1) != ext_phy)
  68. continue;
  69. wcid = rcu_dereference(dev->wcid[j]);
  70. if (!wcid)
  71. continue;
  72. spin_lock(&dev->rx_lock);
  73. if (wcid->inactive_count++ < 5)
  74. cur_rssi = -ewma_signal_read(&wcid->rssi);
  75. else
  76. cur_rssi = 0;
  77. spin_unlock(&dev->rx_lock);
  78. if (cur_rssi < min_rssi)
  79. min_rssi = cur_rssi;
  80. }
  81. }
  82. rcu_read_unlock();
  83. local_bh_enable();
  84. return min_rssi;
  85. }
  86. EXPORT_SYMBOL_GPL(mt76_get_min_avg_rssi);
  87. int __mt76_worker_fn(void *ptr)
  88. {
  89. struct mt76_worker *w = ptr;
  90. while (!kthread_should_stop()) {
  91. set_current_state(TASK_INTERRUPTIBLE);
  92. if (kthread_should_park()) {
  93. kthread_parkme();
  94. continue;
  95. }
  96. if (!test_and_clear_bit(MT76_WORKER_SCHEDULED, &w->state)) {
  97. schedule();
  98. continue;
  99. }
  100. set_bit(MT76_WORKER_RUNNING, &w->state);
  101. set_current_state(TASK_RUNNING);
  102. w->fn(w);
  103. cond_resched();
  104. clear_bit(MT76_WORKER_RUNNING, &w->state);
  105. }
  106. return 0;
  107. }
  108. EXPORT_SYMBOL_GPL(__mt76_worker_fn);
  109. MODULE_LICENSE("Dual BSD/GPL");