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