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