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->phy.sband_2g.sband, false);
160 if (dev->cap.has_5ghz)
161 mt76_init_stream_cap(dev, &dev->phy.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->phy.chandef.chan = &sband->channels[0];
191 dev->phy.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->phy.sband_2g.sband;
228 return mt76_init_sband(dev, &dev->phy.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->phy.sband_5g.sband;
240 return mt76_init_sband(dev, &dev->phy.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_phy *phy;
278 struct mt76_dev *dev;
281 hw = ieee80211_alloc_hw(size, ops);
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);
301 INIT_LIST_HEAD(&dev->txwi_cache);
303 for (i = 0; i < ARRAY_SIZE(dev->q_rx); i++)
304 skb_queue_head_init(&dev->rx_skb[i]);
306 tasklet_init(&dev->tx_tasklet, mt76_tx_tasklet, (unsigned long)dev);
310 EXPORT_SYMBOL_GPL(mt76_alloc_device);
312 int mt76_register_device(struct mt76_dev *dev, bool vht,
313 struct ieee80211_rate *rates, int n_rates)
315 struct ieee80211_hw *hw = dev->hw;
316 struct wiphy *wiphy = hw->wiphy;
319 dev_set_drvdata(dev->dev, dev);
321 SET_IEEE80211_DEV(hw, dev->dev);
322 SET_IEEE80211_PERM_ADDR(hw, dev->macaddr);
324 wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR;
326 wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
327 wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_AIRTIME_FAIRNESS);
329 wiphy->available_antennas_tx = dev->antenna_mask;
330 wiphy->available_antennas_rx = dev->antenna_mask;
332 hw->txq_data_size = sizeof(struct mt76_txq);
333 hw->max_tx_fragments = 16;
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);
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) |
358 BIT(NL80211_IFTYPE_ADHOC);
360 if (dev->cap.has_2ghz) {
361 ret = mt76_init_sband_2g(dev, rates, n_rates);
366 if (dev->cap.has_5ghz) {
367 ret = mt76_init_sband_5g(dev, rates + 4, n_rates - 4, vht);
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);
376 if (IS_ENABLED(CONFIG_MT76_LEDS)) {
377 ret = mt76_led_init(dev);
382 return ieee80211_register_hw(hw);
384 EXPORT_SYMBOL_GPL(mt76_register_device);
386 void mt76_unregister_device(struct mt76_dev *dev)
388 struct ieee80211_hw *hw = dev->hw;
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);
395 EXPORT_SYMBOL_GPL(mt76_unregister_device);
397 void mt76_free_device(struct mt76_dev *dev)
400 ieee80211_free_hw(dev->hw);
402 EXPORT_SYMBOL_GPL(mt76_free_device);
404 void mt76_rx(struct mt76_dev *dev, enum mt76_rxq_id q, struct sk_buff *skb)
406 struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
407 struct mt76_phy *phy = mt76_dev_phy(dev, status->ext_phy);
409 if (!test_bit(MT76_STATE_RUNNING, &phy->state)) {
414 __skb_queue_tail(&dev->rx_skb[q], skb);
416 EXPORT_SYMBOL_GPL(mt76_rx);
418 bool mt76_has_tx_pending(struct mt76_phy *phy)
420 struct mt76_dev *dev = phy->dev;
421 struct mt76_queue *q;
424 offset = __MT_TXQ_MAX * (phy != &dev->phy);
426 for (i = 0; i < __MT_TXQ_MAX; i++) {
427 q = dev->q_tx[offset + i].q;
434 EXPORT_SYMBOL_GPL(mt76_has_tx_pending);
436 static struct mt76_channel_state *
437 mt76_channel_state(struct mt76_phy *phy, struct ieee80211_channel *c)
439 struct mt76_sband *msband;
442 if (c->band == NL80211_BAND_2GHZ)
443 msband = &phy->sband_2g;
445 msband = &phy->sband_5g;
447 idx = c - &msband->sband.channels[0];
448 return &msband->chan[idx];
452 mt76_update_survey_active_time(struct mt76_phy *phy, ktime_t time)
454 struct mt76_channel_state *state = phy->chan_state;
456 state->cc_active += ktime_to_us(ktime_sub(time,
458 phy->survey_time = time;
461 void mt76_update_survey(struct mt76_dev *dev)
465 if (dev->drv->update_survey)
466 dev->drv->update_survey(dev);
468 cur_time = ktime_get_boottime();
469 mt76_update_survey_active_time(&dev->phy, cur_time);
471 mt76_update_survey_active_time(dev->phy2, cur_time);
473 if (dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME) {
474 struct mt76_channel_state *state = dev->phy.chan_state;
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);
482 EXPORT_SYMBOL_GPL(mt76_update_survey);
484 void mt76_set_channel(struct mt76_phy *phy)
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;
492 wait_event_timeout(dev->tx_wait, !mt76_has_tx_pending(phy), timeout);
493 mt76_update_survey(dev);
495 phy->chandef = *chandef;
496 phy->chan_state = mt76_channel_state(phy, chandef->chan);
499 phy->main_chan = chandef->chan;
501 if (chandef->chan != phy->main_chan)
502 memset(phy->chan_state, 0, sizeof(*phy->chan_state));
504 EXPORT_SYMBOL_GPL(mt76_set_channel);
506 int mt76_get_survey(struct ieee80211_hw *hw, int idx,
507 struct survey_info *survey)
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;
516 mutex_lock(&dev->mutex);
517 if (idx == 0 && dev->drv->update_survey)
518 mt76_update_survey(dev);
520 sband = &phy->sband_2g;
521 if (idx >= sband->sband.n_channels) {
522 idx -= sband->sband.n_channels;
523 sband = &phy->sband_5g;
526 if (idx >= sband->sband.n_channels) {
531 chan = &sband->sband.channels[idx];
532 state = mt76_channel_state(phy, chan);
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;
541 if (dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME)
542 survey->filled |= SURVEY_INFO_TIME_BSS_RX;
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);
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);
555 mutex_unlock(&dev->mutex);
559 EXPORT_SYMBOL_GPL(mt76_get_survey);
561 void mt76_wcid_key_setup(struct mt76_dev *dev, struct mt76_wcid *wcid,
562 struct ieee80211_key_conf *key)
564 struct ieee80211_key_seq seq;
567 wcid->rx_check_pn = false;
572 if (key->cipher != WLAN_CIPHER_SUITE_CCMP)
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));
581 EXPORT_SYMBOL(mt76_wcid_key_setup);
584 mt76_rx_convert(struct mt76_dev *dev, struct sk_buff *skb,
585 struct ieee80211_hw **hw,
586 struct ieee80211_sta **sta)
589 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
590 struct mt76_rx_status mstat;
592 mstat = *((struct mt76_rx_status *)skb->cb);
593 memset(status, 0, sizeof(*status));
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;
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));
613 *sta = wcid_to_sta(mstat.wcid);
614 *hw = mt76_phy_hw(dev, mstat.ext_phy);
618 mt76_check_ccmp_pn(struct sk_buff *skb)
620 struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
621 struct mt76_wcid *wcid = status->wcid;
622 struct ieee80211_hdr *hdr;
625 if (!(status->flag & RX_FLAG_DECRYPTED))
628 if (!wcid || !wcid->rx_check_pn)
631 if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
633 * Validate the first fragment both here and in mac80211
634 * All further fragments will be validated by mac80211 only.
636 hdr = (struct ieee80211_hdr *)skb->data;
637 if (ieee80211_is_frag(hdr) &&
638 !ieee80211_is_first_frag(hdr->frame_control))
642 BUILD_BUG_ON(sizeof(status->iv) != sizeof(wcid->rx_key_pn[0]));
643 ret = memcmp(status->iv, wcid->rx_key_pn[status->tid],
646 return -EINVAL; /* replay */
648 memcpy(wcid->rx_key_pn[status->tid], status->iv, sizeof(status->iv));
650 if (status->flag & RX_FLAG_IV_STRIPPED)
651 status->flag |= RX_FLAG_PN_VALIDATED;
657 mt76_airtime_report(struct mt76_dev *dev, struct mt76_rx_status *status,
660 struct mt76_wcid *wcid = status->wcid;
661 struct ieee80211_sta *sta;
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);
669 if (!wcid || !wcid->sta)
672 sta = container_of((void *)wcid, struct ieee80211_sta, drv_priv);
673 ieee80211_sta_register_airtime(sta, status->tid, 0, airtime);
677 mt76_airtime_flush_ampdu(struct mt76_dev *dev)
679 struct mt76_wcid *wcid;
682 if (!dev->rx_ampdu_len)
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]);
690 dev->rx_ampdu_status.wcid = wcid;
692 mt76_airtime_report(dev, &dev->rx_ampdu_status, dev->rx_ampdu_len);
694 dev->rx_ampdu_len = 0;
695 dev->rx_ampdu_ref = 0;
699 mt76_airtime_check(struct mt76_dev *dev, struct sk_buff *skb)
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;
705 if (!(dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME))
708 if (!wcid || !wcid->sta) {
709 if (!ether_addr_equal(hdr->addr1, dev->macaddr))
715 if (!(status->flag & RX_FLAG_AMPDU_DETAILS) ||
716 status->ampdu_ref != dev->rx_ampdu_ref)
717 mt76_airtime_flush_ampdu(dev);
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;
727 dev->rx_ampdu_len += skb->len;
731 mt76_airtime_report(dev, status, skb->len);
735 mt76_check_sta(struct mt76_dev *dev, struct sk_buff *skb)
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;
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);
749 wcid = status->wcid = (struct mt76_wcid *)sta->drv_priv;
752 mt76_airtime_check(dev, skb);
754 if (!wcid || !wcid->sta)
757 sta = container_of((void *)wcid, struct ieee80211_sta, drv_priv);
759 if (status->signal <= 0)
760 ewma_signal_add(&wcid->rssi, -status->signal);
762 wcid->inactive_count = 0;
764 if (!test_bit(MT_WCID_FLAG_CHECK_PS, &wcid->flags))
767 if (ieee80211_is_pspoll(hdr->frame_control)) {
768 ieee80211_sta_pspoll(sta);
772 if (ieee80211_has_morefrags(hdr->frame_control) ||
773 !(ieee80211_is_mgmt(hdr->frame_control) ||
774 ieee80211_is_data(hdr->frame_control)))
777 ps = ieee80211_has_pm(hdr->frame_control);
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);
783 if (!!test_bit(MT_WCID_FLAG_PS, &wcid->flags) == ps)
787 set_bit(MT_WCID_FLAG_PS, &wcid->flags);
789 clear_bit(MT_WCID_FLAG_PS, &wcid->flags);
791 dev->drv->sta_ps(dev, sta, ps);
792 ieee80211_sta_ps_transition(sta, ps);
797 for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
798 struct mt76_txq *mtxq;
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]);
809 void mt76_rx_complete(struct mt76_dev *dev, struct sk_buff_head *frames,
810 struct napi_struct *napi)
812 struct ieee80211_sta *sta;
813 struct ieee80211_hw *hw;
816 spin_lock(&dev->rx_lock);
817 while ((skb = __skb_dequeue(frames)) != NULL) {
818 if (mt76_check_ccmp_pn(skb)) {
823 mt76_rx_convert(dev, skb, &hw, &sta);
824 ieee80211_rx_napi(hw, sta, skb, napi);
826 spin_unlock(&dev->rx_lock);
829 void mt76_rx_poll_complete(struct mt76_dev *dev, enum mt76_rxq_id q,
830 struct napi_struct *napi)
832 struct sk_buff_head frames;
835 __skb_queue_head_init(&frames);
837 while ((skb = __skb_dequeue(&dev->rx_skb[q])) != NULL) {
838 mt76_check_sta(dev, skb);
839 mt76_rx_aggr_reorder(skb, &frames);
842 mt76_rx_complete(dev, &frames, napi);
844 EXPORT_SYMBOL_GPL(mt76_rx_poll_complete);
847 mt76_sta_add(struct mt76_dev *dev, struct ieee80211_vif *vif,
848 struct ieee80211_sta *sta, bool ext_phy)
850 struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
854 mutex_lock(&dev->mutex);
856 ret = dev->drv->sta_add(dev, vif, sta);
860 for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
861 struct mt76_txq *mtxq;
866 mtxq = (struct mt76_txq *)sta->txq[i]->drv_priv;
869 mt76_txq_init(dev, sta->txq[i]);
872 ewma_signal_init(&wcid->rssi);
874 mt76_wcid_mask_set(dev->wcid_phy_mask, wcid->idx);
875 rcu_assign_pointer(dev->wcid[wcid->idx], wcid);
878 mutex_unlock(&dev->mutex);
883 void __mt76_sta_remove(struct mt76_dev *dev, struct ieee80211_vif *vif,
884 struct ieee80211_sta *sta)
886 struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
887 int i, idx = wcid->idx;
889 rcu_assign_pointer(dev->wcid[idx], NULL);
892 for (i = 0; i < ARRAY_SIZE(wcid->aggr); i++)
893 mt76_rx_aggr_stop(dev, wcid, i);
895 if (dev->drv->sta_remove)
896 dev->drv->sta_remove(dev, vif, sta);
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);
904 EXPORT_SYMBOL_GPL(__mt76_sta_remove);
907 mt76_sta_remove(struct mt76_dev *dev, struct ieee80211_vif *vif,
908 struct ieee80211_sta *sta)
910 mutex_lock(&dev->mutex);
911 __mt76_sta_remove(dev, vif, sta);
912 mutex_unlock(&dev->mutex);
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)
920 struct mt76_phy *phy = hw->priv;
921 struct mt76_dev *dev = phy->dev;
922 bool ext_phy = phy != &dev->phy;
924 if (old_state == IEEE80211_STA_NOTEXIST &&
925 new_state == IEEE80211_STA_NONE)
926 return mt76_sta_add(dev, vif, sta, ext_phy);
928 if (old_state == IEEE80211_STA_AUTH &&
929 new_state == IEEE80211_STA_ASSOC &&
931 dev->drv->sta_assoc(dev, vif, sta);
933 if (old_state == IEEE80211_STA_NONE &&
934 new_state == IEEE80211_STA_NOTEXIST)
935 mt76_sta_remove(dev, vif, sta);
939 EXPORT_SYMBOL_GPL(mt76_sta_state);
941 int mt76_get_txpower(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
944 struct mt76_dev *dev = hw->priv;
945 int n_chains = hweight8(dev->antenna_mask);
947 *dbm = DIV_ROUND_UP(dev->txpower_cur, 2);
949 /* convert from per-chain power to combined
968 EXPORT_SYMBOL_GPL(mt76_get_txpower);
971 __mt76_csa_finish(void *priv, u8 *mac, struct ieee80211_vif *vif)
973 if (vif->csa_active && ieee80211_csa_is_complete(vif))
974 ieee80211_csa_finish(vif);
977 void mt76_csa_finish(struct mt76_dev *dev)
979 if (!dev->csa_complete)
982 ieee80211_iterate_active_interfaces_atomic(dev->hw,
983 IEEE80211_IFACE_ITER_RESUME_ALL,
984 __mt76_csa_finish, dev);
986 dev->csa_complete = 0;
988 EXPORT_SYMBOL_GPL(mt76_csa_finish);
991 __mt76_csa_check(void *priv, u8 *mac, struct ieee80211_vif *vif)
993 struct mt76_dev *dev = priv;
995 if (!vif->csa_active)
998 dev->csa_complete |= ieee80211_csa_is_complete(vif);
1001 void mt76_csa_check(struct mt76_dev *dev)
1003 ieee80211_iterate_active_interfaces_atomic(dev->hw,
1004 IEEE80211_IFACE_ITER_RESUME_ALL,
1005 __mt76_csa_check, dev);
1007 EXPORT_SYMBOL_GPL(mt76_csa_check);
1010 mt76_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta, bool set)
1014 EXPORT_SYMBOL_GPL(mt76_set_tim);
1016 void mt76_insert_ccmp_hdr(struct sk_buff *skb, u8 key_id)
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;
1023 memmove(skb->data, skb->data + 8, hdr_len);
1024 hdr = skb->data + hdr_len;
1029 hdr[3] = 0x20 | (key_id << 6);
1035 status->flag &= ~RX_FLAG_IV_STRIPPED;
1037 EXPORT_SYMBOL_GPL(mt76_insert_ccmp_hdr);
1039 int mt76_get_rate(struct mt76_dev *dev,
1040 struct ieee80211_supported_band *sband,
1043 int i, offset = 0, len = sband->n_bitrates;
1046 if (sband == &dev->phy.sband_5g.sband)
1049 idx &= ~BIT(2); /* short preamble */
1050 } else if (sband == &dev->phy.sband_2g.sband) {
1054 for (i = offset; i < len; i++) {
1055 if ((sband->bitrates[i].hw_value & GENMASK(7, 0)) == idx)
1061 EXPORT_SYMBOL_GPL(mt76_get_rate);
1063 void mt76_sw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1066 struct mt76_phy *phy = hw->priv;
1068 set_bit(MT76_SCANNING, &phy->state);
1070 EXPORT_SYMBOL_GPL(mt76_sw_scan);
1072 void mt76_sw_scan_complete(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1074 struct mt76_phy *phy = hw->priv;
1076 clear_bit(MT76_SCANNING, &phy->state);
1078 EXPORT_SYMBOL_GPL(mt76_sw_scan_complete);
1080 int mt76_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
1082 struct mt76_dev *dev = hw->priv;
1084 mutex_lock(&dev->mutex);
1085 *tx_ant = dev->antenna_mask;
1086 *rx_ant = dev->antenna_mask;
1087 mutex_unlock(&dev->mutex);
1091 EXPORT_SYMBOL_GPL(mt76_get_antenna);