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 #ifdef CONFIG_MAC80211_VERBOSE_MESH_SYNC_DEBUG
16 #define msync_dbg(fmt, args...) \
17 printk(KERN_DEBUG "Mesh sync (%s): " fmt "\n", sdata->name, ##args)
19 #define msync_dbg(fmt, args...) do { (void)(0); } while (0)
22 /* This is not in the standard. It represents a tolerable tbtt drift below
23 * which we do no TSF adjustment.
25 #define TBTT_MINIMUM_ADJUSTMENT 10
29 struct ieee80211_mesh_sync_ops ops;
33 * mesh_peer_tbtt_adjusting - check if an mp is currently adjusting its TBTT
35 * @ie: information elements of a management frame from the mesh peer
37 static bool mesh_peer_tbtt_adjusting(struct ieee802_11_elems *ie)
39 return (ie->mesh_config->meshconf_cap &
40 MESHCONF_CAPAB_TBTT_ADJUSTING) != 0;
43 void mesh_sync_adjust_tbtt(struct ieee80211_sub_if_data *sdata)
45 struct ieee80211_local *local = sdata->local;
46 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
47 /* sdata->vif.bss_conf.beacon_int in 1024us units, 0.04% */
48 u64 beacon_int_fraction = sdata->vif.bss_conf.beacon_int * 1024 / 2500;
52 spin_lock_bh(&ifmsh->sync_offset_lock);
54 if (ifmsh->sync_offset_clockdrift_max < beacon_int_fraction) {
55 msync_dbg("TBTT : max clockdrift=%lld; adjusting",
56 (long long) ifmsh->sync_offset_clockdrift_max);
57 tsfdelta = -ifmsh->sync_offset_clockdrift_max;
58 ifmsh->sync_offset_clockdrift_max = 0;
60 msync_dbg("TBTT : max clockdrift=%lld; adjusting by %llu",
61 (long long) ifmsh->sync_offset_clockdrift_max,
62 (unsigned long long) beacon_int_fraction);
63 tsfdelta = -beacon_int_fraction;
64 ifmsh->sync_offset_clockdrift_max -= beacon_int_fraction;
67 tsf = drv_get_tsf(local, sdata);
69 drv_set_tsf(local, sdata, tsf + tsfdelta);
70 spin_unlock_bh(&ifmsh->sync_offset_lock);
73 static void mesh_sync_offset_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
75 struct ieee80211_mgmt *mgmt,
76 struct ieee802_11_elems *elems,
77 struct ieee80211_rx_status *rx_status)
79 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
80 struct ieee80211_local *local = sdata->local;
84 WARN_ON(ifmsh->mesh_sp_id != IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET);
86 /* standard mentions only beacons */
87 if (stype != IEEE80211_STYPE_BEACON)
90 /* The current tsf is a first approximation for the timestamp
91 * for the received beacon. Further down we try to get a
92 * better value from the rx_status->mactime field if
93 * available. Also we have to call drv_get_tsf() before
94 * entering the rcu-read section.*/
95 t_r = drv_get_tsf(local, sdata);
98 sta = sta_info_get(sdata, mgmt->sa);
102 /* check offset sync conditions (13.13.2.2.1)
105 * dot11MeshNbrOffsetMaxNeighbor non-peer non-MBSS neighbors
108 if (elems->mesh_config && mesh_peer_tbtt_adjusting(elems)) {
109 clear_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN);
110 msync_dbg("STA %pM : is adjusting TBTT", sta->sta.addr);
114 if (rx_status->flag & RX_FLAG_MACTIME_MPDU && rx_status->mactime) {
116 * The mactime is defined as the time the first data symbol
117 * of the frame hits the PHY, and the timestamp of the beacon
118 * is defined as "the time that the data symbol containing the
119 * first bit of the timestamp is transmitted to the PHY plus
120 * the transmitting STA's delays through its local PHY from the
121 * MAC-PHY interface to its interface with the WM" (802.11
124 * T_r, in 13.13.2.2.2, is just defined as "the frame reception
125 * time" but we unless we interpret that time to be the same
126 * time of the beacon timestamp, the offset calculation will be
127 * off. Below we adjust t_r to be "the time at which the first
128 * symbol of the timestamp element in the beacon is received".
129 * This correction depends on the rate.
131 * Based on similar code in ibss.c
135 if (rx_status->flag & RX_FLAG_HT) {
137 * In principle there could be HT-beacons (Dual Beacon
138 * HT Operation options), but for now ignore them and
139 * just use the primary (i.e. non-HT) beacons for
144 rate = local->hw.wiphy->bands[rx_status->band]->
145 bitrates[rx_status->rate_idx].bitrate;
147 /* 24 bytes of header * 8 bits/byte *
148 * 10*(100 Kbps)/Mbps / rate (100 Kbps)*/
149 t_r = rx_status->mactime + (24 * 8 * 10 / rate);
152 /* Timing offset calculation (see 13.13.2.2.2) */
153 t_t = le64_to_cpu(mgmt->u.beacon.timestamp);
154 sta->t_offset = t_t - t_r;
156 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
157 s64 t_clockdrift = sta->t_offset_setpoint
160 msync_dbg("STA %pM : sta->t_offset=%lld,"
161 " sta->t_offset_setpoint=%lld,"
162 " t_clockdrift=%lld",
164 (long long) sta->t_offset,
166 sta->t_offset_setpoint,
167 (long long) t_clockdrift);
170 spin_lock_bh(&ifmsh->sync_offset_lock);
172 ifmsh->sync_offset_clockdrift_max)
173 ifmsh->sync_offset_clockdrift_max
175 spin_unlock_bh(&ifmsh->sync_offset_lock);
178 sta->t_offset_setpoint = sta->t_offset;
179 set_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN);
180 msync_dbg("STA %pM : offset was invalid, "
181 " sta->t_offset=%lld",
183 (long long) sta->t_offset);
192 static void mesh_sync_offset_adjust_tbtt(struct ieee80211_sub_if_data *sdata)
194 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
196 WARN_ON(ifmsh->mesh_sp_id
197 != IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET);
198 BUG_ON(!rcu_read_lock_held());
200 spin_lock_bh(&ifmsh->sync_offset_lock);
202 if (ifmsh->sync_offset_clockdrift_max >
203 TBTT_MINIMUM_ADJUSTMENT) {
204 /* Since ajusting the tsf here would
205 * require a possibly blocking call
206 * to the driver tsf setter, we punt
207 * the tsf adjustment to the mesh tasklet
209 msync_dbg("TBTT : kicking off TBTT "
211 "clockdrift_max=%lld",
212 ifmsh->sync_offset_clockdrift_max);
213 set_bit(MESH_WORK_DRIFT_ADJUST,
216 msync_dbg("TBTT : max clockdrift=%lld; "
217 "too small to adjust",
219 ifmsh->sync_offset_clockdrift_max);
220 ifmsh->sync_offset_clockdrift_max = 0;
222 spin_unlock_bh(&ifmsh->sync_offset_lock);
225 static const u8 *mesh_get_vendor_oui(struct ieee80211_sub_if_data *sdata)
227 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
230 if (!ifmsh->ie || !ifmsh->ie_len)
233 offset = ieee80211_ie_split_vendor(ifmsh->ie,
239 return ifmsh->ie + offset + 2;
242 static void mesh_sync_vendor_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
244 struct ieee80211_mgmt *mgmt,
245 struct ieee802_11_elems *elems,
246 struct ieee80211_rx_status *rx_status)
250 WARN_ON(sdata->u.mesh.mesh_sp_id != IEEE80211_SYNC_METHOD_VENDOR);
251 msync_dbg("called mesh_sync_vendor_rx_bcn_presp");
252 oui = mesh_get_vendor_oui(sdata);
253 /* here you would implement the vendor offset tracking for this oui */
256 static void mesh_sync_vendor_adjust_tbtt(struct ieee80211_sub_if_data *sdata)
260 WARN_ON(sdata->u.mesh.mesh_sp_id != IEEE80211_SYNC_METHOD_VENDOR);
261 msync_dbg("called mesh_sync_vendor_adjust_tbtt");
262 oui = mesh_get_vendor_oui(sdata);
263 /* here you would implement the vendor tsf adjustment for this oui */
266 /* global variable */
267 static struct sync_method sync_methods[] = {
269 .method = IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET,
271 .rx_bcn_presp = &mesh_sync_offset_rx_bcn_presp,
272 .adjust_tbtt = &mesh_sync_offset_adjust_tbtt,
276 .method = IEEE80211_SYNC_METHOD_VENDOR,
278 .rx_bcn_presp = &mesh_sync_vendor_rx_bcn_presp,
279 .adjust_tbtt = &mesh_sync_vendor_adjust_tbtt,
284 struct ieee80211_mesh_sync_ops *ieee80211_mesh_sync_ops_get(u8 method)
286 struct ieee80211_mesh_sync_ops *ops = NULL;
289 for (i = 0 ; i < ARRAY_SIZE(sync_methods); ++i) {
290 if (sync_methods[i].method == method) {
291 ops = &sync_methods[i].ops;