Merge tag 'wireless-2024-01-22' of git://git.kernel.org/pub/scm/linux/kernel/git...
[sfrench/cifs-2.6.git] / net / mac80211 / sta_info.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2002-2005, Instant802 Networks, Inc.
4  * Copyright 2006-2007  Jiri Benc <jbenc@suse.cz>
5  * Copyright 2013-2014  Intel Mobile Communications GmbH
6  * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
7  * Copyright (C) 2018-2023 Intel Corporation
8  */
9
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/etherdevice.h>
13 #include <linux/netdevice.h>
14 #include <linux/types.h>
15 #include <linux/slab.h>
16 #include <linux/skbuff.h>
17 #include <linux/if_arp.h>
18 #include <linux/timer.h>
19 #include <linux/rtnetlink.h>
20
21 #include <net/codel.h>
22 #include <net/mac80211.h>
23 #include "ieee80211_i.h"
24 #include "driver-ops.h"
25 #include "rate.h"
26 #include "sta_info.h"
27 #include "debugfs_sta.h"
28 #include "mesh.h"
29 #include "wme.h"
30
31 /**
32  * DOC: STA information lifetime rules
33  *
34  * STA info structures (&struct sta_info) are managed in a hash table
35  * for faster lookup and a list for iteration. They are managed using
36  * RCU, i.e. access to the list and hash table is protected by RCU.
37  *
38  * Upon allocating a STA info structure with sta_info_alloc(), the caller
39  * owns that structure. It must then insert it into the hash table using
40  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
41  * case (which acquires an rcu read section but must not be called from
42  * within one) will the pointer still be valid after the call. Note that
43  * the caller may not do much with the STA info before inserting it; in
44  * particular, it may not start any mesh peer link management or add
45  * encryption keys.
46  *
47  * When the insertion fails (sta_info_insert()) returns non-zero), the
48  * structure will have been freed by sta_info_insert()!
49  *
50  * Station entries are added by mac80211 when you establish a link with a
51  * peer. This means different things for the different type of interfaces
52  * we support. For a regular station this mean we add the AP sta when we
53  * receive an association response from the AP. For IBSS this occurs when
54  * get to know about a peer on the same IBSS. For WDS we add the sta for
55  * the peer immediately upon device open. When using AP mode we add stations
56  * for each respective station upon request from userspace through nl80211.
57  *
58  * In order to remove a STA info structure, various sta_info_destroy_*()
59  * calls are available.
60  *
61  * There is no concept of ownership on a STA entry; each structure is
62  * owned by the global hash table/list until it is removed. All users of
63  * the structure need to be RCU protected so that the structure won't be
64  * freed before they are done using it.
65  */
66
67 struct sta_link_alloc {
68         struct link_sta_info info;
69         struct ieee80211_link_sta sta;
70         struct rcu_head rcu_head;
71 };
72
73 static const struct rhashtable_params sta_rht_params = {
74         .nelem_hint = 3, /* start small */
75         .automatic_shrinking = true,
76         .head_offset = offsetof(struct sta_info, hash_node),
77         .key_offset = offsetof(struct sta_info, addr),
78         .key_len = ETH_ALEN,
79         .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
80 };
81
82 static const struct rhashtable_params link_sta_rht_params = {
83         .nelem_hint = 3, /* start small */
84         .automatic_shrinking = true,
85         .head_offset = offsetof(struct link_sta_info, link_hash_node),
86         .key_offset = offsetof(struct link_sta_info, addr),
87         .key_len = ETH_ALEN,
88         .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
89 };
90
91 static int sta_info_hash_del(struct ieee80211_local *local,
92                              struct sta_info *sta)
93 {
94         return rhltable_remove(&local->sta_hash, &sta->hash_node,
95                                sta_rht_params);
96 }
97
98 static int link_sta_info_hash_add(struct ieee80211_local *local,
99                                   struct link_sta_info *link_sta)
100 {
101         lockdep_assert_wiphy(local->hw.wiphy);
102
103         return rhltable_insert(&local->link_sta_hash,
104                                &link_sta->link_hash_node, link_sta_rht_params);
105 }
106
107 static int link_sta_info_hash_del(struct ieee80211_local *local,
108                                   struct link_sta_info *link_sta)
109 {
110         lockdep_assert_wiphy(local->hw.wiphy);
111
112         return rhltable_remove(&local->link_sta_hash,
113                                &link_sta->link_hash_node, link_sta_rht_params);
114 }
115
116 void ieee80211_purge_sta_txqs(struct sta_info *sta)
117 {
118         struct ieee80211_local *local = sta->sdata->local;
119         int i;
120
121         for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
122                 struct txq_info *txqi;
123
124                 if (!sta->sta.txq[i])
125                         continue;
126
127                 txqi = to_txq_info(sta->sta.txq[i]);
128
129                 ieee80211_txq_purge(local, txqi);
130         }
131 }
132
133 static void __cleanup_single_sta(struct sta_info *sta)
134 {
135         int ac, i;
136         struct tid_ampdu_tx *tid_tx;
137         struct ieee80211_sub_if_data *sdata = sta->sdata;
138         struct ieee80211_local *local = sdata->local;
139         struct ps_data *ps;
140
141         if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
142             test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
143             test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
144                 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
145                     sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
146                         ps = &sdata->bss->ps;
147                 else if (ieee80211_vif_is_mesh(&sdata->vif))
148                         ps = &sdata->u.mesh.ps;
149                 else
150                         return;
151
152                 clear_sta_flag(sta, WLAN_STA_PS_STA);
153                 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
154                 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
155
156                 atomic_dec(&ps->num_sta_ps);
157         }
158
159         ieee80211_purge_sta_txqs(sta);
160
161         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
162                 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
163                 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
164                 ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
165         }
166
167         if (ieee80211_vif_is_mesh(&sdata->vif))
168                 mesh_sta_cleanup(sta);
169
170         cancel_work_sync(&sta->drv_deliver_wk);
171
172         /*
173          * Destroy aggregation state here. It would be nice to wait for the
174          * driver to finish aggregation stop and then clean up, but for now
175          * drivers have to handle aggregation stop being requested, followed
176          * directly by station destruction.
177          */
178         for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
179                 kfree(sta->ampdu_mlme.tid_start_tx[i]);
180                 tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
181                 if (!tid_tx)
182                         continue;
183                 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
184                 kfree(tid_tx);
185         }
186 }
187
188 static void cleanup_single_sta(struct sta_info *sta)
189 {
190         struct ieee80211_sub_if_data *sdata = sta->sdata;
191         struct ieee80211_local *local = sdata->local;
192
193         __cleanup_single_sta(sta);
194         sta_info_free(local, sta);
195 }
196
197 struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local,
198                                          const u8 *addr)
199 {
200         return rhltable_lookup(&local->sta_hash, addr, sta_rht_params);
201 }
202
203 /* protected by RCU */
204 struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
205                               const u8 *addr)
206 {
207         struct ieee80211_local *local = sdata->local;
208         struct rhlist_head *tmp;
209         struct sta_info *sta;
210
211         rcu_read_lock();
212         for_each_sta_info(local, addr, sta, tmp) {
213                 if (sta->sdata == sdata) {
214                         rcu_read_unlock();
215                         /* this is safe as the caller must already hold
216                          * another rcu read section or the mutex
217                          */
218                         return sta;
219                 }
220         }
221         rcu_read_unlock();
222         return NULL;
223 }
224
225 /*
226  * Get sta info either from the specified interface
227  * or from one of its vlans
228  */
229 struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
230                                   const u8 *addr)
231 {
232         struct ieee80211_local *local = sdata->local;
233         struct rhlist_head *tmp;
234         struct sta_info *sta;
235
236         rcu_read_lock();
237         for_each_sta_info(local, addr, sta, tmp) {
238                 if (sta->sdata == sdata ||
239                     (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
240                         rcu_read_unlock();
241                         /* this is safe as the caller must already hold
242                          * another rcu read section or the mutex
243                          */
244                         return sta;
245                 }
246         }
247         rcu_read_unlock();
248         return NULL;
249 }
250
251 struct rhlist_head *link_sta_info_hash_lookup(struct ieee80211_local *local,
252                                               const u8 *addr)
253 {
254         return rhltable_lookup(&local->link_sta_hash, addr,
255                                link_sta_rht_params);
256 }
257
258 struct link_sta_info *
259 link_sta_info_get_bss(struct ieee80211_sub_if_data *sdata, const u8 *addr)
260 {
261         struct ieee80211_local *local = sdata->local;
262         struct rhlist_head *tmp;
263         struct link_sta_info *link_sta;
264
265         rcu_read_lock();
266         for_each_link_sta_info(local, addr, link_sta, tmp) {
267                 struct sta_info *sta = link_sta->sta;
268
269                 if (sta->sdata == sdata ||
270                     (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
271                         rcu_read_unlock();
272                         /* this is safe as the caller must already hold
273                          * another rcu read section or the mutex
274                          */
275                         return link_sta;
276                 }
277         }
278         rcu_read_unlock();
279         return NULL;
280 }
281
282 struct ieee80211_sta *
283 ieee80211_find_sta_by_link_addrs(struct ieee80211_hw *hw,
284                                  const u8 *addr,
285                                  const u8 *localaddr,
286                                  unsigned int *link_id)
287 {
288         struct ieee80211_local *local = hw_to_local(hw);
289         struct link_sta_info *link_sta;
290         struct rhlist_head *tmp;
291
292         for_each_link_sta_info(local, addr, link_sta, tmp) {
293                 struct sta_info *sta = link_sta->sta;
294                 struct ieee80211_link_data *link;
295                 u8 _link_id = link_sta->link_id;
296
297                 if (!localaddr) {
298                         if (link_id)
299                                 *link_id = _link_id;
300                         return &sta->sta;
301                 }
302
303                 link = rcu_dereference(sta->sdata->link[_link_id]);
304                 if (!link)
305                         continue;
306
307                 if (memcmp(link->conf->addr, localaddr, ETH_ALEN))
308                         continue;
309
310                 if (link_id)
311                         *link_id = _link_id;
312                 return &sta->sta;
313         }
314
315         return NULL;
316 }
317 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_link_addrs);
318
319 struct sta_info *sta_info_get_by_addrs(struct ieee80211_local *local,
320                                        const u8 *sta_addr, const u8 *vif_addr)
321 {
322         struct rhlist_head *tmp;
323         struct sta_info *sta;
324
325         for_each_sta_info(local, sta_addr, sta, tmp) {
326                 if (ether_addr_equal(vif_addr, sta->sdata->vif.addr))
327                         return sta;
328         }
329
330         return NULL;
331 }
332
333 struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
334                                      int idx)
335 {
336         struct ieee80211_local *local = sdata->local;
337         struct sta_info *sta;
338         int i = 0;
339
340         list_for_each_entry_rcu(sta, &local->sta_list, list,
341                                 lockdep_is_held(&local->hw.wiphy->mtx)) {
342                 if (sdata != sta->sdata)
343                         continue;
344                 if (i < idx) {
345                         ++i;
346                         continue;
347                 }
348                 return sta;
349         }
350
351         return NULL;
352 }
353
354 static void sta_info_free_link(struct link_sta_info *link_sta)
355 {
356         free_percpu(link_sta->pcpu_rx_stats);
357 }
358
359 static void sta_remove_link(struct sta_info *sta, unsigned int link_id,
360                             bool unhash)
361 {
362         struct sta_link_alloc *alloc = NULL;
363         struct link_sta_info *link_sta;
364
365         lockdep_assert_wiphy(sta->local->hw.wiphy);
366
367         link_sta = rcu_access_pointer(sta->link[link_id]);
368         if (WARN_ON(!link_sta))
369                 return;
370
371         if (unhash)
372                 link_sta_info_hash_del(sta->local, link_sta);
373
374         if (test_sta_flag(sta, WLAN_STA_INSERTED))
375                 ieee80211_link_sta_debugfs_remove(link_sta);
376
377         if (link_sta != &sta->deflink)
378                 alloc = container_of(link_sta, typeof(*alloc), info);
379
380         sta->sta.valid_links &= ~BIT(link_id);
381         RCU_INIT_POINTER(sta->link[link_id], NULL);
382         RCU_INIT_POINTER(sta->sta.link[link_id], NULL);
383         if (alloc) {
384                 sta_info_free_link(&alloc->info);
385                 kfree_rcu(alloc, rcu_head);
386         }
387
388         ieee80211_sta_recalc_aggregates(&sta->sta);
389 }
390
391 /**
392  * sta_info_free - free STA
393  *
394  * @local: pointer to the global information
395  * @sta: STA info to free
396  *
397  * This function must undo everything done by sta_info_alloc()
398  * that may happen before sta_info_insert(). It may only be
399  * called when sta_info_insert() has not been attempted (and
400  * if that fails, the station is freed anyway.)
401  */
402 void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
403 {
404         int i;
405
406         for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
407                 struct link_sta_info *link_sta;
408
409                 link_sta = rcu_access_pointer(sta->link[i]);
410                 if (!link_sta)
411                         continue;
412
413                 sta_remove_link(sta, i, false);
414         }
415
416         /*
417          * If we had used sta_info_pre_move_state() then we might not
418          * have gone through the state transitions down again, so do
419          * it here now (and warn if it's inserted).
420          *
421          * This will clear state such as fast TX/RX that may have been
422          * allocated during state transitions.
423          */
424         while (sta->sta_state > IEEE80211_STA_NONE) {
425                 int ret;
426
427                 WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED));
428
429                 ret = sta_info_move_state(sta, sta->sta_state - 1);
430                 if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret))
431                         break;
432         }
433
434         if (sta->rate_ctrl)
435                 rate_control_free_sta(sta);
436
437         sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
438
439         kfree(to_txq_info(sta->sta.txq[0]));
440         kfree(rcu_dereference_raw(sta->sta.rates));
441 #ifdef CONFIG_MAC80211_MESH
442         kfree(sta->mesh);
443 #endif
444
445         sta_info_free_link(&sta->deflink);
446         kfree(sta);
447 }
448
449 static int sta_info_hash_add(struct ieee80211_local *local,
450                              struct sta_info *sta)
451 {
452         return rhltable_insert(&local->sta_hash, &sta->hash_node,
453                                sta_rht_params);
454 }
455
456 static void sta_deliver_ps_frames(struct work_struct *wk)
457 {
458         struct sta_info *sta;
459
460         sta = container_of(wk, struct sta_info, drv_deliver_wk);
461
462         if (sta->dead)
463                 return;
464
465         local_bh_disable();
466         if (!test_sta_flag(sta, WLAN_STA_PS_STA))
467                 ieee80211_sta_ps_deliver_wakeup(sta);
468         else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
469                 ieee80211_sta_ps_deliver_poll_response(sta);
470         else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
471                 ieee80211_sta_ps_deliver_uapsd(sta);
472         local_bh_enable();
473 }
474
475 static int sta_prepare_rate_control(struct ieee80211_local *local,
476                                     struct sta_info *sta, gfp_t gfp)
477 {
478         if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
479                 return 0;
480
481         sta->rate_ctrl = local->rate_ctrl;
482         sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
483                                                      sta, gfp);
484         if (!sta->rate_ctrl_priv)
485                 return -ENOMEM;
486
487         return 0;
488 }
489
490 static int sta_info_alloc_link(struct ieee80211_local *local,
491                                struct link_sta_info *link_info,
492                                gfp_t gfp)
493 {
494         struct ieee80211_hw *hw = &local->hw;
495         int i;
496
497         if (ieee80211_hw_check(hw, USES_RSS)) {
498                 link_info->pcpu_rx_stats =
499                         alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
500                 if (!link_info->pcpu_rx_stats)
501                         return -ENOMEM;
502         }
503
504         link_info->rx_stats.last_rx = jiffies;
505         u64_stats_init(&link_info->rx_stats.syncp);
506
507         ewma_signal_init(&link_info->rx_stats_avg.signal);
508         ewma_avg_signal_init(&link_info->status_stats.avg_ack_signal);
509         for (i = 0; i < ARRAY_SIZE(link_info->rx_stats_avg.chain_signal); i++)
510                 ewma_signal_init(&link_info->rx_stats_avg.chain_signal[i]);
511
512         return 0;
513 }
514
515 static void sta_info_add_link(struct sta_info *sta,
516                               unsigned int link_id,
517                               struct link_sta_info *link_info,
518                               struct ieee80211_link_sta *link_sta)
519 {
520         link_info->sta = sta;
521         link_info->link_id = link_id;
522         link_info->pub = link_sta;
523         link_info->pub->sta = &sta->sta;
524         link_sta->link_id = link_id;
525         rcu_assign_pointer(sta->link[link_id], link_info);
526         rcu_assign_pointer(sta->sta.link[link_id], link_sta);
527
528         link_sta->smps_mode = IEEE80211_SMPS_OFF;
529         link_sta->agg.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
530 }
531
532 static struct sta_info *
533 __sta_info_alloc(struct ieee80211_sub_if_data *sdata,
534                  const u8 *addr, int link_id, const u8 *link_addr,
535                  gfp_t gfp)
536 {
537         struct ieee80211_local *local = sdata->local;
538         struct ieee80211_hw *hw = &local->hw;
539         struct sta_info *sta;
540         void *txq_data;
541         int size;
542         int i;
543
544         sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
545         if (!sta)
546                 return NULL;
547
548         sta->local = local;
549         sta->sdata = sdata;
550
551         if (sta_info_alloc_link(local, &sta->deflink, gfp))
552                 goto free;
553
554         if (link_id >= 0) {
555                 sta_info_add_link(sta, link_id, &sta->deflink,
556                                   &sta->sta.deflink);
557                 sta->sta.valid_links = BIT(link_id);
558         } else {
559                 sta_info_add_link(sta, 0, &sta->deflink, &sta->sta.deflink);
560         }
561
562         sta->sta.cur = &sta->sta.deflink.agg;
563
564         spin_lock_init(&sta->lock);
565         spin_lock_init(&sta->ps_lock);
566         INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
567         wiphy_work_init(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
568 #ifdef CONFIG_MAC80211_MESH
569         if (ieee80211_vif_is_mesh(&sdata->vif)) {
570                 sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
571                 if (!sta->mesh)
572                         goto free;
573                 sta->mesh->plink_sta = sta;
574                 spin_lock_init(&sta->mesh->plink_lock);
575                 if (!sdata->u.mesh.user_mpm)
576                         timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
577                                     0);
578                 sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
579         }
580 #endif
581
582         memcpy(sta->addr, addr, ETH_ALEN);
583         memcpy(sta->sta.addr, addr, ETH_ALEN);
584         memcpy(sta->deflink.addr, link_addr, ETH_ALEN);
585         memcpy(sta->sta.deflink.addr, link_addr, ETH_ALEN);
586         sta->sta.max_rx_aggregation_subframes =
587                 local->hw.max_rx_aggregation_subframes;
588
589         /* TODO link specific alloc and assignments for MLO Link STA */
590
591         /* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only.
592          * The Tx path starts to use a key as soon as the key slot ptk_idx
593          * references to is not NULL. To not use the initial Rx-only key
594          * prematurely for Tx initialize ptk_idx to an impossible PTK keyid
595          * which always will refer to a NULL key.
596          */
597         BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX);
598         sta->ptk_idx = INVALID_PTK_KEYIDX;
599
600
601         ieee80211_init_frag_cache(&sta->frags);
602
603         sta->sta_state = IEEE80211_STA_NONE;
604
605         if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
606                 sta->amsdu_mesh_control = -1;
607
608         /* Mark TID as unreserved */
609         sta->reserved_tid = IEEE80211_TID_UNRESERVED;
610
611         sta->last_connected = ktime_get_seconds();
612
613         size = sizeof(struct txq_info) +
614                ALIGN(hw->txq_data_size, sizeof(void *));
615
616         txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
617         if (!txq_data)
618                 goto free;
619
620         for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
621                 struct txq_info *txq = txq_data + i * size;
622
623                 /* might not do anything for the (bufferable) MMPDU TXQ */
624                 ieee80211_txq_init(sdata, sta, txq, i);
625         }
626
627         if (sta_prepare_rate_control(local, sta, gfp))
628                 goto free_txq;
629
630         sta->airtime_weight = IEEE80211_DEFAULT_AIRTIME_WEIGHT;
631
632         for (i = 0; i < IEEE80211_NUM_ACS; i++) {
633                 skb_queue_head_init(&sta->ps_tx_buf[i]);
634                 skb_queue_head_init(&sta->tx_filtered[i]);
635                 sta->airtime[i].deficit = sta->airtime_weight;
636                 atomic_set(&sta->airtime[i].aql_tx_pending, 0);
637                 sta->airtime[i].aql_limit_low = local->aql_txq_limit_low[i];
638                 sta->airtime[i].aql_limit_high = local->aql_txq_limit_high[i];
639         }
640
641         for (i = 0; i < IEEE80211_NUM_TIDS; i++)
642                 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
643
644         for (i = 0; i < NUM_NL80211_BANDS; i++) {
645                 u32 mandatory = 0;
646                 int r;
647
648                 if (!hw->wiphy->bands[i])
649                         continue;
650
651                 switch (i) {
652                 case NL80211_BAND_2GHZ:
653                 case NL80211_BAND_LC:
654                         /*
655                          * We use both here, even if we cannot really know for
656                          * sure the station will support both, but the only use
657                          * for this is when we don't know anything yet and send
658                          * management frames, and then we'll pick the lowest
659                          * possible rate anyway.
660                          * If we don't include _G here, we cannot find a rate
661                          * in P2P, and thus trigger the WARN_ONCE() in rate.c
662                          */
663                         mandatory = IEEE80211_RATE_MANDATORY_B |
664                                     IEEE80211_RATE_MANDATORY_G;
665                         break;
666                 case NL80211_BAND_5GHZ:
667                         mandatory = IEEE80211_RATE_MANDATORY_A;
668                         break;
669                 case NL80211_BAND_60GHZ:
670                         WARN_ON(1);
671                         mandatory = 0;
672                         break;
673                 }
674
675                 for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) {
676                         struct ieee80211_rate *rate;
677
678                         rate = &hw->wiphy->bands[i]->bitrates[r];
679
680                         if (!(rate->flags & mandatory))
681                                 continue;
682                         sta->sta.deflink.supp_rates[i] |= BIT(r);
683                 }
684         }
685
686         sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
687         sta->cparams.target = MS2TIME(20);
688         sta->cparams.interval = MS2TIME(100);
689         sta->cparams.ecn = true;
690         sta->cparams.ce_threshold_selector = 0;
691         sta->cparams.ce_threshold_mask = 0;
692
693         sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
694
695         return sta;
696
697 free_txq:
698         kfree(to_txq_info(sta->sta.txq[0]));
699 free:
700         sta_info_free_link(&sta->deflink);
701 #ifdef CONFIG_MAC80211_MESH
702         kfree(sta->mesh);
703 #endif
704         kfree(sta);
705         return NULL;
706 }
707
708 struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
709                                 const u8 *addr, gfp_t gfp)
710 {
711         return __sta_info_alloc(sdata, addr, -1, addr, gfp);
712 }
713
714 struct sta_info *sta_info_alloc_with_link(struct ieee80211_sub_if_data *sdata,
715                                           const u8 *mld_addr,
716                                           unsigned int link_id,
717                                           const u8 *link_addr,
718                                           gfp_t gfp)
719 {
720         return __sta_info_alloc(sdata, mld_addr, link_id, link_addr, gfp);
721 }
722
723 static int sta_info_insert_check(struct sta_info *sta)
724 {
725         struct ieee80211_sub_if_data *sdata = sta->sdata;
726
727         lockdep_assert_wiphy(sdata->local->hw.wiphy);
728
729         /*
730          * Can't be a WARN_ON because it can be triggered through a race:
731          * something inserts a STA (on one CPU) without holding the RTNL
732          * and another CPU turns off the net device.
733          */
734         if (unlikely(!ieee80211_sdata_running(sdata)))
735                 return -ENETDOWN;
736
737         if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
738                     !is_valid_ether_addr(sta->sta.addr)))
739                 return -EINVAL;
740
741         /* The RCU read lock is required by rhashtable due to
742          * asynchronous resize/rehash.  We also require the mutex
743          * for correctness.
744          */
745         rcu_read_lock();
746         if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
747             ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
748                 rcu_read_unlock();
749                 return -ENOTUNIQ;
750         }
751         rcu_read_unlock();
752
753         return 0;
754 }
755
756 static int sta_info_insert_drv_state(struct ieee80211_local *local,
757                                      struct ieee80211_sub_if_data *sdata,
758                                      struct sta_info *sta)
759 {
760         enum ieee80211_sta_state state;
761         int err = 0;
762
763         for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
764                 err = drv_sta_state(local, sdata, sta, state, state + 1);
765                 if (err)
766                         break;
767         }
768
769         if (!err) {
770                 /*
771                  * Drivers using legacy sta_add/sta_remove callbacks only
772                  * get uploaded set to true after sta_add is called.
773                  */
774                 if (!local->ops->sta_add)
775                         sta->uploaded = true;
776                 return 0;
777         }
778
779         if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
780                 sdata_info(sdata,
781                            "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
782                            sta->sta.addr, state + 1, err);
783                 err = 0;
784         }
785
786         /* unwind on error */
787         for (; state > IEEE80211_STA_NOTEXIST; state--)
788                 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
789
790         return err;
791 }
792
793 static void
794 ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
795 {
796         struct ieee80211_local *local = sdata->local;
797         bool allow_p2p_go_ps = sdata->vif.p2p;
798         struct sta_info *sta;
799
800         rcu_read_lock();
801         list_for_each_entry_rcu(sta, &local->sta_list, list) {
802                 if (sdata != sta->sdata ||
803                     !test_sta_flag(sta, WLAN_STA_ASSOC))
804                         continue;
805                 if (!sta->sta.support_p2p_ps) {
806                         allow_p2p_go_ps = false;
807                         break;
808                 }
809         }
810         rcu_read_unlock();
811
812         if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
813                 sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
814                 ieee80211_link_info_change_notify(sdata, &sdata->deflink,
815                                                   BSS_CHANGED_P2P_PS);
816         }
817 }
818
819 static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
820 {
821         struct ieee80211_local *local = sta->local;
822         struct ieee80211_sub_if_data *sdata = sta->sdata;
823         struct station_info *sinfo = NULL;
824         int err = 0;
825
826         lockdep_assert_wiphy(local->hw.wiphy);
827
828         /* check if STA exists already */
829         if (sta_info_get_bss(sdata, sta->sta.addr)) {
830                 err = -EEXIST;
831                 goto out_cleanup;
832         }
833
834         sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
835         if (!sinfo) {
836                 err = -ENOMEM;
837                 goto out_cleanup;
838         }
839
840         local->num_sta++;
841         local->sta_generation++;
842         smp_mb();
843
844         /* simplify things and don't accept BA sessions yet */
845         set_sta_flag(sta, WLAN_STA_BLOCK_BA);
846
847         /* make the station visible */
848         err = sta_info_hash_add(local, sta);
849         if (err)
850                 goto out_drop_sta;
851
852         if (sta->sta.valid_links) {
853                 err = link_sta_info_hash_add(local, &sta->deflink);
854                 if (err) {
855                         sta_info_hash_del(local, sta);
856                         goto out_drop_sta;
857                 }
858         }
859
860         list_add_tail_rcu(&sta->list, &local->sta_list);
861
862         /* update channel context before notifying the driver about state
863          * change, this enables driver using the updated channel context right away.
864          */
865         if (sta->sta_state >= IEEE80211_STA_ASSOC) {
866                 ieee80211_recalc_min_chandef(sta->sdata, -1);
867                 if (!sta->sta.support_p2p_ps)
868                         ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
869         }
870
871         /* notify driver */
872         err = sta_info_insert_drv_state(local, sdata, sta);
873         if (err)
874                 goto out_remove;
875
876         set_sta_flag(sta, WLAN_STA_INSERTED);
877
878         /* accept BA sessions now */
879         clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
880
881         ieee80211_sta_debugfs_add(sta);
882         rate_control_add_sta_debugfs(sta);
883         if (sta->sta.valid_links) {
884                 int i;
885
886                 for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
887                         struct link_sta_info *link_sta;
888
889                         link_sta = rcu_dereference_protected(sta->link[i],
890                                                              lockdep_is_held(&local->hw.wiphy->mtx));
891
892                         if (!link_sta)
893                                 continue;
894
895                         ieee80211_link_sta_debugfs_add(link_sta);
896                         if (sdata->vif.active_links & BIT(i))
897                                 ieee80211_link_sta_debugfs_drv_add(link_sta);
898                 }
899         } else {
900                 ieee80211_link_sta_debugfs_add(&sta->deflink);
901                 ieee80211_link_sta_debugfs_drv_add(&sta->deflink);
902         }
903
904         sinfo->generation = local->sta_generation;
905         cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
906         kfree(sinfo);
907
908         sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
909
910         /* move reference to rcu-protected */
911         rcu_read_lock();
912
913         if (ieee80211_vif_is_mesh(&sdata->vif))
914                 mesh_accept_plinks_update(sdata);
915
916         ieee80211_check_fast_xmit(sta);
917
918         return 0;
919  out_remove:
920         if (sta->sta.valid_links)
921                 link_sta_info_hash_del(local, &sta->deflink);
922         sta_info_hash_del(local, sta);
923         list_del_rcu(&sta->list);
924  out_drop_sta:
925         local->num_sta--;
926         synchronize_net();
927  out_cleanup:
928         cleanup_single_sta(sta);
929         kfree(sinfo);
930         rcu_read_lock();
931         return err;
932 }
933
934 int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
935 {
936         struct ieee80211_local *local = sta->local;
937         int err;
938
939         might_sleep();
940         lockdep_assert_wiphy(local->hw.wiphy);
941
942         err = sta_info_insert_check(sta);
943         if (err) {
944                 sta_info_free(local, sta);
945                 rcu_read_lock();
946                 return err;
947         }
948
949         return sta_info_insert_finish(sta);
950 }
951
952 int sta_info_insert(struct sta_info *sta)
953 {
954         int err = sta_info_insert_rcu(sta);
955
956         rcu_read_unlock();
957
958         return err;
959 }
960
961 static inline void __bss_tim_set(u8 *tim, u16 id)
962 {
963         /*
964          * This format has been mandated by the IEEE specifications,
965          * so this line may not be changed to use the __set_bit() format.
966          */
967         tim[id / 8] |= (1 << (id % 8));
968 }
969
970 static inline void __bss_tim_clear(u8 *tim, u16 id)
971 {
972         /*
973          * This format has been mandated by the IEEE specifications,
974          * so this line may not be changed to use the __clear_bit() format.
975          */
976         tim[id / 8] &= ~(1 << (id % 8));
977 }
978
979 static inline bool __bss_tim_get(u8 *tim, u16 id)
980 {
981         /*
982          * This format has been mandated by the IEEE specifications,
983          * so this line may not be changed to use the test_bit() format.
984          */
985         return tim[id / 8] & (1 << (id % 8));
986 }
987
988 static unsigned long ieee80211_tids_for_ac(int ac)
989 {
990         /* If we ever support TIDs > 7, this obviously needs to be adjusted */
991         switch (ac) {
992         case IEEE80211_AC_VO:
993                 return BIT(6) | BIT(7);
994         case IEEE80211_AC_VI:
995                 return BIT(4) | BIT(5);
996         case IEEE80211_AC_BE:
997                 return BIT(0) | BIT(3);
998         case IEEE80211_AC_BK:
999                 return BIT(1) | BIT(2);
1000         default:
1001                 WARN_ON(1);
1002                 return 0;
1003         }
1004 }
1005
1006 static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
1007 {
1008         struct ieee80211_local *local = sta->local;
1009         struct ps_data *ps;
1010         bool indicate_tim = false;
1011         u8 ignore_for_tim = sta->sta.uapsd_queues;
1012         int ac;
1013         u16 id = sta->sta.aid;
1014
1015         if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1016             sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1017                 if (WARN_ON_ONCE(!sta->sdata->bss))
1018                         return;
1019
1020                 ps = &sta->sdata->bss->ps;
1021 #ifdef CONFIG_MAC80211_MESH
1022         } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
1023                 ps = &sta->sdata->u.mesh.ps;
1024 #endif
1025         } else {
1026                 return;
1027         }
1028
1029         /* No need to do anything if the driver does all */
1030         if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
1031                 return;
1032
1033         if (sta->dead)
1034                 goto done;
1035
1036         /*
1037          * If all ACs are delivery-enabled then we should build
1038          * the TIM bit for all ACs anyway; if only some are then
1039          * we ignore those and build the TIM bit using only the
1040          * non-enabled ones.
1041          */
1042         if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
1043                 ignore_for_tim = 0;
1044
1045         if (ignore_pending)
1046                 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
1047
1048         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1049                 unsigned long tids;
1050
1051                 if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
1052                         continue;
1053
1054                 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
1055                                 !skb_queue_empty(&sta->ps_tx_buf[ac]);
1056                 if (indicate_tim)
1057                         break;
1058
1059                 tids = ieee80211_tids_for_ac(ac);
1060
1061                 indicate_tim |=
1062                         sta->driver_buffered_tids & tids;
1063                 indicate_tim |=
1064                         sta->txq_buffered_tids & tids;
1065         }
1066
1067  done:
1068         spin_lock_bh(&local->tim_lock);
1069
1070         if (indicate_tim == __bss_tim_get(ps->tim, id))
1071                 goto out_unlock;
1072
1073         if (indicate_tim)
1074                 __bss_tim_set(ps->tim, id);
1075         else
1076                 __bss_tim_clear(ps->tim, id);
1077
1078         if (local->ops->set_tim && !WARN_ON(sta->dead)) {
1079                 local->tim_in_locked_section = true;
1080                 drv_set_tim(local, &sta->sta, indicate_tim);
1081                 local->tim_in_locked_section = false;
1082         }
1083
1084 out_unlock:
1085         spin_unlock_bh(&local->tim_lock);
1086 }
1087
1088 void sta_info_recalc_tim(struct sta_info *sta)
1089 {
1090         __sta_info_recalc_tim(sta, false);
1091 }
1092
1093 static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
1094 {
1095         struct ieee80211_tx_info *info;
1096         int timeout;
1097
1098         if (!skb)
1099                 return false;
1100
1101         info = IEEE80211_SKB_CB(skb);
1102
1103         /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
1104         timeout = (sta->listen_interval *
1105                    sta->sdata->vif.bss_conf.beacon_int *
1106                    32 / 15625) * HZ;
1107         if (timeout < STA_TX_BUFFER_EXPIRE)
1108                 timeout = STA_TX_BUFFER_EXPIRE;
1109         return time_after(jiffies, info->control.jiffies + timeout);
1110 }
1111
1112
1113 static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
1114                                                 struct sta_info *sta, int ac)
1115 {
1116         unsigned long flags;
1117         struct sk_buff *skb;
1118
1119         /*
1120          * First check for frames that should expire on the filtered
1121          * queue. Frames here were rejected by the driver and are on
1122          * a separate queue to avoid reordering with normal PS-buffered
1123          * frames. They also aren't accounted for right now in the
1124          * total_ps_buffered counter.
1125          */
1126         for (;;) {
1127                 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1128                 skb = skb_peek(&sta->tx_filtered[ac]);
1129                 if (sta_info_buffer_expired(sta, skb))
1130                         skb = __skb_dequeue(&sta->tx_filtered[ac]);
1131                 else
1132                         skb = NULL;
1133                 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1134
1135                 /*
1136                  * Frames are queued in order, so if this one
1137                  * hasn't expired yet we can stop testing. If
1138                  * we actually reached the end of the queue we
1139                  * also need to stop, of course.
1140                  */
1141                 if (!skb)
1142                         break;
1143                 ieee80211_free_txskb(&local->hw, skb);
1144         }
1145
1146         /*
1147          * Now also check the normal PS-buffered queue, this will
1148          * only find something if the filtered queue was emptied
1149          * since the filtered frames are all before the normal PS
1150          * buffered frames.
1151          */
1152         for (;;) {
1153                 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1154                 skb = skb_peek(&sta->ps_tx_buf[ac]);
1155                 if (sta_info_buffer_expired(sta, skb))
1156                         skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
1157                 else
1158                         skb = NULL;
1159                 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1160
1161                 /*
1162                  * frames are queued in order, so if this one
1163                  * hasn't expired yet (or we reached the end of
1164                  * the queue) we can stop testing
1165                  */
1166                 if (!skb)
1167                         break;
1168
1169                 local->total_ps_buffered--;
1170                 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
1171                        sta->sta.addr);
1172                 ieee80211_free_txskb(&local->hw, skb);
1173         }
1174
1175         /*
1176          * Finally, recalculate the TIM bit for this station -- it might
1177          * now be clear because the station was too slow to retrieve its
1178          * frames.
1179          */
1180         sta_info_recalc_tim(sta);
1181
1182         /*
1183          * Return whether there are any frames still buffered, this is
1184          * used to check whether the cleanup timer still needs to run,
1185          * if there are no frames we don't need to rearm the timer.
1186          */
1187         return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
1188                  skb_queue_empty(&sta->tx_filtered[ac]));
1189 }
1190
1191 static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
1192                                              struct sta_info *sta)
1193 {
1194         bool have_buffered = false;
1195         int ac;
1196
1197         /* This is only necessary for stations on BSS/MBSS interfaces */
1198         if (!sta->sdata->bss &&
1199             !ieee80211_vif_is_mesh(&sta->sdata->vif))
1200                 return false;
1201
1202         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1203                 have_buffered |=
1204                         sta_info_cleanup_expire_buffered_ac(local, sta, ac);
1205
1206         return have_buffered;
1207 }
1208
1209 static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
1210 {
1211         struct ieee80211_local *local;
1212         struct ieee80211_sub_if_data *sdata;
1213         int ret, i;
1214
1215         might_sleep();
1216
1217         if (!sta)
1218                 return -ENOENT;
1219
1220         local = sta->local;
1221         sdata = sta->sdata;
1222
1223         lockdep_assert_wiphy(local->hw.wiphy);
1224
1225         /*
1226          * Before removing the station from the driver and
1227          * rate control, it might still start new aggregation
1228          * sessions -- block that to make sure the tear-down
1229          * will be sufficient.
1230          */
1231         set_sta_flag(sta, WLAN_STA_BLOCK_BA);
1232         ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1233
1234         /*
1235          * Before removing the station from the driver there might be pending
1236          * rx frames on RSS queues sent prior to the disassociation - wait for
1237          * all such frames to be processed.
1238          */
1239         drv_sync_rx_queues(local, sta);
1240
1241         for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
1242                 struct link_sta_info *link_sta;
1243
1244                 if (!(sta->sta.valid_links & BIT(i)))
1245                         continue;
1246
1247                 link_sta = rcu_dereference_protected(sta->link[i],
1248                                                      lockdep_is_held(&local->hw.wiphy->mtx));
1249
1250                 link_sta_info_hash_del(local, link_sta);
1251         }
1252
1253         ret = sta_info_hash_del(local, sta);
1254         if (WARN_ON(ret))
1255                 return ret;
1256
1257         /*
1258          * for TDLS peers, make sure to return to the base channel before
1259          * removal.
1260          */
1261         if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1262                 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1263                 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1264         }
1265
1266         list_del_rcu(&sta->list);
1267         sta->removed = true;
1268
1269         if (sta->uploaded)
1270                 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
1271
1272         if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1273             rcu_access_pointer(sdata->u.vlan.sta) == sta)
1274                 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
1275
1276         return 0;
1277 }
1278
1279 static int _sta_info_move_state(struct sta_info *sta,
1280                                 enum ieee80211_sta_state new_state,
1281                                 bool recalc)
1282 {
1283         struct ieee80211_local *local = sta->local;
1284
1285         might_sleep();
1286
1287         if (sta->sta_state == new_state)
1288                 return 0;
1289
1290         /* check allowed transitions first */
1291
1292         switch (new_state) {
1293         case IEEE80211_STA_NONE:
1294                 if (sta->sta_state != IEEE80211_STA_AUTH)
1295                         return -EINVAL;
1296                 break;
1297         case IEEE80211_STA_AUTH:
1298                 if (sta->sta_state != IEEE80211_STA_NONE &&
1299                     sta->sta_state != IEEE80211_STA_ASSOC)
1300                         return -EINVAL;
1301                 break;
1302         case IEEE80211_STA_ASSOC:
1303                 if (sta->sta_state != IEEE80211_STA_AUTH &&
1304                     sta->sta_state != IEEE80211_STA_AUTHORIZED)
1305                         return -EINVAL;
1306                 break;
1307         case IEEE80211_STA_AUTHORIZED:
1308                 if (sta->sta_state != IEEE80211_STA_ASSOC)
1309                         return -EINVAL;
1310                 break;
1311         default:
1312                 WARN(1, "invalid state %d", new_state);
1313                 return -EINVAL;
1314         }
1315
1316         sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1317                 sta->sta.addr, new_state);
1318
1319         /* notify the driver before the actual changes so it can
1320          * fail the transition
1321          */
1322         if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1323                 int err = drv_sta_state(sta->local, sta->sdata, sta,
1324                                         sta->sta_state, new_state);
1325                 if (err)
1326                         return err;
1327         }
1328
1329         /* reflect the change in all state variables */
1330
1331         switch (new_state) {
1332         case IEEE80211_STA_NONE:
1333                 if (sta->sta_state == IEEE80211_STA_AUTH)
1334                         clear_bit(WLAN_STA_AUTH, &sta->_flags);
1335                 break;
1336         case IEEE80211_STA_AUTH:
1337                 if (sta->sta_state == IEEE80211_STA_NONE) {
1338                         set_bit(WLAN_STA_AUTH, &sta->_flags);
1339                 } else if (sta->sta_state == IEEE80211_STA_ASSOC) {
1340                         clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1341                         if (recalc) {
1342                                 ieee80211_recalc_min_chandef(sta->sdata, -1);
1343                                 if (!sta->sta.support_p2p_ps)
1344                                         ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1345                         }
1346                 }
1347                 break;
1348         case IEEE80211_STA_ASSOC:
1349                 if (sta->sta_state == IEEE80211_STA_AUTH) {
1350                         set_bit(WLAN_STA_ASSOC, &sta->_flags);
1351                         sta->assoc_at = ktime_get_boottime_ns();
1352                         if (recalc) {
1353                                 ieee80211_recalc_min_chandef(sta->sdata, -1);
1354                                 if (!sta->sta.support_p2p_ps)
1355                                         ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1356                         }
1357                 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1358                         ieee80211_vif_dec_num_mcast(sta->sdata);
1359                         clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1360
1361                         /*
1362                          * If we have encryption offload, flush (station) queues
1363                          * (after ensuring concurrent TX completed) so we won't
1364                          * transmit anything later unencrypted if/when keys are
1365                          * also removed, which might otherwise happen depending
1366                          * on how the hardware offload works.
1367                          */
1368                         if (local->ops->set_key) {
1369                                 synchronize_net();
1370                                 if (local->ops->flush_sta)
1371                                         drv_flush_sta(local, sta->sdata, sta);
1372                                 else
1373                                         ieee80211_flush_queues(local,
1374                                                                sta->sdata,
1375                                                                false);
1376                         }
1377
1378                         ieee80211_clear_fast_xmit(sta);
1379                         ieee80211_clear_fast_rx(sta);
1380                 }
1381                 break;
1382         case IEEE80211_STA_AUTHORIZED:
1383                 if (sta->sta_state == IEEE80211_STA_ASSOC) {
1384                         ieee80211_vif_inc_num_mcast(sta->sdata);
1385                         set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1386                         ieee80211_check_fast_xmit(sta);
1387                         ieee80211_check_fast_rx(sta);
1388                 }
1389                 if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
1390                     sta->sdata->vif.type == NL80211_IFTYPE_AP)
1391                         cfg80211_send_layer2_update(sta->sdata->dev,
1392                                                     sta->sta.addr);
1393                 break;
1394         default:
1395                 break;
1396         }
1397
1398         sta->sta_state = new_state;
1399
1400         return 0;
1401 }
1402
1403 int sta_info_move_state(struct sta_info *sta,
1404                         enum ieee80211_sta_state new_state)
1405 {
1406         return _sta_info_move_state(sta, new_state, true);
1407 }
1408
1409 static void __sta_info_destroy_part2(struct sta_info *sta, bool recalc)
1410 {
1411         struct ieee80211_local *local = sta->local;
1412         struct ieee80211_sub_if_data *sdata = sta->sdata;
1413         struct station_info *sinfo;
1414         int ret;
1415
1416         /*
1417          * NOTE: This assumes at least synchronize_net() was done
1418          *       after _part1 and before _part2!
1419          */
1420
1421         /*
1422          * There's a potential race in _part1 where we set WLAN_STA_BLOCK_BA
1423          * but someone might have just gotten past a check, and not yet into
1424          * queuing the work/creating the data/etc.
1425          *
1426          * Do another round of destruction so that the worker is certainly
1427          * canceled before we later free the station.
1428          *
1429          * Since this is after synchronize_rcu()/synchronize_net() we're now
1430          * certain that nobody can actually hold a reference to the STA and
1431          * be calling e.g. ieee80211_start_tx_ba_session().
1432          */
1433         ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1434
1435         might_sleep();
1436         lockdep_assert_wiphy(local->hw.wiphy);
1437
1438         if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1439                 ret = _sta_info_move_state(sta, IEEE80211_STA_ASSOC, recalc);
1440                 WARN_ON_ONCE(ret);
1441         }
1442
1443         /* now keys can no longer be reached */
1444         ieee80211_free_sta_keys(local, sta);
1445
1446         /* disable TIM bit - last chance to tell driver */
1447         __sta_info_recalc_tim(sta, true);
1448
1449         sta->dead = true;
1450
1451         local->num_sta--;
1452         local->sta_generation++;
1453
1454         while (sta->sta_state > IEEE80211_STA_NONE) {
1455                 ret = _sta_info_move_state(sta, sta->sta_state - 1, recalc);
1456                 if (ret) {
1457                         WARN_ON_ONCE(1);
1458                         break;
1459                 }
1460         }
1461
1462         if (sta->uploaded) {
1463                 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1464                                     IEEE80211_STA_NOTEXIST);
1465                 WARN_ON_ONCE(ret != 0);
1466         }
1467
1468         sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1469
1470         sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
1471         if (sinfo)
1472                 sta_set_sinfo(sta, sinfo, true);
1473         cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
1474         kfree(sinfo);
1475
1476         ieee80211_sta_debugfs_remove(sta);
1477
1478         ieee80211_destroy_frag_cache(&sta->frags);
1479
1480         cleanup_single_sta(sta);
1481 }
1482
1483 int __must_check __sta_info_destroy(struct sta_info *sta)
1484 {
1485         int err = __sta_info_destroy_part1(sta);
1486
1487         if (err)
1488                 return err;
1489
1490         synchronize_net();
1491
1492         __sta_info_destroy_part2(sta, true);
1493
1494         return 0;
1495 }
1496
1497 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
1498 {
1499         struct sta_info *sta;
1500
1501         lockdep_assert_wiphy(sdata->local->hw.wiphy);
1502
1503         sta = sta_info_get(sdata, addr);
1504         return __sta_info_destroy(sta);
1505 }
1506
1507 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
1508                               const u8 *addr)
1509 {
1510         struct sta_info *sta;
1511
1512         lockdep_assert_wiphy(sdata->local->hw.wiphy);
1513
1514         sta = sta_info_get_bss(sdata, addr);
1515         return __sta_info_destroy(sta);
1516 }
1517
1518 static void sta_info_cleanup(struct timer_list *t)
1519 {
1520         struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1521         struct sta_info *sta;
1522         bool timer_needed = false;
1523
1524         rcu_read_lock();
1525         list_for_each_entry_rcu(sta, &local->sta_list, list)
1526                 if (sta_info_cleanup_expire_buffered(local, sta))
1527                         timer_needed = true;
1528         rcu_read_unlock();
1529
1530         if (local->quiescing)
1531                 return;
1532
1533         if (!timer_needed)
1534                 return;
1535
1536         mod_timer(&local->sta_cleanup,
1537                   round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1538 }
1539
1540 int sta_info_init(struct ieee80211_local *local)
1541 {
1542         int err;
1543
1544         err = rhltable_init(&local->sta_hash, &sta_rht_params);
1545         if (err)
1546                 return err;
1547
1548         err = rhltable_init(&local->link_sta_hash, &link_sta_rht_params);
1549         if (err) {
1550                 rhltable_destroy(&local->sta_hash);
1551                 return err;
1552         }
1553
1554         spin_lock_init(&local->tim_lock);
1555         INIT_LIST_HEAD(&local->sta_list);
1556
1557         timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
1558         return 0;
1559 }
1560
1561 void sta_info_stop(struct ieee80211_local *local)
1562 {
1563         del_timer_sync(&local->sta_cleanup);
1564         rhltable_destroy(&local->sta_hash);
1565         rhltable_destroy(&local->link_sta_hash);
1566 }
1567
1568
1569 int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1570 {
1571         struct ieee80211_local *local = sdata->local;
1572         struct sta_info *sta, *tmp;
1573         LIST_HEAD(free_list);
1574         int ret = 0;
1575
1576         might_sleep();
1577         lockdep_assert_wiphy(local->hw.wiphy);
1578
1579         WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1580         WARN_ON(vlans && !sdata->bss);
1581
1582         list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1583                 if (sdata == sta->sdata ||
1584                     (vlans && sdata->bss == sta->sdata->bss)) {
1585                         if (!WARN_ON(__sta_info_destroy_part1(sta)))
1586                                 list_add(&sta->free_list, &free_list);
1587                         ret++;
1588                 }
1589         }
1590
1591         if (!list_empty(&free_list)) {
1592                 bool support_p2p_ps = true;
1593
1594                 synchronize_net();
1595                 list_for_each_entry_safe(sta, tmp, &free_list, free_list) {
1596                         if (!sta->sta.support_p2p_ps)
1597                                 support_p2p_ps = false;
1598                         __sta_info_destroy_part2(sta, false);
1599                 }
1600
1601                 ieee80211_recalc_min_chandef(sdata, -1);
1602                 if (!support_p2p_ps)
1603                         ieee80211_recalc_p2p_go_ps_allowed(sdata);
1604         }
1605
1606         return ret;
1607 }
1608
1609 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1610                           unsigned long exp_time)
1611 {
1612         struct ieee80211_local *local = sdata->local;
1613         struct sta_info *sta, *tmp;
1614
1615         lockdep_assert_wiphy(local->hw.wiphy);
1616
1617         list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1618                 unsigned long last_active = ieee80211_sta_last_active(sta);
1619
1620                 if (sdata != sta->sdata)
1621                         continue;
1622
1623                 if (time_is_before_jiffies(last_active + exp_time)) {
1624                         sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1625                                 sta->sta.addr);
1626
1627                         if (ieee80211_vif_is_mesh(&sdata->vif) &&
1628                             test_sta_flag(sta, WLAN_STA_PS_STA))
1629                                 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1630
1631                         WARN_ON(__sta_info_destroy(sta));
1632                 }
1633         }
1634 }
1635
1636 struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1637                                                    const u8 *addr,
1638                                                    const u8 *localaddr)
1639 {
1640         struct ieee80211_local *local = hw_to_local(hw);
1641         struct rhlist_head *tmp;
1642         struct sta_info *sta;
1643
1644         /*
1645          * Just return a random station if localaddr is NULL
1646          * ... first in list.
1647          */
1648         for_each_sta_info(local, addr, sta, tmp) {
1649                 if (localaddr &&
1650                     !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1651                         continue;
1652                 if (!sta->uploaded)
1653                         return NULL;
1654                 return &sta->sta;
1655         }
1656
1657         return NULL;
1658 }
1659 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
1660
1661 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1662                                          const u8 *addr)
1663 {
1664         struct sta_info *sta;
1665
1666         if (!vif)
1667                 return NULL;
1668
1669         sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1670         if (!sta)
1671                 return NULL;
1672
1673         if (!sta->uploaded)
1674                 return NULL;
1675
1676         return &sta->sta;
1677 }
1678 EXPORT_SYMBOL(ieee80211_find_sta);
1679
1680 /* powersave support code */
1681 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1682 {
1683         struct ieee80211_sub_if_data *sdata = sta->sdata;
1684         struct ieee80211_local *local = sdata->local;
1685         struct sk_buff_head pending;
1686         int filtered = 0, buffered = 0, ac, i;
1687         unsigned long flags;
1688         struct ps_data *ps;
1689
1690         if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1691                 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1692                                      u.ap);
1693
1694         if (sdata->vif.type == NL80211_IFTYPE_AP)
1695                 ps = &sdata->bss->ps;
1696         else if (ieee80211_vif_is_mesh(&sdata->vif))
1697                 ps = &sdata->u.mesh.ps;
1698         else
1699                 return;
1700
1701         clear_sta_flag(sta, WLAN_STA_SP);
1702
1703         BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1704         sta->driver_buffered_tids = 0;
1705         sta->txq_buffered_tids = 0;
1706
1707         if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1708                 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1709
1710         for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1711                 if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1712                         continue;
1713
1714                 schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1715         }
1716
1717         skb_queue_head_init(&pending);
1718
1719         /* sync with ieee80211_tx_h_unicast_ps_buf */
1720         spin_lock(&sta->ps_lock);
1721         /* Send all buffered frames to the station */
1722         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1723                 int count = skb_queue_len(&pending), tmp;
1724
1725                 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1726                 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1727                 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1728                 tmp = skb_queue_len(&pending);
1729                 filtered += tmp - count;
1730                 count = tmp;
1731
1732                 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1733                 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1734                 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1735                 tmp = skb_queue_len(&pending);
1736                 buffered += tmp - count;
1737         }
1738
1739         ieee80211_add_pending_skbs(local, &pending);
1740
1741         /* now we're no longer in the deliver code */
1742         clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1743
1744         /* The station might have polled and then woken up before we responded,
1745          * so clear these flags now to avoid them sticking around.
1746          */
1747         clear_sta_flag(sta, WLAN_STA_PSPOLL);
1748         clear_sta_flag(sta, WLAN_STA_UAPSD);
1749         spin_unlock(&sta->ps_lock);
1750
1751         atomic_dec(&ps->num_sta_ps);
1752
1753         local->total_ps_buffered -= buffered;
1754
1755         sta_info_recalc_tim(sta);
1756
1757         ps_dbg(sdata,
1758                "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1759                sta->sta.addr, sta->sta.aid, filtered, buffered);
1760
1761         ieee80211_check_fast_xmit(sta);
1762 }
1763
1764 static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1765                                          enum ieee80211_frame_release_type reason,
1766                                          bool call_driver, bool more_data)
1767 {
1768         struct ieee80211_sub_if_data *sdata = sta->sdata;
1769         struct ieee80211_local *local = sdata->local;
1770         struct ieee80211_qos_hdr *nullfunc;
1771         struct sk_buff *skb;
1772         int size = sizeof(*nullfunc);
1773         __le16 fc;
1774         bool qos = sta->sta.wme;
1775         struct ieee80211_tx_info *info;
1776         struct ieee80211_chanctx_conf *chanctx_conf;
1777
1778         if (qos) {
1779                 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1780                                  IEEE80211_STYPE_QOS_NULLFUNC |
1781                                  IEEE80211_FCTL_FROMDS);
1782         } else {
1783                 size -= 2;
1784                 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1785                                  IEEE80211_STYPE_NULLFUNC |
1786                                  IEEE80211_FCTL_FROMDS);
1787         }
1788
1789         skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1790         if (!skb)
1791                 return;
1792
1793         skb_reserve(skb, local->hw.extra_tx_headroom);
1794
1795         nullfunc = skb_put(skb, size);
1796         nullfunc->frame_control = fc;
1797         nullfunc->duration_id = 0;
1798         memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1799         memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1800         memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1801         nullfunc->seq_ctrl = 0;
1802
1803         skb->priority = tid;
1804         skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
1805         if (qos) {
1806                 nullfunc->qos_ctrl = cpu_to_le16(tid);
1807
1808                 if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1809                         nullfunc->qos_ctrl |=
1810                                 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1811                         if (more_data)
1812                                 nullfunc->frame_control |=
1813                                         cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1814                 }
1815         }
1816
1817         info = IEEE80211_SKB_CB(skb);
1818
1819         /*
1820          * Tell TX path to send this frame even though the
1821          * STA may still remain is PS mode after this frame
1822          * exchange. Also set EOSP to indicate this packet
1823          * ends the poll/service period.
1824          */
1825         info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1826                        IEEE80211_TX_STATUS_EOSP |
1827                        IEEE80211_TX_CTL_REQ_TX_STATUS;
1828
1829         info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1830
1831         if (call_driver)
1832                 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1833                                           reason, false);
1834
1835         skb->dev = sdata->dev;
1836
1837         rcu_read_lock();
1838         chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
1839         if (WARN_ON(!chanctx_conf)) {
1840                 rcu_read_unlock();
1841                 kfree_skb(skb);
1842                 return;
1843         }
1844
1845         info->band = chanctx_conf->def.chan->band;
1846         ieee80211_xmit(sdata, sta, skb);
1847         rcu_read_unlock();
1848 }
1849
1850 static int find_highest_prio_tid(unsigned long tids)
1851 {
1852         /* lower 3 TIDs aren't ordered perfectly */
1853         if (tids & 0xF8)
1854                 return fls(tids) - 1;
1855         /* TID 0 is BE just like TID 3 */
1856         if (tids & BIT(0))
1857                 return 0;
1858         return fls(tids) - 1;
1859 }
1860
1861 /* Indicates if the MORE_DATA bit should be set in the last
1862  * frame obtained by ieee80211_sta_ps_get_frames.
1863  * Note that driver_release_tids is relevant only if
1864  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
1865  */
1866 static bool
1867 ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
1868                            enum ieee80211_frame_release_type reason,
1869                            unsigned long driver_release_tids)
1870 {
1871         int ac;
1872
1873         /* If the driver has data on more than one TID then
1874          * certainly there's more data if we release just a
1875          * single frame now (from a single TID). This will
1876          * only happen for PS-Poll.
1877          */
1878         if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1879             hweight16(driver_release_tids) > 1)
1880                 return true;
1881
1882         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1883                 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1884                         continue;
1885
1886                 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1887                     !skb_queue_empty(&sta->ps_tx_buf[ac]))
1888                         return true;
1889         }
1890
1891         return false;
1892 }
1893
1894 static void
1895 ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
1896                             enum ieee80211_frame_release_type reason,
1897                             struct sk_buff_head *frames,
1898                             unsigned long *driver_release_tids)
1899 {
1900         struct ieee80211_sub_if_data *sdata = sta->sdata;
1901         struct ieee80211_local *local = sdata->local;
1902         int ac;
1903
1904         /* Get response frame(s) and more data bit for the last one. */
1905         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1906                 unsigned long tids;
1907
1908                 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1909                         continue;
1910
1911                 tids = ieee80211_tids_for_ac(ac);
1912
1913                 /* if we already have frames from software, then we can't also
1914                  * release from hardware queues
1915                  */
1916                 if (skb_queue_empty(frames)) {
1917                         *driver_release_tids |=
1918                                 sta->driver_buffered_tids & tids;
1919                         *driver_release_tids |= sta->txq_buffered_tids & tids;
1920                 }
1921
1922                 if (!*driver_release_tids) {
1923                         struct sk_buff *skb;
1924
1925                         while (n_frames > 0) {
1926                                 skb = skb_dequeue(&sta->tx_filtered[ac]);
1927                                 if (!skb) {
1928                                         skb = skb_dequeue(
1929                                                 &sta->ps_tx_buf[ac]);
1930                                         if (skb)
1931                                                 local->total_ps_buffered--;
1932                                 }
1933                                 if (!skb)
1934                                         break;
1935                                 n_frames--;
1936                                 __skb_queue_tail(frames, skb);
1937                         }
1938                 }
1939
1940                 /* If we have more frames buffered on this AC, then abort the
1941                  * loop since we can't send more data from other ACs before
1942                  * the buffered frames from this.
1943                  */
1944                 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1945                     !skb_queue_empty(&sta->ps_tx_buf[ac]))
1946                         break;
1947         }
1948 }
1949
1950 static void
1951 ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1952                                   int n_frames, u8 ignored_acs,
1953                                   enum ieee80211_frame_release_type reason)
1954 {
1955         struct ieee80211_sub_if_data *sdata = sta->sdata;
1956         struct ieee80211_local *local = sdata->local;
1957         unsigned long driver_release_tids = 0;
1958         struct sk_buff_head frames;
1959         bool more_data;
1960
1961         /* Service or PS-Poll period starts */
1962         set_sta_flag(sta, WLAN_STA_SP);
1963
1964         __skb_queue_head_init(&frames);
1965
1966         ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
1967                                     &frames, &driver_release_tids);
1968
1969         more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
1970
1971         if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
1972                 driver_release_tids =
1973                         BIT(find_highest_prio_tid(driver_release_tids));
1974
1975         if (skb_queue_empty(&frames) && !driver_release_tids) {
1976                 int tid, ac;
1977
1978                 /*
1979                  * For PS-Poll, this can only happen due to a race condition
1980                  * when we set the TIM bit and the station notices it, but
1981                  * before it can poll for the frame we expire it.
1982                  *
1983                  * For uAPSD, this is said in the standard (11.2.1.5 h):
1984                  *      At each unscheduled SP for a non-AP STA, the AP shall
1985                  *      attempt to transmit at least one MSDU or MMPDU, but no
1986                  *      more than the value specified in the Max SP Length field
1987                  *      in the QoS Capability element from delivery-enabled ACs,
1988                  *      that are destined for the non-AP STA.
1989                  *
1990                  * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1991                  */
1992
1993                 /* This will evaluate to 1, 3, 5 or 7. */
1994                 for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1995                         if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1996                                 break;
1997                 tid = 7 - 2 * ac;
1998
1999                 ieee80211_send_null_response(sta, tid, reason, true, false);
2000         } else if (!driver_release_tids) {
2001                 struct sk_buff_head pending;
2002                 struct sk_buff *skb;
2003                 int num = 0;
2004                 u16 tids = 0;
2005                 bool need_null = false;
2006
2007                 skb_queue_head_init(&pending);
2008
2009                 while ((skb = __skb_dequeue(&frames))) {
2010                         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2011                         struct ieee80211_hdr *hdr = (void *) skb->data;
2012                         u8 *qoshdr = NULL;
2013
2014                         num++;
2015
2016                         /*
2017                          * Tell TX path to send this frame even though the
2018                          * STA may still remain is PS mode after this frame
2019                          * exchange.
2020                          */
2021                         info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
2022                         info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
2023
2024                         /*
2025                          * Use MoreData flag to indicate whether there are
2026                          * more buffered frames for this STA
2027                          */
2028                         if (more_data || !skb_queue_empty(&frames))
2029                                 hdr->frame_control |=
2030                                         cpu_to_le16(IEEE80211_FCTL_MOREDATA);
2031                         else
2032                                 hdr->frame_control &=
2033                                         cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
2034
2035                         if (ieee80211_is_data_qos(hdr->frame_control) ||
2036                             ieee80211_is_qos_nullfunc(hdr->frame_control))
2037                                 qoshdr = ieee80211_get_qos_ctl(hdr);
2038
2039                         tids |= BIT(skb->priority);
2040
2041                         __skb_queue_tail(&pending, skb);
2042
2043                         /* end service period after last frame or add one */
2044                         if (!skb_queue_empty(&frames))
2045                                 continue;
2046
2047                         if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
2048                                 /* for PS-Poll, there's only one frame */
2049                                 info->flags |= IEEE80211_TX_STATUS_EOSP |
2050                                                IEEE80211_TX_CTL_REQ_TX_STATUS;
2051                                 break;
2052                         }
2053
2054                         /* For uAPSD, things are a bit more complicated. If the
2055                          * last frame has a QoS header (i.e. is a QoS-data or
2056                          * QoS-nulldata frame) then just set the EOSP bit there
2057                          * and be done.
2058                          * If the frame doesn't have a QoS header (which means
2059                          * it should be a bufferable MMPDU) then we can't set
2060                          * the EOSP bit in the QoS header; add a QoS-nulldata
2061                          * frame to the list to send it after the MMPDU.
2062                          *
2063                          * Note that this code is only in the mac80211-release
2064                          * code path, we assume that the driver will not buffer
2065                          * anything but QoS-data frames, or if it does, will
2066                          * create the QoS-nulldata frame by itself if needed.
2067                          *
2068                          * Cf. 802.11-2012 10.2.1.10 (c).
2069                          */
2070                         if (qoshdr) {
2071                                 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
2072
2073                                 info->flags |= IEEE80211_TX_STATUS_EOSP |
2074                                                IEEE80211_TX_CTL_REQ_TX_STATUS;
2075                         } else {
2076                                 /* The standard isn't completely clear on this
2077                                  * as it says the more-data bit should be set
2078                                  * if there are more BUs. The QoS-Null frame
2079                                  * we're about to send isn't buffered yet, we
2080                                  * only create it below, but let's pretend it
2081                                  * was buffered just in case some clients only
2082                                  * expect more-data=0 when eosp=1.
2083                                  */
2084                                 hdr->frame_control |=
2085                                         cpu_to_le16(IEEE80211_FCTL_MOREDATA);
2086                                 need_null = true;
2087                                 num++;
2088                         }
2089                         break;
2090                 }
2091
2092                 drv_allow_buffered_frames(local, sta, tids, num,
2093                                           reason, more_data);
2094
2095                 ieee80211_add_pending_skbs(local, &pending);
2096
2097                 if (need_null)
2098                         ieee80211_send_null_response(
2099                                 sta, find_highest_prio_tid(tids),
2100                                 reason, false, false);
2101
2102                 sta_info_recalc_tim(sta);
2103         } else {
2104                 int tid;
2105
2106                 /*
2107                  * We need to release a frame that is buffered somewhere in the
2108                  * driver ... it'll have to handle that.
2109                  * Note that the driver also has to check the number of frames
2110                  * on the TIDs we're releasing from - if there are more than
2111                  * n_frames it has to set the more-data bit (if we didn't ask
2112                  * it to set it anyway due to other buffered frames); if there
2113                  * are fewer than n_frames it has to make sure to adjust that
2114                  * to allow the service period to end properly.
2115                  */
2116                 drv_release_buffered_frames(local, sta, driver_release_tids,
2117                                             n_frames, reason, more_data);
2118
2119                 /*
2120                  * Note that we don't recalculate the TIM bit here as it would
2121                  * most likely have no effect at all unless the driver told us
2122                  * that the TID(s) became empty before returning here from the
2123                  * release function.
2124                  * Either way, however, when the driver tells us that the TID(s)
2125                  * became empty or we find that a txq became empty, we'll do the
2126                  * TIM recalculation.
2127                  */
2128
2129                 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
2130                         if (!sta->sta.txq[tid] ||
2131                             !(driver_release_tids & BIT(tid)) ||
2132                             txq_has_queue(sta->sta.txq[tid]))
2133                                 continue;
2134
2135                         sta_info_recalc_tim(sta);
2136                         break;
2137                 }
2138         }
2139 }
2140
2141 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
2142 {
2143         u8 ignore_for_response = sta->sta.uapsd_queues;
2144
2145         /*
2146          * If all ACs are delivery-enabled then we should reply
2147          * from any of them, if only some are enabled we reply
2148          * only from the non-enabled ones.
2149          */
2150         if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
2151                 ignore_for_response = 0;
2152
2153         ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
2154                                           IEEE80211_FRAME_RELEASE_PSPOLL);
2155 }
2156
2157 void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
2158 {
2159         int n_frames = sta->sta.max_sp;
2160         u8 delivery_enabled = sta->sta.uapsd_queues;
2161
2162         /*
2163          * If we ever grow support for TSPEC this might happen if
2164          * the TSPEC update from hostapd comes in between a trigger
2165          * frame setting WLAN_STA_UAPSD in the RX path and this
2166          * actually getting called.
2167          */
2168         if (!delivery_enabled)
2169                 return;
2170
2171         switch (sta->sta.max_sp) {
2172         case 1:
2173                 n_frames = 2;
2174                 break;
2175         case 2:
2176                 n_frames = 4;
2177                 break;
2178         case 3:
2179                 n_frames = 6;
2180                 break;
2181         case 0:
2182                 /* XXX: what is a good value? */
2183                 n_frames = 128;
2184                 break;
2185         }
2186
2187         ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
2188                                           IEEE80211_FRAME_RELEASE_UAPSD);
2189 }
2190
2191 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
2192                                struct ieee80211_sta *pubsta, bool block)
2193 {
2194         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2195
2196         trace_api_sta_block_awake(sta->local, pubsta, block);
2197
2198         if (block) {
2199                 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
2200                 ieee80211_clear_fast_xmit(sta);
2201                 return;
2202         }
2203
2204         if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
2205                 return;
2206
2207         if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
2208                 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
2209                 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
2210                 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
2211         } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
2212                    test_sta_flag(sta, WLAN_STA_UAPSD)) {
2213                 /* must be asleep in this case */
2214                 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
2215                 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
2216         } else {
2217                 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
2218                 ieee80211_check_fast_xmit(sta);
2219         }
2220 }
2221 EXPORT_SYMBOL(ieee80211_sta_block_awake);
2222
2223 void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
2224 {
2225         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2226         struct ieee80211_local *local = sta->local;
2227
2228         trace_api_eosp(local, pubsta);
2229
2230         clear_sta_flag(sta, WLAN_STA_SP);
2231 }
2232 EXPORT_SYMBOL(ieee80211_sta_eosp);
2233
2234 void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
2235 {
2236         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2237         enum ieee80211_frame_release_type reason;
2238         bool more_data;
2239
2240         trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
2241
2242         reason = IEEE80211_FRAME_RELEASE_UAPSD;
2243         more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
2244                                                reason, 0);
2245
2246         ieee80211_send_null_response(sta, tid, reason, false, more_data);
2247 }
2248 EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
2249
2250 void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
2251                                 u8 tid, bool buffered)
2252 {
2253         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2254
2255         if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
2256                 return;
2257
2258         trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
2259
2260         if (buffered)
2261                 set_bit(tid, &sta->driver_buffered_tids);
2262         else
2263                 clear_bit(tid, &sta->driver_buffered_tids);
2264
2265         sta_info_recalc_tim(sta);
2266 }
2267 EXPORT_SYMBOL(ieee80211_sta_set_buffered);
2268
2269 void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
2270                                     u32 tx_airtime, u32 rx_airtime)
2271 {
2272         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2273         struct ieee80211_local *local = sta->sdata->local;
2274         u8 ac = ieee80211_ac_from_tid(tid);
2275         u32 airtime = 0;
2276
2277         if (sta->local->airtime_flags & AIRTIME_USE_TX)
2278                 airtime += tx_airtime;
2279         if (sta->local->airtime_flags & AIRTIME_USE_RX)
2280                 airtime += rx_airtime;
2281
2282         spin_lock_bh(&local->active_txq_lock[ac]);
2283         sta->airtime[ac].tx_airtime += tx_airtime;
2284         sta->airtime[ac].rx_airtime += rx_airtime;
2285
2286         if (ieee80211_sta_keep_active(sta, ac))
2287                 sta->airtime[ac].deficit -= airtime;
2288
2289         spin_unlock_bh(&local->active_txq_lock[ac]);
2290 }
2291 EXPORT_SYMBOL(ieee80211_sta_register_airtime);
2292
2293 void __ieee80211_sta_recalc_aggregates(struct sta_info *sta, u16 active_links)
2294 {
2295         bool first = true;
2296         int link_id;
2297
2298         if (!sta->sta.valid_links || !sta->sta.mlo) {
2299                 sta->sta.cur = &sta->sta.deflink.agg;
2300                 return;
2301         }
2302
2303         rcu_read_lock();
2304         for (link_id = 0; link_id < ARRAY_SIZE((sta)->link); link_id++) {
2305                 struct ieee80211_link_sta *link_sta;
2306                 int i;
2307
2308                 if (!(active_links & BIT(link_id)))
2309                         continue;
2310
2311                 link_sta = rcu_dereference(sta->sta.link[link_id]);
2312                 if (!link_sta)
2313                         continue;
2314
2315                 if (first) {
2316                         sta->cur = sta->sta.deflink.agg;
2317                         first = false;
2318                         continue;
2319                 }
2320
2321                 sta->cur.max_amsdu_len =
2322                         min(sta->cur.max_amsdu_len,
2323                             link_sta->agg.max_amsdu_len);
2324                 sta->cur.max_rc_amsdu_len =
2325                         min(sta->cur.max_rc_amsdu_len,
2326                             link_sta->agg.max_rc_amsdu_len);
2327
2328                 for (i = 0; i < ARRAY_SIZE(sta->cur.max_tid_amsdu_len); i++)
2329                         sta->cur.max_tid_amsdu_len[i] =
2330                                 min(sta->cur.max_tid_amsdu_len[i],
2331                                     link_sta->agg.max_tid_amsdu_len[i]);
2332         }
2333         rcu_read_unlock();
2334
2335         sta->sta.cur = &sta->cur;
2336 }
2337
2338 void ieee80211_sta_recalc_aggregates(struct ieee80211_sta *pubsta)
2339 {
2340         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2341
2342         __ieee80211_sta_recalc_aggregates(sta, sta->sdata->vif.active_links);
2343 }
2344 EXPORT_SYMBOL(ieee80211_sta_recalc_aggregates);
2345
2346 void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
2347                                           struct sta_info *sta, u8 ac,
2348                                           u16 tx_airtime, bool tx_completed)
2349 {
2350         int tx_pending;
2351
2352         if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
2353                 return;
2354
2355         if (!tx_completed) {
2356                 if (sta)
2357                         atomic_add(tx_airtime,
2358                                    &sta->airtime[ac].aql_tx_pending);
2359
2360                 atomic_add(tx_airtime, &local->aql_total_pending_airtime);
2361                 atomic_add(tx_airtime, &local->aql_ac_pending_airtime[ac]);
2362                 return;
2363         }
2364
2365         if (sta) {
2366                 tx_pending = atomic_sub_return(tx_airtime,
2367                                                &sta->airtime[ac].aql_tx_pending);
2368                 if (tx_pending < 0)
2369                         atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
2370                                        tx_pending, 0);
2371         }
2372
2373         atomic_sub(tx_airtime, &local->aql_total_pending_airtime);
2374         tx_pending = atomic_sub_return(tx_airtime,
2375                                        &local->aql_ac_pending_airtime[ac]);
2376         if (WARN_ONCE(tx_pending < 0,
2377                       "Device %s AC %d pending airtime underflow: %u, %u",
2378                       wiphy_name(local->hw.wiphy), ac, tx_pending,
2379                       tx_airtime)) {
2380                 atomic_cmpxchg(&local->aql_ac_pending_airtime[ac],
2381                                tx_pending, 0);
2382                 atomic_sub(tx_pending, &local->aql_total_pending_airtime);
2383         }
2384 }
2385
2386 static struct ieee80211_sta_rx_stats *
2387 sta_get_last_rx_stats(struct sta_info *sta)
2388 {
2389         struct ieee80211_sta_rx_stats *stats = &sta->deflink.rx_stats;
2390         int cpu;
2391
2392         if (!sta->deflink.pcpu_rx_stats)
2393                 return stats;
2394
2395         for_each_possible_cpu(cpu) {
2396                 struct ieee80211_sta_rx_stats *cpustats;
2397
2398                 cpustats = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2399
2400                 if (time_after(cpustats->last_rx, stats->last_rx))
2401                         stats = cpustats;
2402         }
2403
2404         return stats;
2405 }
2406
2407 static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
2408                                   struct rate_info *rinfo)
2409 {
2410         rinfo->bw = STA_STATS_GET(BW, rate);
2411
2412         switch (STA_STATS_GET(TYPE, rate)) {
2413         case STA_STATS_RATE_TYPE_VHT:
2414                 rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2415                 rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2416                 rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2417                 if (STA_STATS_GET(SGI, rate))
2418                         rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2419                 break;
2420         case STA_STATS_RATE_TYPE_HT:
2421                 rinfo->flags = RATE_INFO_FLAGS_MCS;
2422                 rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2423                 if (STA_STATS_GET(SGI, rate))
2424                         rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2425                 break;
2426         case STA_STATS_RATE_TYPE_LEGACY: {
2427                 struct ieee80211_supported_band *sband;
2428                 u16 brate;
2429                 unsigned int shift;
2430                 int band = STA_STATS_GET(LEGACY_BAND, rate);
2431                 int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2432
2433                 sband = local->hw.wiphy->bands[band];
2434
2435                 if (WARN_ON_ONCE(!sband->bitrates))
2436                         break;
2437
2438                 brate = sband->bitrates[rate_idx].bitrate;
2439                 if (rinfo->bw == RATE_INFO_BW_5)
2440                         shift = 2;
2441                 else if (rinfo->bw == RATE_INFO_BW_10)
2442                         shift = 1;
2443                 else
2444                         shift = 0;
2445                 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
2446                 break;
2447                 }
2448         case STA_STATS_RATE_TYPE_HE:
2449                 rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
2450                 rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
2451                 rinfo->nss = STA_STATS_GET(HE_NSS, rate);
2452                 rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
2453                 rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
2454                 rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
2455                 break;
2456         case STA_STATS_RATE_TYPE_EHT:
2457                 rinfo->flags = RATE_INFO_FLAGS_EHT_MCS;
2458                 rinfo->mcs = STA_STATS_GET(EHT_MCS, rate);
2459                 rinfo->nss = STA_STATS_GET(EHT_NSS, rate);
2460                 rinfo->eht_gi = STA_STATS_GET(EHT_GI, rate);
2461                 rinfo->eht_ru_alloc = STA_STATS_GET(EHT_RU, rate);
2462                 break;
2463         }
2464 }
2465
2466 static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
2467 {
2468         u32 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
2469
2470         if (rate == STA_STATS_RATE_INVALID)
2471                 return -EINVAL;
2472
2473         sta_stats_decode_rate(sta->local, rate, rinfo);
2474         return 0;
2475 }
2476
2477 static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
2478                                         int tid)
2479 {
2480         unsigned int start;
2481         u64 value;
2482
2483         do {
2484                 start = u64_stats_fetch_begin(&rxstats->syncp);
2485                 value = rxstats->msdu[tid];
2486         } while (u64_stats_fetch_retry(&rxstats->syncp, start));
2487
2488         return value;
2489 }
2490
2491 static void sta_set_tidstats(struct sta_info *sta,
2492                              struct cfg80211_tid_stats *tidstats,
2493                              int tid)
2494 {
2495         struct ieee80211_local *local = sta->local;
2496         int cpu;
2497
2498         if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2499                 tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->deflink.rx_stats,
2500                                                            tid);
2501
2502                 if (sta->deflink.pcpu_rx_stats) {
2503                         for_each_possible_cpu(cpu) {
2504                                 struct ieee80211_sta_rx_stats *cpurxs;
2505
2506                                 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2507                                                      cpu);
2508                                 tidstats->rx_msdu +=
2509                                         sta_get_tidstats_msdu(cpurxs, tid);
2510                         }
2511                 }
2512
2513                 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
2514         }
2515
2516         if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
2517                 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2518                 tidstats->tx_msdu = sta->deflink.tx_stats.msdu[tid];
2519         }
2520
2521         if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
2522             ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2523                 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2524                 tidstats->tx_msdu_retries = sta->deflink.status_stats.msdu_retries[tid];
2525         }
2526
2527         if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
2528             ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2529                 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2530                 tidstats->tx_msdu_failed = sta->deflink.status_stats.msdu_failed[tid];
2531         }
2532
2533         if (tid < IEEE80211_NUM_TIDS) {
2534                 spin_lock_bh(&local->fq.lock);
2535                 rcu_read_lock();
2536
2537                 tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
2538                 ieee80211_fill_txq_stats(&tidstats->txq_stats,
2539                                          to_txq_info(sta->sta.txq[tid]));
2540
2541                 rcu_read_unlock();
2542                 spin_unlock_bh(&local->fq.lock);
2543         }
2544 }
2545
2546 static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2547 {
2548         unsigned int start;
2549         u64 value;
2550
2551         do {
2552                 start = u64_stats_fetch_begin(&rxstats->syncp);
2553                 value = rxstats->bytes;
2554         } while (u64_stats_fetch_retry(&rxstats->syncp, start));
2555
2556         return value;
2557 }
2558
2559 void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
2560                    bool tidstats)
2561 {
2562         struct ieee80211_sub_if_data *sdata = sta->sdata;
2563         struct ieee80211_local *local = sdata->local;
2564         u32 thr = 0;
2565         int i, ac, cpu;
2566         struct ieee80211_sta_rx_stats *last_rxstats;
2567
2568         last_rxstats = sta_get_last_rx_stats(sta);
2569
2570         sinfo->generation = sdata->local->sta_generation;
2571
2572         /* do before driver, so beacon filtering drivers have a
2573          * chance to e.g. just add the number of filtered beacons
2574          * (or just modify the value entirely, of course)
2575          */
2576         if (sdata->vif.type == NL80211_IFTYPE_STATION)
2577                 sinfo->rx_beacon = sdata->deflink.u.mgd.count_beacon_signal;
2578
2579         drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2580         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2581                          BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2582                          BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2583                          BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
2584                          BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
2585                          BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2586
2587         if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2588                 sinfo->beacon_loss_count =
2589                         sdata->deflink.u.mgd.beacon_loss_count;
2590                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2591         }
2592
2593         sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
2594         sinfo->assoc_at = sta->assoc_at;
2595         sinfo->inactive_time =
2596                 jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
2597
2598         if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2599                                BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2600                 sinfo->tx_bytes = 0;
2601                 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2602                         sinfo->tx_bytes += sta->deflink.tx_stats.bytes[ac];
2603                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2604         }
2605
2606         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
2607                 sinfo->tx_packets = 0;
2608                 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2609                         sinfo->tx_packets += sta->deflink.tx_stats.packets[ac];
2610                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
2611         }
2612
2613         if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2614                                BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2615                 sinfo->rx_bytes += sta_get_stats_bytes(&sta->deflink.rx_stats);
2616
2617                 if (sta->deflink.pcpu_rx_stats) {
2618                         for_each_possible_cpu(cpu) {
2619                                 struct ieee80211_sta_rx_stats *cpurxs;
2620
2621                                 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2622                                                      cpu);
2623                                 sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2624                         }
2625                 }
2626
2627                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
2628         }
2629
2630         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2631                 sinfo->rx_packets = sta->deflink.rx_stats.packets;
2632                 if (sta->deflink.pcpu_rx_stats) {
2633                         for_each_possible_cpu(cpu) {
2634                                 struct ieee80211_sta_rx_stats *cpurxs;
2635
2636                                 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2637                                                      cpu);
2638                                 sinfo->rx_packets += cpurxs->packets;
2639                         }
2640                 }
2641                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
2642         }
2643
2644         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2645                 sinfo->tx_retries = sta->deflink.status_stats.retry_count;
2646                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
2647         }
2648
2649         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2650                 sinfo->tx_failed = sta->deflink.status_stats.retry_failed;
2651                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
2652         }
2653
2654         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2655                 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2656                         sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2657                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2658         }
2659
2660         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2661                 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2662                         sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2663                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2664         }
2665
2666         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
2667                 sinfo->airtime_weight = sta->airtime_weight;
2668                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2669         }
2670
2671         sinfo->rx_dropped_misc = sta->deflink.rx_stats.dropped;
2672         if (sta->deflink.pcpu_rx_stats) {
2673                 for_each_possible_cpu(cpu) {
2674                         struct ieee80211_sta_rx_stats *cpurxs;
2675
2676                         cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2677                         sinfo->rx_dropped_misc += cpurxs->dropped;
2678                 }
2679         }
2680
2681         if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2682             !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2683                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2684                                  BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2685                 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2686         }
2687
2688         if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
2689             ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2690                 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2691                         sinfo->signal = (s8)last_rxstats->last_signal;
2692                         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2693                 }
2694
2695                 if (!sta->deflink.pcpu_rx_stats &&
2696                     !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
2697                         sinfo->signal_avg =
2698                                 -ewma_signal_read(&sta->deflink.rx_stats_avg.signal);
2699                         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
2700                 }
2701         }
2702
2703         /* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2704          * the sta->rx_stats struct, so the check here is fine with and without
2705          * pcpu statistics
2706          */
2707         if (last_rxstats->chains &&
2708             !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2709                                BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2710                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2711                 if (!sta->deflink.pcpu_rx_stats)
2712                         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2713
2714                 sinfo->chains = last_rxstats->chains;
2715
2716                 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2717                         sinfo->chain_signal[i] =
2718                                 last_rxstats->chain_signal_last[i];
2719                         sinfo->chain_signal_avg[i] =
2720                                 -ewma_signal_read(&sta->deflink.rx_stats_avg.chain_signal[i]);
2721                 }
2722         }
2723
2724         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) &&
2725             !sta->sta.valid_links &&
2726             ieee80211_rate_valid(&sta->deflink.tx_stats.last_rate)) {
2727                 sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate,
2728                                      &sinfo->txrate);
2729                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
2730         }
2731
2732         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) &&
2733             !sta->sta.valid_links) {
2734                 if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2735                         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
2736         }
2737
2738         if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
2739                 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
2740                         sta_set_tidstats(sta, &sinfo->pertid[i], i);
2741         }
2742
2743         if (ieee80211_vif_is_mesh(&sdata->vif)) {
2744 #ifdef CONFIG_MAC80211_MESH
2745                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2746                                  BIT_ULL(NL80211_STA_INFO_PLID) |
2747                                  BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2748                                  BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2749                                  BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2750                                  BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
2751                                  BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) |
2752                                  BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS);
2753
2754                 sinfo->llid = sta->mesh->llid;
2755                 sinfo->plid = sta->mesh->plid;
2756                 sinfo->plink_state = sta->mesh->plink_state;
2757                 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2758                         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2759                         sinfo->t_offset = sta->mesh->t_offset;
2760                 }
2761                 sinfo->local_pm = sta->mesh->local_pm;
2762                 sinfo->peer_pm = sta->mesh->peer_pm;
2763                 sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2764                 sinfo->connected_to_gate = sta->mesh->connected_to_gate;
2765                 sinfo->connected_to_as = sta->mesh->connected_to_as;
2766 #endif
2767         }
2768
2769         sinfo->bss_param.flags = 0;
2770         if (sdata->vif.bss_conf.use_cts_prot)
2771                 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2772         if (sdata->vif.bss_conf.use_short_preamble)
2773                 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2774         if (sdata->vif.bss_conf.use_short_slot)
2775                 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2776         sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2777         sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2778
2779         sinfo->sta_flags.set = 0;
2780         sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2781                                 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2782                                 BIT(NL80211_STA_FLAG_WME) |
2783                                 BIT(NL80211_STA_FLAG_MFP) |
2784                                 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2785                                 BIT(NL80211_STA_FLAG_ASSOCIATED) |
2786                                 BIT(NL80211_STA_FLAG_TDLS_PEER);
2787         if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2788                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2789         if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2790                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2791         if (sta->sta.wme)
2792                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2793         if (test_sta_flag(sta, WLAN_STA_MFP))
2794                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2795         if (test_sta_flag(sta, WLAN_STA_AUTH))
2796                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2797         if (test_sta_flag(sta, WLAN_STA_ASSOC))
2798                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2799         if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2800                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2801
2802         thr = sta_get_expected_throughput(sta);
2803
2804         if (thr != 0) {
2805                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2806                 sinfo->expected_throughput = thr;
2807         }
2808
2809         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2810             sta->deflink.status_stats.ack_signal_filled) {
2811                 sinfo->ack_signal = sta->deflink.status_stats.last_ack_signal;
2812                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2813         }
2814
2815         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
2816             sta->deflink.status_stats.ack_signal_filled) {
2817                 sinfo->avg_ack_signal =
2818                         -(s8)ewma_avg_signal_read(
2819                                 &sta->deflink.status_stats.avg_ack_signal);
2820                 sinfo->filled |=
2821                         BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
2822         }
2823
2824         if (ieee80211_vif_is_mesh(&sdata->vif)) {
2825                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2826                 sinfo->airtime_link_metric =
2827                         airtime_link_metric_get(local, sta);
2828         }
2829 }
2830
2831 u32 sta_get_expected_throughput(struct sta_info *sta)
2832 {
2833         struct ieee80211_sub_if_data *sdata = sta->sdata;
2834         struct ieee80211_local *local = sdata->local;
2835         struct rate_control_ref *ref = NULL;
2836         u32 thr = 0;
2837
2838         if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
2839                 ref = local->rate_ctrl;
2840
2841         /* check if the driver has a SW RC implementation */
2842         if (ref && ref->ops->get_expected_throughput)
2843                 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2844         else
2845                 thr = drv_get_expected_throughput(local, sta);
2846
2847         return thr;
2848 }
2849
2850 unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2851 {
2852         struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2853
2854         if (!sta->deflink.status_stats.last_ack ||
2855             time_after(stats->last_rx, sta->deflink.status_stats.last_ack))
2856                 return stats->last_rx;
2857         return sta->deflink.status_stats.last_ack;
2858 }
2859
2860 static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2861 {
2862         if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2863                 sta->cparams.target = MS2TIME(50);
2864                 sta->cparams.interval = MS2TIME(300);
2865                 sta->cparams.ecn = false;
2866         } else {
2867                 sta->cparams.target = MS2TIME(20);
2868                 sta->cparams.interval = MS2TIME(100);
2869                 sta->cparams.ecn = true;
2870         }
2871 }
2872
2873 void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2874                                            u32 thr)
2875 {
2876         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2877
2878         sta_update_codel_params(sta, thr);
2879 }
2880
2881 int ieee80211_sta_allocate_link(struct sta_info *sta, unsigned int link_id)
2882 {
2883         struct ieee80211_sub_if_data *sdata = sta->sdata;
2884         struct sta_link_alloc *alloc;
2885         int ret;
2886
2887         lockdep_assert_wiphy(sdata->local->hw.wiphy);
2888
2889         WARN_ON(!test_sta_flag(sta, WLAN_STA_INSERTED));
2890
2891         /* must represent an MLD from the start */
2892         if (WARN_ON(!sta->sta.valid_links))
2893                 return -EINVAL;
2894
2895         if (WARN_ON(sta->sta.valid_links & BIT(link_id) ||
2896                     sta->link[link_id]))
2897                 return -EBUSY;
2898
2899         alloc = kzalloc(sizeof(*alloc), GFP_KERNEL);
2900         if (!alloc)
2901                 return -ENOMEM;
2902
2903         ret = sta_info_alloc_link(sdata->local, &alloc->info, GFP_KERNEL);
2904         if (ret) {
2905                 kfree(alloc);
2906                 return ret;
2907         }
2908
2909         sta_info_add_link(sta, link_id, &alloc->info, &alloc->sta);
2910
2911         ieee80211_link_sta_debugfs_add(&alloc->info);
2912
2913         return 0;
2914 }
2915
2916 void ieee80211_sta_free_link(struct sta_info *sta, unsigned int link_id)
2917 {
2918         lockdep_assert_wiphy(sta->sdata->local->hw.wiphy);
2919
2920         WARN_ON(!test_sta_flag(sta, WLAN_STA_INSERTED));
2921
2922         sta_remove_link(sta, link_id, false);
2923 }
2924
2925 int ieee80211_sta_activate_link(struct sta_info *sta, unsigned int link_id)
2926 {
2927         struct ieee80211_sub_if_data *sdata = sta->sdata;
2928         struct link_sta_info *link_sta;
2929         u16 old_links = sta->sta.valid_links;
2930         u16 new_links = old_links | BIT(link_id);
2931         int ret;
2932
2933         link_sta = rcu_dereference_protected(sta->link[link_id],
2934                                              lockdep_is_held(&sdata->local->hw.wiphy->mtx));
2935
2936         if (WARN_ON(old_links == new_links || !link_sta))
2937                 return -EINVAL;
2938
2939         rcu_read_lock();
2940         if (link_sta_info_hash_lookup(sdata->local, link_sta->addr)) {
2941                 rcu_read_unlock();
2942                 return -EALREADY;
2943         }
2944         /* we only modify under the mutex so this is fine */
2945         rcu_read_unlock();
2946
2947         sta->sta.valid_links = new_links;
2948
2949         if (WARN_ON(!test_sta_flag(sta, WLAN_STA_INSERTED)))
2950                 goto hash;
2951
2952         ieee80211_recalc_min_chandef(sdata, link_id);
2953
2954         /* Ensure the values are updated for the driver,
2955          * redone by sta_remove_link on failure.
2956          */
2957         ieee80211_sta_recalc_aggregates(&sta->sta);
2958
2959         ret = drv_change_sta_links(sdata->local, sdata, &sta->sta,
2960                                    old_links, new_links);
2961         if (ret) {
2962                 sta->sta.valid_links = old_links;
2963                 sta_remove_link(sta, link_id, false);
2964                 return ret;
2965         }
2966
2967 hash:
2968         ret = link_sta_info_hash_add(sdata->local, link_sta);
2969         WARN_ON(ret);
2970         return 0;
2971 }
2972
2973 void ieee80211_sta_remove_link(struct sta_info *sta, unsigned int link_id)
2974 {
2975         struct ieee80211_sub_if_data *sdata = sta->sdata;
2976         u16 old_links = sta->sta.valid_links;
2977
2978         lockdep_assert_wiphy(sdata->local->hw.wiphy);
2979
2980         sta->sta.valid_links &= ~BIT(link_id);
2981
2982         if (!WARN_ON(!test_sta_flag(sta, WLAN_STA_INSERTED)))
2983                 drv_change_sta_links(sdata->local, sdata, &sta->sta,
2984                                      old_links, sta->sta.valid_links);
2985
2986         sta_remove_link(sta, link_id, true);
2987 }
2988
2989 void ieee80211_sta_set_max_amsdu_subframes(struct sta_info *sta,
2990                                            const u8 *ext_capab,
2991                                            unsigned int ext_capab_len)
2992 {
2993         u8 val;
2994
2995         sta->sta.max_amsdu_subframes = 0;
2996
2997         if (ext_capab_len < 8)
2998                 return;
2999
3000         /* The sender might not have sent the last bit, consider it to be 0 */
3001         val = u8_get_bits(ext_capab[7], WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB);
3002
3003         /* we did get all the bits, take the MSB as well */
3004         if (ext_capab_len >= 9)
3005                 val |= u8_get_bits(ext_capab[8],
3006                                    WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB) << 1;
3007
3008         if (val)
3009                 sta->sta.max_amsdu_subframes = 4 << (4 - val);
3010 }
3011
3012 #ifdef CONFIG_LOCKDEP
3013 bool lockdep_sta_mutex_held(struct ieee80211_sta *pubsta)
3014 {
3015         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
3016
3017         return lockdep_is_held(&sta->local->hw.wiphy->mtx);
3018 }
3019 EXPORT_SYMBOL(lockdep_sta_mutex_held);
3020 #endif