Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 1 | /* |
| 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 Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 15 | /* This is not in the standard. It represents a tolerable tbtt drift below |
| 16 | * which we do no TSF adjustment. |
| 17 | */ |
Javier Cardona | a802a6e | 2012-04-12 14:32:22 -0700 | [diff] [blame] | 18 | #define TOFFSET_MINIMUM_ADJUSTMENT 10 |
| 19 | |
Javier Cardona | ec14bcd | 2012-04-12 14:32:23 -0700 | [diff] [blame] | 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. |
| 23 | */ |
| 24 | #define TOFFSET_SET_MARGIN 20 |
| 25 | |
Javier Cardona | a802a6e | 2012-04-12 14:32:22 -0700 | [diff] [blame] | 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 |
| 29 | * reset. |
Javier Cardona | ec14bcd | 2012-04-12 14:32:23 -0700 | [diff] [blame] | 30 | */ |
Javier Cardona | a802a6e | 2012-04-12 14:32:22 -0700 | [diff] [blame] | 31 | #define TOFFSET_MAXIMUM_ADJUSTMENT 30000 /* 30 ms */ |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 32 | |
| 33 | struct 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 | */ |
| 43 | static bool mesh_peer_tbtt_adjusting(struct ieee802_11_elems *ie) |
| 44 | { |
| 45 | return (ie->mesh_config->meshconf_cap & |
| 46 | MESHCONF_CAPAB_TBTT_ADJUSTING) != 0; |
| 47 | } |
| 48 | |
| 49 | void 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 Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 59 | if (ifmsh->sync_offset_clockdrift_max < beacon_int_fraction) { |
Johannes Berg | bdcbd8e | 2012-06-22 11:29:50 +0200 | [diff] [blame] | 60 | msync_dbg(sdata, "TBTT : max clockdrift=%lld; adjusting\n", |
| 61 | (long long) ifmsh->sync_offset_clockdrift_max); |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 62 | tsfdelta = -ifmsh->sync_offset_clockdrift_max; |
| 63 | ifmsh->sync_offset_clockdrift_max = 0; |
| 64 | } else { |
Johannes Berg | bdcbd8e | 2012-06-22 11:29:50 +0200 | [diff] [blame] | 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); |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 68 | tsfdelta = -beacon_int_fraction; |
| 69 | ifmsh->sync_offset_clockdrift_max -= beacon_int_fraction; |
| 70 | } |
Thomas Pedersen | 55fabef | 2012-10-05 17:57:39 -0700 | [diff] [blame] | 71 | spin_unlock_bh(&ifmsh->sync_offset_lock); |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 72 | |
| 73 | tsf = drv_get_tsf(local, sdata); |
| 74 | if (tsf != -1ULL) |
| 75 | drv_set_tsf(local, sdata, tsf + tsfdelta); |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static 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 | |
| 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); |
| 101 | |
| 102 | rcu_read_lock(); |
| 103 | sta = sta_info_get(sdata, mgmt->sa); |
| 104 | if (!sta) |
| 105 | goto no_sync; |
| 106 | |
| 107 | /* check offset sync conditions (13.13.2.2.1) |
| 108 | * |
| 109 | * TODO also sync to |
| 110 | * dot11MeshNbrOffsetMaxNeighbor non-peer non-MBSS neighbors |
| 111 | */ |
| 112 | |
| 113 | if (elems->mesh_config && mesh_peer_tbtt_adjusting(elems)) { |
| 114 | clear_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN); |
Johannes Berg | bdcbd8e | 2012-06-22 11:29:50 +0200 | [diff] [blame] | 115 | msync_dbg(sdata, "STA %pM : is adjusting TBTT\n", sta->sta.addr); |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 116 | goto no_sync; |
| 117 | } |
| 118 | |
| 119 | if (rx_status->flag & RX_FLAG_MACTIME_MPDU && rx_status->mactime) { |
| 120 | /* |
| 121 | * The mactime is defined as the time the first data symbol |
| 122 | * of the frame hits the PHY, and the timestamp of the beacon |
| 123 | * is defined as "the time that the data symbol containing the |
| 124 | * first bit of the timestamp is transmitted to the PHY plus |
| 125 | * the transmitting STA's delays through its local PHY from the |
| 126 | * MAC-PHY interface to its interface with the WM" (802.11 |
| 127 | * 11.1.2) |
| 128 | * |
| 129 | * T_r, in 13.13.2.2.2, is just defined as "the frame reception |
| 130 | * time" but we unless we interpret that time to be the same |
| 131 | * time of the beacon timestamp, the offset calculation will be |
| 132 | * off. Below we adjust t_r to be "the time at which the first |
| 133 | * symbol of the timestamp element in the beacon is received". |
| 134 | * This correction depends on the rate. |
| 135 | * |
| 136 | * Based on similar code in ibss.c |
| 137 | */ |
| 138 | int rate; |
| 139 | |
| 140 | if (rx_status->flag & RX_FLAG_HT) { |
| 141 | /* TODO: |
| 142 | * In principle there could be HT-beacons (Dual Beacon |
| 143 | * HT Operation options), but for now ignore them and |
| 144 | * just use the primary (i.e. non-HT) beacons for |
| 145 | * synchronization. |
| 146 | * */ |
| 147 | goto no_sync; |
| 148 | } else |
| 149 | rate = local->hw.wiphy->bands[rx_status->band]-> |
| 150 | bitrates[rx_status->rate_idx].bitrate; |
| 151 | |
| 152 | /* 24 bytes of header * 8 bits/byte * |
| 153 | * 10*(100 Kbps)/Mbps / rate (100 Kbps)*/ |
| 154 | t_r = rx_status->mactime + (24 * 8 * 10 / rate); |
| 155 | } |
| 156 | |
| 157 | /* Timing offset calculation (see 13.13.2.2.2) */ |
| 158 | t_t = le64_to_cpu(mgmt->u.beacon.timestamp); |
| 159 | sta->t_offset = t_t - t_r; |
| 160 | |
| 161 | if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) { |
| 162 | s64 t_clockdrift = sta->t_offset_setpoint |
| 163 | - sta->t_offset; |
Johannes Berg | bdcbd8e | 2012-06-22 11:29:50 +0200 | [diff] [blame] | 164 | msync_dbg(sdata, |
| 165 | "STA %pM : sta->t_offset=%lld, sta->t_offset_setpoint=%lld, t_clockdrift=%lld\n", |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 166 | sta->sta.addr, |
| 167 | (long long) sta->t_offset, |
| 168 | (long long) |
| 169 | sta->t_offset_setpoint, |
| 170 | (long long) t_clockdrift); |
Javier Cardona | a802a6e | 2012-04-12 14:32:22 -0700 | [diff] [blame] | 171 | |
| 172 | if (t_clockdrift > TOFFSET_MAXIMUM_ADJUSTMENT || |
| 173 | t_clockdrift < -TOFFSET_MAXIMUM_ADJUSTMENT) { |
Johannes Berg | bdcbd8e | 2012-06-22 11:29:50 +0200 | [diff] [blame] | 174 | msync_dbg(sdata, |
| 175 | "STA %pM : t_clockdrift=%lld too large, setpoint reset\n", |
Javier Cardona | a802a6e | 2012-04-12 14:32:22 -0700 | [diff] [blame] | 176 | sta->sta.addr, |
| 177 | (long long) t_clockdrift); |
| 178 | clear_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN); |
| 179 | goto no_sync; |
| 180 | } |
| 181 | |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 182 | rcu_read_unlock(); |
| 183 | |
| 184 | spin_lock_bh(&ifmsh->sync_offset_lock); |
| 185 | if (t_clockdrift > |
| 186 | ifmsh->sync_offset_clockdrift_max) |
| 187 | ifmsh->sync_offset_clockdrift_max |
| 188 | = t_clockdrift; |
| 189 | spin_unlock_bh(&ifmsh->sync_offset_lock); |
| 190 | |
| 191 | } else { |
Javier Cardona | 6ac95b5 | 2012-04-20 09:52:56 -0700 | [diff] [blame] | 192 | sta->t_offset_setpoint = sta->t_offset - TOFFSET_SET_MARGIN; |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 193 | set_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN); |
Johannes Berg | bdcbd8e | 2012-06-22 11:29:50 +0200 | [diff] [blame] | 194 | msync_dbg(sdata, |
| 195 | "STA %pM : offset was invalid, sta->t_offset=%lld\n", |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 196 | sta->sta.addr, |
| 197 | (long long) sta->t_offset); |
| 198 | rcu_read_unlock(); |
| 199 | } |
| 200 | return; |
| 201 | |
| 202 | no_sync: |
| 203 | rcu_read_unlock(); |
| 204 | } |
| 205 | |
| 206 | static void mesh_sync_offset_adjust_tbtt(struct ieee80211_sub_if_data *sdata) |
| 207 | { |
| 208 | struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh; |
| 209 | |
| 210 | WARN_ON(ifmsh->mesh_sp_id |
| 211 | != IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET); |
| 212 | BUG_ON(!rcu_read_lock_held()); |
| 213 | |
| 214 | spin_lock_bh(&ifmsh->sync_offset_lock); |
| 215 | |
| 216 | if (ifmsh->sync_offset_clockdrift_max > |
Javier Cardona | a802a6e | 2012-04-12 14:32:22 -0700 | [diff] [blame] | 217 | TOFFSET_MINIMUM_ADJUSTMENT) { |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 218 | /* Since ajusting the tsf here would |
| 219 | * require a possibly blocking call |
| 220 | * to the driver tsf setter, we punt |
| 221 | * the tsf adjustment to the mesh tasklet |
| 222 | */ |
Johannes Berg | bdcbd8e | 2012-06-22 11:29:50 +0200 | [diff] [blame] | 223 | msync_dbg(sdata, |
| 224 | "TBTT : kicking off TBTT adjustment with clockdrift_max=%lld\n", |
| 225 | ifmsh->sync_offset_clockdrift_max); |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 226 | set_bit(MESH_WORK_DRIFT_ADJUST, |
| 227 | &ifmsh->wrkq_flags); |
| 228 | } else { |
Johannes Berg | bdcbd8e | 2012-06-22 11:29:50 +0200 | [diff] [blame] | 229 | msync_dbg(sdata, |
| 230 | "TBTT : max clockdrift=%lld; too small to adjust\n", |
| 231 | (long long)ifmsh->sync_offset_clockdrift_max); |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 232 | ifmsh->sync_offset_clockdrift_max = 0; |
| 233 | } |
| 234 | spin_unlock_bh(&ifmsh->sync_offset_lock); |
| 235 | } |
| 236 | |
Johannes Berg | 8ba7acf | 2012-09-30 17:07:19 +0200 | [diff] [blame^] | 237 | static const struct sync_method sync_methods[] = { |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 238 | { |
| 239 | .method = IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET, |
| 240 | .ops = { |
| 241 | .rx_bcn_presp = &mesh_sync_offset_rx_bcn_presp, |
| 242 | .adjust_tbtt = &mesh_sync_offset_adjust_tbtt, |
| 243 | } |
| 244 | }, |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 245 | }; |
| 246 | |
Johannes Berg | 8ba7acf | 2012-09-30 17:07:19 +0200 | [diff] [blame^] | 247 | const struct ieee80211_mesh_sync_ops *ieee80211_mesh_sync_ops_get(u8 method) |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 248 | { |
Johannes Berg | 8ba7acf | 2012-09-30 17:07:19 +0200 | [diff] [blame^] | 249 | const struct ieee80211_mesh_sync_ops *ops = NULL; |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 250 | u8 i; |
| 251 | |
| 252 | for (i = 0 ; i < ARRAY_SIZE(sync_methods); ++i) { |
| 253 | if (sync_methods[i].method == method) { |
| 254 | ops = &sync_methods[i].ops; |
| 255 | break; |
| 256 | } |
| 257 | } |
| 258 | return ops; |
| 259 | } |