blob: 2bc5dc25d5adc79a92cd29bc94d058e141d011c6 [file] [log] [blame]
Javier Cardonadbf498f2012-03-31 11:31:32 -07001/*
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.
5 *
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.
9 */
10
11#include "ieee80211_i.h"
12#include "mesh.h"
13#include "driver-ops.h"
14
Javier Cardonadbf498f2012-03-31 11:31:32 -070015/* This is not in the standard. It represents a tolerable tbtt drift below
16 * which we do no TSF adjustment.
17 */
Javier Cardonaa802a6e2012-04-12 14:32:22 -070018#define TOFFSET_MINIMUM_ADJUSTMENT 10
19
Javier Cardonaec14bcd2012-04-12 14:32:23 -070020/* 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.
23 */
24#define TOFFSET_SET_MARGIN 20
25
Javier Cardonaa802a6e2012-04-12 14:32:22 -070026/* 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
29 * reset.
Javier Cardonaec14bcd2012-04-12 14:32:23 -070030 */
Javier Cardonaa802a6e2012-04-12 14:32:22 -070031#define TOFFSET_MAXIMUM_ADJUSTMENT 30000 /* 30 ms */
Javier Cardonadbf498f2012-03-31 11:31:32 -070032
33struct sync_method {
34 u8 method;
35 struct ieee80211_mesh_sync_ops ops;
36};
37
38/**
39 * mesh_peer_tbtt_adjusting - check if an mp is currently adjusting its TBTT
40 *
41 * @ie: information elements of a management frame from the mesh peer
42 */
43static bool mesh_peer_tbtt_adjusting(struct ieee802_11_elems *ie)
44{
45 return (ie->mesh_config->meshconf_cap &
Johannes Bergbf7cd942013-02-15 14:40:31 +010046 IEEE80211_MESHCONF_CAPAB_TBTT_ADJUSTING) != 0;
Javier Cardonadbf498f2012-03-31 11:31:32 -070047}
48
49void mesh_sync_adjust_tbtt(struct ieee80211_sub_if_data *sdata)
50{
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;
55 u64 tsf;
56 u64 tsfdelta;
57
58 spin_lock_bh(&ifmsh->sync_offset_lock);
Javier Cardonadbf498f2012-03-31 11:31:32 -070059 if (ifmsh->sync_offset_clockdrift_max < beacon_int_fraction) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +020060 msync_dbg(sdata, "TBTT : max clockdrift=%lld; adjusting\n",
61 (long long) ifmsh->sync_offset_clockdrift_max);
Javier Cardonadbf498f2012-03-31 11:31:32 -070062 tsfdelta = -ifmsh->sync_offset_clockdrift_max;
63 ifmsh->sync_offset_clockdrift_max = 0;
64 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +020065 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);
Javier Cardonadbf498f2012-03-31 11:31:32 -070068 tsfdelta = -beacon_int_fraction;
69 ifmsh->sync_offset_clockdrift_max -= beacon_int_fraction;
70 }
Thomas Pedersen55fabef2012-10-05 17:57:39 -070071 spin_unlock_bh(&ifmsh->sync_offset_lock);
Javier Cardonadbf498f2012-03-31 11:31:32 -070072
73 tsf = drv_get_tsf(local, sdata);
74 if (tsf != -1ULL)
75 drv_set_tsf(local, sdata, tsf + tsfdelta);
Javier Cardonadbf498f2012-03-31 11:31:32 -070076}
77
78static void mesh_sync_offset_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
79 u16 stype,
80 struct ieee80211_mgmt *mgmt,
81 struct ieee802_11_elems *elems,
82 struct ieee80211_rx_status *rx_status)
83{
84 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
85 struct ieee80211_local *local = sdata->local;
86 struct sta_info *sta;
87 u64 t_t, t_r;
88
89 WARN_ON(ifmsh->mesh_sp_id != IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET);
90
91 /* standard mentions only beacons */
92 if (stype != IEEE80211_STYPE_BEACON)
93 return;
94
Bob Copelandce953202013-11-18 17:25:28 -050095 /*
96 * Get time when timestamp field was received. If we don't
97 * have rx timestamps, then use current tsf as an approximation.
98 * drv_get_tsf() must be called before entering the rcu-read
99 * section.
100 */
101 if (ieee80211_have_rx_timestamp(rx_status))
102 t_r = ieee80211_calculate_rx_timestamp(local, rx_status,
103 24 + 12 +
104 elems->total_len +
105 FCS_LEN,
106 24);
107 else
108 t_r = drv_get_tsf(local, sdata);
Javier Cardonadbf498f2012-03-31 11:31:32 -0700109
110 rcu_read_lock();
111 sta = sta_info_get(sdata, mgmt->sa);
112 if (!sta)
113 goto no_sync;
114
115 /* check offset sync conditions (13.13.2.2.1)
116 *
117 * TODO also sync to
118 * dot11MeshNbrOffsetMaxNeighbor non-peer non-MBSS neighbors
119 */
120
121 if (elems->mesh_config && mesh_peer_tbtt_adjusting(elems)) {
122 clear_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN);
Johannes Bergbf7cd942013-02-15 14:40:31 +0100123 msync_dbg(sdata, "STA %pM : is adjusting TBTT\n",
124 sta->sta.addr);
Javier Cardonadbf498f2012-03-31 11:31:32 -0700125 goto no_sync;
126 }
127
Javier Cardonadbf498f2012-03-31 11:31:32 -0700128 /* Timing offset calculation (see 13.13.2.2.2) */
129 t_t = le64_to_cpu(mgmt->u.beacon.timestamp);
130 sta->t_offset = t_t - t_r;
131
132 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
Johannes Bergbf7cd942013-02-15 14:40:31 +0100133 s64 t_clockdrift = sta->t_offset_setpoint - sta->t_offset;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200134 msync_dbg(sdata,
135 "STA %pM : sta->t_offset=%lld, sta->t_offset_setpoint=%lld, t_clockdrift=%lld\n",
Johannes Bergbf7cd942013-02-15 14:40:31 +0100136 sta->sta.addr, (long long) sta->t_offset,
137 (long long) sta->t_offset_setpoint,
Javier Cardonadbf498f2012-03-31 11:31:32 -0700138 (long long) t_clockdrift);
Javier Cardonaa802a6e2012-04-12 14:32:22 -0700139
140 if (t_clockdrift > TOFFSET_MAXIMUM_ADJUSTMENT ||
Johannes Bergbf7cd942013-02-15 14:40:31 +0100141 t_clockdrift < -TOFFSET_MAXIMUM_ADJUSTMENT) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200142 msync_dbg(sdata,
143 "STA %pM : t_clockdrift=%lld too large, setpoint reset\n",
Javier Cardonaa802a6e2012-04-12 14:32:22 -0700144 sta->sta.addr,
145 (long long) t_clockdrift);
146 clear_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN);
147 goto no_sync;
148 }
149
Javier Cardonadbf498f2012-03-31 11:31:32 -0700150 spin_lock_bh(&ifmsh->sync_offset_lock);
Johannes Bergbf7cd942013-02-15 14:40:31 +0100151 if (t_clockdrift > ifmsh->sync_offset_clockdrift_max)
152 ifmsh->sync_offset_clockdrift_max = t_clockdrift;
Javier Cardonadbf498f2012-03-31 11:31:32 -0700153 spin_unlock_bh(&ifmsh->sync_offset_lock);
Javier Cardonadbf498f2012-03-31 11:31:32 -0700154 } else {
Javier Cardona6ac95b52012-04-20 09:52:56 -0700155 sta->t_offset_setpoint = sta->t_offset - TOFFSET_SET_MARGIN;
Javier Cardonadbf498f2012-03-31 11:31:32 -0700156 set_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200157 msync_dbg(sdata,
158 "STA %pM : offset was invalid, sta->t_offset=%lld\n",
Javier Cardonadbf498f2012-03-31 11:31:32 -0700159 sta->sta.addr,
160 (long long) sta->t_offset);
Javier Cardonadbf498f2012-03-31 11:31:32 -0700161 }
Javier Cardonadbf498f2012-03-31 11:31:32 -0700162
163no_sync:
164 rcu_read_unlock();
165}
166
Thomas Pedersen43552be2013-12-15 13:14:16 -0800167static void mesh_sync_offset_adjust_tbtt(struct ieee80211_sub_if_data *sdata,
168 struct beacon_data *beacon)
Javier Cardonadbf498f2012-03-31 11:31:32 -0700169{
170 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Thomas Pedersen43552be2013-12-15 13:14:16 -0800171 u8 cap;
Javier Cardonadbf498f2012-03-31 11:31:32 -0700172
Johannes Bergbf7cd942013-02-15 14:40:31 +0100173 WARN_ON(ifmsh->mesh_sp_id != IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET);
Javier Cardonadbf498f2012-03-31 11:31:32 -0700174 BUG_ON(!rcu_read_lock_held());
Thomas Pedersen43552be2013-12-15 13:14:16 -0800175 cap = beacon->meshconf->meshconf_cap;
Javier Cardonadbf498f2012-03-31 11:31:32 -0700176
177 spin_lock_bh(&ifmsh->sync_offset_lock);
178
Johannes Bergbf7cd942013-02-15 14:40:31 +0100179 if (ifmsh->sync_offset_clockdrift_max > TOFFSET_MINIMUM_ADJUSTMENT) {
Javier Cardonadbf498f2012-03-31 11:31:32 -0700180 /* Since ajusting the tsf here would
181 * require a possibly blocking call
182 * to the driver tsf setter, we punt
183 * the tsf adjustment to the mesh tasklet
184 */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200185 msync_dbg(sdata,
186 "TBTT : kicking off TBTT adjustment with clockdrift_max=%lld\n",
187 ifmsh->sync_offset_clockdrift_max);
Johannes Bergbf7cd942013-02-15 14:40:31 +0100188 set_bit(MESH_WORK_DRIFT_ADJUST, &ifmsh->wrkq_flags);
Marco Porsch37203902012-11-23 12:23:18 -0800189
190 ifmsh->adjusting_tbtt = true;
Javier Cardonadbf498f2012-03-31 11:31:32 -0700191 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200192 msync_dbg(sdata,
193 "TBTT : max clockdrift=%lld; too small to adjust\n",
194 (long long)ifmsh->sync_offset_clockdrift_max);
Javier Cardonadbf498f2012-03-31 11:31:32 -0700195 ifmsh->sync_offset_clockdrift_max = 0;
Marco Porsch37203902012-11-23 12:23:18 -0800196
197 ifmsh->adjusting_tbtt = false;
Javier Cardonadbf498f2012-03-31 11:31:32 -0700198 }
199 spin_unlock_bh(&ifmsh->sync_offset_lock);
Thomas Pedersen43552be2013-12-15 13:14:16 -0800200
201 beacon->meshconf->meshconf_cap = ifmsh->adjusting_tbtt ?
202 IEEE80211_MESHCONF_CAPAB_TBTT_ADJUSTING | cap :
203 ~IEEE80211_MESHCONF_CAPAB_TBTT_ADJUSTING & cap;
Javier Cardonadbf498f2012-03-31 11:31:32 -0700204}
205
Johannes Berg8ba7acf2012-09-30 17:07:19 +0200206static const struct sync_method sync_methods[] = {
Javier Cardonadbf498f2012-03-31 11:31:32 -0700207 {
208 .method = IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET,
209 .ops = {
210 .rx_bcn_presp = &mesh_sync_offset_rx_bcn_presp,
211 .adjust_tbtt = &mesh_sync_offset_adjust_tbtt,
212 }
213 },
Javier Cardonadbf498f2012-03-31 11:31:32 -0700214};
215
Johannes Berg8ba7acf2012-09-30 17:07:19 +0200216const struct ieee80211_mesh_sync_ops *ieee80211_mesh_sync_ops_get(u8 method)
Javier Cardonadbf498f2012-03-31 11:31:32 -0700217{
Johannes Bergbf7cd942013-02-15 14:40:31 +0100218 int i;
Javier Cardonadbf498f2012-03-31 11:31:32 -0700219
220 for (i = 0 ; i < ARRAY_SIZE(sync_methods); ++i) {
Johannes Bergbf7cd942013-02-15 14:40:31 +0100221 if (sync_methods[i].method == method)
222 return &sync_methods[i].ops;
Javier Cardonadbf498f2012-03-31 11:31:32 -0700223 }
Johannes Bergbf7cd942013-02-15 14:40:31 +0100224 return NULL;
Javier Cardonadbf498f2012-03-31 11:31:32 -0700225}