xref: /OK3568_Linux_fs/kernel/drivers/net/wireless/mediatek/mt76/mt76x02_mac.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: ISC */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
4*4882a593Smuzhiyun  * Copyright (C) 2018 Stanislaw Gruszka <stf_xl@wp.pl>
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #ifndef __MT76X02_MAC_H
8*4882a593Smuzhiyun #define __MT76X02_MAC_H
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun struct mt76x02_dev;
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun struct mt76x02_tx_status {
13*4882a593Smuzhiyun 	u8 valid:1;
14*4882a593Smuzhiyun 	u8 success:1;
15*4882a593Smuzhiyun 	u8 aggr:1;
16*4882a593Smuzhiyun 	u8 ack_req:1;
17*4882a593Smuzhiyun 	u8 wcid;
18*4882a593Smuzhiyun 	u8 pktid;
19*4882a593Smuzhiyun 	u8 retry;
20*4882a593Smuzhiyun 	u16 rate;
21*4882a593Smuzhiyun } __packed __aligned(2);
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #define MT_VIF_WCID(_n)		(254 - ((_n) & 7))
24*4882a593Smuzhiyun #define MT_MAX_VIFS		8
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #define MT_PKTID_RATE		GENMASK(4, 0)
27*4882a593Smuzhiyun #define MT_PKTID_AC		GENMASK(6, 5)
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun struct mt76x02_vif {
30*4882a593Smuzhiyun 	struct mt76_wcid group_wcid; /* must be first */
31*4882a593Smuzhiyun 	u8 idx;
32*4882a593Smuzhiyun };
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun DECLARE_EWMA(pktlen, 8, 8);
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun struct mt76x02_sta {
37*4882a593Smuzhiyun 	struct mt76_wcid wcid; /* must be first */
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 	struct mt76x02_vif *vif;
40*4882a593Smuzhiyun 	struct mt76x02_tx_status status;
41*4882a593Smuzhiyun 	int n_frames;
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 	struct ewma_pktlen pktlen;
44*4882a593Smuzhiyun };
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun #define MT_RXINFO_BA			BIT(0)
47*4882a593Smuzhiyun #define MT_RXINFO_DATA			BIT(1)
48*4882a593Smuzhiyun #define MT_RXINFO_NULL			BIT(2)
49*4882a593Smuzhiyun #define MT_RXINFO_FRAG			BIT(3)
50*4882a593Smuzhiyun #define MT_RXINFO_UNICAST		BIT(4)
51*4882a593Smuzhiyun #define MT_RXINFO_MULTICAST		BIT(5)
52*4882a593Smuzhiyun #define MT_RXINFO_BROADCAST		BIT(6)
53*4882a593Smuzhiyun #define MT_RXINFO_MYBSS			BIT(7)
54*4882a593Smuzhiyun #define MT_RXINFO_CRCERR		BIT(8)
55*4882a593Smuzhiyun #define MT_RXINFO_ICVERR		BIT(9)
56*4882a593Smuzhiyun #define MT_RXINFO_MICERR		BIT(10)
57*4882a593Smuzhiyun #define MT_RXINFO_AMSDU			BIT(11)
58*4882a593Smuzhiyun #define MT_RXINFO_HTC			BIT(12)
59*4882a593Smuzhiyun #define MT_RXINFO_RSSI			BIT(13)
60*4882a593Smuzhiyun #define MT_RXINFO_L2PAD			BIT(14)
61*4882a593Smuzhiyun #define MT_RXINFO_AMPDU			BIT(15)
62*4882a593Smuzhiyun #define MT_RXINFO_DECRYPT		BIT(16)
63*4882a593Smuzhiyun #define MT_RXINFO_BSSIDX3		BIT(17)
64*4882a593Smuzhiyun #define MT_RXINFO_WAPI_KEY		BIT(18)
65*4882a593Smuzhiyun #define MT_RXINFO_PN_LEN		GENMASK(21, 19)
66*4882a593Smuzhiyun #define MT_RXINFO_SW_FTYPE0		BIT(22)
67*4882a593Smuzhiyun #define MT_RXINFO_SW_FTYPE1		BIT(23)
68*4882a593Smuzhiyun #define MT_RXINFO_PROBE_RESP		BIT(24)
69*4882a593Smuzhiyun #define MT_RXINFO_BEACON		BIT(25)
70*4882a593Smuzhiyun #define MT_RXINFO_DISASSOC		BIT(26)
71*4882a593Smuzhiyun #define MT_RXINFO_DEAUTH		BIT(27)
72*4882a593Smuzhiyun #define MT_RXINFO_ACTION		BIT(28)
73*4882a593Smuzhiyun #define MT_RXINFO_TCP_SUM_ERR		BIT(30)
74*4882a593Smuzhiyun #define MT_RXINFO_IP_SUM_ERR		BIT(31)
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun #define MT_RXWI_CTL_WCID		GENMASK(7, 0)
77*4882a593Smuzhiyun #define MT_RXWI_CTL_KEY_IDX		GENMASK(9, 8)
78*4882a593Smuzhiyun #define MT_RXWI_CTL_BSS_IDX		GENMASK(12, 10)
79*4882a593Smuzhiyun #define MT_RXWI_CTL_UDF			GENMASK(15, 13)
80*4882a593Smuzhiyun #define MT_RXWI_CTL_MPDU_LEN		GENMASK(29, 16)
81*4882a593Smuzhiyun #define MT_RXWI_CTL_EOF			BIT(31)
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun #define MT_RXWI_TID			GENMASK(3, 0)
84*4882a593Smuzhiyun #define MT_RXWI_SN			GENMASK(15, 4)
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun #define MT_RXWI_RATE_INDEX		GENMASK(5, 0)
87*4882a593Smuzhiyun #define MT_RXWI_RATE_LDPC		BIT(6)
88*4882a593Smuzhiyun #define MT_RXWI_RATE_BW			GENMASK(8, 7)
89*4882a593Smuzhiyun #define MT_RXWI_RATE_SGI		BIT(9)
90*4882a593Smuzhiyun #define MT_RXWI_RATE_STBC		BIT(10)
91*4882a593Smuzhiyun #define MT_RXWI_RATE_LDPC_EXSYM		BIT(11)
92*4882a593Smuzhiyun #define MT_RXWI_RATE_PHY		GENMASK(15, 13)
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun #define MT_RATE_INDEX_VHT_IDX		GENMASK(3, 0)
95*4882a593Smuzhiyun #define MT_RATE_INDEX_VHT_NSS		GENMASK(5, 4)
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun struct mt76x02_rxwi {
98*4882a593Smuzhiyun 	__le32 rxinfo;
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	__le32 ctl;
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	__le16 tid_sn;
103*4882a593Smuzhiyun 	__le16 rate;
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	u8 rssi[4];
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	__le32 bbp_rxinfo[4];
108*4882a593Smuzhiyun };
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun #define MT_TX_PWR_ADJ			GENMASK(3, 0)
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun enum mt76x2_phy_bandwidth {
113*4882a593Smuzhiyun 	MT_PHY_BW_20,
114*4882a593Smuzhiyun 	MT_PHY_BW_40,
115*4882a593Smuzhiyun 	MT_PHY_BW_80,
116*4882a593Smuzhiyun };
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun #define MT_TXWI_FLAGS_FRAG		BIT(0)
119*4882a593Smuzhiyun #define MT_TXWI_FLAGS_MMPS		BIT(1)
120*4882a593Smuzhiyun #define MT_TXWI_FLAGS_CFACK		BIT(2)
121*4882a593Smuzhiyun #define MT_TXWI_FLAGS_TS		BIT(3)
122*4882a593Smuzhiyun #define MT_TXWI_FLAGS_AMPDU		BIT(4)
123*4882a593Smuzhiyun #define MT_TXWI_FLAGS_MPDU_DENSITY	GENMASK(7, 5)
124*4882a593Smuzhiyun #define MT_TXWI_FLAGS_TXOP		GENMASK(9, 8)
125*4882a593Smuzhiyun #define MT_TXWI_FLAGS_NDPS		BIT(10)
126*4882a593Smuzhiyun #define MT_TXWI_FLAGS_RTSBWSIG		BIT(11)
127*4882a593Smuzhiyun #define MT_TXWI_FLAGS_NDP_BW		GENMASK(13, 12)
128*4882a593Smuzhiyun #define MT_TXWI_FLAGS_SOUND		BIT(14)
129*4882a593Smuzhiyun #define MT_TXWI_FLAGS_TX_RATE_LUT	BIT(15)
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun #define MT_TXWI_ACK_CTL_REQ		BIT(0)
132*4882a593Smuzhiyun #define MT_TXWI_ACK_CTL_NSEQ		BIT(1)
133*4882a593Smuzhiyun #define MT_TXWI_ACK_CTL_BA_WINDOW	GENMASK(7, 2)
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun struct mt76x02_txwi {
136*4882a593Smuzhiyun 	__le16 flags;
137*4882a593Smuzhiyun 	__le16 rate;
138*4882a593Smuzhiyun 	u8 ack_ctl;
139*4882a593Smuzhiyun 	u8 wcid;
140*4882a593Smuzhiyun 	__le16 len_ctl;
141*4882a593Smuzhiyun 	__le32 iv;
142*4882a593Smuzhiyun 	__le32 eiv;
143*4882a593Smuzhiyun 	u8 aid;
144*4882a593Smuzhiyun 	u8 txstream;
145*4882a593Smuzhiyun 	u8 ctl2;
146*4882a593Smuzhiyun 	u8 pktid;
147*4882a593Smuzhiyun } __packed __aligned(4);
148*4882a593Smuzhiyun 
mt76x02_wait_for_mac(struct mt76_dev * dev)149*4882a593Smuzhiyun static inline bool mt76x02_wait_for_mac(struct mt76_dev *dev)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun 	const u32 MAC_CSR0 = 0x1000;
152*4882a593Smuzhiyun 	int i;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	for (i = 0; i < 500; i++) {
155*4882a593Smuzhiyun 		if (test_bit(MT76_REMOVED, &dev->phy.state))
156*4882a593Smuzhiyun 			return false;
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 		switch (dev->bus->rr(dev, MAC_CSR0)) {
159*4882a593Smuzhiyun 		case 0:
160*4882a593Smuzhiyun 		case ~0:
161*4882a593Smuzhiyun 			break;
162*4882a593Smuzhiyun 		default:
163*4882a593Smuzhiyun 			return true;
164*4882a593Smuzhiyun 		}
165*4882a593Smuzhiyun 		usleep_range(5000, 10000);
166*4882a593Smuzhiyun 	}
167*4882a593Smuzhiyun 	return false;
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun void mt76x02_mac_reset_counters(struct mt76x02_dev *dev);
171*4882a593Smuzhiyun void mt76x02_mac_set_short_preamble(struct mt76x02_dev *dev, bool enable);
172*4882a593Smuzhiyun int mt76x02_mac_shared_key_setup(struct mt76x02_dev *dev, u8 vif_idx,
173*4882a593Smuzhiyun 				 u8 key_idx, struct ieee80211_key_conf *key);
174*4882a593Smuzhiyun int mt76x02_mac_wcid_set_key(struct mt76x02_dev *dev, u8 idx,
175*4882a593Smuzhiyun 			     struct ieee80211_key_conf *key);
176*4882a593Smuzhiyun void mt76x02_mac_wcid_sync_pn(struct mt76x02_dev *dev, u8 idx,
177*4882a593Smuzhiyun 			      struct ieee80211_key_conf *key);
178*4882a593Smuzhiyun void mt76x02_mac_wcid_setup(struct mt76x02_dev *dev, u8 idx, u8 vif_idx,
179*4882a593Smuzhiyun 			    u8 *mac);
180*4882a593Smuzhiyun void mt76x02_mac_wcid_set_drop(struct mt76x02_dev *dev, u8 idx, bool drop);
181*4882a593Smuzhiyun void mt76x02_mac_wcid_set_rate(struct mt76x02_dev *dev, struct mt76_wcid *wcid,
182*4882a593Smuzhiyun 			       const struct ieee80211_tx_rate *rate);
183*4882a593Smuzhiyun bool mt76x02_mac_load_tx_status(struct mt76x02_dev *dev,
184*4882a593Smuzhiyun 				struct mt76x02_tx_status *stat);
185*4882a593Smuzhiyun void mt76x02_send_tx_status(struct mt76x02_dev *dev,
186*4882a593Smuzhiyun 			    struct mt76x02_tx_status *stat, u8 *update);
187*4882a593Smuzhiyun int mt76x02_mac_process_rx(struct mt76x02_dev *dev, struct sk_buff *skb,
188*4882a593Smuzhiyun 			   void *rxi);
189*4882a593Smuzhiyun void mt76x02_mac_set_tx_protection(struct mt76x02_dev *dev, bool legacy_prot,
190*4882a593Smuzhiyun 				   int ht_mode);
191*4882a593Smuzhiyun void mt76x02_mac_set_rts_thresh(struct mt76x02_dev *dev, u32 val);
192*4882a593Smuzhiyun void mt76x02_mac_setaddr(struct mt76x02_dev *dev, const u8 *addr);
193*4882a593Smuzhiyun void mt76x02_mac_write_txwi(struct mt76x02_dev *dev, struct mt76x02_txwi *txwi,
194*4882a593Smuzhiyun 			    struct sk_buff *skb, struct mt76_wcid *wcid,
195*4882a593Smuzhiyun 			    struct ieee80211_sta *sta, int len);
196*4882a593Smuzhiyun void mt76x02_mac_poll_tx_status(struct mt76x02_dev *dev, bool irq);
197*4882a593Smuzhiyun void mt76x02_tx_complete_skb(struct mt76_dev *mdev, struct mt76_queue_entry *e);
198*4882a593Smuzhiyun void mt76x02_update_channel(struct mt76_dev *mdev);
199*4882a593Smuzhiyun void mt76x02_mac_work(struct work_struct *work);
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun void mt76x02_mac_cc_reset(struct mt76x02_dev *dev);
202*4882a593Smuzhiyun void mt76x02_mac_set_bssid(struct mt76x02_dev *dev, u8 idx, const u8 *addr);
203*4882a593Smuzhiyun void mt76x02_mac_set_beacon(struct mt76x02_dev *dev, struct sk_buff *skb);
204*4882a593Smuzhiyun void mt76x02_mac_set_beacon_enable(struct mt76x02_dev *dev,
205*4882a593Smuzhiyun 				   struct ieee80211_vif *vif, bool enable);
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun void mt76x02_edcca_init(struct mt76x02_dev *dev);
208*4882a593Smuzhiyun #endif
209