Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
dbf498fb JC |
2 | /* |
3 | * Copyright 2011-2012, Pavel Zubarev <pavel.zubarev@gmail.com> | |
4 | * Copyright 2011-2012, Marco Porsch <marco.porsch@s2005.tu-chemnitz.de> | |
5 | * Copyright 2011-2012, cozybit Inc. | |
a5b983c6 | 6 | * Copyright (C) 2021 Intel Corporation |
dbf498fb JC |
7 | */ |
8 | ||
9 | #include "ieee80211_i.h" | |
10 | #include "mesh.h" | |
11 | #include "driver-ops.h" | |
12 | ||
445cd452 | 13 | /* This is not in the standard. It represents a tolerable tsf drift below |
dbf498fb JC |
14 | * which we do no TSF adjustment. |
15 | */ | |
a802a6eb JC |
16 | #define TOFFSET_MINIMUM_ADJUSTMENT 10 |
17 | ||
ec14bcd2 JC |
18 | /* This is not in the standard. It is a margin added to the |
19 | * Toffset setpoint to mitigate TSF overcorrection | |
20 | * introduced by TSF adjustment latency. | |
21 | */ | |
22 | #define TOFFSET_SET_MARGIN 20 | |
23 | ||
a802a6eb JC |
24 | /* This is not in the standard. It represents the maximum Toffset jump above |
25 | * which we'll invalidate the Toffset setpoint and choose a new setpoint. This | |
26 | * could be, for instance, in case a neighbor is restarted and its TSF counter | |
27 | * reset. | |
ec14bcd2 | 28 | */ |
3a53731d | 29 | #define TOFFSET_MAXIMUM_ADJUSTMENT 800 /* 0.8 ms */ |
dbf498fb JC |
30 | |
31 | struct sync_method { | |
32 | u8 method; | |
33 | struct ieee80211_mesh_sync_ops ops; | |
34 | }; | |
35 | ||
36 | /** | |
37 | * mesh_peer_tbtt_adjusting - check if an mp is currently adjusting its TBTT | |
38 | * | |
a5b983c6 | 39 | * @cfg: mesh config element from the mesh peer (or %NULL) |
dbf498fb | 40 | */ |
a5b983c6 | 41 | static bool mesh_peer_tbtt_adjusting(const struct ieee80211_meshconf_ie *cfg) |
dbf498fb | 42 | { |
a5b983c6 JB |
43 | return cfg && |
44 | (cfg->meshconf_cap & IEEE80211_MESHCONF_CAPAB_TBTT_ADJUSTING); | |
dbf498fb JC |
45 | } |
46 | ||
445cd452 | 47 | void mesh_sync_adjust_tsf(struct ieee80211_sub_if_data *sdata) |
dbf498fb JC |
48 | { |
49 | struct ieee80211_local *local = sdata->local; | |
50 | struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh; | |
51 | /* sdata->vif.bss_conf.beacon_int in 1024us units, 0.04% */ | |
52 | u64 beacon_int_fraction = sdata->vif.bss_conf.beacon_int * 1024 / 2500; | |
53 | u64 tsf; | |
54 | u64 tsfdelta; | |
55 | ||
56 | spin_lock_bh(&ifmsh->sync_offset_lock); | |
dbf498fb | 57 | if (ifmsh->sync_offset_clockdrift_max < beacon_int_fraction) { |
445cd452 | 58 | msync_dbg(sdata, "TSF : max clockdrift=%lld; adjusting\n", |
bdcbd8e0 | 59 | (long long) ifmsh->sync_offset_clockdrift_max); |
dbf498fb JC |
60 | tsfdelta = -ifmsh->sync_offset_clockdrift_max; |
61 | ifmsh->sync_offset_clockdrift_max = 0; | |
62 | } else { | |
445cd452 | 63 | msync_dbg(sdata, "TSF : max clockdrift=%lld; adjusting by %llu\n", |
bdcbd8e0 JB |
64 | (long long) ifmsh->sync_offset_clockdrift_max, |
65 | (unsigned long long) beacon_int_fraction); | |
dbf498fb JC |
66 | tsfdelta = -beacon_int_fraction; |
67 | ifmsh->sync_offset_clockdrift_max -= beacon_int_fraction; | |
68 | } | |
55fabefe | 69 | spin_unlock_bh(&ifmsh->sync_offset_lock); |
dbf498fb | 70 | |
354d381b PT |
71 | if (local->ops->offset_tsf) { |
72 | drv_offset_tsf(local, sdata, tsfdelta); | |
73 | } else { | |
74 | tsf = drv_get_tsf(local, sdata); | |
75 | if (tsf != -1ULL) | |
76 | drv_set_tsf(local, sdata, tsf + tsfdelta); | |
77 | } | |
dbf498fb JC |
78 | } |
79 | ||
a5b983c6 JB |
80 | static void |
81 | mesh_sync_offset_rx_bcn_presp(struct ieee80211_sub_if_data *sdata, u16 stype, | |
82 | struct ieee80211_mgmt *mgmt, unsigned int len, | |
83 | const struct ieee80211_meshconf_ie *mesh_cfg, | |
84 | struct ieee80211_rx_status *rx_status) | |
dbf498fb JC |
85 | { |
86 | struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh; | |
87 | struct ieee80211_local *local = sdata->local; | |
88 | struct sta_info *sta; | |
89 | u64 t_t, t_r; | |
90 | ||
91 | WARN_ON(ifmsh->mesh_sp_id != IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET); | |
92 | ||
93 | /* standard mentions only beacons */ | |
94 | if (stype != IEEE80211_STYPE_BEACON) | |
95 | return; | |
96 | ||
ce953204 BC |
97 | /* |
98 | * Get time when timestamp field was received. If we don't | |
99 | * have rx timestamps, then use current tsf as an approximation. | |
100 | * drv_get_tsf() must be called before entering the rcu-read | |
101 | * section. | |
102 | */ | |
103 | if (ieee80211_have_rx_timestamp(rx_status)) | |
104 | t_r = ieee80211_calculate_rx_timestamp(local, rx_status, | |
a5b983c6 | 105 | len + FCS_LEN, 24); |
ce953204 BC |
106 | else |
107 | t_r = drv_get_tsf(local, sdata); | |
dbf498fb JC |
108 | |
109 | rcu_read_lock(); | |
110 | sta = sta_info_get(sdata, mgmt->sa); | |
111 | if (!sta) | |
112 | goto no_sync; | |
113 | ||
114 | /* check offset sync conditions (13.13.2.2.1) | |
115 | * | |
116 | * TODO also sync to | |
117 | * dot11MeshNbrOffsetMaxNeighbor non-peer non-MBSS neighbors | |
118 | */ | |
119 | ||
a5b983c6 | 120 | if (mesh_peer_tbtt_adjusting(mesh_cfg)) { |
bf7cd94d JB |
121 | msync_dbg(sdata, "STA %pM : is adjusting TBTT\n", |
122 | sta->sta.addr); | |
dbf498fb JC |
123 | goto no_sync; |
124 | } | |
125 | ||
dbf498fb JC |
126 | /* Timing offset calculation (see 13.13.2.2.2) */ |
127 | t_t = le64_to_cpu(mgmt->u.beacon.timestamp); | |
433f5bc1 | 128 | sta->mesh->t_offset = t_t - t_r; |
dbf498fb JC |
129 | |
130 | if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) { | |
433f5bc1 | 131 | s64 t_clockdrift = sta->mesh->t_offset_setpoint - sta->mesh->t_offset; |
bdcbd8e0 | 132 | msync_dbg(sdata, |
433f5bc1 JB |
133 | "STA %pM : t_offset=%lld, t_offset_setpoint=%lld, t_clockdrift=%lld\n", |
134 | sta->sta.addr, (long long) sta->mesh->t_offset, | |
135 | (long long) sta->mesh->t_offset_setpoint, | |
dbf498fb | 136 | (long long) t_clockdrift); |
a802a6eb JC |
137 | |
138 | if (t_clockdrift > TOFFSET_MAXIMUM_ADJUSTMENT || | |
bf7cd94d | 139 | t_clockdrift < -TOFFSET_MAXIMUM_ADJUSTMENT) { |
bdcbd8e0 JB |
140 | msync_dbg(sdata, |
141 | "STA %pM : t_clockdrift=%lld too large, setpoint reset\n", | |
a802a6eb JC |
142 | sta->sta.addr, |
143 | (long long) t_clockdrift); | |
144 | clear_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN); | |
145 | goto no_sync; | |
146 | } | |
147 | ||
dbf498fb | 148 | spin_lock_bh(&ifmsh->sync_offset_lock); |
bf7cd94d JB |
149 | if (t_clockdrift > ifmsh->sync_offset_clockdrift_max) |
150 | ifmsh->sync_offset_clockdrift_max = t_clockdrift; | |
dbf498fb | 151 | spin_unlock_bh(&ifmsh->sync_offset_lock); |
dbf498fb | 152 | } else { |
433f5bc1 | 153 | sta->mesh->t_offset_setpoint = sta->mesh->t_offset - TOFFSET_SET_MARGIN; |
dbf498fb | 154 | set_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN); |
bdcbd8e0 | 155 | msync_dbg(sdata, |
433f5bc1 | 156 | "STA %pM : offset was invalid, t_offset=%lld\n", |
dbf498fb | 157 | sta->sta.addr, |
433f5bc1 | 158 | (long long) sta->mesh->t_offset); |
dbf498fb | 159 | } |
dbf498fb JC |
160 | |
161 | no_sync: | |
162 | rcu_read_unlock(); | |
163 | } | |
164 | ||
445cd452 | 165 | static void mesh_sync_offset_adjust_tsf(struct ieee80211_sub_if_data *sdata, |
43552be1 | 166 | struct beacon_data *beacon) |
dbf498fb JC |
167 | { |
168 | struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh; | |
169 | ||
bf7cd94d | 170 | WARN_ON(ifmsh->mesh_sp_id != IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET); |
8c5bb1fa | 171 | WARN_ON(!rcu_read_lock_held()); |
dbf498fb JC |
172 | |
173 | spin_lock_bh(&ifmsh->sync_offset_lock); | |
174 | ||
bf7cd94d | 175 | if (ifmsh->sync_offset_clockdrift_max > TOFFSET_MINIMUM_ADJUSTMENT) { |
dbf498fb JC |
176 | /* Since ajusting the tsf here would |
177 | * require a possibly blocking call | |
178 | * to the driver tsf setter, we punt | |
179 | * the tsf adjustment to the mesh tasklet | |
180 | */ | |
bdcbd8e0 | 181 | msync_dbg(sdata, |
445cd452 | 182 | "TSF : kicking off TSF adjustment with clockdrift_max=%lld\n", |
bdcbd8e0 | 183 | ifmsh->sync_offset_clockdrift_max); |
bf7cd94d | 184 | set_bit(MESH_WORK_DRIFT_ADJUST, &ifmsh->wrkq_flags); |
dbf498fb | 185 | } else { |
bdcbd8e0 | 186 | msync_dbg(sdata, |
445cd452 | 187 | "TSF : max clockdrift=%lld; too small to adjust\n", |
bdcbd8e0 | 188 | (long long)ifmsh->sync_offset_clockdrift_max); |
dbf498fb JC |
189 | ifmsh->sync_offset_clockdrift_max = 0; |
190 | } | |
191 | spin_unlock_bh(&ifmsh->sync_offset_lock); | |
192 | } | |
193 | ||
8ba7acf3 | 194 | static const struct sync_method sync_methods[] = { |
dbf498fb JC |
195 | { |
196 | .method = IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET, | |
197 | .ops = { | |
198 | .rx_bcn_presp = &mesh_sync_offset_rx_bcn_presp, | |
445cd452 | 199 | .adjust_tsf = &mesh_sync_offset_adjust_tsf, |
dbf498fb JC |
200 | } |
201 | }, | |
dbf498fb JC |
202 | }; |
203 | ||
8ba7acf3 | 204 | const struct ieee80211_mesh_sync_ops *ieee80211_mesh_sync_ops_get(u8 method) |
dbf498fb | 205 | { |
bf7cd94d | 206 | int i; |
dbf498fb JC |
207 | |
208 | for (i = 0 ; i < ARRAY_SIZE(sync_methods); ++i) { | |
bf7cd94d JB |
209 | if (sync_methods[i].method == method) |
210 | return &sync_methods[i].ops; | |
dbf498fb | 211 | } |
bf7cd94d | 212 | return NULL; |
dbf498fb | 213 | } |