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