xref: /linux/drivers/net/wireless/mediatek/mt76/mt76_connac3_mac.c (revision 621cde16e49b3ecf7d59a8106a20aaebfb4a59a9)
1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2023 MediaTek Inc. */
3 
4 #include "mt76_connac.h"
5 #include "mt76_connac3_mac.h"
6 #include "dma.h"
7 
8 #define HE_BITS(f)		cpu_to_le16(IEEE80211_RADIOTAP_HE_##f)
9 #define EHT_BITS(f)		cpu_to_le32(IEEE80211_RADIOTAP_EHT_##f)
10 #define HE_PREP(f, m, v)	le16_encode_bits(le32_get_bits(v, MT_CRXV_HE_##m),\
11 						 IEEE80211_RADIOTAP_HE_##f)
12 #define EHT_PREP(f, m, v)	le32_encode_bits(le32_get_bits(v, MT_CRXV_EHT_##m),\
13 						 IEEE80211_RADIOTAP_EHT_##f)
14 
15 static void
mt76_connac3_mac_decode_he_radiotap_ru(struct mt76_rx_status * status,struct ieee80211_radiotap_he * he,__le32 * rxv)16 mt76_connac3_mac_decode_he_radiotap_ru(struct mt76_rx_status *status,
17 				       struct ieee80211_radiotap_he *he,
18 				       __le32 *rxv)
19 {
20 	u32 ru = le32_get_bits(rxv[0], MT_PRXV_HE_RU_ALLOC), offs = 0;
21 
22 	status->bw = RATE_INFO_BW_HE_RU;
23 
24 	switch (ru) {
25 	case 0 ... 36:
26 		status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_26;
27 		offs = ru;
28 		break;
29 	case 37 ... 52:
30 		status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_52;
31 		offs = ru - 37;
32 		break;
33 	case 53 ... 60:
34 		status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_106;
35 		offs = ru - 53;
36 		break;
37 	case 61 ... 64:
38 		status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_242;
39 		offs = ru - 61;
40 		break;
41 	case 65 ... 66:
42 		status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_484;
43 		offs = ru - 65;
44 		break;
45 	case 67:
46 		status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_996;
47 		break;
48 	case 68:
49 		status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_2x996;
50 		break;
51 	}
52 
53 	he->data1 |= HE_BITS(DATA1_BW_RU_ALLOC_KNOWN);
54 	he->data2 |= HE_BITS(DATA2_RU_OFFSET_KNOWN) |
55 		     le16_encode_bits(offs,
56 				      IEEE80211_RADIOTAP_HE_DATA2_RU_OFFSET);
57 }
58 
59 #define MU_PREP(f, v)	le16_encode_bits(v, IEEE80211_RADIOTAP_HE_MU_##f)
60 static void
mt76_connac3_mac_decode_he_mu_radiotap(struct sk_buff * skb,__le32 * rxv)61 mt76_connac3_mac_decode_he_mu_radiotap(struct sk_buff *skb, __le32 *rxv)
62 {
63 	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
64 	static const struct ieee80211_radiotap_he_mu mu_known = {
65 		.flags1 = HE_BITS(MU_FLAGS1_SIG_B_MCS_KNOWN) |
66 			  HE_BITS(MU_FLAGS1_SIG_B_DCM_KNOWN) |
67 			  HE_BITS(MU_FLAGS1_CH1_RU_KNOWN) |
68 			  HE_BITS(MU_FLAGS1_SIG_B_SYMS_USERS_KNOWN),
69 		.flags2 = HE_BITS(MU_FLAGS2_BW_FROM_SIG_A_BW_KNOWN),
70 	};
71 	struct ieee80211_radiotap_he_mu *he_mu;
72 
73 	status->flag |= RX_FLAG_RADIOTAP_HE_MU;
74 
75 	he_mu = skb_push(skb, sizeof(mu_known));
76 	memcpy(he_mu, &mu_known, sizeof(mu_known));
77 
78 	he_mu->flags1 |= MU_PREP(FLAGS1_SIG_B_MCS, status->rate_idx);
79 	if (status->he_dcm)
80 		he_mu->flags1 |= MU_PREP(FLAGS1_SIG_B_DCM, status->he_dcm);
81 
82 	he_mu->flags2 |= MU_PREP(FLAGS2_BW_FROM_SIG_A_BW, status->bw) |
83 			 MU_PREP(FLAGS2_SIG_B_SYMS_USERS,
84 				 le32_get_bits(rxv[4], MT_CRXV_HE_NUM_USER));
85 
86 	he_mu->ru_ch1[0] = le32_get_bits(rxv[16], MT_CRXV_HE_RU0) & 0xff;
87 
88 	if (status->bw >= RATE_INFO_BW_40) {
89 		he_mu->flags1 |= HE_BITS(MU_FLAGS1_CH2_RU_KNOWN);
90 		he_mu->ru_ch2[0] = le32_get_bits(rxv[16], MT_CRXV_HE_RU1) & 0xff;
91 	}
92 
93 	if (status->bw >= RATE_INFO_BW_80) {
94 		u32 ru_h, ru_l;
95 
96 		he_mu->ru_ch1[1] = le32_get_bits(rxv[16], MT_CRXV_HE_RU2) & 0xff;
97 
98 		ru_l = le32_get_bits(rxv[16], MT_CRXV_HE_RU3_L);
99 		ru_h = le32_get_bits(rxv[17], MT_CRXV_HE_RU3_H) & 0x7;
100 		he_mu->ru_ch2[1] = (u8)(ru_l | ru_h << 4);
101 	}
102 }
103 
mt76_connac3_mac_decode_he_radiotap(struct sk_buff * skb,__le32 * rxv,u8 mode)104 void mt76_connac3_mac_decode_he_radiotap(struct sk_buff *skb, __le32 *rxv,
105 					 u8 mode)
106 {
107 	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
108 	static const struct ieee80211_radiotap_he known = {
109 		.data1 = HE_BITS(DATA1_DATA_MCS_KNOWN) |
110 			 HE_BITS(DATA1_DATA_DCM_KNOWN) |
111 			 HE_BITS(DATA1_STBC_KNOWN) |
112 			 HE_BITS(DATA1_CODING_KNOWN) |
113 			 HE_BITS(DATA1_LDPC_XSYMSEG_KNOWN) |
114 			 HE_BITS(DATA1_DOPPLER_KNOWN) |
115 			 HE_BITS(DATA1_SPTL_REUSE_KNOWN) |
116 			 HE_BITS(DATA1_BSS_COLOR_KNOWN),
117 		.data2 = HE_BITS(DATA2_GI_KNOWN) |
118 			 HE_BITS(DATA2_TXBF_KNOWN) |
119 			 HE_BITS(DATA2_PE_DISAMBIG_KNOWN) |
120 			 HE_BITS(DATA2_TXOP_KNOWN),
121 	};
122 	u32 ltf_size = le32_get_bits(rxv[4], MT_CRXV_HE_LTF_SIZE) + 1;
123 	struct ieee80211_radiotap_he *he;
124 
125 	status->flag |= RX_FLAG_RADIOTAP_HE;
126 
127 	he = skb_push(skb, sizeof(known));
128 	memcpy(he, &known, sizeof(known));
129 
130 	he->data3 = HE_PREP(DATA3_BSS_COLOR, BSS_COLOR, rxv[9]) |
131 		    HE_PREP(DATA3_LDPC_XSYMSEG, LDPC_EXT_SYM, rxv[4]);
132 	he->data4 = HE_PREP(DATA4_SU_MU_SPTL_REUSE, SR_MASK, rxv[13]);
133 	he->data5 = HE_PREP(DATA5_PE_DISAMBIG, PE_DISAMBIG, rxv[5]) |
134 		    le16_encode_bits(ltf_size,
135 				     IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE);
136 	if (le32_to_cpu(rxv[0]) & MT_PRXV_TXBF)
137 		he->data5 |= HE_BITS(DATA5_TXBF);
138 	he->data6 = HE_PREP(DATA6_TXOP, TXOP_DUR, rxv[9]) |
139 		    HE_PREP(DATA6_DOPPLER, DOPPLER, rxv[9]);
140 
141 	switch (mode) {
142 	case MT_PHY_TYPE_HE_SU:
143 		he->data1 |= HE_BITS(DATA1_FORMAT_SU) |
144 			     HE_BITS(DATA1_UL_DL_KNOWN) |
145 			     HE_BITS(DATA1_BEAM_CHANGE_KNOWN) |
146 			     HE_BITS(DATA1_BW_RU_ALLOC_KNOWN);
147 
148 		he->data3 |= HE_PREP(DATA3_BEAM_CHANGE, BEAM_CHNG, rxv[8]) |
149 			     HE_PREP(DATA3_UL_DL, UPLINK, rxv[5]);
150 		break;
151 	case MT_PHY_TYPE_HE_EXT_SU:
152 		he->data1 |= HE_BITS(DATA1_FORMAT_EXT_SU) |
153 			     HE_BITS(DATA1_UL_DL_KNOWN) |
154 			     HE_BITS(DATA1_BW_RU_ALLOC_KNOWN);
155 
156 		he->data3 |= HE_PREP(DATA3_UL_DL, UPLINK, rxv[5]);
157 		break;
158 	case MT_PHY_TYPE_HE_MU:
159 		he->data1 |= HE_BITS(DATA1_FORMAT_MU) |
160 			     HE_BITS(DATA1_UL_DL_KNOWN);
161 
162 		he->data3 |= HE_PREP(DATA3_UL_DL, UPLINK, rxv[5]);
163 		he->data4 |= HE_PREP(DATA4_MU_STA_ID, MU_AID, rxv[8]);
164 
165 		mt76_connac3_mac_decode_he_radiotap_ru(status, he, rxv);
166 		mt76_connac3_mac_decode_he_mu_radiotap(skb, rxv);
167 		break;
168 	case MT_PHY_TYPE_HE_TB:
169 		he->data1 |= HE_BITS(DATA1_FORMAT_TRIG) |
170 			     HE_BITS(DATA1_SPTL_REUSE2_KNOWN) |
171 			     HE_BITS(DATA1_SPTL_REUSE3_KNOWN) |
172 			     HE_BITS(DATA1_SPTL_REUSE4_KNOWN);
173 
174 		he->data4 |= HE_PREP(DATA4_TB_SPTL_REUSE1, SR_MASK, rxv[13]) |
175 			     HE_PREP(DATA4_TB_SPTL_REUSE2, SR1_MASK, rxv[13]) |
176 			     HE_PREP(DATA4_TB_SPTL_REUSE3, SR2_MASK, rxv[13]) |
177 			     HE_PREP(DATA4_TB_SPTL_REUSE4, SR3_MASK, rxv[13]);
178 
179 		mt76_connac3_mac_decode_he_radiotap_ru(status, he, rxv);
180 		break;
181 	default:
182 		break;
183 	}
184 }
185 EXPORT_SYMBOL_GPL(mt76_connac3_mac_decode_he_radiotap);
186 
187 static void *
mt76_connac3_mac_radiotap_push_tlv(struct sk_buff * skb,u16 type,u16 len)188 mt76_connac3_mac_radiotap_push_tlv(struct sk_buff *skb, u16 type, u16 len)
189 {
190 	struct ieee80211_radiotap_tlv *tlv;
191 
192 	tlv = skb_push(skb, sizeof(*tlv) + len);
193 	tlv->type = cpu_to_le16(type);
194 	tlv->len = cpu_to_le16(len);
195 	memset(tlv->data, 0, len);
196 
197 	return tlv->data;
198 }
199 
mt76_connac3_mac_decode_eht_radiotap(struct sk_buff * skb,__le32 * rxv,u8 mode)200 void mt76_connac3_mac_decode_eht_radiotap(struct sk_buff *skb, __le32 *rxv,
201 					  u8 mode)
202 {
203 	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
204 	struct ieee80211_radiotap_eht_usig *usig;
205 	struct ieee80211_radiotap_eht *eht;
206 	u32 ltf_size = le32_get_bits(rxv[4], MT_CRXV_HE_LTF_SIZE) + 1;
207 	u8 bw = FIELD_GET(MT_PRXV_FRAME_MODE, le32_to_cpu(rxv[2]));
208 
209 	if (WARN_ONCE(skb_mac_header(skb) != skb->data,
210 		      "Should push tlv at the top of mac hdr"))
211 		return;
212 
213 	eht = mt76_connac3_mac_radiotap_push_tlv(skb, IEEE80211_RADIOTAP_EHT,
214 						 sizeof(*eht) + sizeof(u32));
215 	usig = mt76_connac3_mac_radiotap_push_tlv(skb, IEEE80211_RADIOTAP_EHT_USIG,
216 						  sizeof(*usig));
217 
218 	status->flag |= RX_FLAG_RADIOTAP_TLV_AT_END;
219 
220 	eht->known |= EHT_BITS(KNOWN_SPATIAL_REUSE) |
221 		      EHT_BITS(KNOWN_GI) |
222 		      EHT_BITS(KNOWN_EHT_LTF) |
223 		      EHT_BITS(KNOWN_LDPC_EXTRA_SYM_OM) |
224 		      EHT_BITS(KNOWN_PE_DISAMBIGUITY_OM) |
225 		      EHT_BITS(KNOWN_NSS_S);
226 
227 	eht->data[0] |=
228 		EHT_PREP(DATA0_SPATIAL_REUSE, SR_MASK, rxv[13]) |
229 		cpu_to_le32(FIELD_PREP(IEEE80211_RADIOTAP_EHT_DATA0_GI, status->eht.gi) |
230 			    FIELD_PREP(IEEE80211_RADIOTAP_EHT_DATA0_LTF, ltf_size)) |
231 		EHT_PREP(DATA0_PE_DISAMBIGUITY_OM, PE_DISAMBIG, rxv[5]) |
232 		EHT_PREP(DATA0_LDPC_EXTRA_SYM_OM, LDPC_EXT_SYM, rxv[4]);
233 
234 	eht->data[7] |= le32_encode_bits(status->nss, IEEE80211_RADIOTAP_EHT_DATA7_NSS_S);
235 
236 	eht->user_info[0] |=
237 		EHT_BITS(USER_INFO_MCS_KNOWN) |
238 		EHT_BITS(USER_INFO_CODING_KNOWN) |
239 		EHT_BITS(USER_INFO_NSS_KNOWN_O) |
240 		EHT_BITS(USER_INFO_BEAMFORMING_KNOWN_O) |
241 		EHT_BITS(USER_INFO_DATA_FOR_USER) |
242 		le32_encode_bits(status->rate_idx, IEEE80211_RADIOTAP_EHT_USER_INFO_MCS) |
243 		le32_encode_bits(status->nss, IEEE80211_RADIOTAP_EHT_USER_INFO_NSS_O);
244 
245 	if (le32_to_cpu(rxv[0]) & MT_PRXV_TXBF)
246 		eht->user_info[0] |= EHT_BITS(USER_INFO_BEAMFORMING_O);
247 
248 	if (le32_to_cpu(rxv[0]) & MT_PRXV_HT_AD_CODE)
249 		eht->user_info[0] |= EHT_BITS(USER_INFO_CODING);
250 
251 	if (mode == MT_PHY_TYPE_EHT_MU)
252 		eht->user_info[0] |= EHT_BITS(USER_INFO_STA_ID_KNOWN) |
253 				     EHT_PREP(USER_INFO_STA_ID, MU_AID, rxv[8]);
254 
255 	usig->common |=
256 		EHT_BITS(USIG_COMMON_PHY_VER_KNOWN) |
257 		EHT_BITS(USIG_COMMON_BW_KNOWN) |
258 		EHT_BITS(USIG_COMMON_UL_DL_KNOWN) |
259 		EHT_BITS(USIG_COMMON_BSS_COLOR_KNOWN) |
260 		EHT_BITS(USIG_COMMON_TXOP_KNOWN) |
261 		le32_encode_bits(0, IEEE80211_RADIOTAP_EHT_USIG_COMMON_PHY_VER) |
262 		le32_encode_bits(bw, IEEE80211_RADIOTAP_EHT_USIG_COMMON_BW) |
263 		EHT_PREP(USIG_COMMON_UL_DL, UPLINK, rxv[5]) |
264 		EHT_PREP(USIG_COMMON_BSS_COLOR, BSS_COLOR, rxv[9]) |
265 		EHT_PREP(USIG_COMMON_TXOP, TXOP_DUR, rxv[9]);
266 }
267 EXPORT_SYMBOL_GPL(mt76_connac3_mac_decode_eht_radiotap);
268