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 | |
Michal Kazior | 484298a | 2014-04-09 15:29:26 +0200 | [diff] [blame] | 279 | INIT_LIST_HEAD(&ctx->assigned_vifs); |
Michal Kazior | e3afb92 | 2014-04-09 15:29:27 +0200 | [diff] [blame^] | 280 | INIT_LIST_HEAD(&ctx->reserved_vifs); |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 281 | ctx->conf.def = *chandef; |
Johannes Berg | 04ecd25 | 2012-09-11 14:34:12 +0200 | [diff] [blame] | 282 | ctx->conf.rx_chains_static = 1; |
| 283 | ctx->conf.rx_chains_dynamic = 1; |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 284 | ctx->mode = mode; |
Simon Wunderlich | e474685 | 2013-04-08 22:43:16 +0200 | [diff] [blame] | 285 | ctx->conf.radar_enabled = ieee80211_is_radar_required(local); |
Eliad Peller | 21f659b | 2013-11-11 20:14:01 +0200 | [diff] [blame] | 286 | ieee80211_recalc_chanctx_min_def(local, ctx); |
Simon Wunderlich | e474685 | 2013-04-08 22:43:16 +0200 | [diff] [blame] | 287 | if (!local->use_chanctx) |
| 288 | local->hw.conf.radar_enabled = ctx->conf.radar_enabled; |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 289 | |
Johannes Berg | 34a3740 | 2013-12-18 09:43:33 +0100 | [diff] [blame] | 290 | /* we hold the mutex to prevent idle from changing */ |
| 291 | lockdep_assert_held(&local->mtx); |
Johannes Berg | 382a103 | 2013-03-22 22:30:09 +0100 | [diff] [blame] | 292 | /* turn idle off *before* setting channel -- some drivers need that */ |
| 293 | changed = ieee80211_idle_off(local); |
| 294 | if (changed) |
| 295 | ieee80211_hw_config(local, changed); |
| 296 | |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 297 | if (!local->use_chanctx) { |
Karl Beldan | 675a0b0 | 2013-03-25 16:26:57 +0100 | [diff] [blame] | 298 | local->_oper_chandef = *chandef; |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 299 | ieee80211_hw_config(local, 0); |
| 300 | } else { |
| 301 | err = drv_add_chanctx(local, ctx); |
| 302 | if (err) { |
| 303 | kfree(ctx); |
Johannes Berg | 382a103 | 2013-03-22 22:30:09 +0100 | [diff] [blame] | 304 | ieee80211_recalc_idle(local); |
Johannes Berg | 34a3740 | 2013-12-18 09:43:33 +0100 | [diff] [blame] | 305 | return ERR_PTR(err); |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 306 | } |
Michal Kazior | 35f2fce | 2012-06-26 14:37:20 +0200 | [diff] [blame] | 307 | } |
| 308 | |
Johannes Berg | 382a103 | 2013-03-22 22:30:09 +0100 | [diff] [blame] | 309 | /* 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] | 310 | list_add_rcu(&ctx->list, &local->chanctx_list); |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 311 | |
| 312 | return ctx; |
| 313 | } |
| 314 | |
| 315 | static void ieee80211_free_chanctx(struct ieee80211_local *local, |
| 316 | struct ieee80211_chanctx *ctx) |
| 317 | { |
Simon Wunderlich | e474685 | 2013-04-08 22:43:16 +0200 | [diff] [blame] | 318 | bool check_single_channel = false; |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 319 | lockdep_assert_held(&local->chanctx_mtx); |
| 320 | |
| 321 | WARN_ON_ONCE(ctx->refcount != 0); |
| 322 | |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 323 | if (!local->use_chanctx) { |
Karl Beldan | 675a0b0 | 2013-03-25 16:26:57 +0100 | [diff] [blame] | 324 | struct cfg80211_chan_def *chandef = &local->_oper_chandef; |
| 325 | chandef->width = NL80211_CHAN_WIDTH_20_NOHT; |
| 326 | chandef->center_freq1 = chandef->chan->center_freq; |
| 327 | chandef->center_freq2 = 0; |
Simon Wunderlich | e474685 | 2013-04-08 22:43:16 +0200 | [diff] [blame] | 328 | |
| 329 | /* NOTE: Disabling radar is only valid here for |
| 330 | * single channel context. To be sure, check it ... |
| 331 | */ |
| 332 | if (local->hw.conf.radar_enabled) |
| 333 | check_single_channel = true; |
| 334 | local->hw.conf.radar_enabled = false; |
| 335 | |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 336 | ieee80211_hw_config(local, 0); |
| 337 | } else { |
| 338 | drv_remove_chanctx(local, ctx); |
| 339 | } |
Michal Kazior | 35f2fce | 2012-06-26 14:37:20 +0200 | [diff] [blame] | 340 | |
Johannes Berg | 3448c00 | 2012-09-11 17:57:42 +0200 | [diff] [blame] | 341 | list_del_rcu(&ctx->list); |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 342 | kfree_rcu(ctx, rcu_head); |
Johannes Berg | fd0f979 | 2013-02-07 00:14:51 +0100 | [diff] [blame] | 343 | |
Simon Wunderlich | e474685 | 2013-04-08 22:43:16 +0200 | [diff] [blame] | 344 | /* throw a warning if this wasn't the only channel context. */ |
| 345 | WARN_ON(check_single_channel && !list_empty(&local->chanctx_list)); |
| 346 | |
Johannes Berg | fd0f979 | 2013-02-07 00:14:51 +0100 | [diff] [blame] | 347 | ieee80211_recalc_idle(local); |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 348 | } |
| 349 | |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 350 | static void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local, |
| 351 | struct ieee80211_chanctx *ctx) |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 352 | { |
| 353 | struct ieee80211_chanctx_conf *conf = &ctx->conf; |
| 354 | struct ieee80211_sub_if_data *sdata; |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 355 | const struct cfg80211_chan_def *compat = NULL; |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 356 | |
| 357 | lockdep_assert_held(&local->chanctx_mtx); |
| 358 | |
| 359 | rcu_read_lock(); |
| 360 | list_for_each_entry_rcu(sdata, &local->interfaces, list) { |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 361 | |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 362 | if (!ieee80211_sdata_running(sdata)) |
| 363 | continue; |
| 364 | if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf) |
| 365 | continue; |
| 366 | |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 367 | if (!compat) |
| 368 | compat = &sdata->vif.bss_conf.chandef; |
| 369 | |
| 370 | compat = cfg80211_chandef_compatible( |
| 371 | &sdata->vif.bss_conf.chandef, compat); |
| 372 | if (!compat) |
| 373 | break; |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 374 | } |
| 375 | rcu_read_unlock(); |
| 376 | |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 377 | if (WARN_ON_ONCE(!compat)) |
| 378 | return; |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 379 | |
Johannes Berg | 18942d3 | 2013-02-07 21:30:37 +0100 | [diff] [blame] | 380 | ieee80211_change_chanctx(local, ctx, compat); |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 381 | } |
| 382 | |
Johannes Berg | 367bbd1 | 2013-12-18 09:36:09 +0100 | [diff] [blame] | 383 | static void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local, |
| 384 | struct ieee80211_chanctx *chanctx) |
| 385 | { |
| 386 | bool radar_enabled; |
| 387 | |
| 388 | lockdep_assert_held(&local->chanctx_mtx); |
Johannes Berg | 34a3740 | 2013-12-18 09:43:33 +0100 | [diff] [blame] | 389 | /* for setting local->radar_detect_enabled */ |
| 390 | lockdep_assert_held(&local->mtx); |
Johannes Berg | 367bbd1 | 2013-12-18 09:36:09 +0100 | [diff] [blame] | 391 | |
| 392 | radar_enabled = ieee80211_is_radar_required(local); |
| 393 | |
| 394 | if (radar_enabled == chanctx->conf.radar_enabled) |
| 395 | return; |
| 396 | |
| 397 | chanctx->conf.radar_enabled = radar_enabled; |
| 398 | local->radar_detect_enabled = chanctx->conf.radar_enabled; |
| 399 | |
| 400 | if (!local->use_chanctx) { |
| 401 | local->hw.conf.radar_enabled = chanctx->conf.radar_enabled; |
| 402 | ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL); |
| 403 | } |
| 404 | |
| 405 | drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR); |
| 406 | } |
| 407 | |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 408 | static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata, |
| 409 | struct ieee80211_chanctx *new_ctx) |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 410 | { |
Michal Kazior | 35f2fce | 2012-06-26 14:37:20 +0200 | [diff] [blame] | 411 | struct ieee80211_local *local = sdata->local; |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 412 | struct ieee80211_chanctx_conf *conf; |
| 413 | struct ieee80211_chanctx *curr_ctx = NULL; |
| 414 | int ret = 0; |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 415 | |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 416 | conf = rcu_dereference_protected(sdata->vif.chanctx_conf, |
| 417 | lockdep_is_held(&local->chanctx_mtx)); |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 418 | |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 419 | if (conf) { |
| 420 | curr_ctx = container_of(conf, struct ieee80211_chanctx, conf); |
Michal Kazior | 35f2fce | 2012-06-26 14:37:20 +0200 | [diff] [blame] | 421 | |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 422 | curr_ctx->refcount--; |
| 423 | drv_unassign_vif_chanctx(local, sdata, curr_ctx); |
| 424 | conf = NULL; |
Michal Kazior | 484298a | 2014-04-09 15:29:26 +0200 | [diff] [blame] | 425 | list_del(&sdata->assigned_chanctx_list); |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | if (new_ctx) { |
| 429 | ret = drv_assign_vif_chanctx(local, sdata, new_ctx); |
| 430 | if (ret) |
| 431 | goto out; |
| 432 | |
| 433 | new_ctx->refcount++; |
| 434 | conf = &new_ctx->conf; |
Michal Kazior | 484298a | 2014-04-09 15:29:26 +0200 | [diff] [blame] | 435 | list_add(&sdata->assigned_chanctx_list, |
| 436 | &new_ctx->assigned_vifs); |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | out: |
| 440 | rcu_assign_pointer(sdata->vif.chanctx_conf, conf); |
| 441 | |
| 442 | sdata->vif.bss_conf.idle = !conf; |
| 443 | |
| 444 | if (curr_ctx && curr_ctx->refcount > 0) { |
| 445 | ieee80211_recalc_chanctx_chantype(local, curr_ctx); |
| 446 | ieee80211_recalc_smps_chanctx(local, curr_ctx); |
| 447 | ieee80211_recalc_radar_chanctx(local, curr_ctx); |
| 448 | ieee80211_recalc_chanctx_min_def(local, curr_ctx); |
| 449 | } |
| 450 | |
| 451 | if (new_ctx && new_ctx->refcount > 0) { |
| 452 | ieee80211_recalc_txpower(sdata); |
| 453 | ieee80211_recalc_chanctx_min_def(local, new_ctx); |
| 454 | } |
Johannes Berg | 5bbe754d | 2013-02-13 13:50:51 +0100 | [diff] [blame] | 455 | |
| 456 | if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE && |
| 457 | sdata->vif.type != NL80211_IFTYPE_MONITOR) |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 458 | ieee80211_bss_info_change_notify(sdata, |
| 459 | BSS_CHANGED_IDLE); |
Johannes Berg | fd0f979 | 2013-02-07 00:14:51 +0100 | [diff] [blame] | 460 | |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 461 | return ret; |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata) |
| 465 | { |
| 466 | struct ieee80211_local *local = sdata->local; |
| 467 | struct ieee80211_chanctx_conf *conf; |
| 468 | struct ieee80211_chanctx *ctx; |
| 469 | |
| 470 | lockdep_assert_held(&local->chanctx_mtx); |
| 471 | |
| 472 | conf = rcu_dereference_protected(sdata->vif.chanctx_conf, |
| 473 | lockdep_is_held(&local->chanctx_mtx)); |
| 474 | if (!conf) |
| 475 | return; |
| 476 | |
| 477 | ctx = container_of(conf, struct ieee80211_chanctx, conf); |
| 478 | |
Luciano Coelho | 11335a5 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 479 | if (sdata->reserved_chanctx) |
| 480 | ieee80211_vif_unreserve_chanctx(sdata); |
| 481 | |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 482 | ieee80211_assign_vif_chanctx(sdata, NULL); |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 483 | if (ctx->refcount == 0) |
| 484 | ieee80211_free_chanctx(local, ctx); |
| 485 | } |
| 486 | |
Johannes Berg | 04ecd25 | 2012-09-11 14:34:12 +0200 | [diff] [blame] | 487 | void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local, |
| 488 | struct ieee80211_chanctx *chanctx) |
| 489 | { |
| 490 | struct ieee80211_sub_if_data *sdata; |
| 491 | u8 rx_chains_static, rx_chains_dynamic; |
| 492 | |
| 493 | lockdep_assert_held(&local->chanctx_mtx); |
| 494 | |
| 495 | rx_chains_static = 1; |
| 496 | rx_chains_dynamic = 1; |
| 497 | |
| 498 | rcu_read_lock(); |
| 499 | list_for_each_entry_rcu(sdata, &local->interfaces, list) { |
| 500 | u8 needed_static, needed_dynamic; |
| 501 | |
| 502 | if (!ieee80211_sdata_running(sdata)) |
| 503 | continue; |
| 504 | |
| 505 | if (rcu_access_pointer(sdata->vif.chanctx_conf) != |
| 506 | &chanctx->conf) |
| 507 | continue; |
| 508 | |
| 509 | switch (sdata->vif.type) { |
| 510 | case NL80211_IFTYPE_P2P_DEVICE: |
| 511 | continue; |
| 512 | case NL80211_IFTYPE_STATION: |
| 513 | if (!sdata->u.mgd.associated) |
| 514 | continue; |
| 515 | break; |
| 516 | case NL80211_IFTYPE_AP_VLAN: |
| 517 | continue; |
| 518 | case NL80211_IFTYPE_AP: |
| 519 | case NL80211_IFTYPE_ADHOC: |
| 520 | case NL80211_IFTYPE_WDS: |
| 521 | case NL80211_IFTYPE_MESH_POINT: |
| 522 | break; |
| 523 | default: |
| 524 | WARN_ON_ONCE(1); |
| 525 | } |
| 526 | |
| 527 | switch (sdata->smps_mode) { |
| 528 | default: |
| 529 | WARN_ONCE(1, "Invalid SMPS mode %d\n", |
| 530 | sdata->smps_mode); |
| 531 | /* fall through */ |
| 532 | case IEEE80211_SMPS_OFF: |
| 533 | needed_static = sdata->needed_rx_chains; |
| 534 | needed_dynamic = sdata->needed_rx_chains; |
| 535 | break; |
| 536 | case IEEE80211_SMPS_DYNAMIC: |
| 537 | needed_static = 1; |
| 538 | needed_dynamic = sdata->needed_rx_chains; |
| 539 | break; |
| 540 | case IEEE80211_SMPS_STATIC: |
| 541 | needed_static = 1; |
| 542 | needed_dynamic = 1; |
| 543 | break; |
| 544 | } |
| 545 | |
| 546 | rx_chains_static = max(rx_chains_static, needed_static); |
| 547 | rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic); |
| 548 | } |
| 549 | rcu_read_unlock(); |
| 550 | |
| 551 | if (!local->use_chanctx) { |
| 552 | if (rx_chains_static > 1) |
| 553 | local->smps_mode = IEEE80211_SMPS_OFF; |
| 554 | else if (rx_chains_dynamic > 1) |
| 555 | local->smps_mode = IEEE80211_SMPS_DYNAMIC; |
| 556 | else |
| 557 | local->smps_mode = IEEE80211_SMPS_STATIC; |
| 558 | ieee80211_hw_config(local, 0); |
| 559 | } |
| 560 | |
| 561 | if (rx_chains_static == chanctx->conf.rx_chains_static && |
| 562 | rx_chains_dynamic == chanctx->conf.rx_chains_dynamic) |
| 563 | return; |
| 564 | |
| 565 | chanctx->conf.rx_chains_static = rx_chains_static; |
| 566 | chanctx->conf.rx_chains_dynamic = rx_chains_dynamic; |
| 567 | drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS); |
| 568 | } |
| 569 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 570 | int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata, |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 571 | const struct cfg80211_chan_def *chandef, |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 572 | enum ieee80211_chanctx_mode mode) |
| 573 | { |
| 574 | struct ieee80211_local *local = sdata->local; |
| 575 | struct ieee80211_chanctx *ctx; |
Luciano Coelho | 73de86a | 2014-02-13 11:31:59 +0200 | [diff] [blame] | 576 | u8 radar_detect_width = 0; |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 577 | int ret; |
| 578 | |
Johannes Berg | 34a3740 | 2013-12-18 09:43:33 +0100 | [diff] [blame] | 579 | lockdep_assert_held(&local->mtx); |
| 580 | |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 581 | WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev)); |
| 582 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 583 | mutex_lock(&local->chanctx_mtx); |
Luciano Coelho | 73de86a | 2014-02-13 11:31:59 +0200 | [diff] [blame] | 584 | |
| 585 | ret = cfg80211_chandef_dfs_required(local->hw.wiphy, |
| 586 | chandef, |
| 587 | sdata->wdev.iftype); |
| 588 | if (ret < 0) |
| 589 | goto out; |
| 590 | if (ret > 0) |
| 591 | radar_detect_width = BIT(chandef->width); |
| 592 | |
| 593 | sdata->radar_required = ret; |
| 594 | |
| 595 | ret = ieee80211_check_combinations(sdata, chandef, mode, |
| 596 | radar_detect_width); |
| 597 | if (ret < 0) |
| 598 | goto out; |
| 599 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 600 | __ieee80211_vif_release_channel(sdata); |
| 601 | |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 602 | ctx = ieee80211_find_chanctx(local, chandef, mode); |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 603 | if (!ctx) |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 604 | ctx = ieee80211_new_chanctx(local, chandef, mode); |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 605 | if (IS_ERR(ctx)) { |
| 606 | ret = PTR_ERR(ctx); |
| 607 | goto out; |
| 608 | } |
| 609 | |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 610 | sdata->vif.bss_conf.chandef = *chandef; |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 611 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 612 | ret = ieee80211_assign_vif_chanctx(sdata, ctx); |
| 613 | if (ret) { |
| 614 | /* if assign fails refcount stays the same */ |
| 615 | if (ctx->refcount == 0) |
| 616 | ieee80211_free_chanctx(local, ctx); |
| 617 | goto out; |
| 618 | } |
| 619 | |
Johannes Berg | 04ecd25 | 2012-09-11 14:34:12 +0200 | [diff] [blame] | 620 | ieee80211_recalc_smps_chanctx(local, ctx); |
Simon Wunderlich | 164eb02 | 2013-02-08 18:16:20 +0100 | [diff] [blame] | 621 | ieee80211_recalc_radar_chanctx(local, ctx); |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 622 | out: |
| 623 | mutex_unlock(&local->chanctx_mtx); |
| 624 | return ret; |
| 625 | } |
| 626 | |
Luciano Coelho | 33ffd95 | 2014-01-30 22:08:16 +0200 | [diff] [blame] | 627 | static int __ieee80211_vif_change_channel(struct ieee80211_sub_if_data *sdata, |
| 628 | struct ieee80211_chanctx *ctx, |
| 629 | u32 *changed) |
Simon Wunderlich | 73da7d5 | 2013-07-11 16:09:06 +0200 | [diff] [blame] | 630 | { |
| 631 | struct ieee80211_local *local = sdata->local; |
Luciano Coelho | 33787fc | 2013-11-11 20:34:54 +0200 | [diff] [blame] | 632 | const struct cfg80211_chan_def *chandef = &sdata->csa_chandef; |
Simon Wunderlich | 73da7d5 | 2013-07-11 16:09:06 +0200 | [diff] [blame] | 633 | u32 chanctx_changed = 0; |
| 634 | |
Simon Wunderlich | 73da7d5 | 2013-07-11 16:09:06 +0200 | [diff] [blame] | 635 | if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef, |
| 636 | IEEE80211_CHAN_DISABLED)) |
| 637 | return -EINVAL; |
| 638 | |
Luciano Coelho | 33ffd95 | 2014-01-30 22:08:16 +0200 | [diff] [blame] | 639 | if (ctx->refcount != 1) |
| 640 | return -EINVAL; |
Simon Wunderlich | 73da7d5 | 2013-07-11 16:09:06 +0200 | [diff] [blame] | 641 | |
| 642 | if (sdata->vif.bss_conf.chandef.width != chandef->width) { |
| 643 | chanctx_changed = IEEE80211_CHANCTX_CHANGE_WIDTH; |
| 644 | *changed |= BSS_CHANGED_BANDWIDTH; |
| 645 | } |
| 646 | |
| 647 | sdata->vif.bss_conf.chandef = *chandef; |
| 648 | ctx->conf.def = *chandef; |
| 649 | |
| 650 | chanctx_changed |= IEEE80211_CHANCTX_CHANGE_CHANNEL; |
| 651 | drv_change_chanctx(local, ctx, chanctx_changed); |
| 652 | |
Simon Wunderlich | 73da7d5 | 2013-07-11 16:09:06 +0200 | [diff] [blame] | 653 | ieee80211_recalc_chanctx_chantype(local, ctx); |
| 654 | ieee80211_recalc_smps_chanctx(local, ctx); |
| 655 | ieee80211_recalc_radar_chanctx(local, ctx); |
Eliad Peller | 21f659b | 2013-11-11 20:14:01 +0200 | [diff] [blame] | 656 | ieee80211_recalc_chanctx_min_def(local, ctx); |
Simon Wunderlich | 73da7d5 | 2013-07-11 16:09:06 +0200 | [diff] [blame] | 657 | |
Luciano Coelho | 33ffd95 | 2014-01-30 22:08:16 +0200 | [diff] [blame] | 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | int ieee80211_vif_change_channel(struct ieee80211_sub_if_data *sdata, |
| 662 | u32 *changed) |
| 663 | { |
| 664 | struct ieee80211_local *local = sdata->local; |
| 665 | struct ieee80211_chanctx_conf *conf; |
| 666 | struct ieee80211_chanctx *ctx; |
| 667 | int ret; |
| 668 | |
| 669 | lockdep_assert_held(&local->mtx); |
| 670 | |
| 671 | /* should never be called if not performing a channel switch. */ |
| 672 | if (WARN_ON(!sdata->vif.csa_active)) |
| 673 | return -EINVAL; |
| 674 | |
| 675 | mutex_lock(&local->chanctx_mtx); |
| 676 | conf = rcu_dereference_protected(sdata->vif.chanctx_conf, |
| 677 | lockdep_is_held(&local->chanctx_mtx)); |
| 678 | if (!conf) { |
| 679 | ret = -EINVAL; |
| 680 | goto out; |
| 681 | } |
| 682 | |
| 683 | ctx = container_of(conf, struct ieee80211_chanctx, conf); |
| 684 | |
| 685 | ret = __ieee80211_vif_change_channel(sdata, ctx, changed); |
Simon Wunderlich | 73da7d5 | 2013-07-11 16:09:06 +0200 | [diff] [blame] | 686 | out: |
| 687 | mutex_unlock(&local->chanctx_mtx); |
| 688 | return ret; |
| 689 | } |
| 690 | |
Luciano Coelho | 11335a5 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 691 | static void |
| 692 | __ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata, |
| 693 | bool clear) |
| 694 | { |
| 695 | struct ieee80211_local *local = sdata->local; |
| 696 | struct ieee80211_sub_if_data *vlan; |
| 697 | struct ieee80211_chanctx_conf *conf; |
| 698 | |
| 699 | if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP)) |
| 700 | return; |
| 701 | |
| 702 | lockdep_assert_held(&local->mtx); |
| 703 | |
| 704 | /* Check that conf exists, even when clearing this function |
| 705 | * must be called with the AP's channel context still there |
| 706 | * as it would otherwise cause VLANs to have an invalid |
| 707 | * channel context pointer for a while, possibly pointing |
| 708 | * to a channel context that has already been freed. |
| 709 | */ |
| 710 | conf = rcu_dereference_protected(sdata->vif.chanctx_conf, |
| 711 | lockdep_is_held(&local->chanctx_mtx)); |
| 712 | WARN_ON(!conf); |
| 713 | |
| 714 | if (clear) |
| 715 | conf = NULL; |
| 716 | |
| 717 | list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) |
| 718 | rcu_assign_pointer(vlan->vif.chanctx_conf, conf); |
| 719 | } |
| 720 | |
| 721 | void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata, |
| 722 | bool clear) |
| 723 | { |
| 724 | struct ieee80211_local *local = sdata->local; |
| 725 | |
| 726 | mutex_lock(&local->chanctx_mtx); |
| 727 | |
| 728 | __ieee80211_vif_copy_chanctx_to_vlans(sdata, clear); |
| 729 | |
| 730 | mutex_unlock(&local->chanctx_mtx); |
| 731 | } |
| 732 | |
| 733 | int ieee80211_vif_unreserve_chanctx(struct ieee80211_sub_if_data *sdata) |
| 734 | { |
Michal Kazior | e3afb92 | 2014-04-09 15:29:27 +0200 | [diff] [blame^] | 735 | struct ieee80211_chanctx *ctx = sdata->reserved_chanctx; |
| 736 | |
Luciano Coelho | 11335a5 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 737 | lockdep_assert_held(&sdata->local->chanctx_mtx); |
| 738 | |
Michal Kazior | e3afb92 | 2014-04-09 15:29:27 +0200 | [diff] [blame^] | 739 | if (WARN_ON(!ctx)) |
Luciano Coelho | 11335a5 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 740 | return -EINVAL; |
| 741 | |
Michal Kazior | e3afb92 | 2014-04-09 15:29:27 +0200 | [diff] [blame^] | 742 | list_del(&sdata->reserved_chanctx_list); |
Luciano Coelho | 11335a5 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 743 | sdata->reserved_chanctx = NULL; |
| 744 | |
Michal Kazior | e3afb92 | 2014-04-09 15:29:27 +0200 | [diff] [blame^] | 745 | if (--ctx->refcount == 0) |
| 746 | ieee80211_free_chanctx(sdata->local, ctx); |
| 747 | |
Luciano Coelho | 11335a5 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 748 | return 0; |
| 749 | } |
| 750 | |
| 751 | int ieee80211_vif_reserve_chanctx(struct ieee80211_sub_if_data *sdata, |
| 752 | const struct cfg80211_chan_def *chandef, |
Michal Kazior | 0933248 | 2014-04-09 15:29:25 +0200 | [diff] [blame] | 753 | enum ieee80211_chanctx_mode mode, |
| 754 | bool radar_required) |
Luciano Coelho | 11335a5 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 755 | { |
| 756 | struct ieee80211_local *local = sdata->local; |
| 757 | struct ieee80211_chanctx_conf *conf; |
| 758 | struct ieee80211_chanctx *new_ctx, *curr_ctx; |
| 759 | int ret = 0; |
| 760 | |
| 761 | mutex_lock(&local->chanctx_mtx); |
| 762 | |
| 763 | conf = rcu_dereference_protected(sdata->vif.chanctx_conf, |
| 764 | lockdep_is_held(&local->chanctx_mtx)); |
| 765 | if (!conf) { |
| 766 | ret = -EINVAL; |
| 767 | goto out; |
| 768 | } |
| 769 | |
| 770 | curr_ctx = container_of(conf, struct ieee80211_chanctx, conf); |
| 771 | |
| 772 | /* try to find another context with the chandef we want */ |
| 773 | new_ctx = ieee80211_find_chanctx(local, chandef, mode); |
| 774 | if (!new_ctx) { |
Luciano Coelho | 5d52ee8 | 2014-02-27 14:33:47 +0200 | [diff] [blame] | 775 | if (curr_ctx->refcount == 1 && |
| 776 | (local->hw.flags & IEEE80211_HW_CHANGE_RUNNING_CHANCTX)) { |
| 777 | /* if we're the only users of the chanctx and |
| 778 | * the driver supports changing a running |
| 779 | * context, reserve our current context |
| 780 | */ |
| 781 | new_ctx = curr_ctx; |
Michal Kazior | c2b90ad | 2014-04-09 15:29:24 +0200 | [diff] [blame] | 782 | } else if (ieee80211_can_create_new_chanctx(local)) { |
Luciano Coelho | 5d52ee8 | 2014-02-27 14:33:47 +0200 | [diff] [blame] | 783 | /* create a new context and reserve it */ |
| 784 | new_ctx = ieee80211_new_chanctx(local, chandef, mode); |
| 785 | if (IS_ERR(new_ctx)) { |
| 786 | ret = PTR_ERR(new_ctx); |
| 787 | goto out; |
| 788 | } |
Michal Kazior | c2b90ad | 2014-04-09 15:29:24 +0200 | [diff] [blame] | 789 | } else { |
| 790 | ret = -EBUSY; |
| 791 | goto out; |
Luciano Coelho | 11335a5 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | |
Michal Kazior | e3afb92 | 2014-04-09 15:29:27 +0200 | [diff] [blame^] | 795 | list_add(&sdata->reserved_chanctx_list, &new_ctx->reserved_vifs); |
Luciano Coelho | 11335a5 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 796 | new_ctx->refcount++; |
| 797 | sdata->reserved_chanctx = new_ctx; |
| 798 | sdata->reserved_chandef = *chandef; |
Michal Kazior | 0933248 | 2014-04-09 15:29:25 +0200 | [diff] [blame] | 799 | sdata->reserved_radar_required = radar_required; |
Luciano Coelho | 11335a5 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 800 | out: |
| 801 | mutex_unlock(&local->chanctx_mtx); |
| 802 | return ret; |
| 803 | } |
| 804 | |
| 805 | int ieee80211_vif_use_reserved_context(struct ieee80211_sub_if_data *sdata, |
| 806 | u32 *changed) |
| 807 | { |
| 808 | struct ieee80211_local *local = sdata->local; |
| 809 | struct ieee80211_chanctx *ctx; |
| 810 | struct ieee80211_chanctx *old_ctx; |
| 811 | struct ieee80211_chanctx_conf *conf; |
| 812 | int ret; |
| 813 | u32 tmp_changed = *changed; |
| 814 | |
| 815 | /* TODO: need to recheck if the chandef is usable etc.? */ |
| 816 | |
| 817 | lockdep_assert_held(&local->mtx); |
| 818 | |
| 819 | mutex_lock(&local->chanctx_mtx); |
| 820 | |
| 821 | ctx = sdata->reserved_chanctx; |
| 822 | if (WARN_ON(!ctx)) { |
| 823 | ret = -EINVAL; |
| 824 | goto out; |
| 825 | } |
| 826 | |
| 827 | conf = rcu_dereference_protected(sdata->vif.chanctx_conf, |
| 828 | lockdep_is_held(&local->chanctx_mtx)); |
| 829 | if (!conf) { |
| 830 | ret = -EINVAL; |
| 831 | goto out; |
| 832 | } |
| 833 | |
| 834 | old_ctx = container_of(conf, struct ieee80211_chanctx, conf); |
| 835 | |
| 836 | if (sdata->vif.bss_conf.chandef.width != sdata->reserved_chandef.width) |
| 837 | tmp_changed |= BSS_CHANGED_BANDWIDTH; |
| 838 | |
| 839 | sdata->vif.bss_conf.chandef = sdata->reserved_chandef; |
| 840 | |
Luciano Coelho | 5d52ee8 | 2014-02-27 14:33:47 +0200 | [diff] [blame] | 841 | /* unref our reservation */ |
Luciano Coelho | 11335a5 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 842 | ctx->refcount--; |
| 843 | sdata->reserved_chanctx = NULL; |
Michal Kazior | 0933248 | 2014-04-09 15:29:25 +0200 | [diff] [blame] | 844 | sdata->radar_required = sdata->reserved_radar_required; |
Michal Kazior | e3afb92 | 2014-04-09 15:29:27 +0200 | [diff] [blame^] | 845 | list_del(&sdata->reserved_chanctx_list); |
Luciano Coelho | 11335a5 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 846 | |
Luciano Coelho | 5d52ee8 | 2014-02-27 14:33:47 +0200 | [diff] [blame] | 847 | if (old_ctx == ctx) { |
| 848 | /* This is our own context, just change it */ |
| 849 | ret = __ieee80211_vif_change_channel(sdata, old_ctx, |
| 850 | &tmp_changed); |
| 851 | if (ret) |
| 852 | goto out; |
| 853 | } else { |
| 854 | ret = ieee80211_assign_vif_chanctx(sdata, ctx); |
| 855 | if (old_ctx->refcount == 0) |
| 856 | ieee80211_free_chanctx(local, old_ctx); |
| 857 | if (ret) { |
| 858 | /* if assign fails refcount stays the same */ |
| 859 | if (ctx->refcount == 0) |
| 860 | ieee80211_free_chanctx(local, ctx); |
| 861 | goto out; |
| 862 | } |
Luciano Coelho | 11335a5 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 863 | |
Luciano Coelho | 5d52ee8 | 2014-02-27 14:33:47 +0200 | [diff] [blame] | 864 | if (sdata->vif.type == NL80211_IFTYPE_AP) |
| 865 | __ieee80211_vif_copy_chanctx_to_vlans(sdata, false); |
| 866 | } |
Luciano Coelho | 11335a5 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 867 | |
| 868 | *changed = tmp_changed; |
| 869 | |
| 870 | ieee80211_recalc_chanctx_chantype(local, ctx); |
| 871 | ieee80211_recalc_smps_chanctx(local, ctx); |
| 872 | ieee80211_recalc_radar_chanctx(local, ctx); |
| 873 | ieee80211_recalc_chanctx_min_def(local, ctx); |
| 874 | out: |
| 875 | mutex_unlock(&local->chanctx_mtx); |
| 876 | return ret; |
| 877 | } |
| 878 | |
Johannes Berg | 2c9b735 | 2013-02-07 21:37:29 +0100 | [diff] [blame] | 879 | int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata, |
| 880 | const struct cfg80211_chan_def *chandef, |
| 881 | u32 *changed) |
| 882 | { |
| 883 | struct ieee80211_local *local = sdata->local; |
| 884 | struct ieee80211_chanctx_conf *conf; |
| 885 | struct ieee80211_chanctx *ctx; |
| 886 | int ret; |
| 887 | |
| 888 | if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef, |
| 889 | IEEE80211_CHAN_DISABLED)) |
| 890 | return -EINVAL; |
| 891 | |
| 892 | mutex_lock(&local->chanctx_mtx); |
| 893 | if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) { |
| 894 | ret = 0; |
| 895 | goto out; |
| 896 | } |
| 897 | |
| 898 | if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT || |
| 899 | sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) { |
| 900 | ret = -EINVAL; |
| 901 | goto out; |
| 902 | } |
| 903 | |
| 904 | conf = rcu_dereference_protected(sdata->vif.chanctx_conf, |
| 905 | lockdep_is_held(&local->chanctx_mtx)); |
| 906 | if (!conf) { |
| 907 | ret = -EINVAL; |
| 908 | goto out; |
| 909 | } |
| 910 | |
| 911 | ctx = container_of(conf, struct ieee80211_chanctx, conf); |
| 912 | if (!cfg80211_chandef_compatible(&conf->def, chandef)) { |
| 913 | ret = -EINVAL; |
| 914 | goto out; |
| 915 | } |
| 916 | |
| 917 | sdata->vif.bss_conf.chandef = *chandef; |
| 918 | |
| 919 | ieee80211_recalc_chanctx_chantype(local, ctx); |
| 920 | |
| 921 | *changed |= BSS_CHANGED_BANDWIDTH; |
| 922 | ret = 0; |
| 923 | out: |
| 924 | mutex_unlock(&local->chanctx_mtx); |
| 925 | return ret; |
| 926 | } |
| 927 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 928 | void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata) |
| 929 | { |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 930 | WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev)); |
| 931 | |
Johannes Berg | 34a3740 | 2013-12-18 09:43:33 +0100 | [diff] [blame] | 932 | lockdep_assert_held(&sdata->local->mtx); |
| 933 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 934 | mutex_lock(&sdata->local->chanctx_mtx); |
| 935 | __ieee80211_vif_release_channel(sdata); |
| 936 | mutex_unlock(&sdata->local->chanctx_mtx); |
| 937 | } |
Johannes Berg | 3448c00 | 2012-09-11 17:57:42 +0200 | [diff] [blame] | 938 | |
Johannes Berg | 4d76d21 | 2012-12-11 20:38:41 +0100 | [diff] [blame] | 939 | void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata) |
| 940 | { |
| 941 | struct ieee80211_local *local = sdata->local; |
| 942 | struct ieee80211_sub_if_data *ap; |
| 943 | struct ieee80211_chanctx_conf *conf; |
| 944 | |
| 945 | if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss)) |
| 946 | return; |
| 947 | |
| 948 | ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap); |
| 949 | |
| 950 | mutex_lock(&local->chanctx_mtx); |
| 951 | |
| 952 | conf = rcu_dereference_protected(ap->vif.chanctx_conf, |
| 953 | lockdep_is_held(&local->chanctx_mtx)); |
| 954 | rcu_assign_pointer(sdata->vif.chanctx_conf, conf); |
| 955 | mutex_unlock(&local->chanctx_mtx); |
| 956 | } |
| 957 | |
Johannes Berg | 3448c00 | 2012-09-11 17:57:42 +0200 | [diff] [blame] | 958 | void ieee80211_iter_chan_contexts_atomic( |
| 959 | struct ieee80211_hw *hw, |
| 960 | void (*iter)(struct ieee80211_hw *hw, |
| 961 | struct ieee80211_chanctx_conf *chanctx_conf, |
| 962 | void *data), |
| 963 | void *iter_data) |
| 964 | { |
| 965 | struct ieee80211_local *local = hw_to_local(hw); |
| 966 | struct ieee80211_chanctx *ctx; |
| 967 | |
| 968 | rcu_read_lock(); |
| 969 | list_for_each_entry_rcu(ctx, &local->chanctx_list, list) |
Johannes Berg | 8a61af6 | 2012-12-13 17:42:30 +0100 | [diff] [blame] | 970 | if (ctx->driver_present) |
| 971 | iter(hw, &ctx->conf, iter_data); |
Johannes Berg | 3448c00 | 2012-09-11 17:57:42 +0200 | [diff] [blame] | 972 | rcu_read_unlock(); |
| 973 | } |
| 974 | EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic); |