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