xref: /linux/net/mac80211/he.c (revision 4ce06406958b67fdddcc2e6948237dd6ff6ba112)
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 	own_he_cap = *own_he_cap_ptr;
131 
132 	/* Make sure size is OK */
133 	mcs_nss_size = ieee80211_he_mcs_nss_size(he_cap_ie_elem);
134 	he_ppe_size =
135 		ieee80211_he_ppe_size(he_cap_ie[sizeof(he_cap->he_cap_elem) +
136 						mcs_nss_size],
137 				      he_cap_ie_elem->phy_cap_info);
138 	he_total_size = sizeof(he_cap->he_cap_elem) + mcs_nss_size +
139 			he_ppe_size;
140 	if (he_cap_len < he_total_size)
141 		return;
142 
143 	memcpy(&he_cap->he_cap_elem, he_cap_ie, sizeof(he_cap->he_cap_elem));
144 
145 	/* HE Tx/Rx HE MCS NSS Support Field */
146 	memcpy(&he_cap->he_mcs_nss_supp,
147 	       &he_cap_ie[sizeof(he_cap->he_cap_elem)], mcs_nss_size);
148 
149 	/* Check if there are (optional) PPE Thresholds */
150 	if (he_cap->he_cap_elem.phy_cap_info[6] &
151 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
152 		memcpy(he_cap->ppe_thres,
153 		       &he_cap_ie[sizeof(he_cap->he_cap_elem) + mcs_nss_size],
154 		       he_ppe_size);
155 
156 	he_cap->has_he = true;
157 
158 	link_sta->cur_max_bandwidth = ieee80211_sta_cap_rx_bw(link_sta);
159 	link_sta->pub->bandwidth = ieee80211_sta_cur_vht_bw(link_sta);
160 
161 	if (he_6ghz_capa)
162 		ieee80211_update_from_he_6ghz_capa(he_6ghz_capa, link_sta);
163 
164 	ieee80211_he_mcs_intersection(&own_he_cap.he_mcs_nss_supp.rx_mcs_80,
165 				      &he_cap->he_mcs_nss_supp.rx_mcs_80,
166 				      &own_he_cap.he_mcs_nss_supp.tx_mcs_80,
167 				      &he_cap->he_mcs_nss_supp.tx_mcs_80);
168 
169 	own_160 = own_he_cap.he_cap_elem.phy_cap_info[0] &
170 		  IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
171 	peer_160 = he_cap->he_cap_elem.phy_cap_info[0] &
172 		   IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
173 
174 	if (peer_160 && own_160) {
175 		ieee80211_he_mcs_intersection(&own_he_cap.he_mcs_nss_supp.rx_mcs_160,
176 					      &he_cap->he_mcs_nss_supp.rx_mcs_160,
177 					      &own_he_cap.he_mcs_nss_supp.tx_mcs_160,
178 					      &he_cap->he_mcs_nss_supp.tx_mcs_160);
179 	} else if (peer_160 && !own_160) {
180 		ieee80211_he_mcs_disable(&he_cap->he_mcs_nss_supp.rx_mcs_160);
181 		ieee80211_he_mcs_disable(&he_cap->he_mcs_nss_supp.tx_mcs_160);
182 		he_cap->he_cap_elem.phy_cap_info[0] &=
183 			~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
184 	}
185 
186 	own_80p80 = own_he_cap.he_cap_elem.phy_cap_info[0] &
187 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
188 	peer_80p80 = he_cap->he_cap_elem.phy_cap_info[0] &
189 		     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
190 
191 	if (peer_80p80 && own_80p80) {
192 		ieee80211_he_mcs_intersection(&own_he_cap.he_mcs_nss_supp.rx_mcs_80p80,
193 					      &he_cap->he_mcs_nss_supp.rx_mcs_80p80,
194 					      &own_he_cap.he_mcs_nss_supp.tx_mcs_80p80,
195 					      &he_cap->he_mcs_nss_supp.tx_mcs_80p80);
196 	} else if (peer_80p80 && !own_80p80) {
197 		ieee80211_he_mcs_disable(&he_cap->he_mcs_nss_supp.rx_mcs_80p80);
198 		ieee80211_he_mcs_disable(&he_cap->he_mcs_nss_supp.tx_mcs_80p80);
199 		he_cap->he_cap_elem.phy_cap_info[0] &=
200 			~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
201 	}
202 }
203 
204 void
205 ieee80211_he_cap_ie_to_sta_he_cap(struct ieee80211_sub_if_data *sdata,
206 				  struct ieee80211_supported_band *sband,
207 				  const u8 *he_cap_ie, u8 he_cap_len,
208 				  const struct ieee80211_he_6ghz_capa *he_6ghz_capa,
209 				  struct link_sta_info *link_sta)
210 {
211 	const struct ieee80211_sta_he_cap *own_he_cap =
212 		ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif);
213 
214 	_ieee80211_he_cap_ie_to_sta_he_cap(sdata, own_he_cap, he_cap_ie,
215 					   he_cap_len,
216 					   (sband->band == NL80211_BAND_6GHZ) ?
217 						he_6ghz_capa : NULL,
218 					   link_sta);
219 }
220 
221 void
222 ieee80211_he_op_ie_to_bss_conf(struct ieee80211_vif *vif,
223 			const struct ieee80211_he_operation *he_op_ie)
224 {
225 	memset(&vif->bss_conf.he_oper, 0, sizeof(vif->bss_conf.he_oper));
226 	if (!he_op_ie)
227 		return;
228 
229 	vif->bss_conf.he_oper.params = __le32_to_cpu(he_op_ie->he_oper_params);
230 	vif->bss_conf.he_oper.nss_set = __le16_to_cpu(he_op_ie->he_mcs_nss_set);
231 }
232 
233 void
234 ieee80211_he_spr_ie_to_bss_conf(struct ieee80211_vif *vif,
235 				const struct ieee80211_he_spr *he_spr_ie_elem)
236 {
237 	struct ieee80211_he_obss_pd *he_obss_pd =
238 					&vif->bss_conf.he_obss_pd;
239 	const u8 *data;
240 
241 	memset(he_obss_pd, 0, sizeof(*he_obss_pd));
242 
243 	if (!he_spr_ie_elem)
244 		return;
245 
246 	he_obss_pd->sr_ctrl = he_spr_ie_elem->he_sr_control;
247 	data = he_spr_ie_elem->optional;
248 
249 	if (he_spr_ie_elem->he_sr_control &
250 	    IEEE80211_HE_SPR_NON_SRG_OFFSET_PRESENT)
251 		he_obss_pd->non_srg_max_offset = *data++;
252 
253 	if (he_spr_ie_elem->he_sr_control &
254 	    IEEE80211_HE_SPR_SRG_INFORMATION_PRESENT) {
255 		he_obss_pd->min_offset = *data++;
256 		he_obss_pd->max_offset = *data++;
257 		memcpy(he_obss_pd->bss_color_bitmap, data, 8);
258 		data += 8;
259 		memcpy(he_obss_pd->partial_bssid_bitmap, data, 8);
260 		he_obss_pd->enable = true;
261 	}
262 }
263 
264 static void ieee80211_link_sta_rc_update_omi(struct ieee80211_link_data *link,
265 					     struct link_sta_info *link_sta)
266 {
267 	struct ieee80211_sub_if_data *sdata = link->sdata;
268 	struct ieee80211_supported_band *sband;
269 	enum ieee80211_sta_rx_bandwidth new_bw;
270 	enum nl80211_band band;
271 
272 	band = link->conf->chanreq.oper.chan->band;
273 	sband = sdata->local->hw.wiphy->bands[band];
274 
275 	new_bw = ieee80211_sta_cur_vht_bw(link_sta);
276 	if (link_sta->pub->bandwidth == new_bw)
277 		return;
278 
279 	link_sta->pub->bandwidth = new_bw;
280 	rate_control_rate_update(sdata->local, sband, link_sta,
281 				 IEEE80211_RC_BW_CHANGED);
282 }
283 
284 bool ieee80211_prepare_rx_omi_bw(struct ieee80211_link_sta *pub_link_sta,
285 				 enum ieee80211_sta_rx_bandwidth bw)
286 {
287 	struct sta_info *sta = container_of(pub_link_sta->sta,
288 					    struct sta_info, sta);
289 	struct ieee80211_local *local = sta->sdata->local;
290 	struct link_sta_info *link_sta =
291 		sdata_dereference(sta->link[pub_link_sta->link_id], sta->sdata);
292 	struct ieee80211_link_data *link =
293 		sdata_dereference(sta->sdata->link[pub_link_sta->link_id],
294 				  sta->sdata);
295 	struct ieee80211_chanctx_conf *conf;
296 	struct ieee80211_chanctx *chanctx;
297 	bool ret;
298 
299 	if (WARN_ON(!link || !link_sta || link_sta->pub != pub_link_sta))
300 		return false;
301 
302 	conf = sdata_dereference(link->conf->chanctx_conf, sta->sdata);
303 	if (WARN_ON(!conf))
304 		return false;
305 
306 	trace_api_prepare_rx_omi_bw(local, sta->sdata, link_sta, bw);
307 
308 	chanctx = container_of(conf, typeof(*chanctx), conf);
309 
310 	if (link_sta->rx_omi_bw_staging == bw) {
311 		ret = false;
312 		goto trace;
313 	}
314 
315 	/* must call this API in pairs */
316 	if (WARN_ON(link_sta->rx_omi_bw_tx != link_sta->rx_omi_bw_staging ||
317 		    link_sta->rx_omi_bw_rx != link_sta->rx_omi_bw_staging)) {
318 		ret = false;
319 		goto trace;
320 	}
321 
322 	if (bw < link_sta->rx_omi_bw_staging) {
323 		link_sta->rx_omi_bw_tx = bw;
324 		ieee80211_link_sta_rc_update_omi(link, link_sta);
325 	} else {
326 		link_sta->rx_omi_bw_rx = bw;
327 		ieee80211_recalc_chanctx_min_def(local, chanctx);
328 	}
329 
330 	link_sta->rx_omi_bw_staging = bw;
331 	ret = true;
332 trace:
333 	trace_api_return_bool(local, ret);
334 	return ret;
335 }
336 EXPORT_SYMBOL_GPL(ieee80211_prepare_rx_omi_bw);
337 
338 void ieee80211_finalize_rx_omi_bw(struct ieee80211_link_sta *pub_link_sta)
339 {
340 	struct sta_info *sta = container_of(pub_link_sta->sta,
341 					    struct sta_info, sta);
342 	struct ieee80211_local *local = sta->sdata->local;
343 	struct link_sta_info *link_sta =
344 		sdata_dereference(sta->link[pub_link_sta->link_id], sta->sdata);
345 	struct ieee80211_link_data *link =
346 		sdata_dereference(sta->sdata->link[pub_link_sta->link_id],
347 				  sta->sdata);
348 	struct ieee80211_chanctx_conf *conf;
349 	struct ieee80211_chanctx *chanctx;
350 
351 	if (WARN_ON(!link || !link_sta || link_sta->pub != pub_link_sta))
352 		return;
353 
354 	conf = sdata_dereference(link->conf->chanctx_conf, sta->sdata);
355 	if (WARN_ON(!conf))
356 		return;
357 
358 	trace_api_finalize_rx_omi_bw(local, sta->sdata, link_sta);
359 
360 	chanctx = container_of(conf, typeof(*chanctx), conf);
361 
362 	if (link_sta->rx_omi_bw_tx != link_sta->rx_omi_bw_staging) {
363 		/* rate control in finalize only when widening bandwidth */
364 		WARN_ON(link_sta->rx_omi_bw_tx > link_sta->rx_omi_bw_staging);
365 		link_sta->rx_omi_bw_tx = link_sta->rx_omi_bw_staging;
366 		ieee80211_link_sta_rc_update_omi(link, link_sta);
367 	}
368 
369 	if (link_sta->rx_omi_bw_rx != link_sta->rx_omi_bw_staging) {
370 		/* channel context in finalize only when narrowing bandwidth */
371 		WARN_ON(link_sta->rx_omi_bw_rx < link_sta->rx_omi_bw_staging);
372 		link_sta->rx_omi_bw_rx = link_sta->rx_omi_bw_staging;
373 		ieee80211_recalc_chanctx_min_def(local, chanctx);
374 	}
375 
376 	trace_api_return_void(local);
377 }
378 EXPORT_SYMBOL_GPL(ieee80211_finalize_rx_omi_bw);
379