xref: /linux/drivers/net/wireless/intel/iwlwifi/mld/mlo.c (revision ccde82e909467abdf098a8ee6f63e1ecf9a47ce5)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2024-2025 Intel Corporation
4  */
5 #include "mlo.h"
6 #include "phy.h"
7 
8 /* Block reasons helper */
9 #define HANDLE_EMLSR_BLOCKED_REASONS(HOW)	\
10 	HOW(PREVENTION)			\
11 	HOW(WOWLAN)			\
12 	HOW(ROC)			\
13 	HOW(NON_BSS)			\
14 	HOW(TMP_NON_BSS)		\
15 	HOW(TPT)
16 
17 static const char *
18 iwl_mld_get_emlsr_blocked_string(enum iwl_mld_emlsr_blocked blocked)
19 {
20 	/* Using switch without "default" will warn about missing entries  */
21 	switch (blocked) {
22 #define REASON_CASE(x) case IWL_MLD_EMLSR_BLOCKED_##x: return #x;
23 	HANDLE_EMLSR_BLOCKED_REASONS(REASON_CASE)
24 #undef REASON_CASE
25 	}
26 
27 	return "ERROR";
28 }
29 
30 static void iwl_mld_print_emlsr_blocked(struct iwl_mld *mld, u32 mask)
31 {
32 #define NAME_FMT(x) "%s"
33 #define NAME_PR(x) (mask & IWL_MLD_EMLSR_BLOCKED_##x) ? "[" #x "]" : "",
34 	IWL_DEBUG_INFO(mld,
35 		       "EMLSR blocked = " HANDLE_EMLSR_BLOCKED_REASONS(NAME_FMT)
36 		       " (0x%x)\n",
37 		       HANDLE_EMLSR_BLOCKED_REASONS(NAME_PR)
38 		       mask);
39 #undef NAME_FMT
40 #undef NAME_PR
41 }
42 
43 /* Exit reasons helper */
44 #define HANDLE_EMLSR_EXIT_REASONS(HOW)	\
45 	HOW(BLOCK)			\
46 	HOW(MISSED_BEACON)		\
47 	HOW(FAIL_ENTRY)			\
48 	HOW(CSA)			\
49 	HOW(EQUAL_BAND)			\
50 	HOW(LOW_RSSI)			\
51 	HOW(LINK_USAGE)			\
52 	HOW(BT_COEX)			\
53 	HOW(CHAN_LOAD)			\
54 	HOW(RFI)			\
55 	HOW(FW_REQUEST)			\
56 	HOW(INVALID)
57 
58 static const char *
59 iwl_mld_get_emlsr_exit_string(enum iwl_mld_emlsr_exit exit)
60 {
61 	/* Using switch without "default" will warn about missing entries  */
62 	switch (exit) {
63 #define REASON_CASE(x) case IWL_MLD_EMLSR_EXIT_##x: return #x;
64 	HANDLE_EMLSR_EXIT_REASONS(REASON_CASE)
65 #undef REASON_CASE
66 	}
67 
68 	return "ERROR";
69 }
70 
71 static void iwl_mld_print_emlsr_exit(struct iwl_mld *mld, u32 mask)
72 {
73 #define NAME_FMT(x) "%s"
74 #define NAME_PR(x) (mask & IWL_MLD_EMLSR_EXIT_##x) ? "[" #x "]" : "",
75 	IWL_DEBUG_INFO(mld,
76 		       "EMLSR exit = " HANDLE_EMLSR_EXIT_REASONS(NAME_FMT)
77 		       " (0x%x)\n",
78 		       HANDLE_EMLSR_EXIT_REASONS(NAME_PR)
79 		       mask);
80 #undef NAME_FMT
81 #undef NAME_PR
82 }
83 
84 void iwl_mld_emlsr_prevent_done_wk(struct wiphy *wiphy, struct wiphy_work *wk)
85 {
86 	struct iwl_mld_vif *mld_vif = container_of(wk, struct iwl_mld_vif,
87 						   emlsr.prevent_done_wk.work);
88 	struct ieee80211_vif *vif =
89 		container_of((void *)mld_vif, struct ieee80211_vif, drv_priv);
90 
91 	if (WARN_ON(!(mld_vif->emlsr.blocked_reasons &
92 		      IWL_MLD_EMLSR_BLOCKED_PREVENTION)))
93 		return;
94 
95 	iwl_mld_unblock_emlsr(mld_vif->mld, vif,
96 			      IWL_MLD_EMLSR_BLOCKED_PREVENTION);
97 }
98 
99 void iwl_mld_emlsr_tmp_non_bss_done_wk(struct wiphy *wiphy,
100 				       struct wiphy_work *wk)
101 {
102 	struct iwl_mld_vif *mld_vif = container_of(wk, struct iwl_mld_vif,
103 						   emlsr.tmp_non_bss_done_wk.work);
104 	struct ieee80211_vif *vif =
105 		container_of((void *)mld_vif, struct ieee80211_vif, drv_priv);
106 
107 	if (WARN_ON(!(mld_vif->emlsr.blocked_reasons &
108 		      IWL_MLD_EMLSR_BLOCKED_TMP_NON_BSS)))
109 		return;
110 
111 	iwl_mld_unblock_emlsr(mld_vif->mld, vif,
112 			      IWL_MLD_EMLSR_BLOCKED_TMP_NON_BSS);
113 }
114 
115 #define IWL_MLD_TRIGGER_LINK_SEL_TIME	(HZ * IWL_MLD_TRIGGER_LINK_SEL_TIME_SEC)
116 #define IWL_MLD_SCAN_EXPIRE_TIME	(HZ * IWL_MLD_SCAN_EXPIRE_TIME_SEC)
117 
118 /* Exit reasons that can cause longer EMLSR prevention */
119 #define IWL_MLD_PREVENT_EMLSR_REASONS	(IWL_MLD_EMLSR_EXIT_MISSED_BEACON	| \
120 					 IWL_MLD_EMLSR_EXIT_LINK_USAGE		| \
121 					 IWL_MLD_EMLSR_EXIT_FW_REQUEST)
122 #define IWL_MLD_PREVENT_EMLSR_TIMEOUT	(HZ * 400)
123 
124 #define IWL_MLD_EMLSR_PREVENT_SHORT	(HZ * 300)
125 #define IWL_MLD_EMLSR_PREVENT_LONG	(HZ * 600)
126 
127 static void iwl_mld_check_emlsr_prevention(struct iwl_mld *mld,
128 					   struct iwl_mld_vif *mld_vif,
129 					   enum iwl_mld_emlsr_exit reason)
130 {
131 	unsigned long delay;
132 
133 	/*
134 	 * Reset the counter if more than 400 seconds have passed between one
135 	 * exit and the other, or if we exited due to a different reason.
136 	 * Will also reset the counter after the long prevention is done.
137 	 */
138 	if (time_after(jiffies, mld_vif->emlsr.last_exit_ts +
139 				IWL_MLD_PREVENT_EMLSR_TIMEOUT) ||
140 	    mld_vif->emlsr.last_exit_reason != reason)
141 		mld_vif->emlsr.exit_repeat_count = 0;
142 
143 	mld_vif->emlsr.last_exit_reason = reason;
144 	mld_vif->emlsr.last_exit_ts = jiffies;
145 	mld_vif->emlsr.exit_repeat_count++;
146 
147 	/*
148 	 * Do not add a prevention when the reason was a block. For a block,
149 	 * EMLSR will be enabled again on unblock.
150 	 */
151 	if (reason == IWL_MLD_EMLSR_EXIT_BLOCK)
152 		return;
153 
154 	/* Set prevention for a minimum of 30 seconds */
155 	mld_vif->emlsr.blocked_reasons |= IWL_MLD_EMLSR_BLOCKED_PREVENTION;
156 	delay = IWL_MLD_TRIGGER_LINK_SEL_TIME;
157 
158 	/* Handle repeats for reasons that can cause long prevention */
159 	if (mld_vif->emlsr.exit_repeat_count > 1 &&
160 	    reason & IWL_MLD_PREVENT_EMLSR_REASONS) {
161 		if (mld_vif->emlsr.exit_repeat_count == 2)
162 			delay = IWL_MLD_EMLSR_PREVENT_SHORT;
163 		else
164 			delay = IWL_MLD_EMLSR_PREVENT_LONG;
165 
166 		/*
167 		 * The timeouts are chosen so that this will not happen, i.e.
168 		 * IWL_MLD_EMLSR_PREVENT_LONG > IWL_MLD_PREVENT_EMLSR_TIMEOUT
169 		 */
170 		WARN_ON(mld_vif->emlsr.exit_repeat_count > 3);
171 	}
172 
173 	IWL_DEBUG_INFO(mld,
174 		       "Preventing EMLSR for %ld seconds due to %u exits with the reason = %s (0x%x)\n",
175 		       delay / HZ, mld_vif->emlsr.exit_repeat_count,
176 		       iwl_mld_get_emlsr_exit_string(reason), reason);
177 
178 	wiphy_delayed_work_queue(mld->wiphy,
179 				 &mld_vif->emlsr.prevent_done_wk, delay);
180 }
181 
182 static void iwl_mld_clear_avg_chan_load_iter(struct ieee80211_hw *hw,
183 					     struct ieee80211_chanctx_conf *ctx,
184 					     void *dat)
185 {
186 	struct iwl_mld_phy *phy = iwl_mld_phy_from_mac80211(ctx);
187 
188 	/* It is ok to do it for all chanctx (and not only for the ones that
189 	 * belong to the EMLSR vif) since EMLSR is not allowed if there is
190 	 * another vif.
191 	 */
192 	phy->avg_channel_load_not_by_us = 0;
193 }
194 
195 static int _iwl_mld_exit_emlsr(struct iwl_mld *mld, struct ieee80211_vif *vif,
196 			       enum iwl_mld_emlsr_exit exit, u8 link_to_keep,
197 			       bool sync)
198 {
199 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
200 	u16 new_active_links;
201 	int ret = 0;
202 
203 	lockdep_assert_wiphy(mld->wiphy);
204 
205 	/* On entry failure need to exit anyway, even if entered from debugfs */
206 	if (exit != IWL_MLD_EMLSR_EXIT_FAIL_ENTRY && !IWL_MLD_AUTO_EML_ENABLE)
207 		return 0;
208 
209 	/* Ignore exit request if EMLSR is not active */
210 	if (!iwl_mld_emlsr_active(vif))
211 		return 0;
212 
213 	if (WARN_ON(!ieee80211_vif_is_mld(vif) || !mld_vif->authorized))
214 		return 0;
215 
216 	if (WARN_ON(!(vif->active_links & BIT(link_to_keep))))
217 		link_to_keep = __ffs(vif->active_links);
218 
219 	new_active_links = BIT(link_to_keep);
220 	IWL_DEBUG_INFO(mld,
221 		       "Exiting EMLSR. reason = %s (0x%x). Current active links=0x%x, new active links = 0x%x\n",
222 		       iwl_mld_get_emlsr_exit_string(exit), exit,
223 		       vif->active_links, new_active_links);
224 
225 	if (sync)
226 		ret = ieee80211_set_active_links(vif, new_active_links);
227 	else
228 		ieee80211_set_active_links_async(vif, new_active_links);
229 
230 	/* Update latest exit reason and check EMLSR prevention */
231 	iwl_mld_check_emlsr_prevention(mld, mld_vif, exit);
232 
233 	/* channel_load_not_by_us is invalid when in EMLSR.
234 	 * Clear it so wrong values won't be used.
235 	 */
236 	ieee80211_iter_chan_contexts_atomic(mld->hw,
237 					    iwl_mld_clear_avg_chan_load_iter,
238 					    NULL);
239 
240 	return ret;
241 }
242 
243 void iwl_mld_exit_emlsr(struct iwl_mld *mld, struct ieee80211_vif *vif,
244 			enum iwl_mld_emlsr_exit exit, u8 link_to_keep)
245 {
246 	_iwl_mld_exit_emlsr(mld, vif, exit, link_to_keep, false);
247 }
248 
249 static int _iwl_mld_emlsr_block(struct iwl_mld *mld, struct ieee80211_vif *vif,
250 				enum iwl_mld_emlsr_blocked reason,
251 				u8 link_to_keep, bool sync)
252 {
253 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
254 
255 	lockdep_assert_wiphy(mld->wiphy);
256 
257 	if (!IWL_MLD_AUTO_EML_ENABLE || !iwl_mld_vif_has_emlsr_cap(vif))
258 		return 0;
259 
260 	if (mld_vif->emlsr.blocked_reasons & reason)
261 		return 0;
262 
263 	mld_vif->emlsr.blocked_reasons |= reason;
264 
265 	IWL_DEBUG_INFO(mld,
266 		       "Blocking EMLSR mode. reason = %s (0x%x)\n",
267 		       iwl_mld_get_emlsr_blocked_string(reason), reason);
268 	iwl_mld_print_emlsr_blocked(mld, mld_vif->emlsr.blocked_reasons);
269 
270 	if (reason == IWL_MLD_EMLSR_BLOCKED_TPT)
271 		wiphy_delayed_work_cancel(mld_vif->mld->wiphy,
272 					  &mld_vif->emlsr.check_tpt_wk);
273 
274 	return _iwl_mld_exit_emlsr(mld, vif, IWL_MLD_EMLSR_EXIT_BLOCK,
275 				   link_to_keep, sync);
276 }
277 
278 void iwl_mld_block_emlsr(struct iwl_mld *mld, struct ieee80211_vif *vif,
279 			 enum iwl_mld_emlsr_blocked reason, u8 link_to_keep)
280 {
281 	_iwl_mld_emlsr_block(mld, vif, reason, link_to_keep, false);
282 }
283 
284 int iwl_mld_block_emlsr_sync(struct iwl_mld *mld, struct ieee80211_vif *vif,
285 			     enum iwl_mld_emlsr_blocked reason, u8 link_to_keep)
286 {
287 	return _iwl_mld_emlsr_block(mld, vif, reason, link_to_keep, true);
288 }
289 
290 #define IWL_MLD_EMLSR_BLOCKED_TMP_NON_BSS_TIMEOUT (10 * HZ)
291 
292 static void iwl_mld_vif_iter_emlsr_block_tmp_non_bss(void *_data, u8 *mac,
293 						     struct ieee80211_vif *vif)
294 {
295 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
296 	int ret;
297 
298 	if (!iwl_mld_vif_has_emlsr_cap(vif))
299 		return;
300 
301 	ret = iwl_mld_block_emlsr_sync(mld_vif->mld, vif,
302 				       IWL_MLD_EMLSR_BLOCKED_TMP_NON_BSS,
303 				       iwl_mld_get_primary_link(vif));
304 	if (ret)
305 		return;
306 
307 	wiphy_delayed_work_queue(mld_vif->mld->wiphy,
308 				 &mld_vif->emlsr.tmp_non_bss_done_wk,
309 				 IWL_MLD_EMLSR_BLOCKED_TMP_NON_BSS_TIMEOUT);
310 }
311 
312 void iwl_mld_emlsr_block_tmp_non_bss(struct iwl_mld *mld)
313 {
314 	ieee80211_iterate_active_interfaces_mtx(mld->hw,
315 						IEEE80211_IFACE_ITER_NORMAL,
316 						iwl_mld_vif_iter_emlsr_block_tmp_non_bss,
317 						NULL);
318 }
319 
320 static void _iwl_mld_select_links(struct iwl_mld *mld,
321 				  struct ieee80211_vif *vif);
322 
323 void iwl_mld_unblock_emlsr(struct iwl_mld *mld, struct ieee80211_vif *vif,
324 			   enum iwl_mld_emlsr_blocked reason)
325 {
326 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
327 
328 	lockdep_assert_wiphy(mld->wiphy);
329 
330 	if (!IWL_MLD_AUTO_EML_ENABLE || !iwl_mld_vif_has_emlsr_cap(vif))
331 		return;
332 
333 	if (!(mld_vif->emlsr.blocked_reasons & reason))
334 		return;
335 
336 	mld_vif->emlsr.blocked_reasons &= ~reason;
337 
338 	IWL_DEBUG_INFO(mld,
339 		       "Unblocking EMLSR mode. reason = %s (0x%x)\n",
340 		       iwl_mld_get_emlsr_blocked_string(reason), reason);
341 	iwl_mld_print_emlsr_blocked(mld, mld_vif->emlsr.blocked_reasons);
342 
343 	if (reason == IWL_MLD_EMLSR_BLOCKED_TPT)
344 		wiphy_delayed_work_queue(mld_vif->mld->wiphy,
345 					 &mld_vif->emlsr.check_tpt_wk,
346 					 round_jiffies_relative(IWL_MLD_TPT_COUNT_WINDOW));
347 
348 	if (mld_vif->emlsr.blocked_reasons)
349 		return;
350 
351 	IWL_DEBUG_INFO(mld, "EMLSR is unblocked\n");
352 	iwl_mld_int_mlo_scan(mld, vif);
353 }
354 
355 static void
356 iwl_mld_vif_iter_emlsr_mode_notif(void *data, u8 *mac,
357 				  struct ieee80211_vif *vif)
358 {
359 	const struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
360 	const struct iwl_esr_mode_notif *notif = (void *)data;
361 	enum iwl_mvm_fw_esr_recommendation action = le32_to_cpu(notif->action);
362 
363 	if (!iwl_mld_vif_has_emlsr_cap(vif))
364 		return;
365 
366 	switch (action) {
367 	case ESR_RECOMMEND_LEAVE:
368 		IWL_DEBUG_INFO(mld_vif->mld,
369 			       "FW recommend leave reason = 0x%x\n",
370 			       le32_to_cpu(notif->leave_reason_mask));
371 
372 		iwl_mld_exit_emlsr(mld_vif->mld, vif,
373 				   IWL_MLD_EMLSR_EXIT_FW_REQUEST,
374 				   iwl_mld_get_primary_link(vif));
375 		break;
376 	case ESR_FORCE_LEAVE:
377 		IWL_DEBUG_INFO(mld_vif->mld,
378 			       "FW force leave reason = 0x%x\n",
379 			       le32_to_cpu(notif->leave_reason_mask));
380 		fallthrough;
381 	case ESR_RECOMMEND_ENTER:
382 	default:
383 		IWL_WARN(mld_vif->mld, "Unexpected EMLSR notification: %d\n",
384 			 action);
385 	}
386 }
387 
388 void iwl_mld_handle_emlsr_mode_notif(struct iwl_mld *mld,
389 				     struct iwl_rx_packet *pkt)
390 {
391 	ieee80211_iterate_active_interfaces_mtx(mld->hw,
392 						IEEE80211_IFACE_ITER_NORMAL,
393 						iwl_mld_vif_iter_emlsr_mode_notif,
394 						pkt->data);
395 }
396 
397 static void
398 iwl_mld_vif_iter_disconnect_emlsr(void *data, u8 *mac,
399 				  struct ieee80211_vif *vif)
400 {
401 	if (!iwl_mld_vif_has_emlsr_cap(vif))
402 		return;
403 
404 	ieee80211_connection_loss(vif);
405 }
406 
407 void iwl_mld_handle_emlsr_trans_fail_notif(struct iwl_mld *mld,
408 					   struct iwl_rx_packet *pkt)
409 {
410 	const struct iwl_esr_trans_fail_notif *notif = (const void *)pkt->data;
411 	u32 fw_link_id = le32_to_cpu(notif->link_id);
412 	struct ieee80211_bss_conf *bss_conf =
413 		iwl_mld_fw_id_to_link_conf(mld, fw_link_id);
414 
415 	IWL_DEBUG_INFO(mld, "Failed to %s EMLSR on link %d (FW: %d), reason %d\n",
416 		       le32_to_cpu(notif->activation) ? "enter" : "exit",
417 		       bss_conf ? bss_conf->link_id : -1,
418 		       le32_to_cpu(notif->link_id),
419 		       le32_to_cpu(notif->err_code));
420 
421 	if (IWL_FW_CHECK(mld, !bss_conf,
422 			 "FW reported failure to %sactivate EMLSR on a non-existing link: %d\n",
423 			 le32_to_cpu(notif->activation) ? "" : "de",
424 			 fw_link_id)) {
425 		ieee80211_iterate_active_interfaces_mtx(
426 			mld->hw, IEEE80211_IFACE_ITER_NORMAL,
427 			iwl_mld_vif_iter_disconnect_emlsr, NULL);
428 		return;
429 	}
430 
431 	/* Disconnect if we failed to deactivate a link */
432 	if (!le32_to_cpu(notif->activation)) {
433 		ieee80211_connection_loss(bss_conf->vif);
434 		return;
435 	}
436 
437 	/*
438 	 * We failed to activate the second link, go back to the link specified
439 	 * by the firmware as that is the one that is still valid now.
440 	 */
441 	iwl_mld_exit_emlsr(mld, bss_conf->vif, IWL_MLD_EMLSR_EXIT_FAIL_ENTRY,
442 			   bss_conf->link_id);
443 }
444 
445 /* Active non-station link tracking */
446 static void iwl_mld_count_non_bss_links(void *_data, u8 *mac,
447 					struct ieee80211_vif *vif)
448 {
449 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
450 	int *count = _data;
451 
452 	if (ieee80211_vif_type_p2p(vif) == NL80211_IFTYPE_STATION)
453 		return;
454 
455 	*count += iwl_mld_count_active_links(mld_vif->mld, vif);
456 }
457 
458 struct iwl_mld_update_emlsr_block_data {
459 	bool block;
460 	int result;
461 };
462 
463 static void
464 iwl_mld_vif_iter_update_emlsr_non_bss_block(void *_data, u8 *mac,
465 					    struct ieee80211_vif *vif)
466 {
467 	struct iwl_mld_update_emlsr_block_data *data = _data;
468 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
469 	int ret;
470 
471 	if (data->block) {
472 		ret = iwl_mld_block_emlsr_sync(mld_vif->mld, vif,
473 					       IWL_MLD_EMLSR_BLOCKED_NON_BSS,
474 					       iwl_mld_get_primary_link(vif));
475 		if (ret)
476 			data->result = ret;
477 	} else {
478 		iwl_mld_unblock_emlsr(mld_vif->mld, vif,
479 				      IWL_MLD_EMLSR_BLOCKED_NON_BSS);
480 	}
481 }
482 
483 int iwl_mld_emlsr_check_non_bss_block(struct iwl_mld *mld,
484 				      int pending_link_changes)
485 {
486 	/* An active link of a non-station vif blocks EMLSR. Upon activation
487 	 * block EMLSR on the bss vif. Upon deactivation, check if this link
488 	 * was the last non-station link active, and if so unblock the bss vif
489 	 */
490 	struct iwl_mld_update_emlsr_block_data block_data = {};
491 	int count = pending_link_changes;
492 
493 	/* No need to count if we are activating a non-BSS link */
494 	if (count <= 0)
495 		ieee80211_iterate_active_interfaces_mtx(mld->hw,
496 							IEEE80211_IFACE_ITER_NORMAL,
497 							iwl_mld_count_non_bss_links,
498 							&count);
499 
500 	/*
501 	 * We could skip updating it if the block change did not change (and
502 	 * pending_link_changes is non-zero).
503 	 */
504 	block_data.block = !!count;
505 
506 	ieee80211_iterate_active_interfaces_mtx(mld->hw,
507 						IEEE80211_IFACE_ITER_NORMAL,
508 						iwl_mld_vif_iter_update_emlsr_non_bss_block,
509 						&block_data);
510 
511 	return block_data.result;
512 }
513 
514 #define EMLSR_SEC_LINK_MIN_PERC 10
515 #define EMLSR_MIN_TX 3000
516 #define EMLSR_MIN_RX 400
517 
518 void iwl_mld_emlsr_check_tpt(struct wiphy *wiphy, struct wiphy_work *wk)
519 {
520 	struct iwl_mld_vif *mld_vif = container_of(wk, struct iwl_mld_vif,
521 						   emlsr.check_tpt_wk.work);
522 	struct ieee80211_vif *vif =
523 		container_of((void *)mld_vif, struct ieee80211_vif, drv_priv);
524 	struct iwl_mld *mld = mld_vif->mld;
525 	struct iwl_mld_sta *mld_sta;
526 	struct iwl_mld_link *sec_link;
527 	unsigned long total_tx = 0, total_rx = 0;
528 	unsigned long sec_link_tx = 0, sec_link_rx = 0;
529 	u8 sec_link_tx_perc, sec_link_rx_perc;
530 	s8 sec_link_id;
531 
532 	if (!iwl_mld_vif_has_emlsr_cap(vif) || !mld_vif->ap_sta)
533 		return;
534 
535 	mld_sta = iwl_mld_sta_from_mac80211(mld_vif->ap_sta);
536 
537 	/* We only count for the AP sta in a MLO connection */
538 	if (!mld_sta->mpdu_counters)
539 		return;
540 
541 	/* This wk should only run when the TPT blocker isn't set.
542 	 * When the blocker is set, the decision to remove it, as well as
543 	 * clearing the counters is done in DP (to avoid having a wk every
544 	 * 5 seconds when idle. When the blocker is unset, we are not idle anyway)
545 	 */
546 	if (WARN_ON(mld_vif->emlsr.blocked_reasons & IWL_MLD_EMLSR_BLOCKED_TPT))
547 		return;
548 	/*
549 	 * TPT is unblocked, need to check if the TPT criteria is still met.
550 	 *
551 	 * If EMLSR is active for at least 5 seconds, then we also
552 	 * need to check the secondary link requirements.
553 	 */
554 	if (iwl_mld_emlsr_active(vif) &&
555 	    time_is_before_jiffies(mld_vif->emlsr.last_entry_ts +
556 				   IWL_MLD_TPT_COUNT_WINDOW)) {
557 		sec_link_id = iwl_mld_get_other_link(vif, iwl_mld_get_primary_link(vif));
558 		sec_link = iwl_mld_link_dereference_check(mld_vif, sec_link_id);
559 		if (WARN_ON_ONCE(!sec_link))
560 			return;
561 		/* We need the FW ID here */
562 		sec_link_id = sec_link->fw_id;
563 	} else {
564 		sec_link_id = -1;
565 	}
566 
567 	/* Sum up RX and TX MPDUs from the different queues/links */
568 	for (int q = 0; q < mld->trans->info.num_rxqs; q++) {
569 		struct iwl_mld_per_q_mpdu_counter *queue_counter =
570 			&mld_sta->mpdu_counters[q];
571 
572 		spin_lock_bh(&queue_counter->lock);
573 
574 		/* The link IDs that doesn't exist will contain 0 */
575 		for (int link = 0;
576 		     link < ARRAY_SIZE(queue_counter->per_link);
577 		     link++) {
578 			total_tx += queue_counter->per_link[link].tx;
579 			total_rx += queue_counter->per_link[link].rx;
580 		}
581 
582 		if (sec_link_id != -1) {
583 			sec_link_tx += queue_counter->per_link[sec_link_id].tx;
584 			sec_link_rx += queue_counter->per_link[sec_link_id].rx;
585 		}
586 
587 		memset(queue_counter->per_link, 0,
588 		       sizeof(queue_counter->per_link));
589 
590 		spin_unlock_bh(&queue_counter->lock);
591 	}
592 
593 	IWL_DEBUG_INFO(mld, "total Tx MPDUs: %ld. total Rx MPDUs: %ld\n",
594 		       total_tx, total_rx);
595 
596 	/* If we don't have enough MPDUs - exit EMLSR */
597 	if (total_tx < IWL_MLD_ENTER_EMLSR_TPT_THRESH &&
598 	    total_rx < IWL_MLD_ENTER_EMLSR_TPT_THRESH) {
599 		iwl_mld_block_emlsr(mld, vif, IWL_MLD_EMLSR_BLOCKED_TPT,
600 				    iwl_mld_get_primary_link(vif));
601 		return;
602 	}
603 
604 	/* EMLSR is not active */
605 	if (sec_link_id == -1)
606 		return;
607 
608 	IWL_DEBUG_INFO(mld, "Secondary Link %d: Tx MPDUs: %ld. Rx MPDUs: %ld\n",
609 		       sec_link_id, sec_link_tx, sec_link_rx);
610 
611 	/* Calculate the percentage of the secondary link TX/RX */
612 	sec_link_tx_perc = total_tx ? sec_link_tx * 100 / total_tx : 0;
613 	sec_link_rx_perc = total_rx ? sec_link_rx * 100 / total_rx : 0;
614 
615 	/*
616 	 * The TX/RX percentage is checked only if it exceeds the required
617 	 * minimum. In addition, RX is checked only if the TX check failed.
618 	 */
619 	if ((total_tx > EMLSR_MIN_TX &&
620 	     sec_link_tx_perc < EMLSR_SEC_LINK_MIN_PERC) ||
621 	    (total_rx > EMLSR_MIN_RX &&
622 	     sec_link_rx_perc < EMLSR_SEC_LINK_MIN_PERC)) {
623 		iwl_mld_exit_emlsr(mld, vif, IWL_MLD_EMLSR_EXIT_LINK_USAGE,
624 				   iwl_mld_get_primary_link(vif));
625 		return;
626 	}
627 
628 	/* Check again when the next window ends  */
629 	wiphy_delayed_work_queue(mld_vif->mld->wiphy,
630 				 &mld_vif->emlsr.check_tpt_wk,
631 				 round_jiffies_relative(IWL_MLD_TPT_COUNT_WINDOW));
632 }
633 
634 void iwl_mld_emlsr_unblock_tpt_wk(struct wiphy *wiphy, struct wiphy_work *wk)
635 {
636 	struct iwl_mld_vif *mld_vif = container_of(wk, struct iwl_mld_vif,
637 						   emlsr.unblock_tpt_wk);
638 	struct ieee80211_vif *vif =
639 		container_of((void *)mld_vif, struct ieee80211_vif, drv_priv);
640 
641 	iwl_mld_unblock_emlsr(mld_vif->mld, vif, IWL_MLD_EMLSR_BLOCKED_TPT);
642 }
643 
644 /*
645  * Link selection
646  */
647 
648 s8 iwl_mld_get_emlsr_rssi_thresh(struct iwl_mld *mld,
649 				 const struct cfg80211_chan_def *chandef,
650 				 bool low)
651 {
652 	if (WARN_ON(chandef->chan->band != NL80211_BAND_2GHZ &&
653 		    chandef->chan->band != NL80211_BAND_5GHZ &&
654 		    chandef->chan->band != NL80211_BAND_6GHZ))
655 		return S8_MAX;
656 
657 #define RSSI_THRESHOLD(_low, _bw)			\
658 	(_low) ? IWL_MLD_LOW_RSSI_THRESH_##_bw##MHZ	\
659 	       : IWL_MLD_HIGH_RSSI_THRESH_##_bw##MHZ
660 
661 	switch (chandef->width) {
662 	case NL80211_CHAN_WIDTH_20_NOHT:
663 	case NL80211_CHAN_WIDTH_20:
664 	/* 320 MHz has the same thresholds as 20 MHz */
665 	case NL80211_CHAN_WIDTH_320:
666 		return RSSI_THRESHOLD(low, 20);
667 	case NL80211_CHAN_WIDTH_40:
668 		return RSSI_THRESHOLD(low, 40);
669 	case NL80211_CHAN_WIDTH_80:
670 		return RSSI_THRESHOLD(low, 80);
671 	case NL80211_CHAN_WIDTH_160:
672 		return RSSI_THRESHOLD(low, 160);
673 	default:
674 		WARN_ON(1);
675 		return S8_MAX;
676 	}
677 #undef RSSI_THRESHOLD
678 }
679 
680 static u32
681 iwl_mld_emlsr_disallowed_with_link(struct iwl_mld *mld,
682 				   struct ieee80211_vif *vif,
683 				   struct iwl_mld_link_sel_data *link,
684 				   bool primary)
685 {
686 	struct wiphy *wiphy = mld->wiphy;
687 	struct ieee80211_bss_conf *conf;
688 	u32 ret = 0;
689 
690 	conf = wiphy_dereference(wiphy, vif->link_conf[link->link_id]);
691 	if (WARN_ON_ONCE(!conf))
692 		return IWL_MLD_EMLSR_EXIT_INVALID;
693 
694 	if (link->chandef->chan->band == NL80211_BAND_2GHZ && mld->bt_is_active)
695 		ret |= IWL_MLD_EMLSR_EXIT_BT_COEX;
696 
697 	if (link->signal <
698 	    iwl_mld_get_emlsr_rssi_thresh(mld, link->chandef, false))
699 		ret |= IWL_MLD_EMLSR_EXIT_LOW_RSSI;
700 
701 	if (conf->csa_active)
702 		ret |= IWL_MLD_EMLSR_EXIT_CSA;
703 
704 	if (ret) {
705 		IWL_DEBUG_INFO(mld,
706 			       "Link %d is not allowed for EMLSR as %s\n",
707 			       link->link_id,
708 			       primary ? "primary" : "secondary");
709 		iwl_mld_print_emlsr_exit(mld, ret);
710 	}
711 
712 	return ret;
713 }
714 
715 static u8
716 iwl_mld_set_link_sel_data(struct iwl_mld *mld,
717 			  struct ieee80211_vif *vif,
718 			  struct iwl_mld_link_sel_data *data,
719 			  unsigned long usable_links,
720 			  u8 *best_link_idx)
721 {
722 	u8 n_data = 0;
723 	u16 max_grade = 0;
724 	unsigned long link_id;
725 
726 	for_each_set_bit(link_id, &usable_links, IEEE80211_MLD_MAX_NUM_LINKS) {
727 		struct ieee80211_bss_conf *link_conf =
728 			link_conf_dereference_protected(vif, link_id);
729 
730 		if (WARN_ON_ONCE(!link_conf))
731 			continue;
732 
733 		/* Ignore any BSS that was not seen in the last MLO scan */
734 		if (ktime_before(link_conf->bss->ts_boottime,
735 				 mld->scan.last_mlo_scan_time))
736 			continue;
737 
738 		data[n_data].link_id = link_id;
739 		data[n_data].chandef = &link_conf->chanreq.oper;
740 		data[n_data].signal = MBM_TO_DBM(link_conf->bss->signal);
741 		data[n_data].grade = iwl_mld_get_link_grade(mld, link_conf);
742 
743 		if (n_data == 0 || data[n_data].grade > max_grade) {
744 			max_grade = data[n_data].grade;
745 			*best_link_idx = n_data;
746 		}
747 		n_data++;
748 	}
749 
750 	return n_data;
751 }
752 
753 static u32
754 iwl_mld_get_min_chan_load_thresh(struct ieee80211_chanctx_conf *chanctx)
755 {
756 	const struct iwl_mld_phy *phy = iwl_mld_phy_from_mac80211(chanctx);
757 
758 	switch (phy->chandef.width) {
759 	case NL80211_CHAN_WIDTH_320:
760 	case NL80211_CHAN_WIDTH_160:
761 		return 5;
762 	case NL80211_CHAN_WIDTH_80:
763 		return 7;
764 	default:
765 		break;
766 	}
767 	return 10;
768 }
769 
770 static bool
771 iwl_mld_channel_load_allows_emlsr(struct iwl_mld *mld,
772 				  struct ieee80211_vif *vif,
773 				  const struct iwl_mld_link_sel_data *a,
774 				  const struct iwl_mld_link_sel_data *b)
775 {
776 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
777 	struct iwl_mld_link *link_a =
778 		iwl_mld_link_dereference_check(mld_vif, a->link_id);
779 	struct ieee80211_chanctx_conf *chanctx_a = NULL;
780 	u32 bw_a, bw_b, ratio;
781 	u32 primary_load_perc;
782 
783 	if (!link_a || !link_a->active) {
784 		IWL_DEBUG_EHT(mld, "Primary link is not active. Can't enter EMLSR\n");
785 		return false;
786 	}
787 
788 	chanctx_a = wiphy_dereference(mld->wiphy, link_a->chan_ctx);
789 
790 	if (WARN_ON(!chanctx_a))
791 		return false;
792 
793 	primary_load_perc =
794 		iwl_mld_phy_from_mac80211(chanctx_a)->avg_channel_load_not_by_us;
795 
796 	IWL_DEBUG_EHT(mld, "Average channel load not by us: %u\n", primary_load_perc);
797 
798 	if (primary_load_perc < iwl_mld_get_min_chan_load_thresh(chanctx_a)) {
799 		IWL_DEBUG_EHT(mld, "Channel load is below the minimum threshold\n");
800 		return false;
801 	}
802 
803 	if (iwl_mld_vif_low_latency(mld_vif)) {
804 		IWL_DEBUG_EHT(mld, "Low latency vif, EMLSR is allowed\n");
805 		return true;
806 	}
807 
808 	if (a->chandef->width <= b->chandef->width)
809 		return true;
810 
811 	bw_a = cfg80211_chandef_get_width(a->chandef);
812 	bw_b = cfg80211_chandef_get_width(b->chandef);
813 	ratio = bw_a / bw_b;
814 
815 	switch (ratio) {
816 	case 2:
817 		return primary_load_perc > 25;
818 	case 4:
819 		return primary_load_perc > 40;
820 	case 8:
821 	case 16:
822 		return primary_load_perc > 50;
823 	}
824 
825 	return false;
826 }
827 
828 VISIBLE_IF_IWLWIFI_KUNIT u32
829 iwl_mld_emlsr_pair_state(struct ieee80211_vif *vif,
830 			 struct iwl_mld_link_sel_data *a,
831 			 struct iwl_mld_link_sel_data *b)
832 {
833 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
834 	struct iwl_mld *mld = mld_vif->mld;
835 	u32 reason_mask = 0;
836 
837 	/* Per-link considerations */
838 	reason_mask = iwl_mld_emlsr_disallowed_with_link(mld, vif, a, true);
839 	if (reason_mask)
840 		return reason_mask;
841 
842 	reason_mask = iwl_mld_emlsr_disallowed_with_link(mld, vif, b, false);
843 	if (reason_mask)
844 		return reason_mask;
845 
846 	if (a->chandef->chan->band == b->chandef->chan->band) {
847 		const struct cfg80211_chan_def *c_low = a->chandef;
848 		const struct cfg80211_chan_def *c_high = b->chandef;
849 		u32 c_low_upper_edge, c_high_lower_edge;
850 
851 		if (c_low->chan->center_freq > c_high->chan->center_freq)
852 			swap(c_low, c_high);
853 
854 		c_low_upper_edge = c_low->chan->center_freq +
855 				   cfg80211_chandef_get_width(c_low) / 2;
856 		c_high_lower_edge = c_high->chan->center_freq -
857 				    cfg80211_chandef_get_width(c_high) / 2;
858 
859 		if (a->chandef->chan->band == NL80211_BAND_5GHZ &&
860 		    c_low_upper_edge <= 5330 && c_high_lower_edge >= 5490) {
861 			/* This case is fine - HW/FW can deal with it, there's
862 			 * enough separation between the two channels.
863 			 */
864 		} else {
865 			reason_mask |= IWL_MLD_EMLSR_EXIT_EQUAL_BAND;
866 		}
867 	}
868 	if (!iwl_mld_channel_load_allows_emlsr(mld, vif, a, b))
869 		reason_mask |= IWL_MLD_EMLSR_EXIT_CHAN_LOAD;
870 
871 	if (reason_mask) {
872 		IWL_DEBUG_INFO(mld,
873 			       "Links %d and %d are not a valid pair for EMLSR\n",
874 			       a->link_id, b->link_id);
875 		IWL_DEBUG_INFO(mld,
876 			       "Links bandwidth are: %d and %d\n",
877 			       nl80211_chan_width_to_mhz(a->chandef->width),
878 			       nl80211_chan_width_to_mhz(b->chandef->width));
879 		iwl_mld_print_emlsr_exit(mld, reason_mask);
880 	}
881 
882 	return reason_mask;
883 }
884 EXPORT_SYMBOL_IF_IWLWIFI_KUNIT(iwl_mld_emlsr_pair_state);
885 
886 /* Calculation is done with fixed-point with a scaling factor of 1/256 */
887 #define SCALE_FACTOR 256
888 
889 /*
890  * Returns the combined grade of two given links.
891  * Returns 0 if EMLSR is not allowed with these 2 links.
892  */
893 static
894 unsigned int iwl_mld_get_emlsr_grade(struct iwl_mld *mld,
895 				     struct ieee80211_vif *vif,
896 				     struct iwl_mld_link_sel_data *a,
897 				     struct iwl_mld_link_sel_data *b,
898 				     u8 *primary_id)
899 {
900 	struct ieee80211_bss_conf *primary_conf;
901 	struct wiphy *wiphy = ieee80211_vif_to_wdev(vif)->wiphy;
902 	unsigned int primary_load;
903 
904 	lockdep_assert_wiphy(wiphy);
905 
906 	/* a is always primary, b is always secondary */
907 	if (b->grade > a->grade)
908 		swap(a, b);
909 
910 	*primary_id = a->link_id;
911 
912 	if (iwl_mld_emlsr_pair_state(vif, a, b))
913 		return 0;
914 
915 	primary_conf = wiphy_dereference(wiphy, vif->link_conf[*primary_id]);
916 
917 	if (WARN_ON_ONCE(!primary_conf))
918 		return 0;
919 
920 	primary_load = iwl_mld_get_chan_load(mld, primary_conf);
921 
922 	/* The more the primary link is loaded, the more worthwhile EMLSR becomes */
923 	return a->grade + ((b->grade * primary_load) / SCALE_FACTOR);
924 }
925 
926 static void _iwl_mld_select_links(struct iwl_mld *mld,
927 				  struct ieee80211_vif *vif)
928 {
929 	struct iwl_mld_link_sel_data data[IEEE80211_MLD_MAX_NUM_LINKS];
930 	struct iwl_mld_link_sel_data *best_link;
931 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
932 	int max_active_links = iwl_mld_max_active_links(mld, vif);
933 	u16 new_active, usable_links = ieee80211_vif_usable_links(vif);
934 	u8 best_idx, new_primary, n_data;
935 	u16 max_grade;
936 
937 	lockdep_assert_wiphy(mld->wiphy);
938 
939 	if (!mld_vif->authorized || hweight16(usable_links) <= 1)
940 		return;
941 
942 	if (WARN(ktime_before(mld->scan.last_mlo_scan_time,
943 			      ktime_sub_ns(ktime_get_boottime_ns(),
944 					   5ULL * NSEC_PER_SEC)),
945 		"Last MLO scan was too long ago, can't select links\n"))
946 		return;
947 
948 	/* The logic below is simple and not suited for more than 2 links */
949 	WARN_ON_ONCE(max_active_links > 2);
950 
951 	n_data = iwl_mld_set_link_sel_data(mld, vif, data, usable_links,
952 					   &best_idx);
953 
954 	if (!n_data) {
955 		IWL_DEBUG_EHT(mld,
956 			      "Couldn't find a valid grade for any link!\n");
957 		return;
958 	}
959 
960 	/* Default to selecting the single best link */
961 	best_link = &data[best_idx];
962 	new_primary = best_link->link_id;
963 	new_active = BIT(best_link->link_id);
964 	max_grade = best_link->grade;
965 
966 	/* If EMLSR is not possible, activate the best link */
967 	if (max_active_links == 1 || n_data == 1 ||
968 	    !iwl_mld_vif_has_emlsr_cap(vif) || !IWL_MLD_AUTO_EML_ENABLE ||
969 	    mld_vif->emlsr.blocked_reasons)
970 		goto set_active;
971 
972 	/* Try to find the best link combination */
973 	for (u8 a = 0; a < n_data; a++) {
974 		for (u8 b = a + 1; b < n_data; b++) {
975 			u8 best_in_pair;
976 			u16 emlsr_grade =
977 				iwl_mld_get_emlsr_grade(mld, vif,
978 							&data[a], &data[b],
979 							&best_in_pair);
980 
981 			/*
982 			 * Prefer (new) EMLSR combination to prefer EMLSR over
983 			 * a single link.
984 			 */
985 			if (emlsr_grade < max_grade)
986 				continue;
987 
988 			max_grade = emlsr_grade;
989 			new_primary = best_in_pair;
990 			new_active = BIT(data[a].link_id) |
991 				     BIT(data[b].link_id);
992 		}
993 	}
994 
995 set_active:
996 	IWL_DEBUG_INFO(mld, "Link selection result: 0x%x. Primary = %d\n",
997 		       new_active, new_primary);
998 
999 	mld_vif->emlsr.selected_primary = new_primary;
1000 	mld_vif->emlsr.selected_links = new_active;
1001 
1002 	ieee80211_set_active_links_async(vif, new_active);
1003 }
1004 
1005 static void iwl_mld_vif_iter_select_links(void *_data, u8 *mac,
1006 					  struct ieee80211_vif *vif)
1007 {
1008 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
1009 	struct iwl_mld *mld = mld_vif->mld;
1010 
1011 	_iwl_mld_select_links(mld, vif);
1012 }
1013 
1014 void iwl_mld_select_links(struct iwl_mld *mld)
1015 {
1016 	ieee80211_iterate_active_interfaces_mtx(mld->hw,
1017 						IEEE80211_IFACE_ITER_NORMAL,
1018 						iwl_mld_vif_iter_select_links,
1019 						NULL);
1020 }
1021 
1022 static void iwl_mld_emlsr_check_bt_iter(void *_data, u8 *mac,
1023 					struct ieee80211_vif *vif)
1024 {
1025 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
1026 	struct iwl_mld *mld = mld_vif->mld;
1027 	struct ieee80211_bss_conf *link;
1028 	unsigned int link_id;
1029 
1030 	if (!iwl_mld_vif_has_emlsr_cap(vif))
1031 		return;
1032 
1033 	if (!mld->bt_is_active) {
1034 		iwl_mld_retry_emlsr(mld, vif);
1035 		return;
1036 	}
1037 
1038 	/* BT is turned ON but we are not in EMLSR, nothing to do */
1039 	if (!iwl_mld_emlsr_active(vif))
1040 		return;
1041 
1042 	/* In EMLSR and BT is turned ON */
1043 
1044 	for_each_vif_active_link(vif, link, link_id) {
1045 		if (WARN_ON(!link->chanreq.oper.chan))
1046 			continue;
1047 
1048 		if (link->chanreq.oper.chan->band == NL80211_BAND_2GHZ) {
1049 			iwl_mld_exit_emlsr(mld, vif, IWL_MLD_EMLSR_EXIT_BT_COEX,
1050 					   iwl_mld_get_primary_link(vif));
1051 			return;
1052 		}
1053 	}
1054 }
1055 
1056 void iwl_mld_emlsr_check_bt(struct iwl_mld *mld)
1057 {
1058 	ieee80211_iterate_active_interfaces_mtx(mld->hw,
1059 						IEEE80211_IFACE_ITER_NORMAL,
1060 						iwl_mld_emlsr_check_bt_iter,
1061 						NULL);
1062 }
1063 
1064 struct iwl_mld_chan_load_data {
1065 	struct iwl_mld_phy *phy;
1066 	u32 prev_chan_load_not_by_us;
1067 };
1068 
1069 static void iwl_mld_chan_load_update_iter(void *_data, u8 *mac,
1070 					  struct ieee80211_vif *vif)
1071 {
1072 	struct iwl_mld_chan_load_data *data = _data;
1073 	const struct iwl_mld_phy *phy = data->phy;
1074 	struct ieee80211_chanctx_conf *chanctx =
1075 		container_of((const void *)phy, struct ieee80211_chanctx_conf,
1076 			     drv_priv);
1077 	struct iwl_mld *mld = iwl_mld_vif_from_mac80211(vif)->mld;
1078 	struct ieee80211_bss_conf *prim_link;
1079 	unsigned int prim_link_id;
1080 
1081 	prim_link_id = iwl_mld_get_primary_link(vif);
1082 	prim_link = link_conf_dereference_protected(vif, prim_link_id);
1083 
1084 	if (WARN_ON(!prim_link))
1085 		return;
1086 
1087 	if (chanctx != rcu_access_pointer(prim_link->chanctx_conf))
1088 		return;
1089 
1090 	if (iwl_mld_emlsr_active(vif)) {
1091 		int chan_load = iwl_mld_get_chan_load_by_others(mld, prim_link,
1092 								true);
1093 
1094 		if (chan_load < 0)
1095 			return;
1096 
1097 		/* chan_load is in range [0,255] */
1098 		if (chan_load < NORMALIZE_PERCENT_TO_255(IWL_MLD_EXIT_EMLSR_CHAN_LOAD))
1099 			iwl_mld_exit_emlsr(mld, vif,
1100 					   IWL_MLD_EMLSR_EXIT_CHAN_LOAD,
1101 					   prim_link_id);
1102 	} else {
1103 		u32 old_chan_load = data->prev_chan_load_not_by_us;
1104 		u32 new_chan_load = phy->avg_channel_load_not_by_us;
1105 		u32 min_thresh = iwl_mld_get_min_chan_load_thresh(chanctx);
1106 
1107 #define THRESHOLD_CROSSED(threshold) \
1108 	(old_chan_load <= (threshold) && new_chan_load > (threshold))
1109 
1110 		if (THRESHOLD_CROSSED(min_thresh) || THRESHOLD_CROSSED(25) ||
1111 		    THRESHOLD_CROSSED(40) || THRESHOLD_CROSSED(50))
1112 			iwl_mld_retry_emlsr(mld, vif);
1113 #undef THRESHOLD_CROSSED
1114 	}
1115 }
1116 
1117 void iwl_mld_emlsr_check_chan_load(struct ieee80211_hw *hw,
1118 				   struct iwl_mld_phy *phy,
1119 				   u32 prev_chan_load_not_by_us)
1120 {
1121 	struct iwl_mld_chan_load_data data = {
1122 		.phy = phy,
1123 		.prev_chan_load_not_by_us = prev_chan_load_not_by_us,
1124 	};
1125 
1126 	ieee80211_iterate_active_interfaces_mtx(hw,
1127 						IEEE80211_IFACE_ITER_NORMAL,
1128 						iwl_mld_chan_load_update_iter,
1129 						&data);
1130 }
1131 
1132 void iwl_mld_retry_emlsr(struct iwl_mld *mld, struct ieee80211_vif *vif)
1133 {
1134 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
1135 
1136 	if (!IWL_MLD_AUTO_EML_ENABLE || !iwl_mld_vif_has_emlsr_cap(vif) ||
1137 	    iwl_mld_emlsr_active(vif) || mld_vif->emlsr.blocked_reasons)
1138 		return;
1139 
1140 	iwl_mld_int_mlo_scan(mld, vif);
1141 }
1142 
1143 static void iwl_mld_ignore_tpt_iter(void *data, u8 *mac,
1144 				    struct ieee80211_vif *vif)
1145 {
1146 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
1147 	struct iwl_mld *mld = mld_vif->mld;
1148 	struct iwl_mld_sta *mld_sta;
1149 	bool *start = (void *)data;
1150 
1151 	/* check_tpt_wk is only used when TPT block isn't set */
1152 	if (mld_vif->emlsr.blocked_reasons & IWL_MLD_EMLSR_BLOCKED_TPT ||
1153 	    !IWL_MLD_AUTO_EML_ENABLE || !mld_vif->ap_sta)
1154 		return;
1155 
1156 	mld_sta = iwl_mld_sta_from_mac80211(mld_vif->ap_sta);
1157 
1158 	/* We only count for the AP sta in a MLO connection */
1159 	if (!mld_sta->mpdu_counters)
1160 		return;
1161 
1162 	if (*start) {
1163 		wiphy_delayed_work_cancel(mld_vif->mld->wiphy,
1164 					  &mld_vif->emlsr.check_tpt_wk);
1165 		IWL_DEBUG_EHT(mld, "TPT check disabled\n");
1166 		return;
1167 	}
1168 
1169 	/* Clear the counters so we start from the beginning */
1170 	for (int q = 0; q < mld->trans->info.num_rxqs; q++) {
1171 		struct iwl_mld_per_q_mpdu_counter *queue_counter =
1172 			&mld_sta->mpdu_counters[q];
1173 
1174 		spin_lock_bh(&queue_counter->lock);
1175 
1176 		memset(queue_counter->per_link, 0,
1177 		       sizeof(queue_counter->per_link));
1178 
1179 		spin_unlock_bh(&queue_counter->lock);
1180 	}
1181 
1182 	/* Schedule the check in 5 seconds */
1183 	wiphy_delayed_work_queue(mld_vif->mld->wiphy,
1184 				 &mld_vif->emlsr.check_tpt_wk,
1185 				 round_jiffies_relative(IWL_MLD_TPT_COUNT_WINDOW));
1186 	IWL_DEBUG_EHT(mld, "TPT check enabled\n");
1187 }
1188 
1189 void iwl_mld_start_ignoring_tpt_updates(struct iwl_mld *mld)
1190 {
1191 	bool start = true;
1192 
1193 	ieee80211_iterate_active_interfaces_mtx(mld->hw,
1194 						IEEE80211_IFACE_ITER_NORMAL,
1195 						iwl_mld_ignore_tpt_iter,
1196 						&start);
1197 }
1198 
1199 void iwl_mld_stop_ignoring_tpt_updates(struct iwl_mld *mld)
1200 {
1201 	bool start = false;
1202 
1203 	ieee80211_iterate_active_interfaces_mtx(mld->hw,
1204 						IEEE80211_IFACE_ITER_NORMAL,
1205 						iwl_mld_ignore_tpt_iter,
1206 						&start);
1207 }
1208