xref: /freebsd/sys/contrib/dev/rtw89/ps.c (revision 354a030185c650d1465ed2035a83636b8f825d72)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright(c) 2019-2020  Realtek Corporation
3  */
4 
5 #include "chan.h"
6 #include "coex.h"
7 #include "core.h"
8 #include "debug.h"
9 #include "fw.h"
10 #include "mac.h"
11 #include "phy.h"
12 #include "ps.h"
13 #include "reg.h"
14 #include "util.h"
15 
rtw89_fw_receive_lps_h2c_check(struct rtw89_dev * rtwdev,u8 macid)16 static int rtw89_fw_receive_lps_h2c_check(struct rtw89_dev *rtwdev, u8 macid)
17 {
18 	struct rtw89_mac_c2h_info c2h_info = {};
19 	u16 c2hreg_macid;
20 	u32 c2hreg_ret;
21 	int ret;
22 
23 	if (!RTW89_CHK_FW_FEATURE(LPS_DACK_BY_C2H_REG, &rtwdev->fw))
24 		return 0;
25 
26 	c2h_info.id = RTW89_FWCMD_C2HREG_FUNC_PS_LEAVE_ACK;
27 	ret = rtw89_fw_msg_reg(rtwdev, NULL, &c2h_info);
28 	if (ret)
29 		return ret;
30 
31 	c2hreg_macid = u32_get_bits(c2h_info.u.c2hreg[0],
32 				    RTW89_C2HREG_PS_LEAVE_ACK_MACID);
33 	c2hreg_ret = u32_get_bits(c2h_info.u.c2hreg[1], RTW89_C2HREG_PS_LEAVE_ACK_RET);
34 
35 	if (macid != c2hreg_macid || c2hreg_ret)
36 		rtw89_warn(rtwdev, "rtw89: check lps h2c received by firmware fail\n");
37 
38 	return 0;
39 }
40 
rtw89_fw_leave_lps_check(struct rtw89_dev * rtwdev,u8 macid)41 static int rtw89_fw_leave_lps_check(struct rtw89_dev *rtwdev, u8 macid)
42 {
43 	const struct rtw89_mac_gen_def *mac = rtwdev->chip->mac_def;
44 	u32 pwr_en_bit = 0xE;
45 	u32 chk_msk = pwr_en_bit << (4 * macid);
46 	u32 polling;
47 	int ret;
48 
49 	ret = read_poll_timeout_atomic(rtw89_read32_mask, polling, !polling,
50 				       1000, 50000, false, rtwdev,
51 				       mac->ps_status, chk_msk);
52 	if (ret) {
53 		rtw89_info(rtwdev, "rtw89: failed to leave lps state\n");
54 		return -EBUSY;
55 	}
56 
57 	return 0;
58 }
59 
rtw89_ps_power_mode_change_with_hci(struct rtw89_dev * rtwdev,bool enter)60 static void rtw89_ps_power_mode_change_with_hci(struct rtw89_dev *rtwdev,
61 						bool enter)
62 {
63 	ieee80211_stop_queues(rtwdev->hw);
64 	rtwdev->hci.paused = true;
65 	flush_work(&rtwdev->txq_work);
66 	ieee80211_wake_queues(rtwdev->hw);
67 
68 	rtw89_hci_pause(rtwdev, true);
69 	rtw89_mac_power_mode_change(rtwdev, enter);
70 	rtw89_hci_switch_mode(rtwdev, enter);
71 	rtw89_hci_pause(rtwdev, false);
72 
73 	rtwdev->hci.paused = false;
74 
75 	if (!enter) {
76 		local_bh_disable();
77 		napi_schedule(&rtwdev->napi);
78 		local_bh_enable();
79 	}
80 }
81 
rtw89_ps_power_mode_change(struct rtw89_dev * rtwdev,bool enter)82 static void rtw89_ps_power_mode_change(struct rtw89_dev *rtwdev, bool enter)
83 {
84 	if (rtwdev->chip->low_power_hci_modes & BIT(rtwdev->ps_mode) &&
85 	    !test_bit(RTW89_FLAG_WOWLAN, rtwdev->flags))
86 		rtw89_ps_power_mode_change_with_hci(rtwdev, enter);
87 	else
88 		rtw89_mac_power_mode_change(rtwdev, enter);
89 }
90 
__rtw89_enter_ps_mode(struct rtw89_dev * rtwdev)91 void __rtw89_enter_ps_mode(struct rtw89_dev *rtwdev)
92 {
93 	if (!rtwdev->ps_mode)
94 		return;
95 
96 	if (test_and_set_bit(RTW89_FLAG_LOW_POWER_MODE, rtwdev->flags))
97 		return;
98 
99 	rtw89_ps_power_mode_change(rtwdev, true);
100 }
101 
__rtw89_leave_ps_mode(struct rtw89_dev * rtwdev)102 void __rtw89_leave_ps_mode(struct rtw89_dev *rtwdev)
103 {
104 	if (!rtwdev->ps_mode)
105 		return;
106 
107 	if (test_and_clear_bit(RTW89_FLAG_LOW_POWER_MODE, rtwdev->flags))
108 		rtw89_ps_power_mode_change(rtwdev, false);
109 }
110 
__rtw89_enter_lps_link(struct rtw89_dev * rtwdev,struct rtw89_vif_link * rtwvif_link)111 static void __rtw89_enter_lps_link(struct rtw89_dev *rtwdev,
112 				   struct rtw89_vif_link *rtwvif_link)
113 {
114 	struct rtw89_lps_parm lps_param = {
115 		.macid = rtwvif_link->mac_id,
116 		.psmode = RTW89_MAC_AX_PS_MODE_LEGACY,
117 		.lastrpwm = RTW89_LAST_RPWM_PS,
118 	};
119 
120 	rtw89_btc_ntfy_radio_state(rtwdev, BTC_RFCTRL_FW_CTRL);
121 	rtw89_fw_h2c_lps_parm(rtwdev, &lps_param);
122 }
123 
__rtw89_leave_lps(struct rtw89_dev * rtwdev,struct rtw89_vif_link * rtwvif_link)124 static void __rtw89_leave_lps(struct rtw89_dev *rtwdev,
125 			      struct rtw89_vif_link *rtwvif_link)
126 {
127 	struct rtw89_lps_parm lps_param = {
128 		.macid = rtwvif_link->mac_id,
129 		.psmode = RTW89_MAC_AX_PS_MODE_ACTIVE,
130 		.lastrpwm = RTW89_LAST_RPWM_ACTIVE,
131 	};
132 
133 	rtw89_fw_h2c_lps_parm(rtwdev, &lps_param);
134 	rtw89_fw_receive_lps_h2c_check(rtwdev, rtwvif_link->mac_id);
135 	rtw89_fw_leave_lps_check(rtwdev, rtwvif_link->mac_id);
136 	rtw89_btc_ntfy_radio_state(rtwdev, BTC_RFCTRL_WL_ON);
137 	rtw89_chip_digital_pwr_comp(rtwdev, rtwvif_link->phy_idx);
138 }
139 
rtw89_leave_ps_mode(struct rtw89_dev * rtwdev)140 void rtw89_leave_ps_mode(struct rtw89_dev *rtwdev)
141 {
142 	lockdep_assert_wiphy(rtwdev->hw->wiphy);
143 
144 	__rtw89_leave_ps_mode(rtwdev);
145 }
146 
rtw89_enter_lps(struct rtw89_dev * rtwdev,struct rtw89_vif * rtwvif,bool ps_mode)147 void rtw89_enter_lps(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif,
148 		     bool ps_mode)
149 {
150 	struct rtw89_vif_link *rtwvif_link;
151 	bool can_ps_mode = true;
152 	unsigned int link_id;
153 
154 	lockdep_assert_wiphy(rtwdev->hw->wiphy);
155 
156 	if (test_and_set_bit(RTW89_FLAG_LEISURE_PS, rtwdev->flags))
157 		return;
158 
159 	rtw89_vif_for_each_link(rtwvif, rtwvif_link, link_id) {
160 		__rtw89_enter_lps_link(rtwdev, rtwvif_link);
161 
162 		if (rtwvif_link->wifi_role == RTW89_WIFI_ROLE_P2P_CLIENT)
163 			can_ps_mode = false;
164 	}
165 
166 	rtw89_fw_h2c_rf_ps_info(rtwdev, rtwvif);
167 
168 	if (RTW89_CHK_FW_FEATURE(LPS_CH_INFO, &rtwdev->fw))
169 		rtw89_fw_h2c_lps_ch_info(rtwdev, rtwvif);
170 	else
171 		rtw89_fw_h2c_lps_ml_cmn_info(rtwdev, rtwvif);
172 
173 	if (ps_mode && can_ps_mode)
174 		__rtw89_enter_ps_mode(rtwdev);
175 }
176 
rtw89_leave_lps_vif(struct rtw89_dev * rtwdev,struct rtw89_vif_link * rtwvif_link)177 static void rtw89_leave_lps_vif(struct rtw89_dev *rtwdev,
178 				struct rtw89_vif_link *rtwvif_link)
179 {
180 	if (rtwvif_link->wifi_role != RTW89_WIFI_ROLE_STATION &&
181 	    rtwvif_link->wifi_role != RTW89_WIFI_ROLE_P2P_CLIENT)
182 		return;
183 
184 	__rtw89_leave_lps(rtwdev, rtwvif_link);
185 }
186 
rtw89_leave_lps(struct rtw89_dev * rtwdev)187 void rtw89_leave_lps(struct rtw89_dev *rtwdev)
188 {
189 	struct rtw89_vif_link *rtwvif_link;
190 	struct rtw89_vif *rtwvif;
191 	unsigned int link_id;
192 
193 	lockdep_assert_wiphy(rtwdev->hw->wiphy);
194 
195 	if (!test_and_clear_bit(RTW89_FLAG_LEISURE_PS, rtwdev->flags))
196 		return;
197 
198 	__rtw89_leave_ps_mode(rtwdev);
199 
200 	rtw89_phy_dm_reinit(rtwdev);
201 
202 	rtw89_for_each_rtwvif(rtwdev, rtwvif)
203 		rtw89_vif_for_each_link(rtwvif, rtwvif_link, link_id)
204 			rtw89_leave_lps_vif(rtwdev, rtwvif_link);
205 }
206 
rtw89_enter_ips(struct rtw89_dev * rtwdev)207 void rtw89_enter_ips(struct rtw89_dev *rtwdev)
208 {
209 	struct rtw89_vif_link *rtwvif_link;
210 	struct rtw89_vif *rtwvif;
211 	unsigned int link_id;
212 
213 	set_bit(RTW89_FLAG_INACTIVE_PS, rtwdev->flags);
214 
215 	if (!test_bit(RTW89_FLAG_POWERON, rtwdev->flags))
216 		return;
217 
218 	rtw89_for_each_rtwvif(rtwdev, rtwvif)
219 		rtw89_vif_for_each_link(rtwvif, rtwvif_link, link_id)
220 			rtw89_mac_vif_deinit(rtwdev, rtwvif_link);
221 
222 	rtw89_core_stop(rtwdev);
223 }
224 
rtw89_leave_ips(struct rtw89_dev * rtwdev)225 void rtw89_leave_ips(struct rtw89_dev *rtwdev)
226 {
227 	struct rtw89_vif_link *rtwvif_link;
228 	struct rtw89_vif *rtwvif;
229 	unsigned int link_id;
230 	int ret;
231 
232 	if (test_bit(RTW89_FLAG_POWERON, rtwdev->flags))
233 		return;
234 
235 	ret = rtw89_core_start(rtwdev);
236 	if (ret)
237 		rtw89_err(rtwdev, "failed to leave idle state\n");
238 
239 	rtw89_set_channel(rtwdev);
240 
241 	rtw89_for_each_rtwvif(rtwdev, rtwvif)
242 		rtw89_vif_for_each_link(rtwvif, rtwvif_link, link_id)
243 			rtw89_mac_vif_init(rtwdev, rtwvif_link);
244 
245 	clear_bit(RTW89_FLAG_INACTIVE_PS, rtwdev->flags);
246 }
247 
rtw89_set_coex_ctrl_lps(struct rtw89_dev * rtwdev,bool btc_ctrl)248 void rtw89_set_coex_ctrl_lps(struct rtw89_dev *rtwdev, bool btc_ctrl)
249 {
250 	if (btc_ctrl)
251 		rtw89_leave_lps(rtwdev);
252 }
253 
rtw89_tsf32_toggle(struct rtw89_dev * rtwdev,struct rtw89_vif_link * rtwvif_link,enum rtw89_p2pps_action act)254 static void rtw89_tsf32_toggle(struct rtw89_dev *rtwdev,
255 			       struct rtw89_vif_link *rtwvif_link,
256 			       enum rtw89_p2pps_action act)
257 {
258 	if (act == RTW89_P2P_ACT_UPDATE || act == RTW89_P2P_ACT_REMOVE)
259 		return;
260 
261 	if (act == RTW89_P2P_ACT_INIT)
262 		rtw89_fw_h2c_tsf32_toggle(rtwdev, rtwvif_link, true);
263 	else if (act == RTW89_P2P_ACT_TERMINATE)
264 		rtw89_fw_h2c_tsf32_toggle(rtwdev, rtwvif_link, false);
265 }
266 
rtw89_p2p_disable_all_noa(struct rtw89_dev * rtwdev,struct rtw89_vif_link * rtwvif_link,struct ieee80211_bss_conf * bss_conf)267 void rtw89_p2p_disable_all_noa(struct rtw89_dev *rtwdev,
268 			       struct rtw89_vif_link *rtwvif_link,
269 			       struct ieee80211_bss_conf *bss_conf)
270 {
271 	enum rtw89_p2pps_action act;
272 	u8 oppps_ctwindow;
273 	u8 noa_id;
274 
275 	rcu_read_lock();
276 
277 	if (!bss_conf)
278 		bss_conf = rtw89_vif_rcu_dereference_link(rtwvif_link, true);
279 
280 	oppps_ctwindow = bss_conf->p2p_noa_attr.oppps_ctwindow;
281 
282 	rcu_read_unlock();
283 
284 	if (rtwvif_link->last_noa_nr == 0)
285 		return;
286 
287 	for (noa_id = 0; noa_id < rtwvif_link->last_noa_nr; noa_id++) {
288 		if (noa_id == rtwvif_link->last_noa_nr - 1)
289 			act = RTW89_P2P_ACT_TERMINATE;
290 		else
291 			act = RTW89_P2P_ACT_REMOVE;
292 		rtw89_tsf32_toggle(rtwdev, rtwvif_link, act);
293 		rtw89_fw_h2c_p2p_act(rtwdev, rtwvif_link, NULL,
294 				     act, noa_id, oppps_ctwindow);
295 	}
296 }
297 
rtw89_p2p_update_noa(struct rtw89_dev * rtwdev,struct rtw89_vif_link * rtwvif_link,struct ieee80211_bss_conf * bss_conf)298 static void rtw89_p2p_update_noa(struct rtw89_dev *rtwdev,
299 				 struct rtw89_vif_link *rtwvif_link,
300 				 struct ieee80211_bss_conf *bss_conf)
301 {
302 	struct ieee80211_p2p_noa_desc *desc;
303 	enum rtw89_p2pps_action act;
304 	u8 noa_id;
305 
306 	for (noa_id = 0; noa_id < RTW89_P2P_MAX_NOA_NUM; noa_id++) {
307 		desc = &bss_conf->p2p_noa_attr.desc[noa_id];
308 		if (!desc->count || !desc->duration)
309 			break;
310 
311 		if (noa_id == 0)
312 			act = RTW89_P2P_ACT_INIT;
313 		else
314 			act = RTW89_P2P_ACT_UPDATE;
315 		rtw89_tsf32_toggle(rtwdev, rtwvif_link, act);
316 		rtw89_fw_h2c_p2p_act(rtwdev, rtwvif_link, desc, act, noa_id,
317 				     bss_conf->p2p_noa_attr.oppps_ctwindow);
318 	}
319 	rtwvif_link->last_noa_nr = noa_id;
320 }
321 
rtw89_process_p2p_ps(struct rtw89_dev * rtwdev,struct rtw89_vif_link * rtwvif_link,struct ieee80211_bss_conf * bss_conf)322 void rtw89_process_p2p_ps(struct rtw89_dev *rtwdev,
323 			  struct rtw89_vif_link *rtwvif_link,
324 			  struct ieee80211_bss_conf *bss_conf)
325 {
326 	rtw89_p2p_disable_all_noa(rtwdev, rtwvif_link, bss_conf);
327 	rtw89_p2p_update_noa(rtwdev, rtwvif_link, bss_conf);
328 }
329 
rtw89_recalc_lps(struct rtw89_dev * rtwdev)330 void rtw89_recalc_lps(struct rtw89_dev *rtwdev)
331 {
332 	struct ieee80211_vif *vif, *found_vif = NULL;
333 	struct rtw89_vif *rtwvif;
334 	enum rtw89_entity_mode mode;
335 	int count = 0;
336 
337 	mode = rtw89_get_entity_mode(rtwdev);
338 	if (mode == RTW89_ENTITY_MODE_MCC)
339 		goto disable_lps;
340 
341 	rtw89_for_each_rtwvif(rtwdev, rtwvif) {
342 		vif = rtwvif_to_vif(rtwvif);
343 
344 		if (vif->type != NL80211_IFTYPE_STATION) {
345 			count = 0;
346 			break;
347 		}
348 
349 		count++;
350 		found_vif = vif;
351 	}
352 
353 	if (count == 1 && found_vif->cfg.ps) {
354 		rtwdev->lps_enabled = true;
355 		return;
356 	}
357 
358 disable_lps:
359 	rtw89_leave_lps(rtwdev);
360 	rtwdev->lps_enabled = false;
361 }
362 
rtw89_p2p_noa_renew(struct rtw89_vif_link * rtwvif_link)363 void rtw89_p2p_noa_renew(struct rtw89_vif_link *rtwvif_link)
364 {
365 	struct rtw89_p2p_noa_setter *setter = &rtwvif_link->p2p_noa;
366 	struct rtw89_p2p_noa_ie *ie = &setter->ie;
367 	struct rtw89_p2p_ie_head *p2p_head = &ie->p2p_head;
368 	struct rtw89_noa_attr_head *noa_head = &ie->noa_head;
369 
370 	if (setter->noa_count) {
371 		setter->noa_index++;
372 		setter->noa_count = 0;
373 	}
374 
375 	memset(ie, 0, sizeof(*ie));
376 
377 	p2p_head->eid = WLAN_EID_VENDOR_SPECIFIC;
378 	p2p_head->ie_len = 4 + sizeof(*noa_head);
379 	p2p_head->oui[0] = (WLAN_OUI_WFA >> 16) & 0xff;
380 	p2p_head->oui[1] = (WLAN_OUI_WFA >> 8) & 0xff;
381 	p2p_head->oui[2] = (WLAN_OUI_WFA >> 0) & 0xff;
382 	p2p_head->oui_type = WLAN_OUI_TYPE_WFA_P2P;
383 
384 	noa_head->attr_type = IEEE80211_P2P_ATTR_ABSENCE_NOTICE;
385 	noa_head->attr_len = cpu_to_le16(2);
386 	noa_head->index = setter->noa_index;
387 	noa_head->oppps_ctwindow = 0;
388 }
389 
rtw89_p2p_noa_append(struct rtw89_vif_link * rtwvif_link,const struct ieee80211_p2p_noa_desc * desc)390 void rtw89_p2p_noa_append(struct rtw89_vif_link *rtwvif_link,
391 			  const struct ieee80211_p2p_noa_desc *desc)
392 {
393 	struct rtw89_p2p_noa_setter *setter = &rtwvif_link->p2p_noa;
394 	struct rtw89_p2p_noa_ie *ie = &setter->ie;
395 	struct rtw89_p2p_ie_head *p2p_head = &ie->p2p_head;
396 	struct rtw89_noa_attr_head *noa_head = &ie->noa_head;
397 
398 	if (!desc->count || !desc->duration)
399 		return;
400 
401 	if (setter->noa_count >= RTW89_P2P_MAX_NOA_NUM)
402 		return;
403 
404 	p2p_head->ie_len += sizeof(*desc);
405 	le16_add_cpu(&noa_head->attr_len, sizeof(*desc));
406 
407 	ie->noa_desc[setter->noa_count++] = *desc;
408 }
409 
rtw89_p2p_noa_fetch(struct rtw89_vif_link * rtwvif_link,void ** data)410 u8 rtw89_p2p_noa_fetch(struct rtw89_vif_link *rtwvif_link, void **data)
411 {
412 	struct rtw89_p2p_noa_setter *setter = &rtwvif_link->p2p_noa;
413 	struct rtw89_p2p_noa_ie *ie = &setter->ie;
414 #if defined(__linux__)
415 	void *tail;
416 #elif defined(__FreeBSD__)
417 	u8 *tail;
418 #endif
419 
420 	if (!setter->noa_count)
421 		return 0;
422 
423 	*data = ie;
424 #if defined(__linux__)
425 	tail = ie->noa_desc + setter->noa_count;
426 	return tail - *data;
427 #elif defined(__FreeBSD__)
428 	tail = (u8 *)ie->noa_desc + setter->noa_count;
429 	return tail - (u8 *)*data;
430 #endif
431 }
432 
rtw89_ps_noa_once_set_work(struct wiphy * wiphy,struct wiphy_work * work)433 static void rtw89_ps_noa_once_set_work(struct wiphy *wiphy, struct wiphy_work *work)
434 {
435 	struct rtw89_ps_noa_once_handler *noa_once =
436 		container_of(work, struct rtw89_ps_noa_once_handler, set_work.work);
437 
438 	lockdep_assert_wiphy(wiphy);
439 
440 	noa_once->in_duration = true;
441 }
442 
rtw89_ps_noa_once_clr_work(struct wiphy * wiphy,struct wiphy_work * work)443 static void rtw89_ps_noa_once_clr_work(struct wiphy *wiphy, struct wiphy_work *work)
444 {
445 	struct rtw89_ps_noa_once_handler *noa_once =
446 		container_of(work, struct rtw89_ps_noa_once_handler, clr_work.work);
447 	struct rtw89_vif_link *rtwvif_link =
448 		container_of(noa_once, struct rtw89_vif_link, noa_once);
449 	struct rtw89_dev *rtwdev = rtwvif_link->rtwvif->rtwdev;
450 
451 	lockdep_assert_wiphy(wiphy);
452 
453 	rtw89_fw_h2c_set_bcn_fltr_cfg(rtwdev, rtwvif_link, true);
454 	noa_once->in_duration = false;
455 }
456 
rtw89_p2p_noa_once_init(struct rtw89_vif_link * rtwvif_link)457 void rtw89_p2p_noa_once_init(struct rtw89_vif_link *rtwvif_link)
458 {
459 	struct rtw89_ps_noa_once_handler *noa_once = &rtwvif_link->noa_once;
460 
461 	noa_once->in_duration = false;
462 	noa_once->tsf_begin = 0;
463 	noa_once->tsf_end = 0;
464 
465 	wiphy_delayed_work_init(&noa_once->set_work, rtw89_ps_noa_once_set_work);
466 	wiphy_delayed_work_init(&noa_once->clr_work, rtw89_ps_noa_once_clr_work);
467 }
468 
rtw89_p2p_noa_once_cancel(struct rtw89_vif_link * rtwvif_link)469 static void rtw89_p2p_noa_once_cancel(struct rtw89_vif_link *rtwvif_link)
470 {
471 	struct rtw89_ps_noa_once_handler *noa_once = &rtwvif_link->noa_once;
472 	struct rtw89_dev *rtwdev = rtwvif_link->rtwvif->rtwdev;
473 	struct wiphy *wiphy = rtwdev->hw->wiphy;
474 
475 	wiphy_delayed_work_cancel(wiphy, &noa_once->set_work);
476 	wiphy_delayed_work_cancel(wiphy, &noa_once->clr_work);
477 }
478 
rtw89_p2p_noa_once_deinit(struct rtw89_vif_link * rtwvif_link)479 void rtw89_p2p_noa_once_deinit(struct rtw89_vif_link *rtwvif_link)
480 {
481 	rtw89_p2p_noa_once_cancel(rtwvif_link);
482 	rtw89_p2p_noa_once_init(rtwvif_link);
483 }
484 
rtw89_p2p_noa_once_recalc(struct rtw89_vif_link * rtwvif_link)485 void rtw89_p2p_noa_once_recalc(struct rtw89_vif_link *rtwvif_link)
486 {
487 	struct rtw89_ps_noa_once_handler *noa_once = &rtwvif_link->noa_once;
488 	struct rtw89_dev *rtwdev = rtwvif_link->rtwvif->rtwdev;
489 	const struct ieee80211_p2p_noa_desc *noa_desc;
490 	struct wiphy *wiphy = rtwdev->hw->wiphy;
491 	struct ieee80211_bss_conf *bss_conf;
492 	u64 tsf_begin = U64_MAX, tsf_end;
493 	u64 set_delay_us = 0;
494 	u64 clr_delay_us = 0;
495 	u32 start_time;
496 	u32 interval;
497 	u32 duration;
498 	u64 tsf;
499 	int ret;
500 	int i;
501 
502 	lockdep_assert_wiphy(wiphy);
503 
504 	ret = rtw89_mac_port_get_tsf(rtwdev, rtwvif_link, &tsf);
505 	if (ret) {
506 		rtw89_warn(rtwdev, "%s: failed to get tsf\n", __func__);
507 		return;
508 	}
509 
510 	rcu_read_lock();
511 
512 	bss_conf = rtw89_vif_rcu_dereference_link(rtwvif_link, true);
513 
514 	for (i = 0; i < ARRAY_SIZE(bss_conf->p2p_noa_attr.desc); i++) {
515 		bool first = tsf_begin == U64_MAX;
516 		u64 tmp;
517 
518 		noa_desc = &bss_conf->p2p_noa_attr.desc[i];
519 		if (noa_desc->count == 0 || noa_desc->count == 255)
520 			continue;
521 
522 		start_time = le32_to_cpu(noa_desc->start_time);
523 		interval = le32_to_cpu(noa_desc->interval);
524 		duration = le32_to_cpu(noa_desc->duration);
525 
526 		if (unlikely(duration == 0 ||
527 			     (noa_desc->count > 1 && interval == 0)))
528 			continue;
529 
530 		tmp = start_time + interval * (noa_desc->count - 1) + duration;
531 		tmp = (tsf & GENMASK_ULL(63, 32)) + tmp;
532 		if (unlikely(tmp <= tsf))
533 			continue;
534 		tsf_end = first ? tmp : max(tsf_end, tmp);
535 
536 		tmp = (tsf & GENMASK_ULL(63, 32)) | start_time;
537 		tsf_begin = first ? tmp : min(tsf_begin, tmp);
538 	}
539 
540 	rcu_read_unlock();
541 
542 	if (tsf_begin == U64_MAX)
543 		return;
544 
545 	rtw89_p2p_noa_once_cancel(rtwvif_link);
546 
547 	if (noa_once->tsf_end > tsf) {
548 		tsf_begin = min(tsf_begin, noa_once->tsf_begin);
549 		tsf_end = max(tsf_end, noa_once->tsf_end);
550 	}
551 
552 	clr_delay_us = min_t(u64, tsf_end - tsf, UINT_MAX);
553 
554 	if (tsf_begin <= tsf) {
555 		noa_once->in_duration = true;
556 		goto out;
557 	}
558 
559 	set_delay_us = tsf_begin - tsf;
560 	if (unlikely(set_delay_us > UINT_MAX)) {
561 		rtw89_warn(rtwdev, "%s: unhandled begin\n", __func__);
562 		set_delay_us = 0;
563 		clr_delay_us = 0;
564 		rtw89_fw_h2c_set_bcn_fltr_cfg(rtwdev, rtwvif_link, true);
565 		noa_once->in_duration = false;
566 	}
567 
568 out:
569 	if (set_delay_us)
570 		wiphy_delayed_work_queue(wiphy, &noa_once->set_work,
571 					 usecs_to_jiffies(set_delay_us));
572 	if (clr_delay_us)
573 		wiphy_delayed_work_queue(wiphy, &noa_once->clr_work,
574 					 usecs_to_jiffies(clr_delay_us));
575 
576 	noa_once->tsf_begin = tsf_begin;
577 	noa_once->tsf_end = tsf_end;
578 }
579