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