1 // SPDX-License-Identifier: ISC 2 /* 3 * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name> 4 */ 5 6 #include <linux/module.h> 7 #if defined(__FreeBSD__) 8 #include <linux/delay.h> 9 #include <linux/kthread.h> 10 #endif 11 #include "mt76.h" 12 13 bool __mt76_poll(struct mt76_dev *dev, u32 offset, u32 mask, u32 val, 14 int timeout) 15 { 16 u32 cur; 17 18 timeout /= 10; 19 do { 20 cur = __mt76_rr(dev, offset) & mask; 21 if (cur == val) 22 return true; 23 24 udelay(10); 25 } while (timeout-- > 0); 26 27 return false; 28 } 29 EXPORT_SYMBOL_GPL(__mt76_poll); 30 31 bool ____mt76_poll_msec(struct mt76_dev *dev, u32 offset, u32 mask, u32 val, 32 int timeout, int tick) 33 { 34 u32 cur; 35 36 timeout /= tick; 37 do { 38 cur = __mt76_rr(dev, offset) & mask; 39 if (cur == val) 40 return true; 41 42 usleep_range(1000 * tick, 2000 * tick); 43 } while (timeout-- > 0); 44 45 return false; 46 } 47 EXPORT_SYMBOL_GPL(____mt76_poll_msec); 48 49 int mt76_wcid_alloc(u32 *mask, int size) 50 { 51 int i, idx = 0, cur; 52 53 for (i = 0; i < DIV_ROUND_UP(size, 32); i++) { 54 idx = ffs(~mask[i]); 55 if (!idx) 56 continue; 57 58 idx--; 59 cur = i * 32 + idx; 60 if (cur >= size) 61 break; 62 63 mask[i] |= BIT(idx); 64 return cur; 65 } 66 67 return -1; 68 } 69 EXPORT_SYMBOL_GPL(mt76_wcid_alloc); 70 71 int mt76_get_min_avg_rssi(struct mt76_dev *dev, bool ext_phy) 72 { 73 struct mt76_wcid *wcid; 74 int i, j, min_rssi = 0; 75 s8 cur_rssi; 76 77 local_bh_disable(); 78 rcu_read_lock(); 79 80 for (i = 0; i < ARRAY_SIZE(dev->wcid_mask); i++) { 81 u32 mask = dev->wcid_mask[i]; 82 u32 phy_mask = dev->wcid_phy_mask[i]; 83 84 if (!mask) 85 continue; 86 87 for (j = i * 32; mask; j++, mask >>= 1, phy_mask >>= 1) { 88 if (!(mask & 1)) 89 continue; 90 91 if (!!(phy_mask & 1) != ext_phy) 92 continue; 93 94 wcid = rcu_dereference(dev->wcid[j]); 95 if (!wcid) 96 continue; 97 98 spin_lock(&dev->rx_lock); 99 if (wcid->inactive_count++ < 5) 100 cur_rssi = -ewma_signal_read(&wcid->rssi); 101 else 102 cur_rssi = 0; 103 spin_unlock(&dev->rx_lock); 104 105 if (cur_rssi < min_rssi) 106 min_rssi = cur_rssi; 107 } 108 } 109 110 rcu_read_unlock(); 111 local_bh_enable(); 112 113 return min_rssi; 114 } 115 EXPORT_SYMBOL_GPL(mt76_get_min_avg_rssi); 116 117 int __mt76_worker_fn(void *ptr) 118 { 119 struct mt76_worker *w = ptr; 120 121 while (!kthread_should_stop()) { 122 set_current_state(TASK_INTERRUPTIBLE); 123 124 if (kthread_should_park()) { 125 kthread_parkme(); 126 continue; 127 } 128 129 if (!test_and_clear_bit(MT76_WORKER_SCHEDULED, &w->state)) { 130 schedule(); 131 continue; 132 } 133 134 set_bit(MT76_WORKER_RUNNING, &w->state); 135 set_current_state(TASK_RUNNING); 136 w->fn(w); 137 cond_resched(); 138 clear_bit(MT76_WORKER_RUNNING, &w->state); 139 } 140 141 return 0; 142 } 143 EXPORT_SYMBOL_GPL(__mt76_worker_fn); 144 145 MODULE_LICENSE("Dual BSD/GPL"); 146 #if defined(__FreeBSD__) 147 MODULE_VERSION(mt76_core, 1); 148 MODULE_DEPEND(mt76_core, linuxkpi, 1, 1, 1); 149 MODULE_DEPEND(mt76_core, linuxkpi_wlan, 1, 1, 1); 150 #endif 151