OSDN Git Service

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