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