xref: /linux/drivers/net/wireless/realtek/rtw89/mac80211.c (revision d30c1683aaecb93d2ab95685dc4300a33d3cea7a)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright(c) 2019-2020  Realtek Corporation
3  */
4 
5 #include "cam.h"
6 #include "chan.h"
7 #include "coex.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 "sar.h"
15 #include "ser.h"
16 #include "util.h"
17 #include "wow.h"
18 
19 static void rtw89_ops_tx(struct ieee80211_hw *hw,
20 			 struct ieee80211_tx_control *control,
21 			 struct sk_buff *skb)
22 {
23 	struct rtw89_dev *rtwdev = hw->priv;
24 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
25 	struct ieee80211_vif *vif = info->control.vif;
26 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
27 	struct ieee80211_sta *sta = control->sta;
28 	u32 flags = IEEE80211_SKB_CB(skb)->flags;
29 	int ret, qsel;
30 
31 	if (rtwvif->offchan && !(flags & IEEE80211_TX_CTL_TX_OFFCHAN) && sta) {
32 		struct rtw89_sta *rtwsta = sta_to_rtwsta(sta);
33 
34 		rtw89_debug(rtwdev, RTW89_DBG_TXRX, "ops_tx during offchan\n");
35 		skb_queue_tail(&rtwsta->roc_queue, skb);
36 		return;
37 	}
38 
39 	ret = rtw89_core_tx_write(rtwdev, vif, sta, skb, &qsel);
40 	if (ret) {
41 		rtw89_err(rtwdev, "failed to transmit skb: %d\n", ret);
42 		ieee80211_free_txskb(hw, skb);
43 		return;
44 	}
45 	rtw89_core_tx_kick_off(rtwdev, qsel);
46 }
47 
48 static void rtw89_ops_wake_tx_queue(struct ieee80211_hw *hw,
49 				    struct ieee80211_txq *txq)
50 {
51 	struct rtw89_dev *rtwdev = hw->priv;
52 
53 	ieee80211_schedule_txq(hw, txq);
54 	queue_work(rtwdev->txq_wq, &rtwdev->txq_work);
55 }
56 
57 static int rtw89_ops_start(struct ieee80211_hw *hw)
58 {
59 	struct rtw89_dev *rtwdev = hw->priv;
60 
61 	lockdep_assert_wiphy(hw->wiphy);
62 
63 	return rtw89_core_start(rtwdev);
64 }
65 
66 static void rtw89_ops_stop(struct ieee80211_hw *hw, bool suspend)
67 {
68 	struct rtw89_dev *rtwdev = hw->priv;
69 
70 	lockdep_assert_wiphy(hw->wiphy);
71 
72 	rtw89_core_stop(rtwdev);
73 }
74 
75 static int rtw89_ops_config(struct ieee80211_hw *hw, int radio_idx, u32 changed)
76 {
77 	struct rtw89_dev *rtwdev = hw->priv;
78 
79 	lockdep_assert_wiphy(hw->wiphy);
80 
81 	/* let previous ips work finish to ensure we don't leave ips twice */
82 	wiphy_work_cancel(hw->wiphy, &rtwdev->ips_work);
83 
84 	rtw89_leave_ps_mode(rtwdev);
85 
86 	if ((changed & IEEE80211_CONF_CHANGE_IDLE) &&
87 	    !(hw->conf.flags & IEEE80211_CONF_IDLE))
88 		rtw89_leave_ips(rtwdev);
89 
90 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
91 		rtw89_config_entity_chandef(rtwdev, RTW89_CHANCTX_0,
92 					    &hw->conf.chandef);
93 		rtw89_set_channel(rtwdev);
94 	}
95 
96 	if ((changed & IEEE80211_CONF_CHANGE_IDLE) &&
97 	    (hw->conf.flags & IEEE80211_CONF_IDLE) &&
98 	    !rtwdev->scanning)
99 		rtw89_enter_ips(rtwdev);
100 
101 	return 0;
102 }
103 
104 static int __rtw89_ops_add_iface_link(struct rtw89_dev *rtwdev,
105 				      struct rtw89_vif_link *rtwvif_link)
106 {
107 	struct ieee80211_bss_conf *bss_conf;
108 	int ret;
109 
110 	rtw89_leave_ps_mode(rtwdev);
111 
112 	rtw89_vif_type_mapping(rtwvif_link, false);
113 
114 	wiphy_work_init(&rtwvif_link->update_beacon_work, rtw89_core_update_beacon_work);
115 	wiphy_delayed_work_init(&rtwvif_link->csa_beacon_work, rtw89_core_csa_beacon_work);
116 	wiphy_delayed_work_init(&rtwvif_link->mcc_gc_detect_beacon_work,
117 				rtw89_mcc_gc_detect_beacon_work);
118 
119 	INIT_LIST_HEAD(&rtwvif_link->general_pkt_list);
120 
121 	rtw89_p2p_noa_once_init(rtwvif_link);
122 
123 	rtwvif_link->hit_rule = 0;
124 	rtwvif_link->bcn_hit_cond = 0;
125 	rtwvif_link->chanctx_assigned = false;
126 	rtwvif_link->chanctx_idx = RTW89_CHANCTX_0;
127 	rtwvif_link->reg_6ghz_power = RTW89_REG_6GHZ_POWER_DFLT;
128 	rtwvif_link->rand_tsf_done = false;
129 	rtwvif_link->detect_bcn_count = 0;
130 
131 	rcu_read_lock();
132 
133 	bss_conf = rtw89_vif_rcu_dereference_link(rtwvif_link, true);
134 	ether_addr_copy(rtwvif_link->mac_addr, bss_conf->addr);
135 
136 	rcu_read_unlock();
137 
138 	ret = rtw89_mac_add_vif(rtwdev, rtwvif_link);
139 	if (ret)
140 		return ret;
141 
142 	rtw89_btc_ntfy_role_info(rtwdev, rtwvif_link, NULL, BTC_ROLE_START);
143 	return 0;
144 }
145 
146 static void __rtw89_ops_remove_iface_link(struct rtw89_dev *rtwdev,
147 					  struct rtw89_vif_link *rtwvif_link)
148 {
149 	lockdep_assert_wiphy(rtwdev->hw->wiphy);
150 
151 	wiphy_work_cancel(rtwdev->hw->wiphy, &rtwvif_link->update_beacon_work);
152 	wiphy_delayed_work_cancel(rtwdev->hw->wiphy, &rtwvif_link->csa_beacon_work);
153 	wiphy_delayed_work_cancel(rtwdev->hw->wiphy,
154 				  &rtwvif_link->mcc_gc_detect_beacon_work);
155 
156 	rtw89_p2p_noa_once_deinit(rtwvif_link);
157 
158 	rtw89_leave_ps_mode(rtwdev);
159 
160 	rtw89_btc_ntfy_role_info(rtwdev, rtwvif_link, NULL, BTC_ROLE_STOP);
161 
162 	rtw89_mac_remove_vif(rtwdev, rtwvif_link);
163 }
164 
165 static int rtw89_ops_add_interface(struct ieee80211_hw *hw,
166 				   struct ieee80211_vif *vif)
167 {
168 	struct rtw89_dev *rtwdev = hw->priv;
169 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
170 	struct rtw89_vif_link *rtwvif_link;
171 	u8 mac_id, port;
172 	int ret = 0;
173 
174 	lockdep_assert_wiphy(hw->wiphy);
175 
176 	rtw89_debug(rtwdev, RTW89_DBG_STATE, "add vif %pM type %d, p2p %d\n",
177 		    vif->addr, vif->type, vif->p2p);
178 
179 	rtw89_leave_ips_by_hwflags(rtwdev);
180 
181 	if (RTW89_CHK_FW_FEATURE(BEACON_FILTER, &rtwdev->fw))
182 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
183 				     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
184 
185 	mac_id = rtw89_acquire_mac_id(rtwdev);
186 	if (mac_id == RTW89_MAX_MAC_ID_NUM)
187 		return -ENOSPC;
188 
189 	port = rtw89_core_acquire_bit_map(rtwdev->hw_port, RTW89_PORT_NUM);
190 	if (port == RTW89_PORT_NUM) {
191 		ret = -ENOSPC;
192 		goto release_macid;
193 	}
194 
195 	rtw89_init_vif(rtwdev, rtwvif, mac_id, port);
196 
197 	rtw89_core_txq_init(rtwdev, vif->txq);
198 
199 	if (!rtw89_rtwvif_in_list(rtwdev, rtwvif)) {
200 		list_add_tail(&rtwvif->list, &rtwdev->rtwvifs_list);
201 		INIT_LIST_HEAD(&rtwvif->mgnt_entry);
202 		INIT_LIST_HEAD(&rtwvif->dlink_pool);
203 	}
204 
205 	ether_addr_copy(rtwvif->mac_addr, vif->addr);
206 
207 	rtwvif->offchan = false;
208 	rtwvif->roc.state = RTW89_ROC_IDLE;
209 	wiphy_delayed_work_init(&rtwvif->roc.roc_work, rtw89_roc_work);
210 
211 	rtw89_traffic_stats_init(rtwdev, &rtwvif->stats);
212 
213 	rtwvif_link = rtw89_vif_set_link(rtwvif, RTW89_VIF_IDLE_LINK_ID);
214 	if (!rtwvif_link) {
215 		ret = -EINVAL;
216 		goto release_port;
217 	}
218 
219 	ret = __rtw89_ops_add_iface_link(rtwdev, rtwvif_link);
220 	if (ret)
221 		goto unset_link;
222 
223 	rtwdev->pure_monitor_mode_vif = vif->type == NL80211_IFTYPE_MONITOR ?
224 					rtwvif : NULL;
225 	rtw89_recalc_lps(rtwdev);
226 	return 0;
227 
228 unset_link:
229 	rtw89_vif_unset_link(rtwvif, RTW89_VIF_IDLE_LINK_ID);
230 release_port:
231 	list_del_init(&rtwvif->list);
232 	rtw89_core_release_bit_map(rtwdev->hw_port, port);
233 release_macid:
234 	rtw89_release_mac_id(rtwdev, mac_id);
235 
236 	return ret;
237 }
238 
239 static void rtw89_ops_remove_interface(struct ieee80211_hw *hw,
240 				       struct ieee80211_vif *vif)
241 {
242 	struct rtw89_dev *rtwdev = hw->priv;
243 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
244 	u8 macid = rtw89_vif_get_main_macid(rtwvif);
245 	u8 port = rtw89_vif_get_main_port(rtwvif);
246 	struct rtw89_vif_link *rtwvif_link;
247 
248 	lockdep_assert_wiphy(hw->wiphy);
249 
250 	rtw89_debug(rtwdev, RTW89_DBG_STATE, "remove vif %pM type %d p2p %d\n",
251 		    vif->addr, vif->type, vif->p2p);
252 
253 	wiphy_delayed_work_cancel(hw->wiphy, &rtwvif->roc.roc_work);
254 
255 	rtwvif_link = rtwvif->links[RTW89_VIF_IDLE_LINK_ID];
256 	if (unlikely(!rtwvif_link)) {
257 		rtw89_err(rtwdev,
258 			  "%s: rtwvif link (link_id %u) is not active\n",
259 			  __func__, RTW89_VIF_IDLE_LINK_ID);
260 		goto bottom;
261 	}
262 
263 	__rtw89_ops_remove_iface_link(rtwdev, rtwvif_link);
264 
265 	rtw89_vif_unset_link(rtwvif, RTW89_VIF_IDLE_LINK_ID);
266 
267 bottom:
268 	list_del_init(&rtwvif->list);
269 	rtw89_core_release_bit_map(rtwdev->hw_port, port);
270 	rtw89_release_mac_id(rtwdev, macid);
271 
272 	rtwdev->pure_monitor_mode_vif = NULL;
273 
274 	rtw89_recalc_lps(rtwdev);
275 	rtw89_enter_ips_by_hwflags(rtwdev);
276 }
277 
278 static int rtw89_ops_change_interface(struct ieee80211_hw *hw,
279 				      struct ieee80211_vif *vif,
280 				      enum nl80211_iftype type, bool p2p)
281 {
282 	struct rtw89_dev *rtwdev = hw->priv;
283 	int ret;
284 
285 	set_bit(RTW89_FLAG_CHANGING_INTERFACE, rtwdev->flags);
286 
287 	rtw89_debug(rtwdev, RTW89_DBG_STATE, "change vif %pM (%d)->(%d), p2p (%d)->(%d)\n",
288 		    vif->addr, vif->type, type, vif->p2p, p2p);
289 
290 	rtw89_ops_remove_interface(hw, vif);
291 
292 	vif->type = type;
293 	vif->p2p = p2p;
294 
295 	ret = rtw89_ops_add_interface(hw, vif);
296 	if (ret)
297 		rtw89_warn(rtwdev, "failed to change interface %d\n", ret);
298 
299 	clear_bit(RTW89_FLAG_CHANGING_INTERFACE, rtwdev->flags);
300 
301 	return ret;
302 }
303 
304 static void rtw89_ops_configure_filter(struct ieee80211_hw *hw,
305 				       unsigned int changed_flags,
306 				       unsigned int *new_flags,
307 				       u64 multicast)
308 {
309 	struct rtw89_dev *rtwdev = hw->priv;
310 	u32 rx_fltr;
311 
312 	lockdep_assert_wiphy(hw->wiphy);
313 
314 	rtw89_leave_ps_mode(rtwdev);
315 
316 	*new_flags &= FIF_ALLMULTI | FIF_OTHER_BSS | FIF_FCSFAIL |
317 		      FIF_BCN_PRBRESP_PROMISC | FIF_PROBE_REQ;
318 
319 	if (changed_flags & FIF_ALLMULTI) {
320 		if (*new_flags & FIF_ALLMULTI)
321 			rtwdev->hal.rx_fltr &= ~B_AX_A_MC;
322 		else
323 			rtwdev->hal.rx_fltr |= B_AX_A_MC;
324 	}
325 	if (changed_flags & FIF_FCSFAIL) {
326 		if (*new_flags & FIF_FCSFAIL)
327 			rtwdev->hal.rx_fltr |= B_AX_A_CRC32_ERR;
328 		else
329 			rtwdev->hal.rx_fltr &= ~B_AX_A_CRC32_ERR;
330 	}
331 	if (changed_flags & FIF_OTHER_BSS) {
332 		if (*new_flags & FIF_OTHER_BSS)
333 			rtwdev->hal.rx_fltr &= ~B_AX_A_A1_MATCH;
334 		else
335 			rtwdev->hal.rx_fltr |= B_AX_A_A1_MATCH;
336 	}
337 	if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
338 		if (*new_flags & FIF_BCN_PRBRESP_PROMISC) {
339 			rtwdev->hal.rx_fltr &= ~B_AX_A_BCN_CHK_EN;
340 			rtwdev->hal.rx_fltr &= ~B_AX_A_BC;
341 			rtwdev->hal.rx_fltr &= ~B_AX_A_A1_MATCH;
342 		} else {
343 			rtwdev->hal.rx_fltr |= B_AX_A_BCN_CHK_EN;
344 			rtwdev->hal.rx_fltr |= B_AX_A_BC;
345 			rtwdev->hal.rx_fltr |= B_AX_A_A1_MATCH;
346 		}
347 	}
348 	if (changed_flags & FIF_PROBE_REQ) {
349 		if (*new_flags & FIF_PROBE_REQ) {
350 			rtwdev->hal.rx_fltr &= ~B_AX_A_BC_CAM_MATCH;
351 			rtwdev->hal.rx_fltr &= ~B_AX_A_UC_CAM_MATCH;
352 		} else {
353 			rtwdev->hal.rx_fltr |= B_AX_A_BC_CAM_MATCH;
354 			rtwdev->hal.rx_fltr |= B_AX_A_UC_CAM_MATCH;
355 		}
356 	}
357 
358 	rx_fltr = rtwdev->hal.rx_fltr;
359 
360 	/* mac80211 doesn't configure filter when HW scan, driver need to
361 	 * set by itself. However, during P2P scan might have configure
362 	 * filter to overwrite filter that HW scan needed, so we need to
363 	 * check scan and append related filter
364 	 */
365 	if (rtwdev->scanning) {
366 		rx_fltr &= ~B_AX_A_BCN_CHK_EN;
367 		rx_fltr &= ~B_AX_A_BC;
368 		rx_fltr &= ~B_AX_A_A1_MATCH;
369 	}
370 
371 	rtw89_mac_set_rx_fltr(rtwdev, RTW89_MAC_0, rx_fltr);
372 	if (!rtwdev->dbcc_en)
373 		return;
374 	rtw89_mac_set_rx_fltr(rtwdev, RTW89_MAC_1, rx_fltr);
375 }
376 
377 static const u8 ac_to_fw_idx[IEEE80211_NUM_ACS] = {
378 	[IEEE80211_AC_VO] = 3,
379 	[IEEE80211_AC_VI] = 2,
380 	[IEEE80211_AC_BE] = 0,
381 	[IEEE80211_AC_BK] = 1,
382 };
383 
384 static u8 rtw89_aifsn_to_aifs(struct rtw89_dev *rtwdev,
385 			      struct rtw89_vif_link *rtwvif_link, u8 aifsn)
386 {
387 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev,
388 						       rtwvif_link->chanctx_idx);
389 	struct ieee80211_bss_conf *bss_conf;
390 	u8 slot_time;
391 	u8 sifs;
392 
393 	rcu_read_lock();
394 
395 	bss_conf = rtw89_vif_rcu_dereference_link(rtwvif_link, true);
396 	slot_time = bss_conf->use_short_slot ? 9 : 20;
397 
398 	rcu_read_unlock();
399 
400 	sifs = chan->band_type == RTW89_BAND_2G ? 10 : 16;
401 
402 	return aifsn * slot_time + sifs;
403 }
404 
405 static void ____rtw89_conf_tx_edca(struct rtw89_dev *rtwdev,
406 				   struct rtw89_vif_link *rtwvif_link, u16 ac)
407 {
408 	struct ieee80211_tx_queue_params *params = &rtwvif_link->tx_params[ac];
409 	u32 val;
410 	u8 ecw_max, ecw_min;
411 	u8 aifs;
412 
413 	/* 2^ecw - 1 = cw; ecw = log2(cw + 1) */
414 	ecw_max = ilog2(params->cw_max + 1);
415 	ecw_min = ilog2(params->cw_min + 1);
416 	aifs = rtw89_aifsn_to_aifs(rtwdev, rtwvif_link, params->aifs);
417 	val = FIELD_PREP(FW_EDCA_PARAM_TXOPLMT_MSK, params->txop) |
418 	      FIELD_PREP(FW_EDCA_PARAM_CWMAX_MSK, ecw_max) |
419 	      FIELD_PREP(FW_EDCA_PARAM_CWMIN_MSK, ecw_min) |
420 	      FIELD_PREP(FW_EDCA_PARAM_AIFS_MSK, aifs);
421 	rtw89_fw_h2c_set_edca(rtwdev, rtwvif_link, ac_to_fw_idx[ac], val);
422 }
423 
424 #define R_MUEDCA_ACS_PARAM(acs) {R_AX_MUEDCA_ ## acs ## _PARAM_0, \
425 				 R_BE_MUEDCA_ ## acs ## _PARAM_0}
426 
427 static const u32 ac_to_mu_edca_param[IEEE80211_NUM_ACS][RTW89_CHIP_GEN_NUM] = {
428 	[IEEE80211_AC_VO] = R_MUEDCA_ACS_PARAM(VO),
429 	[IEEE80211_AC_VI] = R_MUEDCA_ACS_PARAM(VI),
430 	[IEEE80211_AC_BE] = R_MUEDCA_ACS_PARAM(BE),
431 	[IEEE80211_AC_BK] = R_MUEDCA_ACS_PARAM(BK),
432 };
433 
434 static void ____rtw89_conf_tx_mu_edca(struct rtw89_dev *rtwdev,
435 				      struct rtw89_vif_link *rtwvif_link, u16 ac)
436 {
437 	struct ieee80211_tx_queue_params *params = &rtwvif_link->tx_params[ac];
438 	struct ieee80211_he_mu_edca_param_ac_rec *mu_edca;
439 	int gen = rtwdev->chip->chip_gen;
440 	u8 aifs, aifsn;
441 	u16 timer_32us;
442 	u32 reg;
443 	u32 val;
444 
445 	if (!params->mu_edca)
446 		return;
447 
448 	mu_edca = &params->mu_edca_param_rec;
449 	aifsn = FIELD_GET(GENMASK(3, 0), mu_edca->aifsn);
450 	aifs = aifsn ? rtw89_aifsn_to_aifs(rtwdev, rtwvif_link, aifsn) : 0;
451 	timer_32us = mu_edca->mu_edca_timer << 8;
452 
453 	val = FIELD_PREP(B_AX_MUEDCA_BE_PARAM_0_TIMER_MASK, timer_32us) |
454 	      FIELD_PREP(B_AX_MUEDCA_BE_PARAM_0_CW_MASK, mu_edca->ecw_min_max) |
455 	      FIELD_PREP(B_AX_MUEDCA_BE_PARAM_0_AIFS_MASK, aifs);
456 	reg = rtw89_mac_reg_by_idx(rtwdev, ac_to_mu_edca_param[ac][gen],
457 				   rtwvif_link->mac_idx);
458 	rtw89_write32(rtwdev, reg, val);
459 
460 	rtw89_mac_set_hw_muedca_ctrl(rtwdev, rtwvif_link, true);
461 }
462 
463 static void __rtw89_conf_tx(struct rtw89_dev *rtwdev,
464 			    struct rtw89_vif_link *rtwvif_link, u16 ac)
465 {
466 	____rtw89_conf_tx_edca(rtwdev, rtwvif_link, ac);
467 	____rtw89_conf_tx_mu_edca(rtwdev, rtwvif_link, ac);
468 }
469 
470 static void rtw89_conf_tx(struct rtw89_dev *rtwdev,
471 			  struct rtw89_vif_link *rtwvif_link)
472 {
473 	u16 ac;
474 
475 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
476 		__rtw89_conf_tx(rtwdev, rtwvif_link, ac);
477 }
478 
479 static int __rtw89_ops_sta_add(struct rtw89_dev *rtwdev,
480 			       struct ieee80211_vif *vif,
481 			       struct ieee80211_sta *sta)
482 {
483 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
484 	struct rtw89_sta *rtwsta = sta_to_rtwsta(sta);
485 	struct rtw89_vif_link *rtwvif_link;
486 	struct rtw89_sta_link *rtwsta_link;
487 	bool acquire_macid = false;
488 	u8 macid;
489 	int ret;
490 	int i;
491 
492 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
493 		rtwvif->mlo_mode = RTW89_MLO_MODE_MLSR;
494 
495 		/* for station mode, assign the mac_id from itself */
496 		macid = rtw89_vif_get_main_macid(rtwvif);
497 	} else {
498 		macid = rtw89_acquire_mac_id(rtwdev);
499 		if (macid == RTW89_MAX_MAC_ID_NUM)
500 			return -ENOSPC;
501 
502 		acquire_macid = true;
503 	}
504 
505 	rtw89_init_sta(rtwdev, rtwvif, rtwsta, macid);
506 
507 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++)
508 		rtw89_core_txq_init(rtwdev, sta->txq[i]);
509 
510 	INIT_LIST_HEAD(&rtwsta->dlink_pool);
511 
512 	skb_queue_head_init(&rtwsta->roc_queue);
513 	bitmap_zero(rtwsta->pairwise_sec_cam_map, RTW89_MAX_SEC_CAM_NUM);
514 
515 	rtwsta_link = rtw89_sta_set_link(rtwsta, sta->deflink.link_id);
516 	if (!rtwsta_link) {
517 		ret = -EINVAL;
518 		goto err;
519 	}
520 
521 	rtwvif_link = rtwsta_link->rtwvif_link;
522 
523 	ret = rtw89_core_sta_link_add(rtwdev, rtwvif_link, rtwsta_link);
524 	if (ret)
525 		goto unset_link;
526 
527 	if (vif->type == NL80211_IFTYPE_AP || sta->tdls)
528 		rtw89_queue_chanctx_change(rtwdev, RTW89_CHANCTX_REMOTE_STA_CHANGE);
529 
530 	return 0;
531 
532 unset_link:
533 	rtw89_sta_unset_link(rtwsta, sta->deflink.link_id);
534 err:
535 	if (acquire_macid)
536 		rtw89_release_mac_id(rtwdev, macid);
537 
538 	return ret;
539 }
540 
541 static int __rtw89_ops_sta_assoc(struct rtw89_dev *rtwdev,
542 				 struct ieee80211_vif *vif,
543 				 struct ieee80211_sta *sta,
544 				 bool station_mode)
545 {
546 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
547 	struct rtw89_sta *rtwsta = sta_to_rtwsta(sta);
548 	struct rtw89_vif_link *rtwvif_link;
549 	struct rtw89_sta_link *rtwsta_link;
550 	unsigned int link_id;
551 	int ret;
552 
553 	rtw89_sta_for_each_link(rtwsta, rtwsta_link, link_id) {
554 		rtwvif_link = rtwsta_link->rtwvif_link;
555 
556 		if (station_mode)
557 			rtw89_vif_type_mapping(rtwvif_link, true);
558 
559 		ret = rtw89_core_sta_link_assoc(rtwdev, rtwvif_link, rtwsta_link);
560 		if (ret)
561 			return ret;
562 	}
563 
564 	rtwdev->total_sta_assoc++;
565 	if (sta->tdls)
566 		rtwvif->tdls_peer++;
567 
568 	return 0;
569 }
570 
571 static int __rtw89_ops_sta_disassoc(struct rtw89_dev *rtwdev,
572 				    struct ieee80211_vif *vif,
573 				    struct ieee80211_sta *sta)
574 {
575 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
576 	struct rtw89_sta *rtwsta = sta_to_rtwsta(sta);
577 	struct rtw89_vif_link *rtwvif_link;
578 	struct rtw89_sta_link *rtwsta_link;
579 	unsigned int link_id;
580 	int ret;
581 
582 	rtw89_sta_for_each_link(rtwsta, rtwsta_link, link_id) {
583 		rtwvif_link = rtwsta_link->rtwvif_link;
584 		ret = rtw89_core_sta_link_disassoc(rtwdev, rtwvif_link, rtwsta_link);
585 		if (ret)
586 			return ret;
587 	}
588 
589 	rtwsta->disassoc = true;
590 
591 	rtwdev->total_sta_assoc--;
592 	if (sta->tdls)
593 		rtwvif->tdls_peer--;
594 
595 	return 0;
596 }
597 
598 static int __rtw89_ops_sta_disconnect(struct rtw89_dev *rtwdev,
599 				      struct ieee80211_vif *vif,
600 				      struct ieee80211_sta *sta)
601 {
602 	struct rtw89_sta *rtwsta = sta_to_rtwsta(sta);
603 	struct rtw89_vif_link *rtwvif_link;
604 	struct rtw89_sta_link *rtwsta_link;
605 	unsigned int link_id;
606 	int ret;
607 
608 	rtw89_core_free_sta_pending_ba(rtwdev, sta);
609 	rtw89_core_free_sta_pending_forbid_ba(rtwdev, sta);
610 	rtw89_core_free_sta_pending_roc_tx(rtwdev, sta);
611 
612 	rtw89_sta_for_each_link(rtwsta, rtwsta_link, link_id) {
613 		rtwvif_link = rtwsta_link->rtwvif_link;
614 		ret = rtw89_core_sta_link_disconnect(rtwdev, rtwvif_link, rtwsta_link);
615 		if (ret)
616 			return ret;
617 	}
618 
619 	return 0;
620 }
621 
622 static int __rtw89_ops_sta_remove(struct rtw89_dev *rtwdev,
623 				  struct ieee80211_vif *vif,
624 				  struct ieee80211_sta *sta)
625 {
626 	struct rtw89_sta *rtwsta = sta_to_rtwsta(sta);
627 	u8 macid = rtw89_sta_get_main_macid(rtwsta);
628 	struct rtw89_vif_link *rtwvif_link;
629 	struct rtw89_sta_link *rtwsta_link;
630 	unsigned int link_id;
631 	int ret;
632 
633 	rtw89_sta_for_each_link(rtwsta, rtwsta_link, link_id) {
634 		rtwvif_link = rtwsta_link->rtwvif_link;
635 		ret = rtw89_core_sta_link_remove(rtwdev, rtwvif_link, rtwsta_link);
636 		if (ret)
637 			return ret;
638 
639 		rtw89_sta_unset_link(rtwsta, link_id);
640 	}
641 
642 	if (vif->type == NL80211_IFTYPE_AP || sta->tdls) {
643 		rtw89_release_mac_id(rtwdev, macid);
644 		rtw89_queue_chanctx_change(rtwdev, RTW89_CHANCTX_REMOTE_STA_CHANGE);
645 	}
646 
647 	return 0;
648 }
649 
650 static void rtw89_station_mode_sta_assoc(struct rtw89_dev *rtwdev,
651 					 struct ieee80211_vif *vif)
652 {
653 	struct ieee80211_sta *sta;
654 
655 	if (vif->type != NL80211_IFTYPE_STATION)
656 		return;
657 
658 	sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
659 	if (!sta) {
660 		rtw89_err(rtwdev, "can't find sta to set sta_assoc state\n");
661 		return;
662 	}
663 
664 	__rtw89_ops_sta_assoc(rtwdev, vif, sta, true);
665 }
666 
667 static void __rtw89_ops_bss_link_assoc(struct rtw89_dev *rtwdev,
668 				       struct rtw89_vif_link *rtwvif_link)
669 {
670 	rtw89_phy_set_bss_color(rtwdev, rtwvif_link);
671 	rtw89_chip_cfg_txpwr_ul_tb_offset(rtwdev, rtwvif_link);
672 	rtw89_mac_port_update(rtwdev, rtwvif_link);
673 	rtw89_mac_set_he_obss_narrow_bw_ru(rtwdev, rtwvif_link);
674 	rtw89_mac_set_he_tb(rtwdev, rtwvif_link);
675 }
676 
677 static void __rtw89_ops_bss_assoc(struct rtw89_dev *rtwdev,
678 				  struct ieee80211_vif *vif)
679 {
680 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
681 	struct rtw89_vif_link *rtwvif_link;
682 	unsigned int link_id;
683 
684 	rtw89_vif_for_each_link(rtwvif, rtwvif_link, link_id)
685 		__rtw89_ops_bss_link_assoc(rtwdev, rtwvif_link);
686 }
687 
688 static void rtw89_ops_vif_cfg_changed(struct ieee80211_hw *hw,
689 				      struct ieee80211_vif *vif, u64 changed)
690 {
691 	struct rtw89_dev *rtwdev = hw->priv;
692 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
693 
694 	lockdep_assert_wiphy(hw->wiphy);
695 
696 	rtw89_leave_ps_mode(rtwdev);
697 
698 	if (changed & BSS_CHANGED_ASSOC) {
699 		if (vif->cfg.assoc) {
700 			rtw89_station_mode_sta_assoc(rtwdev, vif);
701 			__rtw89_ops_bss_assoc(rtwdev, vif);
702 
703 			rtw89_queue_chanctx_work(rtwdev);
704 		} else {
705 			/* Abort ongoing scan if cancel_scan isn't issued
706 			 * when disconnected by peer
707 			 */
708 			if (rtwdev->scanning)
709 				rtw89_hw_scan_abort(rtwdev, rtwdev->scan_info.scanning_vif);
710 		}
711 	}
712 
713 	if (changed & BSS_CHANGED_PS)
714 		rtw89_recalc_lps(rtwdev);
715 
716 	if (changed & BSS_CHANGED_ARP_FILTER)
717 		rtwvif->ip_addr = vif->cfg.arp_addr_list[0];
718 
719 	if (changed & BSS_CHANGED_MLD_VALID_LINKS) {
720 		struct rtw89_vif_link *cur = rtw89_get_designated_link(rtwvif);
721 
722 		rtw89_chip_rfk_channel(rtwdev, cur);
723 
724 		if (hweight16(vif->active_links) == 1)
725 			rtwvif->mlo_mode = RTW89_MLO_MODE_MLSR;
726 		else
727 			rtwvif->mlo_mode = RTW89_MLO_MODE_EMLSR;
728 	}
729 }
730 
731 static void rtw89_ops_link_info_changed(struct ieee80211_hw *hw,
732 					struct ieee80211_vif *vif,
733 					struct ieee80211_bss_conf *conf,
734 					u64 changed)
735 {
736 	struct rtw89_dev *rtwdev = hw->priv;
737 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
738 	struct rtw89_vif_link *rtwvif_link;
739 
740 	lockdep_assert_wiphy(hw->wiphy);
741 
742 	rtw89_leave_ps_mode(rtwdev);
743 
744 	rtwvif_link = rtwvif->links[conf->link_id];
745 	if (unlikely(!rtwvif_link)) {
746 		rtw89_err(rtwdev,
747 			  "%s: rtwvif link (link_id %u) is not active\n",
748 			  __func__, conf->link_id);
749 		return;
750 	}
751 
752 	if (changed & BSS_CHANGED_BSSID) {
753 		ether_addr_copy(rtwvif_link->bssid, conf->bssid);
754 		rtw89_cam_bssid_changed(rtwdev, rtwvif_link);
755 		rtw89_fw_h2c_cam(rtwdev, rtwvif_link, NULL, NULL, RTW89_ROLE_INFO_CHANGE);
756 		WRITE_ONCE(rtwvif_link->sync_bcn_tsf, 0);
757 	}
758 
759 	if (changed & BSS_CHANGED_BEACON)
760 		rtw89_chip_h2c_update_beacon(rtwdev, rtwvif_link);
761 
762 	if (changed & BSS_CHANGED_ERP_SLOT)
763 		rtw89_conf_tx(rtwdev, rtwvif_link);
764 
765 	if (changed & BSS_CHANGED_HE_BSS_COLOR)
766 		rtw89_phy_set_bss_color(rtwdev, rtwvif_link);
767 
768 	if (changed & BSS_CHANGED_MU_GROUPS)
769 		rtw89_mac_bf_set_gid_table(rtwdev, vif, conf);
770 
771 	if (changed & BSS_CHANGED_P2P_PS)
772 		rtw89_core_update_p2p_ps(rtwdev, rtwvif_link, conf);
773 
774 	if (changed & BSS_CHANGED_CQM)
775 		rtw89_fw_h2c_set_bcn_fltr_cfg(rtwdev, rtwvif_link, true);
776 
777 	if (changed & BSS_CHANGED_TPE)
778 		rtw89_reg_6ghz_recalc(rtwdev, rtwvif_link, true);
779 }
780 
781 static int rtw89_ops_start_ap(struct ieee80211_hw *hw,
782 			      struct ieee80211_vif *vif,
783 			      struct ieee80211_bss_conf *link_conf)
784 {
785 	struct rtw89_dev *rtwdev = hw->priv;
786 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
787 	struct rtw89_vif_link *rtwvif_link;
788 	const struct rtw89_chan *chan;
789 	int ret = 0;
790 
791 	lockdep_assert_wiphy(hw->wiphy);
792 
793 	rtwvif_link = rtwvif->links[link_conf->link_id];
794 	if (unlikely(!rtwvif_link)) {
795 		rtw89_err(rtwdev,
796 			  "%s: rtwvif link (link_id %u) is not active\n",
797 			  __func__, link_conf->link_id);
798 		return -ENOLINK;
799 	}
800 
801 	chan = rtw89_chan_get(rtwdev, rtwvif_link->chanctx_idx);
802 	if (chan->band_type == RTW89_BAND_6G)
803 		return -EOPNOTSUPP;
804 
805 	if (rtwdev->scanning)
806 		rtw89_hw_scan_abort(rtwdev, rtwdev->scan_info.scanning_vif);
807 
808 	ether_addr_copy(rtwvif_link->bssid, link_conf->bssid);
809 	rtw89_cam_bssid_changed(rtwdev, rtwvif_link);
810 	rtw89_mac_port_update(rtwdev, rtwvif_link);
811 	rtw89_chip_h2c_assoc_cmac_tbl(rtwdev, rtwvif_link, NULL);
812 	rtw89_fw_h2c_role_maintain(rtwdev, rtwvif_link, NULL, RTW89_ROLE_TYPE_CHANGE);
813 	rtw89_fw_h2c_join_info(rtwdev, rtwvif_link, NULL, true);
814 	rtw89_fw_h2c_cam(rtwdev, rtwvif_link, NULL, NULL, RTW89_ROLE_TYPE_CHANGE);
815 	rtw89_chip_rfk_channel(rtwdev, rtwvif_link);
816 
817 	if (RTW89_CHK_FW_FEATURE(NOTIFY_AP_INFO, &rtwdev->fw)) {
818 		ret = rtw89_fw_h2c_ap_info_refcount(rtwdev, true);
819 		if (ret)
820 			return ret;
821 	}
822 
823 	rtw89_queue_chanctx_work(rtwdev);
824 
825 	return 0;
826 }
827 
828 static
829 void rtw89_ops_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
830 		       struct ieee80211_bss_conf *link_conf)
831 {
832 	struct rtw89_dev *rtwdev = hw->priv;
833 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
834 	struct rtw89_vif_link *rtwvif_link;
835 
836 	lockdep_assert_wiphy(hw->wiphy);
837 
838 	rtwvif_link = rtwvif->links[link_conf->link_id];
839 	if (unlikely(!rtwvif_link)) {
840 		rtw89_err(rtwdev,
841 			  "%s: rtwvif link (link_id %u) is not active\n",
842 			  __func__, link_conf->link_id);
843 		return;
844 	}
845 
846 	if (RTW89_CHK_FW_FEATURE(NOTIFY_AP_INFO, &rtwdev->fw))
847 		rtw89_fw_h2c_ap_info_refcount(rtwdev, false);
848 
849 	rtw89_mac_stop_ap(rtwdev, rtwvif_link);
850 	rtw89_chip_h2c_assoc_cmac_tbl(rtwdev, rtwvif_link, NULL);
851 	rtw89_fw_h2c_join_info(rtwdev, rtwvif_link, NULL, true);
852 }
853 
854 static int rtw89_ops_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
855 			     bool set)
856 {
857 	struct rtw89_sta *rtwsta = sta_to_rtwsta(sta);
858 	struct rtw89_vif *rtwvif = rtwsta->rtwvif;
859 	struct rtw89_vif_link *rtwvif_link;
860 	unsigned int link_id;
861 
862 	rtw89_vif_for_each_link(rtwvif, rtwvif_link, link_id)
863 		wiphy_work_queue(hw->wiphy, &rtwvif_link->update_beacon_work);
864 
865 	return 0;
866 }
867 
868 static int rtw89_ops_conf_tx(struct ieee80211_hw *hw,
869 			     struct ieee80211_vif *vif,
870 			     unsigned int link_id, u16 ac,
871 			     const struct ieee80211_tx_queue_params *params)
872 {
873 	struct rtw89_dev *rtwdev = hw->priv;
874 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
875 	struct rtw89_vif_link *rtwvif_link;
876 
877 	lockdep_assert_wiphy(hw->wiphy);
878 
879 	rtw89_leave_ps_mode(rtwdev);
880 
881 	rtwvif_link = rtwvif->links[link_id];
882 	if (unlikely(!rtwvif_link)) {
883 		rtw89_err(rtwdev,
884 			  "%s: rtwvif link (link_id %u) is not active\n",
885 			  __func__, link_id);
886 		return -ENOLINK;
887 	}
888 
889 	rtwvif_link->tx_params[ac] = *params;
890 	__rtw89_conf_tx(rtwdev, rtwvif_link, ac);
891 
892 	return 0;
893 }
894 
895 static int __rtw89_ops_sta_state(struct ieee80211_hw *hw,
896 				 struct ieee80211_vif *vif,
897 				 struct ieee80211_sta *sta,
898 				 enum ieee80211_sta_state old_state,
899 				 enum ieee80211_sta_state new_state)
900 {
901 	struct rtw89_dev *rtwdev = hw->priv;
902 
903 	if (old_state == IEEE80211_STA_NOTEXIST &&
904 	    new_state == IEEE80211_STA_NONE)
905 		return __rtw89_ops_sta_add(rtwdev, vif, sta);
906 
907 	if (old_state == IEEE80211_STA_AUTH &&
908 	    new_state == IEEE80211_STA_ASSOC) {
909 		if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
910 			return 0; /* defer to bss_info_changed to have vif info */
911 		return __rtw89_ops_sta_assoc(rtwdev, vif, sta, false);
912 	}
913 
914 	if (old_state == IEEE80211_STA_ASSOC &&
915 	    new_state == IEEE80211_STA_AUTH)
916 		return __rtw89_ops_sta_disassoc(rtwdev, vif, sta);
917 
918 	if (old_state == IEEE80211_STA_AUTH &&
919 	    new_state == IEEE80211_STA_NONE)
920 		return __rtw89_ops_sta_disconnect(rtwdev, vif, sta);
921 
922 	if (old_state == IEEE80211_STA_NONE &&
923 	    new_state == IEEE80211_STA_NOTEXIST)
924 		return __rtw89_ops_sta_remove(rtwdev, vif, sta);
925 
926 	return 0;
927 }
928 
929 static int rtw89_ops_sta_state(struct ieee80211_hw *hw,
930 			       struct ieee80211_vif *vif,
931 			       struct ieee80211_sta *sta,
932 			       enum ieee80211_sta_state old_state,
933 			       enum ieee80211_sta_state new_state)
934 {
935 	struct rtw89_dev *rtwdev = hw->priv;
936 
937 	lockdep_assert_wiphy(hw->wiphy);
938 
939 	rtw89_leave_ps_mode(rtwdev);
940 	return __rtw89_ops_sta_state(hw, vif, sta, old_state, new_state);
941 }
942 
943 static int rtw89_ops_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
944 			     struct ieee80211_vif *vif,
945 			     struct ieee80211_sta *sta,
946 			     struct ieee80211_key_conf *key)
947 {
948 	struct rtw89_dev *rtwdev = hw->priv;
949 	int ret;
950 
951 	lockdep_assert_wiphy(hw->wiphy);
952 
953 	rtw89_leave_ps_mode(rtwdev);
954 
955 	switch (cmd) {
956 	case SET_KEY:
957 		rtw89_btc_ntfy_specific_packet(rtwdev, PACKET_EAPOL_END);
958 		ret = rtw89_cam_sec_key_add(rtwdev, vif, sta, key);
959 		if (ret && ret != -EOPNOTSUPP) {
960 			rtw89_err(rtwdev, "failed to add key to sec cam\n");
961 			return ret;
962 		}
963 		break;
964 	case DISABLE_KEY:
965 		flush_work(&rtwdev->txq_work);
966 		rtw89_hci_flush_queues(rtwdev, BIT(rtwdev->hw->queues) - 1,
967 				       false);
968 		rtw89_mac_flush_txq(rtwdev, BIT(rtwdev->hw->queues) - 1, false);
969 		ret = rtw89_cam_sec_key_del(rtwdev, vif, sta, key, true);
970 		if (ret) {
971 			rtw89_err(rtwdev, "failed to remove key from sec cam\n");
972 			return ret;
973 		}
974 		break;
975 	}
976 
977 	return ret;
978 }
979 
980 static int rtw89_ops_ampdu_action(struct ieee80211_hw *hw,
981 				  struct ieee80211_vif *vif,
982 				  struct ieee80211_ampdu_params *params)
983 {
984 	struct rtw89_dev *rtwdev = hw->priv;
985 	struct ieee80211_sta *sta = params->sta;
986 	struct rtw89_sta *rtwsta = sta_to_rtwsta(sta);
987 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
988 	u16 tid = params->tid;
989 	struct ieee80211_txq *txq = sta->txq[tid];
990 	struct rtw89_txq *rtwtxq = (struct rtw89_txq *)txq->drv_priv;
991 
992 	lockdep_assert_wiphy(rtwdev->hw->wiphy);
993 
994 	switch (params->action) {
995 	case IEEE80211_AMPDU_TX_START:
996 		return IEEE80211_AMPDU_TX_START_IMMEDIATE;
997 	case IEEE80211_AMPDU_TX_STOP_CONT:
998 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
999 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1000 		clear_bit(RTW89_TXQ_F_AMPDU, &rtwtxq->flags);
1001 		clear_bit(tid, rtwsta->ampdu_map);
1002 		rtw89_chip_h2c_ampdu_cmac_tbl(rtwdev, rtwvif, rtwsta);
1003 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1004 		break;
1005 	case IEEE80211_AMPDU_TX_OPERATIONAL:
1006 		set_bit(RTW89_TXQ_F_AMPDU, &rtwtxq->flags);
1007 		rtwsta->ampdu_params[tid].agg_num = params->buf_size;
1008 		rtwsta->ampdu_params[tid].amsdu = params->amsdu;
1009 		set_bit(tid, rtwsta->ampdu_map);
1010 		rtw89_leave_ps_mode(rtwdev);
1011 		rtw89_chip_h2c_ampdu_cmac_tbl(rtwdev, rtwvif, rtwsta);
1012 		break;
1013 	case IEEE80211_AMPDU_RX_START:
1014 		rtw89_chip_h2c_ba_cam(rtwdev, rtwsta, true, params);
1015 		break;
1016 	case IEEE80211_AMPDU_RX_STOP:
1017 		rtw89_chip_h2c_ba_cam(rtwdev, rtwsta, false, params);
1018 		break;
1019 	default:
1020 		WARN_ON(1);
1021 		return -ENOTSUPP;
1022 	}
1023 
1024 	return 0;
1025 }
1026 
1027 static int rtw89_ops_set_rts_threshold(struct ieee80211_hw *hw, int radio_idx,
1028 				       u32 value)
1029 {
1030 	struct rtw89_dev *rtwdev = hw->priv;
1031 
1032 	lockdep_assert_wiphy(hw->wiphy);
1033 
1034 	rtw89_leave_ps_mode(rtwdev);
1035 	if (test_bit(RTW89_FLAG_POWERON, rtwdev->flags))
1036 		rtw89_mac_update_rts_threshold(rtwdev);
1037 
1038 	return 0;
1039 }
1040 
1041 static void rtw89_ops_sta_statistics(struct ieee80211_hw *hw,
1042 				     struct ieee80211_vif *vif,
1043 				     struct ieee80211_sta *sta,
1044 				     struct station_info *sinfo)
1045 {
1046 	struct rtw89_sta *rtwsta = sta_to_rtwsta(sta);
1047 	struct rtw89_sta_link *rtwsta_link;
1048 
1049 	rtwsta_link = rtw89_get_designated_link(rtwsta);
1050 	if (unlikely(!rtwsta_link))
1051 		return;
1052 
1053 	sinfo->txrate = rtwsta_link->ra_report.txrate;
1054 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1055 }
1056 
1057 static
1058 void __rtw89_drop_packets(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif)
1059 {
1060 	struct rtw89_vif *rtwvif;
1061 
1062 	if (vif) {
1063 		rtwvif = vif_to_rtwvif(vif);
1064 		rtw89_mac_pkt_drop_vif(rtwdev, rtwvif);
1065 	} else {
1066 		rtw89_for_each_rtwvif(rtwdev, rtwvif)
1067 			rtw89_mac_pkt_drop_vif(rtwdev, rtwvif);
1068 	}
1069 }
1070 
1071 static void rtw89_ops_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1072 			    u32 queues, bool drop)
1073 {
1074 	struct rtw89_dev *rtwdev = hw->priv;
1075 
1076 	lockdep_assert_wiphy(hw->wiphy);
1077 
1078 	rtw89_leave_lps(rtwdev);
1079 	rtw89_hci_flush_queues(rtwdev, queues, drop);
1080 
1081 	if (drop && !RTW89_CHK_FW_FEATURE(NO_PACKET_DROP, &rtwdev->fw))
1082 		__rtw89_drop_packets(rtwdev, vif);
1083 	else
1084 		rtw89_mac_flush_txq(rtwdev, queues, drop);
1085 }
1086 
1087 struct rtw89_iter_bitrate_mask_data {
1088 	struct rtw89_dev *rtwdev;
1089 	struct ieee80211_vif *vif;
1090 	const struct cfg80211_bitrate_mask *mask;
1091 };
1092 
1093 static void rtw89_ra_mask_info_update_iter(void *data, struct ieee80211_sta *sta)
1094 {
1095 	struct rtw89_iter_bitrate_mask_data *br_data = data;
1096 	struct rtw89_sta *rtwsta = sta_to_rtwsta(sta);
1097 	struct rtw89_vif *rtwvif = rtwsta->rtwvif;
1098 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
1099 	struct rtw89_sta_link *rtwsta_link;
1100 	unsigned int link_id;
1101 
1102 	if (vif != br_data->vif || vif->p2p)
1103 		return;
1104 
1105 	rtw89_sta_for_each_link(rtwsta, rtwsta_link, link_id) {
1106 		rtwsta_link->use_cfg_mask = true;
1107 		rtwsta_link->mask = *br_data->mask;
1108 	}
1109 
1110 	rtw89_phy_ra_update_sta(br_data->rtwdev, sta, IEEE80211_RC_SUPP_RATES_CHANGED);
1111 }
1112 
1113 static void rtw89_ra_mask_info_update(struct rtw89_dev *rtwdev,
1114 				      struct ieee80211_vif *vif,
1115 				      const struct cfg80211_bitrate_mask *mask)
1116 {
1117 	struct rtw89_iter_bitrate_mask_data br_data = { .rtwdev = rtwdev,
1118 							.vif = vif,
1119 							.mask = mask};
1120 
1121 	ieee80211_iterate_stations_atomic(rtwdev->hw, rtw89_ra_mask_info_update_iter,
1122 					  &br_data);
1123 }
1124 
1125 static int rtw89_ops_set_bitrate_mask(struct ieee80211_hw *hw,
1126 				      struct ieee80211_vif *vif,
1127 				      const struct cfg80211_bitrate_mask *mask)
1128 {
1129 	struct rtw89_dev *rtwdev = hw->priv;
1130 
1131 	lockdep_assert_wiphy(hw->wiphy);
1132 
1133 	rtw89_phy_rate_pattern_vif(rtwdev, vif, mask);
1134 	rtw89_ra_mask_info_update(rtwdev, vif, mask);
1135 
1136 	return 0;
1137 }
1138 
1139 static
1140 int rtw89_ops_set_antenna(struct ieee80211_hw *hw, int radio_idx, u32 tx_ant, u32 rx_ant)
1141 {
1142 	struct rtw89_dev *rtwdev = hw->priv;
1143 	struct rtw89_hal *hal = &rtwdev->hal;
1144 	const struct rtw89_chip_info *chip;
1145 
1146 	lockdep_assert_wiphy(hw->wiphy);
1147 
1148 	chip = rtwdev->chip;
1149 
1150 	if (hal->ant_diversity) {
1151 		if (tx_ant != rx_ant || hweight32(tx_ant) != 1)
1152 			return -EINVAL;
1153 	} else if (chip->ops->cfg_txrx_path) {
1154 		/* With cfg_txrx_path ops, chips can configure rx_ant */
1155 	} else if (rx_ant != hw->wiphy->available_antennas_rx && rx_ant != hal->antenna_rx) {
1156 		return -EINVAL;
1157 	}
1158 
1159 	hal->antenna_tx = tx_ant;
1160 	hal->antenna_rx = rx_ant;
1161 	hal->tx_path_diversity = false;
1162 	hal->ant_diversity_fixed = true;
1163 
1164 	return 0;
1165 }
1166 
1167 static
1168 int rtw89_ops_get_antenna(struct ieee80211_hw *hw, int radio_idx, u32 *tx_ant,
1169 			  u32 *rx_ant)
1170 {
1171 	struct rtw89_dev *rtwdev = hw->priv;
1172 	struct rtw89_hal *hal = &rtwdev->hal;
1173 
1174 	*tx_ant = hal->antenna_tx;
1175 	*rx_ant = hal->antenna_rx;
1176 
1177 	return 0;
1178 }
1179 
1180 static void rtw89_ops_sw_scan_start(struct ieee80211_hw *hw,
1181 				    struct ieee80211_vif *vif,
1182 				    const u8 *mac_addr)
1183 {
1184 	struct rtw89_dev *rtwdev = hw->priv;
1185 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
1186 	struct rtw89_vif_link *rtwvif_link;
1187 
1188 	lockdep_assert_wiphy(hw->wiphy);
1189 
1190 	rtwvif_link = rtw89_get_designated_link(rtwvif);
1191 	if (unlikely(!rtwvif_link)) {
1192 		rtw89_err(rtwdev, "sw scan start: find no designated link\n");
1193 		return;
1194 	}
1195 
1196 	rtw89_leave_lps(rtwdev);
1197 
1198 	rtw89_core_scan_start(rtwdev, rtwvif_link, mac_addr, false);
1199 }
1200 
1201 static void rtw89_ops_sw_scan_complete(struct ieee80211_hw *hw,
1202 				       struct ieee80211_vif *vif)
1203 {
1204 	struct rtw89_dev *rtwdev = hw->priv;
1205 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
1206 	struct rtw89_vif_link *rtwvif_link;
1207 
1208 	lockdep_assert_wiphy(hw->wiphy);
1209 
1210 	rtwvif_link = rtw89_get_designated_link(rtwvif);
1211 	if (unlikely(!rtwvif_link)) {
1212 		rtw89_err(rtwdev, "sw scan complete: find no designated link\n");
1213 		return;
1214 	}
1215 
1216 	rtw89_core_scan_complete(rtwdev, rtwvif_link, false);
1217 }
1218 
1219 static void rtw89_ops_reconfig_complete(struct ieee80211_hw *hw,
1220 					enum ieee80211_reconfig_type reconfig_type)
1221 {
1222 	struct rtw89_dev *rtwdev = hw->priv;
1223 
1224 	if (reconfig_type == IEEE80211_RECONFIG_TYPE_RESTART)
1225 		rtw89_ser_recfg_done(rtwdev);
1226 }
1227 
1228 static int rtw89_ops_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1229 			     struct ieee80211_scan_request *req)
1230 {
1231 	struct rtw89_dev *rtwdev = hw->priv;
1232 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
1233 	struct rtw89_vif_link *rtwvif_link;
1234 	int ret;
1235 
1236 	lockdep_assert_wiphy(hw->wiphy);
1237 
1238 	if (!RTW89_CHK_FW_FEATURE(SCAN_OFFLOAD, &rtwdev->fw))
1239 		return 1;
1240 
1241 	if (rtwdev->scanning || rtwvif->offchan)
1242 		return -EBUSY;
1243 
1244 	rtwvif_link = rtw89_get_designated_link(rtwvif);
1245 	if (unlikely(!rtwvif_link)) {
1246 		rtw89_err(rtwdev, "hw scan: find no designated link\n");
1247 		return -ENOLINK;
1248 	}
1249 
1250 	rtw89_leave_lps(rtwdev);
1251 	rtw89_leave_ips_by_hwflags(rtwdev);
1252 
1253 	ret = rtw89_hw_scan_start(rtwdev, rtwvif_link, req);
1254 	if (ret)
1255 		return ret;
1256 
1257 	ret = rtw89_hw_scan_offload(rtwdev, rtwvif_link, true);
1258 	if (ret) {
1259 		rtw89_hw_scan_abort(rtwdev, rtwvif_link);
1260 		rtw89_err(rtwdev, "HW scan failed with status: %d\n", ret);
1261 	}
1262 
1263 	return ret;
1264 }
1265 
1266 static void rtw89_ops_cancel_hw_scan(struct ieee80211_hw *hw,
1267 				     struct ieee80211_vif *vif)
1268 {
1269 	struct rtw89_dev *rtwdev = hw->priv;
1270 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
1271 	struct rtw89_vif_link *rtwvif_link;
1272 
1273 	lockdep_assert_wiphy(hw->wiphy);
1274 
1275 	if (!RTW89_CHK_FW_FEATURE(SCAN_OFFLOAD, &rtwdev->fw))
1276 		return;
1277 
1278 	if (!rtwdev->scanning)
1279 		return;
1280 
1281 	rtwvif_link = rtw89_get_designated_link(rtwvif);
1282 	if (unlikely(!rtwvif_link)) {
1283 		rtw89_err(rtwdev, "cancel hw scan: find no designated link\n");
1284 		return;
1285 	}
1286 
1287 	rtw89_hw_scan_abort(rtwdev, rtwvif_link);
1288 }
1289 
1290 static void rtw89_ops_sta_rc_update(struct ieee80211_hw *hw,
1291 				    struct ieee80211_vif *vif,
1292 				    struct ieee80211_link_sta *link_sta,
1293 				    u32 changed)
1294 {
1295 	struct rtw89_sta *rtwsta = sta_to_rtwsta(link_sta->sta);
1296 	struct rtw89_dev *rtwdev = hw->priv;
1297 	struct rtw89_sta_link *rtwsta_link;
1298 
1299 	rtwsta_link = rtwsta->links[link_sta->link_id];
1300 	if (unlikely(!rtwsta_link))
1301 		return;
1302 
1303 	rtw89_phy_ra_update_sta_link(rtwdev, rtwsta_link, changed);
1304 }
1305 
1306 static int rtw89_ops_add_chanctx(struct ieee80211_hw *hw,
1307 				 struct ieee80211_chanctx_conf *ctx)
1308 {
1309 	struct rtw89_dev *rtwdev = hw->priv;
1310 
1311 	lockdep_assert_wiphy(hw->wiphy);
1312 
1313 	return rtw89_chanctx_ops_add(rtwdev, ctx);
1314 }
1315 
1316 static void rtw89_ops_remove_chanctx(struct ieee80211_hw *hw,
1317 				     struct ieee80211_chanctx_conf *ctx)
1318 {
1319 	struct rtw89_dev *rtwdev = hw->priv;
1320 
1321 	lockdep_assert_wiphy(hw->wiphy);
1322 
1323 	rtw89_chanctx_ops_remove(rtwdev, ctx);
1324 }
1325 
1326 static void rtw89_ops_change_chanctx(struct ieee80211_hw *hw,
1327 				     struct ieee80211_chanctx_conf *ctx,
1328 				     u32 changed)
1329 {
1330 	struct rtw89_dev *rtwdev = hw->priv;
1331 
1332 	lockdep_assert_wiphy(hw->wiphy);
1333 
1334 	rtw89_chanctx_ops_change(rtwdev, ctx, changed);
1335 }
1336 
1337 static int rtw89_ops_assign_vif_chanctx(struct ieee80211_hw *hw,
1338 					struct ieee80211_vif *vif,
1339 					struct ieee80211_bss_conf *link_conf,
1340 					struct ieee80211_chanctx_conf *ctx)
1341 {
1342 	struct rtw89_dev *rtwdev = hw->priv;
1343 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
1344 	struct rtw89_vif_link *rtwvif_link;
1345 
1346 	lockdep_assert_wiphy(hw->wiphy);
1347 
1348 	rtwvif_link = rtwvif->links[link_conf->link_id];
1349 	if (unlikely(!rtwvif_link)) {
1350 		rtw89_err(rtwdev,
1351 			  "%s: rtwvif link (link_id %u) is not active\n",
1352 			  __func__, link_conf->link_id);
1353 		return -ENOLINK;
1354 	}
1355 
1356 	return rtw89_chanctx_ops_assign_vif(rtwdev, rtwvif_link, ctx);
1357 }
1358 
1359 static void rtw89_ops_unassign_vif_chanctx(struct ieee80211_hw *hw,
1360 					   struct ieee80211_vif *vif,
1361 					   struct ieee80211_bss_conf *link_conf,
1362 					   struct ieee80211_chanctx_conf *ctx)
1363 {
1364 	struct rtw89_dev *rtwdev = hw->priv;
1365 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
1366 	struct rtw89_vif_link *rtwvif_link;
1367 
1368 	lockdep_assert_wiphy(hw->wiphy);
1369 
1370 	rtwvif_link = rtwvif->links[link_conf->link_id];
1371 	if (unlikely(!rtwvif_link)) {
1372 		rtw89_err(rtwdev,
1373 			  "%s: rtwvif link (link_id %u) is not active\n",
1374 			  __func__, link_conf->link_id);
1375 		return;
1376 	}
1377 
1378 	rtw89_chanctx_ops_unassign_vif(rtwdev, rtwvif_link, ctx);
1379 }
1380 
1381 static
1382 int rtw89_ops_switch_vif_chanctx(struct ieee80211_hw *hw,
1383 				 struct ieee80211_vif_chanctx_switch *vifs,
1384 				 int n_vifs,
1385 				 enum ieee80211_chanctx_switch_mode mode)
1386 {
1387 	struct rtw89_dev *rtwdev = hw->priv;
1388 	bool replace;
1389 	int ret;
1390 	int i;
1391 
1392 	lockdep_assert_wiphy(hw->wiphy);
1393 
1394 	switch (mode) {
1395 	case CHANCTX_SWMODE_REASSIGN_VIF:
1396 		replace = false;
1397 		break;
1398 	case CHANCTX_SWMODE_SWAP_CONTEXTS:
1399 		replace = true;
1400 		break;
1401 	default:
1402 		return -EOPNOTSUPP;
1403 	}
1404 
1405 	for (i = 0; i < n_vifs; i++) {
1406 		struct ieee80211_vif_chanctx_switch *p = &vifs[i];
1407 		struct ieee80211_bss_conf *link_conf = p->link_conf;
1408 		struct rtw89_vif *rtwvif = vif_to_rtwvif(p->vif);
1409 		struct rtw89_vif_link *rtwvif_link;
1410 
1411 		rtwvif_link = rtwvif->links[link_conf->link_id];
1412 		if (unlikely(!rtwvif_link)) {
1413 			rtw89_err(rtwdev,
1414 				  "%s: rtwvif link (link_id %u) is not active\n",
1415 				  __func__, link_conf->link_id);
1416 			return -ENOLINK;
1417 		}
1418 
1419 		ret = rtw89_chanctx_ops_reassign_vif(rtwdev, rtwvif_link,
1420 						     p->old_ctx, p->new_ctx,
1421 						     replace);
1422 		if (ret)
1423 			return ret;
1424 	}
1425 
1426 	return 0;
1427 }
1428 
1429 static void rtw89_ops_channel_switch_beacon(struct ieee80211_hw *hw,
1430 					    struct ieee80211_vif *vif,
1431 					    struct cfg80211_chan_def *chandef)
1432 {
1433 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
1434 	struct rtw89_dev *rtwdev = hw->priv;
1435 	struct rtw89_vif_link *rtwvif_link;
1436 
1437 	BUILD_BUG_ON(RTW89_MLD_NON_STA_LINK_NUM != 1);
1438 
1439 	rtwvif_link = rtw89_vif_get_link_inst(rtwvif, 0);
1440 	if (unlikely(!rtwvif_link)) {
1441 		rtw89_err(rtwdev, "chsw bcn: find no link on HW-0\n");
1442 		return;
1443 	}
1444 
1445 	wiphy_delayed_work_queue(hw->wiphy, &rtwvif_link->csa_beacon_work, 0);
1446 }
1447 
1448 static int rtw89_ops_remain_on_channel(struct ieee80211_hw *hw,
1449 				       struct ieee80211_vif *vif,
1450 				       struct ieee80211_channel *chan,
1451 				       int duration,
1452 				       enum ieee80211_roc_type type)
1453 {
1454 	struct rtw89_dev *rtwdev = hw->priv;
1455 	struct rtw89_vif *rtwvif = vif_to_rtwvif_safe(vif);
1456 	struct rtw89_roc *roc = &rtwvif->roc;
1457 
1458 	lockdep_assert_wiphy(hw->wiphy);
1459 
1460 	if (!rtwvif)
1461 		return -EINVAL;
1462 
1463 	if (roc->state != RTW89_ROC_IDLE) {
1464 		return -EBUSY;
1465 	}
1466 
1467 	if (rtwdev->scanning)
1468 		rtw89_hw_scan_abort(rtwdev, rtwdev->scan_info.scanning_vif);
1469 
1470 	if (type == IEEE80211_ROC_TYPE_MGMT_TX)
1471 		roc->state = RTW89_ROC_MGMT;
1472 	else
1473 		roc->state = RTW89_ROC_NORMAL;
1474 
1475 	roc->duration = duration;
1476 	roc->chan = *chan;
1477 	roc->type = type;
1478 
1479 	rtw89_roc_start(rtwdev, rtwvif);
1480 
1481 	return 0;
1482 }
1483 
1484 static int rtw89_ops_cancel_remain_on_channel(struct ieee80211_hw *hw,
1485 					      struct ieee80211_vif *vif)
1486 {
1487 	struct rtw89_dev *rtwdev = hw->priv;
1488 	struct rtw89_vif *rtwvif = vif_to_rtwvif_safe(vif);
1489 
1490 	lockdep_assert_wiphy(hw->wiphy);
1491 
1492 	if (!rtwvif)
1493 		return -EINVAL;
1494 
1495 	wiphy_delayed_work_cancel(hw->wiphy, &rtwvif->roc.roc_work);
1496 
1497 	rtw89_roc_end(rtwdev, rtwvif);
1498 
1499 	return 0;
1500 }
1501 
1502 static void rtw89_set_tid_config_iter(void *data, struct ieee80211_sta *sta)
1503 {
1504 	struct cfg80211_tid_config *tid_config = data;
1505 	struct rtw89_sta *rtwsta = sta_to_rtwsta(sta);
1506 	struct rtw89_dev *rtwdev = rtwsta->rtwdev;
1507 
1508 	rtw89_core_set_tid_config(rtwdev, sta, tid_config);
1509 }
1510 
1511 static int rtw89_ops_set_tid_config(struct ieee80211_hw *hw,
1512 				    struct ieee80211_vif *vif,
1513 				    struct ieee80211_sta *sta,
1514 				    struct cfg80211_tid_config *tid_config)
1515 {
1516 	struct rtw89_dev *rtwdev = hw->priv;
1517 
1518 	lockdep_assert_wiphy(hw->wiphy);
1519 
1520 	if (sta)
1521 		rtw89_core_set_tid_config(rtwdev, sta, tid_config);
1522 	else
1523 		ieee80211_iterate_stations_atomic(rtwdev->hw,
1524 						  rtw89_set_tid_config_iter,
1525 						  tid_config);
1526 
1527 	return 0;
1528 }
1529 
1530 static bool rtw89_can_work_on_links(struct rtw89_dev *rtwdev,
1531 				    struct ieee80211_vif *vif, u16 links)
1532 {
1533 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
1534 	u8 w = hweight16(links);
1535 
1536 	if (vif->type != NL80211_IFTYPE_STATION &&
1537 	    w > RTW89_MLD_NON_STA_LINK_NUM)
1538 		return false;
1539 
1540 	return w <= rtwvif->links_inst_valid_num;
1541 }
1542 
1543 static bool rtw89_ops_can_activate_links(struct ieee80211_hw *hw,
1544 					 struct ieee80211_vif *vif,
1545 					 u16 active_links)
1546 {
1547 	struct rtw89_dev *rtwdev = hw->priv;
1548 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
1549 	u16 current_links = vif->active_links;
1550 	struct rtw89_vif_ml_trans trans = {
1551 		.mediate_links = current_links | active_links,
1552 		.links_to_del = current_links & ~active_links,
1553 		.links_to_add = active_links & ~current_links,
1554 	};
1555 
1556 	lockdep_assert_wiphy(hw->wiphy);
1557 
1558 	if (!rtw89_can_work_on_links(rtwdev, vif, active_links))
1559 		return false;
1560 
1561 	/*
1562 	 * Leave LPS at the beginning of ieee80211_set_active_links().
1563 	 * Because the entire process takes the same lock as our track
1564 	 * work, LPS will not enter during ieee80211_set_active_links().
1565 	 */
1566 	rtw89_leave_lps(rtwdev);
1567 
1568 	rtwvif->ml_trans = trans;
1569 
1570 	return true;
1571 }
1572 
1573 static void __rtw89_ops_clr_vif_links(struct rtw89_dev *rtwdev,
1574 				      struct rtw89_vif *rtwvif,
1575 				      unsigned long clr_links)
1576 {
1577 	struct rtw89_vif_link *rtwvif_link;
1578 	unsigned int link_id;
1579 
1580 	for_each_set_bit(link_id, &clr_links, IEEE80211_MLD_MAX_NUM_LINKS) {
1581 		rtwvif_link = rtwvif->links[link_id];
1582 		if (unlikely(!rtwvif_link))
1583 			continue;
1584 
1585 		__rtw89_ops_remove_iface_link(rtwdev, rtwvif_link);
1586 
1587 		rtw89_vif_unset_link(rtwvif, link_id);
1588 	}
1589 }
1590 
1591 static int __rtw89_ops_set_vif_links(struct rtw89_dev *rtwdev,
1592 				     struct rtw89_vif *rtwvif,
1593 				     unsigned long set_links)
1594 {
1595 	struct rtw89_vif_link *rtwvif_link;
1596 	unsigned int link_id;
1597 	int ret;
1598 
1599 	for_each_set_bit(link_id, &set_links, IEEE80211_MLD_MAX_NUM_LINKS) {
1600 		rtwvif_link = rtw89_vif_set_link(rtwvif, link_id);
1601 		if (!rtwvif_link)
1602 			return -EINVAL;
1603 
1604 		ret = __rtw89_ops_add_iface_link(rtwdev, rtwvif_link);
1605 		if (ret) {
1606 			rtw89_err(rtwdev, "%s: failed to add iface (link id %u)\n",
1607 				  __func__, link_id);
1608 			return ret;
1609 		}
1610 	}
1611 
1612 	return 0;
1613 }
1614 
1615 static void rtw89_vif_cfg_fw_links(struct rtw89_dev *rtwdev,
1616 				   struct rtw89_vif *rtwvif,
1617 				   unsigned long links, bool en)
1618 {
1619 	struct rtw89_vif_link *rtwvif_link;
1620 	unsigned int link_id;
1621 
1622 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
1623 		rtwvif_link = rtwvif->links[link_id];
1624 		if (unlikely(!rtwvif_link))
1625 			continue;
1626 
1627 		rtw89_fw_h2c_mlo_link_cfg(rtwdev, rtwvif_link, en);
1628 	}
1629 }
1630 
1631 static void rtw89_vif_update_fw_links(struct rtw89_dev *rtwdev,
1632 				      struct rtw89_vif *rtwvif,
1633 				      u16 current_links)
1634 {
1635 	struct rtw89_vif_ml_trans *trans = &rtwvif->ml_trans;
1636 
1637 	/* Do follow-up when all updating links exist. */
1638 	if (current_links != trans->mediate_links)
1639 		return;
1640 
1641 	rtw89_vif_cfg_fw_links(rtwdev, rtwvif, trans->links_to_del, false);
1642 	rtw89_vif_cfg_fw_links(rtwdev, rtwvif, trans->links_to_add, true);
1643 }
1644 
1645 static
1646 int rtw89_ops_change_vif_links(struct ieee80211_hw *hw,
1647 			       struct ieee80211_vif *vif,
1648 			       u16 old_links, u16 new_links,
1649 			       struct ieee80211_bss_conf *old[IEEE80211_MLD_MAX_NUM_LINKS])
1650 {
1651 	struct rtw89_dev *rtwdev = hw->priv;
1652 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
1653 	unsigned long clr_links = old_links & ~new_links;
1654 	unsigned long set_links = new_links & ~old_links;
1655 	bool removing_links = !old_links || clr_links;
1656 	struct rtw89_link_conf_container *snap;
1657 	int ret = 0;
1658 	int i;
1659 
1660 	lockdep_assert_wiphy(hw->wiphy);
1661 
1662 	rtw89_debug(rtwdev, RTW89_DBG_STATE,
1663 		    "%s: old_links (0x%08x) -> new_links (0x%08x)\n",
1664 		    __func__, old_links, new_links);
1665 
1666 	if (!rtw89_can_work_on_links(rtwdev, vif, new_links))
1667 		return -EOPNOTSUPP;
1668 
1669 	if (removing_links) {
1670 		snap = kzalloc(sizeof(*snap), GFP_KERNEL);
1671 		if (!snap)
1672 			return -ENOMEM;
1673 
1674 		for (i = 0; i < ARRAY_SIZE(snap->link_conf); i++)
1675 			snap->link_conf[i] = old[i];
1676 
1677 		rcu_assign_pointer(rtwvif->snap_link_confs, snap);
1678 	}
1679 
1680 	/* might depend on @snap; don't change order */
1681 	rtw89_leave_ips_by_hwflags(rtwdev);
1682 
1683 	if (rtwdev->scanning)
1684 		rtw89_hw_scan_abort(rtwdev, rtwdev->scan_info.scanning_vif);
1685 
1686 	rtw89_vif_update_fw_links(rtwdev, rtwvif, old_links);
1687 
1688 	if (!old_links)
1689 		__rtw89_ops_clr_vif_links(rtwdev, rtwvif,
1690 					  BIT(RTW89_VIF_IDLE_LINK_ID));
1691 	else if (clr_links)
1692 		__rtw89_ops_clr_vif_links(rtwdev, rtwvif, clr_links);
1693 
1694 	if (removing_links) {
1695 		/* @snap is required if and only if during removing links.
1696 		 * However, it's done here. So, cleanup @snap immediately.
1697 		 */
1698 		rcu_assign_pointer(rtwvif->snap_link_confs, NULL);
1699 
1700 		/* The pointers in @old will free after this function return,
1701 		 * so synchronously wait for all readers of snap to be done.
1702 		 */
1703 		synchronize_rcu();
1704 		kfree(snap);
1705 	}
1706 
1707 	if (set_links) {
1708 		ret = __rtw89_ops_set_vif_links(rtwdev, rtwvif, set_links);
1709 		if (ret)
1710 			__rtw89_ops_clr_vif_links(rtwdev, rtwvif, set_links);
1711 	} else if (!new_links) {
1712 		ret = __rtw89_ops_set_vif_links(rtwdev, rtwvif,
1713 						BIT(RTW89_VIF_IDLE_LINK_ID));
1714 		if (ret)
1715 			__rtw89_ops_clr_vif_links(rtwdev, rtwvif,
1716 						  BIT(RTW89_VIF_IDLE_LINK_ID));
1717 	}
1718 
1719 	rtw89_enter_ips_by_hwflags(rtwdev);
1720 	return ret;
1721 }
1722 
1723 static void __rtw89_ops_clr_sta_links(struct rtw89_dev *rtwdev,
1724 				      struct rtw89_sta *rtwsta,
1725 				      unsigned long clr_links)
1726 {
1727 	struct rtw89_vif_link *rtwvif_link;
1728 	struct rtw89_sta_link *rtwsta_link;
1729 	unsigned int link_id;
1730 
1731 	for_each_set_bit(link_id, &clr_links, IEEE80211_MLD_MAX_NUM_LINKS) {
1732 		rtwsta_link = rtwsta->links[link_id];
1733 		if (unlikely(!rtwsta_link))
1734 			continue;
1735 
1736 		rtwvif_link = rtwsta_link->rtwvif_link;
1737 
1738 		rtw89_core_sta_link_disassoc(rtwdev, rtwvif_link, rtwsta_link);
1739 		rtw89_core_sta_link_disconnect(rtwdev, rtwvif_link, rtwsta_link);
1740 		rtw89_core_sta_link_remove(rtwdev, rtwvif_link, rtwsta_link);
1741 
1742 		rtw89_sta_unset_link(rtwsta, link_id);
1743 	}
1744 }
1745 
1746 static int __rtw89_ops_set_sta_links(struct rtw89_dev *rtwdev,
1747 				     struct rtw89_sta *rtwsta,
1748 				     unsigned long set_links)
1749 {
1750 	struct rtw89_vif_link *rtwvif_link;
1751 	struct rtw89_sta_link *rtwsta_link;
1752 	unsigned int link_id;
1753 	u8 sec_cam_idx;
1754 	int ret;
1755 
1756 	for_each_set_bit(link_id, &set_links, IEEE80211_MLD_MAX_NUM_LINKS) {
1757 		rtwsta_link = rtw89_sta_set_link(rtwsta, link_id);
1758 		if (!rtwsta_link)
1759 			return -EINVAL;
1760 
1761 		rtwvif_link = rtwsta_link->rtwvif_link;
1762 
1763 		ret = rtw89_core_sta_link_add(rtwdev, rtwvif_link, rtwsta_link);
1764 		if (ret) {
1765 			rtw89_err(rtwdev, "%s: failed to add sta (link id %u)\n",
1766 				  __func__, link_id);
1767 			return ret;
1768 		}
1769 
1770 		rtw89_vif_type_mapping(rtwvif_link, true);
1771 
1772 		ret = rtw89_core_sta_link_assoc(rtwdev, rtwvif_link, rtwsta_link);
1773 		if (ret) {
1774 			rtw89_err(rtwdev, "%s: failed to assoc sta (link id %u)\n",
1775 				  __func__, link_id);
1776 			return ret;
1777 		}
1778 
1779 		__rtw89_ops_bss_link_assoc(rtwdev, rtwvif_link);
1780 
1781 		for_each_set_bit(sec_cam_idx, rtwsta->pairwise_sec_cam_map,
1782 				 RTW89_MAX_SEC_CAM_NUM) {
1783 			ret = rtw89_cam_attach_link_sec_cam(rtwdev,
1784 							    rtwvif_link,
1785 							    rtwsta_link,
1786 							    sec_cam_idx);
1787 			if (ret) {
1788 				rtw89_err(rtwdev,
1789 					  "%s: failed to apply pairwise key (link id %u)\n",
1790 					  __func__, link_id);
1791 				return ret;
1792 			}
1793 		}
1794 	}
1795 
1796 	return 0;
1797 }
1798 
1799 static
1800 int rtw89_ops_change_sta_links(struct ieee80211_hw *hw,
1801 			       struct ieee80211_vif *vif,
1802 			       struct ieee80211_sta *sta,
1803 			       u16 old_links, u16 new_links)
1804 {
1805 	struct rtw89_dev *rtwdev = hw->priv;
1806 	struct rtw89_sta *rtwsta = sta_to_rtwsta(sta);
1807 	unsigned long clr_links = old_links & ~new_links;
1808 	unsigned long set_links = new_links & ~old_links;
1809 	int ret = 0;
1810 
1811 	lockdep_assert_wiphy(hw->wiphy);
1812 
1813 	rtw89_debug(rtwdev, RTW89_DBG_STATE,
1814 		    "%s: old_links (0x%08x) -> new_links (0x%08x)\n",
1815 		    __func__, old_links, new_links);
1816 
1817 	if (!rtw89_can_work_on_links(rtwdev, vif, new_links))
1818 		return -EOPNOTSUPP;
1819 
1820 	rtw89_leave_ps_mode(rtwdev);
1821 
1822 	if (clr_links)
1823 		__rtw89_ops_clr_sta_links(rtwdev, rtwsta, clr_links);
1824 
1825 	if (set_links) {
1826 		ret = __rtw89_ops_set_sta_links(rtwdev, rtwsta, set_links);
1827 		if (ret)
1828 			__rtw89_ops_clr_sta_links(rtwdev, rtwsta, set_links);
1829 	}
1830 
1831 	return ret;
1832 }
1833 
1834 #ifdef CONFIG_PM
1835 static int rtw89_ops_suspend(struct ieee80211_hw *hw,
1836 			     struct cfg80211_wowlan *wowlan)
1837 {
1838 	struct rtw89_dev *rtwdev = hw->priv;
1839 	int ret;
1840 
1841 	lockdep_assert_wiphy(hw->wiphy);
1842 
1843 	set_bit(RTW89_FLAG_FORBIDDEN_TRACK_WORK, rtwdev->flags);
1844 	wiphy_delayed_work_cancel(hw->wiphy, &rtwdev->track_work);
1845 	wiphy_delayed_work_cancel(hw->wiphy, &rtwdev->track_ps_work);
1846 
1847 	ret = rtw89_wow_suspend(rtwdev, wowlan);
1848 	if (ret) {
1849 		rtw89_warn(rtwdev, "failed to suspend for wow %d\n", ret);
1850 		clear_bit(RTW89_FLAG_FORBIDDEN_TRACK_WORK, rtwdev->flags);
1851 		return 1;
1852 	}
1853 
1854 	return 0;
1855 }
1856 
1857 static int rtw89_ops_resume(struct ieee80211_hw *hw)
1858 {
1859 	struct rtw89_dev *rtwdev = hw->priv;
1860 	int ret;
1861 
1862 	lockdep_assert_wiphy(hw->wiphy);
1863 
1864 	ret = rtw89_wow_resume(rtwdev);
1865 	if (ret)
1866 		rtw89_warn(rtwdev, "failed to resume for wow %d\n", ret);
1867 
1868 	clear_bit(RTW89_FLAG_FORBIDDEN_TRACK_WORK, rtwdev->flags);
1869 	wiphy_delayed_work_queue(hw->wiphy, &rtwdev->track_work,
1870 				 RTW89_TRACK_WORK_PERIOD);
1871 	wiphy_delayed_work_queue(hw->wiphy, &rtwdev->track_ps_work,
1872 				 RTW89_TRACK_PS_WORK_PERIOD);
1873 
1874 	return ret ? 1 : 0;
1875 }
1876 
1877 static void rtw89_ops_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1878 {
1879 	struct rtw89_dev *rtwdev = hw->priv;
1880 
1881 	device_set_wakeup_enable(rtwdev->dev, enabled);
1882 }
1883 
1884 static void rtw89_set_rekey_data(struct ieee80211_hw *hw,
1885 				 struct ieee80211_vif *vif,
1886 				 struct cfg80211_gtk_rekey_data *data)
1887 {
1888 	struct rtw89_dev *rtwdev = hw->priv;
1889 	struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
1890 	struct rtw89_wow_gtk_info *gtk_info = &rtw_wow->gtk_info;
1891 
1892 	lockdep_assert_wiphy(hw->wiphy);
1893 
1894 	if (data->kek_len > sizeof(gtk_info->kek) ||
1895 	    data->kck_len > sizeof(gtk_info->kck)) {
1896 		rtw89_warn(rtwdev, "kek or kck length over fw limit\n");
1897 		return;
1898 	}
1899 
1900 	memcpy(gtk_info->kek, data->kek, data->kek_len);
1901 	memcpy(gtk_info->kck, data->kck, data->kck_len);
1902 }
1903 #endif
1904 
1905 static int rtw89_ops_get_survey(struct ieee80211_hw *hw, int idx,
1906 				struct survey_info *survey)
1907 {
1908 	struct ieee80211_conf *conf = &hw->conf;
1909 	struct rtw89_dev *rtwdev = hw->priv;
1910 	struct rtw89_bb_ctx *bb;
1911 
1912 	if (idx == 0) {
1913 		survey->channel = conf->chandef.chan;
1914 		survey->filled = SURVEY_INFO_NOISE_DBM;
1915 		survey->noise = RTW89_NOISE_DEFAULT;
1916 
1917 		return 0;
1918 	}
1919 
1920 	rtw89_for_each_active_bb(rtwdev, bb) {
1921 		struct rtw89_env_monitor_info *env = &bb->env_monitor;
1922 		struct rtw89_nhm_report *rpt;
1923 
1924 		rpt = list_first_entry_or_null(&env->nhm_rpt_list, typeof(*rpt), list);
1925 		if (!rpt)
1926 			continue;
1927 
1928 		survey->filled = SURVEY_INFO_NOISE_DBM;
1929 		survey->noise = rpt->noise - MAX_RSSI;
1930 		survey->channel = rpt->channel;
1931 		list_del_init(&rpt->list);
1932 
1933 		return 0;
1934 	}
1935 
1936 	return -EINVAL;
1937 }
1938 
1939 static void rtw89_ops_rfkill_poll(struct ieee80211_hw *hw)
1940 {
1941 	struct rtw89_dev *rtwdev = hw->priv;
1942 
1943 	lockdep_assert_wiphy(hw->wiphy);
1944 
1945 	/* wl_disable GPIO get floating when entering LPS */
1946 	if (test_bit(RTW89_FLAG_RUNNING, rtwdev->flags))
1947 		return;
1948 
1949 	rtw89_core_rfkill_poll(rtwdev, false);
1950 }
1951 
1952 const struct ieee80211_ops rtw89_ops = {
1953 	.tx			= rtw89_ops_tx,
1954 	.wake_tx_queue		= rtw89_ops_wake_tx_queue,
1955 	.start			= rtw89_ops_start,
1956 	.stop			= rtw89_ops_stop,
1957 	.config			= rtw89_ops_config,
1958 	.add_interface		= rtw89_ops_add_interface,
1959 	.change_interface       = rtw89_ops_change_interface,
1960 	.remove_interface	= rtw89_ops_remove_interface,
1961 	.configure_filter	= rtw89_ops_configure_filter,
1962 	.vif_cfg_changed	= rtw89_ops_vif_cfg_changed,
1963 	.link_info_changed	= rtw89_ops_link_info_changed,
1964 	.start_ap		= rtw89_ops_start_ap,
1965 	.stop_ap		= rtw89_ops_stop_ap,
1966 	.set_tim		= rtw89_ops_set_tim,
1967 	.conf_tx		= rtw89_ops_conf_tx,
1968 	.sta_state		= rtw89_ops_sta_state,
1969 	.set_key		= rtw89_ops_set_key,
1970 	.ampdu_action		= rtw89_ops_ampdu_action,
1971 	.get_survey		= rtw89_ops_get_survey,
1972 	.set_rts_threshold	= rtw89_ops_set_rts_threshold,
1973 	.sta_statistics		= rtw89_ops_sta_statistics,
1974 	.flush			= rtw89_ops_flush,
1975 	.set_bitrate_mask	= rtw89_ops_set_bitrate_mask,
1976 	.set_antenna		= rtw89_ops_set_antenna,
1977 	.get_antenna		= rtw89_ops_get_antenna,
1978 	.sw_scan_start		= rtw89_ops_sw_scan_start,
1979 	.sw_scan_complete	= rtw89_ops_sw_scan_complete,
1980 	.reconfig_complete	= rtw89_ops_reconfig_complete,
1981 	.hw_scan		= rtw89_ops_hw_scan,
1982 	.cancel_hw_scan		= rtw89_ops_cancel_hw_scan,
1983 	.add_chanctx		= rtw89_ops_add_chanctx,
1984 	.remove_chanctx		= rtw89_ops_remove_chanctx,
1985 	.change_chanctx		= rtw89_ops_change_chanctx,
1986 	.assign_vif_chanctx	= rtw89_ops_assign_vif_chanctx,
1987 	.unassign_vif_chanctx	= rtw89_ops_unassign_vif_chanctx,
1988 	.switch_vif_chanctx	= rtw89_ops_switch_vif_chanctx,
1989 	.channel_switch_beacon	= rtw89_ops_channel_switch_beacon,
1990 	.remain_on_channel		= rtw89_ops_remain_on_channel,
1991 	.cancel_remain_on_channel	= rtw89_ops_cancel_remain_on_channel,
1992 	.set_sar_specs		= rtw89_ops_set_sar_specs,
1993 	.link_sta_rc_update	= rtw89_ops_sta_rc_update,
1994 	.set_tid_config		= rtw89_ops_set_tid_config,
1995 	.can_activate_links	= rtw89_ops_can_activate_links,
1996 	.change_vif_links	= rtw89_ops_change_vif_links,
1997 	.change_sta_links	= rtw89_ops_change_sta_links,
1998 #ifdef CONFIG_PM
1999 	.suspend		= rtw89_ops_suspend,
2000 	.resume			= rtw89_ops_resume,
2001 	.set_wakeup		= rtw89_ops_set_wakeup,
2002 	.set_rekey_data		= rtw89_set_rekey_data,
2003 #endif
2004 	.rfkill_poll		= rtw89_ops_rfkill_poll,
2005 };
2006 EXPORT_SYMBOL(rtw89_ops);
2007