Johannes Berg | f444de0 | 2010-05-05 15:25:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * mac80211 - channel management |
| 3 | */ |
| 4 | |
Johannes Berg | 0aaffa9 | 2010-05-05 15:28:27 +0200 | [diff] [blame] | 5 | #include <linux/nl80211.h> |
Johannes Berg | 3448c00 | 2012-09-11 17:57:42 +0200 | [diff] [blame] | 6 | #include <linux/export.h> |
Johannes Berg | 4d76d21 | 2012-12-11 20:38:41 +0100 | [diff] [blame] | 7 | #include <linux/rtnetlink.h> |
Paul Stewart | 3117bbdb | 2012-03-13 07:46:18 -0700 | [diff] [blame] | 8 | #include <net/cfg80211.h> |
Johannes Berg | f444de0 | 2010-05-05 15:25:02 +0200 | [diff] [blame] | 9 | #include "ieee80211_i.h" |
Michal Kazior | 35f2fce | 2012-06-26 14:37:20 +0200 | [diff] [blame] | 10 | #include "driver-ops.h" |
Johannes Berg | f444de0 | 2010-05-05 15:25:02 +0200 | [diff] [blame] | 11 | |
Michal Kazior | c2b90ad | 2014-04-09 15:29:24 +0200 | [diff] [blame] | 12 | static int ieee80211_num_chanctx(struct ieee80211_local *local) |
| 13 | { |
| 14 | struct ieee80211_chanctx *ctx; |
| 15 | int num = 0; |
| 16 | |
| 17 | lockdep_assert_held(&local->chanctx_mtx); |
| 18 | |
| 19 | list_for_each_entry(ctx, &local->chanctx_list, list) |
| 20 | num++; |
| 21 | |
| 22 | return num; |
| 23 | } |
| 24 | |
| 25 | static bool ieee80211_can_create_new_chanctx(struct ieee80211_local *local) |
| 26 | { |
| 27 | lockdep_assert_held(&local->chanctx_mtx); |
| 28 | return ieee80211_num_chanctx(local) < ieee80211_max_num_channels(local); |
| 29 | } |
| 30 | |
Eliad Peller | 21f659b | 2013-11-11 20:14:01 +0200 | [diff] [blame] | 31 | static enum nl80211_chan_width ieee80211_get_sta_bw(struct ieee80211_sta *sta) |
| 32 | { |
| 33 | switch (sta->bandwidth) { |
| 34 | case IEEE80211_STA_RX_BW_20: |
| 35 | if (sta->ht_cap.ht_supported) |
| 36 | return NL80211_CHAN_WIDTH_20; |
| 37 | else |
| 38 | return NL80211_CHAN_WIDTH_20_NOHT; |
| 39 | case IEEE80211_STA_RX_BW_40: |
| 40 | return NL80211_CHAN_WIDTH_40; |
| 41 | case IEEE80211_STA_RX_BW_80: |
| 42 | return NL80211_CHAN_WIDTH_80; |
| 43 | case IEEE80211_STA_RX_BW_160: |
| 44 | /* |
| 45 | * This applied for both 160 and 80+80. since we use |
| 46 | * the returned value to consider degradation of |
| 47 | * ctx->conf.min_def, we have to make sure to take |
| 48 | * the bigger one (NL80211_CHAN_WIDTH_160). |
| 49 | * Otherwise we might try degrading even when not |
| 50 | * needed, as the max required sta_bw returned (80+80) |
| 51 | * might be smaller than the configured bw (160). |
| 52 | */ |
| 53 | return NL80211_CHAN_WIDTH_160; |
| 54 | default: |
| 55 | WARN_ON(1); |
| 56 | return NL80211_CHAN_WIDTH_20; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | static enum nl80211_chan_width |
| 61 | ieee80211_get_max_required_bw(struct ieee80211_sub_if_data *sdata) |
| 62 | { |
| 63 | enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT; |
| 64 | struct sta_info *sta; |
| 65 | |
| 66 | rcu_read_lock(); |
| 67 | list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) { |
| 68 | if (sdata != sta->sdata && |
| 69 | !(sta->sdata->bss && sta->sdata->bss == sdata->bss)) |
| 70 | continue; |
| 71 | |
| 72 | if (!sta->uploaded) |
| 73 | continue; |
| 74 | |
| 75 | max_bw = max(max_bw, ieee80211_get_sta_bw(&sta->sta)); |
| 76 | } |
| 77 | rcu_read_unlock(); |
| 78 | |
| 79 | return max_bw; |
| 80 | } |
| 81 | |
| 82 | static enum nl80211_chan_width |
| 83 | ieee80211_get_chanctx_max_required_bw(struct ieee80211_local *local, |
| 84 | struct ieee80211_chanctx_conf *conf) |
| 85 | { |
| 86 | struct ieee80211_sub_if_data *sdata; |
| 87 | enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT; |
| 88 | |
| 89 | rcu_read_lock(); |
| 90 | list_for_each_entry_rcu(sdata, &local->interfaces, list) { |
| 91 | struct ieee80211_vif *vif = &sdata->vif; |
| 92 | enum nl80211_chan_width width = NL80211_CHAN_WIDTH_20_NOHT; |
| 93 | |
| 94 | if (!ieee80211_sdata_running(sdata)) |
| 95 | continue; |
| 96 | |
| 97 | if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf) |
| 98 | continue; |
| 99 | |
| 100 | switch (vif->type) { |
| 101 | case NL80211_IFTYPE_AP: |
| 102 | case NL80211_IFTYPE_AP_VLAN: |
| 103 | width = ieee80211_get_max_required_bw(sdata); |
| 104 | break; |
| 105 | case NL80211_IFTYPE_P2P_DEVICE: |
| 106 | continue; |
| 107 | case NL80211_IFTYPE_STATION: |
| 108 | case NL80211_IFTYPE_ADHOC: |
| 109 | case NL80211_IFTYPE_WDS: |
| 110 | case NL80211_IFTYPE_MESH_POINT: |
| 111 | width = vif->bss_conf.chandef.width; |
| 112 | break; |
| 113 | case NL80211_IFTYPE_UNSPECIFIED: |
| 114 | case NUM_NL80211_IFTYPES: |
| 115 | case NL80211_IFTYPE_MONITOR: |
| 116 | case NL80211_IFTYPE_P2P_CLIENT: |
| 117 | case NL80211_IFTYPE_P2P_GO: |
| 118 | WARN_ON_ONCE(1); |
| 119 | } |
| 120 | max_bw = max(max_bw, width); |
| 121 | } |
Eliad Peller | 1c37a72 | 2014-03-03 13:37:14 +0200 | [diff] [blame] | 122 | |
| 123 | /* use the configured bandwidth in case of monitor interface */ |
| 124 | sdata = rcu_dereference(local->monitor_sdata); |
| 125 | if (sdata && rcu_access_pointer(sdata->vif.chanctx_conf) == conf) |
| 126 | max_bw = max(max_bw, conf->def.width); |
| 127 | |
Eliad Peller | 21f659b | 2013-11-11 20:14:01 +0200 | [diff] [blame] | 128 | rcu_read_unlock(); |
| 129 | |
| 130 | return max_bw; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * recalc the min required chan width of the channel context, which is |
| 135 | * the max of min required widths of all the interfaces bound to this |
| 136 | * channel context. |
| 137 | */ |
| 138 | void ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local, |
| 139 | struct ieee80211_chanctx *ctx) |
| 140 | { |
| 141 | enum nl80211_chan_width max_bw; |
| 142 | struct cfg80211_chan_def min_def; |
| 143 | |
| 144 | lockdep_assert_held(&local->chanctx_mtx); |
| 145 | |
| 146 | /* don't optimize 5MHz, 10MHz, and radar_enabled confs */ |
| 147 | if (ctx->conf.def.width == NL80211_CHAN_WIDTH_5 || |
| 148 | ctx->conf.def.width == NL80211_CHAN_WIDTH_10 || |
| 149 | ctx->conf.radar_enabled) { |
| 150 | ctx->conf.min_def = ctx->conf.def; |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | max_bw = ieee80211_get_chanctx_max_required_bw(local, &ctx->conf); |
| 155 | |
| 156 | /* downgrade chandef up to max_bw */ |
| 157 | min_def = ctx->conf.def; |
| 158 | while (min_def.width > max_bw) |
| 159 | ieee80211_chandef_downgrade(&min_def); |
| 160 | |
| 161 | if (cfg80211_chandef_identical(&ctx->conf.min_def, &min_def)) |
| 162 | return; |
| 163 | |
| 164 | ctx->conf.min_def = min_def; |
| 165 | if (!ctx->driver_present) |
| 166 | return; |
| 167 | |
| 168 | drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_MIN_WIDTH); |
| 169 | } |
| 170 | |
Johannes Berg | 18942d3 | 2013-02-07 21:30:37 +0100 | [diff] [blame] | 171 | static void ieee80211_change_chanctx(struct ieee80211_local *local, |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 172 | struct ieee80211_chanctx *ctx, |
| 173 | const struct cfg80211_chan_def *chandef) |
Michal Kazior | 23a85b45 | 2012-06-26 14:37:21 +0200 | [diff] [blame] | 174 | { |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 175 | if (cfg80211_chandef_identical(&ctx->conf.def, chandef)) |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 176 | return; |
| 177 | |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 178 | WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef)); |
| 179 | |
| 180 | ctx->conf.def = *chandef; |
| 181 | drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_WIDTH); |
Eliad Peller | 21f659b | 2013-11-11 20:14:01 +0200 | [diff] [blame] | 182 | ieee80211_recalc_chanctx_min_def(local, ctx); |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 183 | |
| 184 | if (!local->use_chanctx) { |
Karl Beldan | 675a0b0 | 2013-03-25 16:26:57 +0100 | [diff] [blame] | 185 | local->_oper_chandef = *chandef; |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 186 | ieee80211_hw_config(local, 0); |
| 187 | } |
Johannes Berg | 0aaffa9 | 2010-05-05 15:28:27 +0200 | [diff] [blame] | 188 | } |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 189 | |
Luciano Coelho | 5d52ee8 | 2014-02-27 14:33:47 +0200 | [diff] [blame] | 190 | static bool ieee80211_chanctx_is_reserved(struct ieee80211_local *local, |
| 191 | struct ieee80211_chanctx *ctx) |
| 192 | { |
| 193 | struct ieee80211_sub_if_data *sdata; |
| 194 | bool ret = false; |
| 195 | |
| 196 | lockdep_assert_held(&local->chanctx_mtx); |
| 197 | rcu_read_lock(); |
| 198 | list_for_each_entry_rcu(sdata, &local->interfaces, list) { |
| 199 | if (!ieee80211_sdata_running(sdata)) |
| 200 | continue; |
| 201 | if (sdata->reserved_chanctx == ctx) { |
| 202 | ret = true; |
| 203 | break; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | rcu_read_unlock(); |
| 208 | return ret; |
| 209 | } |
| 210 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 211 | static struct ieee80211_chanctx * |
| 212 | ieee80211_find_chanctx(struct ieee80211_local *local, |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 213 | const struct cfg80211_chan_def *chandef, |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 214 | enum ieee80211_chanctx_mode mode) |
| 215 | { |
| 216 | struct ieee80211_chanctx *ctx; |
| 217 | |
| 218 | lockdep_assert_held(&local->chanctx_mtx); |
| 219 | |
| 220 | if (mode == IEEE80211_CHANCTX_EXCLUSIVE) |
| 221 | return NULL; |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 222 | |
| 223 | list_for_each_entry(ctx, &local->chanctx_list, list) { |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 224 | const struct cfg80211_chan_def *compat; |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 225 | |
Luciano Coelho | 5d52ee8 | 2014-02-27 14:33:47 +0200 | [diff] [blame] | 226 | /* We don't support chanctx reservation for multiple |
| 227 | * vifs yet, so don't allow reserved chanctxs to be |
| 228 | * reused. |
| 229 | */ |
| 230 | if ((ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE) || |
| 231 | ieee80211_chanctx_is_reserved(local, ctx)) |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 232 | continue; |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 233 | |
| 234 | compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef); |
| 235 | if (!compat) |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 236 | continue; |
| 237 | |
Johannes Berg | 18942d3 | 2013-02-07 21:30:37 +0100 | [diff] [blame] | 238 | ieee80211_change_chanctx(local, ctx, compat); |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 239 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 240 | return ctx; |
| 241 | } |
| 242 | |
| 243 | return NULL; |
| 244 | } |
| 245 | |
Simon Wunderlich | e474685 | 2013-04-08 22:43:16 +0200 | [diff] [blame] | 246 | static bool ieee80211_is_radar_required(struct ieee80211_local *local) |
| 247 | { |
| 248 | struct ieee80211_sub_if_data *sdata; |
| 249 | |
Michal Kazior | cc901de | 2014-01-29 07:56:20 +0100 | [diff] [blame] | 250 | lockdep_assert_held(&local->mtx); |
| 251 | |
Simon Wunderlich | e474685 | 2013-04-08 22:43:16 +0200 | [diff] [blame] | 252 | rcu_read_lock(); |
| 253 | list_for_each_entry_rcu(sdata, &local->interfaces, list) { |
| 254 | if (sdata->radar_required) { |
| 255 | rcu_read_unlock(); |
| 256 | return true; |
| 257 | } |
| 258 | } |
| 259 | rcu_read_unlock(); |
| 260 | |
| 261 | return false; |
| 262 | } |
| 263 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 264 | static struct ieee80211_chanctx * |
| 265 | ieee80211_new_chanctx(struct ieee80211_local *local, |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 266 | const struct cfg80211_chan_def *chandef, |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 267 | enum ieee80211_chanctx_mode mode) |
| 268 | { |
| 269 | struct ieee80211_chanctx *ctx; |
Johannes Berg | 382a103 | 2013-03-22 22:30:09 +0100 | [diff] [blame] | 270 | u32 changed; |
Michal Kazior | 35f2fce | 2012-06-26 14:37:20 +0200 | [diff] [blame] | 271 | int err; |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 272 | |
| 273 | lockdep_assert_held(&local->chanctx_mtx); |
| 274 | |
| 275 | ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL); |
| 276 | if (!ctx) |
| 277 | return ERR_PTR(-ENOMEM); |
| 278 | |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 279 | ctx->conf.def = *chandef; |
Johannes Berg | 04ecd25 | 2012-09-11 14:34:12 +0200 | [diff] [blame] | 280 | ctx->conf.rx_chains_static = 1; |
| 281 | ctx->conf.rx_chains_dynamic = 1; |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 282 | ctx->mode = mode; |
Simon Wunderlich | e474685 | 2013-04-08 22:43:16 +0200 | [diff] [blame] | 283 | ctx->conf.radar_enabled = ieee80211_is_radar_required(local); |
Eliad Peller | 21f659b | 2013-11-11 20:14:01 +0200 | [diff] [blame] | 284 | ieee80211_recalc_chanctx_min_def(local, ctx); |
Simon Wunderlich | e474685 | 2013-04-08 22:43:16 +0200 | [diff] [blame] | 285 | if (!local->use_chanctx) |
| 286 | local->hw.conf.radar_enabled = ctx->conf.radar_enabled; |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 287 | |
Johannes Berg | 34a3740 | 2013-12-18 09:43:33 +0100 | [diff] [blame] | 288 | /* we hold the mutex to prevent idle from changing */ |
| 289 | lockdep_assert_held(&local->mtx); |
Johannes Berg | 382a103 | 2013-03-22 22:30:09 +0100 | [diff] [blame] | 290 | /* turn idle off *before* setting channel -- some drivers need that */ |
| 291 | changed = ieee80211_idle_off(local); |
| 292 | if (changed) |
| 293 | ieee80211_hw_config(local, changed); |
| 294 | |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 295 | if (!local->use_chanctx) { |
Karl Beldan | 675a0b0 | 2013-03-25 16:26:57 +0100 | [diff] [blame] | 296 | local->_oper_chandef = *chandef; |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 297 | ieee80211_hw_config(local, 0); |
| 298 | } else { |
| 299 | err = drv_add_chanctx(local, ctx); |
| 300 | if (err) { |
| 301 | kfree(ctx); |
Johannes Berg | 382a103 | 2013-03-22 22:30:09 +0100 | [diff] [blame] | 302 | ieee80211_recalc_idle(local); |
Johannes Berg | 34a3740 | 2013-12-18 09:43:33 +0100 | [diff] [blame] | 303 | return ERR_PTR(err); |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 304 | } |
Michal Kazior | 35f2fce | 2012-06-26 14:37:20 +0200 | [diff] [blame] | 305 | } |
| 306 | |
Johannes Berg | 382a103 | 2013-03-22 22:30:09 +0100 | [diff] [blame] | 307 | /* and keep the mutex held until the new chanctx is on the list */ |
Johannes Berg | 3448c00 | 2012-09-11 17:57:42 +0200 | [diff] [blame] | 308 | list_add_rcu(&ctx->list, &local->chanctx_list); |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 309 | |
| 310 | return ctx; |
| 311 | } |
| 312 | |
| 313 | static void ieee80211_free_chanctx(struct ieee80211_local *local, |
| 314 | struct ieee80211_chanctx *ctx) |
| 315 | { |
Simon Wunderlich | e474685 | 2013-04-08 22:43:16 +0200 | [diff] [blame] | 316 | bool check_single_channel = false; |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 317 | lockdep_assert_held(&local->chanctx_mtx); |
| 318 | |
| 319 | WARN_ON_ONCE(ctx->refcount != 0); |
| 320 | |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 321 | if (!local->use_chanctx) { |
Karl Beldan | 675a0b0 | 2013-03-25 16:26:57 +0100 | [diff] [blame] | 322 | struct cfg80211_chan_def *chandef = &local->_oper_chandef; |
| 323 | chandef->width = NL80211_CHAN_WIDTH_20_NOHT; |
| 324 | chandef->center_freq1 = chandef->chan->center_freq; |
| 325 | chandef->center_freq2 = 0; |
Simon Wunderlich | e474685 | 2013-04-08 22:43:16 +0200 | [diff] [blame] | 326 | |
| 327 | /* NOTE: Disabling radar is only valid here for |
| 328 | * single channel context. To be sure, check it ... |
| 329 | */ |
| 330 | if (local->hw.conf.radar_enabled) |
| 331 | check_single_channel = true; |
| 332 | local->hw.conf.radar_enabled = false; |
| 333 | |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 334 | ieee80211_hw_config(local, 0); |
| 335 | } else { |
| 336 | drv_remove_chanctx(local, ctx); |
| 337 | } |
Michal Kazior | 35f2fce | 2012-06-26 14:37:20 +0200 | [diff] [blame] | 338 | |
Johannes Berg | 3448c00 | 2012-09-11 17:57:42 +0200 | [diff] [blame] | 339 | list_del_rcu(&ctx->list); |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 340 | kfree_rcu(ctx, rcu_head); |
Johannes Berg | fd0f979 | 2013-02-07 00:14:51 +0100 | [diff] [blame] | 341 | |
Simon Wunderlich | e474685 | 2013-04-08 22:43:16 +0200 | [diff] [blame] | 342 | /* throw a warning if this wasn't the only channel context. */ |
| 343 | WARN_ON(check_single_channel && !list_empty(&local->chanctx_list)); |
| 344 | |
Johannes Berg | fd0f979 | 2013-02-07 00:14:51 +0100 | [diff] [blame] | 345 | ieee80211_recalc_idle(local); |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 346 | } |
| 347 | |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 348 | static void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local, |
| 349 | struct ieee80211_chanctx *ctx) |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 350 | { |
| 351 | struct ieee80211_chanctx_conf *conf = &ctx->conf; |
| 352 | struct ieee80211_sub_if_data *sdata; |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 353 | const struct cfg80211_chan_def *compat = NULL; |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 354 | |
| 355 | lockdep_assert_held(&local->chanctx_mtx); |
| 356 | |
| 357 | rcu_read_lock(); |
| 358 | list_for_each_entry_rcu(sdata, &local->interfaces, list) { |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 359 | |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 360 | if (!ieee80211_sdata_running(sdata)) |
| 361 | continue; |
| 362 | if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf) |
| 363 | continue; |
| 364 | |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 365 | if (!compat) |
| 366 | compat = &sdata->vif.bss_conf.chandef; |
| 367 | |
| 368 | compat = cfg80211_chandef_compatible( |
| 369 | &sdata->vif.bss_conf.chandef, compat); |
| 370 | if (!compat) |
| 371 | break; |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 372 | } |
| 373 | rcu_read_unlock(); |
| 374 | |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 375 | if (WARN_ON_ONCE(!compat)) |
| 376 | return; |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 377 | |
Johannes Berg | 18942d3 | 2013-02-07 21:30:37 +0100 | [diff] [blame] | 378 | ieee80211_change_chanctx(local, ctx, compat); |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 379 | } |
| 380 | |
Johannes Berg | 367bbd1 | 2013-12-18 09:36:09 +0100 | [diff] [blame] | 381 | static void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local, |
| 382 | struct ieee80211_chanctx *chanctx) |
| 383 | { |
| 384 | bool radar_enabled; |
| 385 | |
| 386 | lockdep_assert_held(&local->chanctx_mtx); |
Johannes Berg | 34a3740 | 2013-12-18 09:43:33 +0100 | [diff] [blame] | 387 | /* for setting local->radar_detect_enabled */ |
| 388 | lockdep_assert_held(&local->mtx); |
Johannes Berg | 367bbd1 | 2013-12-18 09:36:09 +0100 | [diff] [blame] | 389 | |
| 390 | radar_enabled = ieee80211_is_radar_required(local); |
| 391 | |
| 392 | if (radar_enabled == chanctx->conf.radar_enabled) |
| 393 | return; |
| 394 | |
| 395 | chanctx->conf.radar_enabled = radar_enabled; |
| 396 | local->radar_detect_enabled = chanctx->conf.radar_enabled; |
| 397 | |
| 398 | if (!local->use_chanctx) { |
| 399 | local->hw.conf.radar_enabled = chanctx->conf.radar_enabled; |
| 400 | ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL); |
| 401 | } |
| 402 | |
| 403 | drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR); |
| 404 | } |
| 405 | |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 406 | static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata, |
| 407 | struct ieee80211_chanctx *new_ctx) |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 408 | { |
Michal Kazior | 35f2fce | 2012-06-26 14:37:20 +0200 | [diff] [blame] | 409 | struct ieee80211_local *local = sdata->local; |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 410 | struct ieee80211_chanctx_conf *conf; |
| 411 | struct ieee80211_chanctx *curr_ctx = NULL; |
| 412 | int ret = 0; |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 413 | |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 414 | conf = rcu_dereference_protected(sdata->vif.chanctx_conf, |
| 415 | lockdep_is_held(&local->chanctx_mtx)); |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 416 | |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 417 | if (conf) { |
| 418 | curr_ctx = container_of(conf, struct ieee80211_chanctx, conf); |
Michal Kazior | 35f2fce | 2012-06-26 14:37:20 +0200 | [diff] [blame] | 419 | |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 420 | curr_ctx->refcount--; |
| 421 | drv_unassign_vif_chanctx(local, sdata, curr_ctx); |
| 422 | conf = NULL; |
| 423 | } |
| 424 | |
| 425 | if (new_ctx) { |
| 426 | ret = drv_assign_vif_chanctx(local, sdata, new_ctx); |
| 427 | if (ret) |
| 428 | goto out; |
| 429 | |
| 430 | new_ctx->refcount++; |
| 431 | conf = &new_ctx->conf; |
| 432 | } |
| 433 | |
| 434 | out: |
| 435 | rcu_assign_pointer(sdata->vif.chanctx_conf, conf); |
| 436 | |
| 437 | sdata->vif.bss_conf.idle = !conf; |
| 438 | |
| 439 | if (curr_ctx && curr_ctx->refcount > 0) { |
| 440 | ieee80211_recalc_chanctx_chantype(local, curr_ctx); |
| 441 | ieee80211_recalc_smps_chanctx(local, curr_ctx); |
| 442 | ieee80211_recalc_radar_chanctx(local, curr_ctx); |
| 443 | ieee80211_recalc_chanctx_min_def(local, curr_ctx); |
| 444 | } |
| 445 | |
| 446 | if (new_ctx && new_ctx->refcount > 0) { |
| 447 | ieee80211_recalc_txpower(sdata); |
| 448 | ieee80211_recalc_chanctx_min_def(local, new_ctx); |
| 449 | } |
Johannes Berg | 5bbe754d | 2013-02-13 13:50:51 +0100 | [diff] [blame] | 450 | |
| 451 | if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE && |
| 452 | sdata->vif.type != NL80211_IFTYPE_MONITOR) |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 453 | ieee80211_bss_info_change_notify(sdata, |
| 454 | BSS_CHANGED_IDLE); |
Johannes Berg | fd0f979 | 2013-02-07 00:14:51 +0100 | [diff] [blame] | 455 | |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 456 | return ret; |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata) |
| 460 | { |
| 461 | struct ieee80211_local *local = sdata->local; |
| 462 | struct ieee80211_chanctx_conf *conf; |
| 463 | struct ieee80211_chanctx *ctx; |
| 464 | |
| 465 | lockdep_assert_held(&local->chanctx_mtx); |
| 466 | |
| 467 | conf = rcu_dereference_protected(sdata->vif.chanctx_conf, |
| 468 | lockdep_is_held(&local->chanctx_mtx)); |
| 469 | if (!conf) |
| 470 | return; |
| 471 | |
| 472 | ctx = container_of(conf, struct ieee80211_chanctx, conf); |
| 473 | |
Luciano Coelho | 11335a5 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 474 | if (sdata->reserved_chanctx) |
| 475 | ieee80211_vif_unreserve_chanctx(sdata); |
| 476 | |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 477 | ieee80211_assign_vif_chanctx(sdata, NULL); |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 478 | if (ctx->refcount == 0) |
| 479 | ieee80211_free_chanctx(local, ctx); |
| 480 | } |
| 481 | |
Johannes Berg | 04ecd25 | 2012-09-11 14:34:12 +0200 | [diff] [blame] | 482 | void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local, |
| 483 | struct ieee80211_chanctx *chanctx) |
| 484 | { |
| 485 | struct ieee80211_sub_if_data *sdata; |
| 486 | u8 rx_chains_static, rx_chains_dynamic; |
| 487 | |
| 488 | lockdep_assert_held(&local->chanctx_mtx); |
| 489 | |
| 490 | rx_chains_static = 1; |
| 491 | rx_chains_dynamic = 1; |
| 492 | |
| 493 | rcu_read_lock(); |
| 494 | list_for_each_entry_rcu(sdata, &local->interfaces, list) { |
| 495 | u8 needed_static, needed_dynamic; |
| 496 | |
| 497 | if (!ieee80211_sdata_running(sdata)) |
| 498 | continue; |
| 499 | |
| 500 | if (rcu_access_pointer(sdata->vif.chanctx_conf) != |
| 501 | &chanctx->conf) |
| 502 | continue; |
| 503 | |
| 504 | switch (sdata->vif.type) { |
| 505 | case NL80211_IFTYPE_P2P_DEVICE: |
| 506 | continue; |
| 507 | case NL80211_IFTYPE_STATION: |
| 508 | if (!sdata->u.mgd.associated) |
| 509 | continue; |
| 510 | break; |
| 511 | case NL80211_IFTYPE_AP_VLAN: |
| 512 | continue; |
| 513 | case NL80211_IFTYPE_AP: |
| 514 | case NL80211_IFTYPE_ADHOC: |
| 515 | case NL80211_IFTYPE_WDS: |
| 516 | case NL80211_IFTYPE_MESH_POINT: |
| 517 | break; |
| 518 | default: |
| 519 | WARN_ON_ONCE(1); |
| 520 | } |
| 521 | |
| 522 | switch (sdata->smps_mode) { |
| 523 | default: |
| 524 | WARN_ONCE(1, "Invalid SMPS mode %d\n", |
| 525 | sdata->smps_mode); |
| 526 | /* fall through */ |
| 527 | case IEEE80211_SMPS_OFF: |
| 528 | needed_static = sdata->needed_rx_chains; |
| 529 | needed_dynamic = sdata->needed_rx_chains; |
| 530 | break; |
| 531 | case IEEE80211_SMPS_DYNAMIC: |
| 532 | needed_static = 1; |
| 533 | needed_dynamic = sdata->needed_rx_chains; |
| 534 | break; |
| 535 | case IEEE80211_SMPS_STATIC: |
| 536 | needed_static = 1; |
| 537 | needed_dynamic = 1; |
| 538 | break; |
| 539 | } |
| 540 | |
| 541 | rx_chains_static = max(rx_chains_static, needed_static); |
| 542 | rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic); |
| 543 | } |
| 544 | rcu_read_unlock(); |
| 545 | |
| 546 | if (!local->use_chanctx) { |
| 547 | if (rx_chains_static > 1) |
| 548 | local->smps_mode = IEEE80211_SMPS_OFF; |
| 549 | else if (rx_chains_dynamic > 1) |
| 550 | local->smps_mode = IEEE80211_SMPS_DYNAMIC; |
| 551 | else |
| 552 | local->smps_mode = IEEE80211_SMPS_STATIC; |
| 553 | ieee80211_hw_config(local, 0); |
| 554 | } |
| 555 | |
| 556 | if (rx_chains_static == chanctx->conf.rx_chains_static && |
| 557 | rx_chains_dynamic == chanctx->conf.rx_chains_dynamic) |
| 558 | return; |
| 559 | |
| 560 | chanctx->conf.rx_chains_static = rx_chains_static; |
| 561 | chanctx->conf.rx_chains_dynamic = rx_chains_dynamic; |
| 562 | drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS); |
| 563 | } |
| 564 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 565 | int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata, |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 566 | const struct cfg80211_chan_def *chandef, |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 567 | enum ieee80211_chanctx_mode mode) |
| 568 | { |
| 569 | struct ieee80211_local *local = sdata->local; |
| 570 | struct ieee80211_chanctx *ctx; |
Luciano Coelho | 73de86a | 2014-02-13 11:31:59 +0200 | [diff] [blame] | 571 | u8 radar_detect_width = 0; |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 572 | int ret; |
| 573 | |
Johannes Berg | 34a3740 | 2013-12-18 09:43:33 +0100 | [diff] [blame] | 574 | lockdep_assert_held(&local->mtx); |
| 575 | |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 576 | WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev)); |
| 577 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 578 | mutex_lock(&local->chanctx_mtx); |
Luciano Coelho | 73de86a | 2014-02-13 11:31:59 +0200 | [diff] [blame] | 579 | |
| 580 | ret = cfg80211_chandef_dfs_required(local->hw.wiphy, |
| 581 | chandef, |
| 582 | sdata->wdev.iftype); |
| 583 | if (ret < 0) |
| 584 | goto out; |
| 585 | if (ret > 0) |
| 586 | radar_detect_width = BIT(chandef->width); |
| 587 | |
| 588 | sdata->radar_required = ret; |
| 589 | |
| 590 | ret = ieee80211_check_combinations(sdata, chandef, mode, |
| 591 | radar_detect_width); |
| 592 | if (ret < 0) |
| 593 | goto out; |
| 594 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 595 | __ieee80211_vif_release_channel(sdata); |
| 596 | |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 597 | ctx = ieee80211_find_chanctx(local, chandef, mode); |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 598 | if (!ctx) |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 599 | ctx = ieee80211_new_chanctx(local, chandef, mode); |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 600 | if (IS_ERR(ctx)) { |
| 601 | ret = PTR_ERR(ctx); |
| 602 | goto out; |
| 603 | } |
| 604 | |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 605 | sdata->vif.bss_conf.chandef = *chandef; |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 606 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 607 | ret = ieee80211_assign_vif_chanctx(sdata, ctx); |
| 608 | if (ret) { |
| 609 | /* if assign fails refcount stays the same */ |
| 610 | if (ctx->refcount == 0) |
| 611 | ieee80211_free_chanctx(local, ctx); |
| 612 | goto out; |
| 613 | } |
| 614 | |
Johannes Berg | 04ecd25 | 2012-09-11 14:34:12 +0200 | [diff] [blame] | 615 | ieee80211_recalc_smps_chanctx(local, ctx); |
Simon Wunderlich | 164eb02 | 2013-02-08 18:16:20 +0100 | [diff] [blame] | 616 | ieee80211_recalc_radar_chanctx(local, ctx); |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 617 | out: |
| 618 | mutex_unlock(&local->chanctx_mtx); |
| 619 | return ret; |
| 620 | } |
| 621 | |
Luciano Coelho | 33ffd95 | 2014-01-30 22:08:16 +0200 | [diff] [blame] | 622 | static int __ieee80211_vif_change_channel(struct ieee80211_sub_if_data *sdata, |
| 623 | struct ieee80211_chanctx *ctx, |
| 624 | u32 *changed) |
Simon Wunderlich | 73da7d5 | 2013-07-11 16:09:06 +0200 | [diff] [blame] | 625 | { |
| 626 | struct ieee80211_local *local = sdata->local; |
Luciano Coelho | 33787fc | 2013-11-11 20:34:54 +0200 | [diff] [blame] | 627 | const struct cfg80211_chan_def *chandef = &sdata->csa_chandef; |
Simon Wunderlich | 73da7d5 | 2013-07-11 16:09:06 +0200 | [diff] [blame] | 628 | u32 chanctx_changed = 0; |
| 629 | |
Simon Wunderlich | 73da7d5 | 2013-07-11 16:09:06 +0200 | [diff] [blame] | 630 | if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef, |
| 631 | IEEE80211_CHAN_DISABLED)) |
| 632 | return -EINVAL; |
| 633 | |
Luciano Coelho | 33ffd95 | 2014-01-30 22:08:16 +0200 | [diff] [blame] | 634 | if (ctx->refcount != 1) |
| 635 | return -EINVAL; |
Simon Wunderlich | 73da7d5 | 2013-07-11 16:09:06 +0200 | [diff] [blame] | 636 | |
| 637 | if (sdata->vif.bss_conf.chandef.width != chandef->width) { |
| 638 | chanctx_changed = IEEE80211_CHANCTX_CHANGE_WIDTH; |
| 639 | *changed |= BSS_CHANGED_BANDWIDTH; |
| 640 | } |
| 641 | |
| 642 | sdata->vif.bss_conf.chandef = *chandef; |
| 643 | ctx->conf.def = *chandef; |
| 644 | |
| 645 | chanctx_changed |= IEEE80211_CHANCTX_CHANGE_CHANNEL; |
| 646 | drv_change_chanctx(local, ctx, chanctx_changed); |
| 647 | |
Simon Wunderlich | 73da7d5 | 2013-07-11 16:09:06 +0200 | [diff] [blame] | 648 | ieee80211_recalc_chanctx_chantype(local, ctx); |
| 649 | ieee80211_recalc_smps_chanctx(local, ctx); |
| 650 | ieee80211_recalc_radar_chanctx(local, ctx); |
Eliad Peller | 21f659b | 2013-11-11 20:14:01 +0200 | [diff] [blame] | 651 | ieee80211_recalc_chanctx_min_def(local, ctx); |
Simon Wunderlich | 73da7d5 | 2013-07-11 16:09:06 +0200 | [diff] [blame] | 652 | |
Luciano Coelho | 33ffd95 | 2014-01-30 22:08:16 +0200 | [diff] [blame] | 653 | return 0; |
| 654 | } |
| 655 | |
| 656 | int ieee80211_vif_change_channel(struct ieee80211_sub_if_data *sdata, |
| 657 | u32 *changed) |
| 658 | { |
| 659 | struct ieee80211_local *local = sdata->local; |
| 660 | struct ieee80211_chanctx_conf *conf; |
| 661 | struct ieee80211_chanctx *ctx; |
| 662 | int ret; |
| 663 | |
| 664 | lockdep_assert_held(&local->mtx); |
| 665 | |
| 666 | /* should never be called if not performing a channel switch. */ |
| 667 | if (WARN_ON(!sdata->vif.csa_active)) |
| 668 | return -EINVAL; |
| 669 | |
| 670 | mutex_lock(&local->chanctx_mtx); |
| 671 | conf = rcu_dereference_protected(sdata->vif.chanctx_conf, |
| 672 | lockdep_is_held(&local->chanctx_mtx)); |
| 673 | if (!conf) { |
| 674 | ret = -EINVAL; |
| 675 | goto out; |
| 676 | } |
| 677 | |
| 678 | ctx = container_of(conf, struct ieee80211_chanctx, conf); |
| 679 | |
| 680 | ret = __ieee80211_vif_change_channel(sdata, ctx, changed); |
Simon Wunderlich | 73da7d5 | 2013-07-11 16:09:06 +0200 | [diff] [blame] | 681 | out: |
| 682 | mutex_unlock(&local->chanctx_mtx); |
| 683 | return ret; |
| 684 | } |
| 685 | |
Luciano Coelho | 11335a5 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 686 | static void |
| 687 | __ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata, |
| 688 | bool clear) |
| 689 | { |
| 690 | struct ieee80211_local *local = sdata->local; |
| 691 | struct ieee80211_sub_if_data *vlan; |
| 692 | struct ieee80211_chanctx_conf *conf; |
| 693 | |
| 694 | if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP)) |
| 695 | return; |
| 696 | |
| 697 | lockdep_assert_held(&local->mtx); |
| 698 | |
| 699 | /* Check that conf exists, even when clearing this function |
| 700 | * must be called with the AP's channel context still there |
| 701 | * as it would otherwise cause VLANs to have an invalid |
| 702 | * channel context pointer for a while, possibly pointing |
| 703 | * to a channel context that has already been freed. |
| 704 | */ |
| 705 | conf = rcu_dereference_protected(sdata->vif.chanctx_conf, |
| 706 | lockdep_is_held(&local->chanctx_mtx)); |
| 707 | WARN_ON(!conf); |
| 708 | |
| 709 | if (clear) |
| 710 | conf = NULL; |
| 711 | |
| 712 | list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) |
| 713 | rcu_assign_pointer(vlan->vif.chanctx_conf, conf); |
| 714 | } |
| 715 | |
| 716 | void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata, |
| 717 | bool clear) |
| 718 | { |
| 719 | struct ieee80211_local *local = sdata->local; |
| 720 | |
| 721 | mutex_lock(&local->chanctx_mtx); |
| 722 | |
| 723 | __ieee80211_vif_copy_chanctx_to_vlans(sdata, clear); |
| 724 | |
| 725 | mutex_unlock(&local->chanctx_mtx); |
| 726 | } |
| 727 | |
| 728 | int ieee80211_vif_unreserve_chanctx(struct ieee80211_sub_if_data *sdata) |
| 729 | { |
| 730 | lockdep_assert_held(&sdata->local->chanctx_mtx); |
| 731 | |
| 732 | if (WARN_ON(!sdata->reserved_chanctx)) |
| 733 | return -EINVAL; |
| 734 | |
| 735 | if (--sdata->reserved_chanctx->refcount == 0) |
| 736 | ieee80211_free_chanctx(sdata->local, sdata->reserved_chanctx); |
| 737 | |
| 738 | sdata->reserved_chanctx = NULL; |
| 739 | |
| 740 | return 0; |
| 741 | } |
| 742 | |
| 743 | int ieee80211_vif_reserve_chanctx(struct ieee80211_sub_if_data *sdata, |
| 744 | const struct cfg80211_chan_def *chandef, |
Michal Kazior | 0933248 | 2014-04-09 15:29:25 +0200 | [diff] [blame^] | 745 | enum ieee80211_chanctx_mode mode, |
| 746 | bool radar_required) |
Luciano Coelho | 11335a5 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 747 | { |
| 748 | struct ieee80211_local *local = sdata->local; |
| 749 | struct ieee80211_chanctx_conf *conf; |
| 750 | struct ieee80211_chanctx *new_ctx, *curr_ctx; |
| 751 | int ret = 0; |
| 752 | |
| 753 | mutex_lock(&local->chanctx_mtx); |
| 754 | |
| 755 | conf = rcu_dereference_protected(sdata->vif.chanctx_conf, |
| 756 | lockdep_is_held(&local->chanctx_mtx)); |
| 757 | if (!conf) { |
| 758 | ret = -EINVAL; |
| 759 | goto out; |
| 760 | } |
| 761 | |
| 762 | curr_ctx = container_of(conf, struct ieee80211_chanctx, conf); |
| 763 | |
| 764 | /* try to find another context with the chandef we want */ |
| 765 | new_ctx = ieee80211_find_chanctx(local, chandef, mode); |
| 766 | if (!new_ctx) { |
Luciano Coelho | 5d52ee8 | 2014-02-27 14:33:47 +0200 | [diff] [blame] | 767 | if (curr_ctx->refcount == 1 && |
| 768 | (local->hw.flags & IEEE80211_HW_CHANGE_RUNNING_CHANCTX)) { |
| 769 | /* if we're the only users of the chanctx and |
| 770 | * the driver supports changing a running |
| 771 | * context, reserve our current context |
| 772 | */ |
| 773 | new_ctx = curr_ctx; |
Michal Kazior | c2b90ad | 2014-04-09 15:29:24 +0200 | [diff] [blame] | 774 | } else if (ieee80211_can_create_new_chanctx(local)) { |
Luciano Coelho | 5d52ee8 | 2014-02-27 14:33:47 +0200 | [diff] [blame] | 775 | /* create a new context and reserve it */ |
| 776 | new_ctx = ieee80211_new_chanctx(local, chandef, mode); |
| 777 | if (IS_ERR(new_ctx)) { |
| 778 | ret = PTR_ERR(new_ctx); |
| 779 | goto out; |
| 780 | } |
Michal Kazior | c2b90ad | 2014-04-09 15:29:24 +0200 | [diff] [blame] | 781 | } else { |
| 782 | ret = -EBUSY; |
| 783 | goto out; |
Luciano Coelho | 11335a5 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 784 | } |
| 785 | } |
| 786 | |
| 787 | new_ctx->refcount++; |
| 788 | sdata->reserved_chanctx = new_ctx; |
| 789 | sdata->reserved_chandef = *chandef; |
Michal Kazior | 0933248 | 2014-04-09 15:29:25 +0200 | [diff] [blame^] | 790 | sdata->reserved_radar_required = radar_required; |
Luciano Coelho | 11335a5 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 791 | out: |
| 792 | mutex_unlock(&local->chanctx_mtx); |
| 793 | return ret; |
| 794 | } |
| 795 | |
| 796 | int ieee80211_vif_use_reserved_context(struct ieee80211_sub_if_data *sdata, |
| 797 | u32 *changed) |
| 798 | { |
| 799 | struct ieee80211_local *local = sdata->local; |
| 800 | struct ieee80211_chanctx *ctx; |
| 801 | struct ieee80211_chanctx *old_ctx; |
| 802 | struct ieee80211_chanctx_conf *conf; |
| 803 | int ret; |
| 804 | u32 tmp_changed = *changed; |
| 805 | |
| 806 | /* TODO: need to recheck if the chandef is usable etc.? */ |
| 807 | |
| 808 | lockdep_assert_held(&local->mtx); |
| 809 | |
| 810 | mutex_lock(&local->chanctx_mtx); |
| 811 | |
| 812 | ctx = sdata->reserved_chanctx; |
| 813 | if (WARN_ON(!ctx)) { |
| 814 | ret = -EINVAL; |
| 815 | goto out; |
| 816 | } |
| 817 | |
| 818 | conf = rcu_dereference_protected(sdata->vif.chanctx_conf, |
| 819 | lockdep_is_held(&local->chanctx_mtx)); |
| 820 | if (!conf) { |
| 821 | ret = -EINVAL; |
| 822 | goto out; |
| 823 | } |
| 824 | |
| 825 | old_ctx = container_of(conf, struct ieee80211_chanctx, conf); |
| 826 | |
| 827 | if (sdata->vif.bss_conf.chandef.width != sdata->reserved_chandef.width) |
| 828 | tmp_changed |= BSS_CHANGED_BANDWIDTH; |
| 829 | |
| 830 | sdata->vif.bss_conf.chandef = sdata->reserved_chandef; |
| 831 | |
Luciano Coelho | 5d52ee8 | 2014-02-27 14:33:47 +0200 | [diff] [blame] | 832 | /* unref our reservation */ |
Luciano Coelho | 11335a5 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 833 | ctx->refcount--; |
| 834 | sdata->reserved_chanctx = NULL; |
Michal Kazior | 0933248 | 2014-04-09 15:29:25 +0200 | [diff] [blame^] | 835 | sdata->radar_required = sdata->reserved_radar_required; |
Luciano Coelho | 11335a5 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 836 | |
Luciano Coelho | 5d52ee8 | 2014-02-27 14:33:47 +0200 | [diff] [blame] | 837 | if (old_ctx == ctx) { |
| 838 | /* This is our own context, just change it */ |
| 839 | ret = __ieee80211_vif_change_channel(sdata, old_ctx, |
| 840 | &tmp_changed); |
| 841 | if (ret) |
| 842 | goto out; |
| 843 | } else { |
| 844 | ret = ieee80211_assign_vif_chanctx(sdata, ctx); |
| 845 | if (old_ctx->refcount == 0) |
| 846 | ieee80211_free_chanctx(local, old_ctx); |
| 847 | if (ret) { |
| 848 | /* if assign fails refcount stays the same */ |
| 849 | if (ctx->refcount == 0) |
| 850 | ieee80211_free_chanctx(local, ctx); |
| 851 | goto out; |
| 852 | } |
Luciano Coelho | 11335a5 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 853 | |
Luciano Coelho | 5d52ee8 | 2014-02-27 14:33:47 +0200 | [diff] [blame] | 854 | if (sdata->vif.type == NL80211_IFTYPE_AP) |
| 855 | __ieee80211_vif_copy_chanctx_to_vlans(sdata, false); |
| 856 | } |
Luciano Coelho | 11335a5 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 857 | |
| 858 | *changed = tmp_changed; |
| 859 | |
| 860 | ieee80211_recalc_chanctx_chantype(local, ctx); |
| 861 | ieee80211_recalc_smps_chanctx(local, ctx); |
| 862 | ieee80211_recalc_radar_chanctx(local, ctx); |
| 863 | ieee80211_recalc_chanctx_min_def(local, ctx); |
| 864 | out: |
| 865 | mutex_unlock(&local->chanctx_mtx); |
| 866 | return ret; |
| 867 | } |
| 868 | |
Johannes Berg | 2c9b735 | 2013-02-07 21:37:29 +0100 | [diff] [blame] | 869 | int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata, |
| 870 | const struct cfg80211_chan_def *chandef, |
| 871 | u32 *changed) |
| 872 | { |
| 873 | struct ieee80211_local *local = sdata->local; |
| 874 | struct ieee80211_chanctx_conf *conf; |
| 875 | struct ieee80211_chanctx *ctx; |
| 876 | int ret; |
| 877 | |
| 878 | if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef, |
| 879 | IEEE80211_CHAN_DISABLED)) |
| 880 | return -EINVAL; |
| 881 | |
| 882 | mutex_lock(&local->chanctx_mtx); |
| 883 | if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) { |
| 884 | ret = 0; |
| 885 | goto out; |
| 886 | } |
| 887 | |
| 888 | if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT || |
| 889 | sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) { |
| 890 | ret = -EINVAL; |
| 891 | goto out; |
| 892 | } |
| 893 | |
| 894 | conf = rcu_dereference_protected(sdata->vif.chanctx_conf, |
| 895 | lockdep_is_held(&local->chanctx_mtx)); |
| 896 | if (!conf) { |
| 897 | ret = -EINVAL; |
| 898 | goto out; |
| 899 | } |
| 900 | |
| 901 | ctx = container_of(conf, struct ieee80211_chanctx, conf); |
| 902 | if (!cfg80211_chandef_compatible(&conf->def, chandef)) { |
| 903 | ret = -EINVAL; |
| 904 | goto out; |
| 905 | } |
| 906 | |
| 907 | sdata->vif.bss_conf.chandef = *chandef; |
| 908 | |
| 909 | ieee80211_recalc_chanctx_chantype(local, ctx); |
| 910 | |
| 911 | *changed |= BSS_CHANGED_BANDWIDTH; |
| 912 | ret = 0; |
| 913 | out: |
| 914 | mutex_unlock(&local->chanctx_mtx); |
| 915 | return ret; |
| 916 | } |
| 917 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 918 | void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata) |
| 919 | { |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 920 | WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev)); |
| 921 | |
Johannes Berg | 34a3740 | 2013-12-18 09:43:33 +0100 | [diff] [blame] | 922 | lockdep_assert_held(&sdata->local->mtx); |
| 923 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 924 | mutex_lock(&sdata->local->chanctx_mtx); |
| 925 | __ieee80211_vif_release_channel(sdata); |
| 926 | mutex_unlock(&sdata->local->chanctx_mtx); |
| 927 | } |
Johannes Berg | 3448c00 | 2012-09-11 17:57:42 +0200 | [diff] [blame] | 928 | |
Johannes Berg | 4d76d21 | 2012-12-11 20:38:41 +0100 | [diff] [blame] | 929 | void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata) |
| 930 | { |
| 931 | struct ieee80211_local *local = sdata->local; |
| 932 | struct ieee80211_sub_if_data *ap; |
| 933 | struct ieee80211_chanctx_conf *conf; |
| 934 | |
| 935 | if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss)) |
| 936 | return; |
| 937 | |
| 938 | ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap); |
| 939 | |
| 940 | mutex_lock(&local->chanctx_mtx); |
| 941 | |
| 942 | conf = rcu_dereference_protected(ap->vif.chanctx_conf, |
| 943 | lockdep_is_held(&local->chanctx_mtx)); |
| 944 | rcu_assign_pointer(sdata->vif.chanctx_conf, conf); |
| 945 | mutex_unlock(&local->chanctx_mtx); |
| 946 | } |
| 947 | |
Johannes Berg | 3448c00 | 2012-09-11 17:57:42 +0200 | [diff] [blame] | 948 | void ieee80211_iter_chan_contexts_atomic( |
| 949 | struct ieee80211_hw *hw, |
| 950 | void (*iter)(struct ieee80211_hw *hw, |
| 951 | struct ieee80211_chanctx_conf *chanctx_conf, |
| 952 | void *data), |
| 953 | void *iter_data) |
| 954 | { |
| 955 | struct ieee80211_local *local = hw_to_local(hw); |
| 956 | struct ieee80211_chanctx *ctx; |
| 957 | |
| 958 | rcu_read_lock(); |
| 959 | list_for_each_entry_rcu(ctx, &local->chanctx_list, list) |
Johannes Berg | 8a61af6 | 2012-12-13 17:42:30 +0100 | [diff] [blame] | 960 | if (ctx->driver_present) |
| 961 | iter(hw, &ctx->conf, iter_data); |
Johannes Berg | 3448c00 | 2012-09-11 17:57:42 +0200 | [diff] [blame] | 962 | rcu_read_unlock(); |
| 963 | } |
| 964 | EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic); |