1 // SPDX-License-Identifier: ISC
3 * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
8 #define CHAN2G(_idx, _freq) { \
9 .band = NL80211_BAND_2GHZ, \
10 .center_freq = (_freq), \
15 #define CHAN5G(_idx, _freq) { \
16 .band = NL80211_BAND_5GHZ, \
17 .center_freq = (_freq), \
22 static const struct ieee80211_channel mt76_channels_2ghz
[] = {
39 static const struct ieee80211_channel mt76_channels_5ghz
[] = {
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 },
82 static int mt76_led_init(struct mt76_dev
*dev
)
84 struct device_node
*np
= dev
->dev
->of_node
;
85 struct ieee80211_hw
*hw
= dev
->hw
;
88 if (!dev
->led_cdev
.brightness_set
&& !dev
->led_cdev
.blink_set
)
91 snprintf(dev
->led_name
, sizeof(dev
->led_name
),
92 "mt76-%s", wiphy_name(hw
->wiphy
));
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
,
99 ARRAY_SIZE(mt76_tpt_blink
));
101 np
= of_get_child_by_name(np
, "led");
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");
108 return led_classdev_register(dev
->dev
, &dev
->led_cdev
);
111 static void mt76_led_cleanup(struct mt76_dev
*dev
)
113 if (!dev
->led_cdev
.brightness_set
&& !dev
->led_cdev
.blink_set
)
116 led_classdev_unregister(&dev
->led_cdev
);
119 static void mt76_init_stream_cap(struct mt76_dev
*dev
,
120 struct ieee80211_supported_band
*sband
,
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
;
129 ht_cap
->cap
|= IEEE80211_HT_CAP_TX_STBC
;
131 ht_cap
->cap
&= ~IEEE80211_HT_CAP_TX_STBC
;
133 for (i
= 0; i
< IEEE80211_HT_MCS_MASK_LEN
; i
++)
134 ht_cap
->mcs
.rx_mask
[i
] = i
< nstream
? 0xff : 0;
139 vht_cap
= &sband
->vht_cap
;
141 vht_cap
->cap
|= IEEE80211_VHT_CAP_TXSTBC
;
143 vht_cap
->cap
&= ~IEEE80211_VHT_CAP_TXSTBC
;
145 for (i
= 0; i
< 8; i
++) {
147 mcs_map
|= (IEEE80211_VHT_MCS_SUPPORT_0_9
<< (i
* 2));
150 (IEEE80211_VHT_MCS_NOT_SUPPORTED
<< (i
* 2));
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
);
156 void mt76_set_stream_caps(struct mt76_dev
*dev
, bool vht
)
158 if (dev
->cap
.has_2ghz
)
159 mt76_init_stream_cap(dev
, &dev
->sband_2g
.sband
, false);
160 if (dev
->cap
.has_5ghz
)
161 mt76_init_stream_cap(dev
, &dev
->sband_5g
.sband
, vht
);
163 EXPORT_SYMBOL_GPL(mt76_set_stream_caps
);
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
)
170 struct ieee80211_supported_band
*sband
= &msband
->sband
;
171 struct ieee80211_sta_ht_cap
*ht_cap
;
172 struct ieee80211_sta_vht_cap
*vht_cap
;
176 size
= n_chan
* sizeof(*chan
);
177 chanlist
= devm_kmemdup(dev
->dev
, chan
, size
, GFP_KERNEL
);
181 msband
->chan
= devm_kcalloc(dev
->dev
, n_chan
, sizeof(*msband
->chan
),
186 sband
->channels
= chanlist
;
187 sband
->n_channels
= n_chan
;
188 sband
->bitrates
= rates
;
189 sband
->n_bitrates
= n_rates
;
190 dev
->chandef
.chan
= &sband
->channels
[0];
191 dev
->chan_state
= &msband
->chan
[0];
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
);
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
;
205 mt76_init_stream_cap(dev
, sband
, vht
);
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
);
223 mt76_init_sband_2g(struct mt76_dev
*dev
, struct ieee80211_rate
*rates
,
226 dev
->hw
->wiphy
->bands
[NL80211_BAND_2GHZ
] = &dev
->sband_2g
.sband
;
228 return mt76_init_sband(dev
, &dev
->sband_2g
,
230 ARRAY_SIZE(mt76_channels_2ghz
),
231 rates
, n_rates
, false);
235 mt76_init_sband_5g(struct mt76_dev
*dev
, struct ieee80211_rate
*rates
,
236 int n_rates
, bool vht
)
238 dev
->hw
->wiphy
->bands
[NL80211_BAND_5GHZ
] = &dev
->sband_5g
.sband
;
240 return mt76_init_sband(dev
, &dev
->sband_5g
,
242 ARRAY_SIZE(mt76_channels_5ghz
),
243 rates
, n_rates
, vht
);
247 mt76_check_sband(struct mt76_dev
*dev
, int band
)
249 struct ieee80211_supported_band
*sband
= dev
->hw
->wiphy
->bands
[band
];
256 for (i
= 0; i
< sband
->n_channels
; i
++) {
257 if (sband
->channels
[i
].flags
& IEEE80211_CHAN_DISABLED
)
267 sband
->n_channels
= 0;
268 dev
->hw
->wiphy
->bands
[band
] = NULL
;
272 mt76_alloc_device(struct device
*pdev
, unsigned int size
,
273 const struct ieee80211_ops
*ops
,
274 const struct mt76_driver_ops
*drv_ops
)
276 struct ieee80211_hw
*hw
;
277 struct mt76_dev
*dev
;
279 hw
= ieee80211_alloc_hw(size
, ops
);
288 spin_lock_init(&dev
->rx_lock
);
289 spin_lock_init(&dev
->lock
);
290 spin_lock_init(&dev
->cc_lock
);
291 mutex_init(&dev
->mutex
);
292 init_waitqueue_head(&dev
->tx_wait
);
293 skb_queue_head_init(&dev
->status_list
);
295 tasklet_init(&dev
->tx_tasklet
, mt76_tx_tasklet
, (unsigned long)dev
);
299 EXPORT_SYMBOL_GPL(mt76_alloc_device
);
301 int mt76_register_device(struct mt76_dev
*dev
, bool vht
,
302 struct ieee80211_rate
*rates
, int n_rates
)
304 struct ieee80211_hw
*hw
= dev
->hw
;
305 struct wiphy
*wiphy
= hw
->wiphy
;
308 dev_set_drvdata(dev
->dev
, dev
);
310 INIT_LIST_HEAD(&dev
->txwi_cache
);
312 SET_IEEE80211_DEV(hw
, dev
->dev
);
313 SET_IEEE80211_PERM_ADDR(hw
, dev
->macaddr
);
315 wiphy
->features
|= NL80211_FEATURE_ACTIVE_MONITOR
;
317 wiphy_ext_feature_set(wiphy
, NL80211_EXT_FEATURE_CQM_RSSI_LIST
);
318 wiphy_ext_feature_set(wiphy
, NL80211_EXT_FEATURE_AIRTIME_FAIRNESS
);
320 wiphy
->available_antennas_tx
= dev
->antenna_mask
;
321 wiphy
->available_antennas_rx
= dev
->antenna_mask
;
323 hw
->txq_data_size
= sizeof(struct mt76_txq
);
324 hw
->max_tx_fragments
= 16;
326 ieee80211_hw_set(hw
, SIGNAL_DBM
);
327 ieee80211_hw_set(hw
, PS_NULLFUNC_STACK
);
328 ieee80211_hw_set(hw
, HOST_BROADCAST_PS_BUFFERING
);
329 ieee80211_hw_set(hw
, AMPDU_AGGREGATION
);
330 ieee80211_hw_set(hw
, SUPPORTS_RC_TABLE
);
331 ieee80211_hw_set(hw
, SUPPORT_FAST_XMIT
);
332 ieee80211_hw_set(hw
, SUPPORTS_CLONED_SKBS
);
333 ieee80211_hw_set(hw
, SUPPORTS_AMSDU_IN_AMPDU
);
334 ieee80211_hw_set(hw
, TX_AMSDU
);
335 ieee80211_hw_set(hw
, TX_FRAG_LIST
);
336 ieee80211_hw_set(hw
, MFP_CAPABLE
);
337 ieee80211_hw_set(hw
, AP_LINK_PS
);
338 ieee80211_hw_set(hw
, REPORTS_TX_ACK_STATUS
);
339 ieee80211_hw_set(hw
, NEEDS_UNIQUE_STA_ADDR
);
340 ieee80211_hw_set(hw
, SUPPORTS_REORDERING_BUFFER
);
342 wiphy
->flags
|= WIPHY_FLAG_IBSS_RSN
;
343 wiphy
->interface_modes
=
344 BIT(NL80211_IFTYPE_STATION
) |
345 BIT(NL80211_IFTYPE_AP
) |
346 #ifdef CONFIG_MAC80211_MESH
347 BIT(NL80211_IFTYPE_MESH_POINT
) |
349 BIT(NL80211_IFTYPE_ADHOC
);
351 if (dev
->cap
.has_2ghz
) {
352 ret
= mt76_init_sband_2g(dev
, rates
, n_rates
);
357 if (dev
->cap
.has_5ghz
) {
358 ret
= mt76_init_sband_5g(dev
, rates
+ 4, n_rates
- 4, vht
);
363 wiphy_read_of_freq_limits(dev
->hw
->wiphy
);
364 mt76_check_sband(dev
, NL80211_BAND_2GHZ
);
365 mt76_check_sband(dev
, NL80211_BAND_5GHZ
);
367 if (IS_ENABLED(CONFIG_MT76_LEDS
)) {
368 ret
= mt76_led_init(dev
);
373 return ieee80211_register_hw(hw
);
375 EXPORT_SYMBOL_GPL(mt76_register_device
);
377 void mt76_unregister_device(struct mt76_dev
*dev
)
379 struct ieee80211_hw
*hw
= dev
->hw
;
381 if (IS_ENABLED(CONFIG_MT76_LEDS
))
382 mt76_led_cleanup(dev
);
383 mt76_tx_status_check(dev
, NULL
, true);
384 ieee80211_unregister_hw(hw
);
386 EXPORT_SYMBOL_GPL(mt76_unregister_device
);
388 void mt76_free_device(struct mt76_dev
*dev
)
391 ieee80211_free_hw(dev
->hw
);
393 EXPORT_SYMBOL_GPL(mt76_free_device
);
395 void mt76_rx(struct mt76_dev
*dev
, enum mt76_rxq_id q
, struct sk_buff
*skb
)
397 if (!test_bit(MT76_STATE_RUNNING
, &dev
->state
)) {
402 __skb_queue_tail(&dev
->rx_skb
[q
], skb
);
404 EXPORT_SYMBOL_GPL(mt76_rx
);
406 bool mt76_has_tx_pending(struct mt76_dev
*dev
)
408 struct mt76_queue
*q
;
411 for (i
= 0; i
< ARRAY_SIZE(dev
->q_tx
); i
++) {
419 EXPORT_SYMBOL_GPL(mt76_has_tx_pending
);
421 static struct mt76_channel_state
*
422 mt76_channel_state(struct mt76_dev
*dev
, struct ieee80211_channel
*c
)
424 struct mt76_sband
*msband
;
427 if (c
->band
== NL80211_BAND_2GHZ
)
428 msband
= &dev
->sband_2g
;
430 msband
= &dev
->sband_5g
;
432 idx
= c
- &msband
->sband
.channels
[0];
433 return &msband
->chan
[idx
];
436 void mt76_update_survey(struct mt76_dev
*dev
)
438 struct mt76_channel_state
*state
= dev
->chan_state
;
441 if (!test_bit(MT76_STATE_RUNNING
, &dev
->state
))
444 if (dev
->drv
->update_survey
)
445 dev
->drv
->update_survey(dev
);
447 cur_time
= ktime_get_boottime();
448 state
->cc_active
+= ktime_to_us(ktime_sub(cur_time
,
450 dev
->survey_time
= cur_time
;
452 if (dev
->drv
->drv_flags
& MT_DRV_SW_RX_AIRTIME
) {
453 spin_lock_bh(&dev
->cc_lock
);
454 state
->cc_bss_rx
+= dev
->cur_cc_bss_rx
;
455 dev
->cur_cc_bss_rx
= 0;
456 spin_unlock_bh(&dev
->cc_lock
);
459 EXPORT_SYMBOL_GPL(mt76_update_survey
);
461 void mt76_set_channel(struct mt76_dev
*dev
)
463 struct ieee80211_hw
*hw
= dev
->hw
;
464 struct cfg80211_chan_def
*chandef
= &hw
->conf
.chandef
;
465 bool offchannel
= hw
->conf
.flags
& IEEE80211_CONF_OFFCHANNEL
;
466 int timeout
= HZ
/ 5;
468 wait_event_timeout(dev
->tx_wait
, !mt76_has_tx_pending(dev
), timeout
);
469 mt76_update_survey(dev
);
471 dev
->chandef
= *chandef
;
472 dev
->chan_state
= mt76_channel_state(dev
, chandef
->chan
);
475 dev
->main_chan
= chandef
->chan
;
477 if (chandef
->chan
!= dev
->main_chan
)
478 memset(dev
->chan_state
, 0, sizeof(*dev
->chan_state
));
480 EXPORT_SYMBOL_GPL(mt76_set_channel
);
482 int mt76_get_survey(struct ieee80211_hw
*hw
, int idx
,
483 struct survey_info
*survey
)
485 struct mt76_dev
*dev
= hw
->priv
;
486 struct mt76_sband
*sband
;
487 struct ieee80211_channel
*chan
;
488 struct mt76_channel_state
*state
;
491 mutex_lock(&dev
->mutex
);
492 if (idx
== 0 && dev
->drv
->update_survey
)
493 mt76_update_survey(dev
);
495 sband
= &dev
->sband_2g
;
496 if (idx
>= sband
->sband
.n_channels
) {
497 idx
-= sband
->sband
.n_channels
;
498 sband
= &dev
->sband_5g
;
501 if (idx
>= sband
->sband
.n_channels
) {
506 chan
= &sband
->sband
.channels
[idx
];
507 state
= mt76_channel_state(dev
, chan
);
509 memset(survey
, 0, sizeof(*survey
));
510 survey
->channel
= chan
;
511 survey
->filled
= SURVEY_INFO_TIME
| SURVEY_INFO_TIME_BUSY
;
512 survey
->filled
|= dev
->drv
->survey_flags
;
513 if (chan
== dev
->main_chan
) {
514 survey
->filled
|= SURVEY_INFO_IN_USE
;
516 if (dev
->drv
->drv_flags
& MT_DRV_SW_RX_AIRTIME
)
517 survey
->filled
|= SURVEY_INFO_TIME_BSS_RX
;
520 survey
->time_busy
= div_u64(state
->cc_busy
, 1000);
521 survey
->time_rx
= div_u64(state
->cc_rx
, 1000);
522 survey
->time
= div_u64(state
->cc_active
, 1000);
524 spin_lock_bh(&dev
->cc_lock
);
525 survey
->time_bss_rx
= div_u64(state
->cc_bss_rx
, 1000);
526 survey
->time_tx
= div_u64(state
->cc_tx
, 1000);
527 spin_unlock_bh(&dev
->cc_lock
);
530 mutex_unlock(&dev
->mutex
);
534 EXPORT_SYMBOL_GPL(mt76_get_survey
);
536 void mt76_wcid_key_setup(struct mt76_dev
*dev
, struct mt76_wcid
*wcid
,
537 struct ieee80211_key_conf
*key
)
539 struct ieee80211_key_seq seq
;
542 wcid
->rx_check_pn
= false;
547 if (key
->cipher
!= WLAN_CIPHER_SUITE_CCMP
)
550 wcid
->rx_check_pn
= true;
551 for (i
= 0; i
< IEEE80211_NUM_TIDS
; i
++) {
552 ieee80211_get_key_rx_seq(key
, i
, &seq
);
553 memcpy(wcid
->rx_key_pn
[i
], seq
.ccmp
.pn
, sizeof(seq
.ccmp
.pn
));
556 EXPORT_SYMBOL(mt76_wcid_key_setup
);
558 static struct ieee80211_sta
*mt76_rx_convert(struct sk_buff
*skb
)
560 struct ieee80211_rx_status
*status
= IEEE80211_SKB_RXCB(skb
);
561 struct mt76_rx_status mstat
;
563 mstat
= *((struct mt76_rx_status
*)skb
->cb
);
564 memset(status
, 0, sizeof(*status
));
566 status
->flag
= mstat
.flag
;
567 status
->freq
= mstat
.freq
;
568 status
->enc_flags
= mstat
.enc_flags
;
569 status
->encoding
= mstat
.encoding
;
570 status
->bw
= mstat
.bw
;
571 status
->rate_idx
= mstat
.rate_idx
;
572 status
->nss
= mstat
.nss
;
573 status
->band
= mstat
.band
;
574 status
->signal
= mstat
.signal
;
575 status
->chains
= mstat
.chains
;
576 status
->ampdu_reference
= mstat
.ampdu_ref
;
578 BUILD_BUG_ON(sizeof(mstat
) > sizeof(skb
->cb
));
579 BUILD_BUG_ON(sizeof(status
->chain_signal
) !=
580 sizeof(mstat
.chain_signal
));
581 memcpy(status
->chain_signal
, mstat
.chain_signal
,
582 sizeof(mstat
.chain_signal
));
584 return wcid_to_sta(mstat
.wcid
);
588 mt76_check_ccmp_pn(struct sk_buff
*skb
)
590 struct mt76_rx_status
*status
= (struct mt76_rx_status
*)skb
->cb
;
591 struct mt76_wcid
*wcid
= status
->wcid
;
592 struct ieee80211_hdr
*hdr
;
595 if (!(status
->flag
& RX_FLAG_DECRYPTED
))
598 if (!wcid
|| !wcid
->rx_check_pn
)
601 if (!(status
->flag
& RX_FLAG_IV_STRIPPED
)) {
603 * Validate the first fragment both here and in mac80211
604 * All further fragments will be validated by mac80211 only.
606 hdr
= (struct ieee80211_hdr
*)skb
->data
;
607 if (ieee80211_is_frag(hdr
) &&
608 !ieee80211_is_first_frag(hdr
->frame_control
))
612 BUILD_BUG_ON(sizeof(status
->iv
) != sizeof(wcid
->rx_key_pn
[0]));
613 ret
= memcmp(status
->iv
, wcid
->rx_key_pn
[status
->tid
],
616 return -EINVAL
; /* replay */
618 memcpy(wcid
->rx_key_pn
[status
->tid
], status
->iv
, sizeof(status
->iv
));
620 if (status
->flag
& RX_FLAG_IV_STRIPPED
)
621 status
->flag
|= RX_FLAG_PN_VALIDATED
;
627 mt76_airtime_report(struct mt76_dev
*dev
, struct mt76_rx_status
*status
,
630 struct mt76_wcid
*wcid
= status
->wcid
;
631 struct ieee80211_sta
*sta
;
634 airtime
= mt76_calc_rx_airtime(dev
, status
, len
);
635 spin_lock(&dev
->cc_lock
);
636 dev
->cur_cc_bss_rx
+= airtime
;
637 spin_unlock(&dev
->cc_lock
);
639 if (!wcid
|| !wcid
->sta
)
642 sta
= container_of((void *)wcid
, struct ieee80211_sta
, drv_priv
);
643 ieee80211_sta_register_airtime(sta
, status
->tid
, 0, airtime
);
647 mt76_airtime_flush_ampdu(struct mt76_dev
*dev
)
649 struct mt76_wcid
*wcid
;
652 if (!dev
->rx_ampdu_len
)
655 wcid_idx
= dev
->rx_ampdu_status
.wcid_idx
;
656 if (wcid_idx
< ARRAY_SIZE(dev
->wcid
))
657 wcid
= rcu_dereference(dev
->wcid
[wcid_idx
]);
660 dev
->rx_ampdu_status
.wcid
= wcid
;
662 mt76_airtime_report(dev
, &dev
->rx_ampdu_status
, dev
->rx_ampdu_len
);
664 dev
->rx_ampdu_len
= 0;
665 dev
->rx_ampdu_ref
= 0;
669 mt76_airtime_check(struct mt76_dev
*dev
, struct sk_buff
*skb
)
671 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*)skb
->data
;
672 struct mt76_rx_status
*status
= (struct mt76_rx_status
*)skb
->cb
;
673 struct mt76_wcid
*wcid
= status
->wcid
;
675 if (!(dev
->drv
->drv_flags
& MT_DRV_SW_RX_AIRTIME
))
678 if (!wcid
|| !wcid
->sta
) {
679 if (!ether_addr_equal(hdr
->addr1
, dev
->macaddr
))
685 if (!(status
->flag
& RX_FLAG_AMPDU_DETAILS
) ||
686 status
->ampdu_ref
!= dev
->rx_ampdu_ref
)
687 mt76_airtime_flush_ampdu(dev
);
689 if (status
->flag
& RX_FLAG_AMPDU_DETAILS
) {
690 if (!dev
->rx_ampdu_len
||
691 status
->ampdu_ref
!= dev
->rx_ampdu_ref
) {
692 dev
->rx_ampdu_status
= *status
;
693 dev
->rx_ampdu_status
.wcid_idx
= wcid
? wcid
->idx
: 0xff;
694 dev
->rx_ampdu_ref
= status
->ampdu_ref
;
697 dev
->rx_ampdu_len
+= skb
->len
;
701 mt76_airtime_report(dev
, status
, skb
->len
);
705 mt76_check_sta(struct mt76_dev
*dev
, struct sk_buff
*skb
)
707 struct mt76_rx_status
*status
= (struct mt76_rx_status
*)skb
->cb
;
708 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*)skb
->data
;
709 struct ieee80211_sta
*sta
;
710 struct mt76_wcid
*wcid
= status
->wcid
;
714 if (ieee80211_is_pspoll(hdr
->frame_control
) && !wcid
) {
715 sta
= ieee80211_find_sta_by_ifaddr(dev
->hw
, hdr
->addr2
, NULL
);
717 wcid
= status
->wcid
= (struct mt76_wcid
*)sta
->drv_priv
;
720 mt76_airtime_check(dev
, skb
);
722 if (!wcid
|| !wcid
->sta
)
725 sta
= container_of((void *)wcid
, struct ieee80211_sta
, drv_priv
);
727 if (status
->signal
<= 0)
728 ewma_signal_add(&wcid
->rssi
, -status
->signal
);
730 wcid
->inactive_count
= 0;
732 if (!test_bit(MT_WCID_FLAG_CHECK_PS
, &wcid
->flags
))
735 if (ieee80211_is_pspoll(hdr
->frame_control
)) {
736 ieee80211_sta_pspoll(sta
);
740 if (ieee80211_has_morefrags(hdr
->frame_control
) ||
741 !(ieee80211_is_mgmt(hdr
->frame_control
) ||
742 ieee80211_is_data(hdr
->frame_control
)))
745 ps
= ieee80211_has_pm(hdr
->frame_control
);
747 if (ps
&& (ieee80211_is_data_qos(hdr
->frame_control
) ||
748 ieee80211_is_qos_nullfunc(hdr
->frame_control
)))
749 ieee80211_sta_uapsd_trigger(sta
, status
->tid
);
751 if (!!test_bit(MT_WCID_FLAG_PS
, &wcid
->flags
) == ps
)
755 set_bit(MT_WCID_FLAG_PS
, &wcid
->flags
);
757 clear_bit(MT_WCID_FLAG_PS
, &wcid
->flags
);
759 dev
->drv
->sta_ps(dev
, sta
, ps
);
760 ieee80211_sta_ps_transition(sta
, ps
);
765 for (i
= 0; i
< ARRAY_SIZE(sta
->txq
); i
++) {
766 struct mt76_txq
*mtxq
;
771 mtxq
= (struct mt76_txq
*)sta
->txq
[i
]->drv_priv
;
772 if (!skb_queue_empty(&mtxq
->retry_q
))
773 ieee80211_schedule_txq(dev
->hw
, sta
->txq
[i
]);
777 void mt76_rx_complete(struct mt76_dev
*dev
, struct sk_buff_head
*frames
,
778 struct napi_struct
*napi
)
780 struct ieee80211_sta
*sta
;
783 spin_lock(&dev
->rx_lock
);
784 while ((skb
= __skb_dequeue(frames
)) != NULL
) {
785 if (mt76_check_ccmp_pn(skb
)) {
790 sta
= mt76_rx_convert(skb
);
791 ieee80211_rx_napi(dev
->hw
, sta
, skb
, napi
);
793 spin_unlock(&dev
->rx_lock
);
796 void mt76_rx_poll_complete(struct mt76_dev
*dev
, enum mt76_rxq_id q
,
797 struct napi_struct
*napi
)
799 struct sk_buff_head frames
;
802 __skb_queue_head_init(&frames
);
804 while ((skb
= __skb_dequeue(&dev
->rx_skb
[q
])) != NULL
) {
805 mt76_check_sta(dev
, skb
);
806 mt76_rx_aggr_reorder(skb
, &frames
);
809 mt76_rx_complete(dev
, &frames
, napi
);
811 EXPORT_SYMBOL_GPL(mt76_rx_poll_complete
);
814 mt76_sta_add(struct mt76_dev
*dev
, struct ieee80211_vif
*vif
,
815 struct ieee80211_sta
*sta
)
817 struct mt76_wcid
*wcid
= (struct mt76_wcid
*)sta
->drv_priv
;
821 mutex_lock(&dev
->mutex
);
823 ret
= dev
->drv
->sta_add(dev
, vif
, sta
);
827 for (i
= 0; i
< ARRAY_SIZE(sta
->txq
); i
++) {
828 struct mt76_txq
*mtxq
;
833 mtxq
= (struct mt76_txq
*)sta
->txq
[i
]->drv_priv
;
836 mt76_txq_init(dev
, sta
->txq
[i
]);
839 ewma_signal_init(&wcid
->rssi
);
840 rcu_assign_pointer(dev
->wcid
[wcid
->idx
], wcid
);
843 mutex_unlock(&dev
->mutex
);
848 void __mt76_sta_remove(struct mt76_dev
*dev
, struct ieee80211_vif
*vif
,
849 struct ieee80211_sta
*sta
)
851 struct mt76_wcid
*wcid
= (struct mt76_wcid
*)sta
->drv_priv
;
852 int i
, idx
= wcid
->idx
;
854 rcu_assign_pointer(dev
->wcid
[idx
], NULL
);
857 for (i
= 0; i
< ARRAY_SIZE(wcid
->aggr
); i
++)
858 mt76_rx_aggr_stop(dev
, wcid
, i
);
860 if (dev
->drv
->sta_remove
)
861 dev
->drv
->sta_remove(dev
, vif
, sta
);
863 mt76_tx_status_check(dev
, wcid
, true);
864 for (i
= 0; i
< ARRAY_SIZE(sta
->txq
); i
++)
865 mt76_txq_remove(dev
, sta
->txq
[i
]);
866 mt76_wcid_free(dev
->wcid_mask
, idx
);
868 EXPORT_SYMBOL_GPL(__mt76_sta_remove
);
871 mt76_sta_remove(struct mt76_dev
*dev
, struct ieee80211_vif
*vif
,
872 struct ieee80211_sta
*sta
)
874 mutex_lock(&dev
->mutex
);
875 __mt76_sta_remove(dev
, vif
, sta
);
876 mutex_unlock(&dev
->mutex
);
879 int mt76_sta_state(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
,
880 struct ieee80211_sta
*sta
,
881 enum ieee80211_sta_state old_state
,
882 enum ieee80211_sta_state new_state
)
884 struct mt76_dev
*dev
= hw
->priv
;
886 if (old_state
== IEEE80211_STA_NOTEXIST
&&
887 new_state
== IEEE80211_STA_NONE
)
888 return mt76_sta_add(dev
, vif
, sta
);
890 if (old_state
== IEEE80211_STA_AUTH
&&
891 new_state
== IEEE80211_STA_ASSOC
&&
893 dev
->drv
->sta_assoc(dev
, vif
, sta
);
895 if (old_state
== IEEE80211_STA_NONE
&&
896 new_state
== IEEE80211_STA_NOTEXIST
)
897 mt76_sta_remove(dev
, vif
, sta
);
901 EXPORT_SYMBOL_GPL(mt76_sta_state
);
903 int mt76_get_txpower(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
,
906 struct mt76_dev
*dev
= hw
->priv
;
907 int n_chains
= hweight8(dev
->antenna_mask
);
909 *dbm
= DIV_ROUND_UP(dev
->txpower_cur
, 2);
911 /* convert from per-chain power to combined
930 EXPORT_SYMBOL_GPL(mt76_get_txpower
);
933 __mt76_csa_finish(void *priv
, u8
*mac
, struct ieee80211_vif
*vif
)
935 if (vif
->csa_active
&& ieee80211_csa_is_complete(vif
))
936 ieee80211_csa_finish(vif
);
939 void mt76_csa_finish(struct mt76_dev
*dev
)
941 if (!dev
->csa_complete
)
944 ieee80211_iterate_active_interfaces_atomic(dev
->hw
,
945 IEEE80211_IFACE_ITER_RESUME_ALL
,
946 __mt76_csa_finish
, dev
);
948 dev
->csa_complete
= 0;
950 EXPORT_SYMBOL_GPL(mt76_csa_finish
);
953 __mt76_csa_check(void *priv
, u8
*mac
, struct ieee80211_vif
*vif
)
955 struct mt76_dev
*dev
= priv
;
957 if (!vif
->csa_active
)
960 dev
->csa_complete
|= ieee80211_csa_is_complete(vif
);
963 void mt76_csa_check(struct mt76_dev
*dev
)
965 ieee80211_iterate_active_interfaces_atomic(dev
->hw
,
966 IEEE80211_IFACE_ITER_RESUME_ALL
,
967 __mt76_csa_check
, dev
);
969 EXPORT_SYMBOL_GPL(mt76_csa_check
);
972 mt76_set_tim(struct ieee80211_hw
*hw
, struct ieee80211_sta
*sta
, bool set
)
976 EXPORT_SYMBOL_GPL(mt76_set_tim
);
978 void mt76_insert_ccmp_hdr(struct sk_buff
*skb
, u8 key_id
)
980 struct mt76_rx_status
*status
= (struct mt76_rx_status
*)skb
->cb
;
981 int hdr_len
= ieee80211_get_hdrlen_from_skb(skb
);
982 u8
*hdr
, *pn
= status
->iv
;
985 memmove(skb
->data
, skb
->data
+ 8, hdr_len
);
986 hdr
= skb
->data
+ hdr_len
;
991 hdr
[3] = 0x20 | (key_id
<< 6);
997 status
->flag
&= ~RX_FLAG_IV_STRIPPED
;
999 EXPORT_SYMBOL_GPL(mt76_insert_ccmp_hdr
);
1001 int mt76_get_rate(struct mt76_dev
*dev
,
1002 struct ieee80211_supported_band
*sband
,
1005 int i
, offset
= 0, len
= sband
->n_bitrates
;
1008 if (sband
== &dev
->sband_5g
.sband
)
1011 idx
&= ~BIT(2); /* short preamble */
1012 } else if (sband
== &dev
->sband_2g
.sband
) {
1016 for (i
= offset
; i
< len
; i
++) {
1017 if ((sband
->bitrates
[i
].hw_value
& GENMASK(7, 0)) == idx
)
1023 EXPORT_SYMBOL_GPL(mt76_get_rate
);
1025 void mt76_sw_scan(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
,
1028 struct mt76_dev
*dev
= hw
->priv
;
1030 set_bit(MT76_SCANNING
, &dev
->state
);
1032 EXPORT_SYMBOL_GPL(mt76_sw_scan
);
1034 void mt76_sw_scan_complete(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
)
1036 struct mt76_dev
*dev
= hw
->priv
;
1038 clear_bit(MT76_SCANNING
, &dev
->state
);
1040 EXPORT_SYMBOL_GPL(mt76_sw_scan_complete
);
1042 int mt76_get_antenna(struct ieee80211_hw
*hw
, u32
*tx_ant
, u32
*rx_ant
)
1044 struct mt76_dev
*dev
= hw
->priv
;
1046 mutex_lock(&dev
->mutex
);
1047 *tx_ant
= dev
->antenna_mask
;
1048 *rx_ant
= dev
->antenna_mask
;
1049 mutex_unlock(&dev
->mutex
);
1053 EXPORT_SYMBOL_GPL(mt76_get_antenna
);