xref: /linux/net/mac80211/he.c (revision 5ea5880764cbb164afb17a62e76ca75dc371409d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * HE handling
4  *
5  * Copyright(c) 2017 Intel Deutschland GmbH
6  * Copyright(c) 2019-2025 Intel Corporation
7  */
8 
9 #include "ieee80211_i.h"
10 #include "rate.h"
11 
12 static void
13 ieee80211_update_from_he_6ghz_capa(const struct ieee80211_he_6ghz_capa *he_6ghz_capa,
14 				   struct link_sta_info *link_sta)
15 {
16 	struct sta_info *sta = link_sta->sta;
17 	enum ieee80211_smps_mode smps_mode;
18 
19 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
20 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
21 		switch (le16_get_bits(he_6ghz_capa->capa,
22 				      IEEE80211_HE_6GHZ_CAP_SM_PS)) {
23 		case WLAN_HT_CAP_SM_PS_INVALID:
24 		case WLAN_HT_CAP_SM_PS_STATIC:
25 			smps_mode = IEEE80211_SMPS_STATIC;
26 			break;
27 		case WLAN_HT_CAP_SM_PS_DYNAMIC:
28 			smps_mode = IEEE80211_SMPS_DYNAMIC;
29 			break;
30 		case WLAN_HT_CAP_SM_PS_DISABLED:
31 			smps_mode = IEEE80211_SMPS_OFF;
32 			break;
33 		}
34 
35 		link_sta->pub->smps_mode = smps_mode;
36 	} else {
37 		link_sta->pub->smps_mode = IEEE80211_SMPS_OFF;
38 	}
39 
40 	switch (le16_get_bits(he_6ghz_capa->capa,
41 			      IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN)) {
42 	case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454:
43 		link_sta->pub->agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_11454;
44 		break;
45 	case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991:
46 		link_sta->pub->agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_7991;
47 		break;
48 	case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_3895:
49 	default:
50 		link_sta->pub->agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_3895;
51 		break;
52 	}
53 
54 	ieee80211_sta_recalc_aggregates(&sta->sta);
55 
56 	link_sta->pub->he_6ghz_capa = *he_6ghz_capa;
57 }
58 
59 static void ieee80211_he_mcs_disable(__le16 *he_mcs)
60 {
61 	u32 i;
62 
63 	for (i = 0; i < 8; i++)
64 		*he_mcs |= cpu_to_le16(IEEE80211_HE_MCS_NOT_SUPPORTED << i * 2);
65 }
66 
67 static void ieee80211_he_mcs_intersection(__le16 *he_own_rx, __le16 *he_peer_rx,
68 					  __le16 *he_own_tx, __le16 *he_peer_tx)
69 {
70 	u32 i;
71 	u16 own_rx, own_tx, peer_rx, peer_tx;
72 
73 	for (i = 0; i < 8; i++) {
74 		own_rx = le16_to_cpu(*he_own_rx);
75 		own_rx = (own_rx >> i * 2) & IEEE80211_HE_MCS_NOT_SUPPORTED;
76 
77 		own_tx = le16_to_cpu(*he_own_tx);
78 		own_tx = (own_tx >> i * 2) & IEEE80211_HE_MCS_NOT_SUPPORTED;
79 
80 		peer_rx = le16_to_cpu(*he_peer_rx);
81 		peer_rx = (peer_rx >> i * 2) & IEEE80211_HE_MCS_NOT_SUPPORTED;
82 
83 		peer_tx = le16_to_cpu(*he_peer_tx);
84 		peer_tx = (peer_tx >> i * 2) & IEEE80211_HE_MCS_NOT_SUPPORTED;
85 
86 		if (peer_tx != IEEE80211_HE_MCS_NOT_SUPPORTED) {
87 			if (own_rx == IEEE80211_HE_MCS_NOT_SUPPORTED)
88 				peer_tx = IEEE80211_HE_MCS_NOT_SUPPORTED;
89 			else if (own_rx < peer_tx)
90 				peer_tx = own_rx;
91 		}
92 
93 		if (peer_rx != IEEE80211_HE_MCS_NOT_SUPPORTED) {
94 			if (own_tx == IEEE80211_HE_MCS_NOT_SUPPORTED)
95 				peer_rx = IEEE80211_HE_MCS_NOT_SUPPORTED;
96 			else if (own_tx < peer_rx)
97 				peer_rx = own_tx;
98 		}
99 
100 		*he_peer_rx &=
101 			~cpu_to_le16(IEEE80211_HE_MCS_NOT_SUPPORTED << i * 2);
102 		*he_peer_rx |= cpu_to_le16(peer_rx << i * 2);
103 
104 		*he_peer_tx &=
105 			~cpu_to_le16(IEEE80211_HE_MCS_NOT_SUPPORTED << i * 2);
106 		*he_peer_tx |= cpu_to_le16(peer_tx << i * 2);
107 	}
108 }
109 
110 void
111 _ieee80211_he_cap_ie_to_sta_he_cap(struct ieee80211_sub_if_data *sdata,
112 				   const struct ieee80211_sta_he_cap *own_he_cap_ptr,
113 				   const u8 *he_cap_ie, u8 he_cap_len,
114 				   const struct ieee80211_he_6ghz_capa *he_6ghz_capa,
115 				   struct link_sta_info *link_sta)
116 {
117 	struct ieee80211_sta_he_cap *he_cap = &link_sta->pub->he_cap;
118 	struct ieee80211_sta_he_cap own_he_cap;
119 	struct ieee80211_he_cap_elem *he_cap_ie_elem = (void *)he_cap_ie;
120 	u8 he_ppe_size;
121 	u8 mcs_nss_size;
122 	u8 he_total_size;
123 	bool own_160, peer_160, own_80p80, peer_80p80;
124 
125 	memset(he_cap, 0, sizeof(*he_cap));
126 
127 	if (!he_cap_ie || !own_he_cap_ptr || !own_he_cap_ptr->has_he)
128 		return;
129 
130 	/* NDI station are using the capabilities from the NMI station */
131 	if (WARN_ON_ONCE(sdata->vif.type == NL80211_IFTYPE_NAN_DATA))
132 		return;
133 
134 	own_he_cap = *own_he_cap_ptr;
135 
136 	/* Make sure size is OK */
137 	mcs_nss_size = ieee80211_he_mcs_nss_size(he_cap_ie_elem);
138 	he_ppe_size =
139 		ieee80211_he_ppe_size(he_cap_ie[sizeof(he_cap->he_cap_elem) +
140 						mcs_nss_size],
141 				      he_cap_ie_elem->phy_cap_info);
142 	he_total_size = sizeof(he_cap->he_cap_elem) + mcs_nss_size +
143 			he_ppe_size;
144 	if (he_cap_len < he_total_size)
145 		return;
146 
147 	memcpy(&he_cap->he_cap_elem, he_cap_ie, sizeof(he_cap->he_cap_elem));
148 
149 	/* HE Tx/Rx HE MCS NSS Support Field */
150 	memcpy(&he_cap->he_mcs_nss_supp,
151 	       &he_cap_ie[sizeof(he_cap->he_cap_elem)], mcs_nss_size);
152 
153 	/* Check if there are (optional) PPE Thresholds */
154 	if (he_cap->he_cap_elem.phy_cap_info[6] &
155 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
156 		memcpy(he_cap->ppe_thres,
157 		       &he_cap_ie[sizeof(he_cap->he_cap_elem) + mcs_nss_size],
158 		       he_ppe_size);
159 
160 	he_cap->has_he = true;
161 
162 	link_sta->cur_max_bandwidth = ieee80211_sta_cap_rx_bw(link_sta);
163 	if (sdata->vif.type != NL80211_IFTYPE_NAN)
164 		link_sta->pub->bandwidth = ieee80211_sta_cur_vht_bw(link_sta);
165 
166 	if (he_6ghz_capa)
167 		ieee80211_update_from_he_6ghz_capa(he_6ghz_capa, link_sta);
168 
169 	ieee80211_he_mcs_intersection(&own_he_cap.he_mcs_nss_supp.rx_mcs_80,
170 				      &he_cap->he_mcs_nss_supp.rx_mcs_80,
171 				      &own_he_cap.he_mcs_nss_supp.tx_mcs_80,
172 				      &he_cap->he_mcs_nss_supp.tx_mcs_80);
173 
174 	own_160 = own_he_cap.he_cap_elem.phy_cap_info[0] &
175 		  IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
176 	peer_160 = he_cap->he_cap_elem.phy_cap_info[0] &
177 		   IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
178 
179 	if (peer_160 && own_160) {
180 		ieee80211_he_mcs_intersection(&own_he_cap.he_mcs_nss_supp.rx_mcs_160,
181 					      &he_cap->he_mcs_nss_supp.rx_mcs_160,
182 					      &own_he_cap.he_mcs_nss_supp.tx_mcs_160,
183 					      &he_cap->he_mcs_nss_supp.tx_mcs_160);
184 	} else if (peer_160 && !own_160) {
185 		ieee80211_he_mcs_disable(&he_cap->he_mcs_nss_supp.rx_mcs_160);
186 		ieee80211_he_mcs_disable(&he_cap->he_mcs_nss_supp.tx_mcs_160);
187 		he_cap->he_cap_elem.phy_cap_info[0] &=
188 			~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
189 	}
190 
191 	own_80p80 = own_he_cap.he_cap_elem.phy_cap_info[0] &
192 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
193 	peer_80p80 = he_cap->he_cap_elem.phy_cap_info[0] &
194 		     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
195 
196 	if (peer_80p80 && own_80p80) {
197 		ieee80211_he_mcs_intersection(&own_he_cap.he_mcs_nss_supp.rx_mcs_80p80,
198 					      &he_cap->he_mcs_nss_supp.rx_mcs_80p80,
199 					      &own_he_cap.he_mcs_nss_supp.tx_mcs_80p80,
200 					      &he_cap->he_mcs_nss_supp.tx_mcs_80p80);
201 	} else if (peer_80p80 && !own_80p80) {
202 		ieee80211_he_mcs_disable(&he_cap->he_mcs_nss_supp.rx_mcs_80p80);
203 		ieee80211_he_mcs_disable(&he_cap->he_mcs_nss_supp.tx_mcs_80p80);
204 		he_cap->he_cap_elem.phy_cap_info[0] &=
205 			~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
206 	}
207 }
208 
209 void
210 ieee80211_he_cap_ie_to_sta_he_cap(struct ieee80211_sub_if_data *sdata,
211 				  struct ieee80211_supported_band *sband,
212 				  const u8 *he_cap_ie, u8 he_cap_len,
213 				  const struct ieee80211_he_6ghz_capa *he_6ghz_capa,
214 				  struct link_sta_info *link_sta)
215 {
216 	const struct ieee80211_sta_he_cap *own_he_cap =
217 		ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif);
218 
219 	_ieee80211_he_cap_ie_to_sta_he_cap(sdata, own_he_cap, he_cap_ie,
220 					   he_cap_len,
221 					   (sband->band == NL80211_BAND_6GHZ) ?
222 						he_6ghz_capa : NULL,
223 					   link_sta);
224 }
225 
226 void
227 ieee80211_he_op_ie_to_bss_conf(struct ieee80211_vif *vif,
228 			const struct ieee80211_he_operation *he_op_ie)
229 {
230 	memset(&vif->bss_conf.he_oper, 0, sizeof(vif->bss_conf.he_oper));
231 	if (!he_op_ie)
232 		return;
233 
234 	vif->bss_conf.he_oper.params = __le32_to_cpu(he_op_ie->he_oper_params);
235 	vif->bss_conf.he_oper.nss_set = __le16_to_cpu(he_op_ie->he_mcs_nss_set);
236 }
237 
238 void
239 ieee80211_he_spr_ie_to_bss_conf(struct ieee80211_vif *vif,
240 				const struct ieee80211_he_spr *he_spr_ie_elem)
241 {
242 	struct ieee80211_he_obss_pd *he_obss_pd =
243 					&vif->bss_conf.he_obss_pd;
244 	const u8 *data;
245 
246 	memset(he_obss_pd, 0, sizeof(*he_obss_pd));
247 
248 	if (!he_spr_ie_elem)
249 		return;
250 
251 	he_obss_pd->sr_ctrl = he_spr_ie_elem->he_sr_control;
252 	data = he_spr_ie_elem->optional;
253 
254 	if (he_spr_ie_elem->he_sr_control &
255 	    IEEE80211_HE_SPR_NON_SRG_OFFSET_PRESENT)
256 		he_obss_pd->non_srg_max_offset = *data++;
257 
258 	if (he_spr_ie_elem->he_sr_control &
259 	    IEEE80211_HE_SPR_SRG_INFORMATION_PRESENT) {
260 		he_obss_pd->min_offset = *data++;
261 		he_obss_pd->max_offset = *data++;
262 		memcpy(he_obss_pd->bss_color_bitmap, data, 8);
263 		data += 8;
264 		memcpy(he_obss_pd->partial_bssid_bitmap, data, 8);
265 		he_obss_pd->enable = true;
266 	}
267 }
268 
269 static void ieee80211_link_sta_rc_update_omi(struct ieee80211_link_data *link,
270 					     struct link_sta_info *link_sta)
271 {
272 	struct ieee80211_sub_if_data *sdata = link->sdata;
273 	struct ieee80211_supported_band *sband;
274 	enum ieee80211_sta_rx_bandwidth new_bw;
275 	enum nl80211_band band;
276 
277 	band = link->conf->chanreq.oper.chan->band;
278 	sband = sdata->local->hw.wiphy->bands[band];
279 
280 	new_bw = ieee80211_sta_cur_vht_bw(link_sta);
281 	if (link_sta->pub->bandwidth == new_bw)
282 		return;
283 
284 	link_sta->pub->bandwidth = new_bw;
285 	rate_control_rate_update(sdata->local, sband, link_sta,
286 				 IEEE80211_RC_BW_CHANGED);
287 }
288 
289 bool ieee80211_prepare_rx_omi_bw(struct ieee80211_link_sta *pub_link_sta,
290 				 enum ieee80211_sta_rx_bandwidth bw)
291 {
292 	struct sta_info *sta = container_of(pub_link_sta->sta,
293 					    struct sta_info, sta);
294 	struct ieee80211_local *local = sta->sdata->local;
295 	struct link_sta_info *link_sta =
296 		sdata_dereference(sta->link[pub_link_sta->link_id], sta->sdata);
297 	struct ieee80211_link_data *link =
298 		sdata_dereference(sta->sdata->link[pub_link_sta->link_id],
299 				  sta->sdata);
300 	struct ieee80211_chanctx_conf *conf;
301 	struct ieee80211_chanctx *chanctx;
302 	bool ret;
303 
304 	if (WARN_ON(!link || !link_sta || link_sta->pub != pub_link_sta))
305 		return false;
306 
307 	conf = sdata_dereference(link->conf->chanctx_conf, sta->sdata);
308 	if (WARN_ON(!conf))
309 		return false;
310 
311 	trace_api_prepare_rx_omi_bw(local, sta->sdata, link_sta, bw);
312 
313 	chanctx = container_of(conf, typeof(*chanctx), conf);
314 
315 	if (link_sta->rx_omi_bw_staging == bw) {
316 		ret = false;
317 		goto trace;
318 	}
319 
320 	/* must call this API in pairs */
321 	if (WARN_ON(link_sta->rx_omi_bw_tx != link_sta->rx_omi_bw_staging ||
322 		    link_sta->rx_omi_bw_rx != link_sta->rx_omi_bw_staging)) {
323 		ret = false;
324 		goto trace;
325 	}
326 
327 	if (bw < link_sta->rx_omi_bw_staging) {
328 		link_sta->rx_omi_bw_tx = bw;
329 		ieee80211_link_sta_rc_update_omi(link, link_sta);
330 	} else {
331 		link_sta->rx_omi_bw_rx = bw;
332 		ieee80211_recalc_chanctx_min_def(local, chanctx);
333 	}
334 
335 	link_sta->rx_omi_bw_staging = bw;
336 	ret = true;
337 trace:
338 	trace_api_return_bool(local, ret);
339 	return ret;
340 }
341 EXPORT_SYMBOL_GPL(ieee80211_prepare_rx_omi_bw);
342 
343 void ieee80211_finalize_rx_omi_bw(struct ieee80211_link_sta *pub_link_sta)
344 {
345 	struct sta_info *sta = container_of(pub_link_sta->sta,
346 					    struct sta_info, sta);
347 	struct ieee80211_local *local = sta->sdata->local;
348 	struct link_sta_info *link_sta =
349 		sdata_dereference(sta->link[pub_link_sta->link_id], sta->sdata);
350 	struct ieee80211_link_data *link =
351 		sdata_dereference(sta->sdata->link[pub_link_sta->link_id],
352 				  sta->sdata);
353 	struct ieee80211_chanctx_conf *conf;
354 	struct ieee80211_chanctx *chanctx;
355 
356 	if (WARN_ON(!link || !link_sta || link_sta->pub != pub_link_sta))
357 		return;
358 
359 	conf = sdata_dereference(link->conf->chanctx_conf, sta->sdata);
360 	if (WARN_ON(!conf))
361 		return;
362 
363 	trace_api_finalize_rx_omi_bw(local, sta->sdata, link_sta);
364 
365 	chanctx = container_of(conf, typeof(*chanctx), conf);
366 
367 	if (link_sta->rx_omi_bw_tx != link_sta->rx_omi_bw_staging) {
368 		/* rate control in finalize only when widening bandwidth */
369 		WARN_ON(link_sta->rx_omi_bw_tx > link_sta->rx_omi_bw_staging);
370 		link_sta->rx_omi_bw_tx = link_sta->rx_omi_bw_staging;
371 		ieee80211_link_sta_rc_update_omi(link, link_sta);
372 	}
373 
374 	if (link_sta->rx_omi_bw_rx != link_sta->rx_omi_bw_staging) {
375 		/* channel context in finalize only when narrowing bandwidth */
376 		WARN_ON(link_sta->rx_omi_bw_rx < link_sta->rx_omi_bw_staging);
377 		link_sta->rx_omi_bw_rx = link_sta->rx_omi_bw_staging;
378 		ieee80211_recalc_chanctx_min_def(local, chanctx);
379 	}
380 
381 	trace_api_return_void(local);
382 }
383 EXPORT_SYMBOL_GPL(ieee80211_finalize_rx_omi_bw);
384