GNU Linux-libre 4.19.286-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         return 0;
633  out_remove:
634         sta_info_hash_del(local, sta);
635         list_del_rcu(&sta->list);
636  out_drop_sta:
637         local->num_sta--;
638         synchronize_net();
639         cleanup_single_sta(sta);
640  out_err:
641         mutex_unlock(&local->sta_mtx);
642         kfree(sinfo);
643         rcu_read_lock();
644         return err;
645 }
646
647 int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
648 {
649         struct ieee80211_local *local = sta->local;
650         int err;
651
652         might_sleep();
653
654         mutex_lock(&local->sta_mtx);
655
656         err = sta_info_insert_check(sta);
657         if (err) {
658                 sta_info_free(local, sta);
659                 mutex_unlock(&local->sta_mtx);
660                 rcu_read_lock();
661                 return err;
662         }
663
664         return sta_info_insert_finish(sta);
665 }
666
667 int sta_info_insert(struct sta_info *sta)
668 {
669         int err = sta_info_insert_rcu(sta);
670
671         rcu_read_unlock();
672
673         return err;
674 }
675
676 static inline void __bss_tim_set(u8 *tim, u16 id)
677 {
678         /*
679          * This format has been mandated by the IEEE specifications,
680          * so this line may not be changed to use the __set_bit() format.
681          */
682         tim[id / 8] |= (1 << (id % 8));
683 }
684
685 static inline void __bss_tim_clear(u8 *tim, u16 id)
686 {
687         /*
688          * This format has been mandated by the IEEE specifications,
689          * so this line may not be changed to use the __clear_bit() format.
690          */
691         tim[id / 8] &= ~(1 << (id % 8));
692 }
693
694 static inline bool __bss_tim_get(u8 *tim, u16 id)
695 {
696         /*
697          * This format has been mandated by the IEEE specifications,
698          * so this line may not be changed to use the test_bit() format.
699          */
700         return tim[id / 8] & (1 << (id % 8));
701 }
702
703 static unsigned long ieee80211_tids_for_ac(int ac)
704 {
705         /* If we ever support TIDs > 7, this obviously needs to be adjusted */
706         switch (ac) {
707         case IEEE80211_AC_VO:
708                 return BIT(6) | BIT(7);
709         case IEEE80211_AC_VI:
710                 return BIT(4) | BIT(5);
711         case IEEE80211_AC_BE:
712                 return BIT(0) | BIT(3);
713         case IEEE80211_AC_BK:
714                 return BIT(1) | BIT(2);
715         default:
716                 WARN_ON(1);
717                 return 0;
718         }
719 }
720
721 static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
722 {
723         struct ieee80211_local *local = sta->local;
724         struct ps_data *ps;
725         bool indicate_tim = false;
726         u8 ignore_for_tim = sta->sta.uapsd_queues;
727         int ac;
728         u16 id = sta->sta.aid;
729
730         if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
731             sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
732                 if (WARN_ON_ONCE(!sta->sdata->bss))
733                         return;
734
735                 ps = &sta->sdata->bss->ps;
736 #ifdef CONFIG_MAC80211_MESH
737         } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
738                 ps = &sta->sdata->u.mesh.ps;
739 #endif
740         } else {
741                 return;
742         }
743
744         /* No need to do anything if the driver does all */
745         if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
746                 return;
747
748         if (sta->dead)
749                 goto done;
750
751         /*
752          * If all ACs are delivery-enabled then we should build
753          * the TIM bit for all ACs anyway; if only some are then
754          * we ignore those and build the TIM bit using only the
755          * non-enabled ones.
756          */
757         if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
758                 ignore_for_tim = 0;
759
760         if (ignore_pending)
761                 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
762
763         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
764                 unsigned long tids;
765
766                 if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
767                         continue;
768
769                 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
770                                 !skb_queue_empty(&sta->ps_tx_buf[ac]);
771                 if (indicate_tim)
772                         break;
773
774                 tids = ieee80211_tids_for_ac(ac);
775
776                 indicate_tim |=
777                         sta->driver_buffered_tids & tids;
778                 indicate_tim |=
779                         sta->txq_buffered_tids & tids;
780         }
781
782  done:
783         spin_lock_bh(&local->tim_lock);
784
785         if (indicate_tim == __bss_tim_get(ps->tim, id))
786                 goto out_unlock;
787
788         if (indicate_tim)
789                 __bss_tim_set(ps->tim, id);
790         else
791                 __bss_tim_clear(ps->tim, id);
792
793         if (local->ops->set_tim && !WARN_ON(sta->dead)) {
794                 local->tim_in_locked_section = true;
795                 drv_set_tim(local, &sta->sta, indicate_tim);
796                 local->tim_in_locked_section = false;
797         }
798
799 out_unlock:
800         spin_unlock_bh(&local->tim_lock);
801 }
802
803 void sta_info_recalc_tim(struct sta_info *sta)
804 {
805         __sta_info_recalc_tim(sta, false);
806 }
807
808 static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
809 {
810         struct ieee80211_tx_info *info;
811         int timeout;
812
813         if (!skb)
814                 return false;
815
816         info = IEEE80211_SKB_CB(skb);
817
818         /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
819         timeout = (sta->listen_interval *
820                    sta->sdata->vif.bss_conf.beacon_int *
821                    32 / 15625) * HZ;
822         if (timeout < STA_TX_BUFFER_EXPIRE)
823                 timeout = STA_TX_BUFFER_EXPIRE;
824         return time_after(jiffies, info->control.jiffies + timeout);
825 }
826
827
828 static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
829                                                 struct sta_info *sta, int ac)
830 {
831         unsigned long flags;
832         struct sk_buff *skb;
833
834         /*
835          * First check for frames that should expire on the filtered
836          * queue. Frames here were rejected by the driver and are on
837          * a separate queue to avoid reordering with normal PS-buffered
838          * frames. They also aren't accounted for right now in the
839          * total_ps_buffered counter.
840          */
841         for (;;) {
842                 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
843                 skb = skb_peek(&sta->tx_filtered[ac]);
844                 if (sta_info_buffer_expired(sta, skb))
845                         skb = __skb_dequeue(&sta->tx_filtered[ac]);
846                 else
847                         skb = NULL;
848                 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
849
850                 /*
851                  * Frames are queued in order, so if this one
852                  * hasn't expired yet we can stop testing. If
853                  * we actually reached the end of the queue we
854                  * also need to stop, of course.
855                  */
856                 if (!skb)
857                         break;
858                 ieee80211_free_txskb(&local->hw, skb);
859         }
860
861         /*
862          * Now also check the normal PS-buffered queue, this will
863          * only find something if the filtered queue was emptied
864          * since the filtered frames are all before the normal PS
865          * buffered frames.
866          */
867         for (;;) {
868                 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
869                 skb = skb_peek(&sta->ps_tx_buf[ac]);
870                 if (sta_info_buffer_expired(sta, skb))
871                         skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
872                 else
873                         skb = NULL;
874                 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
875
876                 /*
877                  * frames are queued in order, so if this one
878                  * hasn't expired yet (or we reached the end of
879                  * the queue) we can stop testing
880                  */
881                 if (!skb)
882                         break;
883
884                 local->total_ps_buffered--;
885                 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
886                        sta->sta.addr);
887                 ieee80211_free_txskb(&local->hw, skb);
888         }
889
890         /*
891          * Finally, recalculate the TIM bit for this station -- it might
892          * now be clear because the station was too slow to retrieve its
893          * frames.
894          */
895         sta_info_recalc_tim(sta);
896
897         /*
898          * Return whether there are any frames still buffered, this is
899          * used to check whether the cleanup timer still needs to run,
900          * if there are no frames we don't need to rearm the timer.
901          */
902         return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
903                  skb_queue_empty(&sta->tx_filtered[ac]));
904 }
905
906 static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
907                                              struct sta_info *sta)
908 {
909         bool have_buffered = false;
910         int ac;
911
912         /* This is only necessary for stations on BSS/MBSS interfaces */
913         if (!sta->sdata->bss &&
914             !ieee80211_vif_is_mesh(&sta->sdata->vif))
915                 return false;
916
917         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
918                 have_buffered |=
919                         sta_info_cleanup_expire_buffered_ac(local, sta, ac);
920
921         return have_buffered;
922 }
923
924 static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
925 {
926         struct ieee80211_local *local;
927         struct ieee80211_sub_if_data *sdata;
928         int ret;
929
930         might_sleep();
931
932         if (!sta)
933                 return -ENOENT;
934
935         local = sta->local;
936         sdata = sta->sdata;
937
938         lockdep_assert_held(&local->sta_mtx);
939
940         /*
941          * Before removing the station from the driver and
942          * rate control, it might still start new aggregation
943          * sessions -- block that to make sure the tear-down
944          * will be sufficient.
945          */
946         set_sta_flag(sta, WLAN_STA_BLOCK_BA);
947         ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
948
949         /*
950          * Before removing the station from the driver there might be pending
951          * rx frames on RSS queues sent prior to the disassociation - wait for
952          * all such frames to be processed.
953          */
954         drv_sync_rx_queues(local, sta);
955
956         ret = sta_info_hash_del(local, sta);
957         if (WARN_ON(ret))
958                 return ret;
959
960         /*
961          * for TDLS peers, make sure to return to the base channel before
962          * removal.
963          */
964         if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
965                 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
966                 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
967         }
968
969         list_del_rcu(&sta->list);
970         sta->removed = true;
971
972         if (sta->uploaded)
973                 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
974
975         if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
976             rcu_access_pointer(sdata->u.vlan.sta) == sta)
977                 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
978
979         return 0;
980 }
981
982 static void __sta_info_destroy_part2(struct sta_info *sta)
983 {
984         struct ieee80211_local *local = sta->local;
985         struct ieee80211_sub_if_data *sdata = sta->sdata;
986         struct station_info *sinfo;
987         int ret;
988
989         /*
990          * NOTE: This assumes at least synchronize_net() was done
991          *       after _part1 and before _part2!
992          */
993
994         might_sleep();
995         lockdep_assert_held(&local->sta_mtx);
996
997         if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
998                 ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
999                 WARN_ON_ONCE(ret);
1000         }
1001
1002         /* now keys can no longer be reached */
1003         ieee80211_free_sta_keys(local, sta);
1004
1005         /* disable TIM bit - last chance to tell driver */
1006         __sta_info_recalc_tim(sta, true);
1007
1008         sta->dead = true;
1009
1010         local->num_sta--;
1011         local->sta_generation++;
1012
1013         while (sta->sta_state > IEEE80211_STA_NONE) {
1014                 ret = sta_info_move_state(sta, sta->sta_state - 1);
1015                 if (ret) {
1016                         WARN_ON_ONCE(1);
1017                         break;
1018                 }
1019         }
1020
1021         if (sta->uploaded) {
1022                 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1023                                     IEEE80211_STA_NOTEXIST);
1024                 WARN_ON_ONCE(ret != 0);
1025         }
1026
1027         sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1028
1029         sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
1030         if (sinfo)
1031                 sta_set_sinfo(sta, sinfo, true);
1032         cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
1033         kfree(sinfo);
1034
1035         rate_control_remove_sta_debugfs(sta);
1036         ieee80211_sta_debugfs_remove(sta);
1037
1038         ieee80211_destroy_frag_cache(&sta->frags);
1039
1040         cleanup_single_sta(sta);
1041 }
1042
1043 int __must_check __sta_info_destroy(struct sta_info *sta)
1044 {
1045         int err = __sta_info_destroy_part1(sta);
1046
1047         if (err)
1048                 return err;
1049
1050         synchronize_net();
1051
1052         __sta_info_destroy_part2(sta);
1053
1054         return 0;
1055 }
1056
1057 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
1058 {
1059         struct sta_info *sta;
1060         int ret;
1061
1062         mutex_lock(&sdata->local->sta_mtx);
1063         sta = sta_info_get(sdata, addr);
1064         ret = __sta_info_destroy(sta);
1065         mutex_unlock(&sdata->local->sta_mtx);
1066
1067         return ret;
1068 }
1069
1070 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
1071                               const u8 *addr)
1072 {
1073         struct sta_info *sta;
1074         int ret;
1075
1076         mutex_lock(&sdata->local->sta_mtx);
1077         sta = sta_info_get_bss(sdata, addr);
1078         ret = __sta_info_destroy(sta);
1079         mutex_unlock(&sdata->local->sta_mtx);
1080
1081         return ret;
1082 }
1083
1084 static void sta_info_cleanup(struct timer_list *t)
1085 {
1086         struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1087         struct sta_info *sta;
1088         bool timer_needed = false;
1089
1090         rcu_read_lock();
1091         list_for_each_entry_rcu(sta, &local->sta_list, list)
1092                 if (sta_info_cleanup_expire_buffered(local, sta))
1093                         timer_needed = true;
1094         rcu_read_unlock();
1095
1096         if (local->quiescing)
1097                 return;
1098
1099         if (!timer_needed)
1100                 return;
1101
1102         mod_timer(&local->sta_cleanup,
1103                   round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1104 }
1105
1106 int sta_info_init(struct ieee80211_local *local)
1107 {
1108         int err;
1109
1110         err = rhltable_init(&local->sta_hash, &sta_rht_params);
1111         if (err)
1112                 return err;
1113
1114         spin_lock_init(&local->tim_lock);
1115         mutex_init(&local->sta_mtx);
1116         INIT_LIST_HEAD(&local->sta_list);
1117
1118         timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
1119         return 0;
1120 }
1121
1122 void sta_info_stop(struct ieee80211_local *local)
1123 {
1124         del_timer_sync(&local->sta_cleanup);
1125         rhltable_destroy(&local->sta_hash);
1126 }
1127
1128
1129 int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1130 {
1131         struct ieee80211_local *local = sdata->local;
1132         struct sta_info *sta, *tmp;
1133         LIST_HEAD(free_list);
1134         int ret = 0;
1135
1136         might_sleep();
1137
1138         WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1139         WARN_ON(vlans && !sdata->bss);
1140
1141         mutex_lock(&local->sta_mtx);
1142         list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1143                 if (sdata == sta->sdata ||
1144                     (vlans && sdata->bss == sta->sdata->bss)) {
1145                         if (!WARN_ON(__sta_info_destroy_part1(sta)))
1146                                 list_add(&sta->free_list, &free_list);
1147                         ret++;
1148                 }
1149         }
1150
1151         if (!list_empty(&free_list)) {
1152                 synchronize_net();
1153                 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1154                         __sta_info_destroy_part2(sta);
1155         }
1156         mutex_unlock(&local->sta_mtx);
1157
1158         return ret;
1159 }
1160
1161 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1162                           unsigned long exp_time)
1163 {
1164         struct ieee80211_local *local = sdata->local;
1165         struct sta_info *sta, *tmp;
1166
1167         mutex_lock(&local->sta_mtx);
1168
1169         list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1170                 unsigned long last_active = ieee80211_sta_last_active(sta);
1171
1172                 if (sdata != sta->sdata)
1173                         continue;
1174
1175                 if (time_is_before_jiffies(last_active + exp_time)) {
1176                         sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1177                                 sta->sta.addr);
1178
1179                         if (ieee80211_vif_is_mesh(&sdata->vif) &&
1180                             test_sta_flag(sta, WLAN_STA_PS_STA))
1181                                 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1182
1183                         WARN_ON(__sta_info_destroy(sta));
1184                 }
1185         }
1186
1187         mutex_unlock(&local->sta_mtx);
1188 }
1189
1190 struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1191                                                    const u8 *addr,
1192                                                    const u8 *localaddr)
1193 {
1194         struct ieee80211_local *local = hw_to_local(hw);
1195         struct rhlist_head *tmp;
1196         struct sta_info *sta;
1197
1198         /*
1199          * Just return a random station if localaddr is NULL
1200          * ... first in list.
1201          */
1202         for_each_sta_info(local, addr, sta, tmp) {
1203                 if (localaddr &&
1204                     !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1205                         continue;
1206                 if (!sta->uploaded)
1207                         return NULL;
1208                 return &sta->sta;
1209         }
1210
1211         return NULL;
1212 }
1213 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
1214
1215 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1216                                          const u8 *addr)
1217 {
1218         struct sta_info *sta;
1219
1220         if (!vif)
1221                 return NULL;
1222
1223         sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1224         if (!sta)
1225                 return NULL;
1226
1227         if (!sta->uploaded)
1228                 return NULL;
1229
1230         return &sta->sta;
1231 }
1232 EXPORT_SYMBOL(ieee80211_find_sta);
1233
1234 /* powersave support code */
1235 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1236 {
1237         struct ieee80211_sub_if_data *sdata = sta->sdata;
1238         struct ieee80211_local *local = sdata->local;
1239         struct sk_buff_head pending;
1240         int filtered = 0, buffered = 0, ac, i;
1241         unsigned long flags;
1242         struct ps_data *ps;
1243
1244         if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1245                 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1246                                      u.ap);
1247
1248         if (sdata->vif.type == NL80211_IFTYPE_AP)
1249                 ps = &sdata->bss->ps;
1250         else if (ieee80211_vif_is_mesh(&sdata->vif))
1251                 ps = &sdata->u.mesh.ps;
1252         else
1253                 return;
1254
1255         clear_sta_flag(sta, WLAN_STA_SP);
1256
1257         BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1258         sta->driver_buffered_tids = 0;
1259         sta->txq_buffered_tids = 0;
1260
1261         if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1262                 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1263
1264         if (sta->sta.txq[0]) {
1265                 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1266                         if (!txq_has_queue(sta->sta.txq[i]))
1267                                 continue;
1268
1269                         drv_wake_tx_queue(local, to_txq_info(sta->sta.txq[i]));
1270                 }
1271         }
1272
1273         skb_queue_head_init(&pending);
1274
1275         /* sync with ieee80211_tx_h_unicast_ps_buf */
1276         spin_lock(&sta->ps_lock);
1277         /* Send all buffered frames to the station */
1278         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1279                 int count = skb_queue_len(&pending), tmp;
1280
1281                 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1282                 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1283                 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1284                 tmp = skb_queue_len(&pending);
1285                 filtered += tmp - count;
1286                 count = tmp;
1287
1288                 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1289                 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1290                 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1291                 tmp = skb_queue_len(&pending);
1292                 buffered += tmp - count;
1293         }
1294
1295         ieee80211_add_pending_skbs(local, &pending);
1296
1297         /* now we're no longer in the deliver code */
1298         clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1299
1300         /* The station might have polled and then woken up before we responded,
1301          * so clear these flags now to avoid them sticking around.
1302          */
1303         clear_sta_flag(sta, WLAN_STA_PSPOLL);
1304         clear_sta_flag(sta, WLAN_STA_UAPSD);
1305         spin_unlock(&sta->ps_lock);
1306
1307         atomic_dec(&ps->num_sta_ps);
1308
1309         /* This station just woke up and isn't aware of our SMPS state */
1310         if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1311             !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1312                                            sdata->smps_mode) &&
1313             sta->known_smps_mode != sdata->bss->req_smps &&
1314             sta_info_tx_streams(sta) != 1) {
1315                 ht_dbg(sdata,
1316                        "%pM just woke up and MIMO capable - update SMPS\n",
1317                        sta->sta.addr);
1318                 ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1319                                            sta->sta.addr,
1320                                            sdata->vif.bss_conf.bssid);
1321         }
1322
1323         local->total_ps_buffered -= buffered;
1324
1325         sta_info_recalc_tim(sta);
1326
1327         ps_dbg(sdata,
1328                "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1329                sta->sta.addr, sta->sta.aid, filtered, buffered);
1330
1331         ieee80211_check_fast_xmit(sta);
1332 }
1333
1334 static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1335                                          enum ieee80211_frame_release_type reason,
1336                                          bool call_driver, bool more_data)
1337 {
1338         struct ieee80211_sub_if_data *sdata = sta->sdata;
1339         struct ieee80211_local *local = sdata->local;
1340         struct ieee80211_qos_hdr *nullfunc;
1341         struct sk_buff *skb;
1342         int size = sizeof(*nullfunc);
1343         __le16 fc;
1344         bool qos = sta->sta.wme;
1345         struct ieee80211_tx_info *info;
1346         struct ieee80211_chanctx_conf *chanctx_conf;
1347
1348         if (qos) {
1349                 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1350                                  IEEE80211_STYPE_QOS_NULLFUNC |
1351                                  IEEE80211_FCTL_FROMDS);
1352         } else {
1353                 size -= 2;
1354                 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1355                                  IEEE80211_STYPE_NULLFUNC |
1356                                  IEEE80211_FCTL_FROMDS);
1357         }
1358
1359         skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1360         if (!skb)
1361                 return;
1362
1363         skb_reserve(skb, local->hw.extra_tx_headroom);
1364
1365         nullfunc = skb_put(skb, size);
1366         nullfunc->frame_control = fc;
1367         nullfunc->duration_id = 0;
1368         memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1369         memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1370         memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1371         nullfunc->seq_ctrl = 0;
1372
1373         skb->priority = tid;
1374         skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
1375         if (qos) {
1376                 nullfunc->qos_ctrl = cpu_to_le16(tid);
1377
1378                 if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1379                         nullfunc->qos_ctrl |=
1380                                 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1381                         if (more_data)
1382                                 nullfunc->frame_control |=
1383                                         cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1384                 }
1385         }
1386
1387         info = IEEE80211_SKB_CB(skb);
1388
1389         /*
1390          * Tell TX path to send this frame even though the
1391          * STA may still remain is PS mode after this frame
1392          * exchange. Also set EOSP to indicate this packet
1393          * ends the poll/service period.
1394          */
1395         info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1396                        IEEE80211_TX_STATUS_EOSP |
1397                        IEEE80211_TX_CTL_REQ_TX_STATUS;
1398
1399         info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1400
1401         if (call_driver)
1402                 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1403                                           reason, false);
1404
1405         skb->dev = sdata->dev;
1406
1407         rcu_read_lock();
1408         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1409         if (WARN_ON(!chanctx_conf)) {
1410                 rcu_read_unlock();
1411                 kfree_skb(skb);
1412                 return;
1413         }
1414
1415         info->band = chanctx_conf->def.chan->band;
1416         ieee80211_xmit(sdata, sta, skb, 0);
1417         rcu_read_unlock();
1418 }
1419
1420 static int find_highest_prio_tid(unsigned long tids)
1421 {
1422         /* lower 3 TIDs aren't ordered perfectly */
1423         if (tids & 0xF8)
1424                 return fls(tids) - 1;
1425         /* TID 0 is BE just like TID 3 */
1426         if (tids & BIT(0))
1427                 return 0;
1428         return fls(tids) - 1;
1429 }
1430
1431 /* Indicates if the MORE_DATA bit should be set in the last
1432  * frame obtained by ieee80211_sta_ps_get_frames.
1433  * Note that driver_release_tids is relevant only if
1434  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
1435  */
1436 static bool
1437 ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
1438                            enum ieee80211_frame_release_type reason,
1439                            unsigned long driver_release_tids)
1440 {
1441         int ac;
1442
1443         /* If the driver has data on more than one TID then
1444          * certainly there's more data if we release just a
1445          * single frame now (from a single TID). This will
1446          * only happen for PS-Poll.
1447          */
1448         if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1449             hweight16(driver_release_tids) > 1)
1450                 return true;
1451
1452         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1453                 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1454                         continue;
1455
1456                 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1457                     !skb_queue_empty(&sta->ps_tx_buf[ac]))
1458                         return true;
1459         }
1460
1461         return false;
1462 }
1463
1464 static void
1465 ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
1466                             enum ieee80211_frame_release_type reason,
1467                             struct sk_buff_head *frames,
1468                             unsigned long *driver_release_tids)
1469 {
1470         struct ieee80211_sub_if_data *sdata = sta->sdata;
1471         struct ieee80211_local *local = sdata->local;
1472         int ac;
1473
1474         /* Get response frame(s) and more data bit for the last one. */
1475         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1476                 unsigned long tids;
1477
1478                 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1479                         continue;
1480
1481                 tids = ieee80211_tids_for_ac(ac);
1482
1483                 /* if we already have frames from software, then we can't also
1484                  * release from hardware queues
1485                  */
1486                 if (skb_queue_empty(frames)) {
1487                         *driver_release_tids |=
1488                                 sta->driver_buffered_tids & tids;
1489                         *driver_release_tids |= sta->txq_buffered_tids & tids;
1490                 }
1491
1492                 if (!*driver_release_tids) {
1493                         struct sk_buff *skb;
1494
1495                         while (n_frames > 0) {
1496                                 skb = skb_dequeue(&sta->tx_filtered[ac]);
1497                                 if (!skb) {
1498                                         skb = skb_dequeue(
1499                                                 &sta->ps_tx_buf[ac]);
1500                                         if (skb)
1501                                                 local->total_ps_buffered--;
1502                                 }
1503                                 if (!skb)
1504                                         break;
1505                                 n_frames--;
1506                                 __skb_queue_tail(frames, skb);
1507                         }
1508                 }
1509
1510                 /* If we have more frames buffered on this AC, then abort the
1511                  * loop since we can't send more data from other ACs before
1512                  * the buffered frames from this.
1513                  */
1514                 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1515                     !skb_queue_empty(&sta->ps_tx_buf[ac]))
1516                         break;
1517         }
1518 }
1519
1520 static void
1521 ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1522                                   int n_frames, u8 ignored_acs,
1523                                   enum ieee80211_frame_release_type reason)
1524 {
1525         struct ieee80211_sub_if_data *sdata = sta->sdata;
1526         struct ieee80211_local *local = sdata->local;
1527         unsigned long driver_release_tids = 0;
1528         struct sk_buff_head frames;
1529         bool more_data;
1530
1531         /* Service or PS-Poll period starts */
1532         set_sta_flag(sta, WLAN_STA_SP);
1533
1534         __skb_queue_head_init(&frames);
1535
1536         ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
1537                                     &frames, &driver_release_tids);
1538
1539         more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
1540
1541         if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
1542                 driver_release_tids =
1543                         BIT(find_highest_prio_tid(driver_release_tids));
1544
1545         if (skb_queue_empty(&frames) && !driver_release_tids) {
1546                 int tid, ac;
1547
1548                 /*
1549                  * For PS-Poll, this can only happen due to a race condition
1550                  * when we set the TIM bit and the station notices it, but
1551                  * before it can poll for the frame we expire it.
1552                  *
1553                  * For uAPSD, this is said in the standard (11.2.1.5 h):
1554                  *      At each unscheduled SP for a non-AP STA, the AP shall
1555                  *      attempt to transmit at least one MSDU or MMPDU, but no
1556                  *      more than the value specified in the Max SP Length field
1557                  *      in the QoS Capability element from delivery-enabled ACs,
1558                  *      that are destined for the non-AP STA.
1559                  *
1560                  * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1561                  */
1562
1563                 /* This will evaluate to 1, 3, 5 or 7. */
1564                 for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1565                         if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1566                                 break;
1567                 tid = 7 - 2 * ac;
1568
1569                 ieee80211_send_null_response(sta, tid, reason, true, false);
1570         } else if (!driver_release_tids) {
1571                 struct sk_buff_head pending;
1572                 struct sk_buff *skb;
1573                 int num = 0;
1574                 u16 tids = 0;
1575                 bool need_null = false;
1576
1577                 skb_queue_head_init(&pending);
1578
1579                 while ((skb = __skb_dequeue(&frames))) {
1580                         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1581                         struct ieee80211_hdr *hdr = (void *) skb->data;
1582                         u8 *qoshdr = NULL;
1583
1584                         num++;
1585
1586                         /*
1587                          * Tell TX path to send this frame even though the
1588                          * STA may still remain is PS mode after this frame
1589                          * exchange.
1590                          */
1591                         info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1592                         info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1593
1594                         /*
1595                          * Use MoreData flag to indicate whether there are
1596                          * more buffered frames for this STA
1597                          */
1598                         if (more_data || !skb_queue_empty(&frames))
1599                                 hdr->frame_control |=
1600                                         cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1601                         else
1602                                 hdr->frame_control &=
1603                                         cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1604
1605                         if (ieee80211_is_data_qos(hdr->frame_control) ||
1606                             ieee80211_is_qos_nullfunc(hdr->frame_control))
1607                                 qoshdr = ieee80211_get_qos_ctl(hdr);
1608
1609                         tids |= BIT(skb->priority);
1610
1611                         __skb_queue_tail(&pending, skb);
1612
1613                         /* end service period after last frame or add one */
1614                         if (!skb_queue_empty(&frames))
1615                                 continue;
1616
1617                         if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1618                                 /* for PS-Poll, there's only one frame */
1619                                 info->flags |= IEEE80211_TX_STATUS_EOSP |
1620                                                IEEE80211_TX_CTL_REQ_TX_STATUS;
1621                                 break;
1622                         }
1623
1624                         /* For uAPSD, things are a bit more complicated. If the
1625                          * last frame has a QoS header (i.e. is a QoS-data or
1626                          * QoS-nulldata frame) then just set the EOSP bit there
1627                          * and be done.
1628                          * If the frame doesn't have a QoS header (which means
1629                          * it should be a bufferable MMPDU) then we can't set
1630                          * the EOSP bit in the QoS header; add a QoS-nulldata
1631                          * frame to the list to send it after the MMPDU.
1632                          *
1633                          * Note that this code is only in the mac80211-release
1634                          * code path, we assume that the driver will not buffer
1635                          * anything but QoS-data frames, or if it does, will
1636                          * create the QoS-nulldata frame by itself if needed.
1637                          *
1638                          * Cf. 802.11-2012 10.2.1.10 (c).
1639                          */
1640                         if (qoshdr) {
1641                                 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
1642
1643                                 info->flags |= IEEE80211_TX_STATUS_EOSP |
1644                                                IEEE80211_TX_CTL_REQ_TX_STATUS;
1645                         } else {
1646                                 /* The standard isn't completely clear on this
1647                                  * as it says the more-data bit should be set
1648                                  * if there are more BUs. The QoS-Null frame
1649                                  * we're about to send isn't buffered yet, we
1650                                  * only create it below, but let's pretend it
1651                                  * was buffered just in case some clients only
1652                                  * expect more-data=0 when eosp=1.
1653                                  */
1654                                 hdr->frame_control |=
1655                                         cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1656                                 need_null = true;
1657                                 num++;
1658                         }
1659                         break;
1660                 }
1661
1662                 drv_allow_buffered_frames(local, sta, tids, num,
1663                                           reason, more_data);
1664
1665                 ieee80211_add_pending_skbs(local, &pending);
1666
1667                 if (need_null)
1668                         ieee80211_send_null_response(
1669                                 sta, find_highest_prio_tid(tids),
1670                                 reason, false, false);
1671
1672                 sta_info_recalc_tim(sta);
1673         } else {
1674                 int tid;
1675
1676                 /*
1677                  * We need to release a frame that is buffered somewhere in the
1678                  * driver ... it'll have to handle that.
1679                  * Note that the driver also has to check the number of frames
1680                  * on the TIDs we're releasing from - if there are more than
1681                  * n_frames it has to set the more-data bit (if we didn't ask
1682                  * it to set it anyway due to other buffered frames); if there
1683                  * are fewer than n_frames it has to make sure to adjust that
1684                  * to allow the service period to end properly.
1685                  */
1686                 drv_release_buffered_frames(local, sta, driver_release_tids,
1687                                             n_frames, reason, more_data);
1688
1689                 /*
1690                  * Note that we don't recalculate the TIM bit here as it would
1691                  * most likely have no effect at all unless the driver told us
1692                  * that the TID(s) became empty before returning here from the
1693                  * release function.
1694                  * Either way, however, when the driver tells us that the TID(s)
1695                  * became empty or we find that a txq became empty, we'll do the
1696                  * TIM recalculation.
1697                  */
1698
1699                 if (!sta->sta.txq[0])
1700                         return;
1701
1702                 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1703                         if (!(driver_release_tids & BIT(tid)) ||
1704                             txq_has_queue(sta->sta.txq[tid]))
1705                                 continue;
1706
1707                         sta_info_recalc_tim(sta);
1708                         break;
1709                 }
1710         }
1711 }
1712
1713 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1714 {
1715         u8 ignore_for_response = sta->sta.uapsd_queues;
1716
1717         /*
1718          * If all ACs are delivery-enabled then we should reply
1719          * from any of them, if only some are enabled we reply
1720          * only from the non-enabled ones.
1721          */
1722         if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1723                 ignore_for_response = 0;
1724
1725         ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1726                                           IEEE80211_FRAME_RELEASE_PSPOLL);
1727 }
1728
1729 void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1730 {
1731         int n_frames = sta->sta.max_sp;
1732         u8 delivery_enabled = sta->sta.uapsd_queues;
1733
1734         /*
1735          * If we ever grow support for TSPEC this might happen if
1736          * the TSPEC update from hostapd comes in between a trigger
1737          * frame setting WLAN_STA_UAPSD in the RX path and this
1738          * actually getting called.
1739          */
1740         if (!delivery_enabled)
1741                 return;
1742
1743         switch (sta->sta.max_sp) {
1744         case 1:
1745                 n_frames = 2;
1746                 break;
1747         case 2:
1748                 n_frames = 4;
1749                 break;
1750         case 3:
1751                 n_frames = 6;
1752                 break;
1753         case 0:
1754                 /* XXX: what is a good value? */
1755                 n_frames = 128;
1756                 break;
1757         }
1758
1759         ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1760                                           IEEE80211_FRAME_RELEASE_UAPSD);
1761 }
1762
1763 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1764                                struct ieee80211_sta *pubsta, bool block)
1765 {
1766         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1767
1768         trace_api_sta_block_awake(sta->local, pubsta, block);
1769
1770         if (block) {
1771                 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
1772                 ieee80211_clear_fast_xmit(sta);
1773                 return;
1774         }
1775
1776         if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1777                 return;
1778
1779         if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
1780                 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1781                 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1782                 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1783         } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
1784                    test_sta_flag(sta, WLAN_STA_UAPSD)) {
1785                 /* must be asleep in this case */
1786                 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1787                 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1788         } else {
1789                 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1790                 ieee80211_check_fast_xmit(sta);
1791         }
1792 }
1793 EXPORT_SYMBOL(ieee80211_sta_block_awake);
1794
1795 void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
1796 {
1797         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1798         struct ieee80211_local *local = sta->local;
1799
1800         trace_api_eosp(local, pubsta);
1801
1802         clear_sta_flag(sta, WLAN_STA_SP);
1803 }
1804 EXPORT_SYMBOL(ieee80211_sta_eosp);
1805
1806 void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
1807 {
1808         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1809         enum ieee80211_frame_release_type reason;
1810         bool more_data;
1811
1812         trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
1813
1814         reason = IEEE80211_FRAME_RELEASE_UAPSD;
1815         more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
1816                                                reason, 0);
1817
1818         ieee80211_send_null_response(sta, tid, reason, false, more_data);
1819 }
1820 EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
1821
1822 void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1823                                 u8 tid, bool buffered)
1824 {
1825         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1826
1827         if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1828                 return;
1829
1830         trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
1831
1832         if (buffered)
1833                 set_bit(tid, &sta->driver_buffered_tids);
1834         else
1835                 clear_bit(tid, &sta->driver_buffered_tids);
1836
1837         sta_info_recalc_tim(sta);
1838 }
1839 EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1840
1841 int sta_info_move_state(struct sta_info *sta,
1842                         enum ieee80211_sta_state new_state)
1843 {
1844         might_sleep();
1845
1846         if (sta->sta_state == new_state)
1847                 return 0;
1848
1849         /* check allowed transitions first */
1850
1851         switch (new_state) {
1852         case IEEE80211_STA_NONE:
1853                 if (sta->sta_state != IEEE80211_STA_AUTH)
1854                         return -EINVAL;
1855                 break;
1856         case IEEE80211_STA_AUTH:
1857                 if (sta->sta_state != IEEE80211_STA_NONE &&
1858                     sta->sta_state != IEEE80211_STA_ASSOC)
1859                         return -EINVAL;
1860                 break;
1861         case IEEE80211_STA_ASSOC:
1862                 if (sta->sta_state != IEEE80211_STA_AUTH &&
1863                     sta->sta_state != IEEE80211_STA_AUTHORIZED)
1864                         return -EINVAL;
1865                 break;
1866         case IEEE80211_STA_AUTHORIZED:
1867                 if (sta->sta_state != IEEE80211_STA_ASSOC)
1868                         return -EINVAL;
1869                 break;
1870         default:
1871                 WARN(1, "invalid state %d", new_state);
1872                 return -EINVAL;
1873         }
1874
1875         sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1876                 sta->sta.addr, new_state);
1877
1878         /*
1879          * notify the driver before the actual changes so it can
1880          * fail the transition
1881          */
1882         if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1883                 int err = drv_sta_state(sta->local, sta->sdata, sta,
1884                                         sta->sta_state, new_state);
1885                 if (err)
1886                         return err;
1887         }
1888
1889         /* reflect the change in all state variables */
1890
1891         switch (new_state) {
1892         case IEEE80211_STA_NONE:
1893                 if (sta->sta_state == IEEE80211_STA_AUTH)
1894                         clear_bit(WLAN_STA_AUTH, &sta->_flags);
1895                 break;
1896         case IEEE80211_STA_AUTH:
1897                 if (sta->sta_state == IEEE80211_STA_NONE) {
1898                         set_bit(WLAN_STA_AUTH, &sta->_flags);
1899                 } else if (sta->sta_state == IEEE80211_STA_ASSOC) {
1900                         clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1901                         ieee80211_recalc_min_chandef(sta->sdata);
1902                         if (!sta->sta.support_p2p_ps)
1903                                 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1904                 }
1905                 break;
1906         case IEEE80211_STA_ASSOC:
1907                 if (sta->sta_state == IEEE80211_STA_AUTH) {
1908                         set_bit(WLAN_STA_ASSOC, &sta->_flags);
1909                         ieee80211_recalc_min_chandef(sta->sdata);
1910                         if (!sta->sta.support_p2p_ps)
1911                                 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1912                 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1913                         ieee80211_vif_dec_num_mcast(sta->sdata);
1914                         clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1915                         ieee80211_clear_fast_xmit(sta);
1916                         ieee80211_clear_fast_rx(sta);
1917                 }
1918                 break;
1919         case IEEE80211_STA_AUTHORIZED:
1920                 if (sta->sta_state == IEEE80211_STA_ASSOC) {
1921                         ieee80211_vif_inc_num_mcast(sta->sdata);
1922                         set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1923                         ieee80211_check_fast_xmit(sta);
1924                         ieee80211_check_fast_rx(sta);
1925                 }
1926                 if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
1927                     sta->sdata->vif.type == NL80211_IFTYPE_AP)
1928                         cfg80211_send_layer2_update(sta->sdata->dev,
1929                                                     sta->sta.addr);
1930                 break;
1931         default:
1932                 break;
1933         }
1934
1935         sta->sta_state = new_state;
1936
1937         return 0;
1938 }
1939
1940 u8 sta_info_tx_streams(struct sta_info *sta)
1941 {
1942         struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1943         u8 rx_streams;
1944
1945         if (!sta->sta.ht_cap.ht_supported)
1946                 return 1;
1947
1948         if (sta->sta.vht_cap.vht_supported) {
1949                 int i;
1950                 u16 tx_mcs_map =
1951                         le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1952
1953                 for (i = 7; i >= 0; i--)
1954                         if ((tx_mcs_map & (0x3 << (i * 2))) !=
1955                             IEEE80211_VHT_MCS_NOT_SUPPORTED)
1956                                 return i + 1;
1957         }
1958
1959         if (ht_cap->mcs.rx_mask[3])
1960                 rx_streams = 4;
1961         else if (ht_cap->mcs.rx_mask[2])
1962                 rx_streams = 3;
1963         else if (ht_cap->mcs.rx_mask[1])
1964                 rx_streams = 2;
1965         else
1966                 rx_streams = 1;
1967
1968         if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1969                 return rx_streams;
1970
1971         return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1972                         >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1973 }
1974
1975 static struct ieee80211_sta_rx_stats *
1976 sta_get_last_rx_stats(struct sta_info *sta)
1977 {
1978         struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
1979         struct ieee80211_local *local = sta->local;
1980         int cpu;
1981
1982         if (!ieee80211_hw_check(&local->hw, USES_RSS))
1983                 return stats;
1984
1985         for_each_possible_cpu(cpu) {
1986                 struct ieee80211_sta_rx_stats *cpustats;
1987
1988                 cpustats = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
1989
1990                 if (time_after(cpustats->last_rx, stats->last_rx))
1991                         stats = cpustats;
1992         }
1993
1994         return stats;
1995 }
1996
1997 static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
1998                                   struct rate_info *rinfo)
1999 {
2000         rinfo->bw = STA_STATS_GET(BW, rate);
2001
2002         switch (STA_STATS_GET(TYPE, rate)) {
2003         case STA_STATS_RATE_TYPE_VHT:
2004                 rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2005                 rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2006                 rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2007                 if (STA_STATS_GET(SGI, rate))
2008                         rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2009                 break;
2010         case STA_STATS_RATE_TYPE_HT:
2011                 rinfo->flags = RATE_INFO_FLAGS_MCS;
2012                 rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2013                 if (STA_STATS_GET(SGI, rate))
2014                         rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2015                 break;
2016         case STA_STATS_RATE_TYPE_LEGACY: {
2017                 struct ieee80211_supported_band *sband;
2018                 u16 brate;
2019                 unsigned int shift;
2020                 int band = STA_STATS_GET(LEGACY_BAND, rate);
2021                 int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2022
2023                 sband = local->hw.wiphy->bands[band];
2024
2025                 if (WARN_ON_ONCE(!sband->bitrates))
2026                         break;
2027
2028                 brate = sband->bitrates[rate_idx].bitrate;
2029                 if (rinfo->bw == RATE_INFO_BW_5)
2030                         shift = 2;
2031                 else if (rinfo->bw == RATE_INFO_BW_10)
2032                         shift = 1;
2033                 else
2034                         shift = 0;
2035                 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
2036                 break;
2037                 }
2038         case STA_STATS_RATE_TYPE_HE:
2039                 rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
2040                 rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
2041                 rinfo->nss = STA_STATS_GET(HE_NSS, rate);
2042                 rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
2043                 rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
2044                 rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
2045                 break;
2046         }
2047 }
2048
2049 static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
2050 {
2051         u32 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
2052
2053         if (rate == STA_STATS_RATE_INVALID)
2054                 return -EINVAL;
2055
2056         sta_stats_decode_rate(sta->local, rate, rinfo);
2057         return 0;
2058 }
2059
2060 static void sta_set_tidstats(struct sta_info *sta,
2061                              struct cfg80211_tid_stats *tidstats,
2062                              int tid)
2063 {
2064         struct ieee80211_local *local = sta->local;
2065
2066         if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2067                 unsigned int start;
2068
2069                 do {
2070                         start = u64_stats_fetch_begin(&sta->rx_stats.syncp);
2071                         tidstats->rx_msdu = sta->rx_stats.msdu[tid];
2072                 } while (u64_stats_fetch_retry(&sta->rx_stats.syncp, start));
2073
2074                 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
2075         }
2076
2077         if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
2078                 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2079                 tidstats->tx_msdu = sta->tx_stats.msdu[tid];
2080         }
2081
2082         if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
2083             ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2084                 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2085                 tidstats->tx_msdu_retries = sta->status_stats.msdu_retries[tid];
2086         }
2087
2088         if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
2089             ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2090                 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2091                 tidstats->tx_msdu_failed = sta->status_stats.msdu_failed[tid];
2092         }
2093
2094         if (local->ops->wake_tx_queue && tid < IEEE80211_NUM_TIDS) {
2095                 spin_lock_bh(&local->fq.lock);
2096                 rcu_read_lock();
2097
2098                 tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
2099                 ieee80211_fill_txq_stats(&tidstats->txq_stats,
2100                                          to_txq_info(sta->sta.txq[tid]));
2101
2102                 rcu_read_unlock();
2103                 spin_unlock_bh(&local->fq.lock);
2104         }
2105 }
2106
2107 static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2108 {
2109         unsigned int start;
2110         u64 value;
2111
2112         do {
2113                 start = u64_stats_fetch_begin(&rxstats->syncp);
2114                 value = rxstats->bytes;
2115         } while (u64_stats_fetch_retry(&rxstats->syncp, start));
2116
2117         return value;
2118 }
2119
2120 void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
2121                    bool tidstats)
2122 {
2123         struct ieee80211_sub_if_data *sdata = sta->sdata;
2124         struct ieee80211_local *local = sdata->local;
2125         u32 thr = 0;
2126         int i, ac, cpu;
2127         struct ieee80211_sta_rx_stats *last_rxstats;
2128
2129         last_rxstats = sta_get_last_rx_stats(sta);
2130
2131         sinfo->generation = sdata->local->sta_generation;
2132
2133         /* do before driver, so beacon filtering drivers have a
2134          * chance to e.g. just add the number of filtered beacons
2135          * (or just modify the value entirely, of course)
2136          */
2137         if (sdata->vif.type == NL80211_IFTYPE_STATION)
2138                 sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
2139
2140         drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2141
2142         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2143                          BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2144                          BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2145                          BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
2146                          BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2147
2148         if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2149                 sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
2150                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2151         }
2152
2153         sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
2154         sinfo->inactive_time =
2155                 jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
2156
2157         if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2158                                BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2159                 sinfo->tx_bytes = 0;
2160                 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2161                         sinfo->tx_bytes += sta->tx_stats.bytes[ac];
2162                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2163         }
2164
2165         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
2166                 sinfo->tx_packets = 0;
2167                 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2168                         sinfo->tx_packets += sta->tx_stats.packets[ac];
2169                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
2170         }
2171
2172         if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2173                                BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2174                 sinfo->rx_bytes += sta_get_stats_bytes(&sta->rx_stats);
2175
2176                 if (sta->pcpu_rx_stats) {
2177                         for_each_possible_cpu(cpu) {
2178                                 struct ieee80211_sta_rx_stats *cpurxs;
2179
2180                                 cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2181                                 sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2182                         }
2183                 }
2184
2185                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
2186         }
2187
2188         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2189                 sinfo->rx_packets = sta->rx_stats.packets;
2190                 if (sta->pcpu_rx_stats) {
2191                         for_each_possible_cpu(cpu) {
2192                                 struct ieee80211_sta_rx_stats *cpurxs;
2193
2194                                 cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2195                                 sinfo->rx_packets += cpurxs->packets;
2196                         }
2197                 }
2198                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
2199         }
2200
2201         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2202                 sinfo->tx_retries = sta->status_stats.retry_count;
2203                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
2204         }
2205
2206         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2207                 sinfo->tx_failed = sta->status_stats.retry_failed;
2208                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
2209         }
2210
2211         sinfo->rx_dropped_misc = sta->rx_stats.dropped;
2212         if (sta->pcpu_rx_stats) {
2213                 for_each_possible_cpu(cpu) {
2214                         struct ieee80211_sta_rx_stats *cpurxs;
2215
2216                         cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2217                         sinfo->rx_dropped_misc += cpurxs->dropped;
2218                 }
2219         }
2220
2221         if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2222             !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2223                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2224                                  BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2225                 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2226         }
2227
2228         if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
2229             ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2230                 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2231                         sinfo->signal = (s8)last_rxstats->last_signal;
2232                         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2233                 }
2234
2235                 if (!sta->pcpu_rx_stats &&
2236                     !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
2237                         sinfo->signal_avg =
2238                                 -ewma_signal_read(&sta->rx_stats_avg.signal);
2239                         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
2240                 }
2241         }
2242
2243         /* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2244          * the sta->rx_stats struct, so the check here is fine with and without
2245          * pcpu statistics
2246          */
2247         if (last_rxstats->chains &&
2248             !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2249                                BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2250                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2251                 if (!sta->pcpu_rx_stats)
2252                         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2253
2254                 sinfo->chains = last_rxstats->chains;
2255
2256                 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2257                         sinfo->chain_signal[i] =
2258                                 last_rxstats->chain_signal_last[i];
2259                         sinfo->chain_signal_avg[i] =
2260                                 -ewma_signal_read(&sta->rx_stats_avg.chain_signal[i]);
2261                 }
2262         }
2263
2264         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))) {
2265                 sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
2266                                      &sinfo->txrate);
2267                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
2268         }
2269
2270         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE))) {
2271                 if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2272                         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
2273         }
2274
2275         if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
2276                 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
2277                         struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
2278
2279                         sta_set_tidstats(sta, tidstats, i);
2280                 }
2281         }
2282
2283         if (ieee80211_vif_is_mesh(&sdata->vif)) {
2284 #ifdef CONFIG_MAC80211_MESH
2285                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2286                                  BIT_ULL(NL80211_STA_INFO_PLID) |
2287                                  BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2288                                  BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2289                                  BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2290                                  BIT_ULL(NL80211_STA_INFO_NONPEER_PM);
2291
2292                 sinfo->llid = sta->mesh->llid;
2293                 sinfo->plid = sta->mesh->plid;
2294                 sinfo->plink_state = sta->mesh->plink_state;
2295                 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2296                         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2297                         sinfo->t_offset = sta->mesh->t_offset;
2298                 }
2299                 sinfo->local_pm = sta->mesh->local_pm;
2300                 sinfo->peer_pm = sta->mesh->peer_pm;
2301                 sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2302 #endif
2303         }
2304
2305         sinfo->bss_param.flags = 0;
2306         if (sdata->vif.bss_conf.use_cts_prot)
2307                 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2308         if (sdata->vif.bss_conf.use_short_preamble)
2309                 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2310         if (sdata->vif.bss_conf.use_short_slot)
2311                 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2312         sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2313         sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2314
2315         sinfo->sta_flags.set = 0;
2316         sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2317                                 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2318                                 BIT(NL80211_STA_FLAG_WME) |
2319                                 BIT(NL80211_STA_FLAG_MFP) |
2320                                 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2321                                 BIT(NL80211_STA_FLAG_ASSOCIATED) |
2322                                 BIT(NL80211_STA_FLAG_TDLS_PEER);
2323         if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2324                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2325         if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2326                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2327         if (sta->sta.wme)
2328                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2329         if (test_sta_flag(sta, WLAN_STA_MFP))
2330                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2331         if (test_sta_flag(sta, WLAN_STA_AUTH))
2332                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2333         if (test_sta_flag(sta, WLAN_STA_ASSOC))
2334                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2335         if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2336                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2337
2338         thr = sta_get_expected_throughput(sta);
2339
2340         if (thr != 0) {
2341                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2342                 sinfo->expected_throughput = thr;
2343         }
2344
2345         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2346             sta->status_stats.ack_signal_filled) {
2347                 sinfo->ack_signal = sta->status_stats.last_ack_signal;
2348                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2349         }
2350
2351         if (ieee80211_hw_check(&sta->local->hw, REPORTS_TX_ACK_STATUS) &&
2352             !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_DATA_ACK_SIGNAL_AVG))) {
2353                 sinfo->avg_ack_signal =
2354                         -(s8)ewma_avg_signal_read(
2355                                 &sta->status_stats.avg_ack_signal);
2356                 sinfo->filled |=
2357                         BIT_ULL(NL80211_STA_INFO_DATA_ACK_SIGNAL_AVG);
2358         }
2359 }
2360
2361 u32 sta_get_expected_throughput(struct sta_info *sta)
2362 {
2363         struct ieee80211_sub_if_data *sdata = sta->sdata;
2364         struct ieee80211_local *local = sdata->local;
2365         struct rate_control_ref *ref = NULL;
2366         u32 thr = 0;
2367
2368         if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
2369                 ref = local->rate_ctrl;
2370
2371         /* check if the driver has a SW RC implementation */
2372         if (ref && ref->ops->get_expected_throughput)
2373                 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2374         else
2375                 thr = drv_get_expected_throughput(local, sta);
2376
2377         return thr;
2378 }
2379
2380 unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2381 {
2382         struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2383
2384         if (!sta->status_stats.last_ack ||
2385             time_after(stats->last_rx, sta->status_stats.last_ack))
2386                 return stats->last_rx;
2387         return sta->status_stats.last_ack;
2388 }
2389
2390 static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2391 {
2392         if (!sta->sdata->local->ops->wake_tx_queue)
2393                 return;
2394
2395         if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2396                 sta->cparams.target = MS2TIME(50);
2397                 sta->cparams.interval = MS2TIME(300);
2398                 sta->cparams.ecn = false;
2399         } else {
2400                 sta->cparams.target = MS2TIME(20);
2401                 sta->cparams.interval = MS2TIME(100);
2402                 sta->cparams.ecn = true;
2403         }
2404 }
2405
2406 void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2407                                            u32 thr)
2408 {
2409         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2410
2411         sta_update_codel_params(sta, thr);
2412 }