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