blob: e8b9b0f86e3859d84c6d66ffbac364916de8c508 [file] [log] [blame]
developerb11a5392022-03-31 00:34:47 +08001// SPDX-License-Identifier: ISC
2/*
3 * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
4 */
5#include <linux/sched.h>
6#include <linux/of.h>
7#include "mt76.h"
8
9#define CHAN2G(_idx, _freq) { \
10 .band = NL80211_BAND_2GHZ, \
11 .center_freq = (_freq), \
12 .hw_value = (_idx), \
13 .max_power = 30, \
14}
15
16#define CHAN5G(_idx, _freq) { \
17 .band = NL80211_BAND_5GHZ, \
18 .center_freq = (_freq), \
19 .hw_value = (_idx), \
20 .max_power = 30, \
21}
22
23#define CHAN6G(_idx, _freq) { \
24 .band = NL80211_BAND_6GHZ, \
25 .center_freq = (_freq), \
26 .hw_value = (_idx), \
27 .max_power = 30, \
28}
29
30static const struct ieee80211_channel mt76_channels_2ghz[] = {
31 CHAN2G(1, 2412),
32 CHAN2G(2, 2417),
33 CHAN2G(3, 2422),
34 CHAN2G(4, 2427),
35 CHAN2G(5, 2432),
36 CHAN2G(6, 2437),
37 CHAN2G(7, 2442),
38 CHAN2G(8, 2447),
39 CHAN2G(9, 2452),
40 CHAN2G(10, 2457),
41 CHAN2G(11, 2462),
42 CHAN2G(12, 2467),
43 CHAN2G(13, 2472),
44 CHAN2G(14, 2484),
45};
46
47static const struct ieee80211_channel mt76_channels_5ghz[] = {
48 CHAN5G(36, 5180),
49 CHAN5G(40, 5200),
50 CHAN5G(44, 5220),
51 CHAN5G(48, 5240),
52
53 CHAN5G(52, 5260),
54 CHAN5G(56, 5280),
55 CHAN5G(60, 5300),
56 CHAN5G(64, 5320),
57
58 CHAN5G(100, 5500),
59 CHAN5G(104, 5520),
60 CHAN5G(108, 5540),
61 CHAN5G(112, 5560),
62 CHAN5G(116, 5580),
63 CHAN5G(120, 5600),
64 CHAN5G(124, 5620),
65 CHAN5G(128, 5640),
66 CHAN5G(132, 5660),
67 CHAN5G(136, 5680),
68 CHAN5G(140, 5700),
69 CHAN5G(144, 5720),
70
71 CHAN5G(149, 5745),
72 CHAN5G(153, 5765),
73 CHAN5G(157, 5785),
74 CHAN5G(161, 5805),
75 CHAN5G(165, 5825),
76 CHAN5G(169, 5845),
77 CHAN5G(173, 5865),
78};
79
80static const struct ieee80211_channel mt76_channels_6ghz[] = {
81 /* UNII-5 */
82 CHAN6G(1, 5955),
83 CHAN6G(5, 5975),
84 CHAN6G(9, 5995),
85 CHAN6G(13, 6015),
86 CHAN6G(17, 6035),
87 CHAN6G(21, 6055),
88 CHAN6G(25, 6075),
89 CHAN6G(29, 6095),
90 CHAN6G(33, 6115),
91 CHAN6G(37, 6135),
92 CHAN6G(41, 6155),
93 CHAN6G(45, 6175),
94 CHAN6G(49, 6195),
95 CHAN6G(53, 6215),
96 CHAN6G(57, 6235),
97 CHAN6G(61, 6255),
98 CHAN6G(65, 6275),
99 CHAN6G(69, 6295),
100 CHAN6G(73, 6315),
101 CHAN6G(77, 6335),
102 CHAN6G(81, 6355),
103 CHAN6G(85, 6375),
104 CHAN6G(89, 6395),
105 CHAN6G(93, 6415),
106 /* UNII-6 */
107 CHAN6G(97, 6435),
108 CHAN6G(101, 6455),
109 CHAN6G(105, 6475),
110 CHAN6G(109, 6495),
111 CHAN6G(113, 6515),
112 CHAN6G(117, 6535),
113 /* UNII-7 */
114 CHAN6G(121, 6555),
115 CHAN6G(125, 6575),
116 CHAN6G(129, 6595),
117 CHAN6G(133, 6615),
118 CHAN6G(137, 6635),
119 CHAN6G(141, 6655),
120 CHAN6G(145, 6675),
121 CHAN6G(149, 6695),
122 CHAN6G(153, 6715),
123 CHAN6G(157, 6735),
124 CHAN6G(161, 6755),
125 CHAN6G(165, 6775),
126 CHAN6G(169, 6795),
127 CHAN6G(173, 6815),
128 CHAN6G(177, 6835),
129 CHAN6G(181, 6855),
130 CHAN6G(185, 6875),
131 /* UNII-8 */
132 CHAN6G(189, 6895),
133 CHAN6G(193, 6915),
134 CHAN6G(197, 6935),
135 CHAN6G(201, 6955),
136 CHAN6G(205, 6975),
137 CHAN6G(209, 6995),
138 CHAN6G(213, 7015),
139 CHAN6G(217, 7035),
140 CHAN6G(221, 7055),
141 CHAN6G(225, 7075),
142 CHAN6G(229, 7095),
143 CHAN6G(233, 7115),
144};
145
146static const struct ieee80211_tpt_blink mt76_tpt_blink[] = {
147 { .throughput = 0 * 1024, .blink_time = 334 },
148 { .throughput = 1 * 1024, .blink_time = 260 },
149 { .throughput = 5 * 1024, .blink_time = 220 },
150 { .throughput = 10 * 1024, .blink_time = 190 },
151 { .throughput = 20 * 1024, .blink_time = 170 },
152 { .throughput = 50 * 1024, .blink_time = 150 },
153 { .throughput = 70 * 1024, .blink_time = 130 },
154 { .throughput = 100 * 1024, .blink_time = 110 },
155 { .throughput = 200 * 1024, .blink_time = 80 },
156 { .throughput = 300 * 1024, .blink_time = 50 },
157};
158
159struct ieee80211_rate mt76_rates[] = {
160 CCK_RATE(0, 10),
161 CCK_RATE(1, 20),
162 CCK_RATE(2, 55),
163 CCK_RATE(3, 110),
164 OFDM_RATE(11, 60),
165 OFDM_RATE(15, 90),
166 OFDM_RATE(10, 120),
167 OFDM_RATE(14, 180),
168 OFDM_RATE(9, 240),
169 OFDM_RATE(13, 360),
170 OFDM_RATE(8, 480),
171 OFDM_RATE(12, 540),
172};
173EXPORT_SYMBOL_GPL(mt76_rates);
174
175static const struct cfg80211_sar_freq_ranges mt76_sar_freq_ranges[] = {
176 { .start_freq = 2402, .end_freq = 2494, },
177 { .start_freq = 5150, .end_freq = 5350, },
178 { .start_freq = 5350, .end_freq = 5470, },
179 { .start_freq = 5470, .end_freq = 5725, },
180 { .start_freq = 5725, .end_freq = 5950, },
181};
182
183static const struct cfg80211_sar_capa mt76_sar_capa = {
184 .type = NL80211_SAR_TYPE_POWER,
185 .num_freq_ranges = ARRAY_SIZE(mt76_sar_freq_ranges),
186 .freq_ranges = &mt76_sar_freq_ranges[0],
187};
188
189static int mt76_led_init(struct mt76_dev *dev)
190{
191 struct device_node *np = dev->dev->of_node;
192 struct ieee80211_hw *hw = dev->hw;
193 int led_pin;
194
195 if (!dev->led_cdev.brightness_set && !dev->led_cdev.blink_set)
196 return 0;
197
198 snprintf(dev->led_name, sizeof(dev->led_name),
199 "mt76-%s", wiphy_name(hw->wiphy));
200
201 dev->led_cdev.name = dev->led_name;
202 dev->led_cdev.default_trigger =
203 ieee80211_create_tpt_led_trigger(hw,
204 IEEE80211_TPT_LEDTRIG_FL_RADIO,
205 mt76_tpt_blink,
206 ARRAY_SIZE(mt76_tpt_blink));
207
208 np = of_get_child_by_name(np, "led");
209 if (np) {
210 if (!of_property_read_u32(np, "led-sources", &led_pin))
211 dev->led_pin = led_pin;
212 dev->led_al = of_property_read_bool(np, "led-active-low");
213 }
214
215 return led_classdev_register(dev->dev, &dev->led_cdev);
216}
217
218static void mt76_led_cleanup(struct mt76_dev *dev)
219{
220 if (!dev->led_cdev.brightness_set && !dev->led_cdev.blink_set)
221 return;
222
223 led_classdev_unregister(&dev->led_cdev);
224}
225
226static void mt76_init_stream_cap(struct mt76_phy *phy,
227 struct ieee80211_supported_band *sband,
228 bool vht)
229{
230 struct ieee80211_sta_ht_cap *ht_cap = &sband->ht_cap;
231 int i, nstream = hweight8(phy->antenna_mask);
232 struct ieee80211_sta_vht_cap *vht_cap;
233 u16 mcs_map = 0;
234
235 if (nstream > 1)
236 ht_cap->cap |= IEEE80211_HT_CAP_TX_STBC;
237 else
238 ht_cap->cap &= ~IEEE80211_HT_CAP_TX_STBC;
239
240 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
241 ht_cap->mcs.rx_mask[i] = i < nstream ? 0xff : 0;
242
243 if (!vht)
244 return;
245
246 vht_cap = &sband->vht_cap;
247 if (nstream > 1)
248 vht_cap->cap |= IEEE80211_VHT_CAP_TXSTBC;
249 else
250 vht_cap->cap &= ~IEEE80211_VHT_CAP_TXSTBC;
251
252 for (i = 0; i < 8; i++) {
253 if (i < nstream)
254 mcs_map |= (IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2));
255 else
256 mcs_map |=
257 (IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2));
258 }
259 vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
260 vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
261}
262
263void mt76_set_stream_caps(struct mt76_phy *phy, bool vht)
264{
265 if (phy->cap.has_2ghz)
266 mt76_init_stream_cap(phy, &phy->sband_2g.sband, false);
267 if (phy->cap.has_5ghz)
268 mt76_init_stream_cap(phy, &phy->sband_5g.sband, vht);
269 if (phy->cap.has_6ghz)
270 mt76_init_stream_cap(phy, &phy->sband_6g.sband, vht);
271}
272EXPORT_SYMBOL_GPL(mt76_set_stream_caps);
273
274static int
275mt76_init_sband(struct mt76_phy *phy, struct mt76_sband *msband,
276 const struct ieee80211_channel *chan, int n_chan,
277 struct ieee80211_rate *rates, int n_rates,
278 bool ht, bool vht)
279{
280 struct ieee80211_supported_band *sband = &msband->sband;
281 struct ieee80211_sta_vht_cap *vht_cap;
282 struct ieee80211_sta_ht_cap *ht_cap;
283 struct mt76_dev *dev = phy->dev;
284 void *chanlist;
285 int size;
286
287 size = n_chan * sizeof(*chan);
288 chanlist = devm_kmemdup(dev->dev, chan, size, GFP_KERNEL);
289 if (!chanlist)
290 return -ENOMEM;
291
292 msband->chan = devm_kcalloc(dev->dev, n_chan, sizeof(*msband->chan),
293 GFP_KERNEL);
294 if (!msband->chan)
295 return -ENOMEM;
296
297 sband->channels = chanlist;
298 sband->n_channels = n_chan;
299 sband->bitrates = rates;
300 sband->n_bitrates = n_rates;
301
302 if (!ht)
303 return 0;
304
305 ht_cap = &sband->ht_cap;
306 ht_cap->ht_supported = true;
307 ht_cap->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
308 IEEE80211_HT_CAP_GRN_FLD |
309 IEEE80211_HT_CAP_SGI_20 |
310 IEEE80211_HT_CAP_SGI_40 |
311 (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT);
312
313 ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
314 ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
315
316 mt76_init_stream_cap(phy, sband, vht);
317
318 if (!vht)
319 return 0;
320
321 vht_cap = &sband->vht_cap;
322 vht_cap->vht_supported = true;
323 vht_cap->cap |= IEEE80211_VHT_CAP_RXLDPC |
324 IEEE80211_VHT_CAP_RXSTBC_1 |
325 IEEE80211_VHT_CAP_SHORT_GI_80 |
326 IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN |
327 IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN |
328 (3 << IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT);
329
330 return 0;
331}
332
333static int
334mt76_init_sband_2g(struct mt76_phy *phy, struct ieee80211_rate *rates,
335 int n_rates)
336{
337 phy->hw->wiphy->bands[NL80211_BAND_2GHZ] = &phy->sband_2g.sband;
338
339 return mt76_init_sband(phy, &phy->sband_2g, mt76_channels_2ghz,
340 ARRAY_SIZE(mt76_channels_2ghz), rates,
341 n_rates, true, false);
342}
343
344static int
345mt76_init_sband_5g(struct mt76_phy *phy, struct ieee80211_rate *rates,
346 int n_rates, bool vht)
347{
348 phy->hw->wiphy->bands[NL80211_BAND_5GHZ] = &phy->sband_5g.sband;
349
350 return mt76_init_sband(phy, &phy->sband_5g, mt76_channels_5ghz,
351 ARRAY_SIZE(mt76_channels_5ghz), rates,
352 n_rates, true, vht);
353}
354
355static int
356mt76_init_sband_6g(struct mt76_phy *phy, struct ieee80211_rate *rates,
357 int n_rates)
358{
359 phy->hw->wiphy->bands[NL80211_BAND_6GHZ] = &phy->sband_6g.sband;
360
361 return mt76_init_sband(phy, &phy->sband_6g, mt76_channels_6ghz,
362 ARRAY_SIZE(mt76_channels_6ghz), rates,
363 n_rates, false, false);
364}
365
366static void
367mt76_check_sband(struct mt76_phy *phy, struct mt76_sband *msband,
368 enum nl80211_band band)
369{
370 struct ieee80211_supported_band *sband = &msband->sband;
371 bool found = false;
372 int i;
373
374 if (!sband)
375 return;
376
377 for (i = 0; i < sband->n_channels; i++) {
378 if (sband->channels[i].flags & IEEE80211_CHAN_DISABLED)
379 continue;
380
381 found = true;
382 break;
383 }
384
385 if (found) {
386 phy->chandef.chan = &sband->channels[0];
387 phy->chan_state = &msband->chan[0];
388 return;
389 }
390
391 sband->n_channels = 0;
392 phy->hw->wiphy->bands[band] = NULL;
393}
394
395static int
396mt76_phy_init(struct mt76_phy *phy, struct ieee80211_hw *hw)
397{
398 struct mt76_dev *dev = phy->dev;
399 struct wiphy *wiphy = hw->wiphy;
400
401 SET_IEEE80211_DEV(hw, dev->dev);
402 SET_IEEE80211_PERM_ADDR(hw, phy->macaddr);
403
404 wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR;
405 wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH |
406 WIPHY_FLAG_SUPPORTS_TDLS |
407 WIPHY_FLAG_AP_UAPSD;
408
409 wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
410 wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_AIRTIME_FAIRNESS);
411 wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_AQL);
412
413 wiphy->available_antennas_tx = phy->antenna_mask;
414 wiphy->available_antennas_rx = phy->antenna_mask;
415
416 wiphy->sar_capa = &mt76_sar_capa;
417 phy->frp = devm_kcalloc(dev->dev, wiphy->sar_capa->num_freq_ranges,
418 sizeof(struct mt76_freq_range_power),
419 GFP_KERNEL);
420 if (!phy->frp)
421 return -ENOMEM;
422
423 hw->txq_data_size = sizeof(struct mt76_txq);
424 hw->uapsd_max_sp_len = IEEE80211_WMM_IE_STA_QOSINFO_SP_ALL;
425
426 if (!hw->max_tx_fragments)
427 hw->max_tx_fragments = 16;
428
429 ieee80211_hw_set(hw, SIGNAL_DBM);
430 ieee80211_hw_set(hw, AMPDU_AGGREGATION);
431 ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
432 ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
433 ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
434 ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
435 ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
436 ieee80211_hw_set(hw, TX_AMSDU);
437 ieee80211_hw_set(hw, TX_FRAG_LIST);
438 ieee80211_hw_set(hw, MFP_CAPABLE);
439 ieee80211_hw_set(hw, AP_LINK_PS);
440 ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
441
442 return 0;
443}
444
445struct mt76_phy *
446mt76_alloc_phy(struct mt76_dev *dev, unsigned int size,
447 const struct ieee80211_ops *ops, u8 idx)
448{
449 struct ieee80211_hw *hw;
450 unsigned int phy_size;
451 struct mt76_phy *phy;
452
453 phy_size = ALIGN(sizeof(*phy), 8);
454 hw = ieee80211_alloc_hw(size + phy_size, ops);
455 if (!hw)
456 return NULL;
457
458 phy = hw->priv;
459 phy->dev = dev;
460 phy->hw = hw;
461 phy->priv = hw->priv + phy_size;
462 phy->band_idx = idx;
463
464 hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
465 hw->wiphy->interface_modes =
466 BIT(NL80211_IFTYPE_STATION) |
467 BIT(NL80211_IFTYPE_AP) |
468#ifdef CONFIG_MAC80211_MESH
469 BIT(NL80211_IFTYPE_MESH_POINT) |
470#endif
471 BIT(NL80211_IFTYPE_P2P_CLIENT) |
472 BIT(NL80211_IFTYPE_P2P_GO) |
473 BIT(NL80211_IFTYPE_ADHOC);
474
475 return phy;
476}
477EXPORT_SYMBOL_GPL(mt76_alloc_phy);
478
479int mt76_register_phy(struct mt76_phy *phy, bool vht,
480 struct ieee80211_rate *rates, int n_rates)
481{
482 int ret;
483
484 ret = mt76_phy_init(phy, phy->hw);
485 if (ret)
486 return ret;
487
488 if (phy->cap.has_2ghz) {
489 ret = mt76_init_sband_2g(phy, rates, n_rates);
490 if (ret)
491 return ret;
492 }
493
494 if (phy->cap.has_5ghz) {
495 ret = mt76_init_sband_5g(phy, rates + 4, n_rates - 4, vht);
496 if (ret)
497 return ret;
498 }
499
500 if (phy->cap.has_6ghz) {
501 ret = mt76_init_sband_6g(phy, rates + 4, n_rates - 4);
502 if (ret)
503 return ret;
504 }
505
506 wiphy_read_of_freq_limits(phy->hw->wiphy);
507 mt76_check_sband(phy, &phy->sband_2g, NL80211_BAND_2GHZ);
508 mt76_check_sband(phy, &phy->sband_5g, NL80211_BAND_5GHZ);
509 mt76_check_sband(phy, &phy->sband_6g, NL80211_BAND_6GHZ);
510
511 ret = ieee80211_register_hw(phy->hw);
512 if (ret)
513 return ret;
514
515 if (phy->band_idx == MT_BAND1)
516 phy->dev->phy2 = phy;
517 else
518 phy->dev->phy3 = phy;
519
520 return 0;
521}
522EXPORT_SYMBOL_GPL(mt76_register_phy);
523
524void mt76_unregister_phy(struct mt76_phy *phy)
525{
526 struct mt76_dev *dev = phy->dev;
527
528 mt76_tx_status_check(dev, true);
529 ieee80211_unregister_hw(phy->hw);
530 dev->phy2 = NULL;
531}
532EXPORT_SYMBOL_GPL(mt76_unregister_phy);
533
534struct mt76_dev *
535mt76_alloc_device(struct device *pdev, unsigned int size,
536 const struct ieee80211_ops *ops,
537 const struct mt76_driver_ops *drv_ops)
538{
539 struct ieee80211_hw *hw;
540 struct mt76_phy *phy;
541 struct mt76_dev *dev;
542 int i;
543
544 hw = ieee80211_alloc_hw(size, ops);
545 if (!hw)
546 return NULL;
547
548 dev = hw->priv;
549 dev->hw = hw;
550 dev->dev = pdev;
551 dev->drv = drv_ops;
552
553 phy = &dev->phy;
554 phy->dev = dev;
555 phy->hw = hw;
556 /* default set phy->band_idx to 0 */
557 phy->band_idx = MT_BAND0;
558
559 spin_lock_init(&dev->rx_lock);
560 spin_lock_init(&dev->lock);
561 spin_lock_init(&dev->cc_lock);
562 spin_lock_init(&dev->status_lock);
563 mutex_init(&dev->mutex);
564 init_waitqueue_head(&dev->tx_wait);
565
566 skb_queue_head_init(&dev->mcu.res_q);
567 init_waitqueue_head(&dev->mcu.wait);
568 mutex_init(&dev->mcu.mutex);
569 dev->tx_worker.fn = mt76_tx_worker;
570
571 hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
572 hw->wiphy->interface_modes =
573 BIT(NL80211_IFTYPE_STATION) |
574 BIT(NL80211_IFTYPE_AP) |
575#ifdef CONFIG_MAC80211_MESH
576 BIT(NL80211_IFTYPE_MESH_POINT) |
577#endif
578 BIT(NL80211_IFTYPE_P2P_CLIENT) |
579 BIT(NL80211_IFTYPE_P2P_GO) |
580 BIT(NL80211_IFTYPE_ADHOC);
581
582 spin_lock_init(&dev->token_lock);
583 idr_init(&dev->token);
584
585 INIT_LIST_HEAD(&dev->wcid_list);
586
587 INIT_LIST_HEAD(&dev->txwi_cache);
588
589 for (i = 0; i < ARRAY_SIZE(dev->q_rx); i++)
590 skb_queue_head_init(&dev->rx_skb[i]);
591
592 dev->wq = alloc_ordered_workqueue("mt76", 0);
593 if (!dev->wq) {
594 ieee80211_free_hw(hw);
595 return NULL;
596 }
597
598 return dev;
599}
600EXPORT_SYMBOL_GPL(mt76_alloc_device);
601
602int mt76_register_device(struct mt76_dev *dev, bool vht,
603 struct ieee80211_rate *rates, int n_rates)
604{
605 struct ieee80211_hw *hw = dev->hw;
606 struct mt76_phy *phy = &dev->phy;
607 int ret;
608
609 dev_set_drvdata(dev->dev, dev);
610 ret = mt76_phy_init(phy, hw);
611 if (ret)
612 return ret;
613
614 if (phy->cap.has_2ghz) {
615 ret = mt76_init_sband_2g(phy, rates, n_rates);
616 if (ret)
617 return ret;
618 }
619
620 if (phy->cap.has_5ghz) {
621 ret = mt76_init_sband_5g(phy, rates + 4, n_rates - 4, vht);
622 if (ret)
623 return ret;
624 }
625
626 if (phy->cap.has_6ghz) {
627 ret = mt76_init_sband_6g(phy, rates + 4, n_rates - 4);
628 if (ret)
629 return ret;
630 }
631
632 wiphy_read_of_freq_limits(hw->wiphy);
633 mt76_check_sband(&dev->phy, &phy->sband_2g, NL80211_BAND_2GHZ);
634 mt76_check_sband(&dev->phy, &phy->sband_5g, NL80211_BAND_5GHZ);
635 mt76_check_sband(&dev->phy, &phy->sband_6g, NL80211_BAND_6GHZ);
636
637 if (IS_ENABLED(CONFIG_MT76_LEDS)) {
638 ret = mt76_led_init(dev);
639 if (ret)
640 return ret;
641 }
642
643 ret = ieee80211_register_hw(hw);
644 if (ret)
645 return ret;
646
647 WARN_ON(mt76_worker_setup(hw, &dev->tx_worker, NULL, "tx"));
648 sched_set_fifo_low(dev->tx_worker.task);
649
650 return 0;
651}
652EXPORT_SYMBOL_GPL(mt76_register_device);
653
654void mt76_unregister_device(struct mt76_dev *dev)
655{
656 struct ieee80211_hw *hw = dev->hw;
657
658 if (IS_ENABLED(CONFIG_MT76_LEDS))
659 mt76_led_cleanup(dev);
660 mt76_tx_status_check(dev, true);
661 ieee80211_unregister_hw(hw);
662}
663EXPORT_SYMBOL_GPL(mt76_unregister_device);
664
665void mt76_free_device(struct mt76_dev *dev)
666{
667 mt76_worker_teardown(&dev->tx_worker);
668 if (dev->wq) {
669 destroy_workqueue(dev->wq);
670 dev->wq = NULL;
671 }
672 ieee80211_free_hw(dev->hw);
673}
674EXPORT_SYMBOL_GPL(mt76_free_device);
675
676static void mt76_rx_release_amsdu(struct mt76_phy *phy, enum mt76_rxq_id q)
677{
678 struct sk_buff *skb = phy->rx_amsdu[q].head;
679 struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
680 struct mt76_dev *dev = phy->dev;
681
682 phy->rx_amsdu[q].head = NULL;
683 phy->rx_amsdu[q].tail = NULL;
684
685 /*
686 * Validate if the amsdu has a proper first subframe.
687 * A single MSDU can be parsed as A-MSDU when the unauthenticated A-MSDU
688 * flag of the QoS header gets flipped. In such cases, the first
689 * subframe has a LLC/SNAP header in the location of the destination
690 * address.
691 */
692 if (skb_shinfo(skb)->frag_list) {
693 int offset = 0;
694
695 if (!(status->flag & RX_FLAG_8023)) {
696 offset = ieee80211_get_hdrlen_from_skb(skb);
697
698 if ((status->flag &
699 (RX_FLAG_DECRYPTED | RX_FLAG_IV_STRIPPED)) ==
700 RX_FLAG_DECRYPTED)
701 offset += 8;
702 }
703
704 if (ether_addr_equal(skb->data + offset, rfc1042_header)) {
705 dev_kfree_skb(skb);
706 return;
707 }
708 }
709 __skb_queue_tail(&dev->rx_skb[q], skb);
710}
711
712static void mt76_rx_release_burst(struct mt76_phy *phy, enum mt76_rxq_id q,
713 struct sk_buff *skb)
714{
715 struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
716
717 if (phy->rx_amsdu[q].head &&
718 (!status->amsdu || status->first_amsdu ||
719 status->seqno != phy->rx_amsdu[q].seqno))
720 mt76_rx_release_amsdu(phy, q);
721
722 if (!phy->rx_amsdu[q].head) {
723 phy->rx_amsdu[q].tail = &skb_shinfo(skb)->frag_list;
724 phy->rx_amsdu[q].seqno = status->seqno;
725 phy->rx_amsdu[q].head = skb;
726 } else {
727 *phy->rx_amsdu[q].tail = skb;
728 phy->rx_amsdu[q].tail = &skb->next;
729 }
730
731 if (!status->amsdu || status->last_amsdu)
732 mt76_rx_release_amsdu(phy, q);
733}
734
735void mt76_rx(struct mt76_dev *dev, enum mt76_rxq_id q, struct sk_buff *skb)
736{
737 struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
738 struct mt76_phy *phy = mt76_dev_phy(dev, status->phy_idx);
739
740 if (!test_bit(MT76_STATE_RUNNING, &phy->state)) {
741 dev_kfree_skb(skb);
742 return;
743 }
744
745#ifdef CONFIG_NL80211_TESTMODE
746 if (phy->test.state == MT76_TM_STATE_RX_FRAMES) {
747 phy->test.rx_stats.packets[q]++;
748 if (status->flag & RX_FLAG_FAILED_FCS_CRC)
749 phy->test.rx_stats.fcs_error[q]++;
750 }
751#endif
752
753 mt76_rx_release_burst(phy, q, skb);
754}
755EXPORT_SYMBOL_GPL(mt76_rx);
756
757bool mt76_has_tx_pending(struct mt76_phy *phy)
758{
759 struct mt76_queue *q;
760 int i;
761
762 for (i = 0; i < __MT_TXQ_MAX; i++) {
763 q = phy->q_tx[i];
764 if (q && q->queued)
765 return true;
766 }
767
768 return false;
769}
770EXPORT_SYMBOL_GPL(mt76_has_tx_pending);
771
772static struct mt76_channel_state *
773mt76_channel_state(struct mt76_phy *phy, struct ieee80211_channel *c)
774{
775 struct mt76_sband *msband;
776 int idx;
777
778 if (c->band == NL80211_BAND_2GHZ)
779 msband = &phy->sband_2g;
780 else if (c->band == NL80211_BAND_6GHZ)
781 msband = &phy->sband_6g;
782 else
783 msband = &phy->sband_5g;
784
785 idx = c - &msband->sband.channels[0];
786 return &msband->chan[idx];
787}
788
789void mt76_update_survey_active_time(struct mt76_phy *phy, ktime_t time)
790{
791 struct mt76_channel_state *state = phy->chan_state;
792
793 state->cc_active += ktime_to_us(ktime_sub(time,
794 phy->survey_time));
795 phy->survey_time = time;
796}
797EXPORT_SYMBOL_GPL(mt76_update_survey_active_time);
798
799void mt76_update_survey(struct mt76_phy *phy)
800{
801 struct mt76_dev *dev = phy->dev;
802 ktime_t cur_time;
803
804 if (dev->drv->update_survey)
805 dev->drv->update_survey(phy);
806
807 cur_time = ktime_get_boottime();
808 mt76_update_survey_active_time(phy, cur_time);
809
810 if (dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME) {
811 struct mt76_channel_state *state = phy->chan_state;
812
813 spin_lock_bh(&dev->cc_lock);
814 state->cc_bss_rx += dev->cur_cc_bss_rx;
815 dev->cur_cc_bss_rx = 0;
816 spin_unlock_bh(&dev->cc_lock);
817 }
818}
819EXPORT_SYMBOL_GPL(mt76_update_survey);
820
821void mt76_set_channel(struct mt76_phy *phy)
822{
823 struct mt76_dev *dev = phy->dev;
824 struct ieee80211_hw *hw = phy->hw;
825 struct cfg80211_chan_def *chandef = &hw->conf.chandef;
826 bool offchannel = hw->conf.flags & IEEE80211_CONF_OFFCHANNEL;
827 int timeout = HZ / 5;
828
829 wait_event_timeout(dev->tx_wait, !mt76_has_tx_pending(phy), timeout);
830 mt76_update_survey(phy);
831
832 if (phy->chandef.chan->center_freq != chandef->chan->center_freq ||
833 phy->chandef.width != chandef->width)
834 phy->dfs_state = MT_DFS_STATE_UNKNOWN;
835
836 phy->chandef = *chandef;
837 phy->chan_state = mt76_channel_state(phy, chandef->chan);
838
839 if (!offchannel)
840 phy->main_chan = chandef->chan;
841
842 if (chandef->chan != phy->main_chan)
843 memset(phy->chan_state, 0, sizeof(*phy->chan_state));
844}
845EXPORT_SYMBOL_GPL(mt76_set_channel);
846
847int mt76_get_survey(struct ieee80211_hw *hw, int idx,
848 struct survey_info *survey)
849{
850 struct mt76_phy *phy = hw->priv;
851 struct mt76_dev *dev = phy->dev;
852 struct mt76_sband *sband;
853 struct ieee80211_channel *chan;
854 struct mt76_channel_state *state;
855 int ret = 0;
856
857 mutex_lock(&dev->mutex);
858 if (idx == 0 && dev->drv->update_survey)
859 mt76_update_survey(phy);
860
861 if (idx >= phy->sband_2g.sband.n_channels +
862 phy->sband_5g.sband.n_channels) {
863 idx -= (phy->sband_2g.sband.n_channels +
864 phy->sband_5g.sband.n_channels);
865 sband = &phy->sband_6g;
866 } else if (idx >= phy->sband_2g.sband.n_channels) {
867 idx -= phy->sband_2g.sband.n_channels;
868 sband = &phy->sband_5g;
869 } else {
870 sband = &phy->sband_2g;
871 }
872
873 if (idx >= sband->sband.n_channels) {
874 ret = -ENOENT;
875 goto out;
876 }
877
878 chan = &sband->sband.channels[idx];
879 state = mt76_channel_state(phy, chan);
880
881 memset(survey, 0, sizeof(*survey));
882 survey->channel = chan;
883 survey->filled = SURVEY_INFO_TIME | SURVEY_INFO_TIME_BUSY;
884 survey->filled |= dev->drv->survey_flags;
885 if (state->noise)
886 survey->filled |= SURVEY_INFO_NOISE_DBM;
887
888 if (chan == phy->main_chan) {
889 survey->filled |= SURVEY_INFO_IN_USE;
890
891 if (dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME)
892 survey->filled |= SURVEY_INFO_TIME_BSS_RX;
893 }
894
895 survey->time_busy = div_u64(state->cc_busy, 1000);
896 survey->time_rx = div_u64(state->cc_rx, 1000);
897 survey->time = div_u64(state->cc_active, 1000);
898 survey->noise = state->noise;
899
900 spin_lock_bh(&dev->cc_lock);
901 survey->time_bss_rx = div_u64(state->cc_bss_rx, 1000);
902 survey->time_tx = div_u64(state->cc_tx, 1000);
903 spin_unlock_bh(&dev->cc_lock);
904
905out:
906 mutex_unlock(&dev->mutex);
907
908 return ret;
909}
910EXPORT_SYMBOL_GPL(mt76_get_survey);
911
912void mt76_wcid_key_setup(struct mt76_dev *dev, struct mt76_wcid *wcid,
913 struct ieee80211_key_conf *key)
914{
915 struct ieee80211_key_seq seq;
916 int i;
917
918 wcid->rx_check_pn = false;
919
920 if (!key)
921 return;
922
923 if (key->cipher != WLAN_CIPHER_SUITE_CCMP)
924 return;
925
926 wcid->rx_check_pn = true;
927
928 /* data frame */
929 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
930 ieee80211_get_key_rx_seq(key, i, &seq);
931 memcpy(wcid->rx_key_pn[i], seq.ccmp.pn, sizeof(seq.ccmp.pn));
932 }
933
934 /* robust management frame */
935 ieee80211_get_key_rx_seq(key, -1, &seq);
936 memcpy(wcid->rx_key_pn[i], seq.ccmp.pn, sizeof(seq.ccmp.pn));
937
938}
939EXPORT_SYMBOL(mt76_wcid_key_setup);
940
941static int
942mt76_rx_signal(struct mt76_rx_status *status)
943{
944 s8 *chain_signal = status->chain_signal;
945 int signal = -128;
946 u8 chains;
947
948 for (chains = status->chains; chains; chains >>= 1, chain_signal++) {
949 int cur, diff;
950
951 cur = *chain_signal;
952 if (!(chains & BIT(0)) ||
953 cur > 0)
954 continue;
955
956 if (cur > signal)
957 swap(cur, signal);
958
959 diff = signal - cur;
960 if (diff == 0)
961 signal += 3;
962 else if (diff <= 2)
963 signal += 2;
964 else if (diff <= 6)
965 signal += 1;
966 }
967
968 return signal;
969}
970
971static void
972mt76_rx_convert(struct mt76_dev *dev, struct sk_buff *skb,
973 struct ieee80211_hw **hw,
974 struct ieee80211_sta **sta)
975{
976 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
977 struct ieee80211_hdr *hdr = mt76_skb_get_hdr(skb);
978 struct mt76_rx_status mstat;
979
980 mstat = *((struct mt76_rx_status *)skb->cb);
981 memset(status, 0, sizeof(*status));
982
983 status->flag = mstat.flag;
984 status->freq = mstat.freq;
985 status->enc_flags = mstat.enc_flags;
986 status->encoding = mstat.encoding;
987 status->bw = mstat.bw;
988 status->he_ru = mstat.he_ru;
989 status->he_gi = mstat.he_gi;
990 status->he_dcm = mstat.he_dcm;
991 status->rate_idx = mstat.rate_idx;
992 status->nss = mstat.nss;
993 status->band = mstat.band;
994 status->signal = mstat.signal;
995 status->chains = mstat.chains;
996 status->ampdu_reference = mstat.ampdu_ref;
997 status->device_timestamp = mstat.timestamp;
998 status->mactime = mstat.timestamp;
999 status->signal = mt76_rx_signal(&mstat);
1000 if (status->signal <= -128)
1001 status->flag |= RX_FLAG_NO_SIGNAL_VAL;
1002
1003 if (ieee80211_is_beacon(hdr->frame_control) ||
1004 ieee80211_is_probe_resp(hdr->frame_control))
1005 status->boottime_ns = ktime_get_boottime_ns();
1006
1007 BUILD_BUG_ON(sizeof(mstat) > sizeof(skb->cb));
1008 BUILD_BUG_ON(sizeof(status->chain_signal) !=
1009 sizeof(mstat.chain_signal));
1010 memcpy(status->chain_signal, mstat.chain_signal,
1011 sizeof(mstat.chain_signal));
1012
1013 *sta = wcid_to_sta(mstat.wcid);
1014 *hw = mt76_phy_hw(dev, mstat.phy_idx);
1015}
1016
1017static int
1018mt76_check_ccmp_pn(struct sk_buff *skb)
1019{
1020 struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
1021 struct mt76_wcid *wcid = status->wcid;
1022 struct ieee80211_hdr *hdr;
1023 int security_idx;
1024 int ret;
1025
1026 if (!(status->flag & RX_FLAG_DECRYPTED))
1027 return 0;
1028
1029 if (!wcid || !wcid->rx_check_pn)
1030 return 0;
1031
1032 security_idx = status->qos_ctl & IEEE80211_QOS_CTL_TID_MASK;
1033 if (status->flag & RX_FLAG_8023)
1034 goto skip_hdr_check;
1035
1036 hdr = mt76_skb_get_hdr(skb);
1037 if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
1038 /*
1039 * Validate the first fragment both here and in mac80211
1040 * All further fragments will be validated by mac80211 only.
1041 */
1042 if (ieee80211_is_frag(hdr) &&
1043 !ieee80211_is_first_frag(hdr->frame_control))
1044 return 0;
1045 }
1046
1047 /* IEEE 802.11-2020, 12.5.3.4.4 "PN and replay detection" c):
1048 *
1049 * the recipient shall maintain a single replay counter for received
1050 * individually addressed robust Management frames that are received
1051 * with the To DS subfield equal to 0, [...]
1052 */
1053 if (ieee80211_is_mgmt(hdr->frame_control) &&
1054 !ieee80211_has_tods(hdr->frame_control))
1055 security_idx = IEEE80211_NUM_TIDS;
1056
1057skip_hdr_check:
1058 BUILD_BUG_ON(sizeof(status->iv) != sizeof(wcid->rx_key_pn[0]));
1059 ret = memcmp(status->iv, wcid->rx_key_pn[security_idx],
1060 sizeof(status->iv));
1061 if (ret <= 0)
1062 return -EINVAL; /* replay */
1063
1064 memcpy(wcid->rx_key_pn[security_idx], status->iv, sizeof(status->iv));
1065
1066 if (status->flag & RX_FLAG_IV_STRIPPED)
1067 status->flag |= RX_FLAG_PN_VALIDATED;
1068
1069 return 0;
1070}
1071
1072static void
1073mt76_airtime_report(struct mt76_dev *dev, struct mt76_rx_status *status,
1074 int len)
1075{
1076 struct mt76_wcid *wcid = status->wcid;
1077 struct ieee80211_rx_status info = {
1078 .enc_flags = status->enc_flags,
1079 .rate_idx = status->rate_idx,
1080 .encoding = status->encoding,
1081 .band = status->band,
1082 .nss = status->nss,
1083 .bw = status->bw,
1084 };
1085 struct ieee80211_sta *sta;
1086 u32 airtime;
1087 u8 tidno = status->qos_ctl & IEEE80211_QOS_CTL_TID_MASK;
1088
1089 airtime = ieee80211_calc_rx_airtime(dev->hw, &info, len);
1090 spin_lock(&dev->cc_lock);
1091 dev->cur_cc_bss_rx += airtime;
1092 spin_unlock(&dev->cc_lock);
1093
1094 if (!wcid || !wcid->sta)
1095 return;
1096
1097 sta = container_of((void *)wcid, struct ieee80211_sta, drv_priv);
1098 ieee80211_sta_register_airtime(sta, tidno, 0, airtime);
1099}
1100
1101static void
1102mt76_airtime_flush_ampdu(struct mt76_dev *dev)
1103{
1104 struct mt76_wcid *wcid;
1105 int wcid_idx;
1106
1107 if (!dev->rx_ampdu_len)
1108 return;
1109
1110 wcid_idx = dev->rx_ampdu_status.wcid_idx;
1111 if (wcid_idx < ARRAY_SIZE(dev->wcid))
1112 wcid = rcu_dereference(dev->wcid[wcid_idx]);
1113 else
1114 wcid = NULL;
1115 dev->rx_ampdu_status.wcid = wcid;
1116
1117 mt76_airtime_report(dev, &dev->rx_ampdu_status, dev->rx_ampdu_len);
1118
1119 dev->rx_ampdu_len = 0;
1120 dev->rx_ampdu_ref = 0;
1121}
1122
1123static void
1124mt76_airtime_check(struct mt76_dev *dev, struct sk_buff *skb)
1125{
1126 struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
1127 struct mt76_wcid *wcid = status->wcid;
1128
1129 if (!(dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME))
1130 return;
1131
1132 if (!wcid || !wcid->sta) {
1133 struct ieee80211_hdr *hdr = mt76_skb_get_hdr(skb);
1134
1135 if (status->flag & RX_FLAG_8023)
1136 return;
1137
1138 if (!ether_addr_equal(hdr->addr1, dev->phy.macaddr))
1139 return;
1140
1141 wcid = NULL;
1142 }
1143
1144 if (!(status->flag & RX_FLAG_AMPDU_DETAILS) ||
1145 status->ampdu_ref != dev->rx_ampdu_ref)
1146 mt76_airtime_flush_ampdu(dev);
1147
1148 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
1149 if (!dev->rx_ampdu_len ||
1150 status->ampdu_ref != dev->rx_ampdu_ref) {
1151 dev->rx_ampdu_status = *status;
1152 dev->rx_ampdu_status.wcid_idx = wcid ? wcid->idx : 0xff;
1153 dev->rx_ampdu_ref = status->ampdu_ref;
1154 }
1155
1156 dev->rx_ampdu_len += skb->len;
1157 return;
1158 }
1159
1160 mt76_airtime_report(dev, status, skb->len);
1161}
1162
1163static void
1164mt76_check_sta(struct mt76_dev *dev, struct sk_buff *skb)
1165{
1166 struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
1167 struct ieee80211_hdr *hdr = mt76_skb_get_hdr(skb);
1168 struct ieee80211_sta *sta;
1169 struct ieee80211_hw *hw;
1170 struct mt76_wcid *wcid = status->wcid;
1171 u8 tidno = status->qos_ctl & IEEE80211_QOS_CTL_TID_MASK;
1172 bool ps;
1173
1174 hw = mt76_phy_hw(dev, status->phy_idx);
1175 if (ieee80211_is_pspoll(hdr->frame_control) && !wcid &&
1176 !(status->flag & RX_FLAG_8023)) {
1177 sta = ieee80211_find_sta_by_ifaddr(hw, hdr->addr2, NULL);
1178 if (sta)
1179 wcid = status->wcid = (struct mt76_wcid *)sta->drv_priv;
1180 }
1181
1182 mt76_airtime_check(dev, skb);
1183
1184 if (!wcid || !wcid->sta)
1185 return;
1186
1187 sta = container_of((void *)wcid, struct ieee80211_sta, drv_priv);
1188
1189 if (status->signal <= 0)
1190 ewma_signal_add(&wcid->rssi, -status->signal);
1191
1192 wcid->inactive_count = 0;
1193
1194 if (status->flag & RX_FLAG_8023)
1195 return;
1196
1197 if (!test_bit(MT_WCID_FLAG_CHECK_PS, &wcid->flags))
1198 return;
1199
1200 if (ieee80211_is_pspoll(hdr->frame_control)) {
1201 ieee80211_sta_pspoll(sta);
1202 return;
1203 }
1204
1205 if (ieee80211_has_morefrags(hdr->frame_control) ||
1206 !(ieee80211_is_mgmt(hdr->frame_control) ||
1207 ieee80211_is_data(hdr->frame_control)))
1208 return;
1209
1210 ps = ieee80211_has_pm(hdr->frame_control);
1211
1212 if (ps && (ieee80211_is_data_qos(hdr->frame_control) ||
1213 ieee80211_is_qos_nullfunc(hdr->frame_control)))
1214 ieee80211_sta_uapsd_trigger(sta, tidno);
1215
1216 if (!!test_bit(MT_WCID_FLAG_PS, &wcid->flags) == ps)
1217 return;
1218
1219 if (ps)
1220 set_bit(MT_WCID_FLAG_PS, &wcid->flags);
1221
1222 dev->drv->sta_ps(dev, sta, ps);
1223
1224 if (!ps)
1225 clear_bit(MT_WCID_FLAG_PS, &wcid->flags);
1226
1227 ieee80211_sta_ps_transition(sta, ps);
1228}
1229
1230void mt76_rx_complete(struct mt76_dev *dev, struct sk_buff_head *frames,
1231 struct napi_struct *napi)
1232{
1233 struct ieee80211_sta *sta;
1234 struct ieee80211_hw *hw;
1235 struct sk_buff *skb, *tmp;
1236 LIST_HEAD(list);
1237
1238 spin_lock(&dev->rx_lock);
1239 while ((skb = __skb_dequeue(frames)) != NULL) {
1240 struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
1241
1242 if (mt76_check_ccmp_pn(skb)) {
1243 dev_kfree_skb(skb);
1244 continue;
1245 }
1246
1247 skb_shinfo(skb)->frag_list = NULL;
1248 mt76_rx_convert(dev, skb, &hw, &sta);
1249 ieee80211_rx_list(hw, sta, skb, &list);
1250
1251 /* subsequent amsdu frames */
1252 while (nskb) {
1253 skb = nskb;
1254 nskb = nskb->next;
1255 skb->next = NULL;
1256
1257 mt76_rx_convert(dev, skb, &hw, &sta);
1258 ieee80211_rx_list(hw, sta, skb, &list);
1259 }
1260 }
1261 spin_unlock(&dev->rx_lock);
1262
1263 if (!napi) {
1264 netif_receive_skb_list(&list);
1265 return;
1266 }
1267
1268 list_for_each_entry_safe(skb, tmp, &list, list) {
1269 skb_list_del_init(skb);
1270 napi_gro_receive(napi, skb);
1271 }
1272}
1273
1274void mt76_rx_poll_complete(struct mt76_dev *dev, enum mt76_rxq_id q,
1275 struct napi_struct *napi)
1276{
1277 struct sk_buff_head frames;
1278 struct sk_buff *skb;
1279
1280 __skb_queue_head_init(&frames);
1281
1282 while ((skb = __skb_dequeue(&dev->rx_skb[q])) != NULL) {
1283 mt76_check_sta(dev, skb);
1284 mt76_rx_aggr_reorder(skb, &frames);
1285 }
1286
1287 mt76_rx_complete(dev, &frames, napi);
1288}
1289EXPORT_SYMBOL_GPL(mt76_rx_poll_complete);
1290
1291static int
1292mt76_sta_add(struct mt76_dev *dev, struct ieee80211_vif *vif,
1293 struct ieee80211_sta *sta, u8 phy_idx)
1294{
1295 struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
1296 int ret;
1297 int i;
1298
1299 mutex_lock(&dev->mutex);
1300
1301 ret = dev->drv->sta_add(dev, vif, sta);
1302 if (ret)
1303 goto out;
1304
1305 for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1306 struct mt76_txq *mtxq;
1307
1308 if (!sta->txq[i])
1309 continue;
1310
1311 mtxq = (struct mt76_txq *)sta->txq[i]->drv_priv;
1312 mtxq->wcid = wcid->idx;
1313 }
1314
1315 ewma_signal_init(&wcid->rssi);
1316 if (phy_idx == MT_EXT_PHY)
1317 mt76_wcid_mask_set(dev->wcid_phy_mask, wcid->idx);
1318 if (phy_idx == MT_TRI_PHY)
1319 mt76_wcid_mask_set(dev->wcid_phy3_mask, wcid->idx);
1320
1321 wcid->phy_idx = phy_idx;
1322 rcu_assign_pointer(dev->wcid[wcid->idx], wcid);
1323
1324 mt76_packet_id_init(wcid);
1325out:
1326 mutex_unlock(&dev->mutex);
1327
1328 return ret;
1329}
1330
1331void __mt76_sta_remove(struct mt76_dev *dev, struct ieee80211_vif *vif,
1332 struct ieee80211_sta *sta)
1333{
1334 struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
1335 int i, idx = wcid->idx;
1336
1337 for (i = 0; i < ARRAY_SIZE(wcid->aggr); i++)
1338 mt76_rx_aggr_stop(dev, wcid, i);
1339
1340 if (dev->drv->sta_remove)
1341 dev->drv->sta_remove(dev, vif, sta);
1342
1343 mt76_packet_id_flush(dev, wcid);
1344
1345 mt76_wcid_mask_clear(dev->wcid_mask, idx);
1346 mt76_wcid_mask_clear(dev->wcid_phy_mask, idx);
1347 mt76_wcid_mask_clear(dev->wcid_phy3_mask, idx);
1348}
1349EXPORT_SYMBOL_GPL(__mt76_sta_remove);
1350
1351static void
1352mt76_sta_remove(struct mt76_dev *dev, struct ieee80211_vif *vif,
1353 struct ieee80211_sta *sta)
1354{
1355 mutex_lock(&dev->mutex);
1356 __mt76_sta_remove(dev, vif, sta);
1357 mutex_unlock(&dev->mutex);
1358}
1359
1360int mt76_sta_state(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1361 struct ieee80211_sta *sta,
1362 enum ieee80211_sta_state old_state,
1363 enum ieee80211_sta_state new_state)
1364{
1365 struct mt76_phy *phy = hw->priv;
1366 struct mt76_dev *dev = phy->dev;
1367 u8 phy_idx = mt76_get_phy_id(phy);
1368
1369 if (old_state == IEEE80211_STA_NOTEXIST &&
1370 new_state == IEEE80211_STA_NONE)
1371 return mt76_sta_add(dev, vif, sta, phy_idx);
1372
1373 if (old_state == IEEE80211_STA_AUTH &&
1374 new_state == IEEE80211_STA_ASSOC &&
1375 dev->drv->sta_assoc)
1376 dev->drv->sta_assoc(dev, vif, sta);
1377
1378 if (old_state == IEEE80211_STA_NONE &&
1379 new_state == IEEE80211_STA_NOTEXIST)
1380 mt76_sta_remove(dev, vif, sta);
1381
1382 return 0;
1383}
1384EXPORT_SYMBOL_GPL(mt76_sta_state);
1385
1386void mt76_sta_pre_rcu_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1387 struct ieee80211_sta *sta)
1388{
1389 struct mt76_phy *phy = hw->priv;
1390 struct mt76_dev *dev = phy->dev;
1391 struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
1392
1393 mutex_lock(&dev->mutex);
1394 rcu_assign_pointer(dev->wcid[wcid->idx], NULL);
1395 mutex_unlock(&dev->mutex);
1396}
1397EXPORT_SYMBOL_GPL(mt76_sta_pre_rcu_remove);
1398
1399int mt76_get_txpower(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1400 int *dbm)
1401{
1402 struct mt76_phy *phy = hw->priv;
1403 int n_chains = hweight8(phy->antenna_mask);
1404 int delta = mt76_tx_power_nss_delta(n_chains);
1405
1406 *dbm = DIV_ROUND_UP(phy->txpower_cur + delta, 2);
1407
1408 return 0;
1409}
1410EXPORT_SYMBOL_GPL(mt76_get_txpower);
1411
1412int mt76_init_sar_power(struct ieee80211_hw *hw,
1413 const struct cfg80211_sar_specs *sar)
1414{
1415 struct mt76_phy *phy = hw->priv;
1416 const struct cfg80211_sar_capa *capa = hw->wiphy->sar_capa;
1417 int i;
1418
1419 if (sar->type != NL80211_SAR_TYPE_POWER || !sar->num_sub_specs)
1420 return -EINVAL;
1421
1422 for (i = 0; i < sar->num_sub_specs; i++) {
1423 u32 index = sar->sub_specs[i].freq_range_index;
1424 /* SAR specifies power limitaton in 0.25dbm */
1425 s32 power = sar->sub_specs[i].power >> 1;
1426
1427 if (power > 127 || power < -127)
1428 power = 127;
1429
1430 phy->frp[index].range = &capa->freq_ranges[index];
1431 phy->frp[index].power = power;
1432 }
1433
1434 return 0;
1435}
1436EXPORT_SYMBOL_GPL(mt76_init_sar_power);
1437
1438int mt76_get_sar_power(struct mt76_phy *phy,
1439 struct ieee80211_channel *chan,
1440 int power)
1441{
1442 const struct cfg80211_sar_capa *capa = phy->hw->wiphy->sar_capa;
1443 int freq, i;
1444
1445 if (!capa || !phy->frp)
1446 return power;
1447
1448 if (power > 127 || power < -127)
1449 power = 127;
1450
1451 freq = ieee80211_channel_to_frequency(chan->hw_value, chan->band);
1452 for (i = 0 ; i < capa->num_freq_ranges; i++) {
1453 if (phy->frp[i].range &&
1454 freq >= phy->frp[i].range->start_freq &&
1455 freq < phy->frp[i].range->end_freq) {
1456 power = min_t(int, phy->frp[i].power, power);
1457 break;
1458 }
1459 }
1460
1461 return power;
1462}
1463EXPORT_SYMBOL_GPL(mt76_get_sar_power);
1464
1465static void
1466__mt76_csa_finish(void *priv, u8 *mac, struct ieee80211_vif *vif)
1467{
1468 if (vif->csa_active && ieee80211_beacon_cntdwn_is_complete(vif))
1469 ieee80211_csa_finish(vif);
1470}
1471
1472void mt76_csa_finish(struct mt76_dev *dev)
1473{
1474 if (!dev->csa_complete)
1475 return;
1476
1477 ieee80211_iterate_active_interfaces_atomic(dev->hw,
1478 IEEE80211_IFACE_ITER_RESUME_ALL,
1479 __mt76_csa_finish, dev);
1480
1481 dev->csa_complete = 0;
1482}
1483EXPORT_SYMBOL_GPL(mt76_csa_finish);
1484
1485static void
1486__mt76_csa_check(void *priv, u8 *mac, struct ieee80211_vif *vif)
1487{
1488 struct mt76_dev *dev = priv;
1489
1490 if (!vif->csa_active)
1491 return;
1492
1493 dev->csa_complete |= ieee80211_beacon_cntdwn_is_complete(vif);
1494}
1495
1496void mt76_csa_check(struct mt76_dev *dev)
1497{
1498 ieee80211_iterate_active_interfaces_atomic(dev->hw,
1499 IEEE80211_IFACE_ITER_RESUME_ALL,
1500 __mt76_csa_check, dev);
1501}
1502EXPORT_SYMBOL_GPL(mt76_csa_check);
1503
1504int
1505mt76_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta, bool set)
1506{
1507 return 0;
1508}
1509EXPORT_SYMBOL_GPL(mt76_set_tim);
1510
1511void mt76_insert_ccmp_hdr(struct sk_buff *skb, u8 key_id)
1512{
1513 struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
1514 int hdr_len = ieee80211_get_hdrlen_from_skb(skb);
1515 u8 *hdr, *pn = status->iv;
1516
1517 __skb_push(skb, 8);
1518 memmove(skb->data, skb->data + 8, hdr_len);
1519 hdr = skb->data + hdr_len;
1520
1521 hdr[0] = pn[5];
1522 hdr[1] = pn[4];
1523 hdr[2] = 0;
1524 hdr[3] = 0x20 | (key_id << 6);
1525 hdr[4] = pn[3];
1526 hdr[5] = pn[2];
1527 hdr[6] = pn[1];
1528 hdr[7] = pn[0];
1529
1530 status->flag &= ~RX_FLAG_IV_STRIPPED;
1531}
1532EXPORT_SYMBOL_GPL(mt76_insert_ccmp_hdr);
1533
1534int mt76_get_rate(struct mt76_dev *dev,
1535 struct ieee80211_supported_band *sband,
1536 int idx, bool cck)
1537{
1538 int i, offset = 0, len = sband->n_bitrates;
1539
1540 if (cck) {
1541 if (sband != &dev->phy.sband_2g.sband)
1542 return 0;
1543
1544 idx &= ~BIT(2); /* short preamble */
1545 } else if (sband == &dev->phy.sband_2g.sband) {
1546 offset = 4;
1547 }
1548
1549 for (i = offset; i < len; i++) {
1550 if ((sband->bitrates[i].hw_value & GENMASK(7, 0)) == idx)
1551 return i;
1552 }
1553
1554 return 0;
1555}
1556EXPORT_SYMBOL_GPL(mt76_get_rate);
1557
1558void mt76_sw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1559 const u8 *mac)
1560{
1561 struct mt76_phy *phy = hw->priv;
1562
1563 set_bit(MT76_SCANNING, &phy->state);
1564}
1565EXPORT_SYMBOL_GPL(mt76_sw_scan);
1566
1567void mt76_sw_scan_complete(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1568{
1569 struct mt76_phy *phy = hw->priv;
1570
1571 clear_bit(MT76_SCANNING, &phy->state);
1572}
1573EXPORT_SYMBOL_GPL(mt76_sw_scan_complete);
1574
1575int mt76_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
1576{
1577 struct mt76_phy *phy = hw->priv;
1578 struct mt76_dev *dev = phy->dev;
1579
1580 mutex_lock(&dev->mutex);
1581 *tx_ant = phy->antenna_mask;
1582 *rx_ant = phy->antenna_mask;
1583 mutex_unlock(&dev->mutex);
1584
1585 return 0;
1586}
1587EXPORT_SYMBOL_GPL(mt76_get_antenna);
1588
1589struct mt76_queue *
1590mt76_init_queue(struct mt76_dev *dev, int qid, int idx, int n_desc,
1591 int ring_base)
1592{
1593 struct mt76_queue *hwq;
1594 int err;
1595
1596 hwq = devm_kzalloc(dev->dev, sizeof(*hwq), GFP_KERNEL);
1597 if (!hwq)
1598 return ERR_PTR(-ENOMEM);
1599
1600 err = dev->queue_ops->alloc(dev, hwq, idx, n_desc, 0, ring_base);
1601 if (err < 0)
1602 return ERR_PTR(err);
1603
1604 return hwq;
1605}
1606EXPORT_SYMBOL_GPL(mt76_init_queue);
1607
1608u16 mt76_calculate_default_rate(struct mt76_phy *phy, int rateidx)
1609{
1610 int offset = 0;
1611
1612 if (phy->chandef.chan->band != NL80211_BAND_2GHZ)
1613 offset = 4;
1614
1615 /* pick the lowest rate for hidden nodes */
1616 if (rateidx < 0)
1617 rateidx = 0;
1618
1619 rateidx += offset;
1620 if (rateidx >= ARRAY_SIZE(mt76_rates))
1621 rateidx = offset;
1622
1623 return mt76_rates[rateidx].hw_value;
1624}
1625EXPORT_SYMBOL_GPL(mt76_calculate_default_rate);
1626
1627void mt76_ethtool_worker(struct mt76_ethtool_worker_info *wi,
1628 struct mt76_sta_stats *stats)
1629{
1630 int i, ei = wi->initial_stat_idx;
1631 u64 *data = wi->data;
1632
1633 wi->sta_count++;
1634
1635 data[ei++] += stats->tx_mode[MT_PHY_TYPE_CCK];
1636 data[ei++] += stats->tx_mode[MT_PHY_TYPE_OFDM];
1637 data[ei++] += stats->tx_mode[MT_PHY_TYPE_HT];
1638 data[ei++] += stats->tx_mode[MT_PHY_TYPE_HT_GF];
1639 data[ei++] += stats->tx_mode[MT_PHY_TYPE_VHT];
1640 data[ei++] += stats->tx_mode[MT_PHY_TYPE_HE_SU];
1641 data[ei++] += stats->tx_mode[MT_PHY_TYPE_HE_EXT_SU];
1642 data[ei++] += stats->tx_mode[MT_PHY_TYPE_HE_TB];
1643 data[ei++] += stats->tx_mode[MT_PHY_TYPE_HE_MU];
1644
1645 for (i = 0; i < ARRAY_SIZE(stats->tx_bw); i++)
1646 data[ei++] += stats->tx_bw[i];
1647
1648 for (i = 0; i < 12; i++)
1649 data[ei++] += stats->tx_mcs[i];
1650
1651 wi->worker_stat_count = ei - wi->initial_stat_idx;
1652}
1653EXPORT_SYMBOL_GPL(mt76_ethtool_worker);
1654
1655enum mt76_dfs_state mt76_phy_dfs_state(struct mt76_phy *phy)
1656{
1657 struct ieee80211_hw *hw = phy->hw;
1658 struct mt76_dev *dev = phy->dev;
1659
1660 if (dev->region == NL80211_DFS_UNSET ||
1661 test_bit(MT76_SCANNING, &phy->state))
1662 return MT_DFS_STATE_DISABLED;
1663
1664 if (!hw->conf.radar_enabled) {
1665 if ((hw->conf.flags & IEEE80211_CONF_MONITOR) &&
1666 (phy->chandef.chan->flags & IEEE80211_CHAN_RADAR))
1667 return MT_DFS_STATE_ACTIVE;
1668
1669 return MT_DFS_STATE_DISABLED;
1670 }
1671
1672 if (!cfg80211_reg_can_beacon(hw->wiphy, &phy->chandef, NL80211_IFTYPE_AP))
1673 return MT_DFS_STATE_CAC;
1674
1675 return MT_DFS_STATE_ACTIVE;
1676}
1677EXPORT_SYMBOL_GPL(mt76_phy_dfs_state);