0001
0002
0003
0004
0005
0006
0007
0008
0009 #include "ieee80211_i.h"
0010 #include "mesh.h"
0011 #include "driver-ops.h"
0012
0013
0014
0015
0016 #define TOFFSET_MINIMUM_ADJUSTMENT 10
0017
0018
0019
0020
0021
0022 #define TOFFSET_SET_MARGIN 20
0023
0024
0025
0026
0027
0028
0029 #define TOFFSET_MAXIMUM_ADJUSTMENT 800
0030
0031 struct sync_method {
0032 u8 method;
0033 struct ieee80211_mesh_sync_ops ops;
0034 };
0035
0036
0037
0038
0039
0040
0041 static bool mesh_peer_tbtt_adjusting(const struct ieee80211_meshconf_ie *cfg)
0042 {
0043 return cfg &&
0044 (cfg->meshconf_cap & IEEE80211_MESHCONF_CAPAB_TBTT_ADJUSTING);
0045 }
0046
0047 void mesh_sync_adjust_tsf(struct ieee80211_sub_if_data *sdata)
0048 {
0049 struct ieee80211_local *local = sdata->local;
0050 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
0051
0052 u64 beacon_int_fraction = sdata->vif.bss_conf.beacon_int * 1024 / 2500;
0053 u64 tsf;
0054 u64 tsfdelta;
0055
0056 spin_lock_bh(&ifmsh->sync_offset_lock);
0057 if (ifmsh->sync_offset_clockdrift_max < beacon_int_fraction) {
0058 msync_dbg(sdata, "TSF : max clockdrift=%lld; adjusting\n",
0059 (long long) ifmsh->sync_offset_clockdrift_max);
0060 tsfdelta = -ifmsh->sync_offset_clockdrift_max;
0061 ifmsh->sync_offset_clockdrift_max = 0;
0062 } else {
0063 msync_dbg(sdata, "TSF : max clockdrift=%lld; adjusting by %llu\n",
0064 (long long) ifmsh->sync_offset_clockdrift_max,
0065 (unsigned long long) beacon_int_fraction);
0066 tsfdelta = -beacon_int_fraction;
0067 ifmsh->sync_offset_clockdrift_max -= beacon_int_fraction;
0068 }
0069 spin_unlock_bh(&ifmsh->sync_offset_lock);
0070
0071 if (local->ops->offset_tsf) {
0072 drv_offset_tsf(local, sdata, tsfdelta);
0073 } else {
0074 tsf = drv_get_tsf(local, sdata);
0075 if (tsf != -1ULL)
0076 drv_set_tsf(local, sdata, tsf + tsfdelta);
0077 }
0078 }
0079
0080 static void
0081 mesh_sync_offset_rx_bcn_presp(struct ieee80211_sub_if_data *sdata, u16 stype,
0082 struct ieee80211_mgmt *mgmt, unsigned int len,
0083 const struct ieee80211_meshconf_ie *mesh_cfg,
0084 struct ieee80211_rx_status *rx_status)
0085 {
0086 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
0087 struct ieee80211_local *local = sdata->local;
0088 struct sta_info *sta;
0089 u64 t_t, t_r;
0090
0091 WARN_ON(ifmsh->mesh_sp_id != IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET);
0092
0093
0094 if (stype != IEEE80211_STYPE_BEACON)
0095 return;
0096
0097
0098
0099
0100
0101
0102
0103 if (ieee80211_have_rx_timestamp(rx_status))
0104 t_r = ieee80211_calculate_rx_timestamp(local, rx_status,
0105 len + FCS_LEN, 24);
0106 else
0107 t_r = drv_get_tsf(local, sdata);
0108
0109 rcu_read_lock();
0110 sta = sta_info_get(sdata, mgmt->sa);
0111 if (!sta)
0112 goto no_sync;
0113
0114
0115
0116
0117
0118
0119
0120 if (mesh_peer_tbtt_adjusting(mesh_cfg)) {
0121 msync_dbg(sdata, "STA %pM : is adjusting TBTT\n",
0122 sta->sta.addr);
0123 goto no_sync;
0124 }
0125
0126
0127 t_t = le64_to_cpu(mgmt->u.beacon.timestamp);
0128 sta->mesh->t_offset = t_t - t_r;
0129
0130 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
0131 s64 t_clockdrift = sta->mesh->t_offset_setpoint - sta->mesh->t_offset;
0132 msync_dbg(sdata,
0133 "STA %pM : t_offset=%lld, t_offset_setpoint=%lld, t_clockdrift=%lld\n",
0134 sta->sta.addr, (long long) sta->mesh->t_offset,
0135 (long long) sta->mesh->t_offset_setpoint,
0136 (long long) t_clockdrift);
0137
0138 if (t_clockdrift > TOFFSET_MAXIMUM_ADJUSTMENT ||
0139 t_clockdrift < -TOFFSET_MAXIMUM_ADJUSTMENT) {
0140 msync_dbg(sdata,
0141 "STA %pM : t_clockdrift=%lld too large, setpoint reset\n",
0142 sta->sta.addr,
0143 (long long) t_clockdrift);
0144 clear_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN);
0145 goto no_sync;
0146 }
0147
0148 spin_lock_bh(&ifmsh->sync_offset_lock);
0149 if (t_clockdrift > ifmsh->sync_offset_clockdrift_max)
0150 ifmsh->sync_offset_clockdrift_max = t_clockdrift;
0151 spin_unlock_bh(&ifmsh->sync_offset_lock);
0152 } else {
0153 sta->mesh->t_offset_setpoint = sta->mesh->t_offset - TOFFSET_SET_MARGIN;
0154 set_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN);
0155 msync_dbg(sdata,
0156 "STA %pM : offset was invalid, t_offset=%lld\n",
0157 sta->sta.addr,
0158 (long long) sta->mesh->t_offset);
0159 }
0160
0161 no_sync:
0162 rcu_read_unlock();
0163 }
0164
0165 static void mesh_sync_offset_adjust_tsf(struct ieee80211_sub_if_data *sdata,
0166 struct beacon_data *beacon)
0167 {
0168 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
0169
0170 WARN_ON(ifmsh->mesh_sp_id != IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET);
0171 WARN_ON(!rcu_read_lock_held());
0172
0173 spin_lock_bh(&ifmsh->sync_offset_lock);
0174
0175 if (ifmsh->sync_offset_clockdrift_max > TOFFSET_MINIMUM_ADJUSTMENT) {
0176
0177
0178
0179
0180
0181 msync_dbg(sdata,
0182 "TSF : kicking off TSF adjustment with clockdrift_max=%lld\n",
0183 ifmsh->sync_offset_clockdrift_max);
0184 set_bit(MESH_WORK_DRIFT_ADJUST, &ifmsh->wrkq_flags);
0185 } else {
0186 msync_dbg(sdata,
0187 "TSF : max clockdrift=%lld; too small to adjust\n",
0188 (long long)ifmsh->sync_offset_clockdrift_max);
0189 ifmsh->sync_offset_clockdrift_max = 0;
0190 }
0191 spin_unlock_bh(&ifmsh->sync_offset_lock);
0192 }
0193
0194 static const struct sync_method sync_methods[] = {
0195 {
0196 .method = IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET,
0197 .ops = {
0198 .rx_bcn_presp = &mesh_sync_offset_rx_bcn_presp,
0199 .adjust_tsf = &mesh_sync_offset_adjust_tsf,
0200 }
0201 },
0202 };
0203
0204 const struct ieee80211_mesh_sync_ops *ieee80211_mesh_sync_ops_get(u8 method)
0205 {
0206 int i;
0207
0208 for (i = 0 ; i < ARRAY_SIZE(sync_methods); ++i) {
0209 if (sync_methods[i].method == method)
0210 return &sync_methods[i].ops;
0211 }
0212 return NULL;
0213 }