1e57b7901SRyder Lee // SPDX-License-Identifier: ISC
2e57b7901SRyder Lee /* Copyright (C) 2020 MediaTek Inc. */
3e57b7901SRyder Lee
4e57b7901SRyder Lee #include <linux/etherdevice.h>
5e57b7901SRyder Lee #include <linux/timekeeping.h>
64dbcb912SRyder Lee #include "coredump.h"
7e57b7901SRyder Lee #include "mt7915.h"
8e57b7901SRyder Lee #include "../dma.h"
9e57b7901SRyder Lee #include "mac.h"
103782b69dSLorenzo Bianconi #include "mcu.h"
11e57b7901SRyder Lee
12a71b648eSRyder Lee #define to_rssi(field, rcpi) ((FIELD_GET(field, rcpi) - 220) / 2)
13e57b7901SRyder Lee
14e57b7901SRyder Lee static const struct mt7915_dfs_radar_spec etsi_radar_specs = {
15e57b7901SRyder Lee .pulse_th = { 110, -10, -80, 40, 5200, 128, 5200 },
16e57b7901SRyder Lee .radar_pattern = {
17e57b7901SRyder Lee [5] = { 1, 0, 6, 32, 28, 0, 990, 5010, 17, 1, 1 },
18e57b7901SRyder Lee [6] = { 1, 0, 9, 32, 28, 0, 615, 5010, 27, 1, 1 },
19e57b7901SRyder Lee [7] = { 1, 0, 15, 32, 28, 0, 240, 445, 27, 1, 1 },
20e57b7901SRyder Lee [8] = { 1, 0, 12, 32, 28, 0, 240, 510, 42, 1, 1 },
21e57b7901SRyder Lee [9] = { 1, 1, 0, 0, 0, 0, 2490, 3343, 14, 0, 0, 12, 32, 28, { }, 126 },
22e57b7901SRyder Lee [10] = { 1, 1, 0, 0, 0, 0, 2490, 3343, 14, 0, 0, 15, 32, 24, { }, 126 },
23e57b7901SRyder Lee [11] = { 1, 1, 0, 0, 0, 0, 823, 2510, 14, 0, 0, 18, 32, 28, { }, 54 },
24e57b7901SRyder Lee [12] = { 1, 1, 0, 0, 0, 0, 823, 2510, 14, 0, 0, 27, 32, 24, { }, 54 },
25e57b7901SRyder Lee },
26e57b7901SRyder Lee };
27e57b7901SRyder Lee
28e57b7901SRyder Lee static const struct mt7915_dfs_radar_spec fcc_radar_specs = {
29e57b7901SRyder Lee .pulse_th = { 110, -10, -80, 40, 5200, 128, 5200 },
30e57b7901SRyder Lee .radar_pattern = {
31e57b7901SRyder Lee [0] = { 1, 0, 8, 32, 28, 0, 508, 3076, 13, 1, 1 },
32e57b7901SRyder Lee [1] = { 1, 0, 12, 32, 28, 0, 140, 240, 17, 1, 1 },
33e57b7901SRyder Lee [2] = { 1, 0, 8, 32, 28, 0, 190, 510, 22, 1, 1 },
34e57b7901SRyder Lee [3] = { 1, 0, 6, 32, 28, 0, 190, 510, 32, 1, 1 },
35e57b7901SRyder Lee [4] = { 1, 0, 9, 255, 28, 0, 323, 343, 13, 1, 32 },
36e57b7901SRyder Lee },
37e57b7901SRyder Lee };
38e57b7901SRyder Lee
39e57b7901SRyder Lee static const struct mt7915_dfs_radar_spec jp_radar_specs = {
40e57b7901SRyder Lee .pulse_th = { 110, -10, -80, 40, 5200, 128, 5200 },
41e57b7901SRyder Lee .radar_pattern = {
42e57b7901SRyder Lee [0] = { 1, 0, 8, 32, 28, 0, 508, 3076, 13, 1, 1 },
43e57b7901SRyder Lee [1] = { 1, 0, 12, 32, 28, 0, 140, 240, 17, 1, 1 },
44e57b7901SRyder Lee [2] = { 1, 0, 8, 32, 28, 0, 190, 510, 22, 1, 1 },
45e57b7901SRyder Lee [3] = { 1, 0, 6, 32, 28, 0, 190, 510, 32, 1, 1 },
46e57b7901SRyder Lee [4] = { 1, 0, 9, 255, 28, 0, 323, 343, 13, 1, 32 },
47e57b7901SRyder Lee [13] = { 1, 0, 7, 32, 28, 0, 3836, 3856, 14, 1, 1 },
48e57b7901SRyder Lee [14] = { 1, 0, 6, 32, 28, 0, 615, 5010, 110, 1, 1 },
49e57b7901SRyder Lee [15] = { 1, 1, 0, 0, 0, 0, 15, 5010, 110, 0, 0, 12, 32, 28 },
50e57b7901SRyder Lee },
51e57b7901SRyder Lee };
52e57b7901SRyder Lee
mt7915_rx_get_wcid(struct mt7915_dev * dev,u16 idx,bool unicast)53e57b7901SRyder Lee static struct mt76_wcid *mt7915_rx_get_wcid(struct mt7915_dev *dev,
54e57b7901SRyder Lee u16 idx, bool unicast)
55e57b7901SRyder Lee {
56e57b7901SRyder Lee struct mt7915_sta *sta;
57e57b7901SRyder Lee struct mt76_wcid *wcid;
58e57b7901SRyder Lee
59e57b7901SRyder Lee if (idx >= ARRAY_SIZE(dev->mt76.wcid))
60e57b7901SRyder Lee return NULL;
61e57b7901SRyder Lee
62e57b7901SRyder Lee wcid = rcu_dereference(dev->mt76.wcid[idx]);
63e57b7901SRyder Lee if (unicast || !wcid)
64e57b7901SRyder Lee return wcid;
65e57b7901SRyder Lee
66e57b7901SRyder Lee if (!wcid->sta)
67e57b7901SRyder Lee return NULL;
68e57b7901SRyder Lee
69e57b7901SRyder Lee sta = container_of(wcid, struct mt7915_sta, wcid);
70e57b7901SRyder Lee if (!sta->vif)
71e57b7901SRyder Lee return NULL;
72e57b7901SRyder Lee
73e57b7901SRyder Lee return &sta->vif->sta.wcid;
74e57b7901SRyder Lee }
75e57b7901SRyder Lee
mt7915_mac_wtbl_update(struct mt7915_dev * dev,int idx,u32 mask)76e57b7901SRyder Lee bool mt7915_mac_wtbl_update(struct mt7915_dev *dev, int idx, u32 mask)
77e57b7901SRyder Lee {
78e57b7901SRyder Lee mt76_rmw(dev, MT_WTBL_UPDATE, MT_WTBL_UPDATE_WLAN_IDX,
79e57b7901SRyder Lee FIELD_PREP(MT_WTBL_UPDATE_WLAN_IDX, idx) | mask);
80e57b7901SRyder Lee
81e57b7901SRyder Lee return mt76_poll(dev, MT_WTBL_UPDATE, MT_WTBL_UPDATE_BUSY,
82e57b7901SRyder Lee 0, 5000);
83e57b7901SRyder Lee }
84e57b7901SRyder Lee
mt7915_mac_wtbl_lmac_addr(struct mt7915_dev * dev,u16 wcid,u8 dw)8570fd1333SRyder Lee u32 mt7915_mac_wtbl_lmac_addr(struct mt7915_dev *dev, u16 wcid, u8 dw)
86e57b7901SRyder Lee {
87e57b7901SRyder Lee mt76_wr(dev, MT_WTBLON_TOP_WDUCR,
88e57b7901SRyder Lee FIELD_PREP(MT_WTBLON_TOP_WDUCR_GROUP, (wcid >> 7)));
89e57b7901SRyder Lee
909908d98aSRyder Lee return MT_WTBL_LMAC_OFFS(wcid, dw);
91e57b7901SRyder Lee }
92e57b7901SRyder Lee
mt7915_mac_sta_poll(struct mt7915_dev * dev)930f1c443cSFelix Fietkau static void mt7915_mac_sta_poll(struct mt7915_dev *dev)
94e57b7901SRyder Lee {
95e57b7901SRyder Lee static const u8 ac_to_tid[] = {
96e57b7901SRyder Lee [IEEE80211_AC_BE] = 0,
97e57b7901SRyder Lee [IEEE80211_AC_BK] = 1,
98e57b7901SRyder Lee [IEEE80211_AC_VI] = 4,
99e57b7901SRyder Lee [IEEE80211_AC_VO] = 6
100e57b7901SRyder Lee };
101e57b7901SRyder Lee struct ieee80211_sta *sta;
102e57b7901SRyder Lee struct mt7915_sta *msta;
1039908d98aSRyder Lee struct rate_info *rate;
104e57b7901SRyder Lee u32 tx_time[IEEE80211_NUM_ACS], rx_time[IEEE80211_NUM_ACS];
1050f1c443cSFelix Fietkau LIST_HEAD(sta_poll_list);
106e57b7901SRyder Lee int i;
107e57b7901SRyder Lee
108fbba711cSLorenzo Bianconi spin_lock_bh(&dev->mt76.sta_poll_lock);
109fbba711cSLorenzo Bianconi list_splice_init(&dev->mt76.sta_poll_list, &sta_poll_list);
110fbba711cSLorenzo Bianconi spin_unlock_bh(&dev->mt76.sta_poll_lock);
1110f1c443cSFelix Fietkau
112e57b7901SRyder Lee rcu_read_lock();
113e57b7901SRyder Lee
114e57b7901SRyder Lee while (true) {
115e57b7901SRyder Lee bool clear = false;
1169908d98aSRyder Lee u32 addr, val;
117e57b7901SRyder Lee u16 idx;
118a71b648eSRyder Lee s8 rssi[4];
1199908d98aSRyder Lee u8 bw;
120e57b7901SRyder Lee
121fbba711cSLorenzo Bianconi spin_lock_bh(&dev->mt76.sta_poll_lock);
1220f1c443cSFelix Fietkau if (list_empty(&sta_poll_list)) {
123fbba711cSLorenzo Bianconi spin_unlock_bh(&dev->mt76.sta_poll_lock);
124e57b7901SRyder Lee break;
125e57b7901SRyder Lee }
1260f1c443cSFelix Fietkau msta = list_first_entry(&sta_poll_list,
127b73e1d92SLorenzo Bianconi struct mt7915_sta, wcid.poll_list);
128b73e1d92SLorenzo Bianconi list_del_init(&msta->wcid.poll_list);
129fbba711cSLorenzo Bianconi spin_unlock_bh(&dev->mt76.sta_poll_lock);
130e57b7901SRyder Lee
13168e6644bSFelix Fietkau idx = msta->wcid.idx;
132a71b648eSRyder Lee
133a71b648eSRyder Lee /* refresh peer's airtime reporting */
1349908d98aSRyder Lee addr = mt7915_mac_wtbl_lmac_addr(dev, idx, 20);
135e57b7901SRyder Lee
13668e6644bSFelix Fietkau for (i = 0; i < IEEE80211_NUM_ACS; i++) {
13768e6644bSFelix Fietkau u32 tx_last = msta->airtime_ac[i];
13868e6644bSFelix Fietkau u32 rx_last = msta->airtime_ac[i + 4];
13968e6644bSFelix Fietkau
14068e6644bSFelix Fietkau msta->airtime_ac[i] = mt76_rr(dev, addr);
14168e6644bSFelix Fietkau msta->airtime_ac[i + 4] = mt76_rr(dev, addr + 4);
14268e6644bSFelix Fietkau
14329693184SHenry Yen if (msta->airtime_ac[i] <= tx_last)
14429693184SHenry Yen tx_time[i] = 0;
14529693184SHenry Yen else
146e57b7901SRyder Lee tx_time[i] = msta->airtime_ac[i] - tx_last;
14729693184SHenry Yen
14829693184SHenry Yen if (msta->airtime_ac[i + 4] <= rx_last)
14929693184SHenry Yen rx_time[i] = 0;
15029693184SHenry Yen else
15168e6644bSFelix Fietkau rx_time[i] = msta->airtime_ac[i + 4] - rx_last;
152e57b7901SRyder Lee
153e57b7901SRyder Lee if ((tx_last | rx_last) & BIT(30))
154e57b7901SRyder Lee clear = true;
15568e6644bSFelix Fietkau
15668e6644bSFelix Fietkau addr += 8;
157e57b7901SRyder Lee }
158e57b7901SRyder Lee
159e57b7901SRyder Lee if (clear) {
160e57b7901SRyder Lee mt7915_mac_wtbl_update(dev, idx,
161e57b7901SRyder Lee MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
162e57b7901SRyder Lee memset(msta->airtime_ac, 0, sizeof(msta->airtime_ac));
163e57b7901SRyder Lee }
164e57b7901SRyder Lee
165e57b7901SRyder Lee if (!msta->wcid.sta)
166e57b7901SRyder Lee continue;
167e57b7901SRyder Lee
168e57b7901SRyder Lee sta = container_of((void *)msta, struct ieee80211_sta,
169e57b7901SRyder Lee drv_priv);
170e57b7901SRyder Lee for (i = 0; i < IEEE80211_NUM_ACS; i++) {
171c3137942SSujuan Chen u8 queue = mt76_connac_lmac_mapping(i);
172c3137942SSujuan Chen u32 tx_cur = tx_time[queue];
173c3137942SSujuan Chen u32 rx_cur = rx_time[queue];
174e57b7901SRyder Lee u8 tid = ac_to_tid[i];
175e57b7901SRyder Lee
176e57b7901SRyder Lee if (!tx_cur && !rx_cur)
177e57b7901SRyder Lee continue;
178e57b7901SRyder Lee
179e57b7901SRyder Lee ieee80211_sta_register_airtime(sta, tid, tx_cur,
180e57b7901SRyder Lee rx_cur);
181e57b7901SRyder Lee }
1829908d98aSRyder Lee
1839908d98aSRyder Lee /*
1849908d98aSRyder Lee * We don't support reading GI info from txs packets.
1859908d98aSRyder Lee * For accurate tx status reporting and AQL improvement,
18643eaa368SRyder Lee * we need to make sure that flags match so polling GI
1879908d98aSRyder Lee * from per-sta counters directly.
1889908d98aSRyder Lee */
1899908d98aSRyder Lee rate = &msta->wcid.rate;
1909908d98aSRyder Lee addr = mt7915_mac_wtbl_lmac_addr(dev, idx, 7);
1919908d98aSRyder Lee val = mt76_rr(dev, addr);
1929908d98aSRyder Lee
1939908d98aSRyder Lee switch (rate->bw) {
1949908d98aSRyder Lee case RATE_INFO_BW_160:
1959908d98aSRyder Lee bw = IEEE80211_STA_RX_BW_160;
1969908d98aSRyder Lee break;
1979908d98aSRyder Lee case RATE_INFO_BW_80:
1989908d98aSRyder Lee bw = IEEE80211_STA_RX_BW_80;
1999908d98aSRyder Lee break;
2009908d98aSRyder Lee case RATE_INFO_BW_40:
2019908d98aSRyder Lee bw = IEEE80211_STA_RX_BW_40;
2029908d98aSRyder Lee break;
2039908d98aSRyder Lee default:
2049908d98aSRyder Lee bw = IEEE80211_STA_RX_BW_20;
2059908d98aSRyder Lee break;
2069908d98aSRyder Lee }
2079908d98aSRyder Lee
2089908d98aSRyder Lee if (rate->flags & RATE_INFO_FLAGS_HE_MCS) {
2099908d98aSRyder Lee u8 offs = 24 + 2 * bw;
2109908d98aSRyder Lee
2119908d98aSRyder Lee rate->he_gi = (val & (0x3 << offs)) >> offs;
2129908d98aSRyder Lee } else if (rate->flags &
2139908d98aSRyder Lee (RATE_INFO_FLAGS_VHT_MCS | RATE_INFO_FLAGS_MCS)) {
2149908d98aSRyder Lee if (val & BIT(12 + bw))
2159908d98aSRyder Lee rate->flags |= RATE_INFO_FLAGS_SHORT_GI;
2169908d98aSRyder Lee else
2179908d98aSRyder Lee rate->flags &= ~RATE_INFO_FLAGS_SHORT_GI;
2189908d98aSRyder Lee }
219a71b648eSRyder Lee
220a71b648eSRyder Lee /* get signal strength of resp frames (CTS/BA/ACK) */
221a71b648eSRyder Lee addr = mt7915_mac_wtbl_lmac_addr(dev, idx, 30);
222a71b648eSRyder Lee val = mt76_rr(dev, addr);
223a71b648eSRyder Lee
224a71b648eSRyder Lee rssi[0] = to_rssi(GENMASK(7, 0), val);
225a71b648eSRyder Lee rssi[1] = to_rssi(GENMASK(15, 8), val);
226a71b648eSRyder Lee rssi[2] = to_rssi(GENMASK(23, 16), val);
227a71b648eSRyder Lee rssi[3] = to_rssi(GENMASK(31, 14), val);
228a71b648eSRyder Lee
229a71b648eSRyder Lee msta->ack_signal =
230a71b648eSRyder Lee mt76_rx_signal(msta->vif->phy->mt76->antenna_mask, rssi);
231a71b648eSRyder Lee
232a71b648eSRyder Lee ewma_avg_signal_add(&msta->avg_ack_signal, -msta->ack_signal);
233e57b7901SRyder Lee }
234e57b7901SRyder Lee
235e57b7901SRyder Lee rcu_read_unlock();
236e57b7901SRyder Lee }
237e57b7901SRyder Lee
mt7915_mac_enable_rtscts(struct mt7915_dev * dev,struct ieee80211_vif * vif,bool enable)238150b9141SRyder Lee void mt7915_mac_enable_rtscts(struct mt7915_dev *dev,
239150b9141SRyder Lee struct ieee80211_vif *vif, bool enable)
240150b9141SRyder Lee {
241150b9141SRyder Lee struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
242150b9141SRyder Lee u32 addr;
243150b9141SRyder Lee
244150b9141SRyder Lee addr = mt7915_mac_wtbl_lmac_addr(dev, mvif->sta.wcid.idx, 5);
245150b9141SRyder Lee if (enable)
246150b9141SRyder Lee mt76_set(dev, addr, BIT(5));
247150b9141SRyder Lee else
248150b9141SRyder Lee mt76_clear(dev, addr, BIT(5));
249150b9141SRyder Lee }
250150b9141SRyder Lee
251c3137942SSujuan Chen static void
mt7915_wed_check_ppe(struct mt7915_dev * dev,struct mt76_queue * q,struct mt7915_sta * msta,struct sk_buff * skb,u32 info)252c3137942SSujuan Chen mt7915_wed_check_ppe(struct mt7915_dev *dev, struct mt76_queue *q,
253c3137942SSujuan Chen struct mt7915_sta *msta, struct sk_buff *skb,
254c3137942SSujuan Chen u32 info)
255c3137942SSujuan Chen {
256c3137942SSujuan Chen struct ieee80211_vif *vif;
257c3137942SSujuan Chen struct wireless_dev *wdev;
258c3137942SSujuan Chen
259c3137942SSujuan Chen if (!msta || !msta->vif)
260c3137942SSujuan Chen return;
261c3137942SSujuan Chen
26258bcd4edSLorenzo Bianconi if (!mt76_queue_is_wed_rx(q))
263c3137942SSujuan Chen return;
264c3137942SSujuan Chen
265c3137942SSujuan Chen if (!(info & MT_DMA_INFO_PPE_VLD))
266c3137942SSujuan Chen return;
267c3137942SSujuan Chen
268c3137942SSujuan Chen vif = container_of((void *)msta->vif, struct ieee80211_vif,
269c3137942SSujuan Chen drv_priv);
270c3137942SSujuan Chen wdev = ieee80211_vif_to_wdev(vif);
271c3137942SSujuan Chen skb->dev = wdev->netdev;
272c3137942SSujuan Chen
273c3137942SSujuan Chen mtk_wed_device_ppe_check(&dev->mt76.mmio.wed, skb,
274c3137942SSujuan Chen FIELD_GET(MT_DMA_PPE_CPU_REASON, info),
275c3137942SSujuan Chen FIELD_GET(MT_DMA_PPE_ENTRY, info));
276c3137942SSujuan Chen }
277c3137942SSujuan Chen
278338330bdSFelix Fietkau static int
mt7915_mac_fill_rx(struct mt7915_dev * dev,struct sk_buff * skb,enum mt76_rxq_id q,u32 * info)279c3137942SSujuan Chen mt7915_mac_fill_rx(struct mt7915_dev *dev, struct sk_buff *skb,
280c3137942SSujuan Chen enum mt76_rxq_id q, u32 *info)
281e57b7901SRyder Lee {
282e57b7901SRyder Lee struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
283e57b7901SRyder Lee struct mt76_phy *mphy = &dev->mt76.phy;
284e57b7901SRyder Lee struct mt7915_phy *phy = &dev->phy;
285e57b7901SRyder Lee struct ieee80211_supported_band *sband;
286e57b7901SRyder Lee __le32 *rxd = (__le32 *)skb->data;
2870d4b6909SRyder Lee __le32 *rxv = NULL;
28894244d2eSFelix Fietkau u32 rxd0 = le32_to_cpu(rxd[0]);
289e57b7901SRyder Lee u32 rxd1 = le32_to_cpu(rxd[1]);
290e57b7901SRyder Lee u32 rxd2 = le32_to_cpu(rxd[2]);
291e57b7901SRyder Lee u32 rxd3 = le32_to_cpu(rxd[3]);
292cc4b3c13SLorenzo Bianconi u32 rxd4 = le32_to_cpu(rxd[4]);
29394244d2eSFelix Fietkau u32 csum_mask = MT_RXD0_NORMAL_IP_SUM | MT_RXD0_NORMAL_UDP_TCP_SUM;
294e57b7901SRyder Lee bool unicast, insert_ccmp_hdr = false;
295cc4b3c13SLorenzo Bianconi u8 remove_pad, amsdu_info;
29605268cf1SLorenzo Bianconi u8 mode = 0, qos_ctl = 0;
297b5ee771cSDan Carpenter struct mt7915_sta *msta = NULL;
298443dc85aSFelix Fietkau u32 csum_status = *(u32 *)skb->cb;
29990e3abf0SFelix Fietkau bool hdr_trans;
300dc5399a5SXing Song u16 hdr_gap;
30190e3abf0SFelix Fietkau u16 seq_ctrl = 0;
30290e3abf0SFelix Fietkau __le16 fc = 0;
3034550fb9eSFelix Fietkau int idx;
304e57b7901SRyder Lee
305e57b7901SRyder Lee memset(status, 0, sizeof(*status));
306e57b7901SRyder Lee
3073eb50cc9SRyder Lee if ((rxd1 & MT_RXD1_NORMAL_BAND_IDX) && !phy->mt76->band_idx) {
308dc44c45cSLorenzo Bianconi mphy = dev->mt76.phys[MT_BAND1];
309e57b7901SRyder Lee if (!mphy)
310e57b7901SRyder Lee return -EINVAL;
311e57b7901SRyder Lee
312e57b7901SRyder Lee phy = mphy->priv;
313128c9b7dSLorenzo Bianconi status->phy_idx = 1;
314e57b7901SRyder Lee }
315e57b7901SRyder Lee
316e57b7901SRyder Lee if (!test_bit(MT76_STATE_RUNNING, &mphy->state))
317e57b7901SRyder Lee return -EINVAL;
318e57b7901SRyder Lee
319cc4b3c13SLorenzo Bianconi if (rxd2 & MT_RXD2_NORMAL_AMSDU_ERR)
320cc4b3c13SLorenzo Bianconi return -EINVAL;
321cc4b3c13SLorenzo Bianconi
322dd28dea5SXing Song hdr_trans = rxd2 & MT_RXD2_NORMAL_HDR_TRANS;
323dd28dea5SXing Song if (hdr_trans && (rxd1 & MT_RXD1_NORMAL_CM))
324dd28dea5SXing Song return -EINVAL;
325dd28dea5SXing Song
326dd28dea5SXing Song /* ICV error or CCMP/BIP/WPI MIC error */
327dd28dea5SXing Song if (rxd1 & MT_RXD1_NORMAL_ICV_ERR)
328dd28dea5SXing Song status->flag |= RX_FLAG_ONLY_MONITOR;
329dd28dea5SXing Song
330e57b7901SRyder Lee unicast = FIELD_GET(MT_RXD3_NORMAL_ADDR_TYPE, rxd3) == MT_RXD3_NORMAL_U2M;
331e57b7901SRyder Lee idx = FIELD_GET(MT_RXD1_NORMAL_WLAN_IDX, rxd1);
332e57b7901SRyder Lee status->wcid = mt7915_rx_get_wcid(dev, idx, unicast);
333e57b7901SRyder Lee
334e57b7901SRyder Lee if (status->wcid) {
335e57b7901SRyder Lee msta = container_of(status->wcid, struct mt7915_sta, wcid);
336fbba711cSLorenzo Bianconi spin_lock_bh(&dev->mt76.sta_poll_lock);
337b73e1d92SLorenzo Bianconi if (list_empty(&msta->wcid.poll_list))
338b73e1d92SLorenzo Bianconi list_add_tail(&msta->wcid.poll_list,
339fbba711cSLorenzo Bianconi &dev->mt76.sta_poll_list);
340fbba711cSLorenzo Bianconi spin_unlock_bh(&dev->mt76.sta_poll_lock);
341e57b7901SRyder Lee }
342e57b7901SRyder Lee
343e57b7901SRyder Lee status->freq = mphy->chandef.chan->center_freq;
344e57b7901SRyder Lee status->band = mphy->chandef.chan->band;
345e57b7901SRyder Lee if (status->band == NL80211_BAND_5GHZ)
346e57b7901SRyder Lee sband = &mphy->sband_5g.sband;
347b4d093e3SMeiChia Chiu else if (status->band == NL80211_BAND_6GHZ)
348b4d093e3SMeiChia Chiu sband = &mphy->sband_6g.sband;
349e57b7901SRyder Lee else
350e57b7901SRyder Lee sband = &mphy->sband_2g.sband;
351e57b7901SRyder Lee
352e57b7901SRyder Lee if (!sband->channels)
353e57b7901SRyder Lee return -EINVAL;
354e57b7901SRyder Lee
355443dc85aSFelix Fietkau if ((rxd0 & csum_mask) == csum_mask &&
356443dc85aSFelix Fietkau !(csum_status & (BIT(0) | BIT(2) | BIT(3))))
35794244d2eSFelix Fietkau skb->ip_summed = CHECKSUM_UNNECESSARY;
35894244d2eSFelix Fietkau
359e57b7901SRyder Lee if (rxd1 & MT_RXD1_NORMAL_FCS_ERR)
360e57b7901SRyder Lee status->flag |= RX_FLAG_FAILED_FCS_CRC;
361e57b7901SRyder Lee
362e57b7901SRyder Lee if (rxd1 & MT_RXD1_NORMAL_TKIP_MIC_ERR)
363e57b7901SRyder Lee status->flag |= RX_FLAG_MMIC_ERROR;
364e57b7901SRyder Lee
365e57b7901SRyder Lee if (FIELD_GET(MT_RXD1_NORMAL_SEC_MODE, rxd1) != 0 &&
366e57b7901SRyder Lee !(rxd1 & (MT_RXD1_NORMAL_CLM | MT_RXD1_NORMAL_CM))) {
367e57b7901SRyder Lee status->flag |= RX_FLAG_DECRYPTED;
368e57b7901SRyder Lee status->flag |= RX_FLAG_IV_STRIPPED;
369e57b7901SRyder Lee status->flag |= RX_FLAG_MMIC_STRIPPED | RX_FLAG_MIC_STRIPPED;
370e57b7901SRyder Lee }
371e57b7901SRyder Lee
372e57b7901SRyder Lee remove_pad = FIELD_GET(MT_RXD2_NORMAL_HDR_OFFSET, rxd2);
373e57b7901SRyder Lee
374e57b7901SRyder Lee if (rxd2 & MT_RXD2_NORMAL_MAX_LEN_ERROR)
375e57b7901SRyder Lee return -EINVAL;
376e57b7901SRyder Lee
377e57b7901SRyder Lee rxd += 6;
378e57b7901SRyder Lee if (rxd1 & MT_RXD1_NORMAL_GROUP_4) {
37990e3abf0SFelix Fietkau u32 v0 = le32_to_cpu(rxd[0]);
38090e3abf0SFelix Fietkau u32 v2 = le32_to_cpu(rxd[2]);
38190e3abf0SFelix Fietkau
38290e3abf0SFelix Fietkau fc = cpu_to_le16(FIELD_GET(MT_RXD6_FRAME_CONTROL, v0));
38390e3abf0SFelix Fietkau qos_ctl = FIELD_GET(MT_RXD8_QOS_CTL, v2);
38490e3abf0SFelix Fietkau seq_ctrl = FIELD_GET(MT_RXD8_SEQ_CTRL, v2);
38590e3abf0SFelix Fietkau
386e57b7901SRyder Lee rxd += 4;
387e57b7901SRyder Lee if ((u8 *)rxd - skb->data >= skb->len)
388e57b7901SRyder Lee return -EINVAL;
389e57b7901SRyder Lee }
390e57b7901SRyder Lee
391e57b7901SRyder Lee if (rxd1 & MT_RXD1_NORMAL_GROUP_1) {
392e57b7901SRyder Lee u8 *data = (u8 *)rxd;
393e57b7901SRyder Lee
394e57b7901SRyder Lee if (status->flag & RX_FLAG_DECRYPTED) {
395c368362cSRyder Lee switch (FIELD_GET(MT_RXD1_NORMAL_SEC_MODE, rxd1)) {
396c368362cSRyder Lee case MT_CIPHER_AES_CCMP:
397c368362cSRyder Lee case MT_CIPHER_CCMP_CCX:
398c368362cSRyder Lee case MT_CIPHER_CCMP_256:
399c368362cSRyder Lee insert_ccmp_hdr =
400c368362cSRyder Lee FIELD_GET(MT_RXD2_NORMAL_FRAG, rxd2);
401c368362cSRyder Lee fallthrough;
402c368362cSRyder Lee case MT_CIPHER_TKIP:
403c368362cSRyder Lee case MT_CIPHER_TKIP_NO_MIC:
404c368362cSRyder Lee case MT_CIPHER_GCMP:
405c368362cSRyder Lee case MT_CIPHER_GCMP_256:
406e57b7901SRyder Lee status->iv[0] = data[5];
407e57b7901SRyder Lee status->iv[1] = data[4];
408e57b7901SRyder Lee status->iv[2] = data[3];
409e57b7901SRyder Lee status->iv[3] = data[2];
410e57b7901SRyder Lee status->iv[4] = data[1];
411e57b7901SRyder Lee status->iv[5] = data[0];
412c368362cSRyder Lee break;
413c368362cSRyder Lee default:
414c368362cSRyder Lee break;
415c368362cSRyder Lee }
416e57b7901SRyder Lee }
417e57b7901SRyder Lee rxd += 4;
418e57b7901SRyder Lee if ((u8 *)rxd - skb->data >= skb->len)
419e57b7901SRyder Lee return -EINVAL;
420e57b7901SRyder Lee }
421e57b7901SRyder Lee
422e57b7901SRyder Lee if (rxd1 & MT_RXD1_NORMAL_GROUP_2) {
4230fda6d7bSRyder Lee status->timestamp = le32_to_cpu(rxd[0]);
4240fda6d7bSRyder Lee status->flag |= RX_FLAG_MACTIME_START;
4250fda6d7bSRyder Lee
4260fda6d7bSRyder Lee if (!(rxd2 & MT_RXD2_NORMAL_NON_AMPDU)) {
4270fda6d7bSRyder Lee status->flag |= RX_FLAG_AMPDU_DETAILS;
4280fda6d7bSRyder Lee
4290fda6d7bSRyder Lee /* all subframes of an A-MPDU have the same timestamp */
4300fda6d7bSRyder Lee if (phy->rx_ampdu_ts != status->timestamp) {
4310fda6d7bSRyder Lee if (!++phy->ampdu_ref)
4320fda6d7bSRyder Lee phy->ampdu_ref++;
4330fda6d7bSRyder Lee }
4340fda6d7bSRyder Lee phy->rx_ampdu_ts = status->timestamp;
4350fda6d7bSRyder Lee
4360fda6d7bSRyder Lee status->ampdu_ref = phy->ampdu_ref;
4370fda6d7bSRyder Lee }
4380fda6d7bSRyder Lee
439e57b7901SRyder Lee rxd += 2;
440e57b7901SRyder Lee if ((u8 *)rxd - skb->data >= skb->len)
441e57b7901SRyder Lee return -EINVAL;
442e57b7901SRyder Lee }
443e57b7901SRyder Lee
444e57b7901SRyder Lee /* RXD Group 3 - P-RXV */
445e57b7901SRyder Lee if (rxd1 & MT_RXD1_NORMAL_GROUP_3) {
4461c9db0aaSBo Jiao u32 v0, v1;
4471c9db0aaSBo Jiao int ret;
448b62db09aSRyder Lee
4490d4b6909SRyder Lee rxv = rxd;
450e57b7901SRyder Lee rxd += 2;
451e57b7901SRyder Lee if ((u8 *)rxd - skb->data >= skb->len)
452e57b7901SRyder Lee return -EINVAL;
453e57b7901SRyder Lee
4540d4b6909SRyder Lee v0 = le32_to_cpu(rxv[0]);
4550d4b6909SRyder Lee v1 = le32_to_cpu(rxv[1]);
456b62db09aSRyder Lee
457b62db09aSRyder Lee if (v0 & MT_PRXV_HT_AD_CODE)
458e57b7901SRyder Lee status->enc_flags |= RX_ENC_FLAG_LDPC;
459e57b7901SRyder Lee
460e57b7901SRyder Lee status->chains = mphy->antenna_mask;
461b62db09aSRyder Lee status->chain_signal[0] = to_rssi(MT_PRXV_RCPI0, v1);
462b62db09aSRyder Lee status->chain_signal[1] = to_rssi(MT_PRXV_RCPI1, v1);
463b62db09aSRyder Lee status->chain_signal[2] = to_rssi(MT_PRXV_RCPI2, v1);
464b62db09aSRyder Lee status->chain_signal[3] = to_rssi(MT_PRXV_RCPI3, v1);
465e57b7901SRyder Lee
466e57b7901SRyder Lee /* RXD Group 5 - C-RXV */
467e57b7901SRyder Lee if (rxd1 & MT_RXD1_NORMAL_GROUP_5) {
468e57b7901SRyder Lee rxd += 18;
469e57b7901SRyder Lee if ((u8 *)rxd - skb->data >= skb->len)
470e57b7901SRyder Lee return -EINVAL;
471e57b7901SRyder Lee }
472e57b7901SRyder Lee
473b1481b33SWan Jiabing if (!is_mt7915(&dev->mt76) || (rxd1 & MT_RXD1_NORMAL_GROUP_5)) {
474d832f5e7SLorenzo Bianconi ret = mt76_connac2_mac_fill_rx_rate(&dev->mt76, status,
475d832f5e7SLorenzo Bianconi sband, rxv, &mode);
4761c9db0aaSBo Jiao if (ret < 0)
4771c9db0aaSBo Jiao return ret;
478e57b7901SRyder Lee }
479e57b7901SRyder Lee }
480e57b7901SRyder Lee
481cc4b3c13SLorenzo Bianconi amsdu_info = FIELD_GET(MT_RXD4_NORMAL_PAYLOAD_FORMAT, rxd4);
482cc4b3c13SLorenzo Bianconi status->amsdu = !!amsdu_info;
483cc4b3c13SLorenzo Bianconi if (status->amsdu) {
484cc4b3c13SLorenzo Bianconi status->first_amsdu = amsdu_info == MT_RXD4_FIRST_AMSDU_FRAME;
485cc4b3c13SLorenzo Bianconi status->last_amsdu = amsdu_info == MT_RXD4_LAST_AMSDU_FRAME;
486dc5399a5SXing Song }
487dc5399a5SXing Song
488dc5399a5SXing Song hdr_gap = (u8 *)rxd - skb->data + 2 * remove_pad;
489dc5399a5SXing Song if (hdr_trans && ieee80211_has_morefrags(fc)) {
4900880d408SLorenzo Bianconi struct ieee80211_vif *vif;
4910880d408SLorenzo Bianconi int err;
4920880d408SLorenzo Bianconi
4930880d408SLorenzo Bianconi if (!msta || !msta->vif)
494dc5399a5SXing Song return -EINVAL;
4950880d408SLorenzo Bianconi
4960880d408SLorenzo Bianconi vif = container_of((void *)msta->vif, struct ieee80211_vif,
4970880d408SLorenzo Bianconi drv_priv);
4980880d408SLorenzo Bianconi err = mt76_connac2_reverse_frag0_hdr_trans(vif, skb, hdr_gap);
4990880d408SLorenzo Bianconi if (err)
5000880d408SLorenzo Bianconi return err;
5010880d408SLorenzo Bianconi
502dc5399a5SXing Song hdr_trans = false;
503dc5399a5SXing Song } else {
5041eeff0b4SFelix Fietkau int pad_start = 0;
5051eeff0b4SFelix Fietkau
506dc5399a5SXing Song skb_pull(skb, hdr_gap);
507dc5399a5SXing Song if (!hdr_trans && status->amsdu) {
5081eeff0b4SFelix Fietkau pad_start = ieee80211_get_hdrlen_from_skb(skb);
5091eeff0b4SFelix Fietkau } else if (hdr_trans && (rxd2 & MT_RXD2_NORMAL_HDR_TRANS_ERROR)) {
5101eeff0b4SFelix Fietkau /*
5111eeff0b4SFelix Fietkau * When header translation failure is indicated,
5121eeff0b4SFelix Fietkau * the hardware will insert an extra 2-byte field
5131eeff0b4SFelix Fietkau * containing the data length after the protocol
51447c44088SFelix Fietkau * type field. This happens either when the LLC-SNAP
51547c44088SFelix Fietkau * pattern did not match, or if a VLAN header was
51647c44088SFelix Fietkau * detected.
5171eeff0b4SFelix Fietkau */
5181eeff0b4SFelix Fietkau pad_start = 12;
5191eeff0b4SFelix Fietkau if (get_unaligned_be16(skb->data + pad_start) == ETH_P_8021Q)
5201eeff0b4SFelix Fietkau pad_start += 4;
52147c44088SFelix Fietkau else
5221eeff0b4SFelix Fietkau pad_start = 0;
5231eeff0b4SFelix Fietkau }
5241eeff0b4SFelix Fietkau
5251eeff0b4SFelix Fietkau if (pad_start) {
5261eeff0b4SFelix Fietkau memmove(skb->data + 2, skb->data, pad_start);
527cc4b3c13SLorenzo Bianconi skb_pull(skb, 2);
528cc4b3c13SLorenzo Bianconi }
52990e3abf0SFelix Fietkau }
530cc4b3c13SLorenzo Bianconi
531c23fa1bbSRyder Lee if (!hdr_trans) {
532087baf9bSRyder Lee struct ieee80211_hdr *hdr;
533c23fa1bbSRyder Lee
534c23fa1bbSRyder Lee if (insert_ccmp_hdr) {
535e57b7901SRyder Lee u8 key_id = FIELD_GET(MT_RXD1_NORMAL_KEY_ID, rxd1);
536e57b7901SRyder Lee
537e57b7901SRyder Lee mt76_insert_ccmp_hdr(skb, key_id);
538e57b7901SRyder Lee }
539e57b7901SRyder Lee
540087baf9bSRyder Lee hdr = mt76_skb_get_hdr(skb);
54190e3abf0SFelix Fietkau fc = hdr->frame_control;
54290e3abf0SFelix Fietkau if (ieee80211_is_data_qos(fc)) {
54390e3abf0SFelix Fietkau seq_ctrl = le16_to_cpu(hdr->seq_ctrl);
54490e3abf0SFelix Fietkau qos_ctl = *ieee80211_get_qos_ctl(hdr);
54590e3abf0SFelix Fietkau }
54690e3abf0SFelix Fietkau } else {
54790e3abf0SFelix Fietkau status->flag |= RX_FLAG_8023;
548c3137942SSujuan Chen mt7915_wed_check_ppe(dev, &dev->mt76.q_rx[q], msta, skb,
549c3137942SSujuan Chen *info);
55090e3abf0SFelix Fietkau }
55190e3abf0SFelix Fietkau
552c23fa1bbSRyder Lee if (rxv && mode >= MT_PHY_TYPE_HE_SU && !(status->flag & RX_FLAG_8023))
553f71662deSLorenzo Bianconi mt76_connac2_mac_decode_he_radiotap(&dev->mt76, skb, rxv, mode);
554a82dd24dSRyder Lee
55590e3abf0SFelix Fietkau if (!status->wcid || !ieee80211_is_data_qos(fc))
556e57b7901SRyder Lee return 0;
557e57b7901SRyder Lee
558e57b7901SRyder Lee status->aggr = unicast &&
55990e3abf0SFelix Fietkau !ieee80211_is_qos_nullfunc(fc);
56090e3abf0SFelix Fietkau status->qos_ctl = qos_ctl;
56190e3abf0SFelix Fietkau status->seqno = IEEE80211_SEQ_TO_SN(seq_ctrl);
562e57b7901SRyder Lee
563e57b7901SRyder Lee return 0;
564e57b7901SRyder Lee }
565e57b7901SRyder Lee
566338330bdSFelix Fietkau static void
mt7915_mac_fill_rx_vector(struct mt7915_dev * dev,struct sk_buff * skb)567338330bdSFelix Fietkau mt7915_mac_fill_rx_vector(struct mt7915_dev *dev, struct sk_buff *skb)
5685d8a83f0SShayne Chen {
569338330bdSFelix Fietkau #ifdef CONFIG_NL80211_TESTMODE
57078fc30a2SShayne Chen struct mt7915_phy *phy = &dev->phy;
5715d8a83f0SShayne Chen __le32 *rxd = (__le32 *)skb->data;
57278fc30a2SShayne Chen __le32 *rxv_hdr = rxd + 2;
5735d8a83f0SShayne Chen __le32 *rxv = rxd + 4;
5745d8a83f0SShayne Chen u32 rcpi, ib_rssi, wb_rssi, v20, v21;
575006b9d4aSBo Jiao u8 band_idx;
5765d8a83f0SShayne Chen s32 foe;
5775d8a83f0SShayne Chen u8 snr;
5785d8a83f0SShayne Chen int i;
5795d8a83f0SShayne Chen
580f1fe8eefSRyder Lee band_idx = le32_get_bits(rxv_hdr[1], MT_RXV_HDR_BAND_IDX);
5813eb50cc9SRyder Lee if (band_idx && !phy->mt76->band_idx) {
58278fc30a2SShayne Chen phy = mt7915_ext_phy(dev);
58362fdc974SLorenzo Bianconi if (!phy)
58462fdc974SLorenzo Bianconi goto out;
58562fdc974SLorenzo Bianconi }
58678fc30a2SShayne Chen
5875d8a83f0SShayne Chen rcpi = le32_to_cpu(rxv[6]);
5885d8a83f0SShayne Chen ib_rssi = le32_to_cpu(rxv[7]);
5895d8a83f0SShayne Chen wb_rssi = le32_to_cpu(rxv[8]) >> 5;
5905d8a83f0SShayne Chen
5915d8a83f0SShayne Chen for (i = 0; i < 4; i++, rcpi >>= 8, ib_rssi >>= 8, wb_rssi >>= 9) {
5925d8a83f0SShayne Chen if (i == 3)
5935d8a83f0SShayne Chen wb_rssi = le32_to_cpu(rxv[9]);
5945d8a83f0SShayne Chen
59578fc30a2SShayne Chen phy->test.last_rcpi[i] = rcpi & 0xff;
59678fc30a2SShayne Chen phy->test.last_ib_rssi[i] = ib_rssi & 0xff;
59778fc30a2SShayne Chen phy->test.last_wb_rssi[i] = wb_rssi & 0xff;
5985d8a83f0SShayne Chen }
5995d8a83f0SShayne Chen
6005d8a83f0SShayne Chen v20 = le32_to_cpu(rxv[20]);
6015d8a83f0SShayne Chen v21 = le32_to_cpu(rxv[21]);
6025d8a83f0SShayne Chen
6035d8a83f0SShayne Chen foe = FIELD_GET(MT_CRXV_FOE_LO, v20) |
6045d8a83f0SShayne Chen (FIELD_GET(MT_CRXV_FOE_HI, v21) << MT_CRXV_FOE_SHIFT);
6055d8a83f0SShayne Chen
6065d8a83f0SShayne Chen snr = FIELD_GET(MT_CRXV_SNR, v20) - 16;
6075d8a83f0SShayne Chen
60878fc30a2SShayne Chen phy->test.last_freq_offset = foe;
60978fc30a2SShayne Chen phy->test.last_snr = snr;
61062fdc974SLorenzo Bianconi out:
611338330bdSFelix Fietkau #endif
6125d8a83f0SShayne Chen dev_kfree_skb(skb);
6135d8a83f0SShayne Chen }
6145d8a83f0SShayne Chen
61555f7c9b0SFelix Fietkau static void
mt7915_mac_write_txwi_tm(struct mt7915_phy * phy,__le32 * txwi,struct sk_buff * skb)616c918c74dSShayne Chen mt7915_mac_write_txwi_tm(struct mt7915_phy *phy, __le32 *txwi,
617c918c74dSShayne Chen struct sk_buff *skb)
618aadf0953SShayne Chen {
619aadf0953SShayne Chen #ifdef CONFIG_NL80211_TESTMODE
620c918c74dSShayne Chen struct mt76_testmode_data *td = &phy->mt76->test;
621cc91747bSShayne Chen const struct ieee80211_rate *r;
622cc91747bSShayne Chen u8 bw, mode, nss = td->tx_rate_nss;
623aadf0953SShayne Chen u8 rate_idx = td->tx_rate_idx;
624aadf0953SShayne Chen u16 rateval = 0;
625aadf0953SShayne Chen u32 val;
626cc91747bSShayne Chen bool cck = false;
627cc91747bSShayne Chen int band;
628aadf0953SShayne Chen
629c918c74dSShayne Chen if (skb != phy->mt76->test.tx_skb)
630aadf0953SShayne Chen return;
631aadf0953SShayne Chen
632aadf0953SShayne Chen switch (td->tx_rate_mode) {
633aadf0953SShayne Chen case MT76_TM_TX_MODE_HT:
634aadf0953SShayne Chen nss = 1 + (rate_idx >> 3);
635aadf0953SShayne Chen mode = MT_PHY_TYPE_HT;
636aadf0953SShayne Chen break;
637aadf0953SShayne Chen case MT76_TM_TX_MODE_VHT:
638aadf0953SShayne Chen mode = MT_PHY_TYPE_VHT;
639aadf0953SShayne Chen break;
640aadf0953SShayne Chen case MT76_TM_TX_MODE_HE_SU:
641aadf0953SShayne Chen mode = MT_PHY_TYPE_HE_SU;
642aadf0953SShayne Chen break;
643aadf0953SShayne Chen case MT76_TM_TX_MODE_HE_EXT_SU:
644aadf0953SShayne Chen mode = MT_PHY_TYPE_HE_EXT_SU;
645aadf0953SShayne Chen break;
646aadf0953SShayne Chen case MT76_TM_TX_MODE_HE_TB:
647aadf0953SShayne Chen mode = MT_PHY_TYPE_HE_TB;
648aadf0953SShayne Chen break;
649aadf0953SShayne Chen case MT76_TM_TX_MODE_HE_MU:
650aadf0953SShayne Chen mode = MT_PHY_TYPE_HE_MU;
651aadf0953SShayne Chen break;
652cc91747bSShayne Chen case MT76_TM_TX_MODE_CCK:
653cc91747bSShayne Chen cck = true;
654cc91747bSShayne Chen fallthrough;
655aadf0953SShayne Chen case MT76_TM_TX_MODE_OFDM:
656cc91747bSShayne Chen band = phy->mt76->chandef.chan->band;
657cc91747bSShayne Chen if (band == NL80211_BAND_2GHZ && !cck)
658cc91747bSShayne Chen rate_idx += 4;
659cc91747bSShayne Chen
660cc91747bSShayne Chen r = &phy->mt76->hw->wiphy->bands[band]->bitrates[rate_idx];
661cc91747bSShayne Chen val = cck ? r->hw_value_short : r->hw_value;
662cc91747bSShayne Chen
663cc91747bSShayne Chen mode = val >> 8;
664cc91747bSShayne Chen rate_idx = val & 0xff;
665cc91747bSShayne Chen break;
666aadf0953SShayne Chen default:
667aadf0953SShayne Chen mode = MT_PHY_TYPE_OFDM;
668aadf0953SShayne Chen break;
669aadf0953SShayne Chen }
670aadf0953SShayne Chen
671c918c74dSShayne Chen switch (phy->mt76->chandef.width) {
672aadf0953SShayne Chen case NL80211_CHAN_WIDTH_40:
673aadf0953SShayne Chen bw = 1;
674aadf0953SShayne Chen break;
675aadf0953SShayne Chen case NL80211_CHAN_WIDTH_80:
676aadf0953SShayne Chen bw = 2;
677aadf0953SShayne Chen break;
678aadf0953SShayne Chen case NL80211_CHAN_WIDTH_80P80:
679aadf0953SShayne Chen case NL80211_CHAN_WIDTH_160:
680aadf0953SShayne Chen bw = 3;
681aadf0953SShayne Chen break;
682aadf0953SShayne Chen default:
683aadf0953SShayne Chen bw = 0;
684aadf0953SShayne Chen break;
685aadf0953SShayne Chen }
686aadf0953SShayne Chen
687aadf0953SShayne Chen if (td->tx_rate_stbc && nss == 1) {
688aadf0953SShayne Chen nss++;
689aadf0953SShayne Chen rateval |= MT_TX_RATE_STBC;
690aadf0953SShayne Chen }
691aadf0953SShayne Chen
692aadf0953SShayne Chen rateval |= FIELD_PREP(MT_TX_RATE_IDX, rate_idx) |
693aadf0953SShayne Chen FIELD_PREP(MT_TX_RATE_MODE, mode) |
694aadf0953SShayne Chen FIELD_PREP(MT_TX_RATE_NSS, nss - 1);
695aadf0953SShayne Chen
696aadf0953SShayne Chen txwi[2] |= cpu_to_le32(MT_TXD2_FIX_RATE);
697aadf0953SShayne Chen
698aadf0953SShayne Chen le32p_replace_bits(&txwi[3], 1, MT_TXD3_REM_TX_COUNT);
699aadf0953SShayne Chen if (td->tx_rate_mode < MT76_TM_TX_MODE_HT)
700aadf0953SShayne Chen txwi[3] |= cpu_to_le32(MT_TXD3_BA_DISABLE);
701aadf0953SShayne Chen
702aadf0953SShayne Chen val = MT_TXD6_FIXED_BW |
703aadf0953SShayne Chen FIELD_PREP(MT_TXD6_BW, bw) |
704aadf0953SShayne Chen FIELD_PREP(MT_TXD6_TX_RATE, rateval) |
705aadf0953SShayne Chen FIELD_PREP(MT_TXD6_SGI, td->tx_rate_sgi);
706aadf0953SShayne Chen
707aadf0953SShayne Chen /* for HE_SU/HE_EXT_SU PPDU
708aadf0953SShayne Chen * - 1x, 2x, 4x LTF + 0.8us GI
709aadf0953SShayne Chen * - 2x LTF + 1.6us GI, 4x LTF + 3.2us GI
710aadf0953SShayne Chen * for HE_MU PPDU
711aadf0953SShayne Chen * - 2x, 4x LTF + 0.8us GI
712aadf0953SShayne Chen * - 2x LTF + 1.6us GI, 4x LTF + 3.2us GI
713aadf0953SShayne Chen * for HE_TB PPDU
714aadf0953SShayne Chen * - 1x, 2x LTF + 1.6us GI
715aadf0953SShayne Chen * - 4x LTF + 3.2us GI
716aadf0953SShayne Chen */
717aadf0953SShayne Chen if (mode >= MT_PHY_TYPE_HE_SU)
718aadf0953SShayne Chen val |= FIELD_PREP(MT_TXD6_HELTF, td->tx_ltf);
719aadf0953SShayne Chen
720cc91747bSShayne Chen if (td->tx_rate_ldpc || (bw > 0 && mode >= MT_PHY_TYPE_HE_SU))
721aadf0953SShayne Chen val |= MT_TXD6_LDPC;
722aadf0953SShayne Chen
723cc91747bSShayne Chen txwi[3] &= ~cpu_to_le32(MT_TXD3_SN_VALID);
724aadf0953SShayne Chen txwi[6] |= cpu_to_le32(val);
725aadf0953SShayne Chen txwi[7] |= cpu_to_le32(FIELD_PREP(MT_TXD7_SPE_IDX,
72678fc30a2SShayne Chen phy->test.spe_idx));
727aadf0953SShayne Chen #endif
728aadf0953SShayne Chen }
729aadf0953SShayne Chen
mt7915_mac_write_txwi(struct mt76_dev * dev,__le32 * txwi,struct sk_buff * skb,struct mt76_wcid * wcid,int pid,struct ieee80211_key_conf * key,enum mt76_txq_id qid,u32 changed)730d502e300SLorenzo Bianconi void mt7915_mac_write_txwi(struct mt76_dev *dev, __le32 *txwi,
7313de4cb17SFelix Fietkau struct sk_buff *skb, struct mt76_wcid *wcid, int pid,
7321d5af0acSFelix Fietkau struct ieee80211_key_conf *key,
7331d5af0acSFelix Fietkau enum mt76_txq_id qid, u32 changed)
73455f7c9b0SFelix Fietkau {
73555f7c9b0SFelix Fietkau struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
736a062f001SLorenzo Bianconi u8 phy_idx = (info->hw_queue & MT_TX_HW_QUEUE_PHY) >> 2;
737d502e300SLorenzo Bianconi struct mt76_phy *mphy = &dev->phy;
73855f7c9b0SFelix Fietkau
739a062f001SLorenzo Bianconi if (phy_idx && dev->phys[MT_BAND1])
740dc44c45cSLorenzo Bianconi mphy = dev->phys[MT_BAND1];
74155f7c9b0SFelix Fietkau
7421d5af0acSFelix Fietkau mt76_connac2_mac_write_txwi(dev, txwi, skb, wcid, key, pid, qid, changed);
74355f7c9b0SFelix Fietkau
744c918c74dSShayne Chen if (mt76_testmode_enabled(mphy))
745c918c74dSShayne Chen mt7915_mac_write_txwi_tm(mphy->priv, txwi, skb);
746e57b7901SRyder Lee }
747e57b7901SRyder Lee
mt7915_tx_prepare_skb(struct mt76_dev * mdev,void * txwi_ptr,enum mt76_txq_id qid,struct mt76_wcid * wcid,struct ieee80211_sta * sta,struct mt76_tx_info * tx_info)748e57b7901SRyder Lee int mt7915_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr,
749e57b7901SRyder Lee enum mt76_txq_id qid, struct mt76_wcid *wcid,
750e57b7901SRyder Lee struct ieee80211_sta *sta,
751e57b7901SRyder Lee struct mt76_tx_info *tx_info)
752e57b7901SRyder Lee {
753e57b7901SRyder Lee struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx_info->skb->data;
754e57b7901SRyder Lee struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
755e57b7901SRyder Lee struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_info->skb);
756e57b7901SRyder Lee struct ieee80211_key_conf *key = info->control.hw_key;
757e57b7901SRyder Lee struct ieee80211_vif *vif = info->control.vif;
7585c0bed88SLorenzo Bianconi struct mt76_connac_fw_txp *txp;
759e57b7901SRyder Lee struct mt76_txwi_cache *t;
760e57b7901SRyder Lee int id, i, nbuf = tx_info->nbuf - 1;
761e57b7901SRyder Lee u8 *txwi = (u8 *)txwi_ptr;
7623de4cb17SFelix Fietkau int pid;
763e57b7901SRyder Lee
764b747fa34SRyder Lee if (unlikely(tx_info->skb->len <= ETH_HLEN))
765b747fa34SRyder Lee return -EINVAL;
766b747fa34SRyder Lee
767e57b7901SRyder Lee if (!wcid)
768e57b7901SRyder Lee wcid = &dev->mt76.global_wcid;
769e57b7901SRyder Lee
7709908d98aSRyder Lee if (sta) {
7719908d98aSRyder Lee struct mt7915_sta *msta;
7729908d98aSRyder Lee
7739908d98aSRyder Lee msta = (struct mt7915_sta *)sta->drv_priv;
7749908d98aSRyder Lee
77505909e46SRyder Lee if (time_after(jiffies, msta->jiffies + HZ / 4)) {
7769908d98aSRyder Lee info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
77705909e46SRyder Lee msta->jiffies = jiffies;
7789908d98aSRyder Lee }
7799908d98aSRyder Lee }
7809908d98aSRyder Lee
7812a9e9857SLorenzo Bianconi t = (struct mt76_txwi_cache *)(txwi + mdev->drv->txwi_size);
7822a9e9857SLorenzo Bianconi t->skb = tx_info->skb;
783e57b7901SRyder Lee
7842a9e9857SLorenzo Bianconi id = mt76_token_consume(mdev, &t);
7852a9e9857SLorenzo Bianconi if (id < 0)
7862a9e9857SLorenzo Bianconi return id;
7872a9e9857SLorenzo Bianconi
7882a9e9857SLorenzo Bianconi pid = mt76_tx_status_skb_add(mdev, wcid, tx_info->skb);
7891d5af0acSFelix Fietkau mt7915_mac_write_txwi(mdev, txwi_ptr, tx_info->skb, wcid, pid, key,
7901d5af0acSFelix Fietkau qid, 0);
79155f7c9b0SFelix Fietkau
7925c0bed88SLorenzo Bianconi txp = (struct mt76_connac_fw_txp *)(txwi + MT_TXD_SIZE);
793e57b7901SRyder Lee for (i = 0; i < nbuf; i++) {
794e57b7901SRyder Lee txp->buf[i] = cpu_to_le32(tx_info->buf[i + 1].addr);
795e57b7901SRyder Lee txp->len[i] = cpu_to_le16(tx_info->buf[i + 1].len);
796e57b7901SRyder Lee }
797e57b7901SRyder Lee txp->nbuf = nbuf;
798e57b7901SRyder Lee
799e151d71eSFelix Fietkau txp->flags = cpu_to_le16(MT_CT_INFO_APPLY_TXD | MT_CT_INFO_FROM_HOST);
800e57b7901SRyder Lee
801e57b7901SRyder Lee if (!key)
802e57b7901SRyder Lee txp->flags |= cpu_to_le16(MT_CT_INFO_NONE_CIPHER_FRAME);
803e57b7901SRyder Lee
80455f7c9b0SFelix Fietkau if (!(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) &&
80555f7c9b0SFelix Fietkau ieee80211_is_mgmt(hdr->frame_control))
806e57b7901SRyder Lee txp->flags |= cpu_to_le16(MT_CT_INFO_MGMT_FRAME);
807e57b7901SRyder Lee
808e57b7901SRyder Lee if (vif) {
809e57b7901SRyder Lee struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
810e57b7901SRyder Lee
8116cf4392fSLorenzo Bianconi txp->bss_idx = mvif->mt76.idx;
812e57b7901SRyder Lee }
813e57b7901SRyder Lee
814e57b7901SRyder Lee txp->token = cpu_to_le16(id);
815e151d71eSFelix Fietkau if (test_bit(MT_WCID_FLAG_4ADDR, &wcid->flags))
816e151d71eSFelix Fietkau txp->rept_wds_wcid = cpu_to_le16(wcid->idx);
817e151d71eSFelix Fietkau else
818e151d71eSFelix Fietkau txp->rept_wds_wcid = cpu_to_le16(0x3ff);
819832f4269SFelix Fietkau tx_info->skb = NULL;
820e57b7901SRyder Lee
82194f0e625SFelix Fietkau /* pass partial skb header to fw */
82294f0e625SFelix Fietkau tx_info->buf[1].len = MT_CT_PARSE_LEN;
82394f0e625SFelix Fietkau tx_info->buf[1].skip_unmap = true;
82494f0e625SFelix Fietkau tx_info->nbuf = MT_CT_DMA_BUF_NUM;
82594f0e625SFelix Fietkau
826e57b7901SRyder Lee return 0;
827e57b7901SRyder Lee }
828e57b7901SRyder Lee
mt7915_wed_init_buf(void * ptr,dma_addr_t phys,int token_id)829f68d6762SFelix Fietkau u32 mt7915_wed_init_buf(void *ptr, dma_addr_t phys, int token_id)
830f68d6762SFelix Fietkau {
8315c0bed88SLorenzo Bianconi struct mt76_connac_fw_txp *txp = ptr + MT_TXD_SIZE;
832f68d6762SFelix Fietkau __le32 *txwi = ptr;
833f68d6762SFelix Fietkau u32 val;
834f68d6762SFelix Fietkau
835f68d6762SFelix Fietkau memset(ptr, 0, MT_TXD_SIZE + sizeof(*txp));
836f68d6762SFelix Fietkau
837f68d6762SFelix Fietkau val = FIELD_PREP(MT_TXD0_TX_BYTES, MT_TXD_SIZE) |
838f68d6762SFelix Fietkau FIELD_PREP(MT_TXD0_PKT_FMT, MT_TX_TYPE_CT);
839f68d6762SFelix Fietkau txwi[0] = cpu_to_le32(val);
840f68d6762SFelix Fietkau
841f68d6762SFelix Fietkau val = MT_TXD1_LONG_FORMAT |
842f68d6762SFelix Fietkau FIELD_PREP(MT_TXD1_HDR_FORMAT, MT_HDR_FORMAT_802_3);
843f68d6762SFelix Fietkau txwi[1] = cpu_to_le32(val);
844f68d6762SFelix Fietkau
845f68d6762SFelix Fietkau txp->token = cpu_to_le16(token_id);
846f68d6762SFelix Fietkau txp->nbuf = 1;
847f68d6762SFelix Fietkau txp->buf[0] = cpu_to_le32(phys + MT_TXD_SIZE + sizeof(*txp));
848f68d6762SFelix Fietkau
849f68d6762SFelix Fietkau return MT_TXD_SIZE + sizeof(*txp);
850f68d6762SFelix Fietkau }
851f68d6762SFelix Fietkau
852e57b7901SRyder Lee static void
mt7915_mac_tx_free_prepare(struct mt7915_dev * dev)853f68d6762SFelix Fietkau mt7915_mac_tx_free_prepare(struct mt7915_dev *dev)
854e57b7901SRyder Lee {
855e57b7901SRyder Lee struct mt76_dev *mdev = &dev->mt76;
856dc44c45cSLorenzo Bianconi struct mt76_phy *mphy_ext = mdev->phys[MT_BAND1];
857e57b7901SRyder Lee
858f8a667a9SFelix Fietkau /* clean DMA queues and unmap buffers first */
85991990519SLorenzo Bianconi mt76_queue_tx_cleanup(dev, dev->mphy.q_tx[MT_TXQ_PSD], false);
86091990519SLorenzo Bianconi mt76_queue_tx_cleanup(dev, dev->mphy.q_tx[MT_TXQ_BE], false);
8614c430774SLorenzo Bianconi if (mphy_ext) {
8624c430774SLorenzo Bianconi mt76_queue_tx_cleanup(dev, mphy_ext->q_tx[MT_TXQ_PSD], false);
8634c430774SLorenzo Bianconi mt76_queue_tx_cleanup(dev, mphy_ext->q_tx[MT_TXQ_BE], false);
8644c430774SLorenzo Bianconi }
865f68d6762SFelix Fietkau }
866f68d6762SFelix Fietkau
867f68d6762SFelix Fietkau static void
mt7915_mac_tx_free_done(struct mt7915_dev * dev,struct list_head * free_list,bool wake)868f68d6762SFelix Fietkau mt7915_mac_tx_free_done(struct mt7915_dev *dev,
869f68d6762SFelix Fietkau struct list_head *free_list, bool wake)
870f68d6762SFelix Fietkau {
871f68d6762SFelix Fietkau struct sk_buff *skb, *tmp;
872f68d6762SFelix Fietkau
873f68d6762SFelix Fietkau mt7915_mac_sta_poll(dev);
874f68d6762SFelix Fietkau
875f68d6762SFelix Fietkau if (wake)
876f68d6762SFelix Fietkau mt76_set_tx_blocked(&dev->mt76, false);
877f68d6762SFelix Fietkau
878f68d6762SFelix Fietkau mt76_worker_schedule(&dev->mt76.tx_worker);
879f68d6762SFelix Fietkau
880f68d6762SFelix Fietkau list_for_each_entry_safe(skb, tmp, free_list, list) {
881f68d6762SFelix Fietkau skb_list_del_init(skb);
882f68d6762SFelix Fietkau napi_consume_skb(skb, 1);
883f68d6762SFelix Fietkau }
884f68d6762SFelix Fietkau }
885f68d6762SFelix Fietkau
886f68d6762SFelix Fietkau static void
mt7915_mac_tx_free(struct mt7915_dev * dev,void * data,int len)887f68d6762SFelix Fietkau mt7915_mac_tx_free(struct mt7915_dev *dev, void *data, int len)
888f68d6762SFelix Fietkau {
889a8021cb9SLorenzo Bianconi struct mt76_connac_tx_free *free = data;
890a8021cb9SLorenzo Bianconi __le32 *tx_info = (__le32 *)(data + sizeof(*free));
891f68d6762SFelix Fietkau struct mt76_dev *mdev = &dev->mt76;
892f68d6762SFelix Fietkau struct mt76_txwi_cache *txwi;
893f68d6762SFelix Fietkau struct ieee80211_sta *sta = NULL;
894943e4fb9SRyder Lee struct mt76_wcid *wcid = NULL;
895f68d6762SFelix Fietkau LIST_HEAD(free_list);
896f68d6762SFelix Fietkau void *end = data + len;
897f68d6762SFelix Fietkau bool v3, wake = false;
898f68d6762SFelix Fietkau u16 total, count = 0;
899f68d6762SFelix Fietkau u32 txd = le32_to_cpu(free->txd);
900f68d6762SFelix Fietkau __le32 *cur_info;
901f68d6762SFelix Fietkau
902f68d6762SFelix Fietkau mt7915_mac_tx_free_prepare(dev);
903f8a667a9SFelix Fietkau
904f1fe8eefSRyder Lee total = le16_get_bits(free->ctrl, MT_TX_FREE_MSDU_CNT);
905c17780e7SBo Jiao v3 = (FIELD_GET(MT_TX_FREE_VER, txd) == 0x4);
906e4232f05SFelix Fietkau
907a8021cb9SLorenzo Bianconi for (cur_info = tx_info; count < total; cur_info++) {
9082b685ba7SBen Greear u32 msdu, info;
909c17780e7SBo Jiao u8 i;
910e57b7901SRyder Lee
9112b685ba7SBen Greear if (WARN_ON_ONCE((void *)cur_info >= end))
9122b685ba7SBen Greear return;
9132b685ba7SBen Greear
914e57b7901SRyder Lee /*
915e57b7901SRyder Lee * 1'b1: new wcid pair.
916e57b7901SRyder Lee * 1'b0: msdu_id with the same 'wcid pair' as above.
917e57b7901SRyder Lee */
9182b685ba7SBen Greear info = le32_to_cpu(*cur_info);
919e57b7901SRyder Lee if (info & MT_TX_FREE_PAIR) {
920e57b7901SRyder Lee struct mt7915_sta *msta;
921e57b7901SRyder Lee u16 idx;
922e57b7901SRyder Lee
923e57b7901SRyder Lee idx = FIELD_GET(MT_TX_FREE_WLAN_ID, info);
924e57b7901SRyder Lee wcid = rcu_dereference(dev->mt76.wcid[idx]);
925e57b7901SRyder Lee sta = wcid_to_sta(wcid);
926e57b7901SRyder Lee if (!sta)
927e57b7901SRyder Lee continue;
928e57b7901SRyder Lee
929e57b7901SRyder Lee msta = container_of(wcid, struct mt7915_sta, wcid);
930fbba711cSLorenzo Bianconi spin_lock_bh(&mdev->sta_poll_lock);
931b73e1d92SLorenzo Bianconi if (list_empty(&msta->wcid.poll_list))
932b73e1d92SLorenzo Bianconi list_add_tail(&msta->wcid.poll_list,
933fbba711cSLorenzo Bianconi &mdev->sta_poll_list);
934fbba711cSLorenzo Bianconi spin_unlock_bh(&mdev->sta_poll_lock);
9356425791dSFelix Fietkau continue;
936e57b7901SRyder Lee }
937e57b7901SRyder Lee
938943e4fb9SRyder Lee if (!mtk_wed_device_active(&mdev->mmio.wed) && wcid) {
939943e4fb9SRyder Lee u32 tx_retries = 0, tx_failed = 0;
940943e4fb9SRyder Lee
941943e4fb9SRyder Lee if (v3 && (info & MT_TX_FREE_MPDU_HEADER_V3)) {
942943e4fb9SRyder Lee tx_retries =
943943e4fb9SRyder Lee FIELD_GET(MT_TX_FREE_COUNT_V3, info) - 1;
944943e4fb9SRyder Lee tx_failed = tx_retries +
945943e4fb9SRyder Lee !!FIELD_GET(MT_TX_FREE_STAT_V3, info);
946943e4fb9SRyder Lee } else if (!v3 && (info & MT_TX_FREE_MPDU_HEADER)) {
947943e4fb9SRyder Lee tx_retries =
948943e4fb9SRyder Lee FIELD_GET(MT_TX_FREE_COUNT, info) - 1;
949943e4fb9SRyder Lee tx_failed = tx_retries +
950943e4fb9SRyder Lee !!FIELD_GET(MT_TX_FREE_STAT, info);
951943e4fb9SRyder Lee }
952943e4fb9SRyder Lee wcid->stats.tx_retries += tx_retries;
953943e4fb9SRyder Lee wcid->stats.tx_failed += tx_failed;
954943e4fb9SRyder Lee }
955943e4fb9SRyder Lee
956943e4fb9SRyder Lee if (v3 && (info & MT_TX_FREE_MPDU_HEADER_V3))
957c17780e7SBo Jiao continue;
958c17780e7SBo Jiao
959c17780e7SBo Jiao for (i = 0; i < 1 + v3; i++) {
960c17780e7SBo Jiao if (v3) {
961c17780e7SBo Jiao msdu = (info >> (15 * i)) & MT_TX_FREE_MSDU_ID_V3;
962c17780e7SBo Jiao if (msdu == MT_TX_FREE_MSDU_ID_V3)
963c17780e7SBo Jiao continue;
964c17780e7SBo Jiao } else {
965e57b7901SRyder Lee msdu = FIELD_GET(MT_TX_FREE_MSDU_ID, info);
966c17780e7SBo Jiao }
967c17780e7SBo Jiao count++;
968d089692bSLorenzo Bianconi txwi = mt76_token_release(mdev, msdu, &wake);
969e57b7901SRyder Lee if (!txwi)
970e57b7901SRyder Lee continue;
971e57b7901SRyder Lee
972c8e370feSLorenzo Bianconi mt76_connac2_txwi_free(mdev, txwi, sta, &free_list);
973e57b7901SRyder Lee }
974c17780e7SBo Jiao }
9750f1c443cSFelix Fietkau
976f68d6762SFelix Fietkau mt7915_mac_tx_free_done(dev, &free_list, wake);
977660915d0SFelix Fietkau }
978f68d6762SFelix Fietkau
979f68d6762SFelix Fietkau static void
mt7915_mac_tx_free_v0(struct mt7915_dev * dev,void * data,int len)980f68d6762SFelix Fietkau mt7915_mac_tx_free_v0(struct mt7915_dev *dev, void *data, int len)
981f68d6762SFelix Fietkau {
982a8021cb9SLorenzo Bianconi struct mt76_connac_tx_free *free = data;
983a8021cb9SLorenzo Bianconi __le16 *info = (__le16 *)(data + sizeof(*free));
984f68d6762SFelix Fietkau struct mt76_dev *mdev = &dev->mt76;
985f68d6762SFelix Fietkau void *end = data + len;
986f68d6762SFelix Fietkau LIST_HEAD(free_list);
987f68d6762SFelix Fietkau bool wake = false;
988f68d6762SFelix Fietkau u8 i, count;
989f68d6762SFelix Fietkau
990f68d6762SFelix Fietkau mt7915_mac_tx_free_prepare(dev);
991f68d6762SFelix Fietkau
992f68d6762SFelix Fietkau count = FIELD_GET(MT_TX_FREE_MSDU_CNT_V0, le16_to_cpu(free->ctrl));
993f68d6762SFelix Fietkau if (WARN_ON_ONCE((void *)&info[count] > end))
994f68d6762SFelix Fietkau return;
995f68d6762SFelix Fietkau
996f68d6762SFelix Fietkau for (i = 0; i < count; i++) {
997f68d6762SFelix Fietkau struct mt76_txwi_cache *txwi;
998f68d6762SFelix Fietkau u16 msdu = le16_to_cpu(info[i]);
999f68d6762SFelix Fietkau
1000f68d6762SFelix Fietkau txwi = mt76_token_release(mdev, msdu, &wake);
1001f68d6762SFelix Fietkau if (!txwi)
1002f68d6762SFelix Fietkau continue;
1003f68d6762SFelix Fietkau
1004c8e370feSLorenzo Bianconi mt76_connac2_txwi_free(mdev, txwi, NULL, &free_list);
1005f68d6762SFelix Fietkau }
1006f68d6762SFelix Fietkau
1007f68d6762SFelix Fietkau mt7915_mac_tx_free_done(dev, &free_list, wake);
1008e57b7901SRyder Lee }
1009e57b7901SRyder Lee
mt7915_mac_add_txs(struct mt7915_dev * dev,void * data)10103de4cb17SFelix Fietkau static void mt7915_mac_add_txs(struct mt7915_dev *dev, void *data)
10113de4cb17SFelix Fietkau {
10123de4cb17SFelix Fietkau struct mt7915_sta *msta = NULL;
10133de4cb17SFelix Fietkau struct mt76_wcid *wcid;
10143de4cb17SFelix Fietkau __le32 *txs_data = data;
10153de4cb17SFelix Fietkau u16 wcidx;
10163de4cb17SFelix Fietkau u8 pid;
10173de4cb17SFelix Fietkau
1018f1fe8eefSRyder Lee wcidx = le32_get_bits(txs_data[2], MT_TXS2_WCID);
1019f1fe8eefSRyder Lee pid = le32_get_bits(txs_data[3], MT_TXS3_PID);
10203de4cb17SFelix Fietkau
102143eaa368SRyder Lee if (pid < MT_PACKET_ID_WED)
10223de4cb17SFelix Fietkau return;
10233de4cb17SFelix Fietkau
1024b37d0c97SBo Jiao if (wcidx >= mt7915_wtbl_size(dev))
10253de4cb17SFelix Fietkau return;
10263de4cb17SFelix Fietkau
10273de4cb17SFelix Fietkau rcu_read_lock();
10283de4cb17SFelix Fietkau
10293de4cb17SFelix Fietkau wcid = rcu_dereference(dev->mt76.wcid[wcidx]);
10303de4cb17SFelix Fietkau if (!wcid)
10313de4cb17SFelix Fietkau goto out;
10323de4cb17SFelix Fietkau
1033c4c2a370SBen Greear msta = container_of(wcid, struct mt7915_sta, wcid);
1034c4c2a370SBen Greear
103543eaa368SRyder Lee if (pid == MT_PACKET_ID_WED)
103643eaa368SRyder Lee mt76_connac2_mac_fill_txs(&dev->mt76, wcid, txs_data);
103743eaa368SRyder Lee else
1038dc877523SRyder Lee mt76_connac2_mac_add_txs_skb(&dev->mt76, wcid, pid, txs_data);
103943eaa368SRyder Lee
10403de4cb17SFelix Fietkau if (!wcid->sta)
10413de4cb17SFelix Fietkau goto out;
10423de4cb17SFelix Fietkau
1043fbba711cSLorenzo Bianconi spin_lock_bh(&dev->mt76.sta_poll_lock);
1044b73e1d92SLorenzo Bianconi if (list_empty(&msta->wcid.poll_list))
1045b73e1d92SLorenzo Bianconi list_add_tail(&msta->wcid.poll_list, &dev->mt76.sta_poll_list);
1046fbba711cSLorenzo Bianconi spin_unlock_bh(&dev->mt76.sta_poll_lock);
10473de4cb17SFelix Fietkau
10483de4cb17SFelix Fietkau out:
10493de4cb17SFelix Fietkau rcu_read_unlock();
10503de4cb17SFelix Fietkau }
10513de4cb17SFelix Fietkau
mt7915_rx_check(struct mt76_dev * mdev,void * data,int len)1052e4232f05SFelix Fietkau bool mt7915_rx_check(struct mt76_dev *mdev, void *data, int len)
1053e4232f05SFelix Fietkau {
1054e4232f05SFelix Fietkau struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
1055e4232f05SFelix Fietkau __le32 *rxd = (__le32 *)data;
1056e4232f05SFelix Fietkau __le32 *end = (__le32 *)&rxd[len / 4];
1057e4232f05SFelix Fietkau enum rx_pkt_type type;
1058e4232f05SFelix Fietkau
1059f1fe8eefSRyder Lee type = le32_get_bits(rxd[0], MT_RXD0_PKT_TYPE);
1060f1fe8eefSRyder Lee
1061e4232f05SFelix Fietkau switch (type) {
1062e4232f05SFelix Fietkau case PKT_TYPE_TXRX_NOTIFY:
1063e4232f05SFelix Fietkau mt7915_mac_tx_free(dev, data, len);
1064e4232f05SFelix Fietkau return false;
1065f68d6762SFelix Fietkau case PKT_TYPE_TXRX_NOTIFY_V0:
1066f68d6762SFelix Fietkau mt7915_mac_tx_free_v0(dev, data, len);
1067f68d6762SFelix Fietkau return false;
1068e4232f05SFelix Fietkau case PKT_TYPE_TXS:
1069e4232f05SFelix Fietkau for (rxd += 2; rxd + 8 <= end; rxd += 8)
1070e4232f05SFelix Fietkau mt7915_mac_add_txs(dev, rxd);
1071e4232f05SFelix Fietkau return false;
1072988845c9SFelix Fietkau case PKT_TYPE_RX_FW_MONITOR:
1073988845c9SFelix Fietkau mt7915_debugfs_rx_fw_monitor(dev, data, len);
1074988845c9SFelix Fietkau return false;
1075e4232f05SFelix Fietkau default:
1076e4232f05SFelix Fietkau return true;
1077e4232f05SFelix Fietkau }
1078e4232f05SFelix Fietkau }
1079e4232f05SFelix Fietkau
mt7915_queue_rx_skb(struct mt76_dev * mdev,enum mt76_rxq_id q,struct sk_buff * skb,u32 * info)1080338330bdSFelix Fietkau void mt7915_queue_rx_skb(struct mt76_dev *mdev, enum mt76_rxq_id q,
1081c3137942SSujuan Chen struct sk_buff *skb, u32 *info)
1082338330bdSFelix Fietkau {
1083338330bdSFelix Fietkau struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
1084338330bdSFelix Fietkau __le32 *rxd = (__le32 *)skb->data;
10853de4cb17SFelix Fietkau __le32 *end = (__le32 *)&skb->data[skb->len];
1086338330bdSFelix Fietkau enum rx_pkt_type type;
1087338330bdSFelix Fietkau
1088f1fe8eefSRyder Lee type = le32_get_bits(rxd[0], MT_RXD0_PKT_TYPE);
1089338330bdSFelix Fietkau
1090338330bdSFelix Fietkau switch (type) {
1091338330bdSFelix Fietkau case PKT_TYPE_TXRX_NOTIFY:
1092e4232f05SFelix Fietkau mt7915_mac_tx_free(dev, skb->data, skb->len);
1093e4232f05SFelix Fietkau napi_consume_skb(skb, 1);
1094338330bdSFelix Fietkau break;
1095f68d6762SFelix Fietkau case PKT_TYPE_TXRX_NOTIFY_V0:
1096f68d6762SFelix Fietkau mt7915_mac_tx_free_v0(dev, skb->data, skb->len);
1097f68d6762SFelix Fietkau napi_consume_skb(skb, 1);
1098f68d6762SFelix Fietkau break;
1099338330bdSFelix Fietkau case PKT_TYPE_RX_EVENT:
1100338330bdSFelix Fietkau mt7915_mcu_rx_event(dev, skb);
1101338330bdSFelix Fietkau break;
1102338330bdSFelix Fietkau case PKT_TYPE_TXRXV:
1103338330bdSFelix Fietkau mt7915_mac_fill_rx_vector(dev, skb);
1104338330bdSFelix Fietkau break;
11053de4cb17SFelix Fietkau case PKT_TYPE_TXS:
11063de4cb17SFelix Fietkau for (rxd += 2; rxd + 8 <= end; rxd += 8)
11073de4cb17SFelix Fietkau mt7915_mac_add_txs(dev, rxd);
11083de4cb17SFelix Fietkau dev_kfree_skb(skb);
11093de4cb17SFelix Fietkau break;
1110988845c9SFelix Fietkau case PKT_TYPE_RX_FW_MONITOR:
1111988845c9SFelix Fietkau mt7915_debugfs_rx_fw_monitor(dev, skb->data, skb->len);
1112b962252bSShayne Chen dev_kfree_skb(skb);
1113988845c9SFelix Fietkau break;
1114338330bdSFelix Fietkau case PKT_TYPE_NORMAL:
1115c3137942SSujuan Chen if (!mt7915_mac_fill_rx(dev, skb, q, info)) {
1116338330bdSFelix Fietkau mt76_rx(&dev->mt76, q, skb);
1117338330bdSFelix Fietkau return;
1118338330bdSFelix Fietkau }
1119338330bdSFelix Fietkau fallthrough;
1120338330bdSFelix Fietkau default:
1121338330bdSFelix Fietkau dev_kfree_skb(skb);
1122338330bdSFelix Fietkau break;
1123338330bdSFelix Fietkau }
1124338330bdSFelix Fietkau }
1125338330bdSFelix Fietkau
mt7915_mac_cca_stats_reset(struct mt7915_phy * phy)1126e57b7901SRyder Lee void mt7915_mac_cca_stats_reset(struct mt7915_phy *phy)
1127e57b7901SRyder Lee {
1128e57b7901SRyder Lee struct mt7915_dev *dev = phy->dev;
11293eb50cc9SRyder Lee u32 reg = MT_WF_PHY_RX_CTRL1(phy->mt76->band_idx);
1130e57b7901SRyder Lee
11316d88629eSRyder Lee mt76_clear(dev, reg, MT_WF_PHY_RX_CTRL1_STSCNT_EN);
11326d88629eSRyder Lee mt76_set(dev, reg, BIT(11) | BIT(9));
1133e57b7901SRyder Lee }
1134e57b7901SRyder Lee
mt7915_mac_reset_counters(struct mt7915_phy * phy)1135e57b7901SRyder Lee void mt7915_mac_reset_counters(struct mt7915_phy *phy)
1136e57b7901SRyder Lee {
1137e57b7901SRyder Lee struct mt7915_dev *dev = phy->dev;
1138e57b7901SRyder Lee int i;
1139e57b7901SRyder Lee
1140e57b7901SRyder Lee for (i = 0; i < 4; i++) {
11413eb50cc9SRyder Lee mt76_rr(dev, MT_TX_AGG_CNT(phy->mt76->band_idx, i));
11423eb50cc9SRyder Lee mt76_rr(dev, MT_TX_AGG_CNT2(phy->mt76->band_idx, i));
1143e57b7901SRyder Lee }
1144e57b7901SRyder Lee
1145006b9d4aSBo Jiao phy->mt76->survey_time = ktime_get_boottime();
1146d107501aSLorenzo Bianconi memset(phy->mt76->aggr_stats, 0, sizeof(phy->mt76->aggr_stats));
1147e57b7901SRyder Lee
1148e57b7901SRyder Lee /* reset airtime counters */
11493eb50cc9SRyder Lee mt76_set(dev, MT_WF_RMAC_MIB_AIRTIME0(phy->mt76->band_idx),
1150e57b7901SRyder Lee MT_WF_RMAC_MIB_RXTIME_CLR);
115165430028SRyder Lee
115265430028SRyder Lee mt7915_mcu_get_chan_mib_info(phy, true);
1153e57b7901SRyder Lee }
1154e57b7901SRyder Lee
mt7915_mac_set_timing(struct mt7915_phy * phy)1155e57b7901SRyder Lee void mt7915_mac_set_timing(struct mt7915_phy *phy)
1156e57b7901SRyder Lee {
1157e57b7901SRyder Lee s16 coverage_class = phy->coverage_class;
1158e57b7901SRyder Lee struct mt7915_dev *dev = phy->dev;
1159006b9d4aSBo Jiao struct mt7915_phy *ext_phy = mt7915_ext_phy(dev);
1160e57b7901SRyder Lee u32 val, reg_offset;
1161e57b7901SRyder Lee u32 cck = FIELD_PREP(MT_TIMEOUT_VAL_PLCP, 231) |
1162e57b7901SRyder Lee FIELD_PREP(MT_TIMEOUT_VAL_CCA, 48);
1163e57b7901SRyder Lee u32 ofdm = FIELD_PREP(MT_TIMEOUT_VAL_PLCP, 60) |
1164e57b7901SRyder Lee FIELD_PREP(MT_TIMEOUT_VAL_CCA, 28);
11653eb50cc9SRyder Lee u8 band = phy->mt76->band_idx;
11660c881dc0SRyder Lee int eifs_ofdm = 360, sifs = 10, offset;
1167b4d093e3SMeiChia Chiu bool a_band = !(phy->mt76->chandef.chan->band == NL80211_BAND_2GHZ);
1168e57b7901SRyder Lee
1169e57b7901SRyder Lee if (!test_bit(MT76_STATE_RUNNING, &phy->mt76->state))
1170e57b7901SRyder Lee return;
1171e57b7901SRyder Lee
1172006b9d4aSBo Jiao if (ext_phy)
1173e57b7901SRyder Lee coverage_class = max_t(s16, dev->phy.coverage_class,
1174006b9d4aSBo Jiao ext_phy->coverage_class);
1175e57b7901SRyder Lee
11763eb50cc9SRyder Lee mt76_set(dev, MT_ARB_SCR(band),
1177e57b7901SRyder Lee MT_ARB_SCR_TX_DISABLE | MT_ARB_SCR_RX_DISABLE);
1178e57b7901SRyder Lee udelay(1);
1179e57b7901SRyder Lee
1180e57b7901SRyder Lee offset = 3 * coverage_class;
1181e57b7901SRyder Lee reg_offset = FIELD_PREP(MT_TIMEOUT_VAL_PLCP, offset) |
1182e57b7901SRyder Lee FIELD_PREP(MT_TIMEOUT_VAL_CCA, offset);
1183e57b7901SRyder Lee
11840c881dc0SRyder Lee if (!is_mt7915(&dev->mt76)) {
11850c881dc0SRyder Lee if (!a_band) {
11863eb50cc9SRyder Lee mt76_wr(dev, MT_TMAC_ICR1(band),
11870c881dc0SRyder Lee FIELD_PREP(MT_IFS_EIFS_CCK, 314));
11880c881dc0SRyder Lee eifs_ofdm = 78;
11890c881dc0SRyder Lee } else {
11900c881dc0SRyder Lee eifs_ofdm = 84;
11910c881dc0SRyder Lee }
11920c881dc0SRyder Lee } else if (a_band) {
11930c881dc0SRyder Lee sifs = 16;
11940c881dc0SRyder Lee }
11950c881dc0SRyder Lee
11963eb50cc9SRyder Lee mt76_wr(dev, MT_TMAC_CDTR(band), cck + reg_offset);
11973eb50cc9SRyder Lee mt76_wr(dev, MT_TMAC_ODTR(band), ofdm + reg_offset);
11983eb50cc9SRyder Lee mt76_wr(dev, MT_TMAC_ICR0(band),
11990c881dc0SRyder Lee FIELD_PREP(MT_IFS_EIFS_OFDM, eifs_ofdm) |
1200e57b7901SRyder Lee FIELD_PREP(MT_IFS_RIFS, 2) |
12010c881dc0SRyder Lee FIELD_PREP(MT_IFS_SIFS, sifs) |
1202e57b7901SRyder Lee FIELD_PREP(MT_IFS_SLOT, phy->slottime));
1203e57b7901SRyder Lee
1204b4d093e3SMeiChia Chiu if (phy->slottime < 20 || a_band)
1205e57b7901SRyder Lee val = MT7915_CFEND_RATE_DEFAULT;
1206e57b7901SRyder Lee else
1207e57b7901SRyder Lee val = MT7915_CFEND_RATE_11B;
1208e57b7901SRyder Lee
12093eb50cc9SRyder Lee mt76_rmw_field(dev, MT_AGG_ACR0(band), MT_AGG_ACR_CFEND_RATE, val);
12103eb50cc9SRyder Lee mt76_clear(dev, MT_ARB_SCR(band),
1211e57b7901SRyder Lee MT_ARB_SCR_TX_DISABLE | MT_ARB_SCR_RX_DISABLE);
1212e57b7901SRyder Lee }
1213e57b7901SRyder Lee
mt7915_mac_enable_nf(struct mt7915_dev * dev,bool band)12146f917bbaSRyder Lee void mt7915_mac_enable_nf(struct mt7915_dev *dev, bool band)
121599849398SRyder Lee {
1216cef37c78SBo Jiao u32 reg;
1217cef37c78SBo Jiao
12186f917bbaSRyder Lee reg = is_mt7915(&dev->mt76) ? MT_WF_PHY_RXTD12(band) :
12196f917bbaSRyder Lee MT_WF_PHY_RXTD12_MT7916(band);
1220cef37c78SBo Jiao mt76_set(dev, reg,
122199849398SRyder Lee MT_WF_PHY_RXTD12_IRPI_SW_CLR_ONLY |
122299849398SRyder Lee MT_WF_PHY_RXTD12_IRPI_SW_CLR);
122399849398SRyder Lee
12246f917bbaSRyder Lee reg = is_mt7915(&dev->mt76) ? MT_WF_PHY_RX_CTRL1(band) :
12256f917bbaSRyder Lee MT_WF_PHY_RX_CTRL1_MT7916(band);
1226cef37c78SBo Jiao mt76_set(dev, reg, FIELD_PREP(MT_WF_PHY_RX_CTRL1_IPI_EN, 0x5));
122799849398SRyder Lee }
122899849398SRyder Lee
122999849398SRyder Lee static u8
mt7915_phy_get_nf(struct mt7915_phy * phy,int idx)123099849398SRyder Lee mt7915_phy_get_nf(struct mt7915_phy *phy, int idx)
123199849398SRyder Lee {
123299849398SRyder Lee static const u8 nf_power[] = { 92, 89, 86, 83, 80, 75, 70, 65, 60, 55, 52 };
123399849398SRyder Lee struct mt7915_dev *dev = phy->dev;
123499849398SRyder Lee u32 val, sum = 0, n = 0;
123599849398SRyder Lee int nss, i;
123699849398SRyder Lee
1237b9027e08SLorenzo Bianconi for (nss = 0; nss < hweight8(phy->mt76->chainmask); nss++) {
1238cef37c78SBo Jiao u32 reg = is_mt7915(&dev->mt76) ?
1239cef37c78SBo Jiao MT_WF_IRPI_NSS(0, nss + (idx << dev->dbdc_support)) :
1240cef37c78SBo Jiao MT_WF_IRPI_NSS_MT7916(idx, nss);
124199849398SRyder Lee
124299849398SRyder Lee for (i = 0; i < ARRAY_SIZE(nf_power); i++, reg += 4) {
12436d88629eSRyder Lee val = mt76_rr(dev, reg);
124499849398SRyder Lee sum += val * nf_power[i];
124599849398SRyder Lee n += val;
124699849398SRyder Lee }
124799849398SRyder Lee }
124899849398SRyder Lee
124999849398SRyder Lee if (!n)
125099849398SRyder Lee return 0;
125199849398SRyder Lee
125299849398SRyder Lee return sum / n;
125399849398SRyder Lee }
125499849398SRyder Lee
mt7915_update_channel(struct mt76_phy * mphy)1255c560b137SRyder Lee void mt7915_update_channel(struct mt76_phy *mphy)
1256e57b7901SRyder Lee {
12572fac91f2SWu Yunchuan struct mt7915_phy *phy = mphy->priv;
125865430028SRyder Lee struct mt76_channel_state *state = mphy->chan_state;
125999849398SRyder Lee int nf;
1260e57b7901SRyder Lee
126165430028SRyder Lee mt7915_mcu_get_chan_mib_info(phy, false);
1262e57b7901SRyder Lee
12633eb50cc9SRyder Lee nf = mt7915_phy_get_nf(phy, phy->mt76->band_idx);
126499849398SRyder Lee if (!phy->noise)
126599849398SRyder Lee phy->noise = nf << 4;
126699849398SRyder Lee else if (nf)
126799849398SRyder Lee phy->noise += nf - (phy->noise >> 4);
126899849398SRyder Lee
126999849398SRyder Lee state->noise = -(phy->noise >> 4);
1270e57b7901SRyder Lee }
1271e57b7901SRyder Lee
1272e57b7901SRyder Lee static bool
mt7915_wait_reset_state(struct mt7915_dev * dev,u32 state)1273e57b7901SRyder Lee mt7915_wait_reset_state(struct mt7915_dev *dev, u32 state)
1274e57b7901SRyder Lee {
1275e57b7901SRyder Lee bool ret;
1276e57b7901SRyder Lee
1277e57b7901SRyder Lee ret = wait_event_timeout(dev->reset_wait,
12788a55712dSBo Jiao (READ_ONCE(dev->recovery.state) & state),
1279e57b7901SRyder Lee MT7915_RESET_TIMEOUT);
1280e57b7901SRyder Lee
1281e57b7901SRyder Lee WARN(!ret, "Timeout waiting for MCU reset state %x\n", state);
1282e57b7901SRyder Lee return ret;
1283e57b7901SRyder Lee }
1284e57b7901SRyder Lee
1285e57b7901SRyder Lee static void
mt7915_update_vif_beacon(void * priv,u8 * mac,struct ieee80211_vif * vif)1286e57b7901SRyder Lee mt7915_update_vif_beacon(void *priv, u8 *mac, struct ieee80211_vif *vif)
1287e57b7901SRyder Lee {
1288e57b7901SRyder Lee struct ieee80211_hw *hw = priv;
1289e57b7901SRyder Lee
1290446e06c6SRyder Lee switch (vif->type) {
1291446e06c6SRyder Lee case NL80211_IFTYPE_MESH_POINT:
1292446e06c6SRyder Lee case NL80211_IFTYPE_ADHOC:
1293446e06c6SRyder Lee case NL80211_IFTYPE_AP:
1294869f0646SMeiChia Chiu mt7915_mcu_add_beacon(hw, vif, vif->bss_conf.enable_beacon,
1295869f0646SMeiChia Chiu BSS_CHANGED_BEACON_ENABLED);
1296446e06c6SRyder Lee break;
1297446e06c6SRyder Lee default:
1298446e06c6SRyder Lee break;
1299446e06c6SRyder Lee }
1300e57b7901SRyder Lee }
1301e57b7901SRyder Lee
1302e57b7901SRyder Lee static void
mt7915_update_beacons(struct mt7915_dev * dev)1303e57b7901SRyder Lee mt7915_update_beacons(struct mt7915_dev *dev)
1304e57b7901SRyder Lee {
1305dc44c45cSLorenzo Bianconi struct mt76_phy *mphy_ext = dev->mt76.phys[MT_BAND1];
1306dc44c45cSLorenzo Bianconi
1307e57b7901SRyder Lee ieee80211_iterate_active_interfaces(dev->mt76.hw,
1308e57b7901SRyder Lee IEEE80211_IFACE_ITER_RESUME_ALL,
1309e57b7901SRyder Lee mt7915_update_vif_beacon, dev->mt76.hw);
1310e57b7901SRyder Lee
1311dc44c45cSLorenzo Bianconi if (!mphy_ext)
1312e57b7901SRyder Lee return;
1313e57b7901SRyder Lee
1314dc44c45cSLorenzo Bianconi ieee80211_iterate_active_interfaces(mphy_ext->hw,
1315e57b7901SRyder Lee IEEE80211_IFACE_ITER_RESUME_ALL,
1316dc44c45cSLorenzo Bianconi mt7915_update_vif_beacon, mphy_ext->hw);
1317e57b7901SRyder Lee }
1318e57b7901SRyder Lee
13198a55712dSBo Jiao static int
mt7915_mac_restart(struct mt7915_dev * dev)13208a55712dSBo Jiao mt7915_mac_restart(struct mt7915_dev *dev)
13218a55712dSBo Jiao {
13228a55712dSBo Jiao struct mt7915_phy *phy2;
13238a55712dSBo Jiao struct mt76_phy *ext_phy;
13248a55712dSBo Jiao struct mt76_dev *mdev = &dev->mt76;
13258a55712dSBo Jiao int i, ret;
13268a55712dSBo Jiao
13278a55712dSBo Jiao ext_phy = dev->mt76.phys[MT_BAND1];
13288a55712dSBo Jiao phy2 = ext_phy ? ext_phy->priv : NULL;
13298a55712dSBo Jiao
13308a55712dSBo Jiao if (dev->hif2) {
13318a55712dSBo Jiao mt76_wr(dev, MT_INT1_MASK_CSR, 0x0);
13328a55712dSBo Jiao mt76_wr(dev, MT_INT1_SOURCE_CSR, ~0);
13338a55712dSBo Jiao }
13348a55712dSBo Jiao
13358a55712dSBo Jiao if (dev_is_pci(mdev->dev)) {
13368a55712dSBo Jiao mt76_wr(dev, MT_PCIE_MAC_INT_ENABLE, 0x0);
13371e64fdd4SBo Jiao if (dev->hif2) {
13381e64fdd4SBo Jiao if (is_mt7915(mdev))
13398a55712dSBo Jiao mt76_wr(dev, MT_PCIE1_MAC_INT_ENABLE, 0x0);
13401e64fdd4SBo Jiao else
13411e64fdd4SBo Jiao mt76_wr(dev, MT_PCIE1_MAC_INT_ENABLE_MT7916, 0x0);
13421e64fdd4SBo Jiao }
13438a55712dSBo Jiao }
13448a55712dSBo Jiao
13458a55712dSBo Jiao set_bit(MT76_RESET, &dev->mphy.state);
13468a55712dSBo Jiao set_bit(MT76_MCU_RESET, &dev->mphy.state);
13478a55712dSBo Jiao wake_up(&dev->mt76.mcu.wait);
1348ec8932f2SBo Jiao if (ext_phy)
13498a55712dSBo Jiao set_bit(MT76_RESET, &ext_phy->state);
13508a55712dSBo Jiao
13518a55712dSBo Jiao /* lock/unlock all queues to ensure that no tx is pending */
13528a55712dSBo Jiao mt76_txq_schedule_all(&dev->mphy);
13538a55712dSBo Jiao if (ext_phy)
13548a55712dSBo Jiao mt76_txq_schedule_all(ext_phy);
13558a55712dSBo Jiao
13568a55712dSBo Jiao /* disable all tx/rx napi */
13578a55712dSBo Jiao mt76_worker_disable(&dev->mt76.tx_worker);
13588a55712dSBo Jiao mt76_for_each_q_rx(mdev, i) {
13598a55712dSBo Jiao if (mdev->q_rx[i].ndesc)
13608a55712dSBo Jiao napi_disable(&dev->mt76.napi[i]);
13618a55712dSBo Jiao }
13628a55712dSBo Jiao napi_disable(&dev->mt76.tx_napi);
13638a55712dSBo Jiao
13648a55712dSBo Jiao /* token reinit */
1365c8e370feSLorenzo Bianconi mt76_connac2_tx_token_put(&dev->mt76);
13668a55712dSBo Jiao idr_init(&dev->mt76.token);
13678a55712dSBo Jiao
13688a55712dSBo Jiao mt7915_dma_reset(dev, true);
13698a55712dSBo Jiao
13708a55712dSBo Jiao local_bh_disable();
13718a55712dSBo Jiao mt76_for_each_q_rx(mdev, i) {
13728a55712dSBo Jiao if (mdev->q_rx[i].ndesc) {
13738a55712dSBo Jiao napi_enable(&dev->mt76.napi[i]);
13748a55712dSBo Jiao napi_schedule(&dev->mt76.napi[i]);
13758a55712dSBo Jiao }
13768a55712dSBo Jiao }
13778a55712dSBo Jiao local_bh_enable();
13788a55712dSBo Jiao clear_bit(MT76_MCU_RESET, &dev->mphy.state);
13798a55712dSBo Jiao clear_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state);
13808a55712dSBo Jiao
13818a55712dSBo Jiao mt76_wr(dev, MT_INT_MASK_CSR, dev->mt76.mmio.irqmask);
13828a55712dSBo Jiao mt76_wr(dev, MT_INT_SOURCE_CSR, ~0);
13838a55712dSBo Jiao
13848a55712dSBo Jiao if (dev->hif2) {
13858a55712dSBo Jiao mt76_wr(dev, MT_INT1_MASK_CSR, dev->mt76.mmio.irqmask);
13868a55712dSBo Jiao mt76_wr(dev, MT_INT1_SOURCE_CSR, ~0);
13878a55712dSBo Jiao }
13888a55712dSBo Jiao if (dev_is_pci(mdev->dev)) {
13898a55712dSBo Jiao mt76_wr(dev, MT_PCIE_MAC_INT_ENABLE, 0xff);
13901e64fdd4SBo Jiao if (dev->hif2) {
13911e64fdd4SBo Jiao if (is_mt7915(mdev))
13928a55712dSBo Jiao mt76_wr(dev, MT_PCIE1_MAC_INT_ENABLE, 0xff);
13931e64fdd4SBo Jiao else
13941e64fdd4SBo Jiao mt76_wr(dev, MT_PCIE1_MAC_INT_ENABLE_MT7916, 0xff);
13951e64fdd4SBo Jiao }
13968a55712dSBo Jiao }
13978a55712dSBo Jiao
13988a55712dSBo Jiao /* load firmware */
13998a55712dSBo Jiao ret = mt7915_mcu_init_firmware(dev);
14008a55712dSBo Jiao if (ret)
14018a55712dSBo Jiao goto out;
14028a55712dSBo Jiao
14038a55712dSBo Jiao /* set the necessary init items */
14048a55712dSBo Jiao ret = mt7915_mcu_set_eeprom(dev);
14058a55712dSBo Jiao if (ret)
14068a55712dSBo Jiao goto out;
14078a55712dSBo Jiao
14088a55712dSBo Jiao mt7915_mac_init(dev);
140911a60bd2SShayne Chen mt7915_init_txpower(&dev->phy);
141011a60bd2SShayne Chen mt7915_init_txpower(phy2);
14118a55712dSBo Jiao ret = mt7915_txbf_init(dev);
14128a55712dSBo Jiao
14138a55712dSBo Jiao if (test_bit(MT76_STATE_RUNNING, &dev->mphy.state)) {
14148a55712dSBo Jiao ret = mt7915_run(dev->mphy.hw);
14158a55712dSBo Jiao if (ret)
14168a55712dSBo Jiao goto out;
14178a55712dSBo Jiao }
14188a55712dSBo Jiao
14198a55712dSBo Jiao if (ext_phy && test_bit(MT76_STATE_RUNNING, &ext_phy->state)) {
14208a55712dSBo Jiao ret = mt7915_run(ext_phy->hw);
14218a55712dSBo Jiao if (ret)
14228a55712dSBo Jiao goto out;
14238a55712dSBo Jiao }
14248a55712dSBo Jiao
14258a55712dSBo Jiao out:
14268a55712dSBo Jiao /* reset done */
14278a55712dSBo Jiao clear_bit(MT76_RESET, &dev->mphy.state);
14288a55712dSBo Jiao if (phy2)
14298a55712dSBo Jiao clear_bit(MT76_RESET, &phy2->mt76->state);
14308a55712dSBo Jiao
14318a55712dSBo Jiao local_bh_disable();
14328a55712dSBo Jiao napi_enable(&dev->mt76.tx_napi);
14338a55712dSBo Jiao napi_schedule(&dev->mt76.tx_napi);
14348a55712dSBo Jiao local_bh_enable();
14358a55712dSBo Jiao
14368a55712dSBo Jiao mt76_worker_enable(&dev->mt76.tx_worker);
14378a55712dSBo Jiao
14388a55712dSBo Jiao return ret;
14398a55712dSBo Jiao }
14408a55712dSBo Jiao
14418a55712dSBo Jiao static void
mt7915_mac_full_reset(struct mt7915_dev * dev)14428a55712dSBo Jiao mt7915_mac_full_reset(struct mt7915_dev *dev)
14438a55712dSBo Jiao {
14448a55712dSBo Jiao struct mt76_phy *ext_phy;
14458a55712dSBo Jiao int i;
14468a55712dSBo Jiao
14478a55712dSBo Jiao ext_phy = dev->mt76.phys[MT_BAND1];
14488a55712dSBo Jiao
14498a55712dSBo Jiao dev->recovery.hw_full_reset = true;
14508a55712dSBo Jiao
14518351a4a4SFelix Fietkau set_bit(MT76_MCU_RESET, &dev->mphy.state);
14528a55712dSBo Jiao wake_up(&dev->mt76.mcu.wait);
14538a55712dSBo Jiao ieee80211_stop_queues(mt76_hw(dev));
14548a55712dSBo Jiao if (ext_phy)
14558a55712dSBo Jiao ieee80211_stop_queues(ext_phy->hw);
14568a55712dSBo Jiao
14578a55712dSBo Jiao cancel_delayed_work_sync(&dev->mphy.mac_work);
14588a55712dSBo Jiao if (ext_phy)
14598a55712dSBo Jiao cancel_delayed_work_sync(&ext_phy->mac_work);
14608a55712dSBo Jiao
14618a55712dSBo Jiao mutex_lock(&dev->mt76.mutex);
14628a55712dSBo Jiao for (i = 0; i < 10; i++) {
14638a55712dSBo Jiao if (!mt7915_mac_restart(dev))
14648a55712dSBo Jiao break;
14658a55712dSBo Jiao }
14668a55712dSBo Jiao
14678a55712dSBo Jiao if (i == 10)
14688a55712dSBo Jiao dev_err(dev->mt76.dev, "chip full reset failed\n");
14698a55712dSBo Jiao
1470328e35c7SFelix Fietkau spin_lock_bh(&dev->mt76.sta_poll_lock);
1471328e35c7SFelix Fietkau while (!list_empty(&dev->mt76.sta_poll_list))
1472328e35c7SFelix Fietkau list_del_init(dev->mt76.sta_poll_list.next);
1473328e35c7SFelix Fietkau spin_unlock_bh(&dev->mt76.sta_poll_lock);
1474328e35c7SFelix Fietkau
1475328e35c7SFelix Fietkau memset(dev->mt76.wcid_mask, 0, sizeof(dev->mt76.wcid_mask));
1476328e35c7SFelix Fietkau dev->mt76.vif_mask = 0;
1477328e35c7SFelix Fietkau
1478328e35c7SFelix Fietkau i = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7915_WTBL_STA);
1479328e35c7SFelix Fietkau dev->mt76.global_wcid.idx = i;
1480328e35c7SFelix Fietkau dev->recovery.hw_full_reset = false;
1481328e35c7SFelix Fietkau
1482328e35c7SFelix Fietkau mutex_unlock(&dev->mt76.mutex);
1483328e35c7SFelix Fietkau
14848a55712dSBo Jiao ieee80211_restart_hw(mt76_hw(dev));
14858a55712dSBo Jiao if (ext_phy)
14868a55712dSBo Jiao ieee80211_restart_hw(ext_phy->hw);
14878a55712dSBo Jiao }
14888a55712dSBo Jiao
1489e57b7901SRyder Lee /* system error recovery */
mt7915_mac_reset_work(struct work_struct * work)1490e57b7901SRyder Lee void mt7915_mac_reset_work(struct work_struct *work)
1491e57b7901SRyder Lee {
149257b9df6fSRyder Lee struct mt7915_phy *phy2;
149357b9df6fSRyder Lee struct mt76_phy *ext_phy;
1494e57b7901SRyder Lee struct mt7915_dev *dev;
1495ef55564eSRyder Lee int i;
1496e57b7901SRyder Lee
1497e57b7901SRyder Lee dev = container_of(work, struct mt7915_dev, reset_work);
1498dc44c45cSLorenzo Bianconi ext_phy = dev->mt76.phys[MT_BAND1];
149957b9df6fSRyder Lee phy2 = ext_phy ? ext_phy->priv : NULL;
1500e57b7901SRyder Lee
15018a55712dSBo Jiao /* chip full reset */
15028a55712dSBo Jiao if (dev->recovery.restart) {
15038a55712dSBo Jiao /* disable WA/WM WDT */
15048a55712dSBo Jiao mt76_clear(dev, MT_WFDMA0_MCU_HOST_INT_ENA,
15058a55712dSBo Jiao MT_MCU_CMD_WDT_MASK);
15068a55712dSBo Jiao
1507b662b71aSRyder Lee if (READ_ONCE(dev->recovery.state) & MT_MCU_CMD_WA_WDT)
1508b662b71aSRyder Lee dev->recovery.wa_reset_count++;
1509b662b71aSRyder Lee else
1510b662b71aSRyder Lee dev->recovery.wm_reset_count++;
1511b662b71aSRyder Lee
15128a55712dSBo Jiao mt7915_mac_full_reset(dev);
15138a55712dSBo Jiao
15148a55712dSBo Jiao /* enable mcu irq */
15158a55712dSBo Jiao mt7915_irq_enable(dev, MT_INT_MCU_CMD);
15168a55712dSBo Jiao mt7915_irq_disable(dev, 0);
15178a55712dSBo Jiao
15188a55712dSBo Jiao /* enable WA/WM WDT */
15198a55712dSBo Jiao mt76_set(dev, MT_WFDMA0_MCU_HOST_INT_ENA, MT_MCU_CMD_WDT_MASK);
15208a55712dSBo Jiao
15218a55712dSBo Jiao dev->recovery.state = MT_MCU_CMD_NORMAL_STATE;
15228a55712dSBo Jiao dev->recovery.restart = false;
15238a55712dSBo Jiao return;
15248a55712dSBo Jiao }
15258a55712dSBo Jiao
15268a55712dSBo Jiao /* chip partial reset */
15278a55712dSBo Jiao if (!(READ_ONCE(dev->recovery.state) & MT_MCU_CMD_STOP_DMA))
1528e57b7901SRyder Lee return;
1529e57b7901SRyder Lee
1530e57b7901SRyder Lee ieee80211_stop_queues(mt76_hw(dev));
153157b9df6fSRyder Lee if (ext_phy)
153257b9df6fSRyder Lee ieee80211_stop_queues(ext_phy->hw);
1533e57b7901SRyder Lee
1534e57b7901SRyder Lee set_bit(MT76_RESET, &dev->mphy.state);
1535e57b7901SRyder Lee set_bit(MT76_MCU_RESET, &dev->mphy.state);
1536e57b7901SRyder Lee wake_up(&dev->mt76.mcu.wait);
1537a782f8bfSLorenzo Bianconi cancel_delayed_work_sync(&dev->mphy.mac_work);
153866365392SRyder Lee if (phy2) {
153966365392SRyder Lee set_bit(MT76_RESET, &phy2->mt76->state);
1540a782f8bfSLorenzo Bianconi cancel_delayed_work_sync(&phy2->mt76->mac_work);
154166365392SRyder Lee }
15428f7152f1SFelix Fietkau
15438f7152f1SFelix Fietkau mutex_lock(&dev->mt76.mutex);
15448f7152f1SFelix Fietkau
1545781eef5bSFelix Fietkau mt76_worker_disable(&dev->mt76.tx_worker);
1546ef55564eSRyder Lee mt76_for_each_q_rx(&dev->mt76, i)
1547ef55564eSRyder Lee napi_disable(&dev->mt76.napi[i]);
1548e57b7901SRyder Lee napi_disable(&dev->mt76.tx_napi);
1549e57b7901SRyder Lee
1550e57b7901SRyder Lee
1551350f63c9SFelix Fietkau if (mtk_wed_device_active(&dev->mt76.mmio.wed))
1552350f63c9SFelix Fietkau mtk_wed_device_stop(&dev->mt76.mmio.wed);
1553350f63c9SFelix Fietkau
1554e57b7901SRyder Lee mt76_wr(dev, MT_MCU_INT_EVENT, MT_MCU_INT_EVENT_DMA_STOPPED);
1555e57b7901SRyder Lee
1556e57b7901SRyder Lee if (mt7915_wait_reset_state(dev, MT_MCU_CMD_RESET_DONE)) {
1557d493bb5bSBo Jiao mt7915_dma_reset(dev, false);
1558e57b7901SRyder Lee
1559c8e370feSLorenzo Bianconi mt76_connac2_tx_token_put(&dev->mt76);
1560b17aff33SLorenzo Bianconi idr_init(&dev->mt76.token);
15616362dd16SRyder Lee
1562e57b7901SRyder Lee mt76_wr(dev, MT_MCU_INT_EVENT, MT_MCU_INT_EVENT_DMA_INIT);
1563e57b7901SRyder Lee mt7915_wait_reset_state(dev, MT_MCU_CMD_RECOVERY_DONE);
1564e57b7901SRyder Lee }
1565e57b7901SRyder Lee
15661e64fdd4SBo Jiao mt76_wr(dev, MT_MCU_INT_EVENT, MT_MCU_INT_EVENT_RESET_DONE);
15671e64fdd4SBo Jiao mt7915_wait_reset_state(dev, MT_MCU_CMD_NORMAL_STATE);
15681e64fdd4SBo Jiao
15691e64fdd4SBo Jiao /* enable DMA Tx/Rx and interrupt */
15701e64fdd4SBo Jiao mt7915_dma_start(dev, false, false);
15711e64fdd4SBo Jiao
1572e57b7901SRyder Lee clear_bit(MT76_MCU_RESET, &dev->mphy.state);
1573e57b7901SRyder Lee clear_bit(MT76_RESET, &dev->mphy.state);
157466365392SRyder Lee if (phy2)
157566365392SRyder Lee clear_bit(MT76_RESET, &phy2->mt76->state);
1576e57b7901SRyder Lee
1577970be1dfSFelix Fietkau local_bh_disable();
1578ef55564eSRyder Lee mt76_for_each_q_rx(&dev->mt76, i) {
1579ef55564eSRyder Lee napi_enable(&dev->mt76.napi[i]);
1580ef55564eSRyder Lee napi_schedule(&dev->mt76.napi[i]);
1581ef55564eSRyder Lee }
1582970be1dfSFelix Fietkau local_bh_enable();
1583970be1dfSFelix Fietkau
1584ec193b41SLorenzo Bianconi tasklet_schedule(&dev->mt76.irq_tasklet);
15857f731405SFelix Fietkau
15867f731405SFelix Fietkau mt76_worker_enable(&dev->mt76.tx_worker);
15877f731405SFelix Fietkau
1588c0182aa9SFelix Fietkau local_bh_disable();
15897f731405SFelix Fietkau napi_enable(&dev->mt76.tx_napi);
15907f731405SFelix Fietkau napi_schedule(&dev->mt76.tx_napi);
1591c0182aa9SFelix Fietkau local_bh_enable();
1592e57b7901SRyder Lee
1593e57b7901SRyder Lee ieee80211_wake_queues(mt76_hw(dev));
159457b9df6fSRyder Lee if (ext_phy)
159557b9df6fSRyder Lee ieee80211_wake_queues(ext_phy->hw);
1596e57b7901SRyder Lee
1597e57b7901SRyder Lee mutex_unlock(&dev->mt76.mutex);
1598e57b7901SRyder Lee
1599e57b7901SRyder Lee mt7915_update_beacons(dev);
1600e57b7901SRyder Lee
1601a782f8bfSLorenzo Bianconi ieee80211_queue_delayed_work(mt76_hw(dev), &dev->mphy.mac_work,
160257b9df6fSRyder Lee MT7915_WATCHDOG_TIME);
160357b9df6fSRyder Lee if (phy2)
1604a782f8bfSLorenzo Bianconi ieee80211_queue_delayed_work(ext_phy->hw,
1605a782f8bfSLorenzo Bianconi &phy2->mt76->mac_work,
1606e57b7901SRyder Lee MT7915_WATCHDOG_TIME);
1607e57b7901SRyder Lee }
1608e57b7901SRyder Lee
16094dbcb912SRyder Lee /* firmware coredump */
mt7915_mac_dump_work(struct work_struct * work)16104dbcb912SRyder Lee void mt7915_mac_dump_work(struct work_struct *work)
16114dbcb912SRyder Lee {
16124dbcb912SRyder Lee const struct mt7915_mem_region *mem_region;
16134dbcb912SRyder Lee struct mt7915_crash_data *crash_data;
16144dbcb912SRyder Lee struct mt7915_dev *dev;
16154dbcb912SRyder Lee struct mt7915_mem_hdr *hdr;
16164dbcb912SRyder Lee size_t buf_len;
16174dbcb912SRyder Lee int i;
16184dbcb912SRyder Lee u32 num;
16194dbcb912SRyder Lee u8 *buf;
16204dbcb912SRyder Lee
16214dbcb912SRyder Lee dev = container_of(work, struct mt7915_dev, dump_work);
16224dbcb912SRyder Lee
16234dbcb912SRyder Lee mutex_lock(&dev->dump_mutex);
16244dbcb912SRyder Lee
16254dbcb912SRyder Lee crash_data = mt7915_coredump_new(dev);
16264dbcb912SRyder Lee if (!crash_data) {
16274dbcb912SRyder Lee mutex_unlock(&dev->dump_mutex);
16284dbcb912SRyder Lee goto skip_coredump;
16294dbcb912SRyder Lee }
16304dbcb912SRyder Lee
16314dbcb912SRyder Lee mem_region = mt7915_coredump_get_mem_layout(dev, &num);
16324dbcb912SRyder Lee if (!mem_region || !crash_data->memdump_buf_len) {
16334dbcb912SRyder Lee mutex_unlock(&dev->dump_mutex);
16344dbcb912SRyder Lee goto skip_memdump;
16354dbcb912SRyder Lee }
16364dbcb912SRyder Lee
16374dbcb912SRyder Lee buf = crash_data->memdump_buf;
16384dbcb912SRyder Lee buf_len = crash_data->memdump_buf_len;
16394dbcb912SRyder Lee
16404dbcb912SRyder Lee /* dumping memory content... */
16414dbcb912SRyder Lee memset(buf, 0, buf_len);
16424dbcb912SRyder Lee for (i = 0; i < num; i++) {
16434dbcb912SRyder Lee if (mem_region->len > buf_len) {
16444dbcb912SRyder Lee dev_warn(dev->mt76.dev, "%s len %lu is too large\n",
16454dbcb912SRyder Lee mem_region->name,
16464dbcb912SRyder Lee (unsigned long)mem_region->len);
16474dbcb912SRyder Lee break;
16484dbcb912SRyder Lee }
16494dbcb912SRyder Lee
16504dbcb912SRyder Lee /* reserve space for the header */
16514dbcb912SRyder Lee hdr = (void *)buf;
16524dbcb912SRyder Lee buf += sizeof(*hdr);
16534dbcb912SRyder Lee buf_len -= sizeof(*hdr);
16544dbcb912SRyder Lee
16554dbcb912SRyder Lee mt7915_memcpy_fromio(dev, buf, mem_region->start,
16564dbcb912SRyder Lee mem_region->len);
16574dbcb912SRyder Lee
16584dbcb912SRyder Lee hdr->start = mem_region->start;
16594dbcb912SRyder Lee hdr->len = mem_region->len;
16604dbcb912SRyder Lee
16614dbcb912SRyder Lee if (!mem_region->len)
16624dbcb912SRyder Lee /* note: the header remains, just with zero length */
16634dbcb912SRyder Lee break;
16644dbcb912SRyder Lee
16654dbcb912SRyder Lee buf += mem_region->len;
16664dbcb912SRyder Lee buf_len -= mem_region->len;
16674dbcb912SRyder Lee
16684dbcb912SRyder Lee mem_region++;
16694dbcb912SRyder Lee }
16704dbcb912SRyder Lee
16714dbcb912SRyder Lee mutex_unlock(&dev->dump_mutex);
16724dbcb912SRyder Lee
16734dbcb912SRyder Lee skip_memdump:
16744dbcb912SRyder Lee mt7915_coredump_submit(dev);
16754dbcb912SRyder Lee skip_coredump:
16764dbcb912SRyder Lee queue_work(dev->mt76.wq, &dev->reset_work);
16774dbcb912SRyder Lee }
16784dbcb912SRyder Lee
mt7915_reset(struct mt7915_dev * dev)16798a55712dSBo Jiao void mt7915_reset(struct mt7915_dev *dev)
16808a55712dSBo Jiao {
16818a55712dSBo Jiao if (!dev->recovery.hw_init_done)
16828a55712dSBo Jiao return;
16838a55712dSBo Jiao
16848a55712dSBo Jiao if (dev->recovery.hw_full_reset)
16858a55712dSBo Jiao return;
16868a55712dSBo Jiao
16878a55712dSBo Jiao /* wm/wa exception: do full recovery */
16888a55712dSBo Jiao if (READ_ONCE(dev->recovery.state) & MT_MCU_CMD_WDT_MASK) {
16898a55712dSBo Jiao dev->recovery.restart = true;
16908a55712dSBo Jiao dev_info(dev->mt76.dev,
16918a55712dSBo Jiao "%s indicated firmware crash, attempting recovery\n",
16928a55712dSBo Jiao wiphy_name(dev->mt76.hw->wiphy));
16938a55712dSBo Jiao
16948a55712dSBo Jiao mt7915_irq_disable(dev, MT_INT_MCU_CMD);
16954dbcb912SRyder Lee queue_work(dev->mt76.wq, &dev->dump_work);
16968a55712dSBo Jiao return;
16978a55712dSBo Jiao }
16988a55712dSBo Jiao
1699*b13cd593SFelix Fietkau if ((READ_ONCE(dev->recovery.state) & MT_MCU_CMD_STOP_DMA)) {
1700*b13cd593SFelix Fietkau set_bit(MT76_MCU_RESET, &dev->mphy.state);
1701*b13cd593SFelix Fietkau wake_up(&dev->mt76.mcu.wait);
1702*b13cd593SFelix Fietkau }
1703*b13cd593SFelix Fietkau
17048a55712dSBo Jiao queue_work(dev->mt76.wq, &dev->reset_work);
17058a55712dSBo Jiao wake_up(&dev->reset_wait);
17068a55712dSBo Jiao }
17078a55712dSBo Jiao
mt7915_mac_update_stats(struct mt7915_phy * phy)170881811173SLorenzo Bianconi void mt7915_mac_update_stats(struct mt7915_phy *phy)
1709e57b7901SRyder Lee {
17107f03a563SLorenzo Bianconi struct mt76_mib_stats *mib = &phy->mib;
1711e57b7901SRyder Lee struct mt7915_dev *dev = phy->dev;
1712d107501aSLorenzo Bianconi int i, aggr0 = 0, aggr1, cnt;
17133eb50cc9SRyder Lee u8 band = phy->mt76->band_idx;
1714cd4c314aSBo Jiao u32 val;
1715e57b7901SRyder Lee
17163eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDR3(band));
17173685727cSRyder Lee mib->fcs_err_cnt += is_mt7915(&dev->mt76) ?
17183685727cSRyder Lee FIELD_GET(MT_MIB_SDR3_FCS_ERR_MASK, cnt) :
1719cd4c314aSBo Jiao FIELD_GET(MT_MIB_SDR3_FCS_ERR_MASK_MT7916, cnt);
1720a90f2115SBen Greear
17213eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDR4(band));
1722a90f2115SBen Greear mib->rx_fifo_full_cnt += FIELD_GET(MT_MIB_SDR4_RX_FIFO_FULL_MASK, cnt);
1723a90f2115SBen Greear
17243eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDR5(band));
1725a90f2115SBen Greear mib->rx_mpdu_cnt += cnt;
1726a90f2115SBen Greear
17273eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDR6(band));
1728a90f2115SBen Greear mib->channel_idle_cnt += FIELD_GET(MT_MIB_SDR6_CHANNEL_IDL_CNT_MASK, cnt);
1729a90f2115SBen Greear
17303eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDR7(band));
17313685727cSRyder Lee mib->rx_vector_mismatch_cnt +=
17323685727cSRyder Lee FIELD_GET(MT_MIB_SDR7_RX_VECTOR_MISMATCH_CNT_MASK, cnt);
1733a90f2115SBen Greear
17343eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDR8(band));
17353685727cSRyder Lee mib->rx_delimiter_fail_cnt +=
17363685727cSRyder Lee FIELD_GET(MT_MIB_SDR8_RX_DELIMITER_FAIL_CNT_MASK, cnt);
17373685727cSRyder Lee
17383eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDR10(band));
17393685727cSRyder Lee mib->rx_mrdy_cnt += is_mt7915(&dev->mt76) ?
17403685727cSRyder Lee FIELD_GET(MT_MIB_SDR10_MRDY_COUNT_MASK, cnt) :
17413685727cSRyder Lee FIELD_GET(MT_MIB_SDR10_MRDY_COUNT_MASK_MT7916, cnt);
1742a90f2115SBen Greear
17433eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDR11(band));
17443685727cSRyder Lee mib->rx_len_mismatch_cnt +=
17453685727cSRyder Lee FIELD_GET(MT_MIB_SDR11_RX_LEN_MISMATCH_CNT_MASK, cnt);
1746a90f2115SBen Greear
17473eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDR12(band));
1748a90f2115SBen Greear mib->tx_ampdu_cnt += cnt;
1749a90f2115SBen Greear
17503eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDR13(band));
17513685727cSRyder Lee mib->tx_stop_q_empty_cnt +=
17523685727cSRyder Lee FIELD_GET(MT_MIB_SDR13_TX_STOP_Q_EMPTY_CNT_MASK, cnt);
1753a90f2115SBen Greear
17543eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDR14(band));
1755cd4c314aSBo Jiao mib->tx_mpdu_attempts_cnt += is_mt7915(&dev->mt76) ?
1756cd4c314aSBo Jiao FIELD_GET(MT_MIB_SDR14_TX_MPDU_ATTEMPTS_CNT_MASK, cnt) :
1757cd4c314aSBo Jiao FIELD_GET(MT_MIB_SDR14_TX_MPDU_ATTEMPTS_CNT_MASK_MT7916, cnt);
1758a90f2115SBen Greear
17593eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDR15(band));
1760cd4c314aSBo Jiao mib->tx_mpdu_success_cnt += is_mt7915(&dev->mt76) ?
1761cd4c314aSBo Jiao FIELD_GET(MT_MIB_SDR15_TX_MPDU_SUCCESS_CNT_MASK, cnt) :
1762cd4c314aSBo Jiao FIELD_GET(MT_MIB_SDR15_TX_MPDU_SUCCESS_CNT_MASK_MT7916, cnt);
1763a90f2115SBen Greear
17643eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDR16(band));
17653685727cSRyder Lee mib->primary_cca_busy_time +=
17663685727cSRyder Lee FIELD_GET(MT_MIB_SDR16_PRIMARY_CCA_BUSY_TIME_MASK, cnt);
17673685727cSRyder Lee
17683eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDR17(band));
17693685727cSRyder Lee mib->secondary_cca_busy_time +=
17703685727cSRyder Lee FIELD_GET(MT_MIB_SDR17_SECONDARY_CCA_BUSY_TIME_MASK, cnt);
17713685727cSRyder Lee
17723eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDR18(band));
17733685727cSRyder Lee mib->primary_energy_detect_time +=
17743685727cSRyder Lee FIELD_GET(MT_MIB_SDR18_PRIMARY_ENERGY_DETECT_TIME_MASK, cnt);
17753685727cSRyder Lee
17763eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDR19(band));
17773685727cSRyder Lee mib->cck_mdrdy_time += FIELD_GET(MT_MIB_SDR19_CCK_MDRDY_TIME_MASK, cnt);
17783685727cSRyder Lee
17793eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDR20(band));
17803685727cSRyder Lee mib->ofdm_mdrdy_time +=
17813685727cSRyder Lee FIELD_GET(MT_MIB_SDR20_OFDM_VHT_MDRDY_TIME_MASK, cnt);
17823685727cSRyder Lee
17833eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDR21(band));
17843685727cSRyder Lee mib->green_mdrdy_time +=
17853685727cSRyder Lee FIELD_GET(MT_MIB_SDR21_GREEN_MDRDY_TIME_MASK, cnt);
17863685727cSRyder Lee
17873eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDR22(band));
1788a90f2115SBen Greear mib->rx_ampdu_cnt += cnt;
1789a90f2115SBen Greear
17903eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDR23(band));
1791a90f2115SBen Greear mib->rx_ampdu_bytes_cnt += cnt;
1792a90f2115SBen Greear
17933eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDR24(band));
1794cd4c314aSBo Jiao mib->rx_ampdu_valid_subframe_cnt += is_mt7915(&dev->mt76) ?
1795cd4c314aSBo Jiao FIELD_GET(MT_MIB_SDR24_RX_AMPDU_SF_CNT_MASK, cnt) :
1796cd4c314aSBo Jiao FIELD_GET(MT_MIB_SDR24_RX_AMPDU_SF_CNT_MASK_MT7916, cnt);
1797a90f2115SBen Greear
17983eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDR25(band));
1799a90f2115SBen Greear mib->rx_ampdu_valid_subframe_bytes_cnt += cnt;
1800a90f2115SBen Greear
18013eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDR27(band));
18023685727cSRyder Lee mib->tx_rwp_fail_cnt +=
18033685727cSRyder Lee FIELD_GET(MT_MIB_SDR27_TX_RWP_FAIL_CNT_MASK, cnt);
1804a90f2115SBen Greear
18053eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDR28(band));
18063685727cSRyder Lee mib->tx_rwp_need_cnt +=
18073685727cSRyder Lee FIELD_GET(MT_MIB_SDR28_TX_RWP_NEED_CNT_MASK, cnt);
1808a90f2115SBen Greear
18093eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDR29(band));
1810cd4c314aSBo Jiao mib->rx_pfdrop_cnt += is_mt7915(&dev->mt76) ?
1811cd4c314aSBo Jiao FIELD_GET(MT_MIB_SDR29_RX_PFDROP_CNT_MASK, cnt) :
1812cd4c314aSBo Jiao FIELD_GET(MT_MIB_SDR29_RX_PFDROP_CNT_MASK_MT7916, cnt);
1813a90f2115SBen Greear
18143eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDRVEC(band));
1815cd4c314aSBo Jiao mib->rx_vec_queue_overflow_drop_cnt += is_mt7915(&dev->mt76) ?
1816cd4c314aSBo Jiao FIELD_GET(MT_MIB_SDR30_RX_VEC_QUEUE_OVERFLOW_DROP_CNT_MASK, cnt) :
1817cd4c314aSBo Jiao FIELD_GET(MT_MIB_SDR30_RX_VEC_QUEUE_OVERFLOW_DROP_CNT_MASK_MT7916, cnt);
1818a90f2115SBen Greear
18193eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDR31(band));
1820a90f2115SBen Greear mib->rx_ba_cnt += cnt;
1821a90f2115SBen Greear
18223eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDRMUBF(band));
1823016f2040SBen Greear mib->tx_bf_cnt += FIELD_GET(MT_MIB_MU_BF_TX_CNT, cnt);
1824016f2040SBen Greear
18253eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_DR8(band));
1826016f2040SBen Greear mib->tx_mu_mpdu_cnt += cnt;
1827016f2040SBen Greear
18283eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_DR9(band));
1829016f2040SBen Greear mib->tx_mu_acked_mpdu_cnt += cnt;
1830016f2040SBen Greear
18313eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_DR11(band));
1832016f2040SBen Greear mib->tx_su_acked_mpdu_cnt += cnt;
1833016f2040SBen Greear
18343eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_ETBF_PAR_RPT0(band));
1835bd1407edSShayne Chen mib->tx_bf_rx_fb_bw = FIELD_GET(MT_ETBF_PAR_RPT0_FB_BW, cnt);
1836bd1407edSShayne Chen mib->tx_bf_rx_fb_nc_cnt += FIELD_GET(MT_ETBF_PAR_RPT0_FB_NC, cnt);
1837bd1407edSShayne Chen mib->tx_bf_rx_fb_nr_cnt += FIELD_GET(MT_ETBF_PAR_RPT0_FB_NR, cnt);
1838e57b7901SRyder Lee
183937dd5755SLorenzo Bianconi for (i = 0; i < ARRAY_SIZE(mib->tx_amsdu); i++) {
184037dd5755SLorenzo Bianconi cnt = mt76_rr(dev, MT_PLE_AMSDU_PACK_MSDU_CNT(i));
184137dd5755SLorenzo Bianconi mib->tx_amsdu[i] += cnt;
184237dd5755SLorenzo Bianconi mib->tx_amsdu_cnt += cnt;
184337dd5755SLorenzo Bianconi }
184437dd5755SLorenzo Bianconi
1845cd4c314aSBo Jiao if (is_mt7915(&dev->mt76)) {
1846528d13e7SLorenzo Bianconi for (i = 0, aggr1 = aggr0 + 8; i < 4; i++) {
18473eb50cc9SRyder Lee val = mt76_rr(dev, MT_MIB_MB_SDR1(band, (i << 4)));
18483685727cSRyder Lee mib->ba_miss_cnt +=
18493685727cSRyder Lee FIELD_GET(MT_MIB_BA_MISS_COUNT_MASK, val);
18502b35050aSRyder Lee mib->ack_fail_cnt +=
18512b35050aSRyder Lee FIELD_GET(MT_MIB_ACK_FAIL_COUNT_MASK, val);
1852e57b7901SRyder Lee
18533eb50cc9SRyder Lee val = mt76_rr(dev, MT_MIB_MB_SDR0(band, (i << 4)));
18542b35050aSRyder Lee mib->rts_cnt += FIELD_GET(MT_MIB_RTS_COUNT_MASK, val);
18552b35050aSRyder Lee mib->rts_retries_cnt +=
18562b35050aSRyder Lee FIELD_GET(MT_MIB_RTS_RETRIES_COUNT_MASK, val);
1857e57b7901SRyder Lee
18583eb50cc9SRyder Lee val = mt76_rr(dev, MT_TX_AGG_CNT(band, i));
1859d107501aSLorenzo Bianconi phy->mt76->aggr_stats[aggr0++] += val & 0xffff;
1860d107501aSLorenzo Bianconi phy->mt76->aggr_stats[aggr0++] += val >> 16;
18612b35050aSRyder Lee
18623eb50cc9SRyder Lee val = mt76_rr(dev, MT_TX_AGG_CNT2(band, i));
1863d107501aSLorenzo Bianconi phy->mt76->aggr_stats[aggr1++] += val & 0xffff;
1864d107501aSLorenzo Bianconi phy->mt76->aggr_stats[aggr1++] += val >> 16;
1865e57b7901SRyder Lee }
1866bd1407edSShayne Chen
18673eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDR32(band));
1868bd1407edSShayne Chen mib->tx_pkt_ebf_cnt += FIELD_GET(MT_MIB_SDR32_TX_PKT_EBF_CNT, cnt);
1869bd1407edSShayne Chen
18703eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDR33(band));
1871bd1407edSShayne Chen mib->tx_pkt_ibf_cnt += FIELD_GET(MT_MIB_SDR33_TX_PKT_IBF_CNT, cnt);
1872bd1407edSShayne Chen
18733eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_ETBF_TX_APP_CNT(band));
1874bd1407edSShayne Chen mib->tx_bf_ibf_ppdu_cnt += FIELD_GET(MT_ETBF_TX_IBF_CNT, cnt);
1875bd1407edSShayne Chen mib->tx_bf_ebf_ppdu_cnt += FIELD_GET(MT_ETBF_TX_EBF_CNT, cnt);
1876bd1407edSShayne Chen
18773eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_ETBF_TX_NDP_BFRP(band));
1878bd1407edSShayne Chen mib->tx_bf_fb_cpl_cnt += FIELD_GET(MT_ETBF_TX_FB_CPL, cnt);
1879bd1407edSShayne Chen mib->tx_bf_fb_trig_cnt += FIELD_GET(MT_ETBF_TX_FB_TRI, cnt);
1880bd1407edSShayne Chen
18813eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_ETBF_RX_FB_CNT(band));
1882bd1407edSShayne Chen mib->tx_bf_rx_fb_all_cnt += FIELD_GET(MT_ETBF_RX_FB_ALL, cnt);
1883bd1407edSShayne Chen mib->tx_bf_rx_fb_he_cnt += FIELD_GET(MT_ETBF_RX_FB_HE, cnt);
1884bd1407edSShayne Chen mib->tx_bf_rx_fb_vht_cnt += FIELD_GET(MT_ETBF_RX_FB_VHT, cnt);
1885bd1407edSShayne Chen mib->tx_bf_rx_fb_ht_cnt += FIELD_GET(MT_ETBF_RX_FB_HT, cnt);
1886cd4c314aSBo Jiao } else {
1887cd4c314aSBo Jiao for (i = 0; i < 2; i++) {
1888cd4c314aSBo Jiao /* rts count */
18893eb50cc9SRyder Lee val = mt76_rr(dev, MT_MIB_MB_SDR0(band, (i << 2)));
1890cd4c314aSBo Jiao mib->rts_cnt += FIELD_GET(GENMASK(15, 0), val);
1891cd4c314aSBo Jiao mib->rts_cnt += FIELD_GET(GENMASK(31, 16), val);
1892cd4c314aSBo Jiao
1893cd4c314aSBo Jiao /* rts retry count */
18943eb50cc9SRyder Lee val = mt76_rr(dev, MT_MIB_MB_SDR1(band, (i << 2)));
1895cd4c314aSBo Jiao mib->rts_retries_cnt += FIELD_GET(GENMASK(15, 0), val);
1896cd4c314aSBo Jiao mib->rts_retries_cnt += FIELD_GET(GENMASK(31, 16), val);
1897cd4c314aSBo Jiao
1898cd4c314aSBo Jiao /* ba miss count */
18993eb50cc9SRyder Lee val = mt76_rr(dev, MT_MIB_MB_SDR2(band, (i << 2)));
1900cd4c314aSBo Jiao mib->ba_miss_cnt += FIELD_GET(GENMASK(15, 0), val);
1901cd4c314aSBo Jiao mib->ba_miss_cnt += FIELD_GET(GENMASK(31, 16), val);
1902cd4c314aSBo Jiao
1903cd4c314aSBo Jiao /* ack fail count */
19043eb50cc9SRyder Lee val = mt76_rr(dev, MT_MIB_MB_BFTF(band, (i << 2)));
1905cd4c314aSBo Jiao mib->ack_fail_cnt += FIELD_GET(GENMASK(15, 0), val);
1906cd4c314aSBo Jiao mib->ack_fail_cnt += FIELD_GET(GENMASK(31, 16), val);
1907cd4c314aSBo Jiao }
1908cd4c314aSBo Jiao
1909cd4c314aSBo Jiao for (i = 0; i < 8; i++) {
19103eb50cc9SRyder Lee val = mt76_rr(dev, MT_TX_AGG_CNT(band, i));
1911d107501aSLorenzo Bianconi phy->mt76->aggr_stats[aggr0++] += FIELD_GET(GENMASK(15, 0), val);
1912d107501aSLorenzo Bianconi phy->mt76->aggr_stats[aggr0++] += FIELD_GET(GENMASK(31, 16), val);
1913cd4c314aSBo Jiao }
1914bd1407edSShayne Chen
19153eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_SDR32(band));
1916bd1407edSShayne Chen mib->tx_pkt_ibf_cnt += FIELD_GET(MT_MIB_SDR32_TX_PKT_IBF_CNT, cnt);
1917bd1407edSShayne Chen mib->tx_bf_ibf_ppdu_cnt += FIELD_GET(MT_MIB_SDR32_TX_PKT_IBF_CNT, cnt);
1918bd1407edSShayne Chen mib->tx_pkt_ebf_cnt += FIELD_GET(MT_MIB_SDR32_TX_PKT_EBF_CNT, cnt);
1919bd1407edSShayne Chen mib->tx_bf_ebf_ppdu_cnt += FIELD_GET(MT_MIB_SDR32_TX_PKT_EBF_CNT, cnt);
1920bd1407edSShayne Chen
19213eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_BFCR7(band));
1922bd1407edSShayne Chen mib->tx_bf_fb_cpl_cnt += FIELD_GET(MT_MIB_BFCR7_BFEE_TX_FB_CPL, cnt);
1923bd1407edSShayne Chen
19243eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_BFCR2(band));
1925bd1407edSShayne Chen mib->tx_bf_fb_trig_cnt += FIELD_GET(MT_MIB_BFCR2_BFEE_TX_FB_TRIG, cnt);
1926bd1407edSShayne Chen
19273eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_BFCR0(band));
1928bd1407edSShayne Chen mib->tx_bf_rx_fb_vht_cnt += FIELD_GET(MT_MIB_BFCR0_RX_FB_VHT, cnt);
1929bd1407edSShayne Chen mib->tx_bf_rx_fb_all_cnt += FIELD_GET(MT_MIB_BFCR0_RX_FB_VHT, cnt);
1930bd1407edSShayne Chen mib->tx_bf_rx_fb_ht_cnt += FIELD_GET(MT_MIB_BFCR0_RX_FB_HT, cnt);
1931bd1407edSShayne Chen mib->tx_bf_rx_fb_all_cnt += FIELD_GET(MT_MIB_BFCR0_RX_FB_HT, cnt);
1932bd1407edSShayne Chen
19333eb50cc9SRyder Lee cnt = mt76_rr(dev, MT_MIB_BFCR1(band));
1934bd1407edSShayne Chen mib->tx_bf_rx_fb_he_cnt += FIELD_GET(MT_MIB_BFCR1_RX_FB_HE, cnt);
1935bd1407edSShayne Chen mib->tx_bf_rx_fb_all_cnt += FIELD_GET(MT_MIB_BFCR1_RX_FB_HE, cnt);
1936cd4c314aSBo Jiao }
1937e57b7901SRyder Lee }
1938e57b7901SRyder Lee
mt7915_mac_severe_check(struct mt7915_phy * phy)1939b4c268caSRyder Lee static void mt7915_mac_severe_check(struct mt7915_phy *phy)
1940b4c268caSRyder Lee {
1941b4c268caSRyder Lee struct mt7915_dev *dev = phy->dev;
1942b4c268caSRyder Lee u32 trb;
1943b4c268caSRyder Lee
1944b4c268caSRyder Lee if (!phy->omac_mask)
1945b4c268caSRyder Lee return;
1946b4c268caSRyder Lee
1947b4c268caSRyder Lee /* In rare cases, TRB pointers might be out of sync leads to RMAC
1948b4c268caSRyder Lee * stopping Rx, so check status periodically to see if TRB hardware
1949b4c268caSRyder Lee * requires minimal recovery.
1950b4c268caSRyder Lee */
19513eb50cc9SRyder Lee trb = mt76_rr(dev, MT_TRB_RXPSR0(phy->mt76->band_idx));
1952b4c268caSRyder Lee
1953b4c268caSRyder Lee if ((FIELD_GET(MT_TRB_RXPSR0_RX_RMAC_PTR, trb) !=
1954b4c268caSRyder Lee FIELD_GET(MT_TRB_RXPSR0_RX_WTBL_PTR, trb)) &&
1955b4c268caSRyder Lee (FIELD_GET(MT_TRB_RXPSR0_RX_RMAC_PTR, phy->trb_ts) !=
1956b4c268caSRyder Lee FIELD_GET(MT_TRB_RXPSR0_RX_WTBL_PTR, phy->trb_ts)) &&
1957b4c268caSRyder Lee trb == phy->trb_ts)
1958b4c268caSRyder Lee mt7915_mcu_set_ser(dev, SER_RECOVER, SER_SET_RECOVER_L3_RX_ABORT,
19593eb50cc9SRyder Lee phy->mt76->band_idx);
1960b4c268caSRyder Lee
1961b4c268caSRyder Lee phy->trb_ts = trb;
1962b4c268caSRyder Lee }
1963b4c268caSRyder Lee
mt7915_mac_sta_rc_work(struct work_struct * work)19641daf2522SFelix Fietkau void mt7915_mac_sta_rc_work(struct work_struct *work)
19651daf2522SFelix Fietkau {
19661daf2522SFelix Fietkau struct mt7915_dev *dev = container_of(work, struct mt7915_dev, rc_work);
19671daf2522SFelix Fietkau struct ieee80211_sta *sta;
19681daf2522SFelix Fietkau struct ieee80211_vif *vif;
19691daf2522SFelix Fietkau struct mt7915_sta *msta;
19701daf2522SFelix Fietkau u32 changed;
19711daf2522SFelix Fietkau LIST_HEAD(list);
19721daf2522SFelix Fietkau
1973fbba711cSLorenzo Bianconi spin_lock_bh(&dev->mt76.sta_poll_lock);
19741daf2522SFelix Fietkau list_splice_init(&dev->sta_rc_list, &list);
19751daf2522SFelix Fietkau
19761daf2522SFelix Fietkau while (!list_empty(&list)) {
19771daf2522SFelix Fietkau msta = list_first_entry(&list, struct mt7915_sta, rc_list);
19781daf2522SFelix Fietkau list_del_init(&msta->rc_list);
197905909e46SRyder Lee changed = msta->changed;
198005909e46SRyder Lee msta->changed = 0;
1981fbba711cSLorenzo Bianconi spin_unlock_bh(&dev->mt76.sta_poll_lock);
19821daf2522SFelix Fietkau
19831daf2522SFelix Fietkau sta = container_of((void *)msta, struct ieee80211_sta, drv_priv);
19841daf2522SFelix Fietkau vif = container_of((void *)msta->vif, struct ieee80211_vif, drv_priv);
19851daf2522SFelix Fietkau
19861daf2522SFelix Fietkau if (changed & (IEEE80211_RC_SUPP_RATES_CHANGED |
1987e57b7901SRyder Lee IEEE80211_RC_NSS_CHANGED |
19882eec60dcSRyder Lee IEEE80211_RC_BW_CHANGED))
19892eec60dcSRyder Lee mt7915_mcu_add_rate_ctrl(dev, vif, sta, true);
1990e57b7901SRyder Lee
19911daf2522SFelix Fietkau if (changed & IEEE80211_RC_SMPS_CHANGED)
1992e57b7901SRyder Lee mt7915_mcu_add_smps(dev, vif, sta);
1993e57b7901SRyder Lee
1994fbba711cSLorenzo Bianconi spin_lock_bh(&dev->mt76.sta_poll_lock);
19951daf2522SFelix Fietkau }
19961daf2522SFelix Fietkau
1997fbba711cSLorenzo Bianconi spin_unlock_bh(&dev->mt76.sta_poll_lock);
1998e57b7901SRyder Lee }
1999e57b7901SRyder Lee
mt7915_mac_work(struct work_struct * work)2000e57b7901SRyder Lee void mt7915_mac_work(struct work_struct *work)
2001e57b7901SRyder Lee {
200257b9df6fSRyder Lee struct mt7915_phy *phy;
2003a782f8bfSLorenzo Bianconi struct mt76_phy *mphy;
2004e57b7901SRyder Lee
2005a782f8bfSLorenzo Bianconi mphy = (struct mt76_phy *)container_of(work, struct mt76_phy,
2006e57b7901SRyder Lee mac_work.work);
2007a782f8bfSLorenzo Bianconi phy = mphy->priv;
2008e57b7901SRyder Lee
2009a782f8bfSLorenzo Bianconi mutex_lock(&mphy->dev->mutex);
2010e57b7901SRyder Lee
2011c560b137SRyder Lee mt76_update_survey(mphy);
2012a782f8bfSLorenzo Bianconi if (++mphy->mac_work_count == 5) {
2013a782f8bfSLorenzo Bianconi mphy->mac_work_count = 0;
2014e57b7901SRyder Lee
201565430028SRyder Lee mt7915_mac_update_stats(phy);
2016b4c268caSRyder Lee mt7915_mac_severe_check(phy);
20171258c156SRyder Lee
20181258c156SRyder Lee if (phy->dev->muru_debug)
20191258c156SRyder Lee mt7915_mcu_muru_debug_get(phy);
2020e57b7901SRyder Lee }
2021e57b7901SRyder Lee
2022a782f8bfSLorenzo Bianconi mutex_unlock(&mphy->dev->mutex);
202357b9df6fSRyder Lee
2024c02f86eeSLorenzo Bianconi mt76_tx_status_check(mphy->dev, false);
20253de4cb17SFelix Fietkau
2026a782f8bfSLorenzo Bianconi ieee80211_queue_delayed_work(mphy->hw, &mphy->mac_work,
2027e57b7901SRyder Lee MT7915_WATCHDOG_TIME);
2028e57b7901SRyder Lee }
2029e57b7901SRyder Lee
mt7915_dfs_stop_radar_detector(struct mt7915_phy * phy)2030e57b7901SRyder Lee static void mt7915_dfs_stop_radar_detector(struct mt7915_phy *phy)
2031e57b7901SRyder Lee {
2032e57b7901SRyder Lee struct mt7915_dev *dev = phy->dev;
2033e57b7901SRyder Lee
2034e57b7901SRyder Lee if (phy->rdd_state & BIT(0))
203597cef84dSLorenzo Bianconi mt76_connac_mcu_rdd_cmd(&dev->mt76, RDD_STOP, 0,
203697cef84dSLorenzo Bianconi MT_RX_SEL0, 0);
2037e57b7901SRyder Lee if (phy->rdd_state & BIT(1))
203897cef84dSLorenzo Bianconi mt76_connac_mcu_rdd_cmd(&dev->mt76, RDD_STOP, 1,
203997cef84dSLorenzo Bianconi MT_RX_SEL0, 0);
2040e57b7901SRyder Lee }
2041e57b7901SRyder Lee
mt7915_dfs_start_rdd(struct mt7915_dev * dev,int chain)2042e57b7901SRyder Lee static int mt7915_dfs_start_rdd(struct mt7915_dev *dev, int chain)
2043e57b7901SRyder Lee {
2044233e39d1SEvelyn Tsai int err, region;
2045233e39d1SEvelyn Tsai
2046233e39d1SEvelyn Tsai switch (dev->mt76.region) {
2047233e39d1SEvelyn Tsai case NL80211_DFS_ETSI:
2048233e39d1SEvelyn Tsai region = 0;
2049233e39d1SEvelyn Tsai break;
2050233e39d1SEvelyn Tsai case NL80211_DFS_JP:
2051233e39d1SEvelyn Tsai region = 2;
2052233e39d1SEvelyn Tsai break;
2053233e39d1SEvelyn Tsai case NL80211_DFS_FCC:
2054233e39d1SEvelyn Tsai default:
2055233e39d1SEvelyn Tsai region = 1;
2056233e39d1SEvelyn Tsai break;
2057233e39d1SEvelyn Tsai }
2058e57b7901SRyder Lee
205997cef84dSLorenzo Bianconi err = mt76_connac_mcu_rdd_cmd(&dev->mt76, RDD_START, chain,
2060233e39d1SEvelyn Tsai MT_RX_SEL0, region);
2061e57b7901SRyder Lee if (err < 0)
2062e57b7901SRyder Lee return err;
2063e57b7901SRyder Lee
20647a12e06dSShayne Chen if (is_mt7915(&dev->mt76)) {
20657a12e06dSShayne Chen err = mt76_connac_mcu_rdd_cmd(&dev->mt76, RDD_SET_WF_ANT, chain,
20667a12e06dSShayne Chen 0, dev->dbdc_support ? 2 : 0);
20677a12e06dSShayne Chen if (err < 0)
20687a12e06dSShayne Chen return err;
20697a12e06dSShayne Chen }
20707a12e06dSShayne Chen
207197cef84dSLorenzo Bianconi return mt76_connac_mcu_rdd_cmd(&dev->mt76, RDD_DET_MODE, chain,
207297cef84dSLorenzo Bianconi MT_RX_SEL0, 1);
2073e57b7901SRyder Lee }
2074e57b7901SRyder Lee
mt7915_dfs_start_radar_detector(struct mt7915_phy * phy)2075e57b7901SRyder Lee static int mt7915_dfs_start_radar_detector(struct mt7915_phy *phy)
2076e57b7901SRyder Lee {
2077e57b7901SRyder Lee struct cfg80211_chan_def *chandef = &phy->mt76->chandef;
2078e57b7901SRyder Lee struct mt7915_dev *dev = phy->dev;
2079e57b7901SRyder Lee int err;
2080e57b7901SRyder Lee
2081e57b7901SRyder Lee /* start CAC */
20823eb50cc9SRyder Lee err = mt76_connac_mcu_rdd_cmd(&dev->mt76, RDD_CAC_START,
20833eb50cc9SRyder Lee phy->mt76->band_idx, MT_RX_SEL0, 0);
2084e57b7901SRyder Lee if (err < 0)
2085e57b7901SRyder Lee return err;
2086e57b7901SRyder Lee
20873eb50cc9SRyder Lee err = mt7915_dfs_start_rdd(dev, phy->mt76->band_idx);
2088e57b7901SRyder Lee if (err < 0)
2089e57b7901SRyder Lee return err;
2090e57b7901SRyder Lee
20913eb50cc9SRyder Lee phy->rdd_state |= BIT(phy->mt76->band_idx);
2092006b9d4aSBo Jiao
2093006b9d4aSBo Jiao if (!is_mt7915(&dev->mt76))
2094006b9d4aSBo Jiao return 0;
2095e57b7901SRyder Lee
2096e57b7901SRyder Lee if (chandef->width == NL80211_CHAN_WIDTH_160 ||
2097e57b7901SRyder Lee chandef->width == NL80211_CHAN_WIDTH_80P80) {
2098e57b7901SRyder Lee err = mt7915_dfs_start_rdd(dev, 1);
2099e57b7901SRyder Lee if (err < 0)
2100e57b7901SRyder Lee return err;
2101e57b7901SRyder Lee
2102e57b7901SRyder Lee phy->rdd_state |= BIT(1);
2103e57b7901SRyder Lee }
2104e57b7901SRyder Lee
2105e57b7901SRyder Lee return 0;
2106e57b7901SRyder Lee }
2107e57b7901SRyder Lee
2108e57b7901SRyder Lee static int
mt7915_dfs_init_radar_specs(struct mt7915_phy * phy)2109e57b7901SRyder Lee mt7915_dfs_init_radar_specs(struct mt7915_phy *phy)
2110e57b7901SRyder Lee {
2111e57b7901SRyder Lee const struct mt7915_dfs_radar_spec *radar_specs;
2112e57b7901SRyder Lee struct mt7915_dev *dev = phy->dev;
2113e57b7901SRyder Lee int err, i;
2114e57b7901SRyder Lee
2115e57b7901SRyder Lee switch (dev->mt76.region) {
2116e57b7901SRyder Lee case NL80211_DFS_FCC:
2117e57b7901SRyder Lee radar_specs = &fcc_radar_specs;
2118e57b7901SRyder Lee err = mt7915_mcu_set_fcc5_lpn(dev, 8);
2119e57b7901SRyder Lee if (err < 0)
2120e57b7901SRyder Lee return err;
2121e57b7901SRyder Lee break;
2122e57b7901SRyder Lee case NL80211_DFS_ETSI:
2123e57b7901SRyder Lee radar_specs = &etsi_radar_specs;
2124e57b7901SRyder Lee break;
2125e57b7901SRyder Lee case NL80211_DFS_JP:
2126e57b7901SRyder Lee radar_specs = &jp_radar_specs;
2127e57b7901SRyder Lee break;
2128e57b7901SRyder Lee default:
2129e57b7901SRyder Lee return -EINVAL;
2130e57b7901SRyder Lee }
2131e57b7901SRyder Lee
2132e57b7901SRyder Lee for (i = 0; i < ARRAY_SIZE(radar_specs->radar_pattern); i++) {
2133e57b7901SRyder Lee err = mt7915_mcu_set_radar_th(dev, i,
2134e57b7901SRyder Lee &radar_specs->radar_pattern[i]);
2135e57b7901SRyder Lee if (err < 0)
2136e57b7901SRyder Lee return err;
2137e57b7901SRyder Lee }
2138e57b7901SRyder Lee
2139e57b7901SRyder Lee return mt7915_mcu_set_pulse_th(dev, &radar_specs->pulse_th);
2140e57b7901SRyder Lee }
2141e57b7901SRyder Lee
mt7915_dfs_init_radar_detector(struct mt7915_phy * phy)2142e57b7901SRyder Lee int mt7915_dfs_init_radar_detector(struct mt7915_phy *phy)
2143e57b7901SRyder Lee {
2144e57b7901SRyder Lee struct mt7915_dev *dev = phy->dev;
21453f306448SFelix Fietkau enum mt76_dfs_state dfs_state, prev_state;
2146e57b7901SRyder Lee int err;
2147e57b7901SRyder Lee
21483f306448SFelix Fietkau prev_state = phy->mt76->dfs_state;
21493f306448SFelix Fietkau dfs_state = mt76_phy_dfs_state(phy->mt76);
21503f306448SFelix Fietkau
21513f306448SFelix Fietkau if (prev_state == dfs_state)
21523f306448SFelix Fietkau return 0;
21533f306448SFelix Fietkau
21543f306448SFelix Fietkau if (prev_state == MT_DFS_STATE_UNKNOWN)
21553f306448SFelix Fietkau mt7915_dfs_stop_radar_detector(phy);
21563f306448SFelix Fietkau
21573f306448SFelix Fietkau if (dfs_state == MT_DFS_STATE_DISABLED)
2158e57b7901SRyder Lee goto stop;
2159e57b7901SRyder Lee
21603f306448SFelix Fietkau if (prev_state <= MT_DFS_STATE_DISABLED) {
2161e57b7901SRyder Lee err = mt7915_dfs_init_radar_specs(phy);
21623f306448SFelix Fietkau if (err < 0)
21633f306448SFelix Fietkau return err;
21643f306448SFelix Fietkau
21653f306448SFelix Fietkau err = mt7915_dfs_start_radar_detector(phy);
21663f306448SFelix Fietkau if (err < 0)
21673f306448SFelix Fietkau return err;
21683f306448SFelix Fietkau
21693f306448SFelix Fietkau phy->mt76->dfs_state = MT_DFS_STATE_CAC;
2170e57b7901SRyder Lee }
2171e57b7901SRyder Lee
21723f306448SFelix Fietkau if (dfs_state == MT_DFS_STATE_CAC)
21733f306448SFelix Fietkau return 0;
2174e57b7901SRyder Lee
21753f306448SFelix Fietkau err = mt76_connac_mcu_rdd_cmd(&dev->mt76, RDD_CAC_END,
21763eb50cc9SRyder Lee phy->mt76->band_idx, MT_RX_SEL0, 0);
21773f306448SFelix Fietkau if (err < 0) {
21783f306448SFelix Fietkau phy->mt76->dfs_state = MT_DFS_STATE_UNKNOWN;
21793f306448SFelix Fietkau return err;
2180e57b7901SRyder Lee }
2181e57b7901SRyder Lee
21823f306448SFelix Fietkau phy->mt76->dfs_state = MT_DFS_STATE_ACTIVE;
21833f306448SFelix Fietkau return 0;
21843f306448SFelix Fietkau
2185e57b7901SRyder Lee stop:
2186006b9d4aSBo Jiao err = mt76_connac_mcu_rdd_cmd(&dev->mt76, RDD_NORMAL_START,
21873eb50cc9SRyder Lee phy->mt76->band_idx, MT_RX_SEL0, 0);
2188e57b7901SRyder Lee if (err < 0)
2189e57b7901SRyder Lee return err;
2190e57b7901SRyder Lee
21917a12e06dSShayne Chen if (is_mt7915(&dev->mt76)) {
21927a12e06dSShayne Chen err = mt76_connac_mcu_rdd_cmd(&dev->mt76, RDD_SET_WF_ANT,
21933eb50cc9SRyder Lee phy->mt76->band_idx, 0,
21947a12e06dSShayne Chen dev->dbdc_support ? 2 : 0);
21957a12e06dSShayne Chen if (err < 0)
21967a12e06dSShayne Chen return err;
21977a12e06dSShayne Chen }
21987a12e06dSShayne Chen
2199e57b7901SRyder Lee mt7915_dfs_stop_radar_detector(phy);
22003f306448SFelix Fietkau phy->mt76->dfs_state = MT_DFS_STATE_DISABLED;
22013f306448SFelix Fietkau
2202e57b7901SRyder Lee return 0;
2203e57b7901SRyder Lee }
22043782b69dSLorenzo Bianconi
22053782b69dSLorenzo Bianconi static int
mt7915_mac_twt_duration_align(int duration)22063782b69dSLorenzo Bianconi mt7915_mac_twt_duration_align(int duration)
22073782b69dSLorenzo Bianconi {
22083782b69dSLorenzo Bianconi return duration << 8;
22093782b69dSLorenzo Bianconi }
22103782b69dSLorenzo Bianconi
22113782b69dSLorenzo Bianconi static u64
mt7915_mac_twt_sched_list_add(struct mt7915_dev * dev,struct mt7915_twt_flow * flow)22123782b69dSLorenzo Bianconi mt7915_mac_twt_sched_list_add(struct mt7915_dev *dev,
22133782b69dSLorenzo Bianconi struct mt7915_twt_flow *flow)
22143782b69dSLorenzo Bianconi {
22153782b69dSLorenzo Bianconi struct mt7915_twt_flow *iter, *iter_next;
22163782b69dSLorenzo Bianconi u32 duration = flow->duration << 8;
22173782b69dSLorenzo Bianconi u64 start_tsf;
22183782b69dSLorenzo Bianconi
22193782b69dSLorenzo Bianconi iter = list_first_entry_or_null(&dev->twt_list,
22203782b69dSLorenzo Bianconi struct mt7915_twt_flow, list);
22213782b69dSLorenzo Bianconi if (!iter || !iter->sched || iter->start_tsf > duration) {
22223782b69dSLorenzo Bianconi /* add flow as first entry in the list */
22233782b69dSLorenzo Bianconi list_add(&flow->list, &dev->twt_list);
22243782b69dSLorenzo Bianconi return 0;
22253782b69dSLorenzo Bianconi }
22263782b69dSLorenzo Bianconi
22273782b69dSLorenzo Bianconi list_for_each_entry_safe(iter, iter_next, &dev->twt_list, list) {
22283782b69dSLorenzo Bianconi start_tsf = iter->start_tsf +
22293782b69dSLorenzo Bianconi mt7915_mac_twt_duration_align(iter->duration);
22303782b69dSLorenzo Bianconi if (list_is_last(&iter->list, &dev->twt_list))
22313782b69dSLorenzo Bianconi break;
22323782b69dSLorenzo Bianconi
22333782b69dSLorenzo Bianconi if (!iter_next->sched ||
22343782b69dSLorenzo Bianconi iter_next->start_tsf > start_tsf + duration) {
22353782b69dSLorenzo Bianconi list_add(&flow->list, &iter->list);
22363782b69dSLorenzo Bianconi goto out;
22373782b69dSLorenzo Bianconi }
22383782b69dSLorenzo Bianconi }
22393782b69dSLorenzo Bianconi
22403782b69dSLorenzo Bianconi /* add flow as last entry in the list */
22413782b69dSLorenzo Bianconi list_add_tail(&flow->list, &dev->twt_list);
22423782b69dSLorenzo Bianconi out:
22433782b69dSLorenzo Bianconi return start_tsf;
22443782b69dSLorenzo Bianconi }
22453782b69dSLorenzo Bianconi
mt7915_mac_check_twt_req(struct ieee80211_twt_setup * twt)22463782b69dSLorenzo Bianconi static int mt7915_mac_check_twt_req(struct ieee80211_twt_setup *twt)
22473782b69dSLorenzo Bianconi {
22483782b69dSLorenzo Bianconi struct ieee80211_twt_params *twt_agrt;
22493782b69dSLorenzo Bianconi u64 interval, duration;
22503782b69dSLorenzo Bianconi u16 mantissa;
22513782b69dSLorenzo Bianconi u8 exp;
22523782b69dSLorenzo Bianconi
22533782b69dSLorenzo Bianconi /* only individual agreement supported */
22543782b69dSLorenzo Bianconi if (twt->control & IEEE80211_TWT_CONTROL_NEG_TYPE_BROADCAST)
22553782b69dSLorenzo Bianconi return -EOPNOTSUPP;
22563782b69dSLorenzo Bianconi
22573782b69dSLorenzo Bianconi /* only 256us unit supported */
22583782b69dSLorenzo Bianconi if (twt->control & IEEE80211_TWT_CONTROL_WAKE_DUR_UNIT)
22593782b69dSLorenzo Bianconi return -EOPNOTSUPP;
22603782b69dSLorenzo Bianconi
22613782b69dSLorenzo Bianconi twt_agrt = (struct ieee80211_twt_params *)twt->params;
22623782b69dSLorenzo Bianconi
22633782b69dSLorenzo Bianconi /* explicit agreement not supported */
22643782b69dSLorenzo Bianconi if (!(twt_agrt->req_type & cpu_to_le16(IEEE80211_TWT_REQTYPE_IMPLICIT)))
22653782b69dSLorenzo Bianconi return -EOPNOTSUPP;
22663782b69dSLorenzo Bianconi
22673782b69dSLorenzo Bianconi exp = FIELD_GET(IEEE80211_TWT_REQTYPE_WAKE_INT_EXP,
22683782b69dSLorenzo Bianconi le16_to_cpu(twt_agrt->req_type));
22693782b69dSLorenzo Bianconi mantissa = le16_to_cpu(twt_agrt->mantissa);
22703782b69dSLorenzo Bianconi duration = twt_agrt->min_twt_dur << 8;
22713782b69dSLorenzo Bianconi
22723782b69dSLorenzo Bianconi interval = (u64)mantissa << exp;
22733782b69dSLorenzo Bianconi if (interval < duration)
22743782b69dSLorenzo Bianconi return -EOPNOTSUPP;
22753782b69dSLorenzo Bianconi
22763782b69dSLorenzo Bianconi return 0;
22773782b69dSLorenzo Bianconi }
22783782b69dSLorenzo Bianconi
2279c088eb38SPeter Chiu static bool
mt7915_mac_twt_param_equal(struct mt7915_sta * msta,struct ieee80211_twt_params * twt_agrt)2280c088eb38SPeter Chiu mt7915_mac_twt_param_equal(struct mt7915_sta *msta,
2281c088eb38SPeter Chiu struct ieee80211_twt_params *twt_agrt)
2282c088eb38SPeter Chiu {
2283c088eb38SPeter Chiu u16 type = le16_to_cpu(twt_agrt->req_type);
2284c088eb38SPeter Chiu u8 exp;
2285c088eb38SPeter Chiu int i;
2286c088eb38SPeter Chiu
2287c088eb38SPeter Chiu exp = FIELD_GET(IEEE80211_TWT_REQTYPE_WAKE_INT_EXP, type);
2288c088eb38SPeter Chiu for (i = 0; i < MT7915_MAX_STA_TWT_AGRT; i++) {
2289c088eb38SPeter Chiu struct mt7915_twt_flow *f;
2290c088eb38SPeter Chiu
2291c088eb38SPeter Chiu if (!(msta->twt.flowid_mask & BIT(i)))
2292c088eb38SPeter Chiu continue;
2293c088eb38SPeter Chiu
2294c088eb38SPeter Chiu f = &msta->twt.flow[i];
2295c088eb38SPeter Chiu if (f->duration == twt_agrt->min_twt_dur &&
2296c088eb38SPeter Chiu f->mantissa == twt_agrt->mantissa &&
2297c088eb38SPeter Chiu f->exp == exp &&
2298c088eb38SPeter Chiu f->protection == !!(type & IEEE80211_TWT_REQTYPE_PROTECTION) &&
2299c088eb38SPeter Chiu f->flowtype == !!(type & IEEE80211_TWT_REQTYPE_FLOWTYPE) &&
2300c088eb38SPeter Chiu f->trigger == !!(type & IEEE80211_TWT_REQTYPE_TRIGGER))
2301c088eb38SPeter Chiu return true;
2302c088eb38SPeter Chiu }
2303c088eb38SPeter Chiu
2304c088eb38SPeter Chiu return false;
2305c088eb38SPeter Chiu }
2306c088eb38SPeter Chiu
mt7915_mac_add_twt_setup(struct ieee80211_hw * hw,struct ieee80211_sta * sta,struct ieee80211_twt_setup * twt)23073782b69dSLorenzo Bianconi void mt7915_mac_add_twt_setup(struct ieee80211_hw *hw,
23083782b69dSLorenzo Bianconi struct ieee80211_sta *sta,
23093782b69dSLorenzo Bianconi struct ieee80211_twt_setup *twt)
23103782b69dSLorenzo Bianconi {
23113782b69dSLorenzo Bianconi enum ieee80211_twt_setup_cmd setup_cmd = TWT_SETUP_CMD_REJECT;
23123782b69dSLorenzo Bianconi struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
23133782b69dSLorenzo Bianconi struct ieee80211_twt_params *twt_agrt = (void *)twt->params;
23143782b69dSLorenzo Bianconi u16 req_type = le16_to_cpu(twt_agrt->req_type);
23153782b69dSLorenzo Bianconi enum ieee80211_twt_setup_cmd sta_setup_cmd;
23163782b69dSLorenzo Bianconi struct mt7915_dev *dev = mt7915_hw_dev(hw);
23173782b69dSLorenzo Bianconi struct mt7915_twt_flow *flow;
23183782b69dSLorenzo Bianconi int flowid, table_id;
23193782b69dSLorenzo Bianconi u8 exp;
23203782b69dSLorenzo Bianconi
23213782b69dSLorenzo Bianconi if (mt7915_mac_check_twt_req(twt))
23223782b69dSLorenzo Bianconi goto out;
23233782b69dSLorenzo Bianconi
23243782b69dSLorenzo Bianconi mutex_lock(&dev->mt76.mutex);
23253782b69dSLorenzo Bianconi
23263782b69dSLorenzo Bianconi if (dev->twt.n_agrt == MT7915_MAX_TWT_AGRT)
23273782b69dSLorenzo Bianconi goto unlock;
23283782b69dSLorenzo Bianconi
23293782b69dSLorenzo Bianconi if (hweight8(msta->twt.flowid_mask) == ARRAY_SIZE(msta->twt.flow))
23303782b69dSLorenzo Bianconi goto unlock;
23313782b69dSLorenzo Bianconi
23324ebcff04SPeter Chiu if (twt_agrt->min_twt_dur < MT7915_MIN_TWT_DUR) {
23334ebcff04SPeter Chiu setup_cmd = TWT_SETUP_CMD_DICTATE;
23344ebcff04SPeter Chiu twt_agrt->min_twt_dur = MT7915_MIN_TWT_DUR;
23354ebcff04SPeter Chiu goto unlock;
23364ebcff04SPeter Chiu }
23374ebcff04SPeter Chiu
23383782b69dSLorenzo Bianconi flowid = ffs(~msta->twt.flowid_mask) - 1;
23393d9aa543SLorenzo Bianconi twt_agrt->req_type &= ~cpu_to_le16(IEEE80211_TWT_REQTYPE_FLOWID);
23403d9aa543SLorenzo Bianconi twt_agrt->req_type |= le16_encode_bits(flowid,
23413782b69dSLorenzo Bianconi IEEE80211_TWT_REQTYPE_FLOWID);
23423782b69dSLorenzo Bianconi
23433782b69dSLorenzo Bianconi table_id = ffs(~dev->twt.table_mask) - 1;
23443782b69dSLorenzo Bianconi exp = FIELD_GET(IEEE80211_TWT_REQTYPE_WAKE_INT_EXP, req_type);
23453782b69dSLorenzo Bianconi sta_setup_cmd = FIELD_GET(IEEE80211_TWT_REQTYPE_SETUP_CMD, req_type);
23463782b69dSLorenzo Bianconi
2347c088eb38SPeter Chiu if (mt7915_mac_twt_param_equal(msta, twt_agrt))
2348c088eb38SPeter Chiu goto unlock;
2349c088eb38SPeter Chiu
23503782b69dSLorenzo Bianconi flow = &msta->twt.flow[flowid];
23513782b69dSLorenzo Bianconi memset(flow, 0, sizeof(*flow));
23523782b69dSLorenzo Bianconi INIT_LIST_HEAD(&flow->list);
23533782b69dSLorenzo Bianconi flow->wcid = msta->wcid.idx;
23543782b69dSLorenzo Bianconi flow->table_id = table_id;
23553782b69dSLorenzo Bianconi flow->id = flowid;
23563782b69dSLorenzo Bianconi flow->duration = twt_agrt->min_twt_dur;
23573782b69dSLorenzo Bianconi flow->mantissa = twt_agrt->mantissa;
23583782b69dSLorenzo Bianconi flow->exp = exp;
23593782b69dSLorenzo Bianconi flow->protection = !!(req_type & IEEE80211_TWT_REQTYPE_PROTECTION);
23603782b69dSLorenzo Bianconi flow->flowtype = !!(req_type & IEEE80211_TWT_REQTYPE_FLOWTYPE);
23613782b69dSLorenzo Bianconi flow->trigger = !!(req_type & IEEE80211_TWT_REQTYPE_TRIGGER);
23623782b69dSLorenzo Bianconi
23633782b69dSLorenzo Bianconi if (sta_setup_cmd == TWT_SETUP_CMD_REQUEST ||
23643782b69dSLorenzo Bianconi sta_setup_cmd == TWT_SETUP_CMD_SUGGEST) {
23653782b69dSLorenzo Bianconi u64 interval = (u64)le16_to_cpu(twt_agrt->mantissa) << exp;
23663782b69dSLorenzo Bianconi u64 flow_tsf, curr_tsf;
23673782b69dSLorenzo Bianconi u32 rem;
23683782b69dSLorenzo Bianconi
23693782b69dSLorenzo Bianconi flow->sched = true;
23703782b69dSLorenzo Bianconi flow->start_tsf = mt7915_mac_twt_sched_list_add(dev, flow);
23713782b69dSLorenzo Bianconi curr_tsf = __mt7915_get_tsf(hw, msta->vif);
23723782b69dSLorenzo Bianconi div_u64_rem(curr_tsf - flow->start_tsf, interval, &rem);
23733782b69dSLorenzo Bianconi flow_tsf = curr_tsf + interval - rem;
23743782b69dSLorenzo Bianconi twt_agrt->twt = cpu_to_le64(flow_tsf);
23753782b69dSLorenzo Bianconi } else {
23763782b69dSLorenzo Bianconi list_add_tail(&flow->list, &dev->twt_list);
23773782b69dSLorenzo Bianconi }
23783782b69dSLorenzo Bianconi flow->tsf = le64_to_cpu(twt_agrt->twt);
23793782b69dSLorenzo Bianconi
23803782b69dSLorenzo Bianconi if (mt7915_mcu_twt_agrt_update(dev, msta->vif, flow, MCU_TWT_AGRT_ADD))
23813782b69dSLorenzo Bianconi goto unlock;
23823782b69dSLorenzo Bianconi
23833782b69dSLorenzo Bianconi setup_cmd = TWT_SETUP_CMD_ACCEPT;
23843782b69dSLorenzo Bianconi dev->twt.table_mask |= BIT(table_id);
23853782b69dSLorenzo Bianconi msta->twt.flowid_mask |= BIT(flowid);
23863782b69dSLorenzo Bianconi dev->twt.n_agrt++;
23873782b69dSLorenzo Bianconi
23883782b69dSLorenzo Bianconi unlock:
23893782b69dSLorenzo Bianconi mutex_unlock(&dev->mt76.mutex);
23903782b69dSLorenzo Bianconi out:
23913d9aa543SLorenzo Bianconi twt_agrt->req_type &= ~cpu_to_le16(IEEE80211_TWT_REQTYPE_SETUP_CMD);
23923d9aa543SLorenzo Bianconi twt_agrt->req_type |=
23933d9aa543SLorenzo Bianconi le16_encode_bits(setup_cmd, IEEE80211_TWT_REQTYPE_SETUP_CMD);
23943782b69dSLorenzo Bianconi twt->control = (twt->control & IEEE80211_TWT_CONTROL_WAKE_DUR_UNIT) |
23953782b69dSLorenzo Bianconi (twt->control & IEEE80211_TWT_CONTROL_RX_DISABLED);
23963782b69dSLorenzo Bianconi }
23973782b69dSLorenzo Bianconi
mt7915_mac_twt_teardown_flow(struct mt7915_dev * dev,struct mt7915_sta * msta,u8 flowid)23983782b69dSLorenzo Bianconi void mt7915_mac_twt_teardown_flow(struct mt7915_dev *dev,
23993782b69dSLorenzo Bianconi struct mt7915_sta *msta,
24003782b69dSLorenzo Bianconi u8 flowid)
24013782b69dSLorenzo Bianconi {
24023782b69dSLorenzo Bianconi struct mt7915_twt_flow *flow;
24033782b69dSLorenzo Bianconi
24043782b69dSLorenzo Bianconi lockdep_assert_held(&dev->mt76.mutex);
24053782b69dSLorenzo Bianconi
24063782b69dSLorenzo Bianconi if (flowid >= ARRAY_SIZE(msta->twt.flow))
24073782b69dSLorenzo Bianconi return;
24083782b69dSLorenzo Bianconi
24093782b69dSLorenzo Bianconi if (!(msta->twt.flowid_mask & BIT(flowid)))
24103782b69dSLorenzo Bianconi return;
24113782b69dSLorenzo Bianconi
24123782b69dSLorenzo Bianconi flow = &msta->twt.flow[flowid];
24133782b69dSLorenzo Bianconi if (mt7915_mcu_twt_agrt_update(dev, msta->vif, flow,
24143782b69dSLorenzo Bianconi MCU_TWT_AGRT_DELETE))
24153782b69dSLorenzo Bianconi return;
24163782b69dSLorenzo Bianconi
24173782b69dSLorenzo Bianconi list_del_init(&flow->list);
24183782b69dSLorenzo Bianconi msta->twt.flowid_mask &= ~BIT(flowid);
24193782b69dSLorenzo Bianconi dev->twt.table_mask &= ~BIT(flow->table_id);
24203782b69dSLorenzo Bianconi dev->twt.n_agrt--;
24213782b69dSLorenzo Bianconi }
2422