10e3d6777SRyder Lee // SPDX-License-Identifier: ISC
28d71aef9SStanislaw Gruszka /*
38d71aef9SStanislaw Gruszka * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
48d71aef9SStanislaw Gruszka * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
58d71aef9SStanislaw Gruszka * Copyright (C) 2018 Stanislaw Gruszka <stf_xl@wp.pl>
68d71aef9SStanislaw Gruszka */
78d71aef9SStanislaw Gruszka
88d71aef9SStanislaw Gruszka #include "mt76x02.h"
98d71aef9SStanislaw Gruszka
mt76x02_set_beacon_offsets(struct mt76x02_dev * dev)108d71aef9SStanislaw Gruszka static void mt76x02_set_beacon_offsets(struct mt76x02_dev *dev)
118d71aef9SStanislaw Gruszka {
128d71aef9SStanislaw Gruszka u32 regs[4] = {};
13f2276c29SStanislaw Gruszka u16 val;
148d71aef9SStanislaw Gruszka int i;
158d71aef9SStanislaw Gruszka
16f2276c29SStanislaw Gruszka for (i = 0; i < dev->beacon_ops->nslots; i++) {
17f2276c29SStanislaw Gruszka val = i * dev->beacon_ops->slot_size;
188d71aef9SStanislaw Gruszka regs[i / 4] |= (val / 64) << (8 * (i % 4));
198d71aef9SStanislaw Gruszka }
208d71aef9SStanislaw Gruszka
218d71aef9SStanislaw Gruszka for (i = 0; i < 4; i++)
228d71aef9SStanislaw Gruszka mt76_wr(dev, MT_BCN_OFFSET(i), regs[i]);
238d71aef9SStanislaw Gruszka }
248d71aef9SStanislaw Gruszka
258d71aef9SStanislaw Gruszka static int
mt76x02_write_beacon(struct mt76x02_dev * dev,int offset,struct sk_buff * skb)268d71aef9SStanislaw Gruszka mt76x02_write_beacon(struct mt76x02_dev *dev, int offset, struct sk_buff *skb)
278d71aef9SStanislaw Gruszka {
28f2276c29SStanislaw Gruszka int beacon_len = dev->beacon_ops->slot_size;
298d71aef9SStanislaw Gruszka
308d71aef9SStanislaw Gruszka if (WARN_ON_ONCE(beacon_len < skb->len + sizeof(struct mt76x02_txwi)))
318d71aef9SStanislaw Gruszka return -ENOSPC;
328d71aef9SStanislaw Gruszka
335c48e60eSMarkus Theil /* USB devices already reserve enough skb headroom for txwi's. This
345c48e60eSMarkus Theil * helps to save slow copies over USB.
355c48e60eSMarkus Theil */
365c48e60eSMarkus Theil if (mt76_is_usb(&dev->mt76)) {
375c48e60eSMarkus Theil struct mt76x02_txwi *txwi;
388d71aef9SStanislaw Gruszka
395c48e60eSMarkus Theil txwi = (struct mt76x02_txwi *)(skb->data - sizeof(*txwi));
405c48e60eSMarkus Theil mt76x02_mac_write_txwi(dev, txwi, skb, NULL, NULL, skb->len);
415c48e60eSMarkus Theil skb_push(skb, sizeof(*txwi));
425c48e60eSMarkus Theil } else {
435c48e60eSMarkus Theil struct mt76x02_txwi txwi;
445c48e60eSMarkus Theil
455c48e60eSMarkus Theil mt76x02_mac_write_txwi(dev, &txwi, skb, NULL, NULL, skb->len);
468d71aef9SStanislaw Gruszka mt76_wr_copy(dev, offset, &txwi, sizeof(txwi));
478d71aef9SStanislaw Gruszka offset += sizeof(txwi);
485c48e60eSMarkus Theil }
498d71aef9SStanislaw Gruszka
508d71aef9SStanislaw Gruszka mt76_wr_copy(dev, offset, skb->data, skb->len);
518d71aef9SStanislaw Gruszka return 0;
528d71aef9SStanislaw Gruszka }
538d71aef9SStanislaw Gruszka
mt76x02_mac_set_beacon(struct mt76x02_dev * dev,struct sk_buff * skb)54b74f98b8SStanislaw Gruszka void mt76x02_mac_set_beacon(struct mt76x02_dev *dev,
558d71aef9SStanislaw Gruszka struct sk_buff *skb)
568d71aef9SStanislaw Gruszka {
57f27469a9SMarkus Theil int bcn_len = dev->beacon_ops->slot_size;
58f27469a9SMarkus Theil int bcn_addr = MT_BEACON_BASE + (bcn_len * dev->beacon_data_count);
598d71aef9SStanislaw Gruszka
60*8916e4e5SFelix Fietkau if (!mt76x02_write_beacon(dev, bcn_addr, skb)) {
61*8916e4e5SFelix Fietkau if (!dev->beacon_data_count)
62*8916e4e5SFelix Fietkau dev->beacon_hang_check++;
63f27469a9SMarkus Theil dev->beacon_data_count++;
64*8916e4e5SFelix Fietkau }
65f27469a9SMarkus Theil dev_kfree_skb(skb);
668d71aef9SStanislaw Gruszka }
677e07c27dSStanislaw Gruszka EXPORT_SYMBOL_GPL(mt76x02_mac_set_beacon);
688d71aef9SStanislaw Gruszka
mt76x02_mac_set_beacon_enable(struct mt76x02_dev * dev,struct ieee80211_vif * vif,bool enable)698d71aef9SStanislaw Gruszka void mt76x02_mac_set_beacon_enable(struct mt76x02_dev *dev,
70d5160d86SLorenzo Bianconi struct ieee80211_vif *vif, bool enable)
718d71aef9SStanislaw Gruszka {
72d5160d86SLorenzo Bianconi struct mt76x02_vif *mvif = (struct mt76x02_vif *)vif->drv_priv;
73d5160d86SLorenzo Bianconi u8 old_mask = dev->mt76.beacon_mask;
748d71aef9SStanislaw Gruszka
751920a0ccSLorenzo Bianconi mt76x02_pre_tbtt_enable(dev, false);
76c004b881SStanislaw Gruszka
77c8a04d98SLorenzo Bianconi if (!dev->mt76.beacon_mask)
788d71aef9SStanislaw Gruszka dev->tbtt_count = 0;
798d71aef9SStanislaw Gruszka
80*8916e4e5SFelix Fietkau dev->beacon_hang_check = 0;
81d5160d86SLorenzo Bianconi if (enable) {
82d5160d86SLorenzo Bianconi dev->mt76.beacon_mask |= BIT(mvif->idx);
83d5160d86SLorenzo Bianconi } else {
84d5160d86SLorenzo Bianconi dev->mt76.beacon_mask &= ~BIT(mvif->idx);
85d5160d86SLorenzo Bianconi }
868d71aef9SStanislaw Gruszka
87d5160d86SLorenzo Bianconi if (!!old_mask == !!dev->mt76.beacon_mask)
88d5160d86SLorenzo Bianconi goto out;
89d5160d86SLorenzo Bianconi
90d5160d86SLorenzo Bianconi if (dev->mt76.beacon_mask)
91d5160d86SLorenzo Bianconi mt76_set(dev, MT_BEACON_TIME_CFG,
92d5160d86SLorenzo Bianconi MT_BEACON_TIME_CFG_BEACON_TX |
93d5160d86SLorenzo Bianconi MT_BEACON_TIME_CFG_TBTT_EN |
94d5160d86SLorenzo Bianconi MT_BEACON_TIME_CFG_TIMER_EN);
95d5160d86SLorenzo Bianconi else
96d5160d86SLorenzo Bianconi mt76_clear(dev, MT_BEACON_TIME_CFG,
97d5160d86SLorenzo Bianconi MT_BEACON_TIME_CFG_BEACON_TX |
98d5160d86SLorenzo Bianconi MT_BEACON_TIME_CFG_TBTT_EN |
99d5160d86SLorenzo Bianconi MT_BEACON_TIME_CFG_TIMER_EN);
100d5160d86SLorenzo Bianconi mt76x02_beacon_enable(dev, !!dev->mt76.beacon_mask);
101d5160d86SLorenzo Bianconi
102d5160d86SLorenzo Bianconi out:
1031920a0ccSLorenzo Bianconi mt76x02_pre_tbtt_enable(dev, true);
1048d71aef9SStanislaw Gruszka }
1058d71aef9SStanislaw Gruszka
10631cdd442SStanislaw Gruszka void
mt76x02_resync_beacon_timer(struct mt76x02_dev * dev)10731cdd442SStanislaw Gruszka mt76x02_resync_beacon_timer(struct mt76x02_dev *dev)
10831cdd442SStanislaw Gruszka {
1093041c445SLorenzo Bianconi u32 timer_val = dev->mt76.beacon_int << 4;
11031cdd442SStanislaw Gruszka
11131cdd442SStanislaw Gruszka dev->tbtt_count++;
11231cdd442SStanislaw Gruszka
11331cdd442SStanislaw Gruszka /*
11431cdd442SStanislaw Gruszka * Beacon timer drifts by 1us every tick, the timer is configured
11531cdd442SStanislaw Gruszka * in 1/16 TU (64us) units.
11631cdd442SStanislaw Gruszka */
11731cdd442SStanislaw Gruszka if (dev->tbtt_count < 63)
11831cdd442SStanislaw Gruszka return;
11931cdd442SStanislaw Gruszka
12031cdd442SStanislaw Gruszka /*
12131cdd442SStanislaw Gruszka * The updated beacon interval takes effect after two TBTT, because
12231cdd442SStanislaw Gruszka * at this point the original interval has already been loaded into
12331cdd442SStanislaw Gruszka * the next TBTT_TIMER value
12431cdd442SStanislaw Gruszka */
12531cdd442SStanislaw Gruszka if (dev->tbtt_count == 63)
12631cdd442SStanislaw Gruszka timer_val -= 1;
12731cdd442SStanislaw Gruszka
12831cdd442SStanislaw Gruszka mt76_rmw_field(dev, MT_BEACON_TIME_CFG,
12931cdd442SStanislaw Gruszka MT_BEACON_TIME_CFG_INTVAL, timer_val);
13031cdd442SStanislaw Gruszka
131c15b0f7cSLorenzo Bianconi if (dev->tbtt_count >= 64)
13231cdd442SStanislaw Gruszka dev->tbtt_count = 0;
13331cdd442SStanislaw Gruszka }
13431cdd442SStanislaw Gruszka EXPORT_SYMBOL_GPL(mt76x02_resync_beacon_timer);
13531cdd442SStanislaw Gruszka
13631cdd442SStanislaw Gruszka void
mt76x02_update_beacon_iter(void * priv,u8 * mac,struct ieee80211_vif * vif)13731cdd442SStanislaw Gruszka mt76x02_update_beacon_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
13831cdd442SStanislaw Gruszka {
13931cdd442SStanislaw Gruszka struct mt76x02_dev *dev = (struct mt76x02_dev *)priv;
14031cdd442SStanislaw Gruszka struct mt76x02_vif *mvif = (struct mt76x02_vif *)vif->drv_priv;
14131cdd442SStanislaw Gruszka struct sk_buff *skb = NULL;
14231cdd442SStanislaw Gruszka
143c8a04d98SLorenzo Bianconi if (!(dev->mt76.beacon_mask & BIT(mvif->idx)))
14431cdd442SStanislaw Gruszka return;
14531cdd442SStanislaw Gruszka
1466e8912a5SShaul Triebitz skb = ieee80211_beacon_get(mt76_hw(dev), vif, 0);
14731cdd442SStanislaw Gruszka if (!skb)
14831cdd442SStanislaw Gruszka return;
14931cdd442SStanislaw Gruszka
150f27469a9SMarkus Theil mt76x02_mac_set_beacon(dev, skb);
15131cdd442SStanislaw Gruszka }
15231cdd442SStanislaw Gruszka EXPORT_SYMBOL_GPL(mt76x02_update_beacon_iter);
15331cdd442SStanislaw Gruszka
15431cdd442SStanislaw Gruszka static void
mt76x02_add_buffered_bc(void * priv,u8 * mac,struct ieee80211_vif * vif)15531cdd442SStanislaw Gruszka mt76x02_add_buffered_bc(void *priv, u8 *mac, struct ieee80211_vif *vif)
15631cdd442SStanislaw Gruszka {
15731cdd442SStanislaw Gruszka struct beacon_bc_data *data = priv;
15831cdd442SStanislaw Gruszka struct mt76x02_dev *dev = data->dev;
15931cdd442SStanislaw Gruszka struct mt76x02_vif *mvif = (struct mt76x02_vif *)vif->drv_priv;
16031cdd442SStanislaw Gruszka struct ieee80211_tx_info *info;
16131cdd442SStanislaw Gruszka struct sk_buff *skb;
16231cdd442SStanislaw Gruszka
163c8a04d98SLorenzo Bianconi if (!(dev->mt76.beacon_mask & BIT(mvif->idx)))
16431cdd442SStanislaw Gruszka return;
16531cdd442SStanislaw Gruszka
16631cdd442SStanislaw Gruszka skb = ieee80211_get_buffered_bc(mt76_hw(dev), vif);
16731cdd442SStanislaw Gruszka if (!skb)
16831cdd442SStanislaw Gruszka return;
16931cdd442SStanislaw Gruszka
17031cdd442SStanislaw Gruszka info = IEEE80211_SKB_CB(skb);
17131cdd442SStanislaw Gruszka info->control.vif = vif;
17231cdd442SStanislaw Gruszka info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
17331cdd442SStanislaw Gruszka mt76_skb_set_moredata(skb, true);
17431cdd442SStanislaw Gruszka __skb_queue_tail(&data->q, skb);
17531cdd442SStanislaw Gruszka data->tail[mvif->idx] = skb;
17631cdd442SStanislaw Gruszka }
17731cdd442SStanislaw Gruszka
17831cdd442SStanislaw Gruszka void
mt76x02_enqueue_buffered_bc(struct mt76x02_dev * dev,struct beacon_bc_data * data,int max_nframes)179ff97c52aSRyder Lee mt76x02_enqueue_buffered_bc(struct mt76x02_dev *dev,
180ff97c52aSRyder Lee struct beacon_bc_data *data,
18131cdd442SStanislaw Gruszka int max_nframes)
18231cdd442SStanislaw Gruszka {
18331cdd442SStanislaw Gruszka int i, nframes;
18431cdd442SStanislaw Gruszka
18531cdd442SStanislaw Gruszka data->dev = dev;
18631cdd442SStanislaw Gruszka __skb_queue_head_init(&data->q);
18731cdd442SStanislaw Gruszka
18831cdd442SStanislaw Gruszka do {
18931cdd442SStanislaw Gruszka nframes = skb_queue_len(&data->q);
19031cdd442SStanislaw Gruszka ieee80211_iterate_active_interfaces_atomic(mt76_hw(dev),
19131cdd442SStanislaw Gruszka IEEE80211_IFACE_ITER_RESUME_ALL,
19231cdd442SStanislaw Gruszka mt76x02_add_buffered_bc, data);
19331cdd442SStanislaw Gruszka } while (nframes != skb_queue_len(&data->q) &&
19431cdd442SStanislaw Gruszka skb_queue_len(&data->q) < max_nframes);
19531cdd442SStanislaw Gruszka
19631cdd442SStanislaw Gruszka if (!skb_queue_len(&data->q))
19731cdd442SStanislaw Gruszka return;
19831cdd442SStanislaw Gruszka
19931cdd442SStanislaw Gruszka for (i = 0; i < ARRAY_SIZE(data->tail); i++) {
20031cdd442SStanislaw Gruszka if (!data->tail[i])
20131cdd442SStanislaw Gruszka continue;
20231cdd442SStanislaw Gruszka mt76_skb_set_moredata(data->tail[i], false);
20331cdd442SStanislaw Gruszka }
20431cdd442SStanislaw Gruszka }
20531cdd442SStanislaw Gruszka EXPORT_SYMBOL_GPL(mt76x02_enqueue_buffered_bc);
20631cdd442SStanislaw Gruszka
mt76x02_init_beacon_config(struct mt76x02_dev * dev)2078d71aef9SStanislaw Gruszka void mt76x02_init_beacon_config(struct mt76x02_dev *dev)
2088d71aef9SStanislaw Gruszka {
2098d71aef9SStanislaw Gruszka mt76_clear(dev, MT_BEACON_TIME_CFG, (MT_BEACON_TIME_CFG_TIMER_EN |
2108d71aef9SStanislaw Gruszka MT_BEACON_TIME_CFG_TBTT_EN |
2118d71aef9SStanislaw Gruszka MT_BEACON_TIME_CFG_BEACON_TX));
2128d71aef9SStanislaw Gruszka mt76_set(dev, MT_BEACON_TIME_CFG, MT_BEACON_TIME_CFG_SYNC_MODE);
2138d71aef9SStanislaw Gruszka mt76_wr(dev, MT_BCN_BYPASS_MASK, 0xffff);
2148d71aef9SStanislaw Gruszka mt76x02_set_beacon_offsets(dev);
2158d71aef9SStanislaw Gruszka }
2168d71aef9SStanislaw Gruszka EXPORT_SYMBOL_GPL(mt76x02_init_beacon_config);
2178d71aef9SStanislaw Gruszka
218