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 | c0166da | 2014-04-09 15:29:33 +0200 | [diff] [blame] | 12 | static int ieee80211_chanctx_num_assigned(struct ieee80211_local *local, |
| 13 | struct ieee80211_chanctx *ctx) |
| 14 | { |
| 15 | struct ieee80211_sub_if_data *sdata; |
| 16 | int num = 0; |
| 17 | |
| 18 | lockdep_assert_held(&local->chanctx_mtx); |
| 19 | |
| 20 | list_for_each_entry(sdata, &ctx->assigned_vifs, assigned_chanctx_list) |
| 21 | num++; |
| 22 | |
| 23 | return num; |
| 24 | } |
| 25 | |
| 26 | static int ieee80211_chanctx_num_reserved(struct ieee80211_local *local, |
| 27 | struct ieee80211_chanctx *ctx) |
| 28 | { |
| 29 | struct ieee80211_sub_if_data *sdata; |
| 30 | int num = 0; |
| 31 | |
| 32 | lockdep_assert_held(&local->chanctx_mtx); |
| 33 | |
| 34 | list_for_each_entry(sdata, &ctx->reserved_vifs, reserved_chanctx_list) |
| 35 | num++; |
| 36 | |
| 37 | return num; |
| 38 | } |
| 39 | |
| 40 | int ieee80211_chanctx_refcount(struct ieee80211_local *local, |
| 41 | struct ieee80211_chanctx *ctx) |
| 42 | { |
| 43 | return ieee80211_chanctx_num_assigned(local, ctx) + |
| 44 | ieee80211_chanctx_num_reserved(local, ctx); |
| 45 | } |
| 46 | |
Michal Kazior | c2b90ad | 2014-04-09 15:29:24 +0200 | [diff] [blame] | 47 | static int ieee80211_num_chanctx(struct ieee80211_local *local) |
| 48 | { |
| 49 | struct ieee80211_chanctx *ctx; |
| 50 | int num = 0; |
| 51 | |
| 52 | lockdep_assert_held(&local->chanctx_mtx); |
| 53 | |
| 54 | list_for_each_entry(ctx, &local->chanctx_list, list) |
| 55 | num++; |
| 56 | |
| 57 | return num; |
| 58 | } |
| 59 | |
| 60 | static bool ieee80211_can_create_new_chanctx(struct ieee80211_local *local) |
| 61 | { |
| 62 | lockdep_assert_held(&local->chanctx_mtx); |
| 63 | return ieee80211_num_chanctx(local) < ieee80211_max_num_channels(local); |
| 64 | } |
| 65 | |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 66 | static struct ieee80211_chanctx * |
| 67 | ieee80211_vif_get_chanctx(struct ieee80211_sub_if_data *sdata) |
| 68 | { |
| 69 | struct ieee80211_local *local = sdata->local; |
| 70 | struct ieee80211_chanctx_conf *conf; |
| 71 | |
| 72 | conf = rcu_dereference_protected(sdata->vif.chanctx_conf, |
| 73 | lockdep_is_held(&local->chanctx_mtx)); |
| 74 | if (!conf) |
| 75 | return NULL; |
| 76 | |
| 77 | return container_of(conf, struct ieee80211_chanctx, conf); |
| 78 | } |
| 79 | |
Michal Kazior | 0288157 | 2014-04-09 15:29:28 +0200 | [diff] [blame] | 80 | static const struct cfg80211_chan_def * |
| 81 | ieee80211_chanctx_reserved_chandef(struct ieee80211_local *local, |
| 82 | struct ieee80211_chanctx *ctx, |
| 83 | const struct cfg80211_chan_def *compat) |
| 84 | { |
| 85 | struct ieee80211_sub_if_data *sdata; |
| 86 | |
| 87 | lockdep_assert_held(&local->chanctx_mtx); |
| 88 | |
| 89 | list_for_each_entry(sdata, &ctx->reserved_vifs, |
| 90 | reserved_chanctx_list) { |
| 91 | if (!compat) |
| 92 | compat = &sdata->reserved_chandef; |
| 93 | |
| 94 | compat = cfg80211_chandef_compatible(&sdata->reserved_chandef, |
| 95 | compat); |
| 96 | if (!compat) |
| 97 | break; |
| 98 | } |
| 99 | |
| 100 | return compat; |
| 101 | } |
| 102 | |
Michal Kazior | 13f348a | 2014-04-09 15:29:29 +0200 | [diff] [blame] | 103 | static const struct cfg80211_chan_def * |
| 104 | ieee80211_chanctx_non_reserved_chandef(struct ieee80211_local *local, |
| 105 | struct ieee80211_chanctx *ctx, |
| 106 | const struct cfg80211_chan_def *compat) |
| 107 | { |
| 108 | struct ieee80211_sub_if_data *sdata; |
| 109 | |
| 110 | lockdep_assert_held(&local->chanctx_mtx); |
| 111 | |
| 112 | list_for_each_entry(sdata, &ctx->assigned_vifs, |
| 113 | assigned_chanctx_list) { |
| 114 | if (sdata->reserved_chanctx != NULL) |
| 115 | continue; |
| 116 | |
| 117 | if (!compat) |
| 118 | compat = &sdata->vif.bss_conf.chandef; |
| 119 | |
| 120 | compat = cfg80211_chandef_compatible( |
| 121 | &sdata->vif.bss_conf.chandef, compat); |
| 122 | if (!compat) |
| 123 | break; |
| 124 | } |
| 125 | |
| 126 | return compat; |
| 127 | } |
| 128 | |
| 129 | static const struct cfg80211_chan_def * |
| 130 | ieee80211_chanctx_combined_chandef(struct ieee80211_local *local, |
| 131 | struct ieee80211_chanctx *ctx, |
| 132 | const struct cfg80211_chan_def *compat) |
| 133 | { |
| 134 | lockdep_assert_held(&local->chanctx_mtx); |
| 135 | |
| 136 | compat = ieee80211_chanctx_reserved_chandef(local, ctx, compat); |
| 137 | if (!compat) |
| 138 | return NULL; |
| 139 | |
| 140 | compat = ieee80211_chanctx_non_reserved_chandef(local, ctx, compat); |
| 141 | if (!compat) |
| 142 | return NULL; |
| 143 | |
| 144 | return compat; |
| 145 | } |
| 146 | |
| 147 | static bool |
| 148 | ieee80211_chanctx_can_reserve_chandef(struct ieee80211_local *local, |
| 149 | struct ieee80211_chanctx *ctx, |
| 150 | const struct cfg80211_chan_def *def) |
| 151 | { |
| 152 | lockdep_assert_held(&local->chanctx_mtx); |
| 153 | |
| 154 | if (ieee80211_chanctx_combined_chandef(local, ctx, def)) |
| 155 | return true; |
| 156 | |
| 157 | if (!list_empty(&ctx->reserved_vifs) && |
| 158 | ieee80211_chanctx_reserved_chandef(local, ctx, def)) |
| 159 | return true; |
| 160 | |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | static struct ieee80211_chanctx * |
| 165 | ieee80211_find_reservation_chanctx(struct ieee80211_local *local, |
| 166 | const struct cfg80211_chan_def *chandef, |
| 167 | enum ieee80211_chanctx_mode mode) |
| 168 | { |
| 169 | struct ieee80211_chanctx *ctx; |
| 170 | |
| 171 | lockdep_assert_held(&local->chanctx_mtx); |
| 172 | |
| 173 | if (mode == IEEE80211_CHANCTX_EXCLUSIVE) |
| 174 | return NULL; |
| 175 | |
| 176 | list_for_each_entry(ctx, &local->chanctx_list, list) { |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 177 | if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED) |
| 178 | continue; |
| 179 | |
Michal Kazior | 13f348a | 2014-04-09 15:29:29 +0200 | [diff] [blame] | 180 | if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE) |
| 181 | continue; |
| 182 | |
| 183 | if (!ieee80211_chanctx_can_reserve_chandef(local, ctx, |
| 184 | chandef)) |
| 185 | continue; |
| 186 | |
| 187 | return ctx; |
| 188 | } |
| 189 | |
| 190 | return NULL; |
| 191 | } |
| 192 | |
Eliad Peller | 21f659b | 2013-11-11 20:14:01 +0200 | [diff] [blame] | 193 | static enum nl80211_chan_width ieee80211_get_sta_bw(struct ieee80211_sta *sta) |
| 194 | { |
| 195 | switch (sta->bandwidth) { |
| 196 | case IEEE80211_STA_RX_BW_20: |
| 197 | if (sta->ht_cap.ht_supported) |
| 198 | return NL80211_CHAN_WIDTH_20; |
| 199 | else |
| 200 | return NL80211_CHAN_WIDTH_20_NOHT; |
| 201 | case IEEE80211_STA_RX_BW_40: |
| 202 | return NL80211_CHAN_WIDTH_40; |
| 203 | case IEEE80211_STA_RX_BW_80: |
| 204 | return NL80211_CHAN_WIDTH_80; |
| 205 | case IEEE80211_STA_RX_BW_160: |
| 206 | /* |
| 207 | * This applied for both 160 and 80+80. since we use |
| 208 | * the returned value to consider degradation of |
| 209 | * ctx->conf.min_def, we have to make sure to take |
| 210 | * the bigger one (NL80211_CHAN_WIDTH_160). |
| 211 | * Otherwise we might try degrading even when not |
| 212 | * needed, as the max required sta_bw returned (80+80) |
| 213 | * might be smaller than the configured bw (160). |
| 214 | */ |
| 215 | return NL80211_CHAN_WIDTH_160; |
| 216 | default: |
| 217 | WARN_ON(1); |
| 218 | return NL80211_CHAN_WIDTH_20; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | static enum nl80211_chan_width |
| 223 | ieee80211_get_max_required_bw(struct ieee80211_sub_if_data *sdata) |
| 224 | { |
| 225 | enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT; |
| 226 | struct sta_info *sta; |
| 227 | |
| 228 | rcu_read_lock(); |
| 229 | list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) { |
| 230 | if (sdata != sta->sdata && |
| 231 | !(sta->sdata->bss && sta->sdata->bss == sdata->bss)) |
| 232 | continue; |
| 233 | |
| 234 | if (!sta->uploaded) |
| 235 | continue; |
| 236 | |
| 237 | max_bw = max(max_bw, ieee80211_get_sta_bw(&sta->sta)); |
| 238 | } |
| 239 | rcu_read_unlock(); |
| 240 | |
| 241 | return max_bw; |
| 242 | } |
| 243 | |
| 244 | static enum nl80211_chan_width |
| 245 | ieee80211_get_chanctx_max_required_bw(struct ieee80211_local *local, |
| 246 | struct ieee80211_chanctx_conf *conf) |
| 247 | { |
| 248 | struct ieee80211_sub_if_data *sdata; |
| 249 | enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT; |
| 250 | |
| 251 | rcu_read_lock(); |
| 252 | list_for_each_entry_rcu(sdata, &local->interfaces, list) { |
| 253 | struct ieee80211_vif *vif = &sdata->vif; |
| 254 | enum nl80211_chan_width width = NL80211_CHAN_WIDTH_20_NOHT; |
| 255 | |
| 256 | if (!ieee80211_sdata_running(sdata)) |
| 257 | continue; |
| 258 | |
| 259 | if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf) |
| 260 | continue; |
| 261 | |
| 262 | switch (vif->type) { |
| 263 | case NL80211_IFTYPE_AP: |
| 264 | case NL80211_IFTYPE_AP_VLAN: |
| 265 | width = ieee80211_get_max_required_bw(sdata); |
| 266 | break; |
| 267 | case NL80211_IFTYPE_P2P_DEVICE: |
| 268 | continue; |
| 269 | case NL80211_IFTYPE_STATION: |
| 270 | case NL80211_IFTYPE_ADHOC: |
| 271 | case NL80211_IFTYPE_WDS: |
| 272 | case NL80211_IFTYPE_MESH_POINT: |
| 273 | width = vif->bss_conf.chandef.width; |
| 274 | break; |
| 275 | case NL80211_IFTYPE_UNSPECIFIED: |
| 276 | case NUM_NL80211_IFTYPES: |
| 277 | case NL80211_IFTYPE_MONITOR: |
| 278 | case NL80211_IFTYPE_P2P_CLIENT: |
| 279 | case NL80211_IFTYPE_P2P_GO: |
| 280 | WARN_ON_ONCE(1); |
| 281 | } |
| 282 | max_bw = max(max_bw, width); |
| 283 | } |
Eliad Peller | 1c37a72 | 2014-03-03 13:37:14 +0200 | [diff] [blame] | 284 | |
| 285 | /* use the configured bandwidth in case of monitor interface */ |
| 286 | sdata = rcu_dereference(local->monitor_sdata); |
| 287 | if (sdata && rcu_access_pointer(sdata->vif.chanctx_conf) == conf) |
| 288 | max_bw = max(max_bw, conf->def.width); |
| 289 | |
Eliad Peller | 21f659b | 2013-11-11 20:14:01 +0200 | [diff] [blame] | 290 | rcu_read_unlock(); |
| 291 | |
| 292 | return max_bw; |
| 293 | } |
| 294 | |
| 295 | /* |
| 296 | * recalc the min required chan width of the channel context, which is |
| 297 | * the max of min required widths of all the interfaces bound to this |
| 298 | * channel context. |
| 299 | */ |
| 300 | void ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local, |
| 301 | struct ieee80211_chanctx *ctx) |
| 302 | { |
| 303 | enum nl80211_chan_width max_bw; |
| 304 | struct cfg80211_chan_def min_def; |
| 305 | |
| 306 | lockdep_assert_held(&local->chanctx_mtx); |
| 307 | |
| 308 | /* don't optimize 5MHz, 10MHz, and radar_enabled confs */ |
| 309 | if (ctx->conf.def.width == NL80211_CHAN_WIDTH_5 || |
| 310 | ctx->conf.def.width == NL80211_CHAN_WIDTH_10 || |
| 311 | ctx->conf.radar_enabled) { |
| 312 | ctx->conf.min_def = ctx->conf.def; |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | max_bw = ieee80211_get_chanctx_max_required_bw(local, &ctx->conf); |
| 317 | |
| 318 | /* downgrade chandef up to max_bw */ |
| 319 | min_def = ctx->conf.def; |
| 320 | while (min_def.width > max_bw) |
| 321 | ieee80211_chandef_downgrade(&min_def); |
| 322 | |
| 323 | if (cfg80211_chandef_identical(&ctx->conf.min_def, &min_def)) |
| 324 | return; |
| 325 | |
| 326 | ctx->conf.min_def = min_def; |
| 327 | if (!ctx->driver_present) |
| 328 | return; |
| 329 | |
| 330 | drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_MIN_WIDTH); |
| 331 | } |
| 332 | |
Johannes Berg | 18942d3 | 2013-02-07 21:30:37 +0100 | [diff] [blame] | 333 | static void ieee80211_change_chanctx(struct ieee80211_local *local, |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 334 | struct ieee80211_chanctx *ctx, |
| 335 | const struct cfg80211_chan_def *chandef) |
Michal Kazior | 23a85b45 | 2012-06-26 14:37:21 +0200 | [diff] [blame] | 336 | { |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 337 | if (cfg80211_chandef_identical(&ctx->conf.def, chandef)) |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 338 | return; |
| 339 | |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 340 | WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef)); |
| 341 | |
| 342 | ctx->conf.def = *chandef; |
| 343 | drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_WIDTH); |
Eliad Peller | 21f659b | 2013-11-11 20:14:01 +0200 | [diff] [blame] | 344 | ieee80211_recalc_chanctx_min_def(local, ctx); |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 345 | |
| 346 | if (!local->use_chanctx) { |
Karl Beldan | 675a0b0 | 2013-03-25 16:26:57 +0100 | [diff] [blame] | 347 | local->_oper_chandef = *chandef; |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 348 | ieee80211_hw_config(local, 0); |
| 349 | } |
Johannes Berg | 0aaffa9 | 2010-05-05 15:28:27 +0200 | [diff] [blame] | 350 | } |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 351 | |
| 352 | static struct ieee80211_chanctx * |
| 353 | ieee80211_find_chanctx(struct ieee80211_local *local, |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 354 | const struct cfg80211_chan_def *chandef, |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 355 | enum ieee80211_chanctx_mode mode) |
| 356 | { |
| 357 | struct ieee80211_chanctx *ctx; |
| 358 | |
| 359 | lockdep_assert_held(&local->chanctx_mtx); |
| 360 | |
| 361 | if (mode == IEEE80211_CHANCTX_EXCLUSIVE) |
| 362 | return NULL; |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 363 | |
| 364 | list_for_each_entry(ctx, &local->chanctx_list, list) { |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 365 | const struct cfg80211_chan_def *compat; |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 366 | |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 367 | if (ctx->replace_state != IEEE80211_CHANCTX_REPLACE_NONE) |
| 368 | continue; |
| 369 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 370 | if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE) |
| 371 | continue; |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 372 | |
| 373 | compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef); |
| 374 | if (!compat) |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 375 | continue; |
| 376 | |
Michal Kazior | 0288157 | 2014-04-09 15:29:28 +0200 | [diff] [blame] | 377 | compat = ieee80211_chanctx_reserved_chandef(local, ctx, |
| 378 | compat); |
| 379 | if (!compat) |
| 380 | continue; |
| 381 | |
Johannes Berg | 18942d3 | 2013-02-07 21:30:37 +0100 | [diff] [blame] | 382 | ieee80211_change_chanctx(local, ctx, compat); |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 383 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 384 | return ctx; |
| 385 | } |
| 386 | |
| 387 | return NULL; |
| 388 | } |
| 389 | |
Simon Wunderlich | e474685 | 2013-04-08 22:43:16 +0200 | [diff] [blame] | 390 | static bool ieee80211_is_radar_required(struct ieee80211_local *local) |
| 391 | { |
| 392 | struct ieee80211_sub_if_data *sdata; |
| 393 | |
Michal Kazior | cc901de | 2014-01-29 07:56:20 +0100 | [diff] [blame] | 394 | lockdep_assert_held(&local->mtx); |
| 395 | |
Simon Wunderlich | e474685 | 2013-04-08 22:43:16 +0200 | [diff] [blame] | 396 | rcu_read_lock(); |
| 397 | list_for_each_entry_rcu(sdata, &local->interfaces, list) { |
| 398 | if (sdata->radar_required) { |
| 399 | rcu_read_unlock(); |
| 400 | return true; |
| 401 | } |
| 402 | } |
| 403 | rcu_read_unlock(); |
| 404 | |
| 405 | return false; |
| 406 | } |
| 407 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 408 | static struct ieee80211_chanctx * |
Michal Kazior | ed68ebc | 2014-04-09 15:29:30 +0200 | [diff] [blame] | 409 | ieee80211_alloc_chanctx(struct ieee80211_local *local, |
| 410 | const struct cfg80211_chan_def *chandef, |
| 411 | enum ieee80211_chanctx_mode mode) |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 412 | { |
| 413 | struct ieee80211_chanctx *ctx; |
| 414 | |
| 415 | lockdep_assert_held(&local->chanctx_mtx); |
| 416 | |
| 417 | ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL); |
| 418 | if (!ctx) |
Michal Kazior | ed68ebc | 2014-04-09 15:29:30 +0200 | [diff] [blame] | 419 | return NULL; |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 420 | |
Michal Kazior | 484298a | 2014-04-09 15:29:26 +0200 | [diff] [blame] | 421 | INIT_LIST_HEAD(&ctx->assigned_vifs); |
Michal Kazior | e3afb92 | 2014-04-09 15:29:27 +0200 | [diff] [blame] | 422 | INIT_LIST_HEAD(&ctx->reserved_vifs); |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 423 | ctx->conf.def = *chandef; |
Johannes Berg | 04ecd25 | 2012-09-11 14:34:12 +0200 | [diff] [blame] | 424 | ctx->conf.rx_chains_static = 1; |
| 425 | ctx->conf.rx_chains_dynamic = 1; |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 426 | ctx->mode = mode; |
Simon Wunderlich | e474685 | 2013-04-08 22:43:16 +0200 | [diff] [blame] | 427 | ctx->conf.radar_enabled = ieee80211_is_radar_required(local); |
Eliad Peller | 21f659b | 2013-11-11 20:14:01 +0200 | [diff] [blame] | 428 | ieee80211_recalc_chanctx_min_def(local, ctx); |
Michal Kazior | ed68ebc | 2014-04-09 15:29:30 +0200 | [diff] [blame] | 429 | |
| 430 | return ctx; |
| 431 | } |
| 432 | |
| 433 | static int ieee80211_add_chanctx(struct ieee80211_local *local, |
| 434 | struct ieee80211_chanctx *ctx) |
| 435 | { |
| 436 | u32 changed; |
| 437 | int err; |
| 438 | |
| 439 | lockdep_assert_held(&local->mtx); |
| 440 | lockdep_assert_held(&local->chanctx_mtx); |
| 441 | |
Simon Wunderlich | e474685 | 2013-04-08 22:43:16 +0200 | [diff] [blame] | 442 | if (!local->use_chanctx) |
| 443 | local->hw.conf.radar_enabled = ctx->conf.radar_enabled; |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 444 | |
Johannes Berg | 382a103 | 2013-03-22 22:30:09 +0100 | [diff] [blame] | 445 | /* turn idle off *before* setting channel -- some drivers need that */ |
| 446 | changed = ieee80211_idle_off(local); |
| 447 | if (changed) |
| 448 | ieee80211_hw_config(local, changed); |
| 449 | |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 450 | if (!local->use_chanctx) { |
Michal Kazior | ed68ebc | 2014-04-09 15:29:30 +0200 | [diff] [blame] | 451 | local->_oper_chandef = ctx->conf.def; |
Michal Kazior | 9b4816f | 2014-04-04 13:02:43 +0200 | [diff] [blame] | 452 | ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL); |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 453 | } else { |
| 454 | err = drv_add_chanctx(local, ctx); |
| 455 | if (err) { |
Johannes Berg | 382a103 | 2013-03-22 22:30:09 +0100 | [diff] [blame] | 456 | ieee80211_recalc_idle(local); |
Michal Kazior | ed68ebc | 2014-04-09 15:29:30 +0200 | [diff] [blame] | 457 | return err; |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 458 | } |
Michal Kazior | 35f2fce | 2012-06-26 14:37:20 +0200 | [diff] [blame] | 459 | } |
| 460 | |
Michal Kazior | ed68ebc | 2014-04-09 15:29:30 +0200 | [diff] [blame] | 461 | return 0; |
| 462 | } |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 463 | |
Michal Kazior | ed68ebc | 2014-04-09 15:29:30 +0200 | [diff] [blame] | 464 | static struct ieee80211_chanctx * |
| 465 | ieee80211_new_chanctx(struct ieee80211_local *local, |
| 466 | const struct cfg80211_chan_def *chandef, |
| 467 | enum ieee80211_chanctx_mode mode) |
| 468 | { |
| 469 | struct ieee80211_chanctx *ctx; |
| 470 | int err; |
| 471 | |
| 472 | lockdep_assert_held(&local->mtx); |
| 473 | lockdep_assert_held(&local->chanctx_mtx); |
| 474 | |
| 475 | ctx = ieee80211_alloc_chanctx(local, chandef, mode); |
| 476 | if (!ctx) |
| 477 | return ERR_PTR(-ENOMEM); |
| 478 | |
| 479 | err = ieee80211_add_chanctx(local, ctx); |
| 480 | if (err) { |
| 481 | kfree(ctx); |
| 482 | return ERR_PTR(err); |
| 483 | } |
| 484 | |
| 485 | list_add_rcu(&ctx->list, &local->chanctx_list); |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 486 | return ctx; |
| 487 | } |
| 488 | |
Michal Kazior | 1f0d54c | 2014-04-09 15:29:31 +0200 | [diff] [blame] | 489 | static void ieee80211_del_chanctx(struct ieee80211_local *local, |
| 490 | struct ieee80211_chanctx *ctx) |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 491 | { |
| 492 | lockdep_assert_held(&local->chanctx_mtx); |
| 493 | |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 494 | if (!local->use_chanctx) { |
Karl Beldan | 675a0b0 | 2013-03-25 16:26:57 +0100 | [diff] [blame] | 495 | struct cfg80211_chan_def *chandef = &local->_oper_chandef; |
| 496 | chandef->width = NL80211_CHAN_WIDTH_20_NOHT; |
| 497 | chandef->center_freq1 = chandef->chan->center_freq; |
| 498 | chandef->center_freq2 = 0; |
Simon Wunderlich | e474685 | 2013-04-08 22:43:16 +0200 | [diff] [blame] | 499 | |
| 500 | /* NOTE: Disabling radar is only valid here for |
| 501 | * single channel context. To be sure, check it ... |
| 502 | */ |
Michal Kazior | 1f0d54c | 2014-04-09 15:29:31 +0200 | [diff] [blame] | 503 | WARN_ON(local->hw.conf.radar_enabled && |
| 504 | !list_empty(&local->chanctx_list)); |
| 505 | |
Simon Wunderlich | e474685 | 2013-04-08 22:43:16 +0200 | [diff] [blame] | 506 | local->hw.conf.radar_enabled = false; |
| 507 | |
Michal Kazior | 9b4816f | 2014-04-04 13:02:43 +0200 | [diff] [blame] | 508 | ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL); |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 509 | } else { |
| 510 | drv_remove_chanctx(local, ctx); |
| 511 | } |
Michal Kazior | 35f2fce | 2012-06-26 14:37:20 +0200 | [diff] [blame] | 512 | |
Johannes Berg | fd0f979 | 2013-02-07 00:14:51 +0100 | [diff] [blame] | 513 | ieee80211_recalc_idle(local); |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 514 | } |
| 515 | |
Michal Kazior | 1f0d54c | 2014-04-09 15:29:31 +0200 | [diff] [blame] | 516 | static void ieee80211_free_chanctx(struct ieee80211_local *local, |
| 517 | struct ieee80211_chanctx *ctx) |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 518 | { |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 519 | lockdep_assert_held(&local->chanctx_mtx); |
| 520 | |
Michal Kazior | c0166da | 2014-04-09 15:29:33 +0200 | [diff] [blame] | 521 | WARN_ON_ONCE(ieee80211_chanctx_refcount(local, ctx) != 0); |
Michal Kazior | 35f2fce | 2012-06-26 14:37:20 +0200 | [diff] [blame] | 522 | |
Michal Kazior | 1f0d54c | 2014-04-09 15:29:31 +0200 | [diff] [blame] | 523 | list_del_rcu(&ctx->list); |
| 524 | ieee80211_del_chanctx(local, ctx); |
| 525 | kfree_rcu(ctx, rcu_head); |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 526 | } |
| 527 | |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 528 | static void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local, |
| 529 | struct ieee80211_chanctx *ctx) |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 530 | { |
| 531 | struct ieee80211_chanctx_conf *conf = &ctx->conf; |
| 532 | struct ieee80211_sub_if_data *sdata; |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 533 | const struct cfg80211_chan_def *compat = NULL; |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 534 | |
| 535 | lockdep_assert_held(&local->chanctx_mtx); |
| 536 | |
| 537 | rcu_read_lock(); |
| 538 | list_for_each_entry_rcu(sdata, &local->interfaces, list) { |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 539 | |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 540 | if (!ieee80211_sdata_running(sdata)) |
| 541 | continue; |
| 542 | if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf) |
| 543 | continue; |
| 544 | |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 545 | if (!compat) |
| 546 | compat = &sdata->vif.bss_conf.chandef; |
| 547 | |
| 548 | compat = cfg80211_chandef_compatible( |
| 549 | &sdata->vif.bss_conf.chandef, compat); |
| 550 | if (!compat) |
| 551 | break; |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 552 | } |
| 553 | rcu_read_unlock(); |
| 554 | |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 555 | if (WARN_ON_ONCE(!compat)) |
| 556 | return; |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 557 | |
Johannes Berg | 18942d3 | 2013-02-07 21:30:37 +0100 | [diff] [blame] | 558 | ieee80211_change_chanctx(local, ctx, compat); |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 559 | } |
| 560 | |
Johannes Berg | 367bbd1 | 2013-12-18 09:36:09 +0100 | [diff] [blame] | 561 | static void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local, |
| 562 | struct ieee80211_chanctx *chanctx) |
| 563 | { |
| 564 | bool radar_enabled; |
| 565 | |
| 566 | lockdep_assert_held(&local->chanctx_mtx); |
Johannes Berg | 34a3740 | 2013-12-18 09:43:33 +0100 | [diff] [blame] | 567 | /* for setting local->radar_detect_enabled */ |
| 568 | lockdep_assert_held(&local->mtx); |
Johannes Berg | 367bbd1 | 2013-12-18 09:36:09 +0100 | [diff] [blame] | 569 | |
| 570 | radar_enabled = ieee80211_is_radar_required(local); |
| 571 | |
| 572 | if (radar_enabled == chanctx->conf.radar_enabled) |
| 573 | return; |
| 574 | |
| 575 | chanctx->conf.radar_enabled = radar_enabled; |
| 576 | local->radar_detect_enabled = chanctx->conf.radar_enabled; |
| 577 | |
| 578 | if (!local->use_chanctx) { |
| 579 | local->hw.conf.radar_enabled = chanctx->conf.radar_enabled; |
| 580 | ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL); |
| 581 | } |
| 582 | |
| 583 | drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR); |
| 584 | } |
| 585 | |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 586 | static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata, |
| 587 | struct ieee80211_chanctx *new_ctx) |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 588 | { |
Michal Kazior | 35f2fce | 2012-06-26 14:37:20 +0200 | [diff] [blame] | 589 | struct ieee80211_local *local = sdata->local; |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 590 | struct ieee80211_chanctx_conf *conf; |
| 591 | struct ieee80211_chanctx *curr_ctx = NULL; |
| 592 | int ret = 0; |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 593 | |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 594 | conf = rcu_dereference_protected(sdata->vif.chanctx_conf, |
| 595 | lockdep_is_held(&local->chanctx_mtx)); |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 596 | |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 597 | if (conf) { |
| 598 | curr_ctx = container_of(conf, struct ieee80211_chanctx, conf); |
Michal Kazior | 35f2fce | 2012-06-26 14:37:20 +0200 | [diff] [blame] | 599 | |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 600 | drv_unassign_vif_chanctx(local, sdata, curr_ctx); |
| 601 | conf = NULL; |
Michal Kazior | 484298a | 2014-04-09 15:29:26 +0200 | [diff] [blame] | 602 | list_del(&sdata->assigned_chanctx_list); |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | if (new_ctx) { |
| 606 | ret = drv_assign_vif_chanctx(local, sdata, new_ctx); |
| 607 | if (ret) |
| 608 | goto out; |
| 609 | |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 610 | conf = &new_ctx->conf; |
Michal Kazior | 484298a | 2014-04-09 15:29:26 +0200 | [diff] [blame] | 611 | list_add(&sdata->assigned_chanctx_list, |
| 612 | &new_ctx->assigned_vifs); |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | out: |
| 616 | rcu_assign_pointer(sdata->vif.chanctx_conf, conf); |
| 617 | |
| 618 | sdata->vif.bss_conf.idle = !conf; |
| 619 | |
Michal Kazior | c0166da | 2014-04-09 15:29:33 +0200 | [diff] [blame] | 620 | if (curr_ctx && ieee80211_chanctx_num_assigned(local, curr_ctx) > 0) { |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 621 | ieee80211_recalc_chanctx_chantype(local, curr_ctx); |
| 622 | ieee80211_recalc_smps_chanctx(local, curr_ctx); |
| 623 | ieee80211_recalc_radar_chanctx(local, curr_ctx); |
| 624 | ieee80211_recalc_chanctx_min_def(local, curr_ctx); |
| 625 | } |
| 626 | |
Michal Kazior | c0166da | 2014-04-09 15:29:33 +0200 | [diff] [blame] | 627 | if (new_ctx && ieee80211_chanctx_num_assigned(local, new_ctx) > 0) { |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 628 | ieee80211_recalc_txpower(sdata); |
| 629 | ieee80211_recalc_chanctx_min_def(local, new_ctx); |
| 630 | } |
Johannes Berg | 5bbe754d | 2013-02-13 13:50:51 +0100 | [diff] [blame] | 631 | |
| 632 | if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE && |
| 633 | sdata->vif.type != NL80211_IFTYPE_MONITOR) |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 634 | ieee80211_bss_info_change_notify(sdata, |
| 635 | BSS_CHANGED_IDLE); |
Johannes Berg | fd0f979 | 2013-02-07 00:14:51 +0100 | [diff] [blame] | 636 | |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 637 | return ret; |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata) |
| 641 | { |
| 642 | struct ieee80211_local *local = sdata->local; |
| 643 | struct ieee80211_chanctx_conf *conf; |
| 644 | struct ieee80211_chanctx *ctx; |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 645 | bool use_reserved_switch = false; |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 646 | |
| 647 | lockdep_assert_held(&local->chanctx_mtx); |
| 648 | |
| 649 | conf = rcu_dereference_protected(sdata->vif.chanctx_conf, |
| 650 | lockdep_is_held(&local->chanctx_mtx)); |
| 651 | if (!conf) |
| 652 | return; |
| 653 | |
| 654 | ctx = container_of(conf, struct ieee80211_chanctx, conf); |
| 655 | |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 656 | if (sdata->reserved_chanctx) { |
| 657 | if (sdata->reserved_chanctx->replace_state == |
| 658 | IEEE80211_CHANCTX_REPLACES_OTHER && |
| 659 | ieee80211_chanctx_num_reserved(local, |
| 660 | sdata->reserved_chanctx) > 1) |
| 661 | use_reserved_switch = true; |
| 662 | |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 663 | ieee80211_vif_unreserve_chanctx(sdata); |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 664 | } |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 665 | |
Luciano Coelho | 77eeba9 | 2014-03-11 18:24:12 +0200 | [diff] [blame] | 666 | ieee80211_assign_vif_chanctx(sdata, NULL); |
Michal Kazior | c0166da | 2014-04-09 15:29:33 +0200 | [diff] [blame] | 667 | if (ieee80211_chanctx_refcount(local, ctx) == 0) |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 668 | ieee80211_free_chanctx(local, ctx); |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 669 | |
| 670 | /* Unreserving may ready an in-place reservation. */ |
| 671 | if (use_reserved_switch) |
| 672 | ieee80211_vif_use_reserved_switch(local); |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 673 | } |
| 674 | |
Johannes Berg | 04ecd25 | 2012-09-11 14:34:12 +0200 | [diff] [blame] | 675 | void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local, |
| 676 | struct ieee80211_chanctx *chanctx) |
| 677 | { |
| 678 | struct ieee80211_sub_if_data *sdata; |
| 679 | u8 rx_chains_static, rx_chains_dynamic; |
| 680 | |
| 681 | lockdep_assert_held(&local->chanctx_mtx); |
| 682 | |
| 683 | rx_chains_static = 1; |
| 684 | rx_chains_dynamic = 1; |
| 685 | |
| 686 | rcu_read_lock(); |
| 687 | list_for_each_entry_rcu(sdata, &local->interfaces, list) { |
| 688 | u8 needed_static, needed_dynamic; |
| 689 | |
| 690 | if (!ieee80211_sdata_running(sdata)) |
| 691 | continue; |
| 692 | |
| 693 | if (rcu_access_pointer(sdata->vif.chanctx_conf) != |
| 694 | &chanctx->conf) |
| 695 | continue; |
| 696 | |
| 697 | switch (sdata->vif.type) { |
| 698 | case NL80211_IFTYPE_P2P_DEVICE: |
| 699 | continue; |
| 700 | case NL80211_IFTYPE_STATION: |
| 701 | if (!sdata->u.mgd.associated) |
| 702 | continue; |
| 703 | break; |
| 704 | case NL80211_IFTYPE_AP_VLAN: |
| 705 | continue; |
| 706 | case NL80211_IFTYPE_AP: |
| 707 | case NL80211_IFTYPE_ADHOC: |
| 708 | case NL80211_IFTYPE_WDS: |
| 709 | case NL80211_IFTYPE_MESH_POINT: |
| 710 | break; |
| 711 | default: |
| 712 | WARN_ON_ONCE(1); |
| 713 | } |
| 714 | |
| 715 | switch (sdata->smps_mode) { |
| 716 | default: |
| 717 | WARN_ONCE(1, "Invalid SMPS mode %d\n", |
| 718 | sdata->smps_mode); |
| 719 | /* fall through */ |
| 720 | case IEEE80211_SMPS_OFF: |
| 721 | needed_static = sdata->needed_rx_chains; |
| 722 | needed_dynamic = sdata->needed_rx_chains; |
| 723 | break; |
| 724 | case IEEE80211_SMPS_DYNAMIC: |
| 725 | needed_static = 1; |
| 726 | needed_dynamic = sdata->needed_rx_chains; |
| 727 | break; |
| 728 | case IEEE80211_SMPS_STATIC: |
| 729 | needed_static = 1; |
| 730 | needed_dynamic = 1; |
| 731 | break; |
| 732 | } |
| 733 | |
| 734 | rx_chains_static = max(rx_chains_static, needed_static); |
| 735 | rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic); |
| 736 | } |
Ido Yariv | 7b8a9cd | 2014-03-24 09:55:45 +0200 | [diff] [blame] | 737 | |
| 738 | /* Disable SMPS for the monitor interface */ |
| 739 | sdata = rcu_dereference(local->monitor_sdata); |
| 740 | if (sdata && |
| 741 | rcu_access_pointer(sdata->vif.chanctx_conf) == &chanctx->conf) |
| 742 | rx_chains_dynamic = rx_chains_static = local->rx_chains; |
| 743 | |
Johannes Berg | 04ecd25 | 2012-09-11 14:34:12 +0200 | [diff] [blame] | 744 | rcu_read_unlock(); |
| 745 | |
| 746 | if (!local->use_chanctx) { |
| 747 | if (rx_chains_static > 1) |
| 748 | local->smps_mode = IEEE80211_SMPS_OFF; |
| 749 | else if (rx_chains_dynamic > 1) |
| 750 | local->smps_mode = IEEE80211_SMPS_DYNAMIC; |
| 751 | else |
| 752 | local->smps_mode = IEEE80211_SMPS_STATIC; |
| 753 | ieee80211_hw_config(local, 0); |
| 754 | } |
| 755 | |
| 756 | if (rx_chains_static == chanctx->conf.rx_chains_static && |
| 757 | rx_chains_dynamic == chanctx->conf.rx_chains_dynamic) |
| 758 | return; |
| 759 | |
| 760 | chanctx->conf.rx_chains_static = rx_chains_static; |
| 761 | chanctx->conf.rx_chains_dynamic = rx_chains_dynamic; |
| 762 | drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS); |
| 763 | } |
| 764 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 765 | int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata, |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 766 | const struct cfg80211_chan_def *chandef, |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 767 | enum ieee80211_chanctx_mode mode) |
| 768 | { |
| 769 | struct ieee80211_local *local = sdata->local; |
| 770 | struct ieee80211_chanctx *ctx; |
Luciano Coelho | 73de86a | 2014-02-13 11:31:59 +0200 | [diff] [blame] | 771 | u8 radar_detect_width = 0; |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 772 | int ret; |
| 773 | |
Johannes Berg | 34a3740 | 2013-12-18 09:43:33 +0100 | [diff] [blame] | 774 | lockdep_assert_held(&local->mtx); |
| 775 | |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 776 | WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev)); |
| 777 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 778 | mutex_lock(&local->chanctx_mtx); |
Luciano Coelho | 73de86a | 2014-02-13 11:31:59 +0200 | [diff] [blame] | 779 | |
| 780 | ret = cfg80211_chandef_dfs_required(local->hw.wiphy, |
| 781 | chandef, |
| 782 | sdata->wdev.iftype); |
| 783 | if (ret < 0) |
| 784 | goto out; |
| 785 | if (ret > 0) |
| 786 | radar_detect_width = BIT(chandef->width); |
| 787 | |
| 788 | sdata->radar_required = ret; |
| 789 | |
| 790 | ret = ieee80211_check_combinations(sdata, chandef, mode, |
| 791 | radar_detect_width); |
| 792 | if (ret < 0) |
| 793 | goto out; |
| 794 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 795 | __ieee80211_vif_release_channel(sdata); |
| 796 | |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 797 | ctx = ieee80211_find_chanctx(local, chandef, mode); |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 798 | if (!ctx) |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 799 | ctx = ieee80211_new_chanctx(local, chandef, mode); |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 800 | if (IS_ERR(ctx)) { |
| 801 | ret = PTR_ERR(ctx); |
| 802 | goto out; |
| 803 | } |
| 804 | |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 805 | sdata->vif.bss_conf.chandef = *chandef; |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 806 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 807 | ret = ieee80211_assign_vif_chanctx(sdata, ctx); |
| 808 | if (ret) { |
| 809 | /* if assign fails refcount stays the same */ |
Michal Kazior | c0166da | 2014-04-09 15:29:33 +0200 | [diff] [blame] | 810 | if (ieee80211_chanctx_refcount(local, ctx) == 0) |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 811 | ieee80211_free_chanctx(local, ctx); |
| 812 | goto out; |
| 813 | } |
| 814 | |
Johannes Berg | 04ecd25 | 2012-09-11 14:34:12 +0200 | [diff] [blame] | 815 | ieee80211_recalc_smps_chanctx(local, ctx); |
Simon Wunderlich | 164eb02 | 2013-02-08 18:16:20 +0100 | [diff] [blame] | 816 | ieee80211_recalc_radar_chanctx(local, ctx); |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 817 | out: |
| 818 | mutex_unlock(&local->chanctx_mtx); |
| 819 | return ret; |
| 820 | } |
| 821 | |
Luciano Coelho | 33ffd95 | 2014-01-30 22:08:16 +0200 | [diff] [blame] | 822 | static int __ieee80211_vif_change_channel(struct ieee80211_sub_if_data *sdata, |
| 823 | struct ieee80211_chanctx *ctx, |
| 824 | u32 *changed) |
Simon Wunderlich | 73da7d5 | 2013-07-11 16:09:06 +0200 | [diff] [blame] | 825 | { |
| 826 | struct ieee80211_local *local = sdata->local; |
Luciano Coelho | 33787fc | 2013-11-11 20:34:54 +0200 | [diff] [blame] | 827 | const struct cfg80211_chan_def *chandef = &sdata->csa_chandef; |
Simon Wunderlich | 73da7d5 | 2013-07-11 16:09:06 +0200 | [diff] [blame] | 828 | u32 chanctx_changed = 0; |
| 829 | |
Simon Wunderlich | 73da7d5 | 2013-07-11 16:09:06 +0200 | [diff] [blame] | 830 | if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef, |
| 831 | IEEE80211_CHAN_DISABLED)) |
| 832 | return -EINVAL; |
| 833 | |
Michal Kazior | c0166da | 2014-04-09 15:29:33 +0200 | [diff] [blame] | 834 | if (ieee80211_chanctx_refcount(local, ctx) != 1) |
Luciano Coelho | 33ffd95 | 2014-01-30 22:08:16 +0200 | [diff] [blame] | 835 | return -EINVAL; |
Simon Wunderlich | 73da7d5 | 2013-07-11 16:09:06 +0200 | [diff] [blame] | 836 | |
| 837 | if (sdata->vif.bss_conf.chandef.width != chandef->width) { |
| 838 | chanctx_changed = IEEE80211_CHANCTX_CHANGE_WIDTH; |
| 839 | *changed |= BSS_CHANGED_BANDWIDTH; |
| 840 | } |
| 841 | |
| 842 | sdata->vif.bss_conf.chandef = *chandef; |
| 843 | ctx->conf.def = *chandef; |
| 844 | |
| 845 | chanctx_changed |= IEEE80211_CHANCTX_CHANGE_CHANNEL; |
| 846 | drv_change_chanctx(local, ctx, chanctx_changed); |
| 847 | |
Simon Wunderlich | 73da7d5 | 2013-07-11 16:09:06 +0200 | [diff] [blame] | 848 | ieee80211_recalc_chanctx_chantype(local, ctx); |
| 849 | ieee80211_recalc_smps_chanctx(local, ctx); |
| 850 | ieee80211_recalc_radar_chanctx(local, ctx); |
Eliad Peller | 21f659b | 2013-11-11 20:14:01 +0200 | [diff] [blame] | 851 | ieee80211_recalc_chanctx_min_def(local, ctx); |
Simon Wunderlich | 73da7d5 | 2013-07-11 16:09:06 +0200 | [diff] [blame] | 852 | |
Luciano Coelho | 33ffd95 | 2014-01-30 22:08:16 +0200 | [diff] [blame] | 853 | return 0; |
| 854 | } |
| 855 | |
| 856 | int ieee80211_vif_change_channel(struct ieee80211_sub_if_data *sdata, |
| 857 | u32 *changed) |
| 858 | { |
| 859 | struct ieee80211_local *local = sdata->local; |
| 860 | struct ieee80211_chanctx_conf *conf; |
| 861 | struct ieee80211_chanctx *ctx; |
| 862 | int ret; |
| 863 | |
| 864 | lockdep_assert_held(&local->mtx); |
| 865 | |
| 866 | /* should never be called if not performing a channel switch. */ |
| 867 | if (WARN_ON(!sdata->vif.csa_active)) |
| 868 | return -EINVAL; |
| 869 | |
| 870 | mutex_lock(&local->chanctx_mtx); |
| 871 | conf = rcu_dereference_protected(sdata->vif.chanctx_conf, |
| 872 | lockdep_is_held(&local->chanctx_mtx)); |
| 873 | if (!conf) { |
| 874 | ret = -EINVAL; |
| 875 | goto out; |
| 876 | } |
| 877 | |
| 878 | ctx = container_of(conf, struct ieee80211_chanctx, conf); |
| 879 | |
| 880 | ret = __ieee80211_vif_change_channel(sdata, ctx, changed); |
Simon Wunderlich | 73da7d5 | 2013-07-11 16:09:06 +0200 | [diff] [blame] | 881 | out: |
| 882 | mutex_unlock(&local->chanctx_mtx); |
| 883 | return ret; |
| 884 | } |
| 885 | |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 886 | static void |
| 887 | __ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata, |
| 888 | bool clear) |
| 889 | { |
Johannes Berg | 33926eb | 2014-04-09 21:31:13 +0200 | [diff] [blame] | 890 | struct ieee80211_local *local __maybe_unused = sdata->local; |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 891 | struct ieee80211_sub_if_data *vlan; |
| 892 | struct ieee80211_chanctx_conf *conf; |
| 893 | |
| 894 | if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP)) |
| 895 | return; |
| 896 | |
| 897 | lockdep_assert_held(&local->mtx); |
| 898 | |
| 899 | /* Check that conf exists, even when clearing this function |
| 900 | * must be called with the AP's channel context still there |
| 901 | * as it would otherwise cause VLANs to have an invalid |
| 902 | * channel context pointer for a while, possibly pointing |
| 903 | * to a channel context that has already been freed. |
| 904 | */ |
| 905 | conf = rcu_dereference_protected(sdata->vif.chanctx_conf, |
Johannes Berg | 33926eb | 2014-04-09 21:31:13 +0200 | [diff] [blame] | 906 | lockdep_is_held(&local->chanctx_mtx)); |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 907 | WARN_ON(!conf); |
| 908 | |
| 909 | if (clear) |
| 910 | conf = NULL; |
| 911 | |
| 912 | list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) |
| 913 | rcu_assign_pointer(vlan->vif.chanctx_conf, conf); |
| 914 | } |
| 915 | |
| 916 | void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata, |
| 917 | bool clear) |
| 918 | { |
| 919 | struct ieee80211_local *local = sdata->local; |
| 920 | |
| 921 | mutex_lock(&local->chanctx_mtx); |
| 922 | |
| 923 | __ieee80211_vif_copy_chanctx_to_vlans(sdata, clear); |
| 924 | |
| 925 | mutex_unlock(&local->chanctx_mtx); |
| 926 | } |
| 927 | |
| 928 | int ieee80211_vif_unreserve_chanctx(struct ieee80211_sub_if_data *sdata) |
| 929 | { |
Michal Kazior | e3afb92 | 2014-04-09 15:29:27 +0200 | [diff] [blame] | 930 | struct ieee80211_chanctx *ctx = sdata->reserved_chanctx; |
| 931 | |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 932 | lockdep_assert_held(&sdata->local->chanctx_mtx); |
| 933 | |
Michal Kazior | e3afb92 | 2014-04-09 15:29:27 +0200 | [diff] [blame] | 934 | if (WARN_ON(!ctx)) |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 935 | return -EINVAL; |
| 936 | |
Michal Kazior | e3afb92 | 2014-04-09 15:29:27 +0200 | [diff] [blame] | 937 | list_del(&sdata->reserved_chanctx_list); |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 938 | sdata->reserved_chanctx = NULL; |
| 939 | |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 940 | if (ieee80211_chanctx_refcount(sdata->local, ctx) == 0) { |
| 941 | if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) { |
| 942 | if (WARN_ON(!ctx->replace_ctx)) |
| 943 | return -EINVAL; |
| 944 | |
| 945 | WARN_ON(ctx->replace_ctx->replace_state != |
| 946 | IEEE80211_CHANCTX_WILL_BE_REPLACED); |
| 947 | WARN_ON(ctx->replace_ctx->replace_ctx != ctx); |
| 948 | |
| 949 | ctx->replace_ctx->replace_ctx = NULL; |
| 950 | ctx->replace_ctx->replace_state = |
| 951 | IEEE80211_CHANCTX_REPLACE_NONE; |
| 952 | |
| 953 | list_del_rcu(&ctx->list); |
| 954 | kfree_rcu(ctx, rcu_head); |
| 955 | } else { |
| 956 | ieee80211_free_chanctx(sdata->local, ctx); |
| 957 | } |
| 958 | } |
Michal Kazior | e3afb92 | 2014-04-09 15:29:27 +0200 | [diff] [blame] | 959 | |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 960 | return 0; |
| 961 | } |
| 962 | |
| 963 | int ieee80211_vif_reserve_chanctx(struct ieee80211_sub_if_data *sdata, |
| 964 | const struct cfg80211_chan_def *chandef, |
Michal Kazior | 0933248 | 2014-04-09 15:29:25 +0200 | [diff] [blame] | 965 | enum ieee80211_chanctx_mode mode, |
| 966 | bool radar_required) |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 967 | { |
| 968 | struct ieee80211_local *local = sdata->local; |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 969 | struct ieee80211_chanctx *new_ctx, *curr_ctx, *ctx; |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 970 | |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 971 | lockdep_assert_held(&local->chanctx_mtx); |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 972 | |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 973 | curr_ctx = ieee80211_vif_get_chanctx(sdata); |
| 974 | if (curr_ctx && local->use_chanctx && !local->ops->switch_vif_chanctx) |
| 975 | return -ENOTSUPP; |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 976 | |
Michal Kazior | 13f348a | 2014-04-09 15:29:29 +0200 | [diff] [blame] | 977 | new_ctx = ieee80211_find_reservation_chanctx(local, chandef, mode); |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 978 | if (!new_ctx) { |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 979 | if (ieee80211_can_create_new_chanctx(local)) { |
Luciano Coelho | 5d52ee8 | 2014-02-27 14:33:47 +0200 | [diff] [blame] | 980 | new_ctx = ieee80211_new_chanctx(local, chandef, mode); |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 981 | if (IS_ERR(new_ctx)) |
| 982 | return PTR_ERR(new_ctx); |
Michal Kazior | c2b90ad | 2014-04-09 15:29:24 +0200 | [diff] [blame] | 983 | } else { |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 984 | if (!curr_ctx || |
| 985 | (curr_ctx->replace_state == |
| 986 | IEEE80211_CHANCTX_WILL_BE_REPLACED) || |
| 987 | !list_empty(&curr_ctx->reserved_vifs)) { |
| 988 | /* |
| 989 | * Another vif already requested this context |
| 990 | * for a reservation. Find another one hoping |
| 991 | * all vifs assigned to it will also switch |
| 992 | * soon enough. |
| 993 | * |
| 994 | * TODO: This needs a little more work as some |
| 995 | * cases (more than 2 chanctx capable devices) |
| 996 | * may fail which could otherwise succeed |
| 997 | * provided some channel context juggling was |
| 998 | * performed. |
| 999 | * |
| 1000 | * Consider ctx1..3, vif1..6, each ctx has 2 |
| 1001 | * vifs. vif1 and vif2 from ctx1 request new |
| 1002 | * different chandefs starting 2 in-place |
| 1003 | * reserations with ctx4 and ctx5 replacing |
| 1004 | * ctx1 and ctx2 respectively. Next vif5 and |
| 1005 | * vif6 from ctx3 reserve ctx4. If vif3 and |
| 1006 | * vif4 remain on ctx2 as they are then this |
| 1007 | * fails unless `replace_ctx` from ctx5 is |
| 1008 | * replaced with ctx3. |
| 1009 | */ |
| 1010 | list_for_each_entry(ctx, &local->chanctx_list, |
| 1011 | list) { |
| 1012 | if (ctx->replace_state != |
| 1013 | IEEE80211_CHANCTX_REPLACE_NONE) |
| 1014 | continue; |
| 1015 | |
| 1016 | if (!list_empty(&ctx->reserved_vifs)) |
| 1017 | continue; |
| 1018 | |
| 1019 | curr_ctx = ctx; |
| 1020 | break; |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | /* |
| 1025 | * If that's true then all available contexts already |
| 1026 | * have reservations and cannot be used. |
| 1027 | */ |
| 1028 | if (!curr_ctx || |
| 1029 | (curr_ctx->replace_state == |
| 1030 | IEEE80211_CHANCTX_WILL_BE_REPLACED) || |
| 1031 | !list_empty(&curr_ctx->reserved_vifs)) |
| 1032 | return -EBUSY; |
| 1033 | |
| 1034 | new_ctx = ieee80211_alloc_chanctx(local, chandef, mode); |
| 1035 | if (!new_ctx) |
| 1036 | return -ENOMEM; |
| 1037 | |
| 1038 | new_ctx->replace_ctx = curr_ctx; |
| 1039 | new_ctx->replace_state = |
| 1040 | IEEE80211_CHANCTX_REPLACES_OTHER; |
| 1041 | |
| 1042 | curr_ctx->replace_ctx = new_ctx; |
| 1043 | curr_ctx->replace_state = |
| 1044 | IEEE80211_CHANCTX_WILL_BE_REPLACED; |
| 1045 | |
| 1046 | list_add_rcu(&new_ctx->list, &local->chanctx_list); |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 1047 | } |
| 1048 | } |
| 1049 | |
Michal Kazior | e3afb92 | 2014-04-09 15:29:27 +0200 | [diff] [blame] | 1050 | list_add(&sdata->reserved_chanctx_list, &new_ctx->reserved_vifs); |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 1051 | sdata->reserved_chanctx = new_ctx; |
| 1052 | sdata->reserved_chandef = *chandef; |
Michal Kazior | 0933248 | 2014-04-09 15:29:25 +0200 | [diff] [blame] | 1053 | sdata->reserved_radar_required = radar_required; |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 1054 | sdata->reserved_ready = false; |
| 1055 | |
| 1056 | return 0; |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 1057 | } |
| 1058 | |
Michal Kazior | 03078de | 2014-06-25 12:35:08 +0200 | [diff] [blame^] | 1059 | static void |
| 1060 | ieee80211_vif_chanctx_reservation_complete(struct ieee80211_sub_if_data *sdata) |
| 1061 | { |
| 1062 | switch (sdata->vif.type) { |
| 1063 | case NL80211_IFTYPE_ADHOC: |
| 1064 | case NL80211_IFTYPE_AP: |
| 1065 | case NL80211_IFTYPE_MESH_POINT: |
| 1066 | ieee80211_queue_work(&sdata->local->hw, |
| 1067 | &sdata->csa_finalize_work); |
| 1068 | break; |
| 1069 | case NL80211_IFTYPE_UNSPECIFIED: |
| 1070 | case NL80211_IFTYPE_STATION: |
| 1071 | case NL80211_IFTYPE_AP_VLAN: |
| 1072 | case NL80211_IFTYPE_WDS: |
| 1073 | case NL80211_IFTYPE_MONITOR: |
| 1074 | case NL80211_IFTYPE_P2P_CLIENT: |
| 1075 | case NL80211_IFTYPE_P2P_GO: |
| 1076 | case NL80211_IFTYPE_P2P_DEVICE: |
| 1077 | case NUM_NL80211_IFTYPES: |
| 1078 | WARN_ON(1); |
| 1079 | break; |
| 1080 | } |
| 1081 | } |
| 1082 | |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 1083 | static int |
| 1084 | ieee80211_vif_use_reserved_reassign(struct ieee80211_sub_if_data *sdata) |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 1085 | { |
| 1086 | struct ieee80211_local *local = sdata->local; |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 1087 | struct ieee80211_vif_chanctx_switch vif_chsw[1] = {}; |
| 1088 | struct ieee80211_chanctx *old_ctx, *new_ctx; |
| 1089 | const struct cfg80211_chan_def *chandef; |
| 1090 | u32 changed = 0; |
| 1091 | int err; |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 1092 | |
| 1093 | lockdep_assert_held(&local->mtx); |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 1094 | lockdep_assert_held(&local->chanctx_mtx); |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 1095 | |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 1096 | new_ctx = sdata->reserved_chanctx; |
| 1097 | old_ctx = ieee80211_vif_get_chanctx(sdata); |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 1098 | |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 1099 | if (WARN_ON(!sdata->reserved_ready)) |
| 1100 | return -EBUSY; |
| 1101 | |
| 1102 | if (WARN_ON(!new_ctx)) |
| 1103 | return -EINVAL; |
| 1104 | |
| 1105 | if (WARN_ON(!old_ctx)) |
| 1106 | return -EINVAL; |
| 1107 | |
| 1108 | if (WARN_ON(new_ctx->replace_state == |
| 1109 | IEEE80211_CHANCTX_REPLACES_OTHER)) |
| 1110 | return -EINVAL; |
| 1111 | |
| 1112 | chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx, |
| 1113 | &sdata->reserved_chandef); |
| 1114 | if (WARN_ON(!chandef)) |
| 1115 | return -EINVAL; |
| 1116 | |
| 1117 | vif_chsw[0].vif = &sdata->vif; |
| 1118 | vif_chsw[0].old_ctx = &old_ctx->conf; |
| 1119 | vif_chsw[0].new_ctx = &new_ctx->conf; |
| 1120 | |
| 1121 | list_del(&sdata->reserved_chanctx_list); |
| 1122 | sdata->reserved_chanctx = NULL; |
| 1123 | |
| 1124 | err = drv_switch_vif_chanctx(local, vif_chsw, 1, |
| 1125 | CHANCTX_SWMODE_REASSIGN_VIF); |
| 1126 | if (err) { |
| 1127 | if (ieee80211_chanctx_refcount(local, new_ctx) == 0) |
| 1128 | ieee80211_free_chanctx(local, new_ctx); |
| 1129 | |
Michal Kazior | 03078de | 2014-06-25 12:35:08 +0200 | [diff] [blame^] | 1130 | goto out; |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 1131 | } |
| 1132 | |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 1133 | list_move(&sdata->assigned_chanctx_list, &new_ctx->assigned_vifs); |
| 1134 | rcu_assign_pointer(sdata->vif.chanctx_conf, &new_ctx->conf); |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 1135 | |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 1136 | if (sdata->vif.type == NL80211_IFTYPE_AP) |
| 1137 | __ieee80211_vif_copy_chanctx_to_vlans(sdata, false); |
| 1138 | |
| 1139 | if (ieee80211_chanctx_refcount(local, old_ctx) == 0) |
| 1140 | ieee80211_free_chanctx(local, old_ctx); |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 1141 | |
| 1142 | if (sdata->vif.bss_conf.chandef.width != sdata->reserved_chandef.width) |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 1143 | changed = BSS_CHANGED_BANDWIDTH; |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 1144 | |
| 1145 | sdata->vif.bss_conf.chandef = sdata->reserved_chandef; |
| 1146 | |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 1147 | if (changed) |
| 1148 | ieee80211_bss_info_change_notify(sdata, changed); |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 1149 | |
Michal Kazior | 03078de | 2014-06-25 12:35:08 +0200 | [diff] [blame^] | 1150 | out: |
| 1151 | ieee80211_vif_chanctx_reservation_complete(sdata); |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 1152 | return err; |
| 1153 | } |
| 1154 | |
| 1155 | static int |
| 1156 | ieee80211_vif_use_reserved_assign(struct ieee80211_sub_if_data *sdata) |
| 1157 | { |
| 1158 | struct ieee80211_local *local = sdata->local; |
| 1159 | struct ieee80211_chanctx *old_ctx, *new_ctx; |
| 1160 | const struct cfg80211_chan_def *chandef; |
| 1161 | int err; |
| 1162 | |
| 1163 | old_ctx = ieee80211_vif_get_chanctx(sdata); |
| 1164 | new_ctx = sdata->reserved_chanctx; |
| 1165 | |
| 1166 | if (WARN_ON(!sdata->reserved_ready)) |
| 1167 | return -EINVAL; |
| 1168 | |
| 1169 | if (WARN_ON(old_ctx)) |
| 1170 | return -EINVAL; |
| 1171 | |
| 1172 | if (WARN_ON(!new_ctx)) |
| 1173 | return -EINVAL; |
| 1174 | |
| 1175 | if (WARN_ON(new_ctx->replace_state == |
| 1176 | IEEE80211_CHANCTX_REPLACES_OTHER)) |
| 1177 | return -EINVAL; |
| 1178 | |
| 1179 | chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx, |
| 1180 | &sdata->reserved_chandef); |
| 1181 | if (WARN_ON(!chandef)) |
| 1182 | return -EINVAL; |
| 1183 | |
| 1184 | list_del(&sdata->reserved_chanctx_list); |
| 1185 | sdata->reserved_chanctx = NULL; |
| 1186 | |
| 1187 | err = ieee80211_assign_vif_chanctx(sdata, new_ctx); |
| 1188 | if (err) { |
| 1189 | if (ieee80211_chanctx_refcount(local, new_ctx) == 0) |
| 1190 | ieee80211_free_chanctx(local, new_ctx); |
| 1191 | |
| 1192 | goto out; |
| 1193 | } |
| 1194 | |
| 1195 | out: |
Michal Kazior | 03078de | 2014-06-25 12:35:08 +0200 | [diff] [blame^] | 1196 | ieee80211_vif_chanctx_reservation_complete(sdata); |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 1197 | return err; |
| 1198 | } |
| 1199 | |
| 1200 | static bool |
| 1201 | ieee80211_vif_has_in_place_reservation(struct ieee80211_sub_if_data *sdata) |
| 1202 | { |
| 1203 | struct ieee80211_chanctx *old_ctx, *new_ctx; |
| 1204 | |
| 1205 | lockdep_assert_held(&sdata->local->chanctx_mtx); |
| 1206 | |
| 1207 | new_ctx = sdata->reserved_chanctx; |
| 1208 | old_ctx = ieee80211_vif_get_chanctx(sdata); |
| 1209 | |
| 1210 | if (!old_ctx) |
| 1211 | return false; |
| 1212 | |
| 1213 | if (WARN_ON(!new_ctx)) |
| 1214 | return false; |
| 1215 | |
| 1216 | if (old_ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED) |
| 1217 | return false; |
| 1218 | |
| 1219 | if (new_ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER) |
| 1220 | return false; |
| 1221 | |
| 1222 | return true; |
| 1223 | } |
| 1224 | |
| 1225 | static int ieee80211_chsw_switch_hwconf(struct ieee80211_local *local, |
| 1226 | struct ieee80211_chanctx *new_ctx) |
| 1227 | { |
| 1228 | const struct cfg80211_chan_def *chandef; |
| 1229 | |
| 1230 | lockdep_assert_held(&local->mtx); |
| 1231 | lockdep_assert_held(&local->chanctx_mtx); |
| 1232 | |
| 1233 | chandef = ieee80211_chanctx_reserved_chandef(local, new_ctx, NULL); |
| 1234 | if (WARN_ON(!chandef)) |
| 1235 | return -EINVAL; |
| 1236 | |
| 1237 | local->hw.conf.radar_enabled = new_ctx->conf.radar_enabled; |
| 1238 | local->_oper_chandef = *chandef; |
| 1239 | ieee80211_hw_config(local, 0); |
| 1240 | |
| 1241 | return 0; |
| 1242 | } |
| 1243 | |
| 1244 | static int ieee80211_chsw_switch_vifs(struct ieee80211_local *local, |
| 1245 | int n_vifs) |
| 1246 | { |
| 1247 | struct ieee80211_vif_chanctx_switch *vif_chsw; |
| 1248 | struct ieee80211_sub_if_data *sdata; |
| 1249 | struct ieee80211_chanctx *ctx, *old_ctx; |
| 1250 | int i, err; |
| 1251 | |
| 1252 | lockdep_assert_held(&local->mtx); |
| 1253 | lockdep_assert_held(&local->chanctx_mtx); |
| 1254 | |
| 1255 | vif_chsw = kzalloc(sizeof(vif_chsw[0]) * n_vifs, GFP_KERNEL); |
| 1256 | if (!vif_chsw) |
| 1257 | return -ENOMEM; |
| 1258 | |
| 1259 | i = 0; |
| 1260 | list_for_each_entry(ctx, &local->chanctx_list, list) { |
| 1261 | if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER) |
| 1262 | continue; |
| 1263 | |
| 1264 | if (WARN_ON(!ctx->replace_ctx)) { |
| 1265 | err = -EINVAL; |
Luciano Coelho | 5d52ee8 | 2014-02-27 14:33:47 +0200 | [diff] [blame] | 1266 | goto out; |
| 1267 | } |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 1268 | |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 1269 | list_for_each_entry(sdata, &ctx->reserved_vifs, |
| 1270 | reserved_chanctx_list) { |
| 1271 | if (!ieee80211_vif_has_in_place_reservation( |
| 1272 | sdata)) |
| 1273 | continue; |
| 1274 | |
| 1275 | old_ctx = ieee80211_vif_get_chanctx(sdata); |
| 1276 | vif_chsw[i].vif = &sdata->vif; |
| 1277 | vif_chsw[i].old_ctx = &old_ctx->conf; |
| 1278 | vif_chsw[i].new_ctx = &ctx->conf; |
| 1279 | |
| 1280 | i++; |
| 1281 | } |
Luciano Coelho | 5d52ee8 | 2014-02-27 14:33:47 +0200 | [diff] [blame] | 1282 | } |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 1283 | |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 1284 | err = drv_switch_vif_chanctx(local, vif_chsw, n_vifs, |
| 1285 | CHANCTX_SWMODE_SWAP_CONTEXTS); |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 1286 | |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 1287 | out: |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 1288 | kfree(vif_chsw); |
| 1289 | return err; |
| 1290 | } |
| 1291 | |
| 1292 | static int ieee80211_chsw_switch_ctxs(struct ieee80211_local *local) |
| 1293 | { |
| 1294 | struct ieee80211_chanctx *ctx; |
| 1295 | int err; |
| 1296 | |
| 1297 | lockdep_assert_held(&local->mtx); |
| 1298 | lockdep_assert_held(&local->chanctx_mtx); |
| 1299 | |
| 1300 | list_for_each_entry(ctx, &local->chanctx_list, list) { |
| 1301 | if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER) |
| 1302 | continue; |
| 1303 | |
| 1304 | if (!list_empty(&ctx->replace_ctx->assigned_vifs)) |
| 1305 | continue; |
| 1306 | |
| 1307 | ieee80211_del_chanctx(local, ctx->replace_ctx); |
| 1308 | err = ieee80211_add_chanctx(local, ctx); |
| 1309 | if (err) |
| 1310 | goto err; |
| 1311 | } |
| 1312 | |
| 1313 | return 0; |
| 1314 | |
| 1315 | err: |
| 1316 | WARN_ON(ieee80211_add_chanctx(local, ctx)); |
| 1317 | list_for_each_entry_continue_reverse(ctx, &local->chanctx_list, list) { |
| 1318 | if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER) |
| 1319 | continue; |
| 1320 | |
| 1321 | if (!list_empty(&ctx->replace_ctx->assigned_vifs)) |
| 1322 | continue; |
| 1323 | |
| 1324 | ieee80211_del_chanctx(local, ctx); |
| 1325 | WARN_ON(ieee80211_add_chanctx(local, ctx->replace_ctx)); |
| 1326 | } |
| 1327 | |
| 1328 | return err; |
| 1329 | } |
| 1330 | |
| 1331 | int |
| 1332 | ieee80211_vif_use_reserved_switch(struct ieee80211_local *local) |
| 1333 | { |
| 1334 | struct ieee80211_sub_if_data *sdata, *sdata_tmp; |
| 1335 | struct ieee80211_chanctx *ctx, *ctx_tmp, *old_ctx; |
| 1336 | struct ieee80211_chanctx *new_ctx = NULL; |
| 1337 | int i, err, n_assigned, n_reserved, n_ready; |
| 1338 | int n_ctx = 0, n_vifs_switch = 0, n_vifs_assign = 0, n_vifs_ctxless = 0; |
| 1339 | |
| 1340 | lockdep_assert_held(&local->mtx); |
| 1341 | lockdep_assert_held(&local->chanctx_mtx); |
| 1342 | |
| 1343 | /* |
| 1344 | * If there are 2 independent pairs of channel contexts performing |
| 1345 | * cross-switch of their vifs this code will still wait until both are |
| 1346 | * ready even though it could be possible to switch one before the |
| 1347 | * other is ready. |
| 1348 | * |
| 1349 | * For practical reasons and code simplicity just do a single huge |
| 1350 | * switch. |
| 1351 | */ |
| 1352 | |
| 1353 | /* |
| 1354 | * Verify if the reservation is still feasible. |
| 1355 | * - if it's not then disconnect |
| 1356 | * - if it is but not all vifs necessary are ready then defer |
| 1357 | */ |
| 1358 | |
| 1359 | list_for_each_entry(ctx, &local->chanctx_list, list) { |
| 1360 | if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER) |
| 1361 | continue; |
| 1362 | |
| 1363 | if (WARN_ON(!ctx->replace_ctx)) { |
| 1364 | err = -EINVAL; |
| 1365 | goto err; |
| 1366 | } |
| 1367 | |
| 1368 | if (!local->use_chanctx) |
| 1369 | new_ctx = ctx; |
| 1370 | |
| 1371 | n_ctx++; |
| 1372 | |
| 1373 | n_assigned = 0; |
| 1374 | n_reserved = 0; |
| 1375 | n_ready = 0; |
| 1376 | |
| 1377 | list_for_each_entry(sdata, &ctx->replace_ctx->assigned_vifs, |
| 1378 | assigned_chanctx_list) { |
| 1379 | n_assigned++; |
| 1380 | if (sdata->reserved_chanctx) { |
| 1381 | n_reserved++; |
| 1382 | if (sdata->reserved_ready) |
| 1383 | n_ready++; |
| 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | if (n_assigned != n_reserved) { |
| 1388 | if (n_ready == n_reserved) { |
| 1389 | wiphy_info(local->hw.wiphy, |
| 1390 | "channel context reservation cannot be finalized because some interfaces aren't switching\n"); |
| 1391 | err = -EBUSY; |
| 1392 | goto err; |
| 1393 | } |
| 1394 | |
| 1395 | return -EAGAIN; |
| 1396 | } |
| 1397 | |
| 1398 | ctx->conf.radar_enabled = false; |
| 1399 | list_for_each_entry(sdata, &ctx->reserved_vifs, |
| 1400 | reserved_chanctx_list) { |
| 1401 | if (ieee80211_vif_has_in_place_reservation(sdata) && |
| 1402 | !sdata->reserved_ready) |
| 1403 | return -EAGAIN; |
| 1404 | |
| 1405 | old_ctx = ieee80211_vif_get_chanctx(sdata); |
| 1406 | if (old_ctx) { |
| 1407 | if (old_ctx->replace_state == |
| 1408 | IEEE80211_CHANCTX_WILL_BE_REPLACED) |
| 1409 | n_vifs_switch++; |
| 1410 | else |
| 1411 | n_vifs_assign++; |
| 1412 | } else { |
| 1413 | n_vifs_ctxless++; |
| 1414 | } |
| 1415 | |
| 1416 | if (sdata->reserved_radar_required) |
| 1417 | ctx->conf.radar_enabled = true; |
| 1418 | } |
| 1419 | } |
| 1420 | |
| 1421 | if (WARN_ON(n_ctx == 0) || |
| 1422 | WARN_ON(n_vifs_switch == 0 && |
| 1423 | n_vifs_assign == 0 && |
| 1424 | n_vifs_ctxless == 0) || |
| 1425 | WARN_ON(n_ctx > 1 && !local->use_chanctx) || |
| 1426 | WARN_ON(!new_ctx && !local->use_chanctx)) { |
| 1427 | err = -EINVAL; |
| 1428 | goto err; |
| 1429 | } |
| 1430 | |
| 1431 | /* |
| 1432 | * All necessary vifs are ready. Perform the switch now depending on |
| 1433 | * reservations and driver capabilities. |
| 1434 | */ |
| 1435 | |
| 1436 | if (local->use_chanctx) { |
| 1437 | if (n_vifs_switch > 0) { |
| 1438 | err = ieee80211_chsw_switch_vifs(local, n_vifs_switch); |
| 1439 | if (err) |
| 1440 | goto err; |
| 1441 | } |
| 1442 | |
| 1443 | if (n_vifs_assign > 0 || n_vifs_ctxless > 0) { |
| 1444 | err = ieee80211_chsw_switch_ctxs(local); |
| 1445 | if (err) |
| 1446 | goto err; |
| 1447 | } |
| 1448 | } else { |
| 1449 | err = ieee80211_chsw_switch_hwconf(local, new_ctx); |
| 1450 | if (err) |
| 1451 | goto err; |
| 1452 | } |
| 1453 | |
| 1454 | /* |
| 1455 | * Update all structures, values and pointers to point to new channel |
| 1456 | * context(s). |
| 1457 | */ |
| 1458 | |
| 1459 | i = 0; |
| 1460 | list_for_each_entry(ctx, &local->chanctx_list, list) { |
| 1461 | if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER) |
| 1462 | continue; |
| 1463 | |
| 1464 | if (WARN_ON(!ctx->replace_ctx)) { |
| 1465 | err = -EINVAL; |
| 1466 | goto err; |
| 1467 | } |
| 1468 | |
| 1469 | list_for_each_entry(sdata, &ctx->reserved_vifs, |
| 1470 | reserved_chanctx_list) { |
| 1471 | u32 changed = 0; |
| 1472 | |
| 1473 | if (!ieee80211_vif_has_in_place_reservation(sdata)) |
| 1474 | continue; |
| 1475 | |
| 1476 | rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf); |
| 1477 | |
| 1478 | if (sdata->vif.type == NL80211_IFTYPE_AP) |
| 1479 | __ieee80211_vif_copy_chanctx_to_vlans(sdata, |
| 1480 | false); |
| 1481 | |
| 1482 | sdata->radar_required = sdata->reserved_radar_required; |
| 1483 | |
| 1484 | if (sdata->vif.bss_conf.chandef.width != |
| 1485 | sdata->reserved_chandef.width) |
| 1486 | changed = BSS_CHANGED_BANDWIDTH; |
| 1487 | |
| 1488 | sdata->vif.bss_conf.chandef = sdata->reserved_chandef; |
| 1489 | if (changed) |
| 1490 | ieee80211_bss_info_change_notify(sdata, |
| 1491 | changed); |
| 1492 | |
| 1493 | ieee80211_recalc_txpower(sdata); |
| 1494 | } |
| 1495 | |
| 1496 | ieee80211_recalc_chanctx_chantype(local, ctx); |
| 1497 | ieee80211_recalc_smps_chanctx(local, ctx); |
| 1498 | ieee80211_recalc_radar_chanctx(local, ctx); |
| 1499 | ieee80211_recalc_chanctx_min_def(local, ctx); |
| 1500 | |
| 1501 | list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs, |
| 1502 | reserved_chanctx_list) { |
| 1503 | if (ieee80211_vif_get_chanctx(sdata) != ctx) |
| 1504 | continue; |
| 1505 | |
| 1506 | list_del(&sdata->reserved_chanctx_list); |
| 1507 | list_move(&sdata->assigned_chanctx_list, |
| 1508 | &new_ctx->assigned_vifs); |
| 1509 | sdata->reserved_chanctx = NULL; |
Michal Kazior | 03078de | 2014-06-25 12:35:08 +0200 | [diff] [blame^] | 1510 | |
| 1511 | ieee80211_vif_chanctx_reservation_complete(sdata); |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | /* |
| 1515 | * This context might have been a dependency for an already |
| 1516 | * ready re-assign reservation interface that was deferred. Do |
| 1517 | * not propagate error to the caller though. The in-place |
| 1518 | * reservation for originally requested interface has already |
| 1519 | * succeeded at this point. |
| 1520 | */ |
| 1521 | list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs, |
| 1522 | reserved_chanctx_list) { |
| 1523 | if (WARN_ON(ieee80211_vif_has_in_place_reservation( |
| 1524 | sdata))) |
| 1525 | continue; |
| 1526 | |
| 1527 | if (WARN_ON(sdata->reserved_chanctx != ctx)) |
| 1528 | continue; |
| 1529 | |
| 1530 | if (!sdata->reserved_ready) |
| 1531 | continue; |
| 1532 | |
| 1533 | if (ieee80211_vif_get_chanctx(sdata)) |
| 1534 | err = ieee80211_vif_use_reserved_reassign( |
| 1535 | sdata); |
| 1536 | else |
| 1537 | err = ieee80211_vif_use_reserved_assign(sdata); |
| 1538 | |
| 1539 | if (err) { |
| 1540 | sdata_info(sdata, |
| 1541 | "failed to finalize (re-)assign reservation (err=%d)\n", |
| 1542 | err); |
| 1543 | ieee80211_vif_unreserve_chanctx(sdata); |
| 1544 | cfg80211_stop_iface(local->hw.wiphy, |
| 1545 | &sdata->wdev, |
| 1546 | GFP_KERNEL); |
| 1547 | } |
| 1548 | } |
| 1549 | } |
| 1550 | |
| 1551 | /* |
| 1552 | * Finally free old contexts |
| 1553 | */ |
| 1554 | |
| 1555 | list_for_each_entry_safe(ctx, ctx_tmp, &local->chanctx_list, list) { |
| 1556 | if (ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED) |
| 1557 | continue; |
| 1558 | |
| 1559 | ctx->replace_ctx->replace_ctx = NULL; |
| 1560 | ctx->replace_ctx->replace_state = |
| 1561 | IEEE80211_CHANCTX_REPLACE_NONE; |
| 1562 | |
| 1563 | list_del_rcu(&ctx->list); |
| 1564 | kfree_rcu(ctx, rcu_head); |
| 1565 | } |
| 1566 | |
| 1567 | return 0; |
| 1568 | |
| 1569 | err: |
| 1570 | list_for_each_entry(ctx, &local->chanctx_list, list) { |
| 1571 | if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER) |
| 1572 | continue; |
| 1573 | |
| 1574 | list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs, |
Michal Kazior | 03078de | 2014-06-25 12:35:08 +0200 | [diff] [blame^] | 1575 | reserved_chanctx_list) { |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 1576 | ieee80211_vif_unreserve_chanctx(sdata); |
Michal Kazior | 03078de | 2014-06-25 12:35:08 +0200 | [diff] [blame^] | 1577 | ieee80211_vif_chanctx_reservation_complete(sdata); |
| 1578 | } |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | return err; |
| 1582 | } |
| 1583 | |
| 1584 | int ieee80211_vif_use_reserved_context(struct ieee80211_sub_if_data *sdata) |
| 1585 | { |
| 1586 | struct ieee80211_local *local = sdata->local; |
| 1587 | struct ieee80211_chanctx *new_ctx; |
| 1588 | struct ieee80211_chanctx *old_ctx; |
| 1589 | int err; |
| 1590 | |
| 1591 | lockdep_assert_held(&local->mtx); |
| 1592 | lockdep_assert_held(&local->chanctx_mtx); |
| 1593 | |
| 1594 | new_ctx = sdata->reserved_chanctx; |
| 1595 | old_ctx = ieee80211_vif_get_chanctx(sdata); |
| 1596 | |
| 1597 | if (WARN_ON(!new_ctx)) |
| 1598 | return -EINVAL; |
| 1599 | |
| 1600 | if (WARN_ON(new_ctx->replace_state == |
| 1601 | IEEE80211_CHANCTX_WILL_BE_REPLACED)) |
| 1602 | return -EINVAL; |
| 1603 | |
| 1604 | if (WARN_ON(sdata->reserved_ready)) |
| 1605 | return -EINVAL; |
| 1606 | |
| 1607 | sdata->reserved_ready = true; |
| 1608 | |
| 1609 | if (new_ctx->replace_state == IEEE80211_CHANCTX_REPLACE_NONE) { |
| 1610 | if (old_ctx) |
| 1611 | err = ieee80211_vif_use_reserved_reassign(sdata); |
| 1612 | else |
| 1613 | err = ieee80211_vif_use_reserved_assign(sdata); |
| 1614 | |
| 1615 | if (err) |
| 1616 | return err; |
| 1617 | } |
| 1618 | |
| 1619 | /* |
| 1620 | * In-place reservation may need to be finalized now either if: |
| 1621 | * a) sdata is taking part in the swapping itself and is the last one |
| 1622 | * b) sdata has switched with a re-assign reservation to an existing |
| 1623 | * context readying in-place switching of old_ctx |
| 1624 | * |
| 1625 | * In case of (b) do not propagate the error up because the requested |
| 1626 | * sdata already switched successfully. Just spill an extra warning. |
| 1627 | * The ieee80211_vif_use_reserved_switch() already stops all necessary |
| 1628 | * interfaces upon failure. |
| 1629 | */ |
| 1630 | if ((old_ctx && |
| 1631 | old_ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED) || |
| 1632 | new_ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) { |
| 1633 | err = ieee80211_vif_use_reserved_switch(local); |
| 1634 | if (err && err != -EAGAIN) { |
| 1635 | if (new_ctx->replace_state == |
| 1636 | IEEE80211_CHANCTX_REPLACES_OTHER) |
| 1637 | return err; |
| 1638 | |
| 1639 | wiphy_info(local->hw.wiphy, |
| 1640 | "depending in-place reservation failed (err=%d)\n", |
| 1641 | err); |
| 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | return 0; |
Luciano Coelho | 11335a55 | 2013-10-30 13:09:39 +0200 | [diff] [blame] | 1646 | } |
| 1647 | |
Johannes Berg | 2c9b735 | 2013-02-07 21:37:29 +0100 | [diff] [blame] | 1648 | int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata, |
| 1649 | const struct cfg80211_chan_def *chandef, |
| 1650 | u32 *changed) |
| 1651 | { |
| 1652 | struct ieee80211_local *local = sdata->local; |
| 1653 | struct ieee80211_chanctx_conf *conf; |
| 1654 | struct ieee80211_chanctx *ctx; |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 1655 | const struct cfg80211_chan_def *compat; |
Johannes Berg | 2c9b735 | 2013-02-07 21:37:29 +0100 | [diff] [blame] | 1656 | int ret; |
| 1657 | |
| 1658 | if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef, |
| 1659 | IEEE80211_CHAN_DISABLED)) |
| 1660 | return -EINVAL; |
| 1661 | |
| 1662 | mutex_lock(&local->chanctx_mtx); |
| 1663 | if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) { |
| 1664 | ret = 0; |
| 1665 | goto out; |
| 1666 | } |
| 1667 | |
| 1668 | if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT || |
| 1669 | sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) { |
| 1670 | ret = -EINVAL; |
| 1671 | goto out; |
| 1672 | } |
| 1673 | |
| 1674 | conf = rcu_dereference_protected(sdata->vif.chanctx_conf, |
| 1675 | lockdep_is_held(&local->chanctx_mtx)); |
| 1676 | if (!conf) { |
| 1677 | ret = -EINVAL; |
| 1678 | goto out; |
| 1679 | } |
| 1680 | |
| 1681 | ctx = container_of(conf, struct ieee80211_chanctx, conf); |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 1682 | |
| 1683 | compat = cfg80211_chandef_compatible(&conf->def, chandef); |
| 1684 | if (!compat) { |
Johannes Berg | 2c9b735 | 2013-02-07 21:37:29 +0100 | [diff] [blame] | 1685 | ret = -EINVAL; |
| 1686 | goto out; |
| 1687 | } |
| 1688 | |
Michal Kazior | 5bcae31 | 2014-06-25 12:35:06 +0200 | [diff] [blame] | 1689 | switch (ctx->replace_state) { |
| 1690 | case IEEE80211_CHANCTX_REPLACE_NONE: |
| 1691 | if (!ieee80211_chanctx_reserved_chandef(local, ctx, compat)) { |
| 1692 | ret = -EBUSY; |
| 1693 | goto out; |
| 1694 | } |
| 1695 | break; |
| 1696 | case IEEE80211_CHANCTX_WILL_BE_REPLACED: |
| 1697 | /* TODO: Perhaps the bandwith change could be treated as a |
| 1698 | * reservation itself? */ |
| 1699 | ret = -EBUSY; |
| 1700 | goto out; |
| 1701 | case IEEE80211_CHANCTX_REPLACES_OTHER: |
| 1702 | /* channel context that is going to replace another channel |
| 1703 | * context doesn't really exist and shouldn't be assigned |
| 1704 | * anywhere yet */ |
| 1705 | WARN_ON(1); |
| 1706 | break; |
| 1707 | } |
| 1708 | |
Johannes Berg | 2c9b735 | 2013-02-07 21:37:29 +0100 | [diff] [blame] | 1709 | sdata->vif.bss_conf.chandef = *chandef; |
| 1710 | |
| 1711 | ieee80211_recalc_chanctx_chantype(local, ctx); |
| 1712 | |
| 1713 | *changed |= BSS_CHANGED_BANDWIDTH; |
| 1714 | ret = 0; |
| 1715 | out: |
| 1716 | mutex_unlock(&local->chanctx_mtx); |
| 1717 | return ret; |
| 1718 | } |
| 1719 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 1720 | void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata) |
| 1721 | { |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 1722 | WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev)); |
| 1723 | |
Johannes Berg | 34a3740 | 2013-12-18 09:43:33 +0100 | [diff] [blame] | 1724 | lockdep_assert_held(&sdata->local->mtx); |
| 1725 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 1726 | mutex_lock(&sdata->local->chanctx_mtx); |
| 1727 | __ieee80211_vif_release_channel(sdata); |
| 1728 | mutex_unlock(&sdata->local->chanctx_mtx); |
| 1729 | } |
Johannes Berg | 3448c00 | 2012-09-11 17:57:42 +0200 | [diff] [blame] | 1730 | |
Johannes Berg | 4d76d21 | 2012-12-11 20:38:41 +0100 | [diff] [blame] | 1731 | void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata) |
| 1732 | { |
| 1733 | struct ieee80211_local *local = sdata->local; |
| 1734 | struct ieee80211_sub_if_data *ap; |
| 1735 | struct ieee80211_chanctx_conf *conf; |
| 1736 | |
| 1737 | if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss)) |
| 1738 | return; |
| 1739 | |
| 1740 | ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap); |
| 1741 | |
| 1742 | mutex_lock(&local->chanctx_mtx); |
| 1743 | |
| 1744 | conf = rcu_dereference_protected(ap->vif.chanctx_conf, |
| 1745 | lockdep_is_held(&local->chanctx_mtx)); |
| 1746 | rcu_assign_pointer(sdata->vif.chanctx_conf, conf); |
| 1747 | mutex_unlock(&local->chanctx_mtx); |
| 1748 | } |
| 1749 | |
Johannes Berg | 3448c00 | 2012-09-11 17:57:42 +0200 | [diff] [blame] | 1750 | void ieee80211_iter_chan_contexts_atomic( |
| 1751 | struct ieee80211_hw *hw, |
| 1752 | void (*iter)(struct ieee80211_hw *hw, |
| 1753 | struct ieee80211_chanctx_conf *chanctx_conf, |
| 1754 | void *data), |
| 1755 | void *iter_data) |
| 1756 | { |
| 1757 | struct ieee80211_local *local = hw_to_local(hw); |
| 1758 | struct ieee80211_chanctx *ctx; |
| 1759 | |
| 1760 | rcu_read_lock(); |
| 1761 | list_for_each_entry_rcu(ctx, &local->chanctx_list, list) |
Johannes Berg | 8a61af6 | 2012-12-13 17:42:30 +0100 | [diff] [blame] | 1762 | if (ctx->driver_present) |
| 1763 | iter(hw, &ctx->conf, iter_data); |
Johannes Berg | 3448c00 | 2012-09-11 17:57:42 +0200 | [diff] [blame] | 1764 | rcu_read_unlock(); |
| 1765 | } |
| 1766 | EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic); |