2 * Copyright 2011-2012, Pavel Zubarev <pavel.zubarev@gmail.com>
3 * Copyright 2011-2012, Marco Porsch <marco.porsch@s2005.tu-chemnitz.de>
4 * Copyright 2011-2012, cozybit Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include "ieee80211_i.h"
13 #include "driver-ops.h"
15 /* This is not in the standard. It represents a tolerable tbtt drift below
16 * which we do no TSF adjustment.
18 #define TOFFSET_MINIMUM_ADJUSTMENT 10
20 /* This is not in the standard. It is a margin added to the
21 * Toffset setpoint to mitigate TSF overcorrection
22 * introduced by TSF adjustment latency.
24 #define TOFFSET_SET_MARGIN 20
26 /* This is not in the standard. It represents the maximum Toffset jump above
27 * which we'll invalidate the Toffset setpoint and choose a new setpoint. This
28 * could be, for instance, in case a neighbor is restarted and its TSF counter
31 #define TOFFSET_MAXIMUM_ADJUSTMENT 30000 /* 30 ms */
35 struct ieee80211_mesh_sync_ops ops;
39 * mesh_peer_tbtt_adjusting - check if an mp is currently adjusting its TBTT
41 * @ie: information elements of a management frame from the mesh peer
43 static bool mesh_peer_tbtt_adjusting(struct ieee802_11_elems *ie)
45 return (ie->mesh_config->meshconf_cap &
46 IEEE80211_MESHCONF_CAPAB_TBTT_ADJUSTING) != 0;
49 void mesh_sync_adjust_tbtt(struct ieee80211_sub_if_data *sdata)
51 struct ieee80211_local *local = sdata->local;
52 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
53 /* sdata->vif.bss_conf.beacon_int in 1024us units, 0.04% */
54 u64 beacon_int_fraction = sdata->vif.bss_conf.beacon_int * 1024 / 2500;
58 spin_lock_bh(&ifmsh->sync_offset_lock);
59 if (ifmsh->sync_offset_clockdrift_max < beacon_int_fraction) {
60 msync_dbg(sdata, "TBTT : max clockdrift=%lld; adjusting\n",
61 (long long) ifmsh->sync_offset_clockdrift_max);
62 tsfdelta = -ifmsh->sync_offset_clockdrift_max;
63 ifmsh->sync_offset_clockdrift_max = 0;
65 msync_dbg(sdata, "TBTT : max clockdrift=%lld; adjusting by %llu\n",
66 (long long) ifmsh->sync_offset_clockdrift_max,
67 (unsigned long long) beacon_int_fraction);
68 tsfdelta = -beacon_int_fraction;
69 ifmsh->sync_offset_clockdrift_max -= beacon_int_fraction;
71 spin_unlock_bh(&ifmsh->sync_offset_lock);
73 tsf = drv_get_tsf(local, sdata);
75 drv_set_tsf(local, sdata, tsf + tsfdelta);
78 static void mesh_sync_offset_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
80 struct ieee80211_mgmt *mgmt,
81 struct ieee802_11_elems *elems,
82 struct ieee80211_rx_status *rx_status)
84 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
85 struct ieee80211_local *local = sdata->local;
89 WARN_ON(ifmsh->mesh_sp_id != IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET);
91 /* standard mentions only beacons */
92 if (stype != IEEE80211_STYPE_BEACON)
95 /* The current tsf is a first approximation for the timestamp
96 * for the received beacon. Further down we try to get a
97 * better value from the rx_status->mactime field if
98 * available. Also we have to call drv_get_tsf() before
99 * entering the rcu-read section.*/
100 t_r = drv_get_tsf(local, sdata);
103 sta = sta_info_get(sdata, mgmt->sa);
107 /* check offset sync conditions (13.13.2.2.1)
110 * dot11MeshNbrOffsetMaxNeighbor non-peer non-MBSS neighbors
113 if (elems->mesh_config && mesh_peer_tbtt_adjusting(elems)) {
114 clear_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN);
115 msync_dbg(sdata, "STA %pM : is adjusting TBTT\n", sta->sta.addr);
119 if (ieee80211_have_rx_timestamp(rx_status))
120 /* time when timestamp field was received */
121 t_r = ieee80211_calculate_rx_timestamp(local, rx_status,
127 /* Timing offset calculation (see 13.13.2.2.2) */
128 t_t = le64_to_cpu(mgmt->u.beacon.timestamp);
129 sta->t_offset = t_t - t_r;
131 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
132 s64 t_clockdrift = sta->t_offset_setpoint
135 "STA %pM : sta->t_offset=%lld, sta->t_offset_setpoint=%lld, t_clockdrift=%lld\n",
137 (long long) sta->t_offset,
139 sta->t_offset_setpoint,
140 (long long) t_clockdrift);
142 if (t_clockdrift > TOFFSET_MAXIMUM_ADJUSTMENT ||
143 t_clockdrift < -TOFFSET_MAXIMUM_ADJUSTMENT) {
145 "STA %pM : t_clockdrift=%lld too large, setpoint reset\n",
147 (long long) t_clockdrift);
148 clear_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN);
154 spin_lock_bh(&ifmsh->sync_offset_lock);
156 ifmsh->sync_offset_clockdrift_max)
157 ifmsh->sync_offset_clockdrift_max
159 spin_unlock_bh(&ifmsh->sync_offset_lock);
162 sta->t_offset_setpoint = sta->t_offset - TOFFSET_SET_MARGIN;
163 set_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN);
165 "STA %pM : offset was invalid, sta->t_offset=%lld\n",
167 (long long) sta->t_offset);
176 static void mesh_sync_offset_adjust_tbtt(struct ieee80211_sub_if_data *sdata)
178 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
180 WARN_ON(ifmsh->mesh_sp_id
181 != IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET);
182 BUG_ON(!rcu_read_lock_held());
184 spin_lock_bh(&ifmsh->sync_offset_lock);
186 if (ifmsh->sync_offset_clockdrift_max >
187 TOFFSET_MINIMUM_ADJUSTMENT) {
188 /* Since ajusting the tsf here would
189 * require a possibly blocking call
190 * to the driver tsf setter, we punt
191 * the tsf adjustment to the mesh tasklet
194 "TBTT : kicking off TBTT adjustment with clockdrift_max=%lld\n",
195 ifmsh->sync_offset_clockdrift_max);
196 set_bit(MESH_WORK_DRIFT_ADJUST,
199 ifmsh->adjusting_tbtt = true;
202 "TBTT : max clockdrift=%lld; too small to adjust\n",
203 (long long)ifmsh->sync_offset_clockdrift_max);
204 ifmsh->sync_offset_clockdrift_max = 0;
206 ifmsh->adjusting_tbtt = false;
208 spin_unlock_bh(&ifmsh->sync_offset_lock);
211 static const struct sync_method sync_methods[] = {
213 .method = IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET,
215 .rx_bcn_presp = &mesh_sync_offset_rx_bcn_presp,
216 .adjust_tbtt = &mesh_sync_offset_adjust_tbtt,
221 const struct ieee80211_mesh_sync_ops *ieee80211_mesh_sync_ops_get(u8 method)
223 const struct ieee80211_mesh_sync_ops *ops = NULL;
226 for (i = 0 ; i < ARRAY_SIZE(sync_methods); ++i) {
227 if (sync_methods[i].method == method) {
228 ops = &sync_methods[i].ops;