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