GNU Linux-libre 4.19.314-gnu1
[releases.git] / net / mac80211 / sta_info.c
1 /*
2  * Copyright 2002-2005, Instant802 Networks, Inc.
3  * Copyright 2006-2007  Jiri Benc <jbenc@suse.cz>
4  * Copyright 2013-2014  Intel Mobile Communications GmbH
5  * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
6  * Copyright (C) 2018-2021 Intel Corporation
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/etherdevice.h>
16 #include <linux/netdevice.h>
17 #include <linux/types.h>
18 #include <linux/slab.h>
19 #include <linux/skbuff.h>
20 #include <linux/if_arp.h>
21 #include <linux/timer.h>
22 #include <linux/rtnetlink.h>
23
24 #include <net/codel.h>
25 #include <net/mac80211.h>
26 #include "ieee80211_i.h"
27 #include "driver-ops.h"
28 #include "rate.h"
29 #include "sta_info.h"
30 #include "debugfs_sta.h"
31 #include "mesh.h"
32 #include "wme.h"
33
34 /**
35  * DOC: STA information lifetime rules
36  *
37  * STA info structures (&struct sta_info) are managed in a hash table
38  * for faster lookup and a list for iteration. They are managed using
39  * RCU, i.e. access to the list and hash table is protected by RCU.
40  *
41  * Upon allocating a STA info structure with sta_info_alloc(), the caller
42  * owns that structure. It must then insert it into the hash table using
43  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
44  * case (which acquires an rcu read section but must not be called from
45  * within one) will the pointer still be valid after the call. Note that
46  * the caller may not do much with the STA info before inserting it, in
47  * particular, it may not start any mesh peer link management or add
48  * encryption keys.
49  *
50  * When the insertion fails (sta_info_insert()) returns non-zero), the
51  * structure will have been freed by sta_info_insert()!
52  *
53  * Station entries are added by mac80211 when you establish a link with a
54  * peer. This means different things for the different type of interfaces
55  * we support. For a regular station this mean we add the AP sta when we
56  * receive an association response from the AP. For IBSS this occurs when
57  * get to know about a peer on the same IBSS. For WDS we add the sta for
58  * the peer immediately upon device open. When using AP mode we add stations
59  * for each respective station upon request from userspace through nl80211.
60  *
61  * In order to remove a STA info structure, various sta_info_destroy_*()
62  * calls are available.
63  *
64  * There is no concept of ownership on a STA entry, each structure is
65  * owned by the global hash table/list until it is removed. All users of
66  * the structure need to be RCU protected so that the structure won't be
67  * freed before they are done using it.
68  */
69
70 static const struct rhashtable_params sta_rht_params = {
71         .nelem_hint = 3, /* start small */
72         .automatic_shrinking = true,
73         .head_offset = offsetof(struct sta_info, hash_node),
74         .key_offset = offsetof(struct sta_info, addr),
75         .key_len = ETH_ALEN,
76         .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
77 };
78
79 /* Caller must hold local->sta_mtx */
80 static int sta_info_hash_del(struct ieee80211_local *local,
81                              struct sta_info *sta)
82 {
83         return rhltable_remove(&local->sta_hash, &sta->hash_node,
84                                sta_rht_params);
85 }
86
87 static void __cleanup_single_sta(struct sta_info *sta)
88 {
89         int ac, i;
90         struct tid_ampdu_tx *tid_tx;
91         struct ieee80211_sub_if_data *sdata = sta->sdata;
92         struct ieee80211_local *local = sdata->local;
93         struct fq *fq = &local->fq;
94         struct ps_data *ps;
95
96         if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
97             test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
98             test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
99                 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
100                     sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
101                         ps = &sdata->bss->ps;
102                 else if (ieee80211_vif_is_mesh(&sdata->vif))
103                         ps = &sdata->u.mesh.ps;
104                 else
105                         return;
106
107                 clear_sta_flag(sta, WLAN_STA_PS_STA);
108                 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
109                 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
110
111                 atomic_dec(&ps->num_sta_ps);
112         }
113
114         if (sta->sta.txq[0]) {
115                 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
116                         struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
117
118                         spin_lock_bh(&fq->lock);
119                         ieee80211_txq_purge(local, txqi);
120                         spin_unlock_bh(&fq->lock);
121                 }
122         }
123
124         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
125                 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
126                 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
127                 ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
128         }
129
130         if (ieee80211_vif_is_mesh(&sdata->vif))
131                 mesh_sta_cleanup(sta);
132
133         cancel_work_sync(&sta->drv_deliver_wk);
134
135         /*
136          * Destroy aggregation state here. It would be nice to wait for the
137          * driver to finish aggregation stop and then clean up, but for now
138          * drivers have to handle aggregation stop being requested, followed
139          * directly by station destruction.
140          */
141         for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
142                 kfree(sta->ampdu_mlme.tid_start_tx[i]);
143                 tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
144                 if (!tid_tx)
145                         continue;
146                 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
147                 kfree(tid_tx);
148         }
149 }
150
151 static void cleanup_single_sta(struct sta_info *sta)
152 {
153         struct ieee80211_sub_if_data *sdata = sta->sdata;
154         struct ieee80211_local *local = sdata->local;
155
156         __cleanup_single_sta(sta);
157         sta_info_free(local, sta);
158 }
159
160 struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local,
161                                          const u8 *addr)
162 {
163         return rhltable_lookup(&local->sta_hash, addr, sta_rht_params);
164 }
165
166 /* protected by RCU */
167 struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
168                               const u8 *addr)
169 {
170         struct ieee80211_local *local = sdata->local;
171         struct rhlist_head *tmp;
172         struct sta_info *sta;
173
174         rcu_read_lock();
175         for_each_sta_info(local, addr, sta, tmp) {
176                 if (sta->sdata == sdata) {
177                         rcu_read_unlock();
178                         /* this is safe as the caller must already hold
179                          * another rcu read section or the mutex
180                          */
181                         return sta;
182                 }
183         }
184         rcu_read_unlock();
185         return NULL;
186 }
187
188 /*
189  * Get sta info either from the specified interface
190  * or from one of its vlans
191  */
192 struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
193                                   const u8 *addr)
194 {
195         struct ieee80211_local *local = sdata->local;
196         struct rhlist_head *tmp;
197         struct sta_info *sta;
198
199         rcu_read_lock();
200         for_each_sta_info(local, addr, sta, tmp) {
201                 if (sta->sdata == sdata ||
202                     (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
203                         rcu_read_unlock();
204                         /* this is safe as the caller must already hold
205                          * another rcu read section or the mutex
206                          */
207                         return sta;
208                 }
209         }
210         rcu_read_unlock();
211         return NULL;
212 }
213
214 struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
215                                      int idx)
216 {
217         struct ieee80211_local *local = sdata->local;
218         struct sta_info *sta;
219         int i = 0;
220
221         list_for_each_entry_rcu(sta, &local->sta_list, list) {
222                 if (sdata != sta->sdata)
223                         continue;
224                 if (i < idx) {
225                         ++i;
226                         continue;
227                 }
228                 return sta;
229         }
230
231         return NULL;
232 }
233
234 /**
235  * sta_info_free - free STA
236  *
237  * @local: pointer to the global information
238  * @sta: STA info to free
239  *
240  * This function must undo everything done by sta_info_alloc()
241  * that may happen before sta_info_insert(). It may only be
242  * called when sta_info_insert() has not been attempted (and
243  * if that fails, the station is freed anyway.)
244  */
245 void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
246 {
247         /*
248          * If we had used sta_info_pre_move_state() then we might not
249          * have gone through the state transitions down again, so do
250          * it here now (and warn if it's inserted).
251          *
252          * This will clear state such as fast TX/RX that may have been
253          * allocated during state transitions.
254          */
255         while (sta->sta_state > IEEE80211_STA_NONE) {
256                 int ret;
257
258                 WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED));
259
260                 ret = sta_info_move_state(sta, sta->sta_state - 1);
261                 if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret))
262                         break;
263         }
264
265         if (sta->rate_ctrl)
266                 rate_control_free_sta(sta);
267
268         sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
269
270         if (sta->sta.txq[0])
271                 kfree(to_txq_info(sta->sta.txq[0]));
272         kfree(rcu_dereference_raw(sta->sta.rates));
273 #ifdef CONFIG_MAC80211_MESH
274         kfree(sta->mesh);
275 #endif
276         free_percpu(sta->pcpu_rx_stats);
277         kfree(sta);
278 }
279
280 /* Caller must hold local->sta_mtx */
281 static int sta_info_hash_add(struct ieee80211_local *local,
282                              struct sta_info *sta)
283 {
284         return rhltable_insert(&local->sta_hash, &sta->hash_node,
285                                sta_rht_params);
286 }
287
288 static void sta_deliver_ps_frames(struct work_struct *wk)
289 {
290         struct sta_info *sta;
291
292         sta = container_of(wk, struct sta_info, drv_deliver_wk);
293
294         if (sta->dead)
295                 return;
296
297         local_bh_disable();
298         if (!test_sta_flag(sta, WLAN_STA_PS_STA))
299                 ieee80211_sta_ps_deliver_wakeup(sta);
300         else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
301                 ieee80211_sta_ps_deliver_poll_response(sta);
302         else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
303                 ieee80211_sta_ps_deliver_uapsd(sta);
304         local_bh_enable();
305 }
306
307 static int sta_prepare_rate_control(struct ieee80211_local *local,
308                                     struct sta_info *sta, gfp_t gfp)
309 {
310         if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
311                 return 0;
312
313         sta->rate_ctrl = local->rate_ctrl;
314         sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
315                                                      sta, gfp);
316         if (!sta->rate_ctrl_priv)
317                 return -ENOMEM;
318
319         return 0;
320 }
321
322 struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
323                                 const u8 *addr, gfp_t gfp)
324 {
325         struct ieee80211_local *local = sdata->local;
326         struct ieee80211_hw *hw = &local->hw;
327         struct sta_info *sta;
328         int i;
329
330         sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
331         if (!sta)
332                 return NULL;
333
334         if (ieee80211_hw_check(hw, USES_RSS)) {
335                 sta->pcpu_rx_stats =
336                         alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
337                 if (!sta->pcpu_rx_stats)
338                         goto free;
339         }
340
341         spin_lock_init(&sta->lock);
342         spin_lock_init(&sta->ps_lock);
343         INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
344         INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
345         mutex_init(&sta->ampdu_mlme.mtx);
346 #ifdef CONFIG_MAC80211_MESH
347         if (ieee80211_vif_is_mesh(&sdata->vif)) {
348                 sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
349                 if (!sta->mesh)
350                         goto free;
351                 sta->mesh->plink_sta = sta;
352                 spin_lock_init(&sta->mesh->plink_lock);
353                 if (ieee80211_vif_is_mesh(&sdata->vif) &&
354                     !sdata->u.mesh.user_mpm)
355                         timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
356                                     0);
357                 sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
358         }
359 #endif
360
361         memcpy(sta->addr, addr, ETH_ALEN);
362         memcpy(sta->sta.addr, addr, ETH_ALEN);
363         sta->sta.max_rx_aggregation_subframes =
364                 local->hw.max_rx_aggregation_subframes;
365
366         sta->local = local;
367         sta->sdata = sdata;
368         sta->rx_stats.last_rx = jiffies;
369
370         u64_stats_init(&sta->rx_stats.syncp);
371
372         ieee80211_init_frag_cache(&sta->frags);
373
374         sta->sta_state = IEEE80211_STA_NONE;
375
376         /* Mark TID as unreserved */
377         sta->reserved_tid = IEEE80211_TID_UNRESERVED;
378
379         sta->last_connected = ktime_get_seconds();
380         ewma_signal_init(&sta->rx_stats_avg.signal);
381         ewma_avg_signal_init(&sta->status_stats.avg_ack_signal);
382         for (i = 0; i < ARRAY_SIZE(sta->rx_stats_avg.chain_signal); i++)
383                 ewma_signal_init(&sta->rx_stats_avg.chain_signal[i]);
384
385         if (local->ops->wake_tx_queue) {
386                 void *txq_data;
387                 int size = sizeof(struct txq_info) +
388                            ALIGN(hw->txq_data_size, sizeof(void *));
389
390                 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
391                 if (!txq_data)
392                         goto free;
393
394                 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
395                         struct txq_info *txq = txq_data + i * size;
396
397                         ieee80211_txq_init(sdata, sta, txq, i);
398                 }
399         }
400
401         if (sta_prepare_rate_control(local, sta, gfp))
402                 goto free_txq;
403
404         for (i = 0; i < IEEE80211_NUM_ACS; i++) {
405                 skb_queue_head_init(&sta->ps_tx_buf[i]);
406                 skb_queue_head_init(&sta->tx_filtered[i]);
407         }
408
409         for (i = 0; i < IEEE80211_NUM_TIDS; i++)
410                 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
411
412         sta->sta.smps_mode = IEEE80211_SMPS_OFF;
413         if (sdata->vif.type == NL80211_IFTYPE_AP ||
414             sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
415                 struct ieee80211_supported_band *sband;
416                 u8 smps;
417
418                 sband = ieee80211_get_sband(sdata);
419                 if (!sband)
420                         goto free_txq;
421
422                 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
423                         IEEE80211_HT_CAP_SM_PS_SHIFT;
424                 /*
425                  * Assume that hostapd advertises our caps in the beacon and
426                  * this is the known_smps_mode for a station that just assciated
427                  */
428                 switch (smps) {
429                 case WLAN_HT_SMPS_CONTROL_DISABLED:
430                         sta->known_smps_mode = IEEE80211_SMPS_OFF;
431                         break;
432                 case WLAN_HT_SMPS_CONTROL_STATIC:
433                         sta->known_smps_mode = IEEE80211_SMPS_STATIC;
434                         break;
435                 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
436                         sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
437                         break;
438                 default:
439                         WARN_ON(1);
440                 }
441         }
442
443         sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
444
445         sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
446         sta->cparams.target = MS2TIME(20);
447         sta->cparams.interval = MS2TIME(100);
448         sta->cparams.ecn = true;
449
450         sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
451
452         return sta;
453
454 free_txq:
455         if (sta->sta.txq[0])
456                 kfree(to_txq_info(sta->sta.txq[0]));
457 free:
458         free_percpu(sta->pcpu_rx_stats);
459 #ifdef CONFIG_MAC80211_MESH
460         kfree(sta->mesh);
461 #endif
462         kfree(sta);
463         return NULL;
464 }
465
466 static int sta_info_insert_check(struct sta_info *sta)
467 {
468         struct ieee80211_sub_if_data *sdata = sta->sdata;
469
470         /*
471          * Can't be a WARN_ON because it can be triggered through a race:
472          * something inserts a STA (on one CPU) without holding the RTNL
473          * and another CPU turns off the net device.
474          */
475         if (unlikely(!ieee80211_sdata_running(sdata)))
476                 return -ENETDOWN;
477
478         if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
479                     is_multicast_ether_addr(sta->sta.addr)))
480                 return -EINVAL;
481
482         /* The RCU read lock is required by rhashtable due to
483          * asynchronous resize/rehash.  We also require the mutex
484          * for correctness.
485          */
486         rcu_read_lock();
487         lockdep_assert_held(&sdata->local->sta_mtx);
488         if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
489             ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
490                 rcu_read_unlock();
491                 return -ENOTUNIQ;
492         }
493         rcu_read_unlock();
494
495         return 0;
496 }
497
498 static int sta_info_insert_drv_state(struct ieee80211_local *local,
499                                      struct ieee80211_sub_if_data *sdata,
500                                      struct sta_info *sta)
501 {
502         enum ieee80211_sta_state state;
503         int err = 0;
504
505         for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
506                 err = drv_sta_state(local, sdata, sta, state, state + 1);
507                 if (err)
508                         break;
509         }
510
511         if (!err) {
512                 /*
513                  * Drivers using legacy sta_add/sta_remove callbacks only
514                  * get uploaded set to true after sta_add is called.
515                  */
516                 if (!local->ops->sta_add)
517                         sta->uploaded = true;
518                 return 0;
519         }
520
521         if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
522                 sdata_info(sdata,
523                            "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
524                            sta->sta.addr, state + 1, err);
525                 err = 0;
526         }
527
528         /* unwind on error */
529         for (; state > IEEE80211_STA_NOTEXIST; state--)
530                 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
531
532         return err;
533 }
534
535 static void
536 ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
537 {
538         struct ieee80211_local *local = sdata->local;
539         bool allow_p2p_go_ps = sdata->vif.p2p;
540         struct sta_info *sta;
541
542         rcu_read_lock();
543         list_for_each_entry_rcu(sta, &local->sta_list, list) {
544                 if (sdata != sta->sdata ||
545                     !test_sta_flag(sta, WLAN_STA_ASSOC))
546                         continue;
547                 if (!sta->sta.support_p2p_ps) {
548                         allow_p2p_go_ps = false;
549                         break;
550                 }
551         }
552         rcu_read_unlock();
553
554         if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
555                 sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
556                 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_P2P_PS);
557         }
558 }
559
560 /*
561  * should be called with sta_mtx locked
562  * this function replaces the mutex lock
563  * with a RCU lock
564  */
565 static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
566 {
567         struct ieee80211_local *local = sta->local;
568         struct ieee80211_sub_if_data *sdata = sta->sdata;
569         struct station_info *sinfo = NULL;
570         int err = 0;
571
572         lockdep_assert_held(&local->sta_mtx);
573
574         /* check if STA exists already */
575         if (sta_info_get_bss(sdata, sta->sta.addr)) {
576                 err = -EEXIST;
577                 goto out_err;
578         }
579
580         sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
581         if (!sinfo) {
582                 err = -ENOMEM;
583                 goto out_err;
584         }
585
586         local->num_sta++;
587         local->sta_generation++;
588         smp_mb();
589
590         /* simplify things and don't accept BA sessions yet */
591         set_sta_flag(sta, WLAN_STA_BLOCK_BA);
592
593         /* make the station visible */
594         err = sta_info_hash_add(local, sta);
595         if (err)
596                 goto out_drop_sta;
597
598         list_add_tail_rcu(&sta->list, &local->sta_list);
599
600         /* notify driver */
601         err = sta_info_insert_drv_state(local, sdata, sta);
602         if (err)
603                 goto out_remove;
604
605         set_sta_flag(sta, WLAN_STA_INSERTED);
606
607         if (sta->sta_state >= IEEE80211_STA_ASSOC) {
608                 ieee80211_recalc_min_chandef(sta->sdata);
609                 if (!sta->sta.support_p2p_ps)
610                         ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
611         }
612
613         /* accept BA sessions now */
614         clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
615
616         ieee80211_sta_debugfs_add(sta);
617         rate_control_add_sta_debugfs(sta);
618
619         sinfo->generation = local->sta_generation;
620         cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
621         kfree(sinfo);
622
623         sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
624
625         /* move reference to rcu-protected */
626         rcu_read_lock();
627         mutex_unlock(&local->sta_mtx);
628
629         if (ieee80211_vif_is_mesh(&sdata->vif))
630                 mesh_accept_plinks_update(sdata);
631
632         ieee80211_check_fast_xmit(sta);
633
634         return 0;
635  out_remove:
636         sta_info_hash_del(local, sta);
637         list_del_rcu(&sta->list);
638  out_drop_sta:
639         local->num_sta--;
640         synchronize_net();
641         cleanup_single_sta(sta);
642  out_err:
643         mutex_unlock(&local->sta_mtx);
644         kfree(sinfo);
645         rcu_read_lock();
646         return err;
647 }
648
649 int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
650 {
651         struct ieee80211_local *local = sta->local;
652         int err;
653
654         might_sleep();
655
656         mutex_lock(&local->sta_mtx);
657
658         err = sta_info_insert_check(sta);
659         if (err) {
660                 sta_info_free(local, sta);
661                 mutex_unlock(&local->sta_mtx);
662                 rcu_read_lock();
663                 return err;
664         }
665
666         return sta_info_insert_finish(sta);
667 }
668
669 int sta_info_insert(struct sta_info *sta)
670 {
671         int err = sta_info_insert_rcu(sta);
672
673         rcu_read_unlock();
674
675         return err;
676 }
677
678 static inline void __bss_tim_set(u8 *tim, u16 id)
679 {
680         /*
681          * This format has been mandated by the IEEE specifications,
682          * so this line may not be changed to use the __set_bit() format.
683          */
684         tim[id / 8] |= (1 << (id % 8));
685 }
686
687 static inline void __bss_tim_clear(u8 *tim, u16 id)
688 {
689         /*
690          * This format has been mandated by the IEEE specifications,
691          * so this line may not be changed to use the __clear_bit() format.
692          */
693         tim[id / 8] &= ~(1 << (id % 8));
694 }
695
696 static inline bool __bss_tim_get(u8 *tim, u16 id)
697 {
698         /*
699          * This format has been mandated by the IEEE specifications,
700          * so this line may not be changed to use the test_bit() format.
701          */
702         return tim[id / 8] & (1 << (id % 8));
703 }
704
705 static unsigned long ieee80211_tids_for_ac(int ac)
706 {
707         /* If we ever support TIDs > 7, this obviously needs to be adjusted */
708         switch (ac) {
709         case IEEE80211_AC_VO:
710                 return BIT(6) | BIT(7);
711         case IEEE80211_AC_VI:
712                 return BIT(4) | BIT(5);
713         case IEEE80211_AC_BE:
714                 return BIT(0) | BIT(3);
715         case IEEE80211_AC_BK:
716                 return BIT(1) | BIT(2);
717         default:
718                 WARN_ON(1);
719                 return 0;
720         }
721 }
722
723 static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
724 {
725         struct ieee80211_local *local = sta->local;
726         struct ps_data *ps;
727         bool indicate_tim = false;
728         u8 ignore_for_tim = sta->sta.uapsd_queues;
729         int ac;
730         u16 id = sta->sta.aid;
731
732         if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
733             sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
734                 if (WARN_ON_ONCE(!sta->sdata->bss))
735                         return;
736
737                 ps = &sta->sdata->bss->ps;
738 #ifdef CONFIG_MAC80211_MESH
739         } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
740                 ps = &sta->sdata->u.mesh.ps;
741 #endif
742         } else {
743                 return;
744         }
745
746         /* No need to do anything if the driver does all */
747         if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
748                 return;
749
750         if (sta->dead)
751                 goto done;
752
753         /*
754          * If all ACs are delivery-enabled then we should build
755          * the TIM bit for all ACs anyway; if only some are then
756          * we ignore those and build the TIM bit using only the
757          * non-enabled ones.
758          */
759         if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
760                 ignore_for_tim = 0;
761
762         if (ignore_pending)
763                 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
764
765         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
766                 unsigned long tids;
767
768                 if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
769                         continue;
770
771                 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
772                                 !skb_queue_empty(&sta->ps_tx_buf[ac]);
773                 if (indicate_tim)
774                         break;
775
776                 tids = ieee80211_tids_for_ac(ac);
777
778                 indicate_tim |=
779                         sta->driver_buffered_tids & tids;
780                 indicate_tim |=
781                         sta->txq_buffered_tids & tids;
782         }
783
784  done:
785         spin_lock_bh(&local->tim_lock);
786
787         if (indicate_tim == __bss_tim_get(ps->tim, id))
788                 goto out_unlock;
789
790         if (indicate_tim)
791                 __bss_tim_set(ps->tim, id);
792         else
793                 __bss_tim_clear(ps->tim, id);
794
795         if (local->ops->set_tim && !WARN_ON(sta->dead)) {
796                 local->tim_in_locked_section = true;
797                 drv_set_tim(local, &sta->sta, indicate_tim);
798                 local->tim_in_locked_section = false;
799         }
800
801 out_unlock:
802         spin_unlock_bh(&local->tim_lock);
803 }
804
805 void sta_info_recalc_tim(struct sta_info *sta)
806 {
807         __sta_info_recalc_tim(sta, false);
808 }
809
810 static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
811 {
812         struct ieee80211_tx_info *info;
813         int timeout;
814
815         if (!skb)
816                 return false;
817
818         info = IEEE80211_SKB_CB(skb);
819
820         /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
821         timeout = (sta->listen_interval *
822                    sta->sdata->vif.bss_conf.beacon_int *
823                    32 / 15625) * HZ;
824         if (timeout < STA_TX_BUFFER_EXPIRE)
825                 timeout = STA_TX_BUFFER_EXPIRE;
826         return time_after(jiffies, info->control.jiffies + timeout);
827 }
828
829
830 static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
831                                                 struct sta_info *sta, int ac)
832 {
833         unsigned long flags;
834         struct sk_buff *skb;
835
836         /*
837          * First check for frames that should expire on the filtered
838          * queue. Frames here were rejected by the driver and are on
839          * a separate queue to avoid reordering with normal PS-buffered
840          * frames. They also aren't accounted for right now in the
841          * total_ps_buffered counter.
842          */
843         for (;;) {
844                 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
845                 skb = skb_peek(&sta->tx_filtered[ac]);
846                 if (sta_info_buffer_expired(sta, skb))
847                         skb = __skb_dequeue(&sta->tx_filtered[ac]);
848                 else
849                         skb = NULL;
850                 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
851
852                 /*
853                  * Frames are queued in order, so if this one
854                  * hasn't expired yet we can stop testing. If
855                  * we actually reached the end of the queue we
856                  * also need to stop, of course.
857                  */
858                 if (!skb)
859                         break;
860                 ieee80211_free_txskb(&local->hw, skb);
861         }
862
863         /*
864          * Now also check the normal PS-buffered queue, this will
865          * only find something if the filtered queue was emptied
866          * since the filtered frames are all before the normal PS
867          * buffered frames.
868          */
869         for (;;) {
870                 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
871                 skb = skb_peek(&sta->ps_tx_buf[ac]);
872                 if (sta_info_buffer_expired(sta, skb))
873                         skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
874                 else
875                         skb = NULL;
876                 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
877
878                 /*
879                  * frames are queued in order, so if this one
880                  * hasn't expired yet (or we reached the end of
881                  * the queue) we can stop testing
882                  */
883                 if (!skb)
884                         break;
885
886                 local->total_ps_buffered--;
887                 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
888                        sta->sta.addr);
889                 ieee80211_free_txskb(&local->hw, skb);
890         }
891
892         /*
893          * Finally, recalculate the TIM bit for this station -- it might
894          * now be clear because the station was too slow to retrieve its
895          * frames.
896          */
897         sta_info_recalc_tim(sta);
898
899         /*
900          * Return whether there are any frames still buffered, this is
901          * used to check whether the cleanup timer still needs to run,
902          * if there are no frames we don't need to rearm the timer.
903          */
904         return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
905                  skb_queue_empty(&sta->tx_filtered[ac]));
906 }
907
908 static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
909                                              struct sta_info *sta)
910 {
911         bool have_buffered = false;
912         int ac;
913
914         /* This is only necessary for stations on BSS/MBSS interfaces */
915         if (!sta->sdata->bss &&
916             !ieee80211_vif_is_mesh(&sta->sdata->vif))
917                 return false;
918
919         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
920                 have_buffered |=
921                         sta_info_cleanup_expire_buffered_ac(local, sta, ac);
922
923         return have_buffered;
924 }
925
926 static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
927 {
928         struct ieee80211_local *local;
929         struct ieee80211_sub_if_data *sdata;
930         int ret;
931
932         might_sleep();
933
934         if (!sta)
935                 return -ENOENT;
936
937         local = sta->local;
938         sdata = sta->sdata;
939
940         lockdep_assert_held(&local->sta_mtx);
941
942         /*
943          * Before removing the station from the driver and
944          * rate control, it might still start new aggregation
945          * sessions -- block that to make sure the tear-down
946          * will be sufficient.
947          */
948         set_sta_flag(sta, WLAN_STA_BLOCK_BA);
949         ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
950
951         /*
952          * Before removing the station from the driver there might be pending
953          * rx frames on RSS queues sent prior to the disassociation - wait for
954          * all such frames to be processed.
955          */
956         drv_sync_rx_queues(local, sta);
957
958         ret = sta_info_hash_del(local, sta);
959         if (WARN_ON(ret))
960                 return ret;
961
962         /*
963          * for TDLS peers, make sure to return to the base channel before
964          * removal.
965          */
966         if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
967                 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
968                 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
969         }
970
971         list_del_rcu(&sta->list);
972         sta->removed = true;
973
974         if (sta->uploaded)
975                 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
976
977         if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
978             rcu_access_pointer(sdata->u.vlan.sta) == sta)
979                 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
980
981         return 0;
982 }
983
984 static void __sta_info_destroy_part2(struct sta_info *sta)
985 {
986         struct ieee80211_local *local = sta->local;
987         struct ieee80211_sub_if_data *sdata = sta->sdata;
988         struct station_info *sinfo;
989         int ret;
990
991         /*
992          * NOTE: This assumes at least synchronize_net() was done
993          *       after _part1 and before _part2!
994          */
995
996         might_sleep();
997         lockdep_assert_held(&local->sta_mtx);
998
999         if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1000                 ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1001                 WARN_ON_ONCE(ret);
1002         }
1003
1004         /* now keys can no longer be reached */
1005         ieee80211_free_sta_keys(local, sta);
1006
1007         /* disable TIM bit - last chance to tell driver */
1008         __sta_info_recalc_tim(sta, true);
1009
1010         sta->dead = true;
1011
1012         local->num_sta--;
1013         local->sta_generation++;
1014
1015         while (sta->sta_state > IEEE80211_STA_NONE) {
1016                 ret = sta_info_move_state(sta, sta->sta_state - 1);
1017                 if (ret) {
1018                         WARN_ON_ONCE(1);
1019                         break;
1020                 }
1021         }
1022
1023         if (sta->uploaded) {
1024                 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1025                                     IEEE80211_STA_NOTEXIST);
1026                 WARN_ON_ONCE(ret != 0);
1027         }
1028
1029         sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1030
1031         sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
1032         if (sinfo)
1033                 sta_set_sinfo(sta, sinfo, true);
1034         cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
1035         kfree(sinfo);
1036
1037         rate_control_remove_sta_debugfs(sta);
1038         ieee80211_sta_debugfs_remove(sta);
1039
1040         ieee80211_destroy_frag_cache(&sta->frags);
1041
1042         cleanup_single_sta(sta);
1043 }
1044
1045 int __must_check __sta_info_destroy(struct sta_info *sta)
1046 {
1047         int err = __sta_info_destroy_part1(sta);
1048
1049         if (err)
1050                 return err;
1051
1052         synchronize_net();
1053
1054         __sta_info_destroy_part2(sta);
1055
1056         return 0;
1057 }
1058
1059 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
1060 {
1061         struct sta_info *sta;
1062         int ret;
1063
1064         mutex_lock(&sdata->local->sta_mtx);
1065         sta = sta_info_get(sdata, addr);
1066         ret = __sta_info_destroy(sta);
1067         mutex_unlock(&sdata->local->sta_mtx);
1068
1069         return ret;
1070 }
1071
1072 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
1073                               const u8 *addr)
1074 {
1075         struct sta_info *sta;
1076         int ret;
1077
1078         mutex_lock(&sdata->local->sta_mtx);
1079         sta = sta_info_get_bss(sdata, addr);
1080         ret = __sta_info_destroy(sta);
1081         mutex_unlock(&sdata->local->sta_mtx);
1082
1083         return ret;
1084 }
1085
1086 static void sta_info_cleanup(struct timer_list *t)
1087 {
1088         struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1089         struct sta_info *sta;
1090         bool timer_needed = false;
1091
1092         rcu_read_lock();
1093         list_for_each_entry_rcu(sta, &local->sta_list, list)
1094                 if (sta_info_cleanup_expire_buffered(local, sta))
1095                         timer_needed = true;
1096         rcu_read_unlock();
1097
1098         if (local->quiescing)
1099                 return;
1100
1101         if (!timer_needed)
1102                 return;
1103
1104         mod_timer(&local->sta_cleanup,
1105                   round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1106 }
1107
1108 int sta_info_init(struct ieee80211_local *local)
1109 {
1110         int err;
1111
1112         err = rhltable_init(&local->sta_hash, &sta_rht_params);
1113         if (err)
1114                 return err;
1115
1116         spin_lock_init(&local->tim_lock);
1117         mutex_init(&local->sta_mtx);
1118         INIT_LIST_HEAD(&local->sta_list);
1119
1120         timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
1121         return 0;
1122 }
1123
1124 void sta_info_stop(struct ieee80211_local *local)
1125 {
1126         del_timer_sync(&local->sta_cleanup);
1127         rhltable_destroy(&local->sta_hash);
1128 }
1129
1130
1131 int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1132 {
1133         struct ieee80211_local *local = sdata->local;
1134         struct sta_info *sta, *tmp;
1135         LIST_HEAD(free_list);
1136         int ret = 0;
1137
1138         might_sleep();
1139
1140         WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1141         WARN_ON(vlans && !sdata->bss);
1142
1143         mutex_lock(&local->sta_mtx);
1144         list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1145                 if (sdata == sta->sdata ||
1146                     (vlans && sdata->bss == sta->sdata->bss)) {
1147                         if (!WARN_ON(__sta_info_destroy_part1(sta)))
1148                                 list_add(&sta->free_list, &free_list);
1149                         ret++;
1150                 }
1151         }
1152
1153         if (!list_empty(&free_list)) {
1154                 synchronize_net();
1155                 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1156                         __sta_info_destroy_part2(sta);
1157         }
1158         mutex_unlock(&local->sta_mtx);
1159
1160         return ret;
1161 }
1162
1163 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1164                           unsigned long exp_time)
1165 {
1166         struct ieee80211_local *local = sdata->local;
1167         struct sta_info *sta, *tmp;
1168
1169         mutex_lock(&local->sta_mtx);
1170
1171         list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1172                 unsigned long last_active = ieee80211_sta_last_active(sta);
1173
1174                 if (sdata != sta->sdata)
1175                         continue;
1176
1177                 if (time_is_before_jiffies(last_active + exp_time)) {
1178                         sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1179                                 sta->sta.addr);
1180
1181                         if (ieee80211_vif_is_mesh(&sdata->vif) &&
1182                             test_sta_flag(sta, WLAN_STA_PS_STA))
1183                                 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1184
1185                         WARN_ON(__sta_info_destroy(sta));
1186                 }
1187         }
1188
1189         mutex_unlock(&local->sta_mtx);
1190 }
1191
1192 struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1193                                                    const u8 *addr,
1194                                                    const u8 *localaddr)
1195 {
1196         struct ieee80211_local *local = hw_to_local(hw);
1197         struct rhlist_head *tmp;
1198         struct sta_info *sta;
1199
1200         /*
1201          * Just return a random station if localaddr is NULL
1202          * ... first in list.
1203          */
1204         for_each_sta_info(local, addr, sta, tmp) {
1205                 if (localaddr &&
1206                     !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1207                         continue;
1208                 if (!sta->uploaded)
1209                         return NULL;
1210                 return &sta->sta;
1211         }
1212
1213         return NULL;
1214 }
1215 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
1216
1217 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1218                                          const u8 *addr)
1219 {
1220         struct sta_info *sta;
1221
1222         if (!vif)
1223                 return NULL;
1224
1225         sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1226         if (!sta)
1227                 return NULL;
1228
1229         if (!sta->uploaded)
1230                 return NULL;
1231
1232         return &sta->sta;
1233 }
1234 EXPORT_SYMBOL(ieee80211_find_sta);
1235
1236 /* powersave support code */
1237 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1238 {
1239         struct ieee80211_sub_if_data *sdata = sta->sdata;
1240         struct ieee80211_local *local = sdata->local;
1241         struct sk_buff_head pending;
1242         int filtered = 0, buffered = 0, ac, i;
1243         unsigned long flags;
1244         struct ps_data *ps;
1245
1246         if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1247                 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1248                                      u.ap);
1249
1250         if (sdata->vif.type == NL80211_IFTYPE_AP)
1251                 ps = &sdata->bss->ps;
1252         else if (ieee80211_vif_is_mesh(&sdata->vif))
1253                 ps = &sdata->u.mesh.ps;
1254         else
1255                 return;
1256
1257         clear_sta_flag(sta, WLAN_STA_SP);
1258
1259         BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1260         sta->driver_buffered_tids = 0;
1261         sta->txq_buffered_tids = 0;
1262
1263         if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1264                 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1265
1266         if (sta->sta.txq[0]) {
1267                 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1268                         if (!txq_has_queue(sta->sta.txq[i]))
1269                                 continue;
1270
1271                         drv_wake_tx_queue(local, to_txq_info(sta->sta.txq[i]));
1272                 }
1273         }
1274
1275         skb_queue_head_init(&pending);
1276
1277         /* sync with ieee80211_tx_h_unicast_ps_buf */
1278         spin_lock(&sta->ps_lock);
1279         /* Send all buffered frames to the station */
1280         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1281                 int count = skb_queue_len(&pending), tmp;
1282
1283                 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1284                 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1285                 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1286                 tmp = skb_queue_len(&pending);
1287                 filtered += tmp - count;
1288                 count = tmp;
1289
1290                 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1291                 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1292                 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1293                 tmp = skb_queue_len(&pending);
1294                 buffered += tmp - count;
1295         }
1296
1297         ieee80211_add_pending_skbs(local, &pending);
1298
1299         /* now we're no longer in the deliver code */
1300         clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1301
1302         /* The station might have polled and then woken up before we responded,
1303          * so clear these flags now to avoid them sticking around.
1304          */
1305         clear_sta_flag(sta, WLAN_STA_PSPOLL);
1306         clear_sta_flag(sta, WLAN_STA_UAPSD);
1307         spin_unlock(&sta->ps_lock);
1308
1309         atomic_dec(&ps->num_sta_ps);
1310
1311         /* This station just woke up and isn't aware of our SMPS state */
1312         if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1313             !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1314                                            sdata->smps_mode) &&
1315             sta->known_smps_mode != sdata->bss->req_smps &&
1316             sta_info_tx_streams(sta) != 1) {
1317                 ht_dbg(sdata,
1318                        "%pM just woke up and MIMO capable - update SMPS\n",
1319                        sta->sta.addr);
1320                 ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1321                                            sta->sta.addr,
1322                                            sdata->vif.bss_conf.bssid);
1323         }
1324
1325         local->total_ps_buffered -= buffered;
1326
1327         sta_info_recalc_tim(sta);
1328
1329         ps_dbg(sdata,
1330                "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1331                sta->sta.addr, sta->sta.aid, filtered, buffered);
1332
1333         ieee80211_check_fast_xmit(sta);
1334 }
1335
1336 static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1337                                          enum ieee80211_frame_release_type reason,
1338                                          bool call_driver, bool more_data)
1339 {
1340         struct ieee80211_sub_if_data *sdata = sta->sdata;
1341         struct ieee80211_local *local = sdata->local;
1342         struct ieee80211_qos_hdr *nullfunc;
1343         struct sk_buff *skb;
1344         int size = sizeof(*nullfunc);
1345         __le16 fc;
1346         bool qos = sta->sta.wme;
1347         struct ieee80211_tx_info *info;
1348         struct ieee80211_chanctx_conf *chanctx_conf;
1349
1350         if (qos) {
1351                 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1352                                  IEEE80211_STYPE_QOS_NULLFUNC |
1353                                  IEEE80211_FCTL_FROMDS);
1354         } else {
1355                 size -= 2;
1356                 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1357                                  IEEE80211_STYPE_NULLFUNC |
1358                                  IEEE80211_FCTL_FROMDS);
1359         }
1360
1361         skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1362         if (!skb)
1363                 return;
1364
1365         skb_reserve(skb, local->hw.extra_tx_headroom);
1366
1367         nullfunc = skb_put(skb, size);
1368         nullfunc->frame_control = fc;
1369         nullfunc->duration_id = 0;
1370         memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1371         memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1372         memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1373         nullfunc->seq_ctrl = 0;
1374
1375         skb->priority = tid;
1376         skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
1377         if (qos) {
1378                 nullfunc->qos_ctrl = cpu_to_le16(tid);
1379
1380                 if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1381                         nullfunc->qos_ctrl |=
1382                                 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1383                         if (more_data)
1384                                 nullfunc->frame_control |=
1385                                         cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1386                 }
1387         }
1388
1389         info = IEEE80211_SKB_CB(skb);
1390
1391         /*
1392          * Tell TX path to send this frame even though the
1393          * STA may still remain is PS mode after this frame
1394          * exchange. Also set EOSP to indicate this packet
1395          * ends the poll/service period.
1396          */
1397         info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1398                        IEEE80211_TX_STATUS_EOSP |
1399                        IEEE80211_TX_CTL_REQ_TX_STATUS;
1400
1401         info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1402
1403         if (call_driver)
1404                 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1405                                           reason, false);
1406
1407         skb->dev = sdata->dev;
1408
1409         rcu_read_lock();
1410         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1411         if (WARN_ON(!chanctx_conf)) {
1412                 rcu_read_unlock();
1413                 kfree_skb(skb);
1414                 return;
1415         }
1416
1417         info->band = chanctx_conf->def.chan->band;
1418         ieee80211_xmit(sdata, sta, skb, 0);
1419         rcu_read_unlock();
1420 }
1421
1422 static int find_highest_prio_tid(unsigned long tids)
1423 {
1424         /* lower 3 TIDs aren't ordered perfectly */
1425         if (tids & 0xF8)
1426                 return fls(tids) - 1;
1427         /* TID 0 is BE just like TID 3 */
1428         if (tids & BIT(0))
1429                 return 0;
1430         return fls(tids) - 1;
1431 }
1432
1433 /* Indicates if the MORE_DATA bit should be set in the last
1434  * frame obtained by ieee80211_sta_ps_get_frames.
1435  * Note that driver_release_tids is relevant only if
1436  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
1437  */
1438 static bool
1439 ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
1440                            enum ieee80211_frame_release_type reason,
1441                            unsigned long driver_release_tids)
1442 {
1443         int ac;
1444
1445         /* If the driver has data on more than one TID then
1446          * certainly there's more data if we release just a
1447          * single frame now (from a single TID). This will
1448          * only happen for PS-Poll.
1449          */
1450         if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1451             hweight16(driver_release_tids) > 1)
1452                 return true;
1453
1454         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1455                 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1456                         continue;
1457
1458                 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1459                     !skb_queue_empty(&sta->ps_tx_buf[ac]))
1460                         return true;
1461         }
1462
1463         return false;
1464 }
1465
1466 static void
1467 ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
1468                             enum ieee80211_frame_release_type reason,
1469                             struct sk_buff_head *frames,
1470                             unsigned long *driver_release_tids)
1471 {
1472         struct ieee80211_sub_if_data *sdata = sta->sdata;
1473         struct ieee80211_local *local = sdata->local;
1474         int ac;
1475
1476         /* Get response frame(s) and more data bit for the last one. */
1477         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1478                 unsigned long tids;
1479
1480                 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1481                         continue;
1482
1483                 tids = ieee80211_tids_for_ac(ac);
1484
1485                 /* if we already have frames from software, then we can't also
1486                  * release from hardware queues
1487                  */
1488                 if (skb_queue_empty(frames)) {
1489                         *driver_release_tids |=
1490                                 sta->driver_buffered_tids & tids;
1491                         *driver_release_tids |= sta->txq_buffered_tids & tids;
1492                 }
1493
1494                 if (!*driver_release_tids) {
1495                         struct sk_buff *skb;
1496
1497                         while (n_frames > 0) {
1498                                 skb = skb_dequeue(&sta->tx_filtered[ac]);
1499                                 if (!skb) {
1500                                         skb = skb_dequeue(
1501                                                 &sta->ps_tx_buf[ac]);
1502                                         if (skb)
1503                                                 local->total_ps_buffered--;
1504                                 }
1505                                 if (!skb)
1506                                         break;
1507                                 n_frames--;
1508                                 __skb_queue_tail(frames, skb);
1509                         }
1510                 }
1511
1512                 /* If we have more frames buffered on this AC, then abort the
1513                  * loop since we can't send more data from other ACs before
1514                  * the buffered frames from this.
1515                  */
1516                 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1517                     !skb_queue_empty(&sta->ps_tx_buf[ac]))
1518                         break;
1519         }
1520 }
1521
1522 static void
1523 ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1524                                   int n_frames, u8 ignored_acs,
1525                                   enum ieee80211_frame_release_type reason)
1526 {
1527         struct ieee80211_sub_if_data *sdata = sta->sdata;
1528         struct ieee80211_local *local = sdata->local;
1529         unsigned long driver_release_tids = 0;
1530         struct sk_buff_head frames;
1531         bool more_data;
1532
1533         /* Service or PS-Poll period starts */
1534         set_sta_flag(sta, WLAN_STA_SP);
1535
1536         __skb_queue_head_init(&frames);
1537
1538         ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
1539                                     &frames, &driver_release_tids);
1540
1541         more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
1542
1543         if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
1544                 driver_release_tids =
1545                         BIT(find_highest_prio_tid(driver_release_tids));
1546
1547         if (skb_queue_empty(&frames) && !driver_release_tids) {
1548                 int tid, ac;
1549
1550                 /*
1551                  * For PS-Poll, this can only happen due to a race condition
1552                  * when we set the TIM bit and the station notices it, but
1553                  * before it can poll for the frame we expire it.
1554                  *
1555                  * For uAPSD, this is said in the standard (11.2.1.5 h):
1556                  *      At each unscheduled SP for a non-AP STA, the AP shall
1557                  *      attempt to transmit at least one MSDU or MMPDU, but no
1558                  *      more than the value specified in the Max SP Length field
1559                  *      in the QoS Capability element from delivery-enabled ACs,
1560                  *      that are destined for the non-AP STA.
1561                  *
1562                  * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1563                  */
1564
1565                 /* This will evaluate to 1, 3, 5 or 7. */
1566                 for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1567                         if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1568                                 break;
1569                 tid = 7 - 2 * ac;
1570
1571                 ieee80211_send_null_response(sta, tid, reason, true, false);
1572         } else if (!driver_release_tids) {
1573                 struct sk_buff_head pending;
1574                 struct sk_buff *skb;
1575                 int num = 0;
1576                 u16 tids = 0;
1577                 bool need_null = false;
1578
1579                 skb_queue_head_init(&pending);
1580
1581                 while ((skb = __skb_dequeue(&frames))) {
1582                         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1583                         struct ieee80211_hdr *hdr = (void *) skb->data;
1584                         u8 *qoshdr = NULL;
1585
1586                         num++;
1587
1588                         /*
1589                          * Tell TX path to send this frame even though the
1590                          * STA may still remain is PS mode after this frame
1591                          * exchange.
1592                          */
1593                         info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1594                         info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1595
1596                         /*
1597                          * Use MoreData flag to indicate whether there are
1598                          * more buffered frames for this STA
1599                          */
1600                         if (more_data || !skb_queue_empty(&frames))
1601                                 hdr->frame_control |=
1602                                         cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1603                         else
1604                                 hdr->frame_control &=
1605                                         cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1606
1607                         if (ieee80211_is_data_qos(hdr->frame_control) ||
1608                             ieee80211_is_qos_nullfunc(hdr->frame_control))
1609                                 qoshdr = ieee80211_get_qos_ctl(hdr);
1610
1611                         tids |= BIT(skb->priority);
1612
1613                         __skb_queue_tail(&pending, skb);
1614
1615                         /* end service period after last frame or add one */
1616                         if (!skb_queue_empty(&frames))
1617                                 continue;
1618
1619                         if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1620                                 /* for PS-Poll, there's only one frame */
1621                                 info->flags |= IEEE80211_TX_STATUS_EOSP |
1622                                                IEEE80211_TX_CTL_REQ_TX_STATUS;
1623                                 break;
1624                         }
1625
1626                         /* For uAPSD, things are a bit more complicated. If the
1627                          * last frame has a QoS header (i.e. is a QoS-data or
1628                          * QoS-nulldata frame) then just set the EOSP bit there
1629                          * and be done.
1630                          * If the frame doesn't have a QoS header (which means
1631                          * it should be a bufferable MMPDU) then we can't set
1632                          * the EOSP bit in the QoS header; add a QoS-nulldata
1633                          * frame to the list to send it after the MMPDU.
1634                          *
1635                          * Note that this code is only in the mac80211-release
1636                          * code path, we assume that the driver will not buffer
1637                          * anything but QoS-data frames, or if it does, will
1638                          * create the QoS-nulldata frame by itself if needed.
1639                          *
1640                          * Cf. 802.11-2012 10.2.1.10 (c).
1641                          */
1642                         if (qoshdr) {
1643                                 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
1644
1645                                 info->flags |= IEEE80211_TX_STATUS_EOSP |
1646                                                IEEE80211_TX_CTL_REQ_TX_STATUS;
1647                         } else {
1648                                 /* The standard isn't completely clear on this
1649                                  * as it says the more-data bit should be set
1650                                  * if there are more BUs. The QoS-Null frame
1651                                  * we're about to send isn't buffered yet, we
1652                                  * only create it below, but let's pretend it
1653                                  * was buffered just in case some clients only
1654                                  * expect more-data=0 when eosp=1.
1655                                  */
1656                                 hdr->frame_control |=
1657                                         cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1658                                 need_null = true;
1659                                 num++;
1660                         }
1661                         break;
1662                 }
1663
1664                 drv_allow_buffered_frames(local, sta, tids, num,
1665                                           reason, more_data);
1666
1667                 ieee80211_add_pending_skbs(local, &pending);
1668
1669                 if (need_null)
1670                         ieee80211_send_null_response(
1671                                 sta, find_highest_prio_tid(tids),
1672                                 reason, false, false);
1673
1674                 sta_info_recalc_tim(sta);
1675         } else {
1676                 int tid;
1677
1678                 /*
1679                  * We need to release a frame that is buffered somewhere in the
1680                  * driver ... it'll have to handle that.
1681                  * Note that the driver also has to check the number of frames
1682                  * on the TIDs we're releasing from - if there are more than
1683                  * n_frames it has to set the more-data bit (if we didn't ask
1684                  * it to set it anyway due to other buffered frames); if there
1685                  * are fewer than n_frames it has to make sure to adjust that
1686                  * to allow the service period to end properly.
1687                  */
1688                 drv_release_buffered_frames(local, sta, driver_release_tids,
1689                                             n_frames, reason, more_data);
1690
1691                 /*
1692                  * Note that we don't recalculate the TIM bit here as it would
1693                  * most likely have no effect at all unless the driver told us
1694                  * that the TID(s) became empty before returning here from the
1695                  * release function.
1696                  * Either way, however, when the driver tells us that the TID(s)
1697                  * became empty or we find that a txq became empty, we'll do the
1698                  * TIM recalculation.
1699                  */
1700
1701                 if (!sta->sta.txq[0])
1702                         return;
1703
1704                 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1705                         if (!(driver_release_tids & BIT(tid)) ||
1706                             txq_has_queue(sta->sta.txq[tid]))
1707                                 continue;
1708
1709                         sta_info_recalc_tim(sta);
1710                         break;
1711                 }
1712         }
1713 }
1714
1715 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1716 {
1717         u8 ignore_for_response = sta->sta.uapsd_queues;
1718
1719         /*
1720          * If all ACs are delivery-enabled then we should reply
1721          * from any of them, if only some are enabled we reply
1722          * only from the non-enabled ones.
1723          */
1724         if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1725                 ignore_for_response = 0;
1726
1727         ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1728                                           IEEE80211_FRAME_RELEASE_PSPOLL);
1729 }
1730
1731 void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1732 {
1733         int n_frames = sta->sta.max_sp;
1734         u8 delivery_enabled = sta->sta.uapsd_queues;
1735
1736         /*
1737          * If we ever grow support for TSPEC this might happen if
1738          * the TSPEC update from hostapd comes in between a trigger
1739          * frame setting WLAN_STA_UAPSD in the RX path and this
1740          * actually getting called.
1741          */
1742         if (!delivery_enabled)
1743                 return;
1744
1745         switch (sta->sta.max_sp) {
1746         case 1:
1747                 n_frames = 2;
1748                 break;
1749         case 2:
1750                 n_frames = 4;
1751                 break;
1752         case 3:
1753                 n_frames = 6;
1754                 break;
1755         case 0:
1756                 /* XXX: what is a good value? */
1757                 n_frames = 128;
1758                 break;
1759         }
1760
1761         ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1762                                           IEEE80211_FRAME_RELEASE_UAPSD);
1763 }
1764
1765 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1766                                struct ieee80211_sta *pubsta, bool block)
1767 {
1768         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1769
1770         trace_api_sta_block_awake(sta->local, pubsta, block);
1771
1772         if (block) {
1773                 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
1774                 ieee80211_clear_fast_xmit(sta);
1775                 return;
1776         }
1777
1778         if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1779                 return;
1780
1781         if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
1782                 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1783                 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1784                 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1785         } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
1786                    test_sta_flag(sta, WLAN_STA_UAPSD)) {
1787                 /* must be asleep in this case */
1788                 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1789                 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1790         } else {
1791                 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1792                 ieee80211_check_fast_xmit(sta);
1793         }
1794 }
1795 EXPORT_SYMBOL(ieee80211_sta_block_awake);
1796
1797 void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
1798 {
1799         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1800         struct ieee80211_local *local = sta->local;
1801
1802         trace_api_eosp(local, pubsta);
1803
1804         clear_sta_flag(sta, WLAN_STA_SP);
1805 }
1806 EXPORT_SYMBOL(ieee80211_sta_eosp);
1807
1808 void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
1809 {
1810         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1811         enum ieee80211_frame_release_type reason;
1812         bool more_data;
1813
1814         trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
1815
1816         reason = IEEE80211_FRAME_RELEASE_UAPSD;
1817         more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
1818                                                reason, 0);
1819
1820         ieee80211_send_null_response(sta, tid, reason, false, more_data);
1821 }
1822 EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
1823
1824 void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1825                                 u8 tid, bool buffered)
1826 {
1827         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1828
1829         if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1830                 return;
1831
1832         trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
1833
1834         if (buffered)
1835                 set_bit(tid, &sta->driver_buffered_tids);
1836         else
1837                 clear_bit(tid, &sta->driver_buffered_tids);
1838
1839         sta_info_recalc_tim(sta);
1840 }
1841 EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1842
1843 int sta_info_move_state(struct sta_info *sta,
1844                         enum ieee80211_sta_state new_state)
1845 {
1846         might_sleep();
1847
1848         if (sta->sta_state == new_state)
1849                 return 0;
1850
1851         /* check allowed transitions first */
1852
1853         switch (new_state) {
1854         case IEEE80211_STA_NONE:
1855                 if (sta->sta_state != IEEE80211_STA_AUTH)
1856                         return -EINVAL;
1857                 break;
1858         case IEEE80211_STA_AUTH:
1859                 if (sta->sta_state != IEEE80211_STA_NONE &&
1860                     sta->sta_state != IEEE80211_STA_ASSOC)
1861                         return -EINVAL;
1862                 break;
1863         case IEEE80211_STA_ASSOC:
1864                 if (sta->sta_state != IEEE80211_STA_AUTH &&
1865                     sta->sta_state != IEEE80211_STA_AUTHORIZED)
1866                         return -EINVAL;
1867                 break;
1868         case IEEE80211_STA_AUTHORIZED:
1869                 if (sta->sta_state != IEEE80211_STA_ASSOC)
1870                         return -EINVAL;
1871                 break;
1872         default:
1873                 WARN(1, "invalid state %d", new_state);
1874                 return -EINVAL;
1875         }
1876
1877         sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1878                 sta->sta.addr, new_state);
1879
1880         /*
1881          * notify the driver before the actual changes so it can
1882          * fail the transition
1883          */
1884         if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1885                 int err = drv_sta_state(sta->local, sta->sdata, sta,
1886                                         sta->sta_state, new_state);
1887                 if (err)
1888                         return err;
1889         }
1890
1891         /* reflect the change in all state variables */
1892
1893         switch (new_state) {
1894         case IEEE80211_STA_NONE:
1895                 if (sta->sta_state == IEEE80211_STA_AUTH)
1896                         clear_bit(WLAN_STA_AUTH, &sta->_flags);
1897                 break;
1898         case IEEE80211_STA_AUTH:
1899                 if (sta->sta_state == IEEE80211_STA_NONE) {
1900                         set_bit(WLAN_STA_AUTH, &sta->_flags);
1901                 } else if (sta->sta_state == IEEE80211_STA_ASSOC) {
1902                         clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1903                         ieee80211_recalc_min_chandef(sta->sdata);
1904                         if (!sta->sta.support_p2p_ps)
1905                                 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1906                 }
1907                 break;
1908         case IEEE80211_STA_ASSOC:
1909                 if (sta->sta_state == IEEE80211_STA_AUTH) {
1910                         set_bit(WLAN_STA_ASSOC, &sta->_flags);
1911                         ieee80211_recalc_min_chandef(sta->sdata);
1912                         if (!sta->sta.support_p2p_ps)
1913                                 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1914                 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1915                         ieee80211_vif_dec_num_mcast(sta->sdata);
1916                         clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1917                         ieee80211_clear_fast_xmit(sta);
1918                         ieee80211_clear_fast_rx(sta);
1919                 }
1920                 break;
1921         case IEEE80211_STA_AUTHORIZED:
1922                 if (sta->sta_state == IEEE80211_STA_ASSOC) {
1923                         ieee80211_vif_inc_num_mcast(sta->sdata);
1924                         set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1925                         ieee80211_check_fast_xmit(sta);
1926                         ieee80211_check_fast_rx(sta);
1927                 }
1928                 if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
1929                     sta->sdata->vif.type == NL80211_IFTYPE_AP)
1930                         cfg80211_send_layer2_update(sta->sdata->dev,
1931                                                     sta->sta.addr);
1932                 break;
1933         default:
1934                 break;
1935         }
1936
1937         sta->sta_state = new_state;
1938
1939         return 0;
1940 }
1941
1942 u8 sta_info_tx_streams(struct sta_info *sta)
1943 {
1944         struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1945         u8 rx_streams;
1946
1947         if (!sta->sta.ht_cap.ht_supported)
1948                 return 1;
1949
1950         if (sta->sta.vht_cap.vht_supported) {
1951                 int i;
1952                 u16 tx_mcs_map =
1953                         le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1954
1955                 for (i = 7; i >= 0; i--)
1956                         if ((tx_mcs_map & (0x3 << (i * 2))) !=
1957                             IEEE80211_VHT_MCS_NOT_SUPPORTED)
1958                                 return i + 1;
1959         }
1960
1961         if (ht_cap->mcs.rx_mask[3])
1962                 rx_streams = 4;
1963         else if (ht_cap->mcs.rx_mask[2])
1964                 rx_streams = 3;
1965         else if (ht_cap->mcs.rx_mask[1])
1966                 rx_streams = 2;
1967         else
1968                 rx_streams = 1;
1969
1970         if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1971                 return rx_streams;
1972
1973         return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1974                         >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1975 }
1976
1977 static struct ieee80211_sta_rx_stats *
1978 sta_get_last_rx_stats(struct sta_info *sta)
1979 {
1980         struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
1981         struct ieee80211_local *local = sta->local;
1982         int cpu;
1983
1984         if (!ieee80211_hw_check(&local->hw, USES_RSS))
1985                 return stats;
1986
1987         for_each_possible_cpu(cpu) {
1988                 struct ieee80211_sta_rx_stats *cpustats;
1989
1990                 cpustats = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
1991
1992                 if (time_after(cpustats->last_rx, stats->last_rx))
1993                         stats = cpustats;
1994         }
1995
1996         return stats;
1997 }
1998
1999 static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
2000                                   struct rate_info *rinfo)
2001 {
2002         rinfo->bw = STA_STATS_GET(BW, rate);
2003
2004         switch (STA_STATS_GET(TYPE, rate)) {
2005         case STA_STATS_RATE_TYPE_VHT:
2006                 rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2007                 rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2008                 rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2009                 if (STA_STATS_GET(SGI, rate))
2010                         rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2011                 break;
2012         case STA_STATS_RATE_TYPE_HT:
2013                 rinfo->flags = RATE_INFO_FLAGS_MCS;
2014                 rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2015                 if (STA_STATS_GET(SGI, rate))
2016                         rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2017                 break;
2018         case STA_STATS_RATE_TYPE_LEGACY: {
2019                 struct ieee80211_supported_band *sband;
2020                 u16 brate;
2021                 unsigned int shift;
2022                 int band = STA_STATS_GET(LEGACY_BAND, rate);
2023                 int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2024
2025                 sband = local->hw.wiphy->bands[band];
2026
2027                 if (WARN_ON_ONCE(!sband->bitrates))
2028                         break;
2029
2030                 brate = sband->bitrates[rate_idx].bitrate;
2031                 if (rinfo->bw == RATE_INFO_BW_5)
2032                         shift = 2;
2033                 else if (rinfo->bw == RATE_INFO_BW_10)
2034                         shift = 1;
2035                 else
2036                         shift = 0;
2037                 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
2038                 break;
2039                 }
2040         case STA_STATS_RATE_TYPE_HE:
2041                 rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
2042                 rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
2043                 rinfo->nss = STA_STATS_GET(HE_NSS, rate);
2044                 rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
2045                 rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
2046                 rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
2047                 break;
2048         }
2049 }
2050
2051 static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
2052 {
2053         u32 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
2054
2055         if (rate == STA_STATS_RATE_INVALID)
2056                 return -EINVAL;
2057
2058         sta_stats_decode_rate(sta->local, rate, rinfo);
2059         return 0;
2060 }
2061
2062 static void sta_set_tidstats(struct sta_info *sta,
2063                              struct cfg80211_tid_stats *tidstats,
2064                              int tid)
2065 {
2066         struct ieee80211_local *local = sta->local;
2067
2068         if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2069                 unsigned int start;
2070
2071                 do {
2072                         start = u64_stats_fetch_begin(&sta->rx_stats.syncp);
2073                         tidstats->rx_msdu = sta->rx_stats.msdu[tid];
2074                 } while (u64_stats_fetch_retry(&sta->rx_stats.syncp, start));
2075
2076                 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
2077         }
2078
2079         if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
2080                 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2081                 tidstats->tx_msdu = sta->tx_stats.msdu[tid];
2082         }
2083
2084         if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
2085             ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2086                 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2087                 tidstats->tx_msdu_retries = sta->status_stats.msdu_retries[tid];
2088         }
2089
2090         if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
2091             ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2092                 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2093                 tidstats->tx_msdu_failed = sta->status_stats.msdu_failed[tid];
2094         }
2095
2096         if (local->ops->wake_tx_queue && tid < IEEE80211_NUM_TIDS) {
2097                 spin_lock_bh(&local->fq.lock);
2098                 rcu_read_lock();
2099
2100                 tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
2101                 ieee80211_fill_txq_stats(&tidstats->txq_stats,
2102                                          to_txq_info(sta->sta.txq[tid]));
2103
2104                 rcu_read_unlock();
2105                 spin_unlock_bh(&local->fq.lock);
2106         }
2107 }
2108
2109 static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2110 {
2111         unsigned int start;
2112         u64 value;
2113
2114         do {
2115                 start = u64_stats_fetch_begin(&rxstats->syncp);
2116                 value = rxstats->bytes;
2117         } while (u64_stats_fetch_retry(&rxstats->syncp, start));
2118
2119         return value;
2120 }
2121
2122 void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
2123                    bool tidstats)
2124 {
2125         struct ieee80211_sub_if_data *sdata = sta->sdata;
2126         struct ieee80211_local *local = sdata->local;
2127         u32 thr = 0;
2128         int i, ac, cpu;
2129         struct ieee80211_sta_rx_stats *last_rxstats;
2130
2131         last_rxstats = sta_get_last_rx_stats(sta);
2132
2133         sinfo->generation = sdata->local->sta_generation;
2134
2135         /* do before driver, so beacon filtering drivers have a
2136          * chance to e.g. just add the number of filtered beacons
2137          * (or just modify the value entirely, of course)
2138          */
2139         if (sdata->vif.type == NL80211_IFTYPE_STATION)
2140                 sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
2141
2142         drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2143
2144         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2145                          BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2146                          BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2147                          BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
2148                          BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2149
2150         if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2151                 sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
2152                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2153         }
2154
2155         sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
2156         sinfo->inactive_time =
2157                 jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
2158
2159         if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2160                                BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2161                 sinfo->tx_bytes = 0;
2162                 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2163                         sinfo->tx_bytes += sta->tx_stats.bytes[ac];
2164                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2165         }
2166
2167         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
2168                 sinfo->tx_packets = 0;
2169                 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2170                         sinfo->tx_packets += sta->tx_stats.packets[ac];
2171                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
2172         }
2173
2174         if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2175                                BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2176                 sinfo->rx_bytes += sta_get_stats_bytes(&sta->rx_stats);
2177
2178                 if (sta->pcpu_rx_stats) {
2179                         for_each_possible_cpu(cpu) {
2180                                 struct ieee80211_sta_rx_stats *cpurxs;
2181
2182                                 cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2183                                 sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2184                         }
2185                 }
2186
2187                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
2188         }
2189
2190         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2191                 sinfo->rx_packets = sta->rx_stats.packets;
2192                 if (sta->pcpu_rx_stats) {
2193                         for_each_possible_cpu(cpu) {
2194                                 struct ieee80211_sta_rx_stats *cpurxs;
2195
2196                                 cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2197                                 sinfo->rx_packets += cpurxs->packets;
2198                         }
2199                 }
2200                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
2201         }
2202
2203         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2204                 sinfo->tx_retries = sta->status_stats.retry_count;
2205                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
2206         }
2207
2208         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2209                 sinfo->tx_failed = sta->status_stats.retry_failed;
2210                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
2211         }
2212
2213         sinfo->rx_dropped_misc = sta->rx_stats.dropped;
2214         if (sta->pcpu_rx_stats) {
2215                 for_each_possible_cpu(cpu) {
2216                         struct ieee80211_sta_rx_stats *cpurxs;
2217
2218                         cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2219                         sinfo->rx_dropped_misc += cpurxs->dropped;
2220                 }
2221         }
2222
2223         if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2224             !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2225                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2226                                  BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2227                 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2228         }
2229
2230         if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
2231             ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2232                 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2233                         sinfo->signal = (s8)last_rxstats->last_signal;
2234                         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2235                 }
2236
2237                 if (!sta->pcpu_rx_stats &&
2238                     !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
2239                         sinfo->signal_avg =
2240                                 -ewma_signal_read(&sta->rx_stats_avg.signal);
2241                         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
2242                 }
2243         }
2244
2245         /* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2246          * the sta->rx_stats struct, so the check here is fine with and without
2247          * pcpu statistics
2248          */
2249         if (last_rxstats->chains &&
2250             !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2251                                BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2252                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2253                 if (!sta->pcpu_rx_stats)
2254                         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2255
2256                 sinfo->chains = last_rxstats->chains;
2257
2258                 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2259                         sinfo->chain_signal[i] =
2260                                 last_rxstats->chain_signal_last[i];
2261                         sinfo->chain_signal_avg[i] =
2262                                 -ewma_signal_read(&sta->rx_stats_avg.chain_signal[i]);
2263                 }
2264         }
2265
2266         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))) {
2267                 sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
2268                                      &sinfo->txrate);
2269                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
2270         }
2271
2272         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE))) {
2273                 if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2274                         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
2275         }
2276
2277         if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
2278                 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
2279                         struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
2280
2281                         sta_set_tidstats(sta, tidstats, i);
2282                 }
2283         }
2284
2285         if (ieee80211_vif_is_mesh(&sdata->vif)) {
2286 #ifdef CONFIG_MAC80211_MESH
2287                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2288                                  BIT_ULL(NL80211_STA_INFO_PLID) |
2289                                  BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2290                                  BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2291                                  BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2292                                  BIT_ULL(NL80211_STA_INFO_NONPEER_PM);
2293
2294                 sinfo->llid = sta->mesh->llid;
2295                 sinfo->plid = sta->mesh->plid;
2296                 sinfo->plink_state = sta->mesh->plink_state;
2297                 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2298                         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2299                         sinfo->t_offset = sta->mesh->t_offset;
2300                 }
2301                 sinfo->local_pm = sta->mesh->local_pm;
2302                 sinfo->peer_pm = sta->mesh->peer_pm;
2303                 sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2304 #endif
2305         }
2306
2307         sinfo->bss_param.flags = 0;
2308         if (sdata->vif.bss_conf.use_cts_prot)
2309                 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2310         if (sdata->vif.bss_conf.use_short_preamble)
2311                 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2312         if (sdata->vif.bss_conf.use_short_slot)
2313                 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2314         sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2315         sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2316
2317         sinfo->sta_flags.set = 0;
2318         sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2319                                 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2320                                 BIT(NL80211_STA_FLAG_WME) |
2321                                 BIT(NL80211_STA_FLAG_MFP) |
2322                                 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2323                                 BIT(NL80211_STA_FLAG_ASSOCIATED) |
2324                                 BIT(NL80211_STA_FLAG_TDLS_PEER);
2325         if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2326                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2327         if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2328                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2329         if (sta->sta.wme)
2330                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2331         if (test_sta_flag(sta, WLAN_STA_MFP))
2332                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2333         if (test_sta_flag(sta, WLAN_STA_AUTH))
2334                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2335         if (test_sta_flag(sta, WLAN_STA_ASSOC))
2336                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2337         if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2338                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2339
2340         thr = sta_get_expected_throughput(sta);
2341
2342         if (thr != 0) {
2343                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2344                 sinfo->expected_throughput = thr;
2345         }
2346
2347         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2348             sta->status_stats.ack_signal_filled) {
2349                 sinfo->ack_signal = sta->status_stats.last_ack_signal;
2350                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2351         }
2352
2353         if (ieee80211_hw_check(&sta->local->hw, REPORTS_TX_ACK_STATUS) &&
2354             !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_DATA_ACK_SIGNAL_AVG))) {
2355                 sinfo->avg_ack_signal =
2356                         -(s8)ewma_avg_signal_read(
2357                                 &sta->status_stats.avg_ack_signal);
2358                 sinfo->filled |=
2359                         BIT_ULL(NL80211_STA_INFO_DATA_ACK_SIGNAL_AVG);
2360         }
2361 }
2362
2363 u32 sta_get_expected_throughput(struct sta_info *sta)
2364 {
2365         struct ieee80211_sub_if_data *sdata = sta->sdata;
2366         struct ieee80211_local *local = sdata->local;
2367         struct rate_control_ref *ref = NULL;
2368         u32 thr = 0;
2369
2370         if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
2371                 ref = local->rate_ctrl;
2372
2373         /* check if the driver has a SW RC implementation */
2374         if (ref && ref->ops->get_expected_throughput)
2375                 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2376         else
2377                 thr = drv_get_expected_throughput(local, sta);
2378
2379         return thr;
2380 }
2381
2382 unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2383 {
2384         struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2385
2386         if (!sta->status_stats.last_ack ||
2387             time_after(stats->last_rx, sta->status_stats.last_ack))
2388                 return stats->last_rx;
2389         return sta->status_stats.last_ack;
2390 }
2391
2392 static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2393 {
2394         if (!sta->sdata->local->ops->wake_tx_queue)
2395                 return;
2396
2397         if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2398                 sta->cparams.target = MS2TIME(50);
2399                 sta->cparams.interval = MS2TIME(300);
2400                 sta->cparams.ecn = false;
2401         } else {
2402                 sta->cparams.target = MS2TIME(20);
2403                 sta->cparams.interval = MS2TIME(100);
2404                 sta->cparams.ecn = true;
2405         }
2406 }
2407
2408 void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2409                                            u32 thr)
2410 {
2411         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2412
2413         sta_update_codel_params(sta, thr);
2414 }