1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f444de05SJohannes Berg /*
3f444de05SJohannes Berg * mac80211 - channel management
4d0a9123eSJohannes Berg * Copyright 2020 - 2022 Intel Corporation
5f444de05SJohannes Berg */
6f444de05SJohannes Berg
70aaffa9bSJohannes Berg #include <linux/nl80211.h>
83448c005SJohannes Berg #include <linux/export.h>
94d76d21bSJohannes Berg #include <linux/rtnetlink.h>
103117bbdbSPaul Stewart #include <net/cfg80211.h>
11f444de05SJohannes Berg #include "ieee80211_i.h"
1235f2fce9SMichal Kazior #include "driver-ops.h"
1344b72ca8SIlan Peer #include "rate.h"
14f444de05SJohannes Berg
ieee80211_chanctx_num_assigned(struct ieee80211_local * local,struct ieee80211_chanctx * ctx)15c0166da9SMichal Kazior static int ieee80211_chanctx_num_assigned(struct ieee80211_local *local,
16c0166da9SMichal Kazior struct ieee80211_chanctx *ctx)
17c0166da9SMichal Kazior {
18b4f85443SJohannes Berg struct ieee80211_link_data *link;
19c0166da9SMichal Kazior int num = 0;
20c0166da9SMichal Kazior
21c0166da9SMichal Kazior lockdep_assert_held(&local->chanctx_mtx);
22c0166da9SMichal Kazior
23b4f85443SJohannes Berg list_for_each_entry(link, &ctx->assigned_links, assigned_chanctx_list)
24c0166da9SMichal Kazior num++;
25c0166da9SMichal Kazior
26c0166da9SMichal Kazior return num;
27c0166da9SMichal Kazior }
28c0166da9SMichal Kazior
ieee80211_chanctx_num_reserved(struct ieee80211_local * local,struct ieee80211_chanctx * ctx)29c0166da9SMichal Kazior static int ieee80211_chanctx_num_reserved(struct ieee80211_local *local,
30c0166da9SMichal Kazior struct ieee80211_chanctx *ctx)
31c0166da9SMichal Kazior {
32b4f85443SJohannes Berg struct ieee80211_link_data *link;
33c0166da9SMichal Kazior int num = 0;
34c0166da9SMichal Kazior
35c0166da9SMichal Kazior lockdep_assert_held(&local->chanctx_mtx);
36c0166da9SMichal Kazior
37b4f85443SJohannes Berg list_for_each_entry(link, &ctx->reserved_links, reserved_chanctx_list)
38c0166da9SMichal Kazior num++;
39c0166da9SMichal Kazior
40c0166da9SMichal Kazior return num;
41c0166da9SMichal Kazior }
42c0166da9SMichal Kazior
ieee80211_chanctx_refcount(struct ieee80211_local * local,struct ieee80211_chanctx * ctx)43c0166da9SMichal Kazior int ieee80211_chanctx_refcount(struct ieee80211_local *local,
44c0166da9SMichal Kazior struct ieee80211_chanctx *ctx)
45c0166da9SMichal Kazior {
46c0166da9SMichal Kazior return ieee80211_chanctx_num_assigned(local, ctx) +
47c0166da9SMichal Kazior ieee80211_chanctx_num_reserved(local, ctx);
48c0166da9SMichal Kazior }
49c0166da9SMichal Kazior
ieee80211_num_chanctx(struct ieee80211_local * local)50c2b90ad8SMichal Kazior static int ieee80211_num_chanctx(struct ieee80211_local *local)
51c2b90ad8SMichal Kazior {
52c2b90ad8SMichal Kazior struct ieee80211_chanctx *ctx;
53c2b90ad8SMichal Kazior int num = 0;
54c2b90ad8SMichal Kazior
55c2b90ad8SMichal Kazior lockdep_assert_held(&local->chanctx_mtx);
56c2b90ad8SMichal Kazior
57c2b90ad8SMichal Kazior list_for_each_entry(ctx, &local->chanctx_list, list)
58c2b90ad8SMichal Kazior num++;
59c2b90ad8SMichal Kazior
60c2b90ad8SMichal Kazior return num;
61c2b90ad8SMichal Kazior }
62c2b90ad8SMichal Kazior
ieee80211_can_create_new_chanctx(struct ieee80211_local * local)63c2b90ad8SMichal Kazior static bool ieee80211_can_create_new_chanctx(struct ieee80211_local *local)
64c2b90ad8SMichal Kazior {
65c2b90ad8SMichal Kazior lockdep_assert_held(&local->chanctx_mtx);
66c2b90ad8SMichal Kazior return ieee80211_num_chanctx(local) < ieee80211_max_num_channels(local);
67c2b90ad8SMichal Kazior }
68c2b90ad8SMichal Kazior
695bcae31dSMichal Kazior static struct ieee80211_chanctx *
ieee80211_link_get_chanctx(struct ieee80211_link_data * link)70d8675a63SJohannes Berg ieee80211_link_get_chanctx(struct ieee80211_link_data *link)
715bcae31dSMichal Kazior {
72d8675a63SJohannes Berg struct ieee80211_local *local __maybe_unused = link->sdata->local;
735bcae31dSMichal Kazior struct ieee80211_chanctx_conf *conf;
745bcae31dSMichal Kazior
75d8675a63SJohannes Berg conf = rcu_dereference_protected(link->conf->chanctx_conf,
765bcae31dSMichal Kazior lockdep_is_held(&local->chanctx_mtx));
775bcae31dSMichal Kazior if (!conf)
785bcae31dSMichal Kazior return NULL;
795bcae31dSMichal Kazior
805bcae31dSMichal Kazior return container_of(conf, struct ieee80211_chanctx, conf);
815bcae31dSMichal Kazior }
825bcae31dSMichal Kazior
830288157bSMichal Kazior static const struct cfg80211_chan_def *
ieee80211_chanctx_reserved_chandef(struct ieee80211_local * local,struct ieee80211_chanctx * ctx,const struct cfg80211_chan_def * compat)840288157bSMichal Kazior ieee80211_chanctx_reserved_chandef(struct ieee80211_local *local,
850288157bSMichal Kazior struct ieee80211_chanctx *ctx,
860288157bSMichal Kazior const struct cfg80211_chan_def *compat)
870288157bSMichal Kazior {
88b4f85443SJohannes Berg struct ieee80211_link_data *link;
890288157bSMichal Kazior
900288157bSMichal Kazior lockdep_assert_held(&local->chanctx_mtx);
910288157bSMichal Kazior
92b4f85443SJohannes Berg list_for_each_entry(link, &ctx->reserved_links,
930288157bSMichal Kazior reserved_chanctx_list) {
940288157bSMichal Kazior if (!compat)
95b4f85443SJohannes Berg compat = &link->reserved_chandef;
960288157bSMichal Kazior
97b4f85443SJohannes Berg compat = cfg80211_chandef_compatible(&link->reserved_chandef,
980288157bSMichal Kazior compat);
990288157bSMichal Kazior if (!compat)
1000288157bSMichal Kazior break;
1010288157bSMichal Kazior }
1020288157bSMichal Kazior
1030288157bSMichal Kazior return compat;
1040288157bSMichal Kazior }
1050288157bSMichal Kazior
10613f348a8SMichal Kazior static const struct cfg80211_chan_def *
ieee80211_chanctx_non_reserved_chandef(struct ieee80211_local * local,struct ieee80211_chanctx * ctx,const struct cfg80211_chan_def * compat)10713f348a8SMichal Kazior ieee80211_chanctx_non_reserved_chandef(struct ieee80211_local *local,
10813f348a8SMichal Kazior struct ieee80211_chanctx *ctx,
10913f348a8SMichal Kazior const struct cfg80211_chan_def *compat)
11013f348a8SMichal Kazior {
111b4f85443SJohannes Berg struct ieee80211_link_data *link;
11213f348a8SMichal Kazior
11313f348a8SMichal Kazior lockdep_assert_held(&local->chanctx_mtx);
11413f348a8SMichal Kazior
115b4f85443SJohannes Berg list_for_each_entry(link, &ctx->assigned_links,
11613f348a8SMichal Kazior assigned_chanctx_list) {
117d8675a63SJohannes Berg struct ieee80211_bss_conf *link_conf = link->conf;
118b4f85443SJohannes Berg
119b4f85443SJohannes Berg if (link->reserved_chanctx)
12013f348a8SMichal Kazior continue;
12113f348a8SMichal Kazior
12213f348a8SMichal Kazior if (!compat)
123b4f85443SJohannes Berg compat = &link_conf->chandef;
12413f348a8SMichal Kazior
12513f348a8SMichal Kazior compat = cfg80211_chandef_compatible(
126b4f85443SJohannes Berg &link_conf->chandef, compat);
12713f348a8SMichal Kazior if (!compat)
12813f348a8SMichal Kazior break;
12913f348a8SMichal Kazior }
13013f348a8SMichal Kazior
13113f348a8SMichal Kazior return compat;
13213f348a8SMichal Kazior }
13313f348a8SMichal Kazior
13413f348a8SMichal Kazior static const struct cfg80211_chan_def *
ieee80211_chanctx_combined_chandef(struct ieee80211_local * local,struct ieee80211_chanctx * ctx,const struct cfg80211_chan_def * compat)13513f348a8SMichal Kazior ieee80211_chanctx_combined_chandef(struct ieee80211_local *local,
13613f348a8SMichal Kazior struct ieee80211_chanctx *ctx,
13713f348a8SMichal Kazior const struct cfg80211_chan_def *compat)
13813f348a8SMichal Kazior {
13913f348a8SMichal Kazior lockdep_assert_held(&local->chanctx_mtx);
14013f348a8SMichal Kazior
14113f348a8SMichal Kazior compat = ieee80211_chanctx_reserved_chandef(local, ctx, compat);
14213f348a8SMichal Kazior if (!compat)
14313f348a8SMichal Kazior return NULL;
14413f348a8SMichal Kazior
14513f348a8SMichal Kazior compat = ieee80211_chanctx_non_reserved_chandef(local, ctx, compat);
14613f348a8SMichal Kazior if (!compat)
14713f348a8SMichal Kazior return NULL;
14813f348a8SMichal Kazior
14913f348a8SMichal Kazior return compat;
15013f348a8SMichal Kazior }
15113f348a8SMichal Kazior
15213f348a8SMichal Kazior static bool
ieee80211_chanctx_can_reserve_chandef(struct ieee80211_local * local,struct ieee80211_chanctx * ctx,const struct cfg80211_chan_def * def)15313f348a8SMichal Kazior ieee80211_chanctx_can_reserve_chandef(struct ieee80211_local *local,
15413f348a8SMichal Kazior struct ieee80211_chanctx *ctx,
15513f348a8SMichal Kazior const struct cfg80211_chan_def *def)
15613f348a8SMichal Kazior {
15713f348a8SMichal Kazior lockdep_assert_held(&local->chanctx_mtx);
15813f348a8SMichal Kazior
15913f348a8SMichal Kazior if (ieee80211_chanctx_combined_chandef(local, ctx, def))
16013f348a8SMichal Kazior return true;
16113f348a8SMichal Kazior
162b4f85443SJohannes Berg if (!list_empty(&ctx->reserved_links) &&
16313f348a8SMichal Kazior ieee80211_chanctx_reserved_chandef(local, ctx, def))
16413f348a8SMichal Kazior return true;
16513f348a8SMichal Kazior
16613f348a8SMichal Kazior return false;
16713f348a8SMichal Kazior }
16813f348a8SMichal Kazior
16913f348a8SMichal Kazior static struct ieee80211_chanctx *
ieee80211_find_reservation_chanctx(struct ieee80211_local * local,const struct cfg80211_chan_def * chandef,enum ieee80211_chanctx_mode mode)17013f348a8SMichal Kazior ieee80211_find_reservation_chanctx(struct ieee80211_local *local,
17113f348a8SMichal Kazior const struct cfg80211_chan_def *chandef,
17213f348a8SMichal Kazior enum ieee80211_chanctx_mode mode)
17313f348a8SMichal Kazior {
17413f348a8SMichal Kazior struct ieee80211_chanctx *ctx;
17513f348a8SMichal Kazior
17613f348a8SMichal Kazior lockdep_assert_held(&local->chanctx_mtx);
17713f348a8SMichal Kazior
17813f348a8SMichal Kazior if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
17913f348a8SMichal Kazior return NULL;
18013f348a8SMichal Kazior
18113f348a8SMichal Kazior list_for_each_entry(ctx, &local->chanctx_list, list) {
1825bcae31dSMichal Kazior if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
1835bcae31dSMichal Kazior continue;
1845bcae31dSMichal Kazior
18513f348a8SMichal Kazior if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
18613f348a8SMichal Kazior continue;
18713f348a8SMichal Kazior
18813f348a8SMichal Kazior if (!ieee80211_chanctx_can_reserve_chandef(local, ctx,
18913f348a8SMichal Kazior chandef))
19013f348a8SMichal Kazior continue;
19113f348a8SMichal Kazior
19213f348a8SMichal Kazior return ctx;
19313f348a8SMichal Kazior }
19413f348a8SMichal Kazior
19513f348a8SMichal Kazior return NULL;
19613f348a8SMichal Kazior }
19713f348a8SMichal Kazior
ieee80211_get_sta_bw(struct sta_info * sta,unsigned int link_id)198b4f85443SJohannes Berg static enum nl80211_chan_width ieee80211_get_sta_bw(struct sta_info *sta,
199b4f85443SJohannes Berg unsigned int link_id)
20021f659bfSEliad Peller {
201c71420dbSJohannes Berg enum ieee80211_sta_rx_bandwidth width;
202c71420dbSJohannes Berg struct link_sta_info *link_sta;
203c71420dbSJohannes Berg
204c71420dbSJohannes Berg link_sta = rcu_dereference(sta->link[link_id]);
205b4f85443SJohannes Berg
206b4f85443SJohannes Berg /* no effect if this STA has no presence on this link */
207c71420dbSJohannes Berg if (!link_sta)
208b4f85443SJohannes Berg return NL80211_CHAN_WIDTH_20_NOHT;
209bbf31e88SIlan Peer
210c71420dbSJohannes Berg width = ieee80211_sta_cap_rx_bw(link_sta);
211c71420dbSJohannes Berg
212bbf31e88SIlan Peer switch (width) {
21321f659bfSEliad Peller case IEEE80211_STA_RX_BW_20:
214c71420dbSJohannes Berg if (link_sta->pub->ht_cap.ht_supported)
21521f659bfSEliad Peller return NL80211_CHAN_WIDTH_20;
21621f659bfSEliad Peller else
21721f659bfSEliad Peller return NL80211_CHAN_WIDTH_20_NOHT;
21821f659bfSEliad Peller case IEEE80211_STA_RX_BW_40:
21921f659bfSEliad Peller return NL80211_CHAN_WIDTH_40;
22021f659bfSEliad Peller case IEEE80211_STA_RX_BW_80:
22121f659bfSEliad Peller return NL80211_CHAN_WIDTH_80;
22221f659bfSEliad Peller case IEEE80211_STA_RX_BW_160:
22321f659bfSEliad Peller /*
22421f659bfSEliad Peller * This applied for both 160 and 80+80. since we use
22521f659bfSEliad Peller * the returned value to consider degradation of
22621f659bfSEliad Peller * ctx->conf.min_def, we have to make sure to take
22721f659bfSEliad Peller * the bigger one (NL80211_CHAN_WIDTH_160).
22821f659bfSEliad Peller * Otherwise we might try degrading even when not
22921f659bfSEliad Peller * needed, as the max required sta_bw returned (80+80)
23021f659bfSEliad Peller * might be smaller than the configured bw (160).
23121f659bfSEliad Peller */
23221f659bfSEliad Peller return NL80211_CHAN_WIDTH_160;
2335dca295dSIlan Peer case IEEE80211_STA_RX_BW_320:
2345dca295dSIlan Peer return NL80211_CHAN_WIDTH_320;
23521f659bfSEliad Peller default:
23621f659bfSEliad Peller WARN_ON(1);
23721f659bfSEliad Peller return NL80211_CHAN_WIDTH_20;
23821f659bfSEliad Peller }
23921f659bfSEliad Peller }
24021f659bfSEliad Peller
24121f659bfSEliad Peller static enum nl80211_chan_width
ieee80211_get_max_required_bw(struct ieee80211_sub_if_data * sdata,unsigned int link_id)242b4f85443SJohannes Berg ieee80211_get_max_required_bw(struct ieee80211_sub_if_data *sdata,
243b4f85443SJohannes Berg unsigned int link_id)
24421f659bfSEliad Peller {
24521f659bfSEliad Peller enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
24621f659bfSEliad Peller struct sta_info *sta;
24721f659bfSEliad Peller
24821f659bfSEliad Peller list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
24921f659bfSEliad Peller if (sdata != sta->sdata &&
25021f659bfSEliad Peller !(sta->sdata->bss && sta->sdata->bss == sdata->bss))
25121f659bfSEliad Peller continue;
25221f659bfSEliad Peller
253b4f85443SJohannes Berg max_bw = max(max_bw, ieee80211_get_sta_bw(sta, link_id));
25421f659bfSEliad Peller }
25521f659bfSEliad Peller
25621f659bfSEliad Peller return max_bw;
25721f659bfSEliad Peller }
25821f659bfSEliad Peller
25921f659bfSEliad Peller static enum nl80211_chan_width
ieee80211_get_chanctx_vif_max_required_bw(struct ieee80211_sub_if_data * sdata,struct ieee80211_chanctx * ctx,struct ieee80211_link_data * rsvd_for)260b4f85443SJohannes Berg ieee80211_get_chanctx_vif_max_required_bw(struct ieee80211_sub_if_data *sdata,
261b72a455aSJohannes Berg struct ieee80211_chanctx *ctx,
262b72a455aSJohannes Berg struct ieee80211_link_data *rsvd_for)
263b4f85443SJohannes Berg {
264b4f85443SJohannes Berg enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
265b4f85443SJohannes Berg struct ieee80211_vif *vif = &sdata->vif;
266b4f85443SJohannes Berg int link_id;
267b4f85443SJohannes Berg
268d8675a63SJohannes Berg rcu_read_lock();
269b4f85443SJohannes Berg for (link_id = 0; link_id < ARRAY_SIZE(sdata->link); link_id++) {
270b4f85443SJohannes Berg enum nl80211_chan_width width = NL80211_CHAN_WIDTH_20_NOHT;
271b72a455aSJohannes Berg struct ieee80211_link_data *link =
272b72a455aSJohannes Berg rcu_dereference(sdata->link[link_id]);
273b4f85443SJohannes Berg
274b72a455aSJohannes Berg if (!link)
275b4f85443SJohannes Berg continue;
276b4f85443SJohannes Berg
277b72a455aSJohannes Berg if (link != rsvd_for &&
278b72a455aSJohannes Berg rcu_access_pointer(link->conf->chanctx_conf) != &ctx->conf)
279b4f85443SJohannes Berg continue;
280b4f85443SJohannes Berg
281b4f85443SJohannes Berg switch (vif->type) {
282b4f85443SJohannes Berg case NL80211_IFTYPE_AP:
283b4f85443SJohannes Berg case NL80211_IFTYPE_AP_VLAN:
284b4f85443SJohannes Berg width = ieee80211_get_max_required_bw(sdata, link_id);
285b4f85443SJohannes Berg break;
286b4f85443SJohannes Berg case NL80211_IFTYPE_STATION:
287b4f85443SJohannes Berg /*
288b4f85443SJohannes Berg * The ap's sta->bandwidth is not set yet at this
289b4f85443SJohannes Berg * point, so take the width from the chandef, but
290b4f85443SJohannes Berg * account also for TDLS peers
291b4f85443SJohannes Berg */
292b72a455aSJohannes Berg width = max(link->conf->chandef.width,
293b4f85443SJohannes Berg ieee80211_get_max_required_bw(sdata, link_id));
294b4f85443SJohannes Berg break;
295b4f85443SJohannes Berg case NL80211_IFTYPE_P2P_DEVICE:
296b4f85443SJohannes Berg case NL80211_IFTYPE_NAN:
297b4f85443SJohannes Berg continue;
298b4f85443SJohannes Berg case NL80211_IFTYPE_ADHOC:
299b4f85443SJohannes Berg case NL80211_IFTYPE_MESH_POINT:
300b4f85443SJohannes Berg case NL80211_IFTYPE_OCB:
301b72a455aSJohannes Berg width = link->conf->chandef.width;
302b4f85443SJohannes Berg break;
303b4f85443SJohannes Berg case NL80211_IFTYPE_WDS:
304b4f85443SJohannes Berg case NL80211_IFTYPE_UNSPECIFIED:
305b4f85443SJohannes Berg case NUM_NL80211_IFTYPES:
306b4f85443SJohannes Berg case NL80211_IFTYPE_MONITOR:
307b4f85443SJohannes Berg case NL80211_IFTYPE_P2P_CLIENT:
308b4f85443SJohannes Berg case NL80211_IFTYPE_P2P_GO:
309b4f85443SJohannes Berg WARN_ON_ONCE(1);
310b4f85443SJohannes Berg }
311b4f85443SJohannes Berg
312b4f85443SJohannes Berg max_bw = max(max_bw, width);
313b4f85443SJohannes Berg }
314d8675a63SJohannes Berg rcu_read_unlock();
315b4f85443SJohannes Berg
316b4f85443SJohannes Berg return max_bw;
317b4f85443SJohannes Berg }
318b4f85443SJohannes Berg
319b4f85443SJohannes Berg static enum nl80211_chan_width
ieee80211_get_chanctx_max_required_bw(struct ieee80211_local * local,struct ieee80211_chanctx * ctx,struct ieee80211_link_data * rsvd_for)32021f659bfSEliad Peller ieee80211_get_chanctx_max_required_bw(struct ieee80211_local *local,
321b72a455aSJohannes Berg struct ieee80211_chanctx *ctx,
322b72a455aSJohannes Berg struct ieee80211_link_data *rsvd_for)
32321f659bfSEliad Peller {
32421f659bfSEliad Peller struct ieee80211_sub_if_data *sdata;
32521f659bfSEliad Peller enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
32621f659bfSEliad Peller
32721f659bfSEliad Peller rcu_read_lock();
32821f659bfSEliad Peller list_for_each_entry_rcu(sdata, &local->interfaces, list) {
329b4f85443SJohannes Berg enum nl80211_chan_width width;
33021f659bfSEliad Peller
33121f659bfSEliad Peller if (!ieee80211_sdata_running(sdata))
33221f659bfSEliad Peller continue;
33321f659bfSEliad Peller
334b72a455aSJohannes Berg width = ieee80211_get_chanctx_vif_max_required_bw(sdata, ctx,
335b72a455aSJohannes Berg rsvd_for);
33621f659bfSEliad Peller
33721f659bfSEliad Peller max_bw = max(max_bw, width);
33821f659bfSEliad Peller }
3391c37a72cSEliad Peller
3401c37a72cSEliad Peller /* use the configured bandwidth in case of monitor interface */
3411c37a72cSEliad Peller sdata = rcu_dereference(local->monitor_sdata);
342b4f85443SJohannes Berg if (sdata &&
343b72a455aSJohannes Berg rcu_access_pointer(sdata->vif.bss_conf.chanctx_conf) == &ctx->conf)
344b72a455aSJohannes Berg max_bw = max(max_bw, ctx->conf.def.width);
3451c37a72cSEliad Peller
34621f659bfSEliad Peller rcu_read_unlock();
34721f659bfSEliad Peller
34821f659bfSEliad Peller return max_bw;
34921f659bfSEliad Peller }
35021f659bfSEliad Peller
35121f659bfSEliad Peller /*
35221f659bfSEliad Peller * recalc the min required chan width of the channel context, which is
35321f659bfSEliad Peller * the max of min required widths of all the interfaces bound to this
35421f659bfSEliad Peller * channel context.
35521f659bfSEliad Peller */
356b72a455aSJohannes Berg static u32
_ieee80211_recalc_chanctx_min_def(struct ieee80211_local * local,struct ieee80211_chanctx * ctx,struct ieee80211_link_data * rsvd_for)357b72a455aSJohannes Berg _ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local,
358b72a455aSJohannes Berg struct ieee80211_chanctx *ctx,
359b72a455aSJohannes Berg struct ieee80211_link_data *rsvd_for)
36021f659bfSEliad Peller {
36121f659bfSEliad Peller enum nl80211_chan_width max_bw;
36221f659bfSEliad Peller struct cfg80211_chan_def min_def;
36321f659bfSEliad Peller
36421f659bfSEliad Peller lockdep_assert_held(&local->chanctx_mtx);
36521f659bfSEliad Peller
366df78a0c0SThomas Pedersen /* don't optimize non-20MHz based and radar_enabled confs */
36721f659bfSEliad Peller if (ctx->conf.def.width == NL80211_CHAN_WIDTH_5 ||
36821f659bfSEliad Peller ctx->conf.def.width == NL80211_CHAN_WIDTH_10 ||
369df78a0c0SThomas Pedersen ctx->conf.def.width == NL80211_CHAN_WIDTH_1 ||
370df78a0c0SThomas Pedersen ctx->conf.def.width == NL80211_CHAN_WIDTH_2 ||
371df78a0c0SThomas Pedersen ctx->conf.def.width == NL80211_CHAN_WIDTH_4 ||
372df78a0c0SThomas Pedersen ctx->conf.def.width == NL80211_CHAN_WIDTH_8 ||
373df78a0c0SThomas Pedersen ctx->conf.def.width == NL80211_CHAN_WIDTH_16 ||
37421f659bfSEliad Peller ctx->conf.radar_enabled) {
37521f659bfSEliad Peller ctx->conf.min_def = ctx->conf.def;
376d6c37509SMordechay Goodstein return 0;
37721f659bfSEliad Peller }
37821f659bfSEliad Peller
379b72a455aSJohannes Berg max_bw = ieee80211_get_chanctx_max_required_bw(local, ctx, rsvd_for);
38021f659bfSEliad Peller
38121f659bfSEliad Peller /* downgrade chandef up to max_bw */
38221f659bfSEliad Peller min_def = ctx->conf.def;
38321f659bfSEliad Peller while (min_def.width > max_bw)
38421f659bfSEliad Peller ieee80211_chandef_downgrade(&min_def);
38521f659bfSEliad Peller
38621f659bfSEliad Peller if (cfg80211_chandef_identical(&ctx->conf.min_def, &min_def))
387d6c37509SMordechay Goodstein return 0;
38821f659bfSEliad Peller
38921f659bfSEliad Peller ctx->conf.min_def = min_def;
39021f659bfSEliad Peller if (!ctx->driver_present)
391d6c37509SMordechay Goodstein return 0;
39221f659bfSEliad Peller
393d6c37509SMordechay Goodstein return IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
39421f659bfSEliad Peller }
39521f659bfSEliad Peller
396d6c37509SMordechay Goodstein /* calling this function is assuming that station vif is updated to
397b4f85443SJohannes Berg * lates changes by calling ieee80211_link_update_chandef
398d6c37509SMordechay Goodstein */
ieee80211_chan_bw_change(struct ieee80211_local * local,struct ieee80211_chanctx * ctx,bool narrowed)39944b72ca8SIlan Peer static void ieee80211_chan_bw_change(struct ieee80211_local *local,
400d6c37509SMordechay Goodstein struct ieee80211_chanctx *ctx,
401d6c37509SMordechay Goodstein bool narrowed)
40244b72ca8SIlan Peer {
40344b72ca8SIlan Peer struct sta_info *sta;
40444b72ca8SIlan Peer struct ieee80211_supported_band *sband =
40544b72ca8SIlan Peer local->hw.wiphy->bands[ctx->conf.def.chan->band];
40644b72ca8SIlan Peer
40744b72ca8SIlan Peer rcu_read_lock();
40844b72ca8SIlan Peer list_for_each_entry_rcu(sta, &local->sta_list,
40944b72ca8SIlan Peer list) {
410b4f85443SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata;
41144b72ca8SIlan Peer enum ieee80211_sta_rx_bandwidth new_sta_bw;
412b4f85443SJohannes Berg unsigned int link_id;
41344b72ca8SIlan Peer
41444b72ca8SIlan Peer if (!ieee80211_sdata_running(sta->sdata))
41544b72ca8SIlan Peer continue;
41644b72ca8SIlan Peer
417b4f85443SJohannes Berg for (link_id = 0; link_id < ARRAY_SIZE(sta->sdata->link); link_id++) {
418b4f85443SJohannes Berg struct ieee80211_bss_conf *link_conf =
419d8675a63SJohannes Berg rcu_dereference(sdata->vif.link_conf[link_id]);
420c71420dbSJohannes Berg struct link_sta_info *link_sta;
421b4f85443SJohannes Berg
422b4f85443SJohannes Berg if (!link_conf)
42344b72ca8SIlan Peer continue;
42444b72ca8SIlan Peer
425b4f85443SJohannes Berg if (rcu_access_pointer(link_conf->chanctx_conf) != &ctx->conf)
426b4f85443SJohannes Berg continue;
427b4f85443SJohannes Berg
428c71420dbSJohannes Berg link_sta = rcu_dereference(sta->link[link_id]);
429c71420dbSJohannes Berg if (!link_sta)
430c71420dbSJohannes Berg continue;
431c71420dbSJohannes Berg
432c71420dbSJohannes Berg new_sta_bw = ieee80211_sta_cur_vht_bw(link_sta);
433d6c37509SMordechay Goodstein
434d6c37509SMordechay Goodstein /* nothing change */
435c71420dbSJohannes Berg if (new_sta_bw == link_sta->pub->bandwidth)
43644b72ca8SIlan Peer continue;
43744b72ca8SIlan Peer
438d6c37509SMordechay Goodstein /* vif changed to narrow BW and narrow BW for station wasn't
439d6c37509SMordechay Goodstein * requested or vise versa */
440c71420dbSJohannes Berg if ((new_sta_bw < link_sta->pub->bandwidth) == !narrowed)
441d6c37509SMordechay Goodstein continue;
442d6c37509SMordechay Goodstein
443c71420dbSJohannes Berg link_sta->pub->bandwidth = new_sta_bw;
444b4f85443SJohannes Berg rate_control_rate_update(local, sband, sta, link_id,
44544b72ca8SIlan Peer IEEE80211_RC_BW_CHANGED);
44644b72ca8SIlan Peer }
447b4f85443SJohannes Berg }
44844b72ca8SIlan Peer rcu_read_unlock();
44944b72ca8SIlan Peer }
45044b72ca8SIlan Peer
451d6c37509SMordechay Goodstein /*
452d6c37509SMordechay Goodstein * recalc the min required chan width of the channel context, which is
453d6c37509SMordechay Goodstein * the max of min required widths of all the interfaces bound to this
454d6c37509SMordechay Goodstein * channel context.
455d6c37509SMordechay Goodstein */
ieee80211_recalc_chanctx_min_def(struct ieee80211_local * local,struct ieee80211_chanctx * ctx,struct ieee80211_link_data * rsvd_for)456d6c37509SMordechay Goodstein void ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local,
457b72a455aSJohannes Berg struct ieee80211_chanctx *ctx,
458b72a455aSJohannes Berg struct ieee80211_link_data *rsvd_for)
459e89a96f5SMichal Kazior {
460b72a455aSJohannes Berg u32 changed = _ieee80211_recalc_chanctx_min_def(local, ctx, rsvd_for);
46144b72ca8SIlan Peer
462d6c37509SMordechay Goodstein if (!changed)
463e89a96f5SMichal Kazior return;
464d6c37509SMordechay Goodstein
465d6c37509SMordechay Goodstein /* check is BW narrowed */
466d6c37509SMordechay Goodstein ieee80211_chan_bw_change(local, ctx, true);
467d6c37509SMordechay Goodstein
468d6c37509SMordechay Goodstein drv_change_chanctx(local, ctx, changed);
469d6c37509SMordechay Goodstein
470d6c37509SMordechay Goodstein /* check is BW wider */
471d6c37509SMordechay Goodstein ieee80211_chan_bw_change(local, ctx, false);
472aa507a7bSArik Nemtsov }
473e89a96f5SMichal Kazior
_ieee80211_change_chanctx(struct ieee80211_local * local,struct ieee80211_chanctx * ctx,struct ieee80211_chanctx * old_ctx,const struct cfg80211_chan_def * chandef,struct ieee80211_link_data * rsvd_for)474b72a455aSJohannes Berg static void _ieee80211_change_chanctx(struct ieee80211_local *local,
475d6c37509SMordechay Goodstein struct ieee80211_chanctx *ctx,
476d6c37509SMordechay Goodstein struct ieee80211_chanctx *old_ctx,
477b72a455aSJohannes Berg const struct cfg80211_chan_def *chandef,
478b72a455aSJohannes Berg struct ieee80211_link_data *rsvd_for)
479d6c37509SMordechay Goodstein {
480d6c37509SMordechay Goodstein u32 changed;
48144b72ca8SIlan Peer
4825dca295dSIlan Peer /* expected to handle only 20/40/80/160/320 channel widths */
48344b72ca8SIlan Peer switch (chandef->width) {
48444b72ca8SIlan Peer case NL80211_CHAN_WIDTH_20_NOHT:
48544b72ca8SIlan Peer case NL80211_CHAN_WIDTH_20:
48644b72ca8SIlan Peer case NL80211_CHAN_WIDTH_40:
48744b72ca8SIlan Peer case NL80211_CHAN_WIDTH_80:
48844b72ca8SIlan Peer case NL80211_CHAN_WIDTH_80P80:
48944b72ca8SIlan Peer case NL80211_CHAN_WIDTH_160:
4905dca295dSIlan Peer case NL80211_CHAN_WIDTH_320:
49144b72ca8SIlan Peer break;
49244b72ca8SIlan Peer default:
49344b72ca8SIlan Peer WARN_ON(1);
49444b72ca8SIlan Peer }
49544b72ca8SIlan Peer
496d6c37509SMordechay Goodstein /* Check maybe BW narrowed - we do this _before_ calling recalc_chanctx_min_def
497d6c37509SMordechay Goodstein * due to maybe not returning from it, e.g in case new context was added
498d6c37509SMordechay Goodstein * first time with all parameters up to date.
499d6c37509SMordechay Goodstein */
500d6c37509SMordechay Goodstein ieee80211_chan_bw_change(local, old_ctx, true);
50144b72ca8SIlan Peer
502d6c37509SMordechay Goodstein if (cfg80211_chandef_identical(&ctx->conf.def, chandef)) {
503b72a455aSJohannes Berg ieee80211_recalc_chanctx_min_def(local, ctx, rsvd_for);
504d6c37509SMordechay Goodstein return;
505d6c37509SMordechay Goodstein }
506d6c37509SMordechay Goodstein
507d6c37509SMordechay Goodstein WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef));
508d6c37509SMordechay Goodstein
509d6c37509SMordechay Goodstein ctx->conf.def = *chandef;
510d6c37509SMordechay Goodstein
511d6c37509SMordechay Goodstein /* check if min chanctx also changed */
512d6c37509SMordechay Goodstein changed = IEEE80211_CHANCTX_CHANGE_WIDTH |
513b72a455aSJohannes Berg _ieee80211_recalc_chanctx_min_def(local, ctx, rsvd_for);
514d6c37509SMordechay Goodstein drv_change_chanctx(local, ctx, changed);
51555de908aSJohannes Berg
51655de908aSJohannes Berg if (!local->use_chanctx) {
517675a0b04SKarl Beldan local->_oper_chandef = *chandef;
51855de908aSJohannes Berg ieee80211_hw_config(local, 0);
51955de908aSJohannes Berg }
52044b72ca8SIlan Peer
521d6c37509SMordechay Goodstein /* check is BW wider */
522d6c37509SMordechay Goodstein ieee80211_chan_bw_change(local, old_ctx, false);
5230aaffa9bSJohannes Berg }
524d01a1e65SMichal Kazior
ieee80211_change_chanctx(struct ieee80211_local * local,struct ieee80211_chanctx * ctx,struct ieee80211_chanctx * old_ctx,const struct cfg80211_chan_def * chandef)525b72a455aSJohannes Berg static void ieee80211_change_chanctx(struct ieee80211_local *local,
526b72a455aSJohannes Berg struct ieee80211_chanctx *ctx,
527b72a455aSJohannes Berg struct ieee80211_chanctx *old_ctx,
528b72a455aSJohannes Berg const struct cfg80211_chan_def *chandef)
529b72a455aSJohannes Berg {
530b72a455aSJohannes Berg _ieee80211_change_chanctx(local, ctx, old_ctx, chandef, NULL);
531b72a455aSJohannes Berg }
532b72a455aSJohannes Berg
533d01a1e65SMichal Kazior static struct ieee80211_chanctx *
ieee80211_find_chanctx(struct ieee80211_local * local,const struct cfg80211_chan_def * chandef,enum ieee80211_chanctx_mode mode)534d01a1e65SMichal Kazior ieee80211_find_chanctx(struct ieee80211_local *local,
5354bf88530SJohannes Berg const struct cfg80211_chan_def *chandef,
536d01a1e65SMichal Kazior enum ieee80211_chanctx_mode mode)
537d01a1e65SMichal Kazior {
538d01a1e65SMichal Kazior struct ieee80211_chanctx *ctx;
539d01a1e65SMichal Kazior
540d01a1e65SMichal Kazior lockdep_assert_held(&local->chanctx_mtx);
541d01a1e65SMichal Kazior
542d01a1e65SMichal Kazior if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
543d01a1e65SMichal Kazior return NULL;
544d01a1e65SMichal Kazior
545d01a1e65SMichal Kazior list_for_each_entry(ctx, &local->chanctx_list, list) {
5464bf88530SJohannes Berg const struct cfg80211_chan_def *compat;
547e89a96f5SMichal Kazior
5485bcae31dSMichal Kazior if (ctx->replace_state != IEEE80211_CHANCTX_REPLACE_NONE)
5495bcae31dSMichal Kazior continue;
5505bcae31dSMichal Kazior
551d01a1e65SMichal Kazior if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
552d01a1e65SMichal Kazior continue;
5534bf88530SJohannes Berg
5544bf88530SJohannes Berg compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef);
5554bf88530SJohannes Berg if (!compat)
556d01a1e65SMichal Kazior continue;
557d01a1e65SMichal Kazior
5580288157bSMichal Kazior compat = ieee80211_chanctx_reserved_chandef(local, ctx,
5590288157bSMichal Kazior compat);
5600288157bSMichal Kazior if (!compat)
5610288157bSMichal Kazior continue;
5620288157bSMichal Kazior
563d6c37509SMordechay Goodstein ieee80211_change_chanctx(local, ctx, ctx, compat);
564e89a96f5SMichal Kazior
565d01a1e65SMichal Kazior return ctx;
566d01a1e65SMichal Kazior }
567d01a1e65SMichal Kazior
568d01a1e65SMichal Kazior return NULL;
569d01a1e65SMichal Kazior }
570d01a1e65SMichal Kazior
ieee80211_is_radar_required(struct ieee80211_local * local)5715cbc95a7SEliad Peller bool ieee80211_is_radar_required(struct ieee80211_local *local)
572e4746851SSimon Wunderlich {
573e4746851SSimon Wunderlich struct ieee80211_sub_if_data *sdata;
574e4746851SSimon Wunderlich
575cc901de1SMichal Kazior lockdep_assert_held(&local->mtx);
576cc901de1SMichal Kazior
577e4746851SSimon Wunderlich rcu_read_lock();
578e4746851SSimon Wunderlich list_for_each_entry_rcu(sdata, &local->interfaces, list) {
579b4f85443SJohannes Berg unsigned int link_id;
580b4f85443SJohannes Berg
581b4f85443SJohannes Berg for (link_id = 0; link_id < ARRAY_SIZE(sdata->link); link_id++) {
582d8675a63SJohannes Berg struct ieee80211_link_data *link;
583d8675a63SJohannes Berg
584d8675a63SJohannes Berg link = rcu_dereference(sdata->link[link_id]);
585d8675a63SJohannes Berg
586d8675a63SJohannes Berg if (link && link->radar_required) {
587e4746851SSimon Wunderlich rcu_read_unlock();
588e4746851SSimon Wunderlich return true;
589e4746851SSimon Wunderlich }
590e4746851SSimon Wunderlich }
591b4f85443SJohannes Berg }
592e4746851SSimon Wunderlich rcu_read_unlock();
593e4746851SSimon Wunderlich
594e4746851SSimon Wunderlich return false;
595e4746851SSimon Wunderlich }
596e4746851SSimon Wunderlich
597e7f2337aSEliad Peller static bool
ieee80211_chanctx_radar_required(struct ieee80211_local * local,struct ieee80211_chanctx * ctx)598e7f2337aSEliad Peller ieee80211_chanctx_radar_required(struct ieee80211_local *local,
599e7f2337aSEliad Peller struct ieee80211_chanctx *ctx)
600e7f2337aSEliad Peller {
601e7f2337aSEliad Peller struct ieee80211_chanctx_conf *conf = &ctx->conf;
602e7f2337aSEliad Peller struct ieee80211_sub_if_data *sdata;
603e7f2337aSEliad Peller bool required = false;
604e7f2337aSEliad Peller
605e7f2337aSEliad Peller lockdep_assert_held(&local->chanctx_mtx);
606e7f2337aSEliad Peller lockdep_assert_held(&local->mtx);
607e7f2337aSEliad Peller
608e7f2337aSEliad Peller rcu_read_lock();
609e7f2337aSEliad Peller list_for_each_entry_rcu(sdata, &local->interfaces, list) {
610b4f85443SJohannes Berg unsigned int link_id;
611b4f85443SJohannes Berg
612e7f2337aSEliad Peller if (!ieee80211_sdata_running(sdata))
613e7f2337aSEliad Peller continue;
614b4f85443SJohannes Berg for (link_id = 0; link_id < ARRAY_SIZE(sdata->link); link_id++) {
615d8675a63SJohannes Berg struct ieee80211_link_data *link;
616b4f85443SJohannes Berg
617d8675a63SJohannes Berg link = rcu_dereference(sdata->link[link_id]);
618d8675a63SJohannes Berg if (!link)
619e7f2337aSEliad Peller continue;
620e7f2337aSEliad Peller
621d8675a63SJohannes Berg if (rcu_access_pointer(link->conf->chanctx_conf) != conf)
622b4f85443SJohannes Berg continue;
623d8675a63SJohannes Berg if (!link->radar_required)
624b4f85443SJohannes Berg continue;
625e7f2337aSEliad Peller required = true;
626e7f2337aSEliad Peller break;
627e7f2337aSEliad Peller }
628b4f85443SJohannes Berg
629b4f85443SJohannes Berg if (required)
630b4f85443SJohannes Berg break;
631b4f85443SJohannes Berg }
632e7f2337aSEliad Peller rcu_read_unlock();
633e7f2337aSEliad Peller
634e7f2337aSEliad Peller return required;
635e7f2337aSEliad Peller }
636e7f2337aSEliad Peller
637d01a1e65SMichal Kazior static struct ieee80211_chanctx *
ieee80211_alloc_chanctx(struct ieee80211_local * local,const struct cfg80211_chan_def * chandef,enum ieee80211_chanctx_mode mode)638ed68ebcaSMichal Kazior ieee80211_alloc_chanctx(struct ieee80211_local *local,
6394bf88530SJohannes Berg const struct cfg80211_chan_def *chandef,
640d01a1e65SMichal Kazior enum ieee80211_chanctx_mode mode)
641d01a1e65SMichal Kazior {
642d01a1e65SMichal Kazior struct ieee80211_chanctx *ctx;
643d01a1e65SMichal Kazior
644d01a1e65SMichal Kazior lockdep_assert_held(&local->chanctx_mtx);
645d01a1e65SMichal Kazior
646d01a1e65SMichal Kazior ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
647d01a1e65SMichal Kazior if (!ctx)
648ed68ebcaSMichal Kazior return NULL;
649d01a1e65SMichal Kazior
650b4f85443SJohannes Berg INIT_LIST_HEAD(&ctx->assigned_links);
651b4f85443SJohannes Berg INIT_LIST_HEAD(&ctx->reserved_links);
6524bf88530SJohannes Berg ctx->conf.def = *chandef;
65304ecd257SJohannes Berg ctx->conf.rx_chains_static = 1;
65404ecd257SJohannes Berg ctx->conf.rx_chains_dynamic = 1;
655d01a1e65SMichal Kazior ctx->mode = mode;
656e7f2337aSEliad Peller ctx->conf.radar_enabled = false;
657b72a455aSJohannes Berg _ieee80211_recalc_chanctx_min_def(local, ctx, NULL);
658ed68ebcaSMichal Kazior
659ed68ebcaSMichal Kazior return ctx;
660ed68ebcaSMichal Kazior }
661ed68ebcaSMichal Kazior
ieee80211_add_chanctx(struct ieee80211_local * local,struct ieee80211_chanctx * ctx)662ed68ebcaSMichal Kazior static int ieee80211_add_chanctx(struct ieee80211_local *local,
663ed68ebcaSMichal Kazior struct ieee80211_chanctx *ctx)
664ed68ebcaSMichal Kazior {
665ed68ebcaSMichal Kazior u32 changed;
666ed68ebcaSMichal Kazior int err;
667ed68ebcaSMichal Kazior
668ed68ebcaSMichal Kazior lockdep_assert_held(&local->mtx);
669ed68ebcaSMichal Kazior lockdep_assert_held(&local->chanctx_mtx);
670ed68ebcaSMichal Kazior
671e4746851SSimon Wunderlich if (!local->use_chanctx)
672e4746851SSimon Wunderlich local->hw.conf.radar_enabled = ctx->conf.radar_enabled;
673d01a1e65SMichal Kazior
674382a103bSJohannes Berg /* turn idle off *before* setting channel -- some drivers need that */
675382a103bSJohannes Berg changed = ieee80211_idle_off(local);
676382a103bSJohannes Berg if (changed)
677382a103bSJohannes Berg ieee80211_hw_config(local, changed);
678382a103bSJohannes Berg
67955de908aSJohannes Berg if (!local->use_chanctx) {
680ed68ebcaSMichal Kazior local->_oper_chandef = ctx->conf.def;
6819b4816f5SMichal Kazior ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
68255de908aSJohannes Berg } else {
68335f2fce9SMichal Kazior err = drv_add_chanctx(local, ctx);
68435f2fce9SMichal Kazior if (err) {
685382a103bSJohannes Berg ieee80211_recalc_idle(local);
686ed68ebcaSMichal Kazior return err;
687ed68ebcaSMichal Kazior }
688ed68ebcaSMichal Kazior }
689ed68ebcaSMichal Kazior
690ed68ebcaSMichal Kazior return 0;
691ed68ebcaSMichal Kazior }
692ed68ebcaSMichal Kazior
693ed68ebcaSMichal Kazior static struct ieee80211_chanctx *
ieee80211_new_chanctx(struct ieee80211_local * local,const struct cfg80211_chan_def * chandef,enum ieee80211_chanctx_mode mode)694ed68ebcaSMichal Kazior ieee80211_new_chanctx(struct ieee80211_local *local,
695ed68ebcaSMichal Kazior const struct cfg80211_chan_def *chandef,
696ed68ebcaSMichal Kazior enum ieee80211_chanctx_mode mode)
697ed68ebcaSMichal Kazior {
698ed68ebcaSMichal Kazior struct ieee80211_chanctx *ctx;
699ed68ebcaSMichal Kazior int err;
700ed68ebcaSMichal Kazior
701ed68ebcaSMichal Kazior lockdep_assert_held(&local->mtx);
702ed68ebcaSMichal Kazior lockdep_assert_held(&local->chanctx_mtx);
703ed68ebcaSMichal Kazior
704ed68ebcaSMichal Kazior ctx = ieee80211_alloc_chanctx(local, chandef, mode);
705ed68ebcaSMichal Kazior if (!ctx)
706ed68ebcaSMichal Kazior return ERR_PTR(-ENOMEM);
707ed68ebcaSMichal Kazior
708ed68ebcaSMichal Kazior err = ieee80211_add_chanctx(local, ctx);
709ed68ebcaSMichal Kazior if (err) {
710ed68ebcaSMichal Kazior kfree(ctx);
71134a3740dSJohannes Berg return ERR_PTR(err);
71235f2fce9SMichal Kazior }
71335f2fce9SMichal Kazior
7143448c005SJohannes Berg list_add_rcu(&ctx->list, &local->chanctx_list);
715d01a1e65SMichal Kazior return ctx;
716d01a1e65SMichal Kazior }
717d01a1e65SMichal Kazior
ieee80211_del_chanctx(struct ieee80211_local * local,struct ieee80211_chanctx * ctx)7181f0d54cdSMichal Kazior static void ieee80211_del_chanctx(struct ieee80211_local *local,
719d01a1e65SMichal Kazior struct ieee80211_chanctx *ctx)
720d01a1e65SMichal Kazior {
721d01a1e65SMichal Kazior lockdep_assert_held(&local->chanctx_mtx);
722d01a1e65SMichal Kazior
72355de908aSJohannes Berg if (!local->use_chanctx) {
724675a0b04SKarl Beldan struct cfg80211_chan_def *chandef = &local->_oper_chandef;
7255e480774SThomas Pedersen /* S1G doesn't have 20MHz, so get the correct width for the
7265e480774SThomas Pedersen * current channel.
7275e480774SThomas Pedersen */
7285e480774SThomas Pedersen if (chandef->chan->band == NL80211_BAND_S1GHZ)
7295e480774SThomas Pedersen chandef->width =
7305e480774SThomas Pedersen ieee80211_s1g_channel_width(chandef->chan);
7315e480774SThomas Pedersen else
732675a0b04SKarl Beldan chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
733675a0b04SKarl Beldan chandef->center_freq1 = chandef->chan->center_freq;
734b6011960SThomas Pedersen chandef->freq1_offset = chandef->chan->freq_offset;
735675a0b04SKarl Beldan chandef->center_freq2 = 0;
736e4746851SSimon Wunderlich
737e4746851SSimon Wunderlich /* NOTE: Disabling radar is only valid here for
738e4746851SSimon Wunderlich * single channel context. To be sure, check it ...
739e4746851SSimon Wunderlich */
7401f0d54cdSMichal Kazior WARN_ON(local->hw.conf.radar_enabled &&
7411f0d54cdSMichal Kazior !list_empty(&local->chanctx_list));
7421f0d54cdSMichal Kazior
743e4746851SSimon Wunderlich local->hw.conf.radar_enabled = false;
744e4746851SSimon Wunderlich
7459b4816f5SMichal Kazior ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
74655de908aSJohannes Berg } else {
74735f2fce9SMichal Kazior drv_remove_chanctx(local, ctx);
74855de908aSJohannes Berg }
74935f2fce9SMichal Kazior
750fd0f979aSJohannes Berg ieee80211_recalc_idle(local);
751d01a1e65SMichal Kazior }
752d01a1e65SMichal Kazior
ieee80211_free_chanctx(struct ieee80211_local * local,struct ieee80211_chanctx * ctx)7531f0d54cdSMichal Kazior static void ieee80211_free_chanctx(struct ieee80211_local *local,
754d01a1e65SMichal Kazior struct ieee80211_chanctx *ctx)
755d01a1e65SMichal Kazior {
756d01a1e65SMichal Kazior lockdep_assert_held(&local->chanctx_mtx);
757d01a1e65SMichal Kazior
758c0166da9SMichal Kazior WARN_ON_ONCE(ieee80211_chanctx_refcount(local, ctx) != 0);
75935f2fce9SMichal Kazior
7601f0d54cdSMichal Kazior list_del_rcu(&ctx->list);
7611f0d54cdSMichal Kazior ieee80211_del_chanctx(local, ctx);
7621f0d54cdSMichal Kazior kfree_rcu(ctx, rcu_head);
763d01a1e65SMichal Kazior }
764d01a1e65SMichal Kazior
ieee80211_recalc_chanctx_chantype(struct ieee80211_local * local,struct ieee80211_chanctx * ctx)7650fabfaafSArik Nemtsov void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
766e89a96f5SMichal Kazior struct ieee80211_chanctx *ctx)
767e89a96f5SMichal Kazior {
768e89a96f5SMichal Kazior struct ieee80211_chanctx_conf *conf = &ctx->conf;
769e89a96f5SMichal Kazior struct ieee80211_sub_if_data *sdata;
7704bf88530SJohannes Berg const struct cfg80211_chan_def *compat = NULL;
7710fabfaafSArik Nemtsov struct sta_info *sta;
772e89a96f5SMichal Kazior
773e89a96f5SMichal Kazior lockdep_assert_held(&local->chanctx_mtx);
774e89a96f5SMichal Kazior
775e89a96f5SMichal Kazior rcu_read_lock();
776e89a96f5SMichal Kazior list_for_each_entry_rcu(sdata, &local->interfaces, list) {
777b4f85443SJohannes Berg int link_id;
7784bf88530SJohannes Berg
779e89a96f5SMichal Kazior if (!ieee80211_sdata_running(sdata))
780e89a96f5SMichal Kazior continue;
781b4f85443SJohannes Berg
7820e67c136SFelix Fietkau if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
7830e67c136SFelix Fietkau continue;
784e89a96f5SMichal Kazior
785b4f85443SJohannes Berg for (link_id = 0; link_id < ARRAY_SIZE(sdata->link); link_id++) {
786b4f85443SJohannes Berg struct ieee80211_bss_conf *link_conf =
787d8675a63SJohannes Berg rcu_dereference(sdata->vif.link_conf[link_id]);
7884bf88530SJohannes Berg
789b4f85443SJohannes Berg if (!link_conf)
790b4f85443SJohannes Berg continue;
791b4f85443SJohannes Berg
792b4f85443SJohannes Berg if (rcu_access_pointer(link_conf->chanctx_conf) != conf)
793b4f85443SJohannes Berg continue;
794b4f85443SJohannes Berg
795b4f85443SJohannes Berg if (!compat)
796b4f85443SJohannes Berg compat = &link_conf->chandef;
797b4f85443SJohannes Berg
798b4f85443SJohannes Berg compat = cfg80211_chandef_compatible(&link_conf->chandef,
799b4f85443SJohannes Berg compat);
800a00f4f6eSMichal Kazior if (WARN_ON_ONCE(!compat))
8014bf88530SJohannes Berg break;
802e89a96f5SMichal Kazior }
803b4f85443SJohannes Berg }
8040fabfaafSArik Nemtsov
805*05050a2bSAnjaneyulu if (WARN_ON_ONCE(!compat)) {
806*05050a2bSAnjaneyulu rcu_read_unlock();
807*05050a2bSAnjaneyulu return;
808*05050a2bSAnjaneyulu }
809*05050a2bSAnjaneyulu
8100fabfaafSArik Nemtsov /* TDLS peers can sometimes affect the chandef width */
8110fabfaafSArik Nemtsov list_for_each_entry_rcu(sta, &local->sta_list, list) {
8120fabfaafSArik Nemtsov if (!sta->uploaded ||
8130fabfaafSArik Nemtsov !test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW) ||
8140fabfaafSArik Nemtsov !test_sta_flag(sta, WLAN_STA_AUTHORIZED) ||
8150fabfaafSArik Nemtsov !sta->tdls_chandef.chan)
8160fabfaafSArik Nemtsov continue;
8170fabfaafSArik Nemtsov
8180fabfaafSArik Nemtsov compat = cfg80211_chandef_compatible(&sta->tdls_chandef,
8190fabfaafSArik Nemtsov compat);
8200fabfaafSArik Nemtsov if (WARN_ON_ONCE(!compat))
8210fabfaafSArik Nemtsov break;
8220fabfaafSArik Nemtsov }
823e89a96f5SMichal Kazior rcu_read_unlock();
824e89a96f5SMichal Kazior
825a00f4f6eSMichal Kazior if (!compat)
8264bf88530SJohannes Berg return;
827e89a96f5SMichal Kazior
828d6c37509SMordechay Goodstein ieee80211_change_chanctx(local, ctx, ctx, compat);
829e89a96f5SMichal Kazior }
830e89a96f5SMichal Kazior
ieee80211_recalc_radar_chanctx(struct ieee80211_local * local,struct ieee80211_chanctx * chanctx)831367bbd10SJohannes Berg static void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local,
832367bbd10SJohannes Berg struct ieee80211_chanctx *chanctx)
833367bbd10SJohannes Berg {
834367bbd10SJohannes Berg bool radar_enabled;
835367bbd10SJohannes Berg
836367bbd10SJohannes Berg lockdep_assert_held(&local->chanctx_mtx);
8375cbc95a7SEliad Peller /* for ieee80211_is_radar_required */
83834a3740dSJohannes Berg lockdep_assert_held(&local->mtx);
839367bbd10SJohannes Berg
840e7f2337aSEliad Peller radar_enabled = ieee80211_chanctx_radar_required(local, chanctx);
841367bbd10SJohannes Berg
842367bbd10SJohannes Berg if (radar_enabled == chanctx->conf.radar_enabled)
843367bbd10SJohannes Berg return;
844367bbd10SJohannes Berg
845367bbd10SJohannes Berg chanctx->conf.radar_enabled = radar_enabled;
846367bbd10SJohannes Berg
847367bbd10SJohannes Berg if (!local->use_chanctx) {
848367bbd10SJohannes Berg local->hw.conf.radar_enabled = chanctx->conf.radar_enabled;
849367bbd10SJohannes Berg ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
850367bbd10SJohannes Berg }
851367bbd10SJohannes Berg
852367bbd10SJohannes Berg drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR);
853367bbd10SJohannes Berg }
854367bbd10SJohannes Berg
ieee80211_assign_link_chanctx(struct ieee80211_link_data * link,struct ieee80211_chanctx * new_ctx)855b4f85443SJohannes Berg static int ieee80211_assign_link_chanctx(struct ieee80211_link_data *link,
85677eeba97SLuciano Coelho struct ieee80211_chanctx *new_ctx)
857d01a1e65SMichal Kazior {
858b4f85443SJohannes Berg struct ieee80211_sub_if_data *sdata = link->sdata;
85935f2fce9SMichal Kazior struct ieee80211_local *local = sdata->local;
86077eeba97SLuciano Coelho struct ieee80211_chanctx_conf *conf;
86177eeba97SLuciano Coelho struct ieee80211_chanctx *curr_ctx = NULL;
86277eeba97SLuciano Coelho int ret = 0;
863d01a1e65SMichal Kazior
864708d50edSAyala Beker if (WARN_ON(sdata->vif.type == NL80211_IFTYPE_NAN))
865708d50edSAyala Beker return -ENOTSUPP;
866708d50edSAyala Beker
867d8675a63SJohannes Berg conf = rcu_dereference_protected(link->conf->chanctx_conf,
86877eeba97SLuciano Coelho lockdep_is_held(&local->chanctx_mtx));
869d01a1e65SMichal Kazior
87077eeba97SLuciano Coelho if (conf) {
87177eeba97SLuciano Coelho curr_ctx = container_of(conf, struct ieee80211_chanctx, conf);
87235f2fce9SMichal Kazior
873727eff4dSGregory Greenman drv_unassign_vif_chanctx(local, sdata, link->conf, curr_ctx);
87477eeba97SLuciano Coelho conf = NULL;
875b4f85443SJohannes Berg list_del(&link->assigned_chanctx_list);
87677eeba97SLuciano Coelho }
87777eeba97SLuciano Coelho
87877eeba97SLuciano Coelho if (new_ctx) {
87904312de4SJohannes Berg /* recalc considering the link we'll use it for now */
88004312de4SJohannes Berg ieee80211_recalc_chanctx_min_def(local, new_ctx, link);
88104312de4SJohannes Berg
882727eff4dSGregory Greenman ret = drv_assign_vif_chanctx(local, sdata, link->conf, new_ctx);
88377eeba97SLuciano Coelho if (ret)
88477eeba97SLuciano Coelho goto out;
88577eeba97SLuciano Coelho
88677eeba97SLuciano Coelho conf = &new_ctx->conf;
887b4f85443SJohannes Berg list_add(&link->assigned_chanctx_list,
888b4f85443SJohannes Berg &new_ctx->assigned_links);
88977eeba97SLuciano Coelho }
89077eeba97SLuciano Coelho
89177eeba97SLuciano Coelho out:
892d8675a63SJohannes Berg rcu_assign_pointer(link->conf->chanctx_conf, conf);
89377eeba97SLuciano Coelho
894f276e20bSJohannes Berg sdata->vif.cfg.idle = !conf;
89577eeba97SLuciano Coelho
896c0166da9SMichal Kazior if (curr_ctx && ieee80211_chanctx_num_assigned(local, curr_ctx) > 0) {
89777eeba97SLuciano Coelho ieee80211_recalc_chanctx_chantype(local, curr_ctx);
89877eeba97SLuciano Coelho ieee80211_recalc_smps_chanctx(local, curr_ctx);
89977eeba97SLuciano Coelho ieee80211_recalc_radar_chanctx(local, curr_ctx);
900b72a455aSJohannes Berg ieee80211_recalc_chanctx_min_def(local, curr_ctx, NULL);
90177eeba97SLuciano Coelho }
90277eeba97SLuciano Coelho
903c0166da9SMichal Kazior if (new_ctx && ieee80211_chanctx_num_assigned(local, new_ctx) > 0) {
904db82d8a9SLorenzo Bianconi ieee80211_recalc_txpower(sdata, false);
905b72a455aSJohannes Berg ieee80211_recalc_chanctx_min_def(local, new_ctx, NULL);
90677eeba97SLuciano Coelho }
9075bbe754dSJohannes Berg
9085bbe754dSJohannes Berg if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
9095bbe754dSJohannes Berg sdata->vif.type != NL80211_IFTYPE_MONITOR)
9107b7090b4SJohannes Berg ieee80211_vif_cfg_change_notify(sdata, BSS_CHANGED_IDLE);
911fd0f979aSJohannes Berg
91217c18bf8SJohannes Berg ieee80211_check_fast_xmit_iface(sdata);
91317c18bf8SJohannes Berg
91477eeba97SLuciano Coelho return ret;
915d01a1e65SMichal Kazior }
916d01a1e65SMichal Kazior
ieee80211_recalc_smps_chanctx(struct ieee80211_local * local,struct ieee80211_chanctx * chanctx)91704ecd257SJohannes Berg void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
91804ecd257SJohannes Berg struct ieee80211_chanctx *chanctx)
91904ecd257SJohannes Berg {
92004ecd257SJohannes Berg struct ieee80211_sub_if_data *sdata;
92104ecd257SJohannes Berg u8 rx_chains_static, rx_chains_dynamic;
92204ecd257SJohannes Berg
92304ecd257SJohannes Berg lockdep_assert_held(&local->chanctx_mtx);
92404ecd257SJohannes Berg
92504ecd257SJohannes Berg rx_chains_static = 1;
92604ecd257SJohannes Berg rx_chains_dynamic = 1;
92704ecd257SJohannes Berg
92804ecd257SJohannes Berg rcu_read_lock();
92904ecd257SJohannes Berg list_for_each_entry_rcu(sdata, &local->interfaces, list) {
93004ecd257SJohannes Berg u8 needed_static, needed_dynamic;
931b4f85443SJohannes Berg unsigned int link_id;
93204ecd257SJohannes Berg
93304ecd257SJohannes Berg if (!ieee80211_sdata_running(sdata))
93404ecd257SJohannes Berg continue;
93504ecd257SJohannes Berg
93604ecd257SJohannes Berg switch (sdata->vif.type) {
93704ecd257SJohannes Berg case NL80211_IFTYPE_STATION:
93804ecd257SJohannes Berg if (!sdata->u.mgd.associated)
93904ecd257SJohannes Berg continue;
94004ecd257SJohannes Berg break;
94104ecd257SJohannes Berg case NL80211_IFTYPE_AP:
94204ecd257SJohannes Berg case NL80211_IFTYPE_ADHOC:
94304ecd257SJohannes Berg case NL80211_IFTYPE_MESH_POINT:
944239281f8SRostislav Lisovy case NL80211_IFTYPE_OCB:
94504ecd257SJohannes Berg break;
94604ecd257SJohannes Berg default:
947b4f85443SJohannes Berg continue;
94804ecd257SJohannes Berg }
94904ecd257SJohannes Berg
950b4f85443SJohannes Berg for (link_id = 0; link_id < ARRAY_SIZE(sdata->link); link_id++) {
951d8675a63SJohannes Berg struct ieee80211_link_data *link;
952b4f85443SJohannes Berg
953d8675a63SJohannes Berg link = rcu_dereference(sdata->link[link_id]);
954d8675a63SJohannes Berg
955d8675a63SJohannes Berg if (!link)
956b4f85443SJohannes Berg continue;
957b4f85443SJohannes Berg
958d8675a63SJohannes Berg if (rcu_access_pointer(link->conf->chanctx_conf) != &chanctx->conf)
959b4f85443SJohannes Berg continue;
960b4f85443SJohannes Berg
961b4f85443SJohannes Berg switch (link->smps_mode) {
96204ecd257SJohannes Berg default:
96304ecd257SJohannes Berg WARN_ONCE(1, "Invalid SMPS mode %d\n",
964b4f85443SJohannes Berg link->smps_mode);
965fc0561dcSGustavo A. R. Silva fallthrough;
96604ecd257SJohannes Berg case IEEE80211_SMPS_OFF:
967b4f85443SJohannes Berg needed_static = link->needed_rx_chains;
968b4f85443SJohannes Berg needed_dynamic = link->needed_rx_chains;
96904ecd257SJohannes Berg break;
97004ecd257SJohannes Berg case IEEE80211_SMPS_DYNAMIC:
97104ecd257SJohannes Berg needed_static = 1;
972b4f85443SJohannes Berg needed_dynamic = link->needed_rx_chains;
97304ecd257SJohannes Berg break;
97404ecd257SJohannes Berg case IEEE80211_SMPS_STATIC:
97504ecd257SJohannes Berg needed_static = 1;
97604ecd257SJohannes Berg needed_dynamic = 1;
97704ecd257SJohannes Berg break;
97804ecd257SJohannes Berg }
97904ecd257SJohannes Berg
98004ecd257SJohannes Berg rx_chains_static = max(rx_chains_static, needed_static);
98104ecd257SJohannes Berg rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
98204ecd257SJohannes Berg }
983b4f85443SJohannes Berg }
9847b8a9cddSIdo Yariv
9857b8a9cddSIdo Yariv /* Disable SMPS for the monitor interface */
9867b8a9cddSIdo Yariv sdata = rcu_dereference(local->monitor_sdata);
9877b8a9cddSIdo Yariv if (sdata &&
988d8675a63SJohannes Berg rcu_access_pointer(sdata->vif.bss_conf.chanctx_conf) == &chanctx->conf)
9897b8a9cddSIdo Yariv rx_chains_dynamic = rx_chains_static = local->rx_chains;
9907b8a9cddSIdo Yariv
99104ecd257SJohannes Berg rcu_read_unlock();
99204ecd257SJohannes Berg
99304ecd257SJohannes Berg if (!local->use_chanctx) {
99404ecd257SJohannes Berg if (rx_chains_static > 1)
99504ecd257SJohannes Berg local->smps_mode = IEEE80211_SMPS_OFF;
99604ecd257SJohannes Berg else if (rx_chains_dynamic > 1)
99704ecd257SJohannes Berg local->smps_mode = IEEE80211_SMPS_DYNAMIC;
99804ecd257SJohannes Berg else
99904ecd257SJohannes Berg local->smps_mode = IEEE80211_SMPS_STATIC;
100004ecd257SJohannes Berg ieee80211_hw_config(local, 0);
100104ecd257SJohannes Berg }
100204ecd257SJohannes Berg
100304ecd257SJohannes Berg if (rx_chains_static == chanctx->conf.rx_chains_static &&
100404ecd257SJohannes Berg rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
100504ecd257SJohannes Berg return;
100604ecd257SJohannes Berg
100704ecd257SJohannes Berg chanctx->conf.rx_chains_static = rx_chains_static;
100804ecd257SJohannes Berg chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
100904ecd257SJohannes Berg drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
101004ecd257SJohannes Berg }
101104ecd257SJohannes Berg
101211335a55SLuciano Coelho static void
__ieee80211_link_copy_chanctx_to_vlans(struct ieee80211_link_data * link,bool clear)1013b4f85443SJohannes Berg __ieee80211_link_copy_chanctx_to_vlans(struct ieee80211_link_data *link,
101411335a55SLuciano Coelho bool clear)
101511335a55SLuciano Coelho {
1016b4f85443SJohannes Berg struct ieee80211_sub_if_data *sdata = link->sdata;
1017b4f85443SJohannes Berg unsigned int link_id = link->link_id;
1018d8675a63SJohannes Berg struct ieee80211_bss_conf *link_conf = link->conf;
101933926eb7SJohannes Berg struct ieee80211_local *local __maybe_unused = sdata->local;
102011335a55SLuciano Coelho struct ieee80211_sub_if_data *vlan;
102111335a55SLuciano Coelho struct ieee80211_chanctx_conf *conf;
102211335a55SLuciano Coelho
102311335a55SLuciano Coelho if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
102411335a55SLuciano Coelho return;
102511335a55SLuciano Coelho
102611335a55SLuciano Coelho lockdep_assert_held(&local->mtx);
102711335a55SLuciano Coelho
102811335a55SLuciano Coelho /* Check that conf exists, even when clearing this function
102911335a55SLuciano Coelho * must be called with the AP's channel context still there
103011335a55SLuciano Coelho * as it would otherwise cause VLANs to have an invalid
103111335a55SLuciano Coelho * channel context pointer for a while, possibly pointing
103211335a55SLuciano Coelho * to a channel context that has already been freed.
103311335a55SLuciano Coelho */
1034b4f85443SJohannes Berg conf = rcu_dereference_protected(link_conf->chanctx_conf,
103511335a55SLuciano Coelho lockdep_is_held(&local->chanctx_mtx));
103611335a55SLuciano Coelho WARN_ON(!conf);
103711335a55SLuciano Coelho
103811335a55SLuciano Coelho if (clear)
103911335a55SLuciano Coelho conf = NULL;
104011335a55SLuciano Coelho
1041d8675a63SJohannes Berg rcu_read_lock();
1042d8675a63SJohannes Berg list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) {
1043d8675a63SJohannes Berg struct ieee80211_bss_conf *vlan_conf;
1044d8675a63SJohannes Berg
1045d8675a63SJohannes Berg vlan_conf = rcu_dereference(vlan->vif.link_conf[link_id]);
1046d8675a63SJohannes Berg if (WARN_ON(!vlan_conf))
1047d8675a63SJohannes Berg continue;
1048d8675a63SJohannes Berg
1049d8675a63SJohannes Berg rcu_assign_pointer(vlan_conf->chanctx_conf, conf);
1050d8675a63SJohannes Berg }
1051d8675a63SJohannes Berg rcu_read_unlock();
105211335a55SLuciano Coelho }
105311335a55SLuciano Coelho
ieee80211_link_copy_chanctx_to_vlans(struct ieee80211_link_data * link,bool clear)1054b4f85443SJohannes Berg void ieee80211_link_copy_chanctx_to_vlans(struct ieee80211_link_data *link,
105511335a55SLuciano Coelho bool clear)
105611335a55SLuciano Coelho {
1057b4f85443SJohannes Berg struct ieee80211_local *local = link->sdata->local;
105811335a55SLuciano Coelho
105911335a55SLuciano Coelho mutex_lock(&local->chanctx_mtx);
106011335a55SLuciano Coelho
1061b4f85443SJohannes Berg __ieee80211_link_copy_chanctx_to_vlans(link, clear);
106211335a55SLuciano Coelho
106311335a55SLuciano Coelho mutex_unlock(&local->chanctx_mtx);
106411335a55SLuciano Coelho }
106511335a55SLuciano Coelho
ieee80211_link_unreserve_chanctx(struct ieee80211_link_data * link)1066b4f85443SJohannes Berg int ieee80211_link_unreserve_chanctx(struct ieee80211_link_data *link)
106711335a55SLuciano Coelho {
1068b4f85443SJohannes Berg struct ieee80211_sub_if_data *sdata = link->sdata;
1069b4f85443SJohannes Berg struct ieee80211_chanctx *ctx = link->reserved_chanctx;
1070e3afb920SMichal Kazior
107111335a55SLuciano Coelho lockdep_assert_held(&sdata->local->chanctx_mtx);
107211335a55SLuciano Coelho
1073e3afb920SMichal Kazior if (WARN_ON(!ctx))
107411335a55SLuciano Coelho return -EINVAL;
107511335a55SLuciano Coelho
1076b4f85443SJohannes Berg list_del(&link->reserved_chanctx_list);
1077b4f85443SJohannes Berg link->reserved_chanctx = NULL;
107811335a55SLuciano Coelho
10795bcae31dSMichal Kazior if (ieee80211_chanctx_refcount(sdata->local, ctx) == 0) {
10805bcae31dSMichal Kazior if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
10815bcae31dSMichal Kazior if (WARN_ON(!ctx->replace_ctx))
10825bcae31dSMichal Kazior return -EINVAL;
10835bcae31dSMichal Kazior
10845bcae31dSMichal Kazior WARN_ON(ctx->replace_ctx->replace_state !=
10855bcae31dSMichal Kazior IEEE80211_CHANCTX_WILL_BE_REPLACED);
10865bcae31dSMichal Kazior WARN_ON(ctx->replace_ctx->replace_ctx != ctx);
10875bcae31dSMichal Kazior
10885bcae31dSMichal Kazior ctx->replace_ctx->replace_ctx = NULL;
10895bcae31dSMichal Kazior ctx->replace_ctx->replace_state =
10905bcae31dSMichal Kazior IEEE80211_CHANCTX_REPLACE_NONE;
10915bcae31dSMichal Kazior
10925bcae31dSMichal Kazior list_del_rcu(&ctx->list);
10935bcae31dSMichal Kazior kfree_rcu(ctx, rcu_head);
10945bcae31dSMichal Kazior } else {
1095e3afb920SMichal Kazior ieee80211_free_chanctx(sdata->local, ctx);
10965bcae31dSMichal Kazior }
10975bcae31dSMichal Kazior }
1098e3afb920SMichal Kazior
109911335a55SLuciano Coelho return 0;
110011335a55SLuciano Coelho }
110111335a55SLuciano Coelho
ieee80211_link_reserve_chanctx(struct ieee80211_link_data * link,const struct cfg80211_chan_def * chandef,enum ieee80211_chanctx_mode mode,bool radar_required)1102b4f85443SJohannes Berg int ieee80211_link_reserve_chanctx(struct ieee80211_link_data *link,
110311335a55SLuciano Coelho const struct cfg80211_chan_def *chandef,
110409332481SMichal Kazior enum ieee80211_chanctx_mode mode,
110509332481SMichal Kazior bool radar_required)
110611335a55SLuciano Coelho {
1107b4f85443SJohannes Berg struct ieee80211_sub_if_data *sdata = link->sdata;
110811335a55SLuciano Coelho struct ieee80211_local *local = sdata->local;
11095bcae31dSMichal Kazior struct ieee80211_chanctx *new_ctx, *curr_ctx, *ctx;
111011335a55SLuciano Coelho
11115bcae31dSMichal Kazior lockdep_assert_held(&local->chanctx_mtx);
111211335a55SLuciano Coelho
1113b4f85443SJohannes Berg curr_ctx = ieee80211_link_get_chanctx(link);
11145bcae31dSMichal Kazior if (curr_ctx && local->use_chanctx && !local->ops->switch_vif_chanctx)
11155bcae31dSMichal Kazior return -ENOTSUPP;
111611335a55SLuciano Coelho
111713f348a8SMichal Kazior new_ctx = ieee80211_find_reservation_chanctx(local, chandef, mode);
111811335a55SLuciano Coelho if (!new_ctx) {
11195bcae31dSMichal Kazior if (ieee80211_can_create_new_chanctx(local)) {
112011335a55SLuciano Coelho new_ctx = ieee80211_new_chanctx(local, chandef, mode);
11215bcae31dSMichal Kazior if (IS_ERR(new_ctx))
11225bcae31dSMichal Kazior return PTR_ERR(new_ctx);
1123c2b90ad8SMichal Kazior } else {
11245bcae31dSMichal Kazior if (!curr_ctx ||
11255bcae31dSMichal Kazior (curr_ctx->replace_state ==
11265bcae31dSMichal Kazior IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
1127b4f85443SJohannes Berg !list_empty(&curr_ctx->reserved_links)) {
11285bcae31dSMichal Kazior /*
1129b4f85443SJohannes Berg * Another link already requested this context
11305bcae31dSMichal Kazior * for a reservation. Find another one hoping
1131b4f85443SJohannes Berg * all links assigned to it will also switch
11325bcae31dSMichal Kazior * soon enough.
11335bcae31dSMichal Kazior *
11345bcae31dSMichal Kazior * TODO: This needs a little more work as some
11355bcae31dSMichal Kazior * cases (more than 2 chanctx capable devices)
11365bcae31dSMichal Kazior * may fail which could otherwise succeed
11375bcae31dSMichal Kazior * provided some channel context juggling was
11385bcae31dSMichal Kazior * performed.
11395bcae31dSMichal Kazior *
1140b4f85443SJohannes Berg * Consider ctx1..3, link1..6, each ctx has 2
1141b4f85443SJohannes Berg * links. link1 and link2 from ctx1 request new
11425bcae31dSMichal Kazior * different chandefs starting 2 in-place
11435bcae31dSMichal Kazior * reserations with ctx4 and ctx5 replacing
1144b4f85443SJohannes Berg * ctx1 and ctx2 respectively. Next link5 and
1145b4f85443SJohannes Berg * link6 from ctx3 reserve ctx4. If link3 and
1146b4f85443SJohannes Berg * link4 remain on ctx2 as they are then this
11475bcae31dSMichal Kazior * fails unless `replace_ctx` from ctx5 is
11485bcae31dSMichal Kazior * replaced with ctx3.
11495bcae31dSMichal Kazior */
11505bcae31dSMichal Kazior list_for_each_entry(ctx, &local->chanctx_list,
11515bcae31dSMichal Kazior list) {
11525bcae31dSMichal Kazior if (ctx->replace_state !=
11535bcae31dSMichal Kazior IEEE80211_CHANCTX_REPLACE_NONE)
11545bcae31dSMichal Kazior continue;
11555bcae31dSMichal Kazior
1156b4f85443SJohannes Berg if (!list_empty(&ctx->reserved_links))
11575bcae31dSMichal Kazior continue;
11585bcae31dSMichal Kazior
11595bcae31dSMichal Kazior curr_ctx = ctx;
11605bcae31dSMichal Kazior break;
11615bcae31dSMichal Kazior }
11625bcae31dSMichal Kazior }
11635bcae31dSMichal Kazior
11645bcae31dSMichal Kazior /*
11655bcae31dSMichal Kazior * If that's true then all available contexts already
11665bcae31dSMichal Kazior * have reservations and cannot be used.
11675bcae31dSMichal Kazior */
11685bcae31dSMichal Kazior if (!curr_ctx ||
11695bcae31dSMichal Kazior (curr_ctx->replace_state ==
11705bcae31dSMichal Kazior IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
1171b4f85443SJohannes Berg !list_empty(&curr_ctx->reserved_links))
11725bcae31dSMichal Kazior return -EBUSY;
11735bcae31dSMichal Kazior
11745bcae31dSMichal Kazior new_ctx = ieee80211_alloc_chanctx(local, chandef, mode);
11755bcae31dSMichal Kazior if (!new_ctx)
11765bcae31dSMichal Kazior return -ENOMEM;
11775bcae31dSMichal Kazior
11785bcae31dSMichal Kazior new_ctx->replace_ctx = curr_ctx;
11795bcae31dSMichal Kazior new_ctx->replace_state =
11805bcae31dSMichal Kazior IEEE80211_CHANCTX_REPLACES_OTHER;
11815bcae31dSMichal Kazior
11825bcae31dSMichal Kazior curr_ctx->replace_ctx = new_ctx;
11835bcae31dSMichal Kazior curr_ctx->replace_state =
11845bcae31dSMichal Kazior IEEE80211_CHANCTX_WILL_BE_REPLACED;
11855bcae31dSMichal Kazior
11865bcae31dSMichal Kazior list_add_rcu(&new_ctx->list, &local->chanctx_list);
118711335a55SLuciano Coelho }
11885d52ee81SLuciano Coelho }
118911335a55SLuciano Coelho
1190b4f85443SJohannes Berg list_add(&link->reserved_chanctx_list, &new_ctx->reserved_links);
1191b4f85443SJohannes Berg link->reserved_chanctx = new_ctx;
1192b4f85443SJohannes Berg link->reserved_chandef = *chandef;
1193b4f85443SJohannes Berg link->reserved_radar_required = radar_required;
1194b4f85443SJohannes Berg link->reserved_ready = false;
11955bcae31dSMichal Kazior
11965bcae31dSMichal Kazior return 0;
119711335a55SLuciano Coelho }
119811335a55SLuciano Coelho
119903078de4SMichal Kazior static void
ieee80211_link_chanctx_reservation_complete(struct ieee80211_link_data * link)1200b4f85443SJohannes Berg ieee80211_link_chanctx_reservation_complete(struct ieee80211_link_data *link)
120103078de4SMichal Kazior {
1202b4f85443SJohannes Berg struct ieee80211_sub_if_data *sdata = link->sdata;
1203b4f85443SJohannes Berg
120403078de4SMichal Kazior switch (sdata->vif.type) {
120503078de4SMichal Kazior case NL80211_IFTYPE_ADHOC:
120603078de4SMichal Kazior case NL80211_IFTYPE_AP:
120703078de4SMichal Kazior case NL80211_IFTYPE_MESH_POINT:
12086e0bd6c3SRostislav Lisovy case NL80211_IFTYPE_OCB:
120903078de4SMichal Kazior ieee80211_queue_work(&sdata->local->hw,
1210b4f85443SJohannes Berg &link->csa_finalize_work);
121103078de4SMichal Kazior break;
121203078de4SMichal Kazior case NL80211_IFTYPE_STATION:
1213ec3252bfSJohannes Berg wiphy_delayed_work_queue(sdata->local->hw.wiphy,
1214ec3252bfSJohannes Berg &link->u.mgd.chswitch_work, 0);
12154c3ebc56SMichal Kazior break;
12164c3ebc56SMichal Kazior case NL80211_IFTYPE_UNSPECIFIED:
121703078de4SMichal Kazior case NL80211_IFTYPE_AP_VLAN:
121803078de4SMichal Kazior case NL80211_IFTYPE_WDS:
121903078de4SMichal Kazior case NL80211_IFTYPE_MONITOR:
122003078de4SMichal Kazior case NL80211_IFTYPE_P2P_CLIENT:
122103078de4SMichal Kazior case NL80211_IFTYPE_P2P_GO:
122203078de4SMichal Kazior case NL80211_IFTYPE_P2P_DEVICE:
1223cb3b7d87SAyala Beker case NL80211_IFTYPE_NAN:
122403078de4SMichal Kazior case NUM_NL80211_IFTYPES:
122503078de4SMichal Kazior WARN_ON(1);
122603078de4SMichal Kazior break;
122703078de4SMichal Kazior }
122803078de4SMichal Kazior }
122903078de4SMichal Kazior
12302967e031SFelix Fietkau static void
ieee80211_link_update_chandef(struct ieee80211_link_data * link,const struct cfg80211_chan_def * chandef)1231b4f85443SJohannes Berg ieee80211_link_update_chandef(struct ieee80211_link_data *link,
12322967e031SFelix Fietkau const struct cfg80211_chan_def *chandef)
12332967e031SFelix Fietkau {
1234b4f85443SJohannes Berg struct ieee80211_sub_if_data *sdata = link->sdata;
1235b4f85443SJohannes Berg unsigned int link_id = link->link_id;
12362967e031SFelix Fietkau struct ieee80211_sub_if_data *vlan;
12372967e031SFelix Fietkau
1238d8675a63SJohannes Berg link->conf->chandef = *chandef;
12392967e031SFelix Fietkau
12402967e031SFelix Fietkau if (sdata->vif.type != NL80211_IFTYPE_AP)
12412967e031SFelix Fietkau return;
12422967e031SFelix Fietkau
1243d8675a63SJohannes Berg rcu_read_lock();
1244d8675a63SJohannes Berg list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) {
1245d8675a63SJohannes Berg struct ieee80211_bss_conf *vlan_conf;
1246d8675a63SJohannes Berg
1247d8675a63SJohannes Berg vlan_conf = rcu_dereference(vlan->vif.link_conf[link_id]);
1248d8675a63SJohannes Berg if (WARN_ON(!vlan_conf))
1249d8675a63SJohannes Berg continue;
1250d8675a63SJohannes Berg
1251d8675a63SJohannes Berg vlan_conf->chandef = *chandef;
1252d8675a63SJohannes Berg }
1253d8675a63SJohannes Berg rcu_read_unlock();
12542967e031SFelix Fietkau }
12552967e031SFelix Fietkau
12565bcae31dSMichal Kazior static int
ieee80211_link_use_reserved_reassign(struct ieee80211_link_data * link)1257b4f85443SJohannes Berg ieee80211_link_use_reserved_reassign(struct ieee80211_link_data *link)
125811335a55SLuciano Coelho {
1259b4f85443SJohannes Berg struct ieee80211_sub_if_data *sdata = link->sdata;
1260d8675a63SJohannes Berg struct ieee80211_bss_conf *link_conf = link->conf;
126111335a55SLuciano Coelho struct ieee80211_local *local = sdata->local;
12625bcae31dSMichal Kazior struct ieee80211_vif_chanctx_switch vif_chsw[1] = {};
12635bcae31dSMichal Kazior struct ieee80211_chanctx *old_ctx, *new_ctx;
12645bcae31dSMichal Kazior const struct cfg80211_chan_def *chandef;
126515ddba5fSAnjaneyulu u64 changed = 0;
12665bcae31dSMichal Kazior int err;
126711335a55SLuciano Coelho
126811335a55SLuciano Coelho lockdep_assert_held(&local->mtx);
12695bcae31dSMichal Kazior lockdep_assert_held(&local->chanctx_mtx);
127011335a55SLuciano Coelho
1271b4f85443SJohannes Berg new_ctx = link->reserved_chanctx;
1272b4f85443SJohannes Berg old_ctx = ieee80211_link_get_chanctx(link);
127311335a55SLuciano Coelho
1274b4f85443SJohannes Berg if (WARN_ON(!link->reserved_ready))
12755bcae31dSMichal Kazior return -EBUSY;
127611335a55SLuciano Coelho
12775bcae31dSMichal Kazior if (WARN_ON(!new_ctx))
12785bcae31dSMichal Kazior return -EINVAL;
127911335a55SLuciano Coelho
12805bcae31dSMichal Kazior if (WARN_ON(!old_ctx))
12815bcae31dSMichal Kazior return -EINVAL;
128211335a55SLuciano Coelho
12835bcae31dSMichal Kazior if (WARN_ON(new_ctx->replace_state ==
12845bcae31dSMichal Kazior IEEE80211_CHANCTX_REPLACES_OTHER))
12855bcae31dSMichal Kazior return -EINVAL;
128611335a55SLuciano Coelho
12875bcae31dSMichal Kazior chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1288b4f85443SJohannes Berg &link->reserved_chandef);
12895bcae31dSMichal Kazior if (WARN_ON(!chandef))
12905bcae31dSMichal Kazior return -EINVAL;
129111335a55SLuciano Coelho
1292b4f85443SJohannes Berg if (link_conf->chandef.width != link->reserved_chandef.width)
1293d6c37509SMordechay Goodstein changed = BSS_CHANGED_BANDWIDTH;
129444b72ca8SIlan Peer
1295b4f85443SJohannes Berg ieee80211_link_update_chandef(link, &link->reserved_chandef);
1296bf45a242SAndrei Otcheretianski
1297b72a455aSJohannes Berg _ieee80211_change_chanctx(local, new_ctx, old_ctx, chandef, link);
129844b72ca8SIlan Peer
12995bcae31dSMichal Kazior vif_chsw[0].vif = &sdata->vif;
13005bcae31dSMichal Kazior vif_chsw[0].old_ctx = &old_ctx->conf;
13015bcae31dSMichal Kazior vif_chsw[0].new_ctx = &new_ctx->conf;
1302727eff4dSGregory Greenman vif_chsw[0].link_conf = link->conf;
13035bcae31dSMichal Kazior
1304b4f85443SJohannes Berg list_del(&link->reserved_chanctx_list);
1305b4f85443SJohannes Berg link->reserved_chanctx = NULL;
130611335a55SLuciano Coelho
13075bcae31dSMichal Kazior err = drv_switch_vif_chanctx(local, vif_chsw, 1,
13085bcae31dSMichal Kazior CHANCTX_SWMODE_REASSIGN_VIF);
13095bcae31dSMichal Kazior if (err) {
13105bcae31dSMichal Kazior if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
13115bcae31dSMichal Kazior ieee80211_free_chanctx(local, new_ctx);
13125bcae31dSMichal Kazior
131303078de4SMichal Kazior goto out;
131411335a55SLuciano Coelho }
131511335a55SLuciano Coelho
1316b4f85443SJohannes Berg list_move(&link->assigned_chanctx_list, &new_ctx->assigned_links);
1317b4f85443SJohannes Berg rcu_assign_pointer(link_conf->chanctx_conf, &new_ctx->conf);
13185bcae31dSMichal Kazior
131911335a55SLuciano Coelho if (sdata->vif.type == NL80211_IFTYPE_AP)
1320b4f85443SJohannes Berg __ieee80211_link_copy_chanctx_to_vlans(link, false);
13215bcae31dSMichal Kazior
132217c18bf8SJohannes Berg ieee80211_check_fast_xmit_iface(sdata);
132317c18bf8SJohannes Berg
13245bcae31dSMichal Kazior if (ieee80211_chanctx_refcount(local, old_ctx) == 0)
13255bcae31dSMichal Kazior ieee80211_free_chanctx(local, old_ctx);
13265bcae31dSMichal Kazior
1327b72a455aSJohannes Berg ieee80211_recalc_chanctx_min_def(local, new_ctx, NULL);
1328722ddb0dSEmmanuel Grumbach ieee80211_recalc_smps_chanctx(local, new_ctx);
1329722ddb0dSEmmanuel Grumbach ieee80211_recalc_radar_chanctx(local, new_ctx);
1330722ddb0dSEmmanuel Grumbach
13315bcae31dSMichal Kazior if (changed)
1332d8675a63SJohannes Berg ieee80211_link_info_change_notify(sdata, link, changed);
13335bcae31dSMichal Kazior
133403078de4SMichal Kazior out:
1335b4f85443SJohannes Berg ieee80211_link_chanctx_reservation_complete(link);
13365bcae31dSMichal Kazior return err;
13375d52ee81SLuciano Coelho }
133811335a55SLuciano Coelho
13395bcae31dSMichal Kazior static int
ieee80211_link_use_reserved_assign(struct ieee80211_link_data * link)1340b4f85443SJohannes Berg ieee80211_link_use_reserved_assign(struct ieee80211_link_data *link)
13415bcae31dSMichal Kazior {
1342b4f85443SJohannes Berg struct ieee80211_sub_if_data *sdata = link->sdata;
13435bcae31dSMichal Kazior struct ieee80211_local *local = sdata->local;
13445bcae31dSMichal Kazior struct ieee80211_chanctx *old_ctx, *new_ctx;
13455bcae31dSMichal Kazior const struct cfg80211_chan_def *chandef;
13465bcae31dSMichal Kazior int err;
13475bcae31dSMichal Kazior
1348d8675a63SJohannes Berg old_ctx = ieee80211_link_get_chanctx(link);
1349b4f85443SJohannes Berg new_ctx = link->reserved_chanctx;
13505bcae31dSMichal Kazior
1351b4f85443SJohannes Berg if (WARN_ON(!link->reserved_ready))
13525bcae31dSMichal Kazior return -EINVAL;
13535bcae31dSMichal Kazior
13545bcae31dSMichal Kazior if (WARN_ON(old_ctx))
13555bcae31dSMichal Kazior return -EINVAL;
13565bcae31dSMichal Kazior
13575bcae31dSMichal Kazior if (WARN_ON(!new_ctx))
13585bcae31dSMichal Kazior return -EINVAL;
13595bcae31dSMichal Kazior
13605bcae31dSMichal Kazior if (WARN_ON(new_ctx->replace_state ==
13615bcae31dSMichal Kazior IEEE80211_CHANCTX_REPLACES_OTHER))
13625bcae31dSMichal Kazior return -EINVAL;
13635bcae31dSMichal Kazior
13645bcae31dSMichal Kazior chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1365b4f85443SJohannes Berg &link->reserved_chandef);
13665bcae31dSMichal Kazior if (WARN_ON(!chandef))
13675bcae31dSMichal Kazior return -EINVAL;
13685bcae31dSMichal Kazior
1369d6c37509SMordechay Goodstein ieee80211_change_chanctx(local, new_ctx, new_ctx, chandef);
1370bf45a242SAndrei Otcheretianski
1371b4f85443SJohannes Berg list_del(&link->reserved_chanctx_list);
1372b4f85443SJohannes Berg link->reserved_chanctx = NULL;
13735bcae31dSMichal Kazior
1374b4f85443SJohannes Berg err = ieee80211_assign_link_chanctx(link, new_ctx);
13755bcae31dSMichal Kazior if (err) {
13765bcae31dSMichal Kazior if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
13775bcae31dSMichal Kazior ieee80211_free_chanctx(local, new_ctx);
13785bcae31dSMichal Kazior
13795bcae31dSMichal Kazior goto out;
13805bcae31dSMichal Kazior }
13815bcae31dSMichal Kazior
13825bcae31dSMichal Kazior out:
1383b4f85443SJohannes Berg ieee80211_link_chanctx_reservation_complete(link);
13845bcae31dSMichal Kazior return err;
13855bcae31dSMichal Kazior }
13865bcae31dSMichal Kazior
13875bcae31dSMichal Kazior static bool
ieee80211_link_has_in_place_reservation(struct ieee80211_link_data * link)1388b4f85443SJohannes Berg ieee80211_link_has_in_place_reservation(struct ieee80211_link_data *link)
13895bcae31dSMichal Kazior {
1390b4f85443SJohannes Berg struct ieee80211_sub_if_data *sdata = link->sdata;
13915bcae31dSMichal Kazior struct ieee80211_chanctx *old_ctx, *new_ctx;
13925bcae31dSMichal Kazior
13935bcae31dSMichal Kazior lockdep_assert_held(&sdata->local->chanctx_mtx);
13945bcae31dSMichal Kazior
1395b4f85443SJohannes Berg new_ctx = link->reserved_chanctx;
1396b4f85443SJohannes Berg old_ctx = ieee80211_link_get_chanctx(link);
13975bcae31dSMichal Kazior
13985bcae31dSMichal Kazior if (!old_ctx)
13995bcae31dSMichal Kazior return false;
14005bcae31dSMichal Kazior
14015bcae31dSMichal Kazior if (WARN_ON(!new_ctx))
14025bcae31dSMichal Kazior return false;
14035bcae31dSMichal Kazior
14045bcae31dSMichal Kazior if (old_ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
14055bcae31dSMichal Kazior return false;
14065bcae31dSMichal Kazior
14075bcae31dSMichal Kazior if (new_ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
14085bcae31dSMichal Kazior return false;
14095bcae31dSMichal Kazior
14105bcae31dSMichal Kazior return true;
14115bcae31dSMichal Kazior }
14125bcae31dSMichal Kazior
ieee80211_chsw_switch_hwconf(struct ieee80211_local * local,struct ieee80211_chanctx * new_ctx)14135bcae31dSMichal Kazior static int ieee80211_chsw_switch_hwconf(struct ieee80211_local *local,
14145bcae31dSMichal Kazior struct ieee80211_chanctx *new_ctx)
14155bcae31dSMichal Kazior {
14165bcae31dSMichal Kazior const struct cfg80211_chan_def *chandef;
14175bcae31dSMichal Kazior
14185bcae31dSMichal Kazior lockdep_assert_held(&local->mtx);
14195bcae31dSMichal Kazior lockdep_assert_held(&local->chanctx_mtx);
14205bcae31dSMichal Kazior
14215bcae31dSMichal Kazior chandef = ieee80211_chanctx_reserved_chandef(local, new_ctx, NULL);
14225bcae31dSMichal Kazior if (WARN_ON(!chandef))
14235bcae31dSMichal Kazior return -EINVAL;
14245bcae31dSMichal Kazior
14255bcae31dSMichal Kazior local->hw.conf.radar_enabled = new_ctx->conf.radar_enabled;
14265bcae31dSMichal Kazior local->_oper_chandef = *chandef;
14275bcae31dSMichal Kazior ieee80211_hw_config(local, 0);
14285bcae31dSMichal Kazior
14295bcae31dSMichal Kazior return 0;
14305bcae31dSMichal Kazior }
14315bcae31dSMichal Kazior
ieee80211_chsw_switch_vifs(struct ieee80211_local * local,int n_vifs)14325bcae31dSMichal Kazior static int ieee80211_chsw_switch_vifs(struct ieee80211_local *local,
14335bcae31dSMichal Kazior int n_vifs)
14345bcae31dSMichal Kazior {
14355bcae31dSMichal Kazior struct ieee80211_vif_chanctx_switch *vif_chsw;
1436b4f85443SJohannes Berg struct ieee80211_link_data *link;
14375bcae31dSMichal Kazior struct ieee80211_chanctx *ctx, *old_ctx;
14385bcae31dSMichal Kazior int i, err;
14395bcae31dSMichal Kazior
14405bcae31dSMichal Kazior lockdep_assert_held(&local->mtx);
14415bcae31dSMichal Kazior lockdep_assert_held(&local->chanctx_mtx);
14425bcae31dSMichal Kazior
14436396bb22SKees Cook vif_chsw = kcalloc(n_vifs, sizeof(vif_chsw[0]), GFP_KERNEL);
14445bcae31dSMichal Kazior if (!vif_chsw)
14455bcae31dSMichal Kazior return -ENOMEM;
14465bcae31dSMichal Kazior
14475bcae31dSMichal Kazior i = 0;
14485bcae31dSMichal Kazior list_for_each_entry(ctx, &local->chanctx_list, list) {
14495bcae31dSMichal Kazior if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
14505bcae31dSMichal Kazior continue;
14515bcae31dSMichal Kazior
14525bcae31dSMichal Kazior if (WARN_ON(!ctx->replace_ctx)) {
14535bcae31dSMichal Kazior err = -EINVAL;
14545bcae31dSMichal Kazior goto out;
14555bcae31dSMichal Kazior }
14565bcae31dSMichal Kazior
1457b4f85443SJohannes Berg list_for_each_entry(link, &ctx->reserved_links,
14585bcae31dSMichal Kazior reserved_chanctx_list) {
1459b4f85443SJohannes Berg if (!ieee80211_link_has_in_place_reservation(link))
14605bcae31dSMichal Kazior continue;
14615bcae31dSMichal Kazior
1462b4f85443SJohannes Berg old_ctx = ieee80211_link_get_chanctx(link);
1463b4f85443SJohannes Berg vif_chsw[i].vif = &link->sdata->vif;
14645bcae31dSMichal Kazior vif_chsw[i].old_ctx = &old_ctx->conf;
14655bcae31dSMichal Kazior vif_chsw[i].new_ctx = &ctx->conf;
1466727eff4dSGregory Greenman vif_chsw[i].link_conf = link->conf;
14675bcae31dSMichal Kazior
14685bcae31dSMichal Kazior i++;
14695bcae31dSMichal Kazior }
14705bcae31dSMichal Kazior }
14715bcae31dSMichal Kazior
14725bcae31dSMichal Kazior err = drv_switch_vif_chanctx(local, vif_chsw, n_vifs,
14735bcae31dSMichal Kazior CHANCTX_SWMODE_SWAP_CONTEXTS);
14745bcae31dSMichal Kazior
14755bcae31dSMichal Kazior out:
14765bcae31dSMichal Kazior kfree(vif_chsw);
14775bcae31dSMichal Kazior return err;
14785bcae31dSMichal Kazior }
14795bcae31dSMichal Kazior
ieee80211_chsw_switch_ctxs(struct ieee80211_local * local)14805bcae31dSMichal Kazior static int ieee80211_chsw_switch_ctxs(struct ieee80211_local *local)
14815bcae31dSMichal Kazior {
14825bcae31dSMichal Kazior struct ieee80211_chanctx *ctx;
14835bcae31dSMichal Kazior int err;
14845bcae31dSMichal Kazior
14855bcae31dSMichal Kazior lockdep_assert_held(&local->mtx);
14865bcae31dSMichal Kazior lockdep_assert_held(&local->chanctx_mtx);
14875bcae31dSMichal Kazior
14885bcae31dSMichal Kazior list_for_each_entry(ctx, &local->chanctx_list, list) {
14895bcae31dSMichal Kazior if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
14905bcae31dSMichal Kazior continue;
14915bcae31dSMichal Kazior
1492b4f85443SJohannes Berg if (!list_empty(&ctx->replace_ctx->assigned_links))
14935bcae31dSMichal Kazior continue;
14945bcae31dSMichal Kazior
14955bcae31dSMichal Kazior ieee80211_del_chanctx(local, ctx->replace_ctx);
14965bcae31dSMichal Kazior err = ieee80211_add_chanctx(local, ctx);
14975bcae31dSMichal Kazior if (err)
14985bcae31dSMichal Kazior goto err;
14995bcae31dSMichal Kazior }
15005bcae31dSMichal Kazior
15015bcae31dSMichal Kazior return 0;
15025bcae31dSMichal Kazior
15035bcae31dSMichal Kazior err:
15045bcae31dSMichal Kazior WARN_ON(ieee80211_add_chanctx(local, ctx));
15055bcae31dSMichal Kazior list_for_each_entry_continue_reverse(ctx, &local->chanctx_list, list) {
15065bcae31dSMichal Kazior if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
15075bcae31dSMichal Kazior continue;
15085bcae31dSMichal Kazior
1509b4f85443SJohannes Berg if (!list_empty(&ctx->replace_ctx->assigned_links))
15105bcae31dSMichal Kazior continue;
15115bcae31dSMichal Kazior
15125bcae31dSMichal Kazior ieee80211_del_chanctx(local, ctx);
15135bcae31dSMichal Kazior WARN_ON(ieee80211_add_chanctx(local, ctx->replace_ctx));
15145bcae31dSMichal Kazior }
15155bcae31dSMichal Kazior
15165bcae31dSMichal Kazior return err;
15175bcae31dSMichal Kazior }
15185bcae31dSMichal Kazior
ieee80211_vif_use_reserved_switch(struct ieee80211_local * local)1519649b2a4dSJohannes Berg static int ieee80211_vif_use_reserved_switch(struct ieee80211_local *local)
15205bcae31dSMichal Kazior {
15215bcae31dSMichal Kazior struct ieee80211_chanctx *ctx, *ctx_tmp, *old_ctx;
15225bcae31dSMichal Kazior struct ieee80211_chanctx *new_ctx = NULL;
1523b7f2405cSKirtika Ruchandani int err, n_assigned, n_reserved, n_ready;
15245bcae31dSMichal Kazior int n_ctx = 0, n_vifs_switch = 0, n_vifs_assign = 0, n_vifs_ctxless = 0;
15255bcae31dSMichal Kazior
15265bcae31dSMichal Kazior lockdep_assert_held(&local->mtx);
15275bcae31dSMichal Kazior lockdep_assert_held(&local->chanctx_mtx);
15285bcae31dSMichal Kazior
15295bcae31dSMichal Kazior /*
15305bcae31dSMichal Kazior * If there are 2 independent pairs of channel contexts performing
15315bcae31dSMichal Kazior * cross-switch of their vifs this code will still wait until both are
15325bcae31dSMichal Kazior * ready even though it could be possible to switch one before the
15335bcae31dSMichal Kazior * other is ready.
15345bcae31dSMichal Kazior *
15355bcae31dSMichal Kazior * For practical reasons and code simplicity just do a single huge
15365bcae31dSMichal Kazior * switch.
15375bcae31dSMichal Kazior */
15385bcae31dSMichal Kazior
15395bcae31dSMichal Kazior /*
15405bcae31dSMichal Kazior * Verify if the reservation is still feasible.
15415bcae31dSMichal Kazior * - if it's not then disconnect
15425bcae31dSMichal Kazior * - if it is but not all vifs necessary are ready then defer
15435bcae31dSMichal Kazior */
15445bcae31dSMichal Kazior
15455bcae31dSMichal Kazior list_for_each_entry(ctx, &local->chanctx_list, list) {
1546b4f85443SJohannes Berg struct ieee80211_link_data *link;
1547b4f85443SJohannes Berg
15485bcae31dSMichal Kazior if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
15495bcae31dSMichal Kazior continue;
15505bcae31dSMichal Kazior
15515bcae31dSMichal Kazior if (WARN_ON(!ctx->replace_ctx)) {
15525bcae31dSMichal Kazior err = -EINVAL;
15535bcae31dSMichal Kazior goto err;
15545bcae31dSMichal Kazior }
15555bcae31dSMichal Kazior
15565bcae31dSMichal Kazior if (!local->use_chanctx)
15575bcae31dSMichal Kazior new_ctx = ctx;
15585bcae31dSMichal Kazior
15595bcae31dSMichal Kazior n_ctx++;
15605bcae31dSMichal Kazior
15615bcae31dSMichal Kazior n_assigned = 0;
15625bcae31dSMichal Kazior n_reserved = 0;
15635bcae31dSMichal Kazior n_ready = 0;
15645bcae31dSMichal Kazior
1565b4f85443SJohannes Berg list_for_each_entry(link, &ctx->replace_ctx->assigned_links,
15665bcae31dSMichal Kazior assigned_chanctx_list) {
15675bcae31dSMichal Kazior n_assigned++;
1568b4f85443SJohannes Berg if (link->reserved_chanctx) {
15695bcae31dSMichal Kazior n_reserved++;
1570b4f85443SJohannes Berg if (link->reserved_ready)
15715bcae31dSMichal Kazior n_ready++;
15725bcae31dSMichal Kazior }
15735bcae31dSMichal Kazior }
15745bcae31dSMichal Kazior
15755bcae31dSMichal Kazior if (n_assigned != n_reserved) {
15765bcae31dSMichal Kazior if (n_ready == n_reserved) {
15775bcae31dSMichal Kazior wiphy_info(local->hw.wiphy,
15785bcae31dSMichal Kazior "channel context reservation cannot be finalized because some interfaces aren't switching\n");
15795bcae31dSMichal Kazior err = -EBUSY;
15805bcae31dSMichal Kazior goto err;
15815bcae31dSMichal Kazior }
15825bcae31dSMichal Kazior
15835bcae31dSMichal Kazior return -EAGAIN;
15845bcae31dSMichal Kazior }
15855bcae31dSMichal Kazior
15865bcae31dSMichal Kazior ctx->conf.radar_enabled = false;
1587b4f85443SJohannes Berg list_for_each_entry(link, &ctx->reserved_links,
15885bcae31dSMichal Kazior reserved_chanctx_list) {
1589b4f85443SJohannes Berg if (ieee80211_link_has_in_place_reservation(link) &&
1590b4f85443SJohannes Berg !link->reserved_ready)
15915bcae31dSMichal Kazior return -EAGAIN;
15925bcae31dSMichal Kazior
1593b4f85443SJohannes Berg old_ctx = ieee80211_link_get_chanctx(link);
15945bcae31dSMichal Kazior if (old_ctx) {
15955bcae31dSMichal Kazior if (old_ctx->replace_state ==
15965bcae31dSMichal Kazior IEEE80211_CHANCTX_WILL_BE_REPLACED)
15975bcae31dSMichal Kazior n_vifs_switch++;
15985bcae31dSMichal Kazior else
15995bcae31dSMichal Kazior n_vifs_assign++;
16005bcae31dSMichal Kazior } else {
16015bcae31dSMichal Kazior n_vifs_ctxless++;
16025bcae31dSMichal Kazior }
16035bcae31dSMichal Kazior
1604b4f85443SJohannes Berg if (link->reserved_radar_required)
16055bcae31dSMichal Kazior ctx->conf.radar_enabled = true;
16065bcae31dSMichal Kazior }
16075bcae31dSMichal Kazior }
16085bcae31dSMichal Kazior
16095bcae31dSMichal Kazior if (WARN_ON(n_ctx == 0) ||
16105bcae31dSMichal Kazior WARN_ON(n_vifs_switch == 0 &&
16115bcae31dSMichal Kazior n_vifs_assign == 0 &&
16125bcae31dSMichal Kazior n_vifs_ctxless == 0) ||
16135bcae31dSMichal Kazior WARN_ON(n_ctx > 1 && !local->use_chanctx) ||
16145bcae31dSMichal Kazior WARN_ON(!new_ctx && !local->use_chanctx)) {
16155bcae31dSMichal Kazior err = -EINVAL;
16165bcae31dSMichal Kazior goto err;
16175bcae31dSMichal Kazior }
16185bcae31dSMichal Kazior
16195bcae31dSMichal Kazior /*
16205bcae31dSMichal Kazior * All necessary vifs are ready. Perform the switch now depending on
16215bcae31dSMichal Kazior * reservations and driver capabilities.
16225bcae31dSMichal Kazior */
16235bcae31dSMichal Kazior
16245bcae31dSMichal Kazior if (local->use_chanctx) {
16255bcae31dSMichal Kazior if (n_vifs_switch > 0) {
16265bcae31dSMichal Kazior err = ieee80211_chsw_switch_vifs(local, n_vifs_switch);
16275bcae31dSMichal Kazior if (err)
16285bcae31dSMichal Kazior goto err;
16295bcae31dSMichal Kazior }
16305bcae31dSMichal Kazior
16315bcae31dSMichal Kazior if (n_vifs_assign > 0 || n_vifs_ctxless > 0) {
16325bcae31dSMichal Kazior err = ieee80211_chsw_switch_ctxs(local);
16335bcae31dSMichal Kazior if (err)
16345bcae31dSMichal Kazior goto err;
16355bcae31dSMichal Kazior }
16365bcae31dSMichal Kazior } else {
16375bcae31dSMichal Kazior err = ieee80211_chsw_switch_hwconf(local, new_ctx);
16385bcae31dSMichal Kazior if (err)
16395bcae31dSMichal Kazior goto err;
16405bcae31dSMichal Kazior }
16415bcae31dSMichal Kazior
16425bcae31dSMichal Kazior /*
16435bcae31dSMichal Kazior * Update all structures, values and pointers to point to new channel
16445bcae31dSMichal Kazior * context(s).
16455bcae31dSMichal Kazior */
16465bcae31dSMichal Kazior list_for_each_entry(ctx, &local->chanctx_list, list) {
1647b4f85443SJohannes Berg struct ieee80211_link_data *link, *link_tmp;
1648b4f85443SJohannes Berg
16495bcae31dSMichal Kazior if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
16505bcae31dSMichal Kazior continue;
16515bcae31dSMichal Kazior
16525bcae31dSMichal Kazior if (WARN_ON(!ctx->replace_ctx)) {
16535bcae31dSMichal Kazior err = -EINVAL;
16545bcae31dSMichal Kazior goto err;
16555bcae31dSMichal Kazior }
16565bcae31dSMichal Kazior
1657b4f85443SJohannes Berg list_for_each_entry(link, &ctx->reserved_links,
16585bcae31dSMichal Kazior reserved_chanctx_list) {
1659b4f85443SJohannes Berg struct ieee80211_sub_if_data *sdata = link->sdata;
1660d8675a63SJohannes Berg struct ieee80211_bss_conf *link_conf = link->conf;
166115ddba5fSAnjaneyulu u64 changed = 0;
16625bcae31dSMichal Kazior
1663b4f85443SJohannes Berg if (!ieee80211_link_has_in_place_reservation(link))
16645bcae31dSMichal Kazior continue;
16655bcae31dSMichal Kazior
1666b4f85443SJohannes Berg rcu_assign_pointer(link_conf->chanctx_conf,
1667d0a9123eSJohannes Berg &ctx->conf);
16685bcae31dSMichal Kazior
16695bcae31dSMichal Kazior if (sdata->vif.type == NL80211_IFTYPE_AP)
1670b4f85443SJohannes Berg __ieee80211_link_copy_chanctx_to_vlans(link,
16715bcae31dSMichal Kazior false);
16725bcae31dSMichal Kazior
167317c18bf8SJohannes Berg ieee80211_check_fast_xmit_iface(sdata);
167417c18bf8SJohannes Berg
1675b4f85443SJohannes Berg link->radar_required = link->reserved_radar_required;
16765bcae31dSMichal Kazior
1677b4f85443SJohannes Berg if (link_conf->chandef.width != link->reserved_chandef.width)
16785bcae31dSMichal Kazior changed = BSS_CHANGED_BANDWIDTH;
16795bcae31dSMichal Kazior
1680b4f85443SJohannes Berg ieee80211_link_update_chandef(link, &link->reserved_chandef);
16815bcae31dSMichal Kazior if (changed)
1682b4f85443SJohannes Berg ieee80211_link_info_change_notify(sdata,
1683d8675a63SJohannes Berg link,
16845bcae31dSMichal Kazior changed);
16855bcae31dSMichal Kazior
1686db82d8a9SLorenzo Bianconi ieee80211_recalc_txpower(sdata, false);
16875bcae31dSMichal Kazior }
168811335a55SLuciano Coelho
168911335a55SLuciano Coelho ieee80211_recalc_chanctx_chantype(local, ctx);
169011335a55SLuciano Coelho ieee80211_recalc_smps_chanctx(local, ctx);
169111335a55SLuciano Coelho ieee80211_recalc_radar_chanctx(local, ctx);
1692b72a455aSJohannes Berg ieee80211_recalc_chanctx_min_def(local, ctx, NULL);
16935bcae31dSMichal Kazior
1694b4f85443SJohannes Berg list_for_each_entry_safe(link, link_tmp, &ctx->reserved_links,
16955bcae31dSMichal Kazior reserved_chanctx_list) {
1696b4f85443SJohannes Berg if (ieee80211_link_get_chanctx(link) != ctx)
16975bcae31dSMichal Kazior continue;
16985bcae31dSMichal Kazior
1699b4f85443SJohannes Berg list_del(&link->reserved_chanctx_list);
1700b4f85443SJohannes Berg list_move(&link->assigned_chanctx_list,
1701b4f85443SJohannes Berg &ctx->assigned_links);
1702b4f85443SJohannes Berg link->reserved_chanctx = NULL;
170303078de4SMichal Kazior
1704b4f85443SJohannes Berg ieee80211_link_chanctx_reservation_complete(link);
17055bcae31dSMichal Kazior }
17065bcae31dSMichal Kazior
17075bcae31dSMichal Kazior /*
17085bcae31dSMichal Kazior * This context might have been a dependency for an already
17095bcae31dSMichal Kazior * ready re-assign reservation interface that was deferred. Do
17105bcae31dSMichal Kazior * not propagate error to the caller though. The in-place
17115bcae31dSMichal Kazior * reservation for originally requested interface has already
17125bcae31dSMichal Kazior * succeeded at this point.
17135bcae31dSMichal Kazior */
1714b4f85443SJohannes Berg list_for_each_entry_safe(link, link_tmp, &ctx->reserved_links,
17155bcae31dSMichal Kazior reserved_chanctx_list) {
1716b4f85443SJohannes Berg if (WARN_ON(ieee80211_link_has_in_place_reservation(link)))
17175bcae31dSMichal Kazior continue;
17185bcae31dSMichal Kazior
1719b4f85443SJohannes Berg if (WARN_ON(link->reserved_chanctx != ctx))
17205bcae31dSMichal Kazior continue;
17215bcae31dSMichal Kazior
1722b4f85443SJohannes Berg if (!link->reserved_ready)
17235bcae31dSMichal Kazior continue;
17245bcae31dSMichal Kazior
1725b4f85443SJohannes Berg if (ieee80211_link_get_chanctx(link))
1726b4f85443SJohannes Berg err = ieee80211_link_use_reserved_reassign(link);
17275bcae31dSMichal Kazior else
1728b4f85443SJohannes Berg err = ieee80211_link_use_reserved_assign(link);
17295bcae31dSMichal Kazior
17305bcae31dSMichal Kazior if (err) {
1731b4f85443SJohannes Berg link_info(link,
17325bcae31dSMichal Kazior "failed to finalize (re-)assign reservation (err=%d)\n",
17335bcae31dSMichal Kazior err);
1734b4f85443SJohannes Berg ieee80211_link_unreserve_chanctx(link);
17355bcae31dSMichal Kazior cfg80211_stop_iface(local->hw.wiphy,
1736b4f85443SJohannes Berg &link->sdata->wdev,
17375bcae31dSMichal Kazior GFP_KERNEL);
17385bcae31dSMichal Kazior }
17395bcae31dSMichal Kazior }
17405bcae31dSMichal Kazior }
17415bcae31dSMichal Kazior
17425bcae31dSMichal Kazior /*
17435bcae31dSMichal Kazior * Finally free old contexts
17445bcae31dSMichal Kazior */
17455bcae31dSMichal Kazior
17465bcae31dSMichal Kazior list_for_each_entry_safe(ctx, ctx_tmp, &local->chanctx_list, list) {
17475bcae31dSMichal Kazior if (ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
17485bcae31dSMichal Kazior continue;
17495bcae31dSMichal Kazior
17505bcae31dSMichal Kazior ctx->replace_ctx->replace_ctx = NULL;
17515bcae31dSMichal Kazior ctx->replace_ctx->replace_state =
17525bcae31dSMichal Kazior IEEE80211_CHANCTX_REPLACE_NONE;
17535bcae31dSMichal Kazior
17545bcae31dSMichal Kazior list_del_rcu(&ctx->list);
17555bcae31dSMichal Kazior kfree_rcu(ctx, rcu_head);
17565bcae31dSMichal Kazior }
17575bcae31dSMichal Kazior
17585bcae31dSMichal Kazior return 0;
17595bcae31dSMichal Kazior
17605bcae31dSMichal Kazior err:
17615bcae31dSMichal Kazior list_for_each_entry(ctx, &local->chanctx_list, list) {
1762b4f85443SJohannes Berg struct ieee80211_link_data *link, *link_tmp;
1763b4f85443SJohannes Berg
17645bcae31dSMichal Kazior if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
17655bcae31dSMichal Kazior continue;
17665bcae31dSMichal Kazior
1767b4f85443SJohannes Berg list_for_each_entry_safe(link, link_tmp, &ctx->reserved_links,
176803078de4SMichal Kazior reserved_chanctx_list) {
1769b4f85443SJohannes Berg ieee80211_link_unreserve_chanctx(link);
1770b4f85443SJohannes Berg ieee80211_link_chanctx_reservation_complete(link);
177103078de4SMichal Kazior }
17725bcae31dSMichal Kazior }
17735bcae31dSMichal Kazior
17745bcae31dSMichal Kazior return err;
17755bcae31dSMichal Kazior }
17765bcae31dSMichal Kazior
__ieee80211_link_release_channel(struct ieee80211_link_data * link)1777b4f85443SJohannes Berg static void __ieee80211_link_release_channel(struct ieee80211_link_data *link)
1778649b2a4dSJohannes Berg {
1779b4f85443SJohannes Berg struct ieee80211_sub_if_data *sdata = link->sdata;
1780d8675a63SJohannes Berg struct ieee80211_bss_conf *link_conf = link->conf;
1781649b2a4dSJohannes Berg struct ieee80211_local *local = sdata->local;
1782649b2a4dSJohannes Berg struct ieee80211_chanctx_conf *conf;
1783649b2a4dSJohannes Berg struct ieee80211_chanctx *ctx;
1784649b2a4dSJohannes Berg bool use_reserved_switch = false;
1785649b2a4dSJohannes Berg
1786649b2a4dSJohannes Berg lockdep_assert_held(&local->chanctx_mtx);
1787649b2a4dSJohannes Berg
1788b4f85443SJohannes Berg conf = rcu_dereference_protected(link_conf->chanctx_conf,
1789649b2a4dSJohannes Berg lockdep_is_held(&local->chanctx_mtx));
1790649b2a4dSJohannes Berg if (!conf)
1791649b2a4dSJohannes Berg return;
1792649b2a4dSJohannes Berg
1793649b2a4dSJohannes Berg ctx = container_of(conf, struct ieee80211_chanctx, conf);
1794649b2a4dSJohannes Berg
1795b4f85443SJohannes Berg if (link->reserved_chanctx) {
1796b4f85443SJohannes Berg if (link->reserved_chanctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER &&
1797b4f85443SJohannes Berg ieee80211_chanctx_num_reserved(local, link->reserved_chanctx) > 1)
1798649b2a4dSJohannes Berg use_reserved_switch = true;
1799649b2a4dSJohannes Berg
1800b4f85443SJohannes Berg ieee80211_link_unreserve_chanctx(link);
1801649b2a4dSJohannes Berg }
1802649b2a4dSJohannes Berg
1803b4f85443SJohannes Berg ieee80211_assign_link_chanctx(link, NULL);
1804649b2a4dSJohannes Berg if (ieee80211_chanctx_refcount(local, ctx) == 0)
1805649b2a4dSJohannes Berg ieee80211_free_chanctx(local, ctx);
1806649b2a4dSJohannes Berg
1807b4f85443SJohannes Berg link->radar_required = false;
1808104f5a62SEliad Peller
1809649b2a4dSJohannes Berg /* Unreserving may ready an in-place reservation. */
1810649b2a4dSJohannes Berg if (use_reserved_switch)
1811649b2a4dSJohannes Berg ieee80211_vif_use_reserved_switch(local);
1812649b2a4dSJohannes Berg }
1813649b2a4dSJohannes Berg
ieee80211_link_use_channel(struct ieee80211_link_data * link,const struct cfg80211_chan_def * chandef,enum ieee80211_chanctx_mode mode)1814b4f85443SJohannes Berg int ieee80211_link_use_channel(struct ieee80211_link_data *link,
1815649b2a4dSJohannes Berg const struct cfg80211_chan_def *chandef,
1816649b2a4dSJohannes Berg enum ieee80211_chanctx_mode mode)
1817649b2a4dSJohannes Berg {
1818b4f85443SJohannes Berg struct ieee80211_sub_if_data *sdata = link->sdata;
1819649b2a4dSJohannes Berg struct ieee80211_local *local = sdata->local;
1820649b2a4dSJohannes Berg struct ieee80211_chanctx *ctx;
1821649b2a4dSJohannes Berg u8 radar_detect_width = 0;
1822649b2a4dSJohannes Berg int ret;
1823649b2a4dSJohannes Berg
1824649b2a4dSJohannes Berg lockdep_assert_held(&local->mtx);
1825649b2a4dSJohannes Berg
1826efe9c2bfSJohannes Berg if (sdata->vif.active_links &&
1827efe9c2bfSJohannes Berg !(sdata->vif.active_links & BIT(link->link_id))) {
1828efe9c2bfSJohannes Berg ieee80211_link_update_chandef(link, chandef);
1829efe9c2bfSJohannes Berg return 0;
1830efe9c2bfSJohannes Berg }
1831efe9c2bfSJohannes Berg
1832649b2a4dSJohannes Berg mutex_lock(&local->chanctx_mtx);
1833649b2a4dSJohannes Berg
1834649b2a4dSJohannes Berg ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
1835649b2a4dSJohannes Berg chandef,
1836649b2a4dSJohannes Berg sdata->wdev.iftype);
1837649b2a4dSJohannes Berg if (ret < 0)
1838649b2a4dSJohannes Berg goto out;
1839649b2a4dSJohannes Berg if (ret > 0)
1840649b2a4dSJohannes Berg radar_detect_width = BIT(chandef->width);
1841649b2a4dSJohannes Berg
1842d8675a63SJohannes Berg link->radar_required = ret;
1843649b2a4dSJohannes Berg
1844649b2a4dSJohannes Berg ret = ieee80211_check_combinations(sdata, chandef, mode,
1845649b2a4dSJohannes Berg radar_detect_width);
1846649b2a4dSJohannes Berg if (ret < 0)
1847649b2a4dSJohannes Berg goto out;
1848649b2a4dSJohannes Berg
1849b4f85443SJohannes Berg __ieee80211_link_release_channel(link);
1850649b2a4dSJohannes Berg
1851649b2a4dSJohannes Berg ctx = ieee80211_find_chanctx(local, chandef, mode);
1852649b2a4dSJohannes Berg if (!ctx)
1853649b2a4dSJohannes Berg ctx = ieee80211_new_chanctx(local, chandef, mode);
1854649b2a4dSJohannes Berg if (IS_ERR(ctx)) {
1855649b2a4dSJohannes Berg ret = PTR_ERR(ctx);
1856649b2a4dSJohannes Berg goto out;
1857649b2a4dSJohannes Berg }
1858649b2a4dSJohannes Berg
1859b4f85443SJohannes Berg ieee80211_link_update_chandef(link, chandef);
1860649b2a4dSJohannes Berg
1861b4f85443SJohannes Berg ret = ieee80211_assign_link_chanctx(link, ctx);
1862649b2a4dSJohannes Berg if (ret) {
1863649b2a4dSJohannes Berg /* if assign fails refcount stays the same */
1864649b2a4dSJohannes Berg if (ieee80211_chanctx_refcount(local, ctx) == 0)
1865649b2a4dSJohannes Berg ieee80211_free_chanctx(local, ctx);
1866649b2a4dSJohannes Berg goto out;
1867649b2a4dSJohannes Berg }
1868649b2a4dSJohannes Berg
1869649b2a4dSJohannes Berg ieee80211_recalc_smps_chanctx(local, ctx);
1870649b2a4dSJohannes Berg ieee80211_recalc_radar_chanctx(local, ctx);
1871649b2a4dSJohannes Berg out:
1872104f5a62SEliad Peller if (ret)
1873b4f85443SJohannes Berg link->radar_required = false;
1874104f5a62SEliad Peller
1875649b2a4dSJohannes Berg mutex_unlock(&local->chanctx_mtx);
1876649b2a4dSJohannes Berg return ret;
1877649b2a4dSJohannes Berg }
1878649b2a4dSJohannes Berg
ieee80211_link_use_reserved_context(struct ieee80211_link_data * link)1879b4f85443SJohannes Berg int ieee80211_link_use_reserved_context(struct ieee80211_link_data *link)
18805bcae31dSMichal Kazior {
1881b4f85443SJohannes Berg struct ieee80211_sub_if_data *sdata = link->sdata;
18825bcae31dSMichal Kazior struct ieee80211_local *local = sdata->local;
18835bcae31dSMichal Kazior struct ieee80211_chanctx *new_ctx;
18845bcae31dSMichal Kazior struct ieee80211_chanctx *old_ctx;
18855bcae31dSMichal Kazior int err;
18865bcae31dSMichal Kazior
18875bcae31dSMichal Kazior lockdep_assert_held(&local->mtx);
18885bcae31dSMichal Kazior lockdep_assert_held(&local->chanctx_mtx);
18895bcae31dSMichal Kazior
1890b4f85443SJohannes Berg new_ctx = link->reserved_chanctx;
1891b4f85443SJohannes Berg old_ctx = ieee80211_link_get_chanctx(link);
18925bcae31dSMichal Kazior
18935bcae31dSMichal Kazior if (WARN_ON(!new_ctx))
18945bcae31dSMichal Kazior return -EINVAL;
18955bcae31dSMichal Kazior
18965bcae31dSMichal Kazior if (WARN_ON(new_ctx->replace_state ==
18975bcae31dSMichal Kazior IEEE80211_CHANCTX_WILL_BE_REPLACED))
18985bcae31dSMichal Kazior return -EINVAL;
18995bcae31dSMichal Kazior
1900b4f85443SJohannes Berg if (WARN_ON(link->reserved_ready))
19015bcae31dSMichal Kazior return -EINVAL;
19025bcae31dSMichal Kazior
1903b4f85443SJohannes Berg link->reserved_ready = true;
19045bcae31dSMichal Kazior
19055bcae31dSMichal Kazior if (new_ctx->replace_state == IEEE80211_CHANCTX_REPLACE_NONE) {
19065bcae31dSMichal Kazior if (old_ctx)
1907b4f85443SJohannes Berg return ieee80211_link_use_reserved_reassign(link);
19085bcae31dSMichal Kazior
1909b4f85443SJohannes Berg return ieee80211_link_use_reserved_assign(link);
19105bcae31dSMichal Kazior }
19115bcae31dSMichal Kazior
19125bcae31dSMichal Kazior /*
19135bcae31dSMichal Kazior * In-place reservation may need to be finalized now either if:
19145bcae31dSMichal Kazior * a) sdata is taking part in the swapping itself and is the last one
19155bcae31dSMichal Kazior * b) sdata has switched with a re-assign reservation to an existing
19165bcae31dSMichal Kazior * context readying in-place switching of old_ctx
19175bcae31dSMichal Kazior *
19185bcae31dSMichal Kazior * In case of (b) do not propagate the error up because the requested
19195bcae31dSMichal Kazior * sdata already switched successfully. Just spill an extra warning.
19205bcae31dSMichal Kazior * The ieee80211_vif_use_reserved_switch() already stops all necessary
19215bcae31dSMichal Kazior * interfaces upon failure.
19225bcae31dSMichal Kazior */
19235bcae31dSMichal Kazior if ((old_ctx &&
19245bcae31dSMichal Kazior old_ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
19255bcae31dSMichal Kazior new_ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
19265bcae31dSMichal Kazior err = ieee80211_vif_use_reserved_switch(local);
19275bcae31dSMichal Kazior if (err && err != -EAGAIN) {
19285bcae31dSMichal Kazior if (new_ctx->replace_state ==
19295bcae31dSMichal Kazior IEEE80211_CHANCTX_REPLACES_OTHER)
19305bcae31dSMichal Kazior return err;
19315bcae31dSMichal Kazior
19325bcae31dSMichal Kazior wiphy_info(local->hw.wiphy,
19335bcae31dSMichal Kazior "depending in-place reservation failed (err=%d)\n",
19345bcae31dSMichal Kazior err);
19355bcae31dSMichal Kazior }
19365bcae31dSMichal Kazior }
19375bcae31dSMichal Kazior
19385bcae31dSMichal Kazior return 0;
193973da7d5bSSimon Wunderlich }
194073da7d5bSSimon Wunderlich
ieee80211_link_change_bandwidth(struct ieee80211_link_data * link,const struct cfg80211_chan_def * chandef,u64 * changed)1941b4f85443SJohannes Berg int ieee80211_link_change_bandwidth(struct ieee80211_link_data *link,
19422c9b7359SJohannes Berg const struct cfg80211_chan_def *chandef,
1943aa87cd8bSJohannes Berg u64 *changed)
19442c9b7359SJohannes Berg {
1945b4f85443SJohannes Berg struct ieee80211_sub_if_data *sdata = link->sdata;
1946d8675a63SJohannes Berg struct ieee80211_bss_conf *link_conf = link->conf;
19472c9b7359SJohannes Berg struct ieee80211_local *local = sdata->local;
19482c9b7359SJohannes Berg struct ieee80211_chanctx_conf *conf;
19492c9b7359SJohannes Berg struct ieee80211_chanctx *ctx;
19505bcae31dSMichal Kazior const struct cfg80211_chan_def *compat;
19512c9b7359SJohannes Berg int ret;
19522c9b7359SJohannes Berg
19532c9b7359SJohannes Berg if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
19542c9b7359SJohannes Berg IEEE80211_CHAN_DISABLED))
19552c9b7359SJohannes Berg return -EINVAL;
19562c9b7359SJohannes Berg
19572c9b7359SJohannes Berg mutex_lock(&local->chanctx_mtx);
1958b4f85443SJohannes Berg if (cfg80211_chandef_identical(chandef, &link_conf->chandef)) {
19592c9b7359SJohannes Berg ret = 0;
19602c9b7359SJohannes Berg goto out;
19612c9b7359SJohannes Berg }
19622c9b7359SJohannes Berg
19632c9b7359SJohannes Berg if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT ||
1964b4f85443SJohannes Berg link_conf->chandef.width == NL80211_CHAN_WIDTH_20_NOHT) {
19652c9b7359SJohannes Berg ret = -EINVAL;
19662c9b7359SJohannes Berg goto out;
19672c9b7359SJohannes Berg }
19682c9b7359SJohannes Berg
1969b4f85443SJohannes Berg conf = rcu_dereference_protected(link_conf->chanctx_conf,
19702c9b7359SJohannes Berg lockdep_is_held(&local->chanctx_mtx));
19712c9b7359SJohannes Berg if (!conf) {
19722c9b7359SJohannes Berg ret = -EINVAL;
19732c9b7359SJohannes Berg goto out;
19742c9b7359SJohannes Berg }
19752c9b7359SJohannes Berg
19762c9b7359SJohannes Berg ctx = container_of(conf, struct ieee80211_chanctx, conf);
19775bcae31dSMichal Kazior
19785bcae31dSMichal Kazior compat = cfg80211_chandef_compatible(&conf->def, chandef);
19795bcae31dSMichal Kazior if (!compat) {
19802c9b7359SJohannes Berg ret = -EINVAL;
19812c9b7359SJohannes Berg goto out;
19822c9b7359SJohannes Berg }
19832c9b7359SJohannes Berg
19845bcae31dSMichal Kazior switch (ctx->replace_state) {
19855bcae31dSMichal Kazior case IEEE80211_CHANCTX_REPLACE_NONE:
19865bcae31dSMichal Kazior if (!ieee80211_chanctx_reserved_chandef(local, ctx, compat)) {
19875bcae31dSMichal Kazior ret = -EBUSY;
19885bcae31dSMichal Kazior goto out;
19895bcae31dSMichal Kazior }
19905bcae31dSMichal Kazior break;
19915bcae31dSMichal Kazior case IEEE80211_CHANCTX_WILL_BE_REPLACED:
1992d070f913SStephen Hemminger /* TODO: Perhaps the bandwidth change could be treated as a
19935bcae31dSMichal Kazior * reservation itself? */
19945bcae31dSMichal Kazior ret = -EBUSY;
19955bcae31dSMichal Kazior goto out;
19965bcae31dSMichal Kazior case IEEE80211_CHANCTX_REPLACES_OTHER:
19975bcae31dSMichal Kazior /* channel context that is going to replace another channel
19985bcae31dSMichal Kazior * context doesn't really exist and shouldn't be assigned
19995bcae31dSMichal Kazior * anywhere yet */
20005bcae31dSMichal Kazior WARN_ON(1);
20015bcae31dSMichal Kazior break;
20025bcae31dSMichal Kazior }
20035bcae31dSMichal Kazior
2004b4f85443SJohannes Berg ieee80211_link_update_chandef(link, chandef);
20052c9b7359SJohannes Berg
20062c9b7359SJohannes Berg ieee80211_recalc_chanctx_chantype(local, ctx);
20072c9b7359SJohannes Berg
20082c9b7359SJohannes Berg *changed |= BSS_CHANGED_BANDWIDTH;
20092c9b7359SJohannes Berg ret = 0;
20102c9b7359SJohannes Berg out:
20112c9b7359SJohannes Berg mutex_unlock(&local->chanctx_mtx);
20122c9b7359SJohannes Berg return ret;
20132c9b7359SJohannes Berg }
20142c9b7359SJohannes Berg
ieee80211_link_release_channel(struct ieee80211_link_data * link)2015b4f85443SJohannes Berg void ieee80211_link_release_channel(struct ieee80211_link_data *link)
2016d01a1e65SMichal Kazior {
2017b4f85443SJohannes Berg struct ieee80211_sub_if_data *sdata = link->sdata;
2018b4f85443SJohannes Berg
2019d01a1e65SMichal Kazior mutex_lock(&sdata->local->chanctx_mtx);
2020939c4c7eSJohannes Berg if (rcu_access_pointer(link->conf->chanctx_conf)) {
2021939c4c7eSJohannes Berg lockdep_assert_held(&sdata->local->mtx);
2022b4f85443SJohannes Berg __ieee80211_link_release_channel(link);
2023939c4c7eSJohannes Berg }
2024d01a1e65SMichal Kazior mutex_unlock(&sdata->local->chanctx_mtx);
2025d01a1e65SMichal Kazior }
20263448c005SJohannes Berg
ieee80211_link_vlan_copy_chanctx(struct ieee80211_link_data * link)2027b4f85443SJohannes Berg void ieee80211_link_vlan_copy_chanctx(struct ieee80211_link_data *link)
20284d76d21bSJohannes Berg {
2029b4f85443SJohannes Berg struct ieee80211_sub_if_data *sdata = link->sdata;
2030b4f85443SJohannes Berg unsigned int link_id = link->link_id;
2031d8675a63SJohannes Berg struct ieee80211_bss_conf *link_conf = link->conf;
2032d8675a63SJohannes Berg struct ieee80211_bss_conf *ap_conf;
20334d76d21bSJohannes Berg struct ieee80211_local *local = sdata->local;
20344d76d21bSJohannes Berg struct ieee80211_sub_if_data *ap;
20354d76d21bSJohannes Berg struct ieee80211_chanctx_conf *conf;
20364d76d21bSJohannes Berg
20374d76d21bSJohannes Berg if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
20384d76d21bSJohannes Berg return;
20394d76d21bSJohannes Berg
20404d76d21bSJohannes Berg ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
20414d76d21bSJohannes Berg
20424d76d21bSJohannes Berg mutex_lock(&local->chanctx_mtx);
20434d76d21bSJohannes Berg
2044d8675a63SJohannes Berg rcu_read_lock();
2045d8675a63SJohannes Berg ap_conf = rcu_dereference(ap->vif.link_conf[link_id]);
2046d8675a63SJohannes Berg conf = rcu_dereference_protected(ap_conf->chanctx_conf,
20474d76d21bSJohannes Berg lockdep_is_held(&local->chanctx_mtx));
2048b4f85443SJohannes Berg rcu_assign_pointer(link_conf->chanctx_conf, conf);
2049d8675a63SJohannes Berg rcu_read_unlock();
20504d76d21bSJohannes Berg mutex_unlock(&local->chanctx_mtx);
20514d76d21bSJohannes Berg }
20524d76d21bSJohannes Berg
ieee80211_iter_chan_contexts_atomic(struct ieee80211_hw * hw,void (* iter)(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * chanctx_conf,void * data),void * iter_data)20533448c005SJohannes Berg void ieee80211_iter_chan_contexts_atomic(
20543448c005SJohannes Berg struct ieee80211_hw *hw,
20553448c005SJohannes Berg void (*iter)(struct ieee80211_hw *hw,
20563448c005SJohannes Berg struct ieee80211_chanctx_conf *chanctx_conf,
20573448c005SJohannes Berg void *data),
20583448c005SJohannes Berg void *iter_data)
20593448c005SJohannes Berg {
20603448c005SJohannes Berg struct ieee80211_local *local = hw_to_local(hw);
20613448c005SJohannes Berg struct ieee80211_chanctx *ctx;
20623448c005SJohannes Berg
20633448c005SJohannes Berg rcu_read_lock();
20643448c005SJohannes Berg list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
20658a61af65SJohannes Berg if (ctx->driver_present)
20663448c005SJohannes Berg iter(hw, &ctx->conf, iter_data);
20673448c005SJohannes Berg rcu_read_unlock();
20683448c005SJohannes Berg }
20693448c005SJohannes Berg EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);
2070