xref: /linux/drivers/net/wireless/mediatek/mt76/mt76x02_mac.c (revision 5567b37359919d2f678de04e9d6e83571b3140ff)
1c378f247SStanislaw Gruszka /*
2c378f247SStanislaw Gruszka  * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
3c378f247SStanislaw Gruszka  * Copyright (C) 2018 Stanislaw Gruszka <stf_xl@wp.pl>
4c378f247SStanislaw Gruszka  *
5c378f247SStanislaw Gruszka  * Permission to use, copy, modify, and/or distribute this software for any
6c378f247SStanislaw Gruszka  * purpose with or without fee is hereby granted, provided that the above
7c378f247SStanislaw Gruszka  * copyright notice and this permission notice appear in all copies.
8c378f247SStanislaw Gruszka  *
9c378f247SStanislaw Gruszka  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10c378f247SStanislaw Gruszka  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11c378f247SStanislaw Gruszka  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12c378f247SStanislaw Gruszka  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13c378f247SStanislaw Gruszka  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14c378f247SStanislaw Gruszka  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15c378f247SStanislaw Gruszka  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16c378f247SStanislaw Gruszka  */
17c378f247SStanislaw Gruszka 
187a07adcdSLorenzo Bianconi #include "mt76x02.h"
193e2342edSLorenzo Bianconi #include "mt76x02_trace.h"
20c378f247SStanislaw Gruszka 
21*5567b373SFelix Fietkau static enum mt76x02_cipher_type
22c378f247SStanislaw Gruszka mt76x02_mac_get_key_info(struct ieee80211_key_conf *key, u8 *key_data)
23c378f247SStanislaw Gruszka {
24c378f247SStanislaw Gruszka 	memset(key_data, 0, 32);
25c378f247SStanislaw Gruszka 	if (!key)
26c378f247SStanislaw Gruszka 		return MT_CIPHER_NONE;
27c378f247SStanislaw Gruszka 
28c378f247SStanislaw Gruszka 	if (key->keylen > 32)
29c378f247SStanislaw Gruszka 		return MT_CIPHER_NONE;
30c378f247SStanislaw Gruszka 
31c378f247SStanislaw Gruszka 	memcpy(key_data, key->key, key->keylen);
32c378f247SStanislaw Gruszka 
33c378f247SStanislaw Gruszka 	switch (key->cipher) {
34c378f247SStanislaw Gruszka 	case WLAN_CIPHER_SUITE_WEP40:
35c378f247SStanislaw Gruszka 		return MT_CIPHER_WEP40;
36c378f247SStanislaw Gruszka 	case WLAN_CIPHER_SUITE_WEP104:
37c378f247SStanislaw Gruszka 		return MT_CIPHER_WEP104;
38c378f247SStanislaw Gruszka 	case WLAN_CIPHER_SUITE_TKIP:
39c378f247SStanislaw Gruszka 		return MT_CIPHER_TKIP;
40c378f247SStanislaw Gruszka 	case WLAN_CIPHER_SUITE_CCMP:
41c378f247SStanislaw Gruszka 		return MT_CIPHER_AES_CCMP;
42c378f247SStanislaw Gruszka 	default:
43c378f247SStanislaw Gruszka 		return MT_CIPHER_NONE;
44c378f247SStanislaw Gruszka 	}
45c378f247SStanislaw Gruszka }
46047aed1cSStanislaw Gruszka 
478d66af49SLorenzo Bianconi int mt76x02_mac_shared_key_setup(struct mt76x02_dev *dev, u8 vif_idx,
488d66af49SLorenzo Bianconi 				 u8 key_idx, struct ieee80211_key_conf *key)
49047aed1cSStanislaw Gruszka {
50047aed1cSStanislaw Gruszka 	enum mt76x02_cipher_type cipher;
51047aed1cSStanislaw Gruszka 	u8 key_data[32];
52047aed1cSStanislaw Gruszka 	u32 val;
53047aed1cSStanislaw Gruszka 
54047aed1cSStanislaw Gruszka 	cipher = mt76x02_mac_get_key_info(key, key_data);
55047aed1cSStanislaw Gruszka 	if (cipher == MT_CIPHER_NONE && key)
56047aed1cSStanislaw Gruszka 		return -EOPNOTSUPP;
57047aed1cSStanislaw Gruszka 
588d66af49SLorenzo Bianconi 	val = mt76_rr(dev, MT_SKEY_MODE(vif_idx));
59047aed1cSStanislaw Gruszka 	val &= ~(MT_SKEY_MODE_MASK << MT_SKEY_MODE_SHIFT(vif_idx, key_idx));
60047aed1cSStanislaw Gruszka 	val |= cipher << MT_SKEY_MODE_SHIFT(vif_idx, key_idx);
618d66af49SLorenzo Bianconi 	mt76_wr(dev, MT_SKEY_MODE(vif_idx), val);
62047aed1cSStanislaw Gruszka 
638d66af49SLorenzo Bianconi 	mt76_wr_copy(dev, MT_SKEY(vif_idx, key_idx), key_data,
64047aed1cSStanislaw Gruszka 		     sizeof(key_data));
65047aed1cSStanislaw Gruszka 
66047aed1cSStanislaw Gruszka 	return 0;
67047aed1cSStanislaw Gruszka }
68047aed1cSStanislaw Gruszka EXPORT_SYMBOL_GPL(mt76x02_mac_shared_key_setup);
6946436b5eSStanislaw Gruszka 
708d66af49SLorenzo Bianconi int mt76x02_mac_wcid_set_key(struct mt76x02_dev *dev, u8 idx,
7146436b5eSStanislaw Gruszka 			     struct ieee80211_key_conf *key)
7246436b5eSStanislaw Gruszka {
7346436b5eSStanislaw Gruszka 	enum mt76x02_cipher_type cipher;
7446436b5eSStanislaw Gruszka 	u8 key_data[32];
7546436b5eSStanislaw Gruszka 	u8 iv_data[8];
7646436b5eSStanislaw Gruszka 
7746436b5eSStanislaw Gruszka 	cipher = mt76x02_mac_get_key_info(key, key_data);
7846436b5eSStanislaw Gruszka 	if (cipher == MT_CIPHER_NONE && key)
7946436b5eSStanislaw Gruszka 		return -EOPNOTSUPP;
8046436b5eSStanislaw Gruszka 
818d66af49SLorenzo Bianconi 	mt76_wr_copy(dev, MT_WCID_KEY(idx), key_data, sizeof(key_data));
828d66af49SLorenzo Bianconi 	mt76_rmw_field(dev, MT_WCID_ATTR(idx), MT_WCID_ATTR_PKEY_MODE, cipher);
8346436b5eSStanislaw Gruszka 
8446436b5eSStanislaw Gruszka 	memset(iv_data, 0, sizeof(iv_data));
8546436b5eSStanislaw Gruszka 	if (key) {
868d66af49SLorenzo Bianconi 		mt76_rmw_field(dev, MT_WCID_ATTR(idx), MT_WCID_ATTR_PAIRWISE,
8746436b5eSStanislaw Gruszka 			       !!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE));
8846436b5eSStanislaw Gruszka 		iv_data[3] = key->keyidx << 6;
8946436b5eSStanislaw Gruszka 		if (cipher >= MT_CIPHER_TKIP)
9046436b5eSStanislaw Gruszka 			iv_data[3] |= 0x20;
9146436b5eSStanislaw Gruszka 	}
9246436b5eSStanislaw Gruszka 
938d66af49SLorenzo Bianconi 	mt76_wr_copy(dev, MT_WCID_IV(idx), iv_data, sizeof(iv_data));
9446436b5eSStanislaw Gruszka 
9546436b5eSStanislaw Gruszka 	return 0;
9646436b5eSStanislaw Gruszka }
9732bb405fSStanislaw Gruszka 
988d66af49SLorenzo Bianconi void mt76x02_mac_wcid_setup(struct mt76x02_dev *dev, u8 idx,
998d66af49SLorenzo Bianconi 			    u8 vif_idx, u8 *mac)
10032bb405fSStanislaw Gruszka {
10132bb405fSStanislaw Gruszka 	struct mt76_wcid_addr addr = {};
10232bb405fSStanislaw Gruszka 	u32 attr;
10332bb405fSStanislaw Gruszka 
10432bb405fSStanislaw Gruszka 	attr = FIELD_PREP(MT_WCID_ATTR_BSS_IDX, vif_idx & 7) |
10532bb405fSStanislaw Gruszka 	       FIELD_PREP(MT_WCID_ATTR_BSS_IDX_EXT, !!(vif_idx & 8));
10632bb405fSStanislaw Gruszka 
1078d66af49SLorenzo Bianconi 	mt76_wr(dev, MT_WCID_ATTR(idx), attr);
10832bb405fSStanislaw Gruszka 
1098d66af49SLorenzo Bianconi 	mt76_wr(dev, MT_WCID_TX_RATE(idx), 0);
1108d66af49SLorenzo Bianconi 	mt76_wr(dev, MT_WCID_TX_RATE(idx) + 4, 0);
11132bb405fSStanislaw Gruszka 
11232bb405fSStanislaw Gruszka 	if (idx >= 128)
11332bb405fSStanislaw Gruszka 		return;
11432bb405fSStanislaw Gruszka 
11532bb405fSStanislaw Gruszka 	if (mac)
11632bb405fSStanislaw Gruszka 		memcpy(addr.macaddr, mac, ETH_ALEN);
11732bb405fSStanislaw Gruszka 
1188d66af49SLorenzo Bianconi 	mt76_wr_copy(dev, MT_WCID_ADDR(idx), &addr, sizeof(addr));
11932bb405fSStanislaw Gruszka }
12032bb405fSStanislaw Gruszka EXPORT_SYMBOL_GPL(mt76x02_mac_wcid_setup);
121516ea2a2SStanislaw Gruszka 
1228d66af49SLorenzo Bianconi void mt76x02_mac_wcid_set_drop(struct mt76x02_dev *dev, u8 idx, bool drop)
123516ea2a2SStanislaw Gruszka {
1248d66af49SLorenzo Bianconi 	u32 val = mt76_rr(dev, MT_WCID_DROP(idx));
125516ea2a2SStanislaw Gruszka 	u32 bit = MT_WCID_DROP_MASK(idx);
126516ea2a2SStanislaw Gruszka 
127516ea2a2SStanislaw Gruszka 	/* prevent unnecessary writes */
128516ea2a2SStanislaw Gruszka 	if ((val & bit) != (bit * drop))
1298d66af49SLorenzo Bianconi 		mt76_wr(dev, MT_WCID_DROP(idx), (val & ~bit) | (bit * drop));
130516ea2a2SStanislaw Gruszka }
131516ea2a2SStanislaw Gruszka EXPORT_SYMBOL_GPL(mt76x02_mac_wcid_set_drop);
132f5a7f126SStanislaw Gruszka 
1338d66af49SLorenzo Bianconi void mt76x02_txq_init(struct mt76x02_dev *dev, struct ieee80211_txq *txq)
134f5a7f126SStanislaw Gruszka {
135f5a7f126SStanislaw Gruszka 	struct mt76_txq *mtxq;
136f5a7f126SStanislaw Gruszka 
137f5a7f126SStanislaw Gruszka 	if (!txq)
138f5a7f126SStanislaw Gruszka 		return;
139f5a7f126SStanislaw Gruszka 
140f5a7f126SStanislaw Gruszka 	mtxq = (struct mt76_txq *) txq->drv_priv;
141f5a7f126SStanislaw Gruszka 	if (txq->sta) {
142f5a7f126SStanislaw Gruszka 		struct mt76x02_sta *sta;
143f5a7f126SStanislaw Gruszka 
144f5a7f126SStanislaw Gruszka 		sta = (struct mt76x02_sta *) txq->sta->drv_priv;
145f5a7f126SStanislaw Gruszka 		mtxq->wcid = &sta->wcid;
146f5a7f126SStanislaw Gruszka 	} else {
147f5a7f126SStanislaw Gruszka 		struct mt76x02_vif *mvif;
148f5a7f126SStanislaw Gruszka 
149f5a7f126SStanislaw Gruszka 		mvif = (struct mt76x02_vif *) txq->vif->drv_priv;
150f5a7f126SStanislaw Gruszka 		mtxq->wcid = &mvif->group_wcid;
151f5a7f126SStanislaw Gruszka 	}
152f5a7f126SStanislaw Gruszka 
1538d66af49SLorenzo Bianconi 	mt76_txq_init(&dev->mt76, txq);
154f5a7f126SStanislaw Gruszka }
1555327b5eaSStanislaw Gruszka 
156c4ed5088SLorenzo Bianconi static __le16
1578d66af49SLorenzo Bianconi mt76x02_mac_tx_rate_val(struct mt76x02_dev *dev,
1585327b5eaSStanislaw Gruszka 			const struct ieee80211_tx_rate *rate, u8 *nss_val)
1595327b5eaSStanislaw Gruszka {
1605327b5eaSStanislaw Gruszka 	u16 rateval;
1615327b5eaSStanislaw Gruszka 	u8 phy, rate_idx;
1625327b5eaSStanislaw Gruszka 	u8 nss = 1;
1635327b5eaSStanislaw Gruszka 	u8 bw = 0;
1645327b5eaSStanislaw Gruszka 
1655327b5eaSStanislaw Gruszka 	if (rate->flags & IEEE80211_TX_RC_VHT_MCS) {
1665327b5eaSStanislaw Gruszka 		rate_idx = rate->idx;
1675327b5eaSStanislaw Gruszka 		nss = 1 + (rate->idx >> 4);
1685327b5eaSStanislaw Gruszka 		phy = MT_PHY_TYPE_VHT;
1695327b5eaSStanislaw Gruszka 		if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1705327b5eaSStanislaw Gruszka 			bw = 2;
1715327b5eaSStanislaw Gruszka 		else if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1725327b5eaSStanislaw Gruszka 			bw = 1;
1735327b5eaSStanislaw Gruszka 	} else if (rate->flags & IEEE80211_TX_RC_MCS) {
1745327b5eaSStanislaw Gruszka 		rate_idx = rate->idx;
1755327b5eaSStanislaw Gruszka 		nss = 1 + (rate->idx >> 3);
1765327b5eaSStanislaw Gruszka 		phy = MT_PHY_TYPE_HT;
1775327b5eaSStanislaw Gruszka 		if (rate->flags & IEEE80211_TX_RC_GREEN_FIELD)
1785327b5eaSStanislaw Gruszka 			phy = MT_PHY_TYPE_HT_GF;
1795327b5eaSStanislaw Gruszka 		if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1805327b5eaSStanislaw Gruszka 			bw = 1;
1815327b5eaSStanislaw Gruszka 	} else {
1825327b5eaSStanislaw Gruszka 		const struct ieee80211_rate *r;
1838d66af49SLorenzo Bianconi 		int band = dev->mt76.chandef.chan->band;
1845327b5eaSStanislaw Gruszka 		u16 val;
1855327b5eaSStanislaw Gruszka 
1868d66af49SLorenzo Bianconi 		r = &dev->mt76.hw->wiphy->bands[band]->bitrates[rate->idx];
1875327b5eaSStanislaw Gruszka 		if (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
1885327b5eaSStanislaw Gruszka 			val = r->hw_value_short;
1895327b5eaSStanislaw Gruszka 		else
1905327b5eaSStanislaw Gruszka 			val = r->hw_value;
1915327b5eaSStanislaw Gruszka 
1925327b5eaSStanislaw Gruszka 		phy = val >> 8;
1935327b5eaSStanislaw Gruszka 		rate_idx = val & 0xff;
1945327b5eaSStanislaw Gruszka 		bw = 0;
1955327b5eaSStanislaw Gruszka 	}
1965327b5eaSStanislaw Gruszka 
1975327b5eaSStanislaw Gruszka 	rateval = FIELD_PREP(MT_RXWI_RATE_INDEX, rate_idx);
1985327b5eaSStanislaw Gruszka 	rateval |= FIELD_PREP(MT_RXWI_RATE_PHY, phy);
1995327b5eaSStanislaw Gruszka 	rateval |= FIELD_PREP(MT_RXWI_RATE_BW, bw);
2005327b5eaSStanislaw Gruszka 	if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
2015327b5eaSStanislaw Gruszka 		rateval |= MT_RXWI_RATE_SGI;
2025327b5eaSStanislaw Gruszka 
2035327b5eaSStanislaw Gruszka 	*nss_val = nss;
2045327b5eaSStanislaw Gruszka 	return cpu_to_le16(rateval);
2055327b5eaSStanislaw Gruszka }
2065327b5eaSStanislaw Gruszka 
2078d66af49SLorenzo Bianconi void mt76x02_mac_wcid_set_rate(struct mt76x02_dev *dev, struct mt76_wcid *wcid,
2085327b5eaSStanislaw Gruszka 			       const struct ieee80211_tx_rate *rate)
2095327b5eaSStanislaw Gruszka {
2108d66af49SLorenzo Bianconi 	spin_lock_bh(&dev->mt76.lock);
2115327b5eaSStanislaw Gruszka 	wcid->tx_rate = mt76x02_mac_tx_rate_val(dev, rate, &wcid->tx_rate_nss);
2125327b5eaSStanislaw Gruszka 	wcid->tx_rate_set = true;
2138d66af49SLorenzo Bianconi 	spin_unlock_bh(&dev->mt76.lock);
2145327b5eaSStanislaw Gruszka }
215b490b1dfSStanislaw Gruszka 
2168d66af49SLorenzo Bianconi bool mt76x02_mac_load_tx_status(struct mt76x02_dev *dev,
217b490b1dfSStanislaw Gruszka 				struct mt76x02_tx_status *stat)
218b490b1dfSStanislaw Gruszka {
219b490b1dfSStanislaw Gruszka 	u32 stat1, stat2;
220b490b1dfSStanislaw Gruszka 
2218d66af49SLorenzo Bianconi 	stat2 = mt76_rr(dev, MT_TX_STAT_FIFO_EXT);
2228d66af49SLorenzo Bianconi 	stat1 = mt76_rr(dev, MT_TX_STAT_FIFO);
223b490b1dfSStanislaw Gruszka 
224b490b1dfSStanislaw Gruszka 	stat->valid = !!(stat1 & MT_TX_STAT_FIFO_VALID);
225b490b1dfSStanislaw Gruszka 	if (!stat->valid)
226b490b1dfSStanislaw Gruszka 		return false;
227b490b1dfSStanislaw Gruszka 
228b490b1dfSStanislaw Gruszka 	stat->success = !!(stat1 & MT_TX_STAT_FIFO_SUCCESS);
229b490b1dfSStanislaw Gruszka 	stat->aggr = !!(stat1 & MT_TX_STAT_FIFO_AGGR);
230b490b1dfSStanislaw Gruszka 	stat->ack_req = !!(stat1 & MT_TX_STAT_FIFO_ACKREQ);
231b490b1dfSStanislaw Gruszka 	stat->wcid = FIELD_GET(MT_TX_STAT_FIFO_WCID, stat1);
232b490b1dfSStanislaw Gruszka 	stat->rate = FIELD_GET(MT_TX_STAT_FIFO_RATE, stat1);
233b490b1dfSStanislaw Gruszka 
234b490b1dfSStanislaw Gruszka 	stat->retry = FIELD_GET(MT_TX_STAT_FIFO_EXT_RETRY, stat2);
235b490b1dfSStanislaw Gruszka 	stat->pktid = FIELD_GET(MT_TX_STAT_FIFO_EXT_PKTID, stat2);
236b490b1dfSStanislaw Gruszka 
237b490b1dfSStanislaw Gruszka 	return true;
238b490b1dfSStanislaw Gruszka }
2397c1f8881SStanislaw Gruszka 
2407c1f8881SStanislaw Gruszka static int
2417c1f8881SStanislaw Gruszka mt76x02_mac_process_tx_rate(struct ieee80211_tx_rate *txrate, u16 rate,
2427c1f8881SStanislaw Gruszka 			   enum nl80211_band band)
2437c1f8881SStanislaw Gruszka {
2447c1f8881SStanislaw Gruszka 	u8 idx = FIELD_GET(MT_RXWI_RATE_INDEX, rate);
2457c1f8881SStanislaw Gruszka 
2467c1f8881SStanislaw Gruszka 	txrate->idx = 0;
2477c1f8881SStanislaw Gruszka 	txrate->flags = 0;
2487c1f8881SStanislaw Gruszka 	txrate->count = 1;
2497c1f8881SStanislaw Gruszka 
2507c1f8881SStanislaw Gruszka 	switch (FIELD_GET(MT_RXWI_RATE_PHY, rate)) {
2517c1f8881SStanislaw Gruszka 	case MT_PHY_TYPE_OFDM:
2527c1f8881SStanislaw Gruszka 		if (band == NL80211_BAND_2GHZ)
2537c1f8881SStanislaw Gruszka 			idx += 4;
2547c1f8881SStanislaw Gruszka 
2557c1f8881SStanislaw Gruszka 		txrate->idx = idx;
2567c1f8881SStanislaw Gruszka 		return 0;
2577c1f8881SStanislaw Gruszka 	case MT_PHY_TYPE_CCK:
2587c1f8881SStanislaw Gruszka 		if (idx >= 8)
2597c1f8881SStanislaw Gruszka 			idx -= 8;
2607c1f8881SStanislaw Gruszka 
2617c1f8881SStanislaw Gruszka 		txrate->idx = idx;
2627c1f8881SStanislaw Gruszka 		return 0;
2637c1f8881SStanislaw Gruszka 	case MT_PHY_TYPE_HT_GF:
2647c1f8881SStanislaw Gruszka 		txrate->flags |= IEEE80211_TX_RC_GREEN_FIELD;
2657c1f8881SStanislaw Gruszka 		/* fall through */
2667c1f8881SStanislaw Gruszka 	case MT_PHY_TYPE_HT:
2677c1f8881SStanislaw Gruszka 		txrate->flags |= IEEE80211_TX_RC_MCS;
2687c1f8881SStanislaw Gruszka 		txrate->idx = idx;
2697c1f8881SStanislaw Gruszka 		break;
2707c1f8881SStanislaw Gruszka 	case MT_PHY_TYPE_VHT:
2717c1f8881SStanislaw Gruszka 		txrate->flags |= IEEE80211_TX_RC_VHT_MCS;
2727c1f8881SStanislaw Gruszka 		txrate->idx = idx;
2737c1f8881SStanislaw Gruszka 		break;
2747c1f8881SStanislaw Gruszka 	default:
2757c1f8881SStanislaw Gruszka 		return -EINVAL;
2767c1f8881SStanislaw Gruszka 	}
2777c1f8881SStanislaw Gruszka 
2787c1f8881SStanislaw Gruszka 	switch (FIELD_GET(MT_RXWI_RATE_BW, rate)) {
2797c1f8881SStanislaw Gruszka 	case MT_PHY_BW_20:
2807c1f8881SStanislaw Gruszka 		break;
2817c1f8881SStanislaw Gruszka 	case MT_PHY_BW_40:
2827c1f8881SStanislaw Gruszka 		txrate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2837c1f8881SStanislaw Gruszka 		break;
2847c1f8881SStanislaw Gruszka 	case MT_PHY_BW_80:
2857c1f8881SStanislaw Gruszka 		txrate->flags |= IEEE80211_TX_RC_80_MHZ_WIDTH;
2867c1f8881SStanislaw Gruszka 		break;
2877c1f8881SStanislaw Gruszka 	default:
2887c1f8881SStanislaw Gruszka 		return -EINVAL;
2897c1f8881SStanislaw Gruszka 	}
2907c1f8881SStanislaw Gruszka 
2917c1f8881SStanislaw Gruszka 	if (rate & MT_RXWI_RATE_SGI)
2927c1f8881SStanislaw Gruszka 		txrate->flags |= IEEE80211_TX_RC_SHORT_GI;
2937c1f8881SStanislaw Gruszka 
2947c1f8881SStanislaw Gruszka 	return 0;
2957c1f8881SStanislaw Gruszka }
2967c1f8881SStanislaw Gruszka 
2978d66af49SLorenzo Bianconi void mt76x02_mac_write_txwi(struct mt76x02_dev *dev, struct mt76x02_txwi *txwi,
298427f9ebeSLorenzo Bianconi 			    struct sk_buff *skb, struct mt76_wcid *wcid,
299427f9ebeSLorenzo Bianconi 			    struct ieee80211_sta *sta, int len)
300427f9ebeSLorenzo Bianconi {
301320c85e6SLorenzo Bianconi 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
302427f9ebeSLorenzo Bianconi 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
303427f9ebeSLorenzo Bianconi 	struct ieee80211_tx_rate *rate = &info->control.rates[0];
304427f9ebeSLorenzo Bianconi 	struct ieee80211_key_conf *key = info->control.hw_key;
305427f9ebeSLorenzo Bianconi 	u16 rate_ht_mask = FIELD_PREP(MT_RXWI_RATE_PHY, BIT(1) | BIT(2));
306320c85e6SLorenzo Bianconi 	u16 txwi_flags = 0;
307427f9ebeSLorenzo Bianconi 	u8 nss;
308427f9ebeSLorenzo Bianconi 	s8 txpwr_adj, max_txpwr_adj;
3098d66af49SLorenzo Bianconi 	u8 ccmp_pn[8], nstreams = dev->mt76.chainmask & 0xf;
310427f9ebeSLorenzo Bianconi 
311427f9ebeSLorenzo Bianconi 	memset(txwi, 0, sizeof(*txwi));
312427f9ebeSLorenzo Bianconi 
313427f9ebeSLorenzo Bianconi 	if (wcid)
314427f9ebeSLorenzo Bianconi 		txwi->wcid = wcid->idx;
315427f9ebeSLorenzo Bianconi 	else
316427f9ebeSLorenzo Bianconi 		txwi->wcid = 0xff;
317427f9ebeSLorenzo Bianconi 
318427f9ebeSLorenzo Bianconi 	txwi->pktid = 1;
319427f9ebeSLorenzo Bianconi 
320427f9ebeSLorenzo Bianconi 	if (wcid && wcid->sw_iv && key) {
321427f9ebeSLorenzo Bianconi 		u64 pn = atomic64_inc_return(&key->tx_pn);
322427f9ebeSLorenzo Bianconi 		ccmp_pn[0] = pn;
323427f9ebeSLorenzo Bianconi 		ccmp_pn[1] = pn >> 8;
324427f9ebeSLorenzo Bianconi 		ccmp_pn[2] = 0;
325427f9ebeSLorenzo Bianconi 		ccmp_pn[3] = 0x20 | (key->keyidx << 6);
326427f9ebeSLorenzo Bianconi 		ccmp_pn[4] = pn >> 16;
327427f9ebeSLorenzo Bianconi 		ccmp_pn[5] = pn >> 24;
328427f9ebeSLorenzo Bianconi 		ccmp_pn[6] = pn >> 32;
329427f9ebeSLorenzo Bianconi 		ccmp_pn[7] = pn >> 40;
330427f9ebeSLorenzo Bianconi 		txwi->iv = *((__le32 *)&ccmp_pn[0]);
331427f9ebeSLorenzo Bianconi 		txwi->eiv = *((__le32 *)&ccmp_pn[1]);
332427f9ebeSLorenzo Bianconi 	}
333427f9ebeSLorenzo Bianconi 
3348d66af49SLorenzo Bianconi 	spin_lock_bh(&dev->mt76.lock);
335427f9ebeSLorenzo Bianconi 	if (wcid && (rate->idx < 0 || !rate->count)) {
336427f9ebeSLorenzo Bianconi 		txwi->rate = wcid->tx_rate;
337427f9ebeSLorenzo Bianconi 		max_txpwr_adj = wcid->max_txpwr_adj;
338427f9ebeSLorenzo Bianconi 		nss = wcid->tx_rate_nss;
339427f9ebeSLorenzo Bianconi 	} else {
340427f9ebeSLorenzo Bianconi 		txwi->rate = mt76x02_mac_tx_rate_val(dev, rate, &nss);
34191be8e8aSLorenzo Bianconi 		max_txpwr_adj = mt76x02_tx_get_max_txpwr_adj(dev, rate);
342427f9ebeSLorenzo Bianconi 	}
3438d66af49SLorenzo Bianconi 	spin_unlock_bh(&dev->mt76.lock);
344427f9ebeSLorenzo Bianconi 
34591be8e8aSLorenzo Bianconi 	txpwr_adj = mt76x02_tx_get_txpwr_adj(dev, dev->mt76.txpower_conf,
346427f9ebeSLorenzo Bianconi 					     max_txpwr_adj);
347427f9ebeSLorenzo Bianconi 	txwi->ctl2 = FIELD_PREP(MT_TX_PWR_ADJ, txpwr_adj);
348427f9ebeSLorenzo Bianconi 
3498d66af49SLorenzo Bianconi 	if (nstreams > 1 && mt76_rev(&dev->mt76) >= MT76XX_REV_E4)
350427f9ebeSLorenzo Bianconi 		txwi->txstream = 0x13;
3518d66af49SLorenzo Bianconi 	else if (nstreams > 1 && mt76_rev(&dev->mt76) >= MT76XX_REV_E3 &&
352427f9ebeSLorenzo Bianconi 		 !(txwi->rate & cpu_to_le16(rate_ht_mask)))
353427f9ebeSLorenzo Bianconi 		txwi->txstream = 0x93;
354427f9ebeSLorenzo Bianconi 
355320c85e6SLorenzo Bianconi 	if (is_mt76x2(dev) && (info->flags & IEEE80211_TX_CTL_LDPC))
356320c85e6SLorenzo Bianconi 		txwi->rate |= cpu_to_le16(MT_RXWI_RATE_LDPC);
357320c85e6SLorenzo Bianconi 	if ((info->flags & IEEE80211_TX_CTL_STBC) && nss == 1)
358320c85e6SLorenzo Bianconi 		txwi->rate |= cpu_to_le16(MT_RXWI_RATE_STBC);
359320c85e6SLorenzo Bianconi 	if (nss > 1 && sta && sta->smps_mode == IEEE80211_SMPS_DYNAMIC)
360320c85e6SLorenzo Bianconi 		txwi_flags |= MT_TXWI_FLAGS_MMPS;
361320c85e6SLorenzo Bianconi 	if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
362320c85e6SLorenzo Bianconi 		txwi->ack_ctl |= MT_TXWI_ACK_CTL_REQ;
363320c85e6SLorenzo Bianconi 	if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
364320c85e6SLorenzo Bianconi 		txwi->ack_ctl |= MT_TXWI_ACK_CTL_NSEQ;
365320c85e6SLorenzo Bianconi 	if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
366320c85e6SLorenzo Bianconi 		txwi->pktid |= MT_TXWI_PKTID_PROBE;
367320c85e6SLorenzo Bianconi 	if ((info->flags & IEEE80211_TX_CTL_AMPDU) && sta) {
368320c85e6SLorenzo Bianconi 		u8 ba_size = IEEE80211_MIN_AMPDU_BUF;
369320c85e6SLorenzo Bianconi 
370320c85e6SLorenzo Bianconi 		ba_size <<= sta->ht_cap.ampdu_factor;
371320c85e6SLorenzo Bianconi 		ba_size = min_t(int, 63, ba_size - 1);
372320c85e6SLorenzo Bianconi 		if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
373320c85e6SLorenzo Bianconi 			ba_size = 0;
374320c85e6SLorenzo Bianconi 		txwi->ack_ctl |= FIELD_PREP(MT_TXWI_ACK_CTL_BA_WINDOW, ba_size);
375320c85e6SLorenzo Bianconi 
376320c85e6SLorenzo Bianconi 		txwi_flags |= MT_TXWI_FLAGS_AMPDU |
377320c85e6SLorenzo Bianconi 			 FIELD_PREP(MT_TXWI_FLAGS_MPDU_DENSITY,
378320c85e6SLorenzo Bianconi 				    sta->ht_cap.ampdu_density);
379320c85e6SLorenzo Bianconi 	}
380320c85e6SLorenzo Bianconi 
381320c85e6SLorenzo Bianconi 	if (ieee80211_is_probe_resp(hdr->frame_control) ||
382320c85e6SLorenzo Bianconi 	    ieee80211_is_beacon(hdr->frame_control))
383320c85e6SLorenzo Bianconi 		txwi_flags |= MT_TXWI_FLAGS_TS;
384320c85e6SLorenzo Bianconi 
385320c85e6SLorenzo Bianconi 	txwi->flags |= cpu_to_le16(txwi_flags);
386320c85e6SLorenzo Bianconi 	txwi->len_ctl = cpu_to_le16(len);
387427f9ebeSLorenzo Bianconi }
388427f9ebeSLorenzo Bianconi EXPORT_SYMBOL_GPL(mt76x02_mac_write_txwi);
389427f9ebeSLorenzo Bianconi 
3907c1f8881SStanislaw Gruszka static void
3918d66af49SLorenzo Bianconi mt76x02_mac_fill_tx_status(struct mt76x02_dev *dev,
3927c1f8881SStanislaw Gruszka 			   struct ieee80211_tx_info *info,
3937c1f8881SStanislaw Gruszka 			   struct mt76x02_tx_status *st, int n_frames)
3947c1f8881SStanislaw Gruszka {
3957c1f8881SStanislaw Gruszka 	struct ieee80211_tx_rate *rate = info->status.rates;
3967c1f8881SStanislaw Gruszka 	int cur_idx, last_rate;
3977c1f8881SStanislaw Gruszka 	int i;
3987c1f8881SStanislaw Gruszka 
3997c1f8881SStanislaw Gruszka 	if (!n_frames)
4007c1f8881SStanislaw Gruszka 		return;
4017c1f8881SStanislaw Gruszka 
4027c1f8881SStanislaw Gruszka 	last_rate = min_t(int, st->retry, IEEE80211_TX_MAX_RATES - 1);
4037c1f8881SStanislaw Gruszka 	mt76x02_mac_process_tx_rate(&rate[last_rate], st->rate,
4048d66af49SLorenzo Bianconi 				    dev->mt76.chandef.chan->band);
4057c1f8881SStanislaw Gruszka 	if (last_rate < IEEE80211_TX_MAX_RATES - 1)
4067c1f8881SStanislaw Gruszka 		rate[last_rate + 1].idx = -1;
4077c1f8881SStanislaw Gruszka 
4087c1f8881SStanislaw Gruszka 	cur_idx = rate[last_rate].idx + last_rate;
4097c1f8881SStanislaw Gruszka 	for (i = 0; i <= last_rate; i++) {
4107c1f8881SStanislaw Gruszka 		rate[i].flags = rate[last_rate].flags;
4117c1f8881SStanislaw Gruszka 		rate[i].idx = max_t(int, 0, cur_idx - i);
4127c1f8881SStanislaw Gruszka 		rate[i].count = 1;
4137c1f8881SStanislaw Gruszka 	}
4147c1f8881SStanislaw Gruszka 	rate[last_rate].count = st->retry + 1 - last_rate;
4157c1f8881SStanislaw Gruszka 
4167c1f8881SStanislaw Gruszka 	info->status.ampdu_len = n_frames;
4177c1f8881SStanislaw Gruszka 	info->status.ampdu_ack_len = st->success ? n_frames : 0;
4187c1f8881SStanislaw Gruszka 
4197c1f8881SStanislaw Gruszka 	if (st->pktid & MT_TXWI_PKTID_PROBE)
4207c1f8881SStanislaw Gruszka 		info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
4217c1f8881SStanislaw Gruszka 
4227c1f8881SStanislaw Gruszka 	if (st->aggr)
4237c1f8881SStanislaw Gruszka 		info->flags |= IEEE80211_TX_CTL_AMPDU |
4247c1f8881SStanislaw Gruszka 			       IEEE80211_TX_STAT_AMPDU;
4257c1f8881SStanislaw Gruszka 
4267c1f8881SStanislaw Gruszka 	if (!st->ack_req)
4277c1f8881SStanislaw Gruszka 		info->flags |= IEEE80211_TX_CTL_NO_ACK;
4287c1f8881SStanislaw Gruszka 	else if (st->success)
4297c1f8881SStanislaw Gruszka 		info->flags |= IEEE80211_TX_STAT_ACK;
4307c1f8881SStanislaw Gruszka }
4317c1f8881SStanislaw Gruszka 
4328d66af49SLorenzo Bianconi void mt76x02_send_tx_status(struct mt76x02_dev *dev,
4337c1f8881SStanislaw Gruszka 			    struct mt76x02_tx_status *stat, u8 *update)
4347c1f8881SStanislaw Gruszka {
4357c1f8881SStanislaw Gruszka 	struct ieee80211_tx_info info = {};
4367c1f8881SStanislaw Gruszka 	struct ieee80211_sta *sta = NULL;
4377c1f8881SStanislaw Gruszka 	struct mt76_wcid *wcid = NULL;
4387c1f8881SStanislaw Gruszka 	struct mt76x02_sta *msta = NULL;
4397c1f8881SStanislaw Gruszka 
4407c1f8881SStanislaw Gruszka 	rcu_read_lock();
4418d66af49SLorenzo Bianconi 	if (stat->wcid < ARRAY_SIZE(dev->mt76.wcid))
4428d66af49SLorenzo Bianconi 		wcid = rcu_dereference(dev->mt76.wcid[stat->wcid]);
4437c1f8881SStanislaw Gruszka 
4447c1f8881SStanislaw Gruszka 	if (wcid) {
4457c1f8881SStanislaw Gruszka 		void *priv;
4467c1f8881SStanislaw Gruszka 
4477c1f8881SStanislaw Gruszka 		priv = msta = container_of(wcid, struct mt76x02_sta, wcid);
4487c1f8881SStanislaw Gruszka 		sta = container_of(priv, struct ieee80211_sta,
4497c1f8881SStanislaw Gruszka 				   drv_priv);
4507c1f8881SStanislaw Gruszka 	}
4517c1f8881SStanislaw Gruszka 
4527c1f8881SStanislaw Gruszka 	if (msta && stat->aggr) {
4537c1f8881SStanislaw Gruszka 		u32 stat_val, stat_cache;
4547c1f8881SStanislaw Gruszka 
4557c1f8881SStanislaw Gruszka 		stat_val = stat->rate;
4567c1f8881SStanislaw Gruszka 		stat_val |= ((u32) stat->retry) << 16;
4577c1f8881SStanislaw Gruszka 		stat_cache = msta->status.rate;
4587c1f8881SStanislaw Gruszka 		stat_cache |= ((u32) msta->status.retry) << 16;
4597c1f8881SStanislaw Gruszka 
4607c1f8881SStanislaw Gruszka 		if (*update == 0 && stat_val == stat_cache &&
4617c1f8881SStanislaw Gruszka 		    stat->wcid == msta->status.wcid && msta->n_frames < 32) {
4627c1f8881SStanislaw Gruszka 			msta->n_frames++;
4637c1f8881SStanislaw Gruszka 			goto out;
4647c1f8881SStanislaw Gruszka 		}
4657c1f8881SStanislaw Gruszka 
4667c1f8881SStanislaw Gruszka 		mt76x02_mac_fill_tx_status(dev, &info, &msta->status,
4677c1f8881SStanislaw Gruszka 					   msta->n_frames);
4687c1f8881SStanislaw Gruszka 
4697c1f8881SStanislaw Gruszka 		msta->status = *stat;
4707c1f8881SStanislaw Gruszka 		msta->n_frames = 1;
4717c1f8881SStanislaw Gruszka 		*update = 0;
4727c1f8881SStanislaw Gruszka 	} else {
4737c1f8881SStanislaw Gruszka 		mt76x02_mac_fill_tx_status(dev, &info, stat, 1);
4747c1f8881SStanislaw Gruszka 		*update = 1;
4757c1f8881SStanislaw Gruszka 	}
4767c1f8881SStanislaw Gruszka 
4778d66af49SLorenzo Bianconi 	ieee80211_tx_status_noskb(dev->mt76.hw, sta, &info);
4787c1f8881SStanislaw Gruszka 
4797c1f8881SStanislaw Gruszka out:
4807c1f8881SStanislaw Gruszka 	rcu_read_unlock();
4817c1f8881SStanislaw Gruszka }
48274ff4539SStanislaw Gruszka 
48374ff4539SStanislaw Gruszka int
48474ff4539SStanislaw Gruszka mt76x02_mac_process_rate(struct mt76_rx_status *status, u16 rate)
48574ff4539SStanislaw Gruszka {
48674ff4539SStanislaw Gruszka 	u8 idx = FIELD_GET(MT_RXWI_RATE_INDEX, rate);
48774ff4539SStanislaw Gruszka 
48874ff4539SStanislaw Gruszka 	switch (FIELD_GET(MT_RXWI_RATE_PHY, rate)) {
48974ff4539SStanislaw Gruszka 	case MT_PHY_TYPE_OFDM:
49074ff4539SStanislaw Gruszka 		if (idx >= 8)
49174ff4539SStanislaw Gruszka 			idx = 0;
49274ff4539SStanislaw Gruszka 
49374ff4539SStanislaw Gruszka 		if (status->band == NL80211_BAND_2GHZ)
49474ff4539SStanislaw Gruszka 			idx += 4;
49574ff4539SStanislaw Gruszka 
49674ff4539SStanislaw Gruszka 		status->rate_idx = idx;
49774ff4539SStanislaw Gruszka 		return 0;
49874ff4539SStanislaw Gruszka 	case MT_PHY_TYPE_CCK:
49974ff4539SStanislaw Gruszka 		if (idx >= 8) {
50074ff4539SStanislaw Gruszka 			idx -= 8;
50174ff4539SStanislaw Gruszka 			status->enc_flags |= RX_ENC_FLAG_SHORTPRE;
50274ff4539SStanislaw Gruszka 		}
50374ff4539SStanislaw Gruszka 
50474ff4539SStanislaw Gruszka 		if (idx >= 4)
50574ff4539SStanislaw Gruszka 			idx = 0;
50674ff4539SStanislaw Gruszka 
50774ff4539SStanislaw Gruszka 		status->rate_idx = idx;
50874ff4539SStanislaw Gruszka 		return 0;
50974ff4539SStanislaw Gruszka 	case MT_PHY_TYPE_HT_GF:
51074ff4539SStanislaw Gruszka 		status->enc_flags |= RX_ENC_FLAG_HT_GF;
51174ff4539SStanislaw Gruszka 		/* fall through */
51274ff4539SStanislaw Gruszka 	case MT_PHY_TYPE_HT:
51374ff4539SStanislaw Gruszka 		status->encoding = RX_ENC_HT;
51474ff4539SStanislaw Gruszka 		status->rate_idx = idx;
51574ff4539SStanislaw Gruszka 		break;
51674ff4539SStanislaw Gruszka 	case MT_PHY_TYPE_VHT:
51774ff4539SStanislaw Gruszka 		status->encoding = RX_ENC_VHT;
51874ff4539SStanislaw Gruszka 		status->rate_idx = FIELD_GET(MT_RATE_INDEX_VHT_IDX, idx);
51974ff4539SStanislaw Gruszka 		status->nss = FIELD_GET(MT_RATE_INDEX_VHT_NSS, idx) + 1;
52074ff4539SStanislaw Gruszka 		break;
52174ff4539SStanislaw Gruszka 	default:
52274ff4539SStanislaw Gruszka 		return -EINVAL;
52374ff4539SStanislaw Gruszka 	}
52474ff4539SStanislaw Gruszka 
52574ff4539SStanislaw Gruszka 	if (rate & MT_RXWI_RATE_LDPC)
52674ff4539SStanislaw Gruszka 		status->enc_flags |= RX_ENC_FLAG_LDPC;
52774ff4539SStanislaw Gruszka 
52874ff4539SStanislaw Gruszka 	if (rate & MT_RXWI_RATE_SGI)
52974ff4539SStanislaw Gruszka 		status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
53074ff4539SStanislaw Gruszka 
53174ff4539SStanislaw Gruszka 	if (rate & MT_RXWI_RATE_STBC)
53274ff4539SStanislaw Gruszka 		status->enc_flags |= 1 << RX_ENC_FLAG_STBC_SHIFT;
53374ff4539SStanislaw Gruszka 
53474ff4539SStanislaw Gruszka 	switch (FIELD_GET(MT_RXWI_RATE_BW, rate)) {
53574ff4539SStanislaw Gruszka 	case MT_PHY_BW_20:
53674ff4539SStanislaw Gruszka 		break;
53774ff4539SStanislaw Gruszka 	case MT_PHY_BW_40:
53874ff4539SStanislaw Gruszka 		status->bw = RATE_INFO_BW_40;
53974ff4539SStanislaw Gruszka 		break;
54074ff4539SStanislaw Gruszka 	case MT_PHY_BW_80:
54174ff4539SStanislaw Gruszka 		status->bw = RATE_INFO_BW_80;
54274ff4539SStanislaw Gruszka 		break;
54374ff4539SStanislaw Gruszka 	default:
54474ff4539SStanislaw Gruszka 		break;
54574ff4539SStanislaw Gruszka 	}
54674ff4539SStanislaw Gruszka 
54774ff4539SStanislaw Gruszka 	return 0;
54874ff4539SStanislaw Gruszka }
54974ff4539SStanislaw Gruszka EXPORT_SYMBOL_GPL(mt76x02_mac_process_rate);
55089a8607cSLorenzo Bianconi 
5518d66af49SLorenzo Bianconi void mt76x02_mac_setaddr(struct mt76x02_dev *dev, u8 *addr)
55289a8607cSLorenzo Bianconi {
5538d66af49SLorenzo Bianconi 	ether_addr_copy(dev->mt76.macaddr, addr);
55489a8607cSLorenzo Bianconi 
5558d66af49SLorenzo Bianconi 	if (!is_valid_ether_addr(dev->mt76.macaddr)) {
5568d66af49SLorenzo Bianconi 		eth_random_addr(dev->mt76.macaddr);
5578d66af49SLorenzo Bianconi 		dev_info(dev->mt76.dev,
55889a8607cSLorenzo Bianconi 			 "Invalid MAC address, using random address %pM\n",
5598d66af49SLorenzo Bianconi 			 dev->mt76.macaddr);
56089a8607cSLorenzo Bianconi 	}
56189a8607cSLorenzo Bianconi 
5628d66af49SLorenzo Bianconi 	mt76_wr(dev, MT_MAC_ADDR_DW0, get_unaligned_le32(dev->mt76.macaddr));
5638d66af49SLorenzo Bianconi 	mt76_wr(dev, MT_MAC_ADDR_DW1,
5648d66af49SLorenzo Bianconi 		get_unaligned_le16(dev->mt76.macaddr + 4) |
56589a8607cSLorenzo Bianconi 		FIELD_PREP(MT_MAC_ADDR_DW1_U2ME_MASK, 0xff));
56689a8607cSLorenzo Bianconi }
56789a8607cSLorenzo Bianconi EXPORT_SYMBOL_GPL(mt76x02_mac_setaddr);
568d9f8934eSLorenzo Bianconi 
569d9f8934eSLorenzo Bianconi static int
570d9f8934eSLorenzo Bianconi mt76x02_mac_get_rssi(struct mt76x02_dev *dev, s8 rssi, int chain)
571d9f8934eSLorenzo Bianconi {
572d9f8934eSLorenzo Bianconi 	struct mt76x02_rx_freq_cal *cal = &dev->cal.rx;
573d9f8934eSLorenzo Bianconi 
574d9f8934eSLorenzo Bianconi 	rssi += cal->rssi_offset[chain];
575d9f8934eSLorenzo Bianconi 	rssi -= cal->lna_gain;
576d9f8934eSLorenzo Bianconi 
577d9f8934eSLorenzo Bianconi 	return rssi;
578d9f8934eSLorenzo Bianconi }
579d9f8934eSLorenzo Bianconi 
580d9f8934eSLorenzo Bianconi int mt76x02_mac_process_rx(struct mt76x02_dev *dev, struct sk_buff *skb,
581d9f8934eSLorenzo Bianconi 			   void *rxi)
582d9f8934eSLorenzo Bianconi {
583d9f8934eSLorenzo Bianconi 	struct mt76_rx_status *status = (struct mt76_rx_status *) skb->cb;
584d9f8934eSLorenzo Bianconi 	struct mt76x02_rxwi *rxwi = rxi;
585d9f8934eSLorenzo Bianconi 	struct mt76x02_sta *sta;
586d9f8934eSLorenzo Bianconi 	u32 rxinfo = le32_to_cpu(rxwi->rxinfo);
587d9f8934eSLorenzo Bianconi 	u32 ctl = le32_to_cpu(rxwi->ctl);
588d9f8934eSLorenzo Bianconi 	u16 rate = le16_to_cpu(rxwi->rate);
589d9f8934eSLorenzo Bianconi 	u16 tid_sn = le16_to_cpu(rxwi->tid_sn);
590d9f8934eSLorenzo Bianconi 	bool unicast = rxwi->rxinfo & cpu_to_le32(MT_RXINFO_UNICAST);
591d9f8934eSLorenzo Bianconi 	int i, pad_len = 0, nstreams = dev->mt76.chainmask & 0xf;
592d9f8934eSLorenzo Bianconi 	s8 signal;
593d9f8934eSLorenzo Bianconi 	u8 pn_len;
594d9f8934eSLorenzo Bianconi 	u8 wcid;
595d9f8934eSLorenzo Bianconi 	int len;
596d9f8934eSLorenzo Bianconi 
597d9f8934eSLorenzo Bianconi 	if (!test_bit(MT76_STATE_RUNNING, &dev->mt76.state))
598d9f8934eSLorenzo Bianconi 		return -EINVAL;
599d9f8934eSLorenzo Bianconi 
600d9f8934eSLorenzo Bianconi 	if (rxinfo & MT_RXINFO_L2PAD)
601d9f8934eSLorenzo Bianconi 		pad_len += 2;
602d9f8934eSLorenzo Bianconi 
603d9f8934eSLorenzo Bianconi 	if (rxinfo & MT_RXINFO_DECRYPT) {
604d9f8934eSLorenzo Bianconi 		status->flag |= RX_FLAG_DECRYPTED;
605d9f8934eSLorenzo Bianconi 		status->flag |= RX_FLAG_MMIC_STRIPPED;
606d9f8934eSLorenzo Bianconi 		status->flag |= RX_FLAG_MIC_STRIPPED;
607d9f8934eSLorenzo Bianconi 		status->flag |= RX_FLAG_IV_STRIPPED;
608d9f8934eSLorenzo Bianconi 	}
609d9f8934eSLorenzo Bianconi 
610d9f8934eSLorenzo Bianconi 	wcid = FIELD_GET(MT_RXWI_CTL_WCID, ctl);
611d9f8934eSLorenzo Bianconi 	sta = mt76x02_rx_get_sta(&dev->mt76, wcid);
612d9f8934eSLorenzo Bianconi 	status->wcid = mt76x02_rx_get_sta_wcid(sta, unicast);
613d9f8934eSLorenzo Bianconi 
614d9f8934eSLorenzo Bianconi 	len = FIELD_GET(MT_RXWI_CTL_MPDU_LEN, ctl);
615d9f8934eSLorenzo Bianconi 	pn_len = FIELD_GET(MT_RXINFO_PN_LEN, rxinfo);
616d9f8934eSLorenzo Bianconi 	if (pn_len) {
617d9f8934eSLorenzo Bianconi 		int offset = ieee80211_get_hdrlen_from_skb(skb) + pad_len;
618d9f8934eSLorenzo Bianconi 		u8 *data = skb->data + offset;
619d9f8934eSLorenzo Bianconi 
620d9f8934eSLorenzo Bianconi 		status->iv[0] = data[7];
621d9f8934eSLorenzo Bianconi 		status->iv[1] = data[6];
622d9f8934eSLorenzo Bianconi 		status->iv[2] = data[5];
623d9f8934eSLorenzo Bianconi 		status->iv[3] = data[4];
624d9f8934eSLorenzo Bianconi 		status->iv[4] = data[1];
625d9f8934eSLorenzo Bianconi 		status->iv[5] = data[0];
626d9f8934eSLorenzo Bianconi 
627d9f8934eSLorenzo Bianconi 		/*
628d9f8934eSLorenzo Bianconi 		 * Driver CCMP validation can't deal with fragments.
629d9f8934eSLorenzo Bianconi 		 * Let mac80211 take care of it.
630d9f8934eSLorenzo Bianconi 		 */
631d9f8934eSLorenzo Bianconi 		if (rxinfo & MT_RXINFO_FRAG) {
632d9f8934eSLorenzo Bianconi 			status->flag &= ~RX_FLAG_IV_STRIPPED;
633d9f8934eSLorenzo Bianconi 		} else {
634d9f8934eSLorenzo Bianconi 			pad_len += pn_len << 2;
635d9f8934eSLorenzo Bianconi 			len -= pn_len << 2;
636d9f8934eSLorenzo Bianconi 		}
637d9f8934eSLorenzo Bianconi 	}
638d9f8934eSLorenzo Bianconi 
639d9f8934eSLorenzo Bianconi 	mt76x02_remove_hdr_pad(skb, pad_len);
640d9f8934eSLorenzo Bianconi 
641d9f8934eSLorenzo Bianconi 	if ((rxinfo & MT_RXINFO_BA) && !(rxinfo & MT_RXINFO_NULL))
642d9f8934eSLorenzo Bianconi 		status->aggr = true;
643d9f8934eSLorenzo Bianconi 
644d9f8934eSLorenzo Bianconi 	if (WARN_ON_ONCE(len > skb->len))
645d9f8934eSLorenzo Bianconi 		return -EINVAL;
646d9f8934eSLorenzo Bianconi 
647d9f8934eSLorenzo Bianconi 	pskb_trim(skb, len);
648d9f8934eSLorenzo Bianconi 
649d9f8934eSLorenzo Bianconi 	status->chains = BIT(0);
650d9f8934eSLorenzo Bianconi 	signal = mt76x02_mac_get_rssi(dev, rxwi->rssi[0], 0);
651d9f8934eSLorenzo Bianconi 	for (i = 1; i < nstreams; i++) {
652d9f8934eSLorenzo Bianconi 		status->chains |= BIT(i);
653d9f8934eSLorenzo Bianconi 		status->chain_signal[i] = mt76x02_mac_get_rssi(dev,
654d9f8934eSLorenzo Bianconi 							       rxwi->rssi[i],
655d9f8934eSLorenzo Bianconi 							       i);
656d9f8934eSLorenzo Bianconi 		signal = max_t(s8, signal, status->chain_signal[i]);
657d9f8934eSLorenzo Bianconi 	}
658d9f8934eSLorenzo Bianconi 	status->signal = signal;
659d9f8934eSLorenzo Bianconi 	status->freq = dev->mt76.chandef.chan->center_freq;
660d9f8934eSLorenzo Bianconi 	status->band = dev->mt76.chandef.chan->band;
661d9f8934eSLorenzo Bianconi 
662d9f8934eSLorenzo Bianconi 	status->tid = FIELD_GET(MT_RXWI_TID, tid_sn);
663d9f8934eSLorenzo Bianconi 	status->seqno = FIELD_GET(MT_RXWI_SN, tid_sn);
664d9f8934eSLorenzo Bianconi 
665d9f8934eSLorenzo Bianconi 	if (sta) {
666d9f8934eSLorenzo Bianconi 		ewma_signal_add(&sta->rssi, status->signal);
667d9f8934eSLorenzo Bianconi 		sta->inactive_count = 0;
668d9f8934eSLorenzo Bianconi 	}
669d9f8934eSLorenzo Bianconi 
670d9f8934eSLorenzo Bianconi 	return mt76x02_mac_process_rate(status, rate);
671d9f8934eSLorenzo Bianconi }
6723e2342edSLorenzo Bianconi 
6733e2342edSLorenzo Bianconi void mt76x02_mac_poll_tx_status(struct mt76x02_dev *dev, bool irq)
6743e2342edSLorenzo Bianconi {
6753e2342edSLorenzo Bianconi 	struct mt76x02_tx_status stat = {};
6763e2342edSLorenzo Bianconi 	unsigned long flags;
6773e2342edSLorenzo Bianconi 	u8 update = 1;
6783e2342edSLorenzo Bianconi 	bool ret;
6793e2342edSLorenzo Bianconi 
6803e2342edSLorenzo Bianconi 	if (!test_bit(MT76_STATE_RUNNING, &dev->mt76.state))
6813e2342edSLorenzo Bianconi 		return;
6823e2342edSLorenzo Bianconi 
6833e2342edSLorenzo Bianconi 	trace_mac_txstat_poll(dev);
6843e2342edSLorenzo Bianconi 
6853e2342edSLorenzo Bianconi 	while (!irq || !kfifo_is_full(&dev->txstatus_fifo)) {
6863e2342edSLorenzo Bianconi 		spin_lock_irqsave(&dev->mt76.mmio.irq_lock, flags);
6878d66af49SLorenzo Bianconi 		ret = mt76x02_mac_load_tx_status(dev, &stat);
6883e2342edSLorenzo Bianconi 		spin_unlock_irqrestore(&dev->mt76.mmio.irq_lock, flags);
6893e2342edSLorenzo Bianconi 
6903e2342edSLorenzo Bianconi 		if (!ret)
6913e2342edSLorenzo Bianconi 			break;
6923e2342edSLorenzo Bianconi 
6933e2342edSLorenzo Bianconi 		trace_mac_txstat_fetch(dev, &stat);
6943e2342edSLorenzo Bianconi 
6953e2342edSLorenzo Bianconi 		if (!irq) {
6968d66af49SLorenzo Bianconi 			mt76x02_send_tx_status(dev, &stat, &update);
6973e2342edSLorenzo Bianconi 			continue;
6983e2342edSLorenzo Bianconi 		}
6993e2342edSLorenzo Bianconi 
7003e2342edSLorenzo Bianconi 		kfifo_put(&dev->txstatus_fifo, stat);
7013e2342edSLorenzo Bianconi 	}
7023e2342edSLorenzo Bianconi }
703466495b1SLorenzo Bianconi 
704466495b1SLorenzo Bianconi static void
705466495b1SLorenzo Bianconi mt76x02_mac_queue_txdone(struct mt76x02_dev *dev, struct sk_buff *skb,
706466495b1SLorenzo Bianconi 			 void *txwi_ptr)
707466495b1SLorenzo Bianconi {
708466495b1SLorenzo Bianconi 	struct mt76x02_tx_info *txi = mt76x02_skb_tx_info(skb);
709466495b1SLorenzo Bianconi 	struct mt76x02_txwi *txwi = txwi_ptr;
710466495b1SLorenzo Bianconi 
711466495b1SLorenzo Bianconi 	mt76x02_mac_poll_tx_status(dev, false);
712466495b1SLorenzo Bianconi 
713466495b1SLorenzo Bianconi 	txi->tries = 0;
714466495b1SLorenzo Bianconi 	txi->jiffies = jiffies;
715466495b1SLorenzo Bianconi 	txi->wcid = txwi->wcid;
716466495b1SLorenzo Bianconi 	txi->pktid = txwi->pktid;
717466495b1SLorenzo Bianconi 	trace_mac_txdone_add(dev, txwi->wcid, txwi->pktid);
718466495b1SLorenzo Bianconi 	mt76x02_tx_complete(&dev->mt76, skb);
719466495b1SLorenzo Bianconi }
720466495b1SLorenzo Bianconi 
721466495b1SLorenzo Bianconi void mt76x02_tx_complete_skb(struct mt76_dev *mdev, struct mt76_queue *q,
722466495b1SLorenzo Bianconi 			     struct mt76_queue_entry *e, bool flush)
723466495b1SLorenzo Bianconi {
724466495b1SLorenzo Bianconi 	struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
725466495b1SLorenzo Bianconi 
726466495b1SLorenzo Bianconi 	if (e->txwi)
727466495b1SLorenzo Bianconi 		mt76x02_mac_queue_txdone(dev, e->skb, &e->txwi->txwi);
728466495b1SLorenzo Bianconi 	else
729466495b1SLorenzo Bianconi 		dev_kfree_skb_any(e->skb);
730466495b1SLorenzo Bianconi }
731466495b1SLorenzo Bianconi EXPORT_SYMBOL_GPL(mt76x02_tx_complete_skb);
73262503186SLorenzo Bianconi 
73362503186SLorenzo Bianconi void mt76x02_update_channel(struct mt76_dev *mdev)
73462503186SLorenzo Bianconi {
73562503186SLorenzo Bianconi 	struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
73662503186SLorenzo Bianconi 	struct mt76_channel_state *state;
73762503186SLorenzo Bianconi 	u32 active, busy;
73862503186SLorenzo Bianconi 
73962503186SLorenzo Bianconi 	state = mt76_channel_state(&dev->mt76, dev->mt76.chandef.chan);
74062503186SLorenzo Bianconi 
74162503186SLorenzo Bianconi 	busy = mt76_rr(dev, MT_CH_BUSY);
74262503186SLorenzo Bianconi 	active = busy + mt76_rr(dev, MT_CH_IDLE);
74362503186SLorenzo Bianconi 
74462503186SLorenzo Bianconi 	spin_lock_bh(&dev->mt76.cc_lock);
74562503186SLorenzo Bianconi 	state->cc_busy += busy;
74662503186SLorenzo Bianconi 	state->cc_active += active;
74762503186SLorenzo Bianconi 	spin_unlock_bh(&dev->mt76.cc_lock);
74862503186SLorenzo Bianconi }
74962503186SLorenzo Bianconi EXPORT_SYMBOL_GPL(mt76x02_update_channel);
7507dd73588SLorenzo Bianconi 
75173556561SLorenzo Bianconi static void mt76x02_check_mac_err(struct mt76x02_dev *dev)
75273556561SLorenzo Bianconi {
75373556561SLorenzo Bianconi 	u32 val = mt76_rr(dev, 0x10f4);
75473556561SLorenzo Bianconi 
75573556561SLorenzo Bianconi 	if (!(val & BIT(29)) || !(val & (BIT(7) | BIT(5))))
75673556561SLorenzo Bianconi 		return;
75773556561SLorenzo Bianconi 
75873556561SLorenzo Bianconi 	dev_err(dev->mt76.dev, "mac specific condition occurred\n");
75973556561SLorenzo Bianconi 
76073556561SLorenzo Bianconi 	mt76_set(dev, MT_MAC_SYS_CTRL, MT_MAC_SYS_CTRL_RESET_CSR);
76173556561SLorenzo Bianconi 	udelay(10);
76273556561SLorenzo Bianconi 	mt76_clear(dev, MT_MAC_SYS_CTRL,
76373556561SLorenzo Bianconi 		   MT_MAC_SYS_CTRL_ENABLE_TX | MT_MAC_SYS_CTRL_ENABLE_RX);
76473556561SLorenzo Bianconi }
76573556561SLorenzo Bianconi 
7667dd73588SLorenzo Bianconi void mt76x02_mac_work(struct work_struct *work)
7677dd73588SLorenzo Bianconi {
7687dd73588SLorenzo Bianconi 	struct mt76x02_dev *dev = container_of(work, struct mt76x02_dev,
7697dd73588SLorenzo Bianconi 					       mac_work.work);
7707dd73588SLorenzo Bianconi 	int i, idx;
7717dd73588SLorenzo Bianconi 
7727dd73588SLorenzo Bianconi 	mt76x02_update_channel(&dev->mt76);
7737dd73588SLorenzo Bianconi 	for (i = 0, idx = 0; i < 16; i++) {
7747dd73588SLorenzo Bianconi 		u32 val = mt76_rr(dev, MT_TX_AGG_CNT(i));
7757dd73588SLorenzo Bianconi 
7767dd73588SLorenzo Bianconi 		dev->aggr_stats[idx++] += val & 0xffff;
7777dd73588SLorenzo Bianconi 		dev->aggr_stats[idx++] += val >> 16;
7787dd73588SLorenzo Bianconi 	}
7797dd73588SLorenzo Bianconi 
78073556561SLorenzo Bianconi 	/* XXX: check beacon stuck for ap mode */
78173556561SLorenzo Bianconi 	if (!dev->beacon_mask)
78273556561SLorenzo Bianconi 		mt76x02_check_mac_err(dev);
78373556561SLorenzo Bianconi 
7847dd73588SLorenzo Bianconi 	ieee80211_queue_delayed_work(mt76_hw(dev), &dev->mac_work,
7857dd73588SLorenzo Bianconi 				     MT_CALIBRATE_INTERVAL);
7867dd73588SLorenzo Bianconi }
7877dd73588SLorenzo Bianconi EXPORT_SYMBOL_GPL(mt76x02_mac_work);
788