mt76: move state from struct mt76_dev to mt76_phy
[linux-2.6-microblaze.git] / drivers / net / wireless / mediatek / mt76 / mac80211.c
1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
4  */
5 #include <linux/of.h>
6 #include "mt76.h"
7
8 #define CHAN2G(_idx, _freq) {                   \
9         .band = NL80211_BAND_2GHZ,              \
10         .center_freq = (_freq),                 \
11         .hw_value = (_idx),                     \
12         .max_power = 30,                        \
13 }
14
15 #define CHAN5G(_idx, _freq) {                   \
16         .band = NL80211_BAND_5GHZ,              \
17         .center_freq = (_freq),                 \
18         .hw_value = (_idx),                     \
19         .max_power = 30,                        \
20 }
21
22 static const struct ieee80211_channel mt76_channels_2ghz[] = {
23         CHAN2G(1, 2412),
24         CHAN2G(2, 2417),
25         CHAN2G(3, 2422),
26         CHAN2G(4, 2427),
27         CHAN2G(5, 2432),
28         CHAN2G(6, 2437),
29         CHAN2G(7, 2442),
30         CHAN2G(8, 2447),
31         CHAN2G(9, 2452),
32         CHAN2G(10, 2457),
33         CHAN2G(11, 2462),
34         CHAN2G(12, 2467),
35         CHAN2G(13, 2472),
36         CHAN2G(14, 2484),
37 };
38
39 static const struct ieee80211_channel mt76_channels_5ghz[] = {
40         CHAN5G(36, 5180),
41         CHAN5G(40, 5200),
42         CHAN5G(44, 5220),
43         CHAN5G(48, 5240),
44
45         CHAN5G(52, 5260),
46         CHAN5G(56, 5280),
47         CHAN5G(60, 5300),
48         CHAN5G(64, 5320),
49
50         CHAN5G(100, 5500),
51         CHAN5G(104, 5520),
52         CHAN5G(108, 5540),
53         CHAN5G(112, 5560),
54         CHAN5G(116, 5580),
55         CHAN5G(120, 5600),
56         CHAN5G(124, 5620),
57         CHAN5G(128, 5640),
58         CHAN5G(132, 5660),
59         CHAN5G(136, 5680),
60         CHAN5G(140, 5700),
61
62         CHAN5G(149, 5745),
63         CHAN5G(153, 5765),
64         CHAN5G(157, 5785),
65         CHAN5G(161, 5805),
66         CHAN5G(165, 5825),
67 };
68
69 static const struct ieee80211_tpt_blink mt76_tpt_blink[] = {
70         { .throughput =   0 * 1024, .blink_time = 334 },
71         { .throughput =   1 * 1024, .blink_time = 260 },
72         { .throughput =   5 * 1024, .blink_time = 220 },
73         { .throughput =  10 * 1024, .blink_time = 190 },
74         { .throughput =  20 * 1024, .blink_time = 170 },
75         { .throughput =  50 * 1024, .blink_time = 150 },
76         { .throughput =  70 * 1024, .blink_time = 130 },
77         { .throughput = 100 * 1024, .blink_time = 110 },
78         { .throughput = 200 * 1024, .blink_time =  80 },
79         { .throughput = 300 * 1024, .blink_time =  50 },
80 };
81
82 static int mt76_led_init(struct mt76_dev *dev)
83 {
84         struct device_node *np = dev->dev->of_node;
85         struct ieee80211_hw *hw = dev->hw;
86         int led_pin;
87
88         if (!dev->led_cdev.brightness_set && !dev->led_cdev.blink_set)
89                 return 0;
90
91         snprintf(dev->led_name, sizeof(dev->led_name),
92                  "mt76-%s", wiphy_name(hw->wiphy));
93
94         dev->led_cdev.name = dev->led_name;
95         dev->led_cdev.default_trigger =
96                 ieee80211_create_tpt_led_trigger(hw,
97                                         IEEE80211_TPT_LEDTRIG_FL_RADIO,
98                                         mt76_tpt_blink,
99                                         ARRAY_SIZE(mt76_tpt_blink));
100
101         np = of_get_child_by_name(np, "led");
102         if (np) {
103                 if (!of_property_read_u32(np, "led-sources", &led_pin))
104                         dev->led_pin = led_pin;
105                 dev->led_al = of_property_read_bool(np, "led-active-low");
106         }
107
108         return led_classdev_register(dev->dev, &dev->led_cdev);
109 }
110
111 static void mt76_led_cleanup(struct mt76_dev *dev)
112 {
113         if (!dev->led_cdev.brightness_set && !dev->led_cdev.blink_set)
114                 return;
115
116         led_classdev_unregister(&dev->led_cdev);
117 }
118
119 static void mt76_init_stream_cap(struct mt76_dev *dev,
120                                  struct ieee80211_supported_band *sband,
121                                  bool vht)
122 {
123         struct ieee80211_sta_ht_cap *ht_cap = &sband->ht_cap;
124         int i, nstream = hweight8(dev->antenna_mask);
125         struct ieee80211_sta_vht_cap *vht_cap;
126         u16 mcs_map = 0;
127
128         if (nstream > 1)
129                 ht_cap->cap |= IEEE80211_HT_CAP_TX_STBC;
130         else
131                 ht_cap->cap &= ~IEEE80211_HT_CAP_TX_STBC;
132
133         for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
134                 ht_cap->mcs.rx_mask[i] = i < nstream ? 0xff : 0;
135
136         if (!vht)
137                 return;
138
139         vht_cap = &sband->vht_cap;
140         if (nstream > 1)
141                 vht_cap->cap |= IEEE80211_VHT_CAP_TXSTBC;
142         else
143                 vht_cap->cap &= ~IEEE80211_VHT_CAP_TXSTBC;
144
145         for (i = 0; i < 8; i++) {
146                 if (i < nstream)
147                         mcs_map |= (IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2));
148                 else
149                         mcs_map |=
150                                 (IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2));
151         }
152         vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
153         vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
154 }
155
156 void mt76_set_stream_caps(struct mt76_dev *dev, bool vht)
157 {
158         if (dev->cap.has_2ghz)
159                 mt76_init_stream_cap(dev, &dev->phy.sband_2g.sband, false);
160         if (dev->cap.has_5ghz)
161                 mt76_init_stream_cap(dev, &dev->phy.sband_5g.sband, vht);
162 }
163 EXPORT_SYMBOL_GPL(mt76_set_stream_caps);
164
165 static int
166 mt76_init_sband(struct mt76_dev *dev, struct mt76_sband *msband,
167                 const struct ieee80211_channel *chan, int n_chan,
168                 struct ieee80211_rate *rates, int n_rates, bool vht)
169 {
170         struct ieee80211_supported_band *sband = &msband->sband;
171         struct ieee80211_sta_ht_cap *ht_cap;
172         struct ieee80211_sta_vht_cap *vht_cap;
173         void *chanlist;
174         int size;
175
176         size = n_chan * sizeof(*chan);
177         chanlist = devm_kmemdup(dev->dev, chan, size, GFP_KERNEL);
178         if (!chanlist)
179                 return -ENOMEM;
180
181         msband->chan = devm_kcalloc(dev->dev, n_chan, sizeof(*msband->chan),
182                                     GFP_KERNEL);
183         if (!msband->chan)
184                 return -ENOMEM;
185
186         sband->channels = chanlist;
187         sband->n_channels = n_chan;
188         sband->bitrates = rates;
189         sband->n_bitrates = n_rates;
190         dev->phy.chandef.chan = &sband->channels[0];
191         dev->phy.chan_state = &msband->chan[0];
192
193         ht_cap = &sband->ht_cap;
194         ht_cap->ht_supported = true;
195         ht_cap->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
196                        IEEE80211_HT_CAP_GRN_FLD |
197                        IEEE80211_HT_CAP_SGI_20 |
198                        IEEE80211_HT_CAP_SGI_40 |
199                        (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT);
200
201         ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
202         ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
203         ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_4;
204
205         mt76_init_stream_cap(dev, sband, vht);
206
207         if (!vht)
208                 return 0;
209
210         vht_cap = &sband->vht_cap;
211         vht_cap->vht_supported = true;
212         vht_cap->cap |= IEEE80211_VHT_CAP_RXLDPC |
213                         IEEE80211_VHT_CAP_RXSTBC_1 |
214                         IEEE80211_VHT_CAP_SHORT_GI_80 |
215                         IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN |
216                         IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN |
217                         (3 << IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT);
218
219         return 0;
220 }
221
222 static int
223 mt76_init_sband_2g(struct mt76_dev *dev, struct ieee80211_rate *rates,
224                    int n_rates)
225 {
226         dev->hw->wiphy->bands[NL80211_BAND_2GHZ] = &dev->phy.sband_2g.sband;
227
228         return mt76_init_sband(dev, &dev->phy.sband_2g,
229                                mt76_channels_2ghz,
230                                ARRAY_SIZE(mt76_channels_2ghz),
231                                rates, n_rates, false);
232 }
233
234 static int
235 mt76_init_sband_5g(struct mt76_dev *dev, struct ieee80211_rate *rates,
236                    int n_rates, bool vht)
237 {
238         dev->hw->wiphy->bands[NL80211_BAND_5GHZ] = &dev->phy.sband_5g.sband;
239
240         return mt76_init_sband(dev, &dev->phy.sband_5g,
241                                mt76_channels_5ghz,
242                                ARRAY_SIZE(mt76_channels_5ghz),
243                                rates, n_rates, vht);
244 }
245
246 static void
247 mt76_check_sband(struct mt76_dev *dev, int band)
248 {
249         struct ieee80211_supported_band *sband = dev->hw->wiphy->bands[band];
250         bool found = false;
251         int i;
252
253         if (!sband)
254                 return;
255
256         for (i = 0; i < sband->n_channels; i++) {
257                 if (sband->channels[i].flags & IEEE80211_CHAN_DISABLED)
258                         continue;
259
260                 found = true;
261                 break;
262         }
263
264         if (found)
265                 return;
266
267         sband->n_channels = 0;
268         dev->hw->wiphy->bands[band] = NULL;
269 }
270
271 struct mt76_dev *
272 mt76_alloc_device(struct device *pdev, unsigned int size,
273                   const struct ieee80211_ops *ops,
274                   const struct mt76_driver_ops *drv_ops)
275 {
276         struct ieee80211_hw *hw;
277         struct mt76_phy *phy;
278         struct mt76_dev *dev;
279         int i;
280
281         hw = ieee80211_alloc_hw(size, ops);
282         if (!hw)
283                 return NULL;
284
285         dev = hw->priv;
286         dev->hw = hw;
287         dev->dev = pdev;
288         dev->drv = drv_ops;
289
290         phy = &dev->phy;
291         phy->dev = dev;
292         phy->hw = hw;
293
294         spin_lock_init(&dev->rx_lock);
295         spin_lock_init(&dev->lock);
296         spin_lock_init(&dev->cc_lock);
297         mutex_init(&dev->mutex);
298         init_waitqueue_head(&dev->tx_wait);
299         skb_queue_head_init(&dev->status_list);
300
301         INIT_LIST_HEAD(&dev->txwi_cache);
302
303         for (i = 0; i < ARRAY_SIZE(dev->q_rx); i++)
304                 skb_queue_head_init(&dev->rx_skb[i]);
305
306         tasklet_init(&dev->tx_tasklet, mt76_tx_tasklet, (unsigned long)dev);
307
308         return dev;
309 }
310 EXPORT_SYMBOL_GPL(mt76_alloc_device);
311
312 int mt76_register_device(struct mt76_dev *dev, bool vht,
313                          struct ieee80211_rate *rates, int n_rates)
314 {
315         struct ieee80211_hw *hw = dev->hw;
316         struct wiphy *wiphy = hw->wiphy;
317         int ret;
318
319         dev_set_drvdata(dev->dev, dev);
320
321         SET_IEEE80211_DEV(hw, dev->dev);
322         SET_IEEE80211_PERM_ADDR(hw, dev->macaddr);
323
324         wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR;
325
326         wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
327         wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_AIRTIME_FAIRNESS);
328
329         wiphy->available_antennas_tx = dev->antenna_mask;
330         wiphy->available_antennas_rx = dev->antenna_mask;
331
332         hw->txq_data_size = sizeof(struct mt76_txq);
333         hw->max_tx_fragments = 16;
334
335         ieee80211_hw_set(hw, SIGNAL_DBM);
336         ieee80211_hw_set(hw, PS_NULLFUNC_STACK);
337         ieee80211_hw_set(hw, HOST_BROADCAST_PS_BUFFERING);
338         ieee80211_hw_set(hw, AMPDU_AGGREGATION);
339         ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
340         ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
341         ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
342         ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
343         ieee80211_hw_set(hw, TX_AMSDU);
344         ieee80211_hw_set(hw, TX_FRAG_LIST);
345         ieee80211_hw_set(hw, MFP_CAPABLE);
346         ieee80211_hw_set(hw, AP_LINK_PS);
347         ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
348         ieee80211_hw_set(hw, NEEDS_UNIQUE_STA_ADDR);
349         ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
350
351         wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
352         wiphy->interface_modes =
353                 BIT(NL80211_IFTYPE_STATION) |
354                 BIT(NL80211_IFTYPE_AP) |
355 #ifdef CONFIG_MAC80211_MESH
356                 BIT(NL80211_IFTYPE_MESH_POINT) |
357 #endif
358                 BIT(NL80211_IFTYPE_ADHOC);
359
360         if (dev->cap.has_2ghz) {
361                 ret = mt76_init_sband_2g(dev, rates, n_rates);
362                 if (ret)
363                         return ret;
364         }
365
366         if (dev->cap.has_5ghz) {
367                 ret = mt76_init_sband_5g(dev, rates + 4, n_rates - 4, vht);
368                 if (ret)
369                         return ret;
370         }
371
372         wiphy_read_of_freq_limits(dev->hw->wiphy);
373         mt76_check_sband(dev, NL80211_BAND_2GHZ);
374         mt76_check_sband(dev, NL80211_BAND_5GHZ);
375
376         if (IS_ENABLED(CONFIG_MT76_LEDS)) {
377                 ret = mt76_led_init(dev);
378                 if (ret)
379                         return ret;
380         }
381
382         return ieee80211_register_hw(hw);
383 }
384 EXPORT_SYMBOL_GPL(mt76_register_device);
385
386 void mt76_unregister_device(struct mt76_dev *dev)
387 {
388         struct ieee80211_hw *hw = dev->hw;
389
390         if (IS_ENABLED(CONFIG_MT76_LEDS))
391                 mt76_led_cleanup(dev);
392         mt76_tx_status_check(dev, NULL, true);
393         ieee80211_unregister_hw(hw);
394 }
395 EXPORT_SYMBOL_GPL(mt76_unregister_device);
396
397 void mt76_free_device(struct mt76_dev *dev)
398 {
399         mt76_tx_free(dev);
400         ieee80211_free_hw(dev->hw);
401 }
402 EXPORT_SYMBOL_GPL(mt76_free_device);
403
404 void mt76_rx(struct mt76_dev *dev, enum mt76_rxq_id q, struct sk_buff *skb)
405 {
406         struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
407         struct mt76_phy *phy = mt76_dev_phy(dev, status->ext_phy);
408
409         if (!test_bit(MT76_STATE_RUNNING, &phy->state)) {
410                 dev_kfree_skb(skb);
411                 return;
412         }
413
414         __skb_queue_tail(&dev->rx_skb[q], skb);
415 }
416 EXPORT_SYMBOL_GPL(mt76_rx);
417
418 bool mt76_has_tx_pending(struct mt76_phy *phy)
419 {
420         struct mt76_dev *dev = phy->dev;
421         struct mt76_queue *q;
422         int i, offset;
423
424         offset = __MT_TXQ_MAX * (phy != &dev->phy);
425
426         for (i = 0; i < __MT_TXQ_MAX; i++) {
427                 q = dev->q_tx[offset + i].q;
428                 if (q && q->queued)
429                         return true;
430         }
431
432         return false;
433 }
434 EXPORT_SYMBOL_GPL(mt76_has_tx_pending);
435
436 static struct mt76_channel_state *
437 mt76_channel_state(struct mt76_phy *phy, struct ieee80211_channel *c)
438 {
439         struct mt76_sband *msband;
440         int idx;
441
442         if (c->band == NL80211_BAND_2GHZ)
443                 msband = &phy->sband_2g;
444         else
445                 msband = &phy->sband_5g;
446
447         idx = c - &msband->sband.channels[0];
448         return &msband->chan[idx];
449 }
450
451 static void
452 mt76_update_survey_active_time(struct mt76_phy *phy, ktime_t time)
453 {
454         struct mt76_channel_state *state = phy->chan_state;
455
456         state->cc_active += ktime_to_us(ktime_sub(time,
457                                                   phy->survey_time));
458         phy->survey_time = time;
459 }
460
461 void mt76_update_survey(struct mt76_dev *dev)
462 {
463         ktime_t cur_time;
464
465         if (dev->drv->update_survey)
466                 dev->drv->update_survey(dev);
467
468         cur_time = ktime_get_boottime();
469         mt76_update_survey_active_time(&dev->phy, cur_time);
470         if (dev->phy2)
471                 mt76_update_survey_active_time(dev->phy2, cur_time);
472
473         if (dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME) {
474                 struct mt76_channel_state *state = dev->phy.chan_state;
475
476                 spin_lock_bh(&dev->cc_lock);
477                 state->cc_bss_rx += dev->cur_cc_bss_rx;
478                 dev->cur_cc_bss_rx = 0;
479                 spin_unlock_bh(&dev->cc_lock);
480         }
481 }
482 EXPORT_SYMBOL_GPL(mt76_update_survey);
483
484 void mt76_set_channel(struct mt76_phy *phy)
485 {
486         struct mt76_dev *dev = phy->dev;
487         struct ieee80211_hw *hw = phy->hw;
488         struct cfg80211_chan_def *chandef = &hw->conf.chandef;
489         bool offchannel = hw->conf.flags & IEEE80211_CONF_OFFCHANNEL;
490         int timeout = HZ / 5;
491
492         wait_event_timeout(dev->tx_wait, !mt76_has_tx_pending(phy), timeout);
493         mt76_update_survey(dev);
494
495         phy->chandef = *chandef;
496         phy->chan_state = mt76_channel_state(phy, chandef->chan);
497
498         if (!offchannel)
499                 phy->main_chan = chandef->chan;
500
501         if (chandef->chan != phy->main_chan)
502                 memset(phy->chan_state, 0, sizeof(*phy->chan_state));
503 }
504 EXPORT_SYMBOL_GPL(mt76_set_channel);
505
506 int mt76_get_survey(struct ieee80211_hw *hw, int idx,
507                     struct survey_info *survey)
508 {
509         struct mt76_phy *phy = hw->priv;
510         struct mt76_dev *dev = phy->dev;
511         struct mt76_sband *sband;
512         struct ieee80211_channel *chan;
513         struct mt76_channel_state *state;
514         int ret = 0;
515
516         mutex_lock(&dev->mutex);
517         if (idx == 0 && dev->drv->update_survey)
518                 mt76_update_survey(dev);
519
520         sband = &phy->sband_2g;
521         if (idx >= sband->sband.n_channels) {
522                 idx -= sband->sband.n_channels;
523                 sband = &phy->sband_5g;
524         }
525
526         if (idx >= sband->sband.n_channels) {
527                 ret = -ENOENT;
528                 goto out;
529         }
530
531         chan = &sband->sband.channels[idx];
532         state = mt76_channel_state(phy, chan);
533
534         memset(survey, 0, sizeof(*survey));
535         survey->channel = chan;
536         survey->filled = SURVEY_INFO_TIME | SURVEY_INFO_TIME_BUSY;
537         survey->filled |= dev->drv->survey_flags;
538         if (chan == phy->main_chan) {
539                 survey->filled |= SURVEY_INFO_IN_USE;
540
541                 if (dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME)
542                         survey->filled |= SURVEY_INFO_TIME_BSS_RX;
543         }
544
545         survey->time_busy = div_u64(state->cc_busy, 1000);
546         survey->time_rx = div_u64(state->cc_rx, 1000);
547         survey->time = div_u64(state->cc_active, 1000);
548
549         spin_lock_bh(&dev->cc_lock);
550         survey->time_bss_rx = div_u64(state->cc_bss_rx, 1000);
551         survey->time_tx = div_u64(state->cc_tx, 1000);
552         spin_unlock_bh(&dev->cc_lock);
553
554 out:
555         mutex_unlock(&dev->mutex);
556
557         return ret;
558 }
559 EXPORT_SYMBOL_GPL(mt76_get_survey);
560
561 void mt76_wcid_key_setup(struct mt76_dev *dev, struct mt76_wcid *wcid,
562                          struct ieee80211_key_conf *key)
563 {
564         struct ieee80211_key_seq seq;
565         int i;
566
567         wcid->rx_check_pn = false;
568
569         if (!key)
570                 return;
571
572         if (key->cipher != WLAN_CIPHER_SUITE_CCMP)
573                 return;
574
575         wcid->rx_check_pn = true;
576         for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
577                 ieee80211_get_key_rx_seq(key, i, &seq);
578                 memcpy(wcid->rx_key_pn[i], seq.ccmp.pn, sizeof(seq.ccmp.pn));
579         }
580 }
581 EXPORT_SYMBOL(mt76_wcid_key_setup);
582
583 static void
584 mt76_rx_convert(struct mt76_dev *dev, struct sk_buff *skb,
585                 struct ieee80211_hw **hw,
586                 struct ieee80211_sta **sta)
587 {
588
589         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
590         struct mt76_rx_status mstat;
591
592         mstat = *((struct mt76_rx_status *)skb->cb);
593         memset(status, 0, sizeof(*status));
594
595         status->flag = mstat.flag;
596         status->freq = mstat.freq;
597         status->enc_flags = mstat.enc_flags;
598         status->encoding = mstat.encoding;
599         status->bw = mstat.bw;
600         status->rate_idx = mstat.rate_idx;
601         status->nss = mstat.nss;
602         status->band = mstat.band;
603         status->signal = mstat.signal;
604         status->chains = mstat.chains;
605         status->ampdu_reference = mstat.ampdu_ref;
606
607         BUILD_BUG_ON(sizeof(mstat) > sizeof(skb->cb));
608         BUILD_BUG_ON(sizeof(status->chain_signal) !=
609                      sizeof(mstat.chain_signal));
610         memcpy(status->chain_signal, mstat.chain_signal,
611                sizeof(mstat.chain_signal));
612
613         *sta = wcid_to_sta(mstat.wcid);
614         *hw = mt76_phy_hw(dev, mstat.ext_phy);
615 }
616
617 static int
618 mt76_check_ccmp_pn(struct sk_buff *skb)
619 {
620         struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
621         struct mt76_wcid *wcid = status->wcid;
622         struct ieee80211_hdr *hdr;
623         int ret;
624
625         if (!(status->flag & RX_FLAG_DECRYPTED))
626                 return 0;
627
628         if (!wcid || !wcid->rx_check_pn)
629                 return 0;
630
631         if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
632                 /*
633                  * Validate the first fragment both here and in mac80211
634                  * All further fragments will be validated by mac80211 only.
635                  */
636                 hdr = (struct ieee80211_hdr *)skb->data;
637                 if (ieee80211_is_frag(hdr) &&
638                     !ieee80211_is_first_frag(hdr->frame_control))
639                         return 0;
640         }
641
642         BUILD_BUG_ON(sizeof(status->iv) != sizeof(wcid->rx_key_pn[0]));
643         ret = memcmp(status->iv, wcid->rx_key_pn[status->tid],
644                      sizeof(status->iv));
645         if (ret <= 0)
646                 return -EINVAL; /* replay */
647
648         memcpy(wcid->rx_key_pn[status->tid], status->iv, sizeof(status->iv));
649
650         if (status->flag & RX_FLAG_IV_STRIPPED)
651                 status->flag |= RX_FLAG_PN_VALIDATED;
652
653         return 0;
654 }
655
656 static void
657 mt76_airtime_report(struct mt76_dev *dev, struct mt76_rx_status *status,
658                     int len)
659 {
660         struct mt76_wcid *wcid = status->wcid;
661         struct ieee80211_sta *sta;
662         u32 airtime;
663
664         airtime = mt76_calc_rx_airtime(dev, status, len);
665         spin_lock(&dev->cc_lock);
666         dev->cur_cc_bss_rx += airtime;
667         spin_unlock(&dev->cc_lock);
668
669         if (!wcid || !wcid->sta)
670                 return;
671
672         sta = container_of((void *)wcid, struct ieee80211_sta, drv_priv);
673         ieee80211_sta_register_airtime(sta, status->tid, 0, airtime);
674 }
675
676 static void
677 mt76_airtime_flush_ampdu(struct mt76_dev *dev)
678 {
679         struct mt76_wcid *wcid;
680         int wcid_idx;
681
682         if (!dev->rx_ampdu_len)
683                 return;
684
685         wcid_idx = dev->rx_ampdu_status.wcid_idx;
686         if (wcid_idx < ARRAY_SIZE(dev->wcid))
687                 wcid = rcu_dereference(dev->wcid[wcid_idx]);
688         else
689                 wcid = NULL;
690         dev->rx_ampdu_status.wcid = wcid;
691
692         mt76_airtime_report(dev, &dev->rx_ampdu_status, dev->rx_ampdu_len);
693
694         dev->rx_ampdu_len = 0;
695         dev->rx_ampdu_ref = 0;
696 }
697
698 static void
699 mt76_airtime_check(struct mt76_dev *dev, struct sk_buff *skb)
700 {
701         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
702         struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
703         struct mt76_wcid *wcid = status->wcid;
704
705         if (!(dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME))
706                 return;
707
708         if (!wcid || !wcid->sta) {
709                 if (!ether_addr_equal(hdr->addr1, dev->macaddr))
710                         return;
711
712                 wcid = NULL;
713         }
714
715         if (!(status->flag & RX_FLAG_AMPDU_DETAILS) ||
716             status->ampdu_ref != dev->rx_ampdu_ref)
717                 mt76_airtime_flush_ampdu(dev);
718
719         if (status->flag & RX_FLAG_AMPDU_DETAILS) {
720                 if (!dev->rx_ampdu_len ||
721                     status->ampdu_ref != dev->rx_ampdu_ref) {
722                         dev->rx_ampdu_status = *status;
723                         dev->rx_ampdu_status.wcid_idx = wcid ? wcid->idx : 0xff;
724                         dev->rx_ampdu_ref = status->ampdu_ref;
725                 }
726
727                 dev->rx_ampdu_len += skb->len;
728                 return;
729         }
730
731         mt76_airtime_report(dev, status, skb->len);
732 }
733
734 static void
735 mt76_check_sta(struct mt76_dev *dev, struct sk_buff *skb)
736 {
737         struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
738         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
739         struct ieee80211_sta *sta;
740         struct ieee80211_hw *hw;
741         struct mt76_wcid *wcid = status->wcid;
742         bool ps;
743         int i;
744
745         hw = mt76_phy_hw(dev, status->ext_phy);
746         if (ieee80211_is_pspoll(hdr->frame_control) && !wcid) {
747                 sta = ieee80211_find_sta_by_ifaddr(hw, hdr->addr2, NULL);
748                 if (sta)
749                         wcid = status->wcid = (struct mt76_wcid *)sta->drv_priv;
750         }
751
752         mt76_airtime_check(dev, skb);
753
754         if (!wcid || !wcid->sta)
755                 return;
756
757         sta = container_of((void *)wcid, struct ieee80211_sta, drv_priv);
758
759         if (status->signal <= 0)
760                 ewma_signal_add(&wcid->rssi, -status->signal);
761
762         wcid->inactive_count = 0;
763
764         if (!test_bit(MT_WCID_FLAG_CHECK_PS, &wcid->flags))
765                 return;
766
767         if (ieee80211_is_pspoll(hdr->frame_control)) {
768                 ieee80211_sta_pspoll(sta);
769                 return;
770         }
771
772         if (ieee80211_has_morefrags(hdr->frame_control) ||
773             !(ieee80211_is_mgmt(hdr->frame_control) ||
774               ieee80211_is_data(hdr->frame_control)))
775                 return;
776
777         ps = ieee80211_has_pm(hdr->frame_control);
778
779         if (ps && (ieee80211_is_data_qos(hdr->frame_control) ||
780                    ieee80211_is_qos_nullfunc(hdr->frame_control)))
781                 ieee80211_sta_uapsd_trigger(sta, status->tid);
782
783         if (!!test_bit(MT_WCID_FLAG_PS, &wcid->flags) == ps)
784                 return;
785
786         if (ps)
787                 set_bit(MT_WCID_FLAG_PS, &wcid->flags);
788         else
789                 clear_bit(MT_WCID_FLAG_PS, &wcid->flags);
790
791         dev->drv->sta_ps(dev, sta, ps);
792         ieee80211_sta_ps_transition(sta, ps);
793
794         if (ps)
795                 return;
796
797         for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
798                 struct mt76_txq *mtxq;
799
800                 if (!sta->txq[i])
801                         continue;
802
803                 mtxq = (struct mt76_txq *)sta->txq[i]->drv_priv;
804                 if (!skb_queue_empty(&mtxq->retry_q))
805                         ieee80211_schedule_txq(hw, sta->txq[i]);
806         }
807 }
808
809 void mt76_rx_complete(struct mt76_dev *dev, struct sk_buff_head *frames,
810                       struct napi_struct *napi)
811 {
812         struct ieee80211_sta *sta;
813         struct ieee80211_hw *hw;
814         struct sk_buff *skb;
815
816         spin_lock(&dev->rx_lock);
817         while ((skb = __skb_dequeue(frames)) != NULL) {
818                 if (mt76_check_ccmp_pn(skb)) {
819                         dev_kfree_skb(skb);
820                         continue;
821                 }
822
823                 mt76_rx_convert(dev, skb, &hw, &sta);
824                 ieee80211_rx_napi(hw, sta, skb, napi);
825         }
826         spin_unlock(&dev->rx_lock);
827 }
828
829 void mt76_rx_poll_complete(struct mt76_dev *dev, enum mt76_rxq_id q,
830                            struct napi_struct *napi)
831 {
832         struct sk_buff_head frames;
833         struct sk_buff *skb;
834
835         __skb_queue_head_init(&frames);
836
837         while ((skb = __skb_dequeue(&dev->rx_skb[q])) != NULL) {
838                 mt76_check_sta(dev, skb);
839                 mt76_rx_aggr_reorder(skb, &frames);
840         }
841
842         mt76_rx_complete(dev, &frames, napi);
843 }
844 EXPORT_SYMBOL_GPL(mt76_rx_poll_complete);
845
846 static int
847 mt76_sta_add(struct mt76_dev *dev, struct ieee80211_vif *vif,
848              struct ieee80211_sta *sta, bool ext_phy)
849 {
850         struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
851         int ret;
852         int i;
853
854         mutex_lock(&dev->mutex);
855
856         ret = dev->drv->sta_add(dev, vif, sta);
857         if (ret)
858                 goto out;
859
860         for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
861                 struct mt76_txq *mtxq;
862
863                 if (!sta->txq[i])
864                         continue;
865
866                 mtxq = (struct mt76_txq *)sta->txq[i]->drv_priv;
867                 mtxq->wcid = wcid;
868
869                 mt76_txq_init(dev, sta->txq[i]);
870         }
871
872         ewma_signal_init(&wcid->rssi);
873         if (ext_phy)
874                 mt76_wcid_mask_set(dev->wcid_phy_mask, wcid->idx);
875         rcu_assign_pointer(dev->wcid[wcid->idx], wcid);
876
877 out:
878         mutex_unlock(&dev->mutex);
879
880         return ret;
881 }
882
883 void __mt76_sta_remove(struct mt76_dev *dev, struct ieee80211_vif *vif,
884                        struct ieee80211_sta *sta)
885 {
886         struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
887         int i, idx = wcid->idx;
888
889         rcu_assign_pointer(dev->wcid[idx], NULL);
890         synchronize_rcu();
891
892         for (i = 0; i < ARRAY_SIZE(wcid->aggr); i++)
893                 mt76_rx_aggr_stop(dev, wcid, i);
894
895         if (dev->drv->sta_remove)
896                 dev->drv->sta_remove(dev, vif, sta);
897
898         mt76_tx_status_check(dev, wcid, true);
899         for (i = 0; i < ARRAY_SIZE(sta->txq); i++)
900                 mt76_txq_remove(dev, sta->txq[i]);
901         mt76_wcid_mask_clear(dev->wcid_mask, idx);
902         mt76_wcid_mask_clear(dev->wcid_phy_mask, idx);
903 }
904 EXPORT_SYMBOL_GPL(__mt76_sta_remove);
905
906 static void
907 mt76_sta_remove(struct mt76_dev *dev, struct ieee80211_vif *vif,
908                 struct ieee80211_sta *sta)
909 {
910         mutex_lock(&dev->mutex);
911         __mt76_sta_remove(dev, vif, sta);
912         mutex_unlock(&dev->mutex);
913 }
914
915 int mt76_sta_state(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
916                    struct ieee80211_sta *sta,
917                    enum ieee80211_sta_state old_state,
918                    enum ieee80211_sta_state new_state)
919 {
920         struct mt76_phy *phy = hw->priv;
921         struct mt76_dev *dev = phy->dev;
922         bool ext_phy = phy != &dev->phy;
923
924         if (old_state == IEEE80211_STA_NOTEXIST &&
925             new_state == IEEE80211_STA_NONE)
926                 return mt76_sta_add(dev, vif, sta, ext_phy);
927
928         if (old_state == IEEE80211_STA_AUTH &&
929             new_state == IEEE80211_STA_ASSOC &&
930             dev->drv->sta_assoc)
931                 dev->drv->sta_assoc(dev, vif, sta);
932
933         if (old_state == IEEE80211_STA_NONE &&
934             new_state == IEEE80211_STA_NOTEXIST)
935                 mt76_sta_remove(dev, vif, sta);
936
937         return 0;
938 }
939 EXPORT_SYMBOL_GPL(mt76_sta_state);
940
941 int mt76_get_txpower(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
942                      int *dbm)
943 {
944         struct mt76_dev *dev = hw->priv;
945         int n_chains = hweight8(dev->antenna_mask);
946
947         *dbm = DIV_ROUND_UP(dev->txpower_cur, 2);
948
949         /* convert from per-chain power to combined
950          * output power
951          */
952         switch (n_chains) {
953         case 4:
954                 *dbm += 6;
955                 break;
956         case 3:
957                 *dbm += 4;
958                 break;
959         case 2:
960                 *dbm += 3;
961                 break;
962         default:
963                 break;
964         }
965
966         return 0;
967 }
968 EXPORT_SYMBOL_GPL(mt76_get_txpower);
969
970 static void
971 __mt76_csa_finish(void *priv, u8 *mac, struct ieee80211_vif *vif)
972 {
973         if (vif->csa_active && ieee80211_csa_is_complete(vif))
974                 ieee80211_csa_finish(vif);
975 }
976
977 void mt76_csa_finish(struct mt76_dev *dev)
978 {
979         if (!dev->csa_complete)
980                 return;
981
982         ieee80211_iterate_active_interfaces_atomic(dev->hw,
983                 IEEE80211_IFACE_ITER_RESUME_ALL,
984                 __mt76_csa_finish, dev);
985
986         dev->csa_complete = 0;
987 }
988 EXPORT_SYMBOL_GPL(mt76_csa_finish);
989
990 static void
991 __mt76_csa_check(void *priv, u8 *mac, struct ieee80211_vif *vif)
992 {
993         struct mt76_dev *dev = priv;
994
995         if (!vif->csa_active)
996                 return;
997
998         dev->csa_complete |= ieee80211_csa_is_complete(vif);
999 }
1000
1001 void mt76_csa_check(struct mt76_dev *dev)
1002 {
1003         ieee80211_iterate_active_interfaces_atomic(dev->hw,
1004                 IEEE80211_IFACE_ITER_RESUME_ALL,
1005                 __mt76_csa_check, dev);
1006 }
1007 EXPORT_SYMBOL_GPL(mt76_csa_check);
1008
1009 int
1010 mt76_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta, bool set)
1011 {
1012         return 0;
1013 }
1014 EXPORT_SYMBOL_GPL(mt76_set_tim);
1015
1016 void mt76_insert_ccmp_hdr(struct sk_buff *skb, u8 key_id)
1017 {
1018         struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
1019         int hdr_len = ieee80211_get_hdrlen_from_skb(skb);
1020         u8 *hdr, *pn = status->iv;
1021
1022         __skb_push(skb, 8);
1023         memmove(skb->data, skb->data + 8, hdr_len);
1024         hdr = skb->data + hdr_len;
1025
1026         hdr[0] = pn[5];
1027         hdr[1] = pn[4];
1028         hdr[2] = 0;
1029         hdr[3] = 0x20 | (key_id << 6);
1030         hdr[4] = pn[3];
1031         hdr[5] = pn[2];
1032         hdr[6] = pn[1];
1033         hdr[7] = pn[0];
1034
1035         status->flag &= ~RX_FLAG_IV_STRIPPED;
1036 }
1037 EXPORT_SYMBOL_GPL(mt76_insert_ccmp_hdr);
1038
1039 int mt76_get_rate(struct mt76_dev *dev,
1040                   struct ieee80211_supported_band *sband,
1041                   int idx, bool cck)
1042 {
1043         int i, offset = 0, len = sband->n_bitrates;
1044
1045         if (cck) {
1046                 if (sband == &dev->phy.sband_5g.sband)
1047                         return 0;
1048
1049                 idx &= ~BIT(2); /* short preamble */
1050         } else if (sband == &dev->phy.sband_2g.sband) {
1051                 offset = 4;
1052         }
1053
1054         for (i = offset; i < len; i++) {
1055                 if ((sband->bitrates[i].hw_value & GENMASK(7, 0)) == idx)
1056                         return i;
1057         }
1058
1059         return 0;
1060 }
1061 EXPORT_SYMBOL_GPL(mt76_get_rate);
1062
1063 void mt76_sw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1064                   const u8 *mac)
1065 {
1066         struct mt76_phy *phy = hw->priv;
1067
1068         set_bit(MT76_SCANNING, &phy->state);
1069 }
1070 EXPORT_SYMBOL_GPL(mt76_sw_scan);
1071
1072 void mt76_sw_scan_complete(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1073 {
1074         struct mt76_phy *phy = hw->priv;
1075
1076         clear_bit(MT76_SCANNING, &phy->state);
1077 }
1078 EXPORT_SYMBOL_GPL(mt76_sw_scan_complete);
1079
1080 int mt76_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
1081 {
1082         struct mt76_dev *dev = hw->priv;
1083
1084         mutex_lock(&dev->mutex);
1085         *tx_ant = dev->antenna_mask;
1086         *rx_ant = dev->antenna_mask;
1087         mutex_unlock(&dev->mutex);
1088
1089         return 0;
1090 }
1091 EXPORT_SYMBOL_GPL(mt76_get_antenna);