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 | |
Johannes Berg | 18942d3 | 2013-02-07 21:30:37 +0100 | [diff] [blame] | 12 | static void ieee80211_change_chanctx(struct ieee80211_local *local, |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 13 | struct ieee80211_chanctx *ctx, |
| 14 | const struct cfg80211_chan_def *chandef) |
Michal Kazior | 23a85b45 | 2012-06-26 14:37:21 +0200 | [diff] [blame] | 15 | { |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 16 | if (cfg80211_chandef_identical(&ctx->conf.def, chandef)) |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 17 | return; |
| 18 | |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 19 | WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef)); |
| 20 | |
| 21 | ctx->conf.def = *chandef; |
| 22 | drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_WIDTH); |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 23 | |
| 24 | if (!local->use_chanctx) { |
Karl Beldan | 675a0b0 | 2013-03-25 16:26:57 +0100 | [diff] [blame^] | 25 | local->_oper_chandef = *chandef; |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 26 | ieee80211_hw_config(local, 0); |
| 27 | } |
Johannes Berg | 0aaffa9 | 2010-05-05 15:28:27 +0200 | [diff] [blame] | 28 | } |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 29 | |
| 30 | static struct ieee80211_chanctx * |
| 31 | ieee80211_find_chanctx(struct ieee80211_local *local, |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 32 | const struct cfg80211_chan_def *chandef, |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 33 | enum ieee80211_chanctx_mode mode) |
| 34 | { |
| 35 | struct ieee80211_chanctx *ctx; |
| 36 | |
| 37 | lockdep_assert_held(&local->chanctx_mtx); |
| 38 | |
| 39 | if (mode == IEEE80211_CHANCTX_EXCLUSIVE) |
| 40 | return NULL; |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 41 | |
| 42 | list_for_each_entry(ctx, &local->chanctx_list, list) { |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 43 | const struct cfg80211_chan_def *compat; |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 44 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 45 | if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE) |
| 46 | continue; |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 47 | |
| 48 | compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef); |
| 49 | if (!compat) |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 50 | continue; |
| 51 | |
Johannes Berg | 18942d3 | 2013-02-07 21:30:37 +0100 | [diff] [blame] | 52 | ieee80211_change_chanctx(local, ctx, compat); |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 53 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 54 | return ctx; |
| 55 | } |
| 56 | |
| 57 | return NULL; |
| 58 | } |
| 59 | |
| 60 | static struct ieee80211_chanctx * |
| 61 | ieee80211_new_chanctx(struct ieee80211_local *local, |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 62 | const struct cfg80211_chan_def *chandef, |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 63 | enum ieee80211_chanctx_mode mode) |
| 64 | { |
| 65 | struct ieee80211_chanctx *ctx; |
Michal Kazior | 35f2fce | 2012-06-26 14:37:20 +0200 | [diff] [blame] | 66 | int err; |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 67 | |
| 68 | lockdep_assert_held(&local->chanctx_mtx); |
| 69 | |
| 70 | ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL); |
| 71 | if (!ctx) |
| 72 | return ERR_PTR(-ENOMEM); |
| 73 | |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 74 | ctx->conf.def = *chandef; |
Johannes Berg | 04ecd25 | 2012-09-11 14:34:12 +0200 | [diff] [blame] | 75 | ctx->conf.rx_chains_static = 1; |
| 76 | ctx->conf.rx_chains_dynamic = 1; |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 77 | ctx->mode = mode; |
| 78 | |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 79 | if (!local->use_chanctx) { |
Karl Beldan | 675a0b0 | 2013-03-25 16:26:57 +0100 | [diff] [blame^] | 80 | local->_oper_chandef = *chandef; |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 81 | ieee80211_hw_config(local, 0); |
| 82 | } else { |
| 83 | err = drv_add_chanctx(local, ctx); |
| 84 | if (err) { |
| 85 | kfree(ctx); |
| 86 | return ERR_PTR(err); |
| 87 | } |
Michal Kazior | 35f2fce | 2012-06-26 14:37:20 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Johannes Berg | 3448c00 | 2012-09-11 17:57:42 +0200 | [diff] [blame] | 90 | list_add_rcu(&ctx->list, &local->chanctx_list); |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 91 | |
Johannes Berg | fd0f979 | 2013-02-07 00:14:51 +0100 | [diff] [blame] | 92 | mutex_lock(&local->mtx); |
| 93 | ieee80211_recalc_idle(local); |
| 94 | mutex_unlock(&local->mtx); |
| 95 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 96 | return ctx; |
| 97 | } |
| 98 | |
| 99 | static void ieee80211_free_chanctx(struct ieee80211_local *local, |
| 100 | struct ieee80211_chanctx *ctx) |
| 101 | { |
| 102 | lockdep_assert_held(&local->chanctx_mtx); |
| 103 | |
| 104 | WARN_ON_ONCE(ctx->refcount != 0); |
| 105 | |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 106 | if (!local->use_chanctx) { |
Karl Beldan | 675a0b0 | 2013-03-25 16:26:57 +0100 | [diff] [blame^] | 107 | struct cfg80211_chan_def *chandef = &local->_oper_chandef; |
| 108 | chandef->width = NL80211_CHAN_WIDTH_20_NOHT; |
| 109 | chandef->center_freq1 = chandef->chan->center_freq; |
| 110 | chandef->center_freq2 = 0; |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 111 | ieee80211_hw_config(local, 0); |
| 112 | } else { |
| 113 | drv_remove_chanctx(local, ctx); |
| 114 | } |
Michal Kazior | 35f2fce | 2012-06-26 14:37:20 +0200 | [diff] [blame] | 115 | |
Johannes Berg | 3448c00 | 2012-09-11 17:57:42 +0200 | [diff] [blame] | 116 | list_del_rcu(&ctx->list); |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 117 | kfree_rcu(ctx, rcu_head); |
Johannes Berg | fd0f979 | 2013-02-07 00:14:51 +0100 | [diff] [blame] | 118 | |
| 119 | mutex_lock(&local->mtx); |
| 120 | ieee80211_recalc_idle(local); |
| 121 | mutex_unlock(&local->mtx); |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata, |
| 125 | struct ieee80211_chanctx *ctx) |
| 126 | { |
Michal Kazior | 35f2fce | 2012-06-26 14:37:20 +0200 | [diff] [blame] | 127 | struct ieee80211_local *local = sdata->local; |
| 128 | int ret; |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 129 | |
| 130 | lockdep_assert_held(&local->chanctx_mtx); |
| 131 | |
Michal Kazior | 35f2fce | 2012-06-26 14:37:20 +0200 | [diff] [blame] | 132 | ret = drv_assign_vif_chanctx(local, sdata, ctx); |
| 133 | if (ret) |
| 134 | return ret; |
| 135 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 136 | rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf); |
| 137 | ctx->refcount++; |
| 138 | |
Johannes Berg | 1ea6f9c | 2012-10-24 10:59:25 +0200 | [diff] [blame] | 139 | ieee80211_recalc_txpower(sdata); |
Johannes Berg | fd0f979 | 2013-02-07 00:14:51 +0100 | [diff] [blame] | 140 | sdata->vif.bss_conf.idle = false; |
Johannes Berg | 5bbe754d | 2013-02-13 13:50:51 +0100 | [diff] [blame] | 141 | |
| 142 | if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE && |
| 143 | sdata->vif.type != NL80211_IFTYPE_MONITOR) |
| 144 | ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IDLE); |
Johannes Berg | 1ea6f9c | 2012-10-24 10:59:25 +0200 | [diff] [blame] | 145 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 146 | return 0; |
| 147 | } |
| 148 | |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 149 | static void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local, |
| 150 | struct ieee80211_chanctx *ctx) |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 151 | { |
| 152 | struct ieee80211_chanctx_conf *conf = &ctx->conf; |
| 153 | struct ieee80211_sub_if_data *sdata; |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 154 | const struct cfg80211_chan_def *compat = NULL; |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 155 | |
| 156 | lockdep_assert_held(&local->chanctx_mtx); |
| 157 | |
| 158 | rcu_read_lock(); |
| 159 | list_for_each_entry_rcu(sdata, &local->interfaces, list) { |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 160 | |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 161 | if (!ieee80211_sdata_running(sdata)) |
| 162 | continue; |
| 163 | if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf) |
| 164 | continue; |
| 165 | |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 166 | if (!compat) |
| 167 | compat = &sdata->vif.bss_conf.chandef; |
| 168 | |
| 169 | compat = cfg80211_chandef_compatible( |
| 170 | &sdata->vif.bss_conf.chandef, compat); |
| 171 | if (!compat) |
| 172 | break; |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 173 | } |
| 174 | rcu_read_unlock(); |
| 175 | |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 176 | if (WARN_ON_ONCE(!compat)) |
| 177 | return; |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 178 | |
Johannes Berg | 18942d3 | 2013-02-07 21:30:37 +0100 | [diff] [blame] | 179 | ieee80211_change_chanctx(local, ctx, compat); |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 180 | } |
| 181 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 182 | static void ieee80211_unassign_vif_chanctx(struct ieee80211_sub_if_data *sdata, |
| 183 | struct ieee80211_chanctx *ctx) |
| 184 | { |
Michal Kazior | 35f2fce | 2012-06-26 14:37:20 +0200 | [diff] [blame] | 185 | struct ieee80211_local *local = sdata->local; |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 186 | |
| 187 | lockdep_assert_held(&local->chanctx_mtx); |
| 188 | |
| 189 | ctx->refcount--; |
| 190 | rcu_assign_pointer(sdata->vif.chanctx_conf, NULL); |
Michal Kazior | 35f2fce | 2012-06-26 14:37:20 +0200 | [diff] [blame] | 191 | |
Johannes Berg | fd0f979 | 2013-02-07 00:14:51 +0100 | [diff] [blame] | 192 | sdata->vif.bss_conf.idle = true; |
Johannes Berg | 5bbe754d | 2013-02-13 13:50:51 +0100 | [diff] [blame] | 193 | |
| 194 | if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE && |
| 195 | sdata->vif.type != NL80211_IFTYPE_MONITOR) |
| 196 | ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IDLE); |
Johannes Berg | fd0f979 | 2013-02-07 00:14:51 +0100 | [diff] [blame] | 197 | |
Michal Kazior | 35f2fce | 2012-06-26 14:37:20 +0200 | [diff] [blame] | 198 | drv_unassign_vif_chanctx(local, sdata, ctx); |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 199 | |
Johannes Berg | 04ecd25 | 2012-09-11 14:34:12 +0200 | [diff] [blame] | 200 | if (ctx->refcount > 0) { |
Michal Kazior | e89a96f | 2012-06-26 14:37:22 +0200 | [diff] [blame] | 201 | ieee80211_recalc_chanctx_chantype(sdata->local, ctx); |
Johannes Berg | 04ecd25 | 2012-09-11 14:34:12 +0200 | [diff] [blame] | 202 | ieee80211_recalc_smps_chanctx(local, ctx); |
Simon Wunderlich | 164eb02 | 2013-02-08 18:16:20 +0100 | [diff] [blame] | 203 | ieee80211_recalc_radar_chanctx(local, ctx); |
Johannes Berg | 04ecd25 | 2012-09-11 14:34:12 +0200 | [diff] [blame] | 204 | } |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata) |
| 208 | { |
| 209 | struct ieee80211_local *local = sdata->local; |
| 210 | struct ieee80211_chanctx_conf *conf; |
| 211 | struct ieee80211_chanctx *ctx; |
| 212 | |
| 213 | lockdep_assert_held(&local->chanctx_mtx); |
| 214 | |
| 215 | conf = rcu_dereference_protected(sdata->vif.chanctx_conf, |
| 216 | lockdep_is_held(&local->chanctx_mtx)); |
| 217 | if (!conf) |
| 218 | return; |
| 219 | |
| 220 | ctx = container_of(conf, struct ieee80211_chanctx, conf); |
| 221 | |
| 222 | ieee80211_unassign_vif_chanctx(sdata, ctx); |
| 223 | if (ctx->refcount == 0) |
| 224 | ieee80211_free_chanctx(local, ctx); |
| 225 | } |
| 226 | |
Simon Wunderlich | 164eb02 | 2013-02-08 18:16:20 +0100 | [diff] [blame] | 227 | void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local, |
| 228 | struct ieee80211_chanctx *chanctx) |
| 229 | { |
| 230 | struct ieee80211_sub_if_data *sdata; |
| 231 | bool radar_enabled = false; |
| 232 | |
| 233 | lockdep_assert_held(&local->chanctx_mtx); |
| 234 | |
| 235 | rcu_read_lock(); |
| 236 | list_for_each_entry_rcu(sdata, &local->interfaces, list) { |
| 237 | if (sdata->radar_required) { |
| 238 | radar_enabled = true; |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | rcu_read_unlock(); |
| 243 | |
| 244 | if (radar_enabled == chanctx->conf.radar_enabled) |
| 245 | return; |
| 246 | |
| 247 | chanctx->conf.radar_enabled = radar_enabled; |
| 248 | local->radar_detect_enabled = chanctx->conf.radar_enabled; |
| 249 | |
| 250 | if (!local->use_chanctx) { |
| 251 | local->hw.conf.radar_enabled = chanctx->conf.radar_enabled; |
| 252 | ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL); |
| 253 | } |
| 254 | |
| 255 | drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR); |
| 256 | } |
| 257 | |
Johannes Berg | 04ecd25 | 2012-09-11 14:34:12 +0200 | [diff] [blame] | 258 | void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local, |
| 259 | struct ieee80211_chanctx *chanctx) |
| 260 | { |
| 261 | struct ieee80211_sub_if_data *sdata; |
| 262 | u8 rx_chains_static, rx_chains_dynamic; |
| 263 | |
| 264 | lockdep_assert_held(&local->chanctx_mtx); |
| 265 | |
| 266 | rx_chains_static = 1; |
| 267 | rx_chains_dynamic = 1; |
| 268 | |
| 269 | rcu_read_lock(); |
| 270 | list_for_each_entry_rcu(sdata, &local->interfaces, list) { |
| 271 | u8 needed_static, needed_dynamic; |
| 272 | |
| 273 | if (!ieee80211_sdata_running(sdata)) |
| 274 | continue; |
| 275 | |
| 276 | if (rcu_access_pointer(sdata->vif.chanctx_conf) != |
| 277 | &chanctx->conf) |
| 278 | continue; |
| 279 | |
| 280 | switch (sdata->vif.type) { |
| 281 | case NL80211_IFTYPE_P2P_DEVICE: |
| 282 | continue; |
| 283 | case NL80211_IFTYPE_STATION: |
| 284 | if (!sdata->u.mgd.associated) |
| 285 | continue; |
| 286 | break; |
| 287 | case NL80211_IFTYPE_AP_VLAN: |
| 288 | continue; |
| 289 | case NL80211_IFTYPE_AP: |
| 290 | case NL80211_IFTYPE_ADHOC: |
| 291 | case NL80211_IFTYPE_WDS: |
| 292 | case NL80211_IFTYPE_MESH_POINT: |
| 293 | break; |
| 294 | default: |
| 295 | WARN_ON_ONCE(1); |
| 296 | } |
| 297 | |
| 298 | switch (sdata->smps_mode) { |
| 299 | default: |
| 300 | WARN_ONCE(1, "Invalid SMPS mode %d\n", |
| 301 | sdata->smps_mode); |
| 302 | /* fall through */ |
| 303 | case IEEE80211_SMPS_OFF: |
| 304 | needed_static = sdata->needed_rx_chains; |
| 305 | needed_dynamic = sdata->needed_rx_chains; |
| 306 | break; |
| 307 | case IEEE80211_SMPS_DYNAMIC: |
| 308 | needed_static = 1; |
| 309 | needed_dynamic = sdata->needed_rx_chains; |
| 310 | break; |
| 311 | case IEEE80211_SMPS_STATIC: |
| 312 | needed_static = 1; |
| 313 | needed_dynamic = 1; |
| 314 | break; |
| 315 | } |
| 316 | |
| 317 | rx_chains_static = max(rx_chains_static, needed_static); |
| 318 | rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic); |
| 319 | } |
| 320 | rcu_read_unlock(); |
| 321 | |
| 322 | if (!local->use_chanctx) { |
| 323 | if (rx_chains_static > 1) |
| 324 | local->smps_mode = IEEE80211_SMPS_OFF; |
| 325 | else if (rx_chains_dynamic > 1) |
| 326 | local->smps_mode = IEEE80211_SMPS_DYNAMIC; |
| 327 | else |
| 328 | local->smps_mode = IEEE80211_SMPS_STATIC; |
| 329 | ieee80211_hw_config(local, 0); |
| 330 | } |
| 331 | |
| 332 | if (rx_chains_static == chanctx->conf.rx_chains_static && |
| 333 | rx_chains_dynamic == chanctx->conf.rx_chains_dynamic) |
| 334 | return; |
| 335 | |
| 336 | chanctx->conf.rx_chains_static = rx_chains_static; |
| 337 | chanctx->conf.rx_chains_dynamic = rx_chains_dynamic; |
| 338 | drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS); |
| 339 | } |
| 340 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 341 | int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata, |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 342 | const struct cfg80211_chan_def *chandef, |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 343 | enum ieee80211_chanctx_mode mode) |
| 344 | { |
| 345 | struct ieee80211_local *local = sdata->local; |
| 346 | struct ieee80211_chanctx *ctx; |
| 347 | int ret; |
| 348 | |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 349 | WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev)); |
| 350 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 351 | mutex_lock(&local->chanctx_mtx); |
| 352 | __ieee80211_vif_release_channel(sdata); |
| 353 | |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 354 | ctx = ieee80211_find_chanctx(local, chandef, mode); |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 355 | if (!ctx) |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 356 | ctx = ieee80211_new_chanctx(local, chandef, mode); |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 357 | if (IS_ERR(ctx)) { |
| 358 | ret = PTR_ERR(ctx); |
| 359 | goto out; |
| 360 | } |
| 361 | |
Johannes Berg | 4bf8853 | 2012-11-09 11:39:59 +0100 | [diff] [blame] | 362 | sdata->vif.bss_conf.chandef = *chandef; |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 363 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 364 | ret = ieee80211_assign_vif_chanctx(sdata, ctx); |
| 365 | if (ret) { |
| 366 | /* if assign fails refcount stays the same */ |
| 367 | if (ctx->refcount == 0) |
| 368 | ieee80211_free_chanctx(local, ctx); |
| 369 | goto out; |
| 370 | } |
| 371 | |
Johannes Berg | 04ecd25 | 2012-09-11 14:34:12 +0200 | [diff] [blame] | 372 | ieee80211_recalc_smps_chanctx(local, ctx); |
Simon Wunderlich | 164eb02 | 2013-02-08 18:16:20 +0100 | [diff] [blame] | 373 | ieee80211_recalc_radar_chanctx(local, ctx); |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 374 | out: |
| 375 | mutex_unlock(&local->chanctx_mtx); |
| 376 | return ret; |
| 377 | } |
| 378 | |
Johannes Berg | 2c9b735 | 2013-02-07 21:37:29 +0100 | [diff] [blame] | 379 | int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata, |
| 380 | const struct cfg80211_chan_def *chandef, |
| 381 | u32 *changed) |
| 382 | { |
| 383 | struct ieee80211_local *local = sdata->local; |
| 384 | struct ieee80211_chanctx_conf *conf; |
| 385 | struct ieee80211_chanctx *ctx; |
| 386 | int ret; |
| 387 | |
| 388 | if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef, |
| 389 | IEEE80211_CHAN_DISABLED)) |
| 390 | return -EINVAL; |
| 391 | |
| 392 | mutex_lock(&local->chanctx_mtx); |
| 393 | if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) { |
| 394 | ret = 0; |
| 395 | goto out; |
| 396 | } |
| 397 | |
| 398 | if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT || |
| 399 | sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) { |
| 400 | ret = -EINVAL; |
| 401 | goto out; |
| 402 | } |
| 403 | |
| 404 | conf = rcu_dereference_protected(sdata->vif.chanctx_conf, |
| 405 | lockdep_is_held(&local->chanctx_mtx)); |
| 406 | if (!conf) { |
| 407 | ret = -EINVAL; |
| 408 | goto out; |
| 409 | } |
| 410 | |
| 411 | ctx = container_of(conf, struct ieee80211_chanctx, conf); |
| 412 | if (!cfg80211_chandef_compatible(&conf->def, chandef)) { |
| 413 | ret = -EINVAL; |
| 414 | goto out; |
| 415 | } |
| 416 | |
| 417 | sdata->vif.bss_conf.chandef = *chandef; |
| 418 | |
| 419 | ieee80211_recalc_chanctx_chantype(local, ctx); |
| 420 | |
| 421 | *changed |= BSS_CHANGED_BANDWIDTH; |
| 422 | ret = 0; |
| 423 | out: |
| 424 | mutex_unlock(&local->chanctx_mtx); |
| 425 | return ret; |
| 426 | } |
| 427 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 428 | void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata) |
| 429 | { |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 430 | WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev)); |
| 431 | |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame] | 432 | mutex_lock(&sdata->local->chanctx_mtx); |
| 433 | __ieee80211_vif_release_channel(sdata); |
| 434 | mutex_unlock(&sdata->local->chanctx_mtx); |
| 435 | } |
Johannes Berg | 3448c00 | 2012-09-11 17:57:42 +0200 | [diff] [blame] | 436 | |
Johannes Berg | 4d76d21 | 2012-12-11 20:38:41 +0100 | [diff] [blame] | 437 | void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata) |
| 438 | { |
| 439 | struct ieee80211_local *local = sdata->local; |
| 440 | struct ieee80211_sub_if_data *ap; |
| 441 | struct ieee80211_chanctx_conf *conf; |
| 442 | |
| 443 | if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss)) |
| 444 | return; |
| 445 | |
| 446 | ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap); |
| 447 | |
| 448 | mutex_lock(&local->chanctx_mtx); |
| 449 | |
| 450 | conf = rcu_dereference_protected(ap->vif.chanctx_conf, |
| 451 | lockdep_is_held(&local->chanctx_mtx)); |
| 452 | rcu_assign_pointer(sdata->vif.chanctx_conf, conf); |
| 453 | mutex_unlock(&local->chanctx_mtx); |
| 454 | } |
| 455 | |
Johannes Berg | 1f4ac5a | 2013-02-08 12:07:44 +0100 | [diff] [blame] | 456 | void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata, |
| 457 | bool clear) |
| 458 | { |
| 459 | struct ieee80211_local *local = sdata->local; |
| 460 | struct ieee80211_sub_if_data *vlan; |
| 461 | struct ieee80211_chanctx_conf *conf; |
| 462 | |
| 463 | ASSERT_RTNL(); |
| 464 | |
| 465 | if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP)) |
| 466 | return; |
| 467 | |
| 468 | mutex_lock(&local->chanctx_mtx); |
| 469 | |
| 470 | /* |
| 471 | * Check that conf exists, even when clearing this function |
| 472 | * must be called with the AP's channel context still there |
| 473 | * as it would otherwise cause VLANs to have an invalid |
| 474 | * channel context pointer for a while, possibly pointing |
| 475 | * to a channel context that has already been freed. |
| 476 | */ |
| 477 | conf = rcu_dereference_protected(sdata->vif.chanctx_conf, |
| 478 | lockdep_is_held(&local->chanctx_mtx)); |
| 479 | WARN_ON(!conf); |
| 480 | |
| 481 | if (clear) |
| 482 | conf = NULL; |
| 483 | |
| 484 | list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) |
| 485 | rcu_assign_pointer(vlan->vif.chanctx_conf, conf); |
| 486 | |
| 487 | mutex_unlock(&local->chanctx_mtx); |
| 488 | } |
| 489 | |
Johannes Berg | 3448c00 | 2012-09-11 17:57:42 +0200 | [diff] [blame] | 490 | void ieee80211_iter_chan_contexts_atomic( |
| 491 | struct ieee80211_hw *hw, |
| 492 | void (*iter)(struct ieee80211_hw *hw, |
| 493 | struct ieee80211_chanctx_conf *chanctx_conf, |
| 494 | void *data), |
| 495 | void *iter_data) |
| 496 | { |
| 497 | struct ieee80211_local *local = hw_to_local(hw); |
| 498 | struct ieee80211_chanctx *ctx; |
| 499 | |
| 500 | rcu_read_lock(); |
| 501 | list_for_each_entry_rcu(ctx, &local->chanctx_list, list) |
Johannes Berg | 8a61af6 | 2012-12-13 17:42:30 +0100 | [diff] [blame] | 502 | if (ctx->driver_present) |
| 503 | iter(hw, &ctx->conf, iter_data); |
Johannes Berg | 3448c00 | 2012-09-11 17:57:42 +0200 | [diff] [blame] | 504 | rcu_read_unlock(); |
| 505 | } |
| 506 | EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic); |